Class InteriorIntersectionFinderAdder

 1 /*
 2  * Copyright (c) 2016 Vivid Solutions.
 3  *
 4  * All rights reserved. This program and the accompanying materials
 5  * are made available under the terms of the Eclipse Public License 2.0
 6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 8  * and the Eclipse Distribution License is available at
 9  *
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  */
12 package org.locationtech.jts.noding;
13  
14 import java.util.ArrayList;
15 import java.util.List;
16  
17 import org.locationtech.jts.algorithm.LineIntersector;
18 import org.locationtech.jts.geom.Coordinate;
19  
20 /**
21  * Finds <b>interior</b> intersections between line segments in {@link NodedSegmentString}s,
22  * and adds them as nodes
23  * using {@link NodedSegmentString#addIntersection(LineIntersector, int, int, int)}.
24  * <p>
25  * This class is used primarily for Snap-Rounding.  
26  * For general-purpose noding, use {@link IntersectionAdder}.
27  *
28  * @version 1.7
29  * @see IntersectionAdder
30  */
31 public class InteriorIntersectionFinderAdder
32     implements SegmentIntersector
33 {
34   private LineIntersector li;
35   private final List interiorIntersections;
36  
37  
38   /**
39    * Creates an intersection finder which finds all proper intersections
40    *
41    * @param li the LineIntersector to use
42    */
43   public InteriorIntersectionFinderAdder(LineIntersector li)
44   {
45     this.li = li;
46     interiorIntersections = new ArrayList();
47   }
48  
49   public List getInteriorIntersections()  {    return interiorIntersections;  }
50  
51   /**
52    * This method is called by clients
53    * of the {@link SegmentIntersector} class to process
54    * intersections for two segments of the {@link SegmentString}s being intersected.
55    * Note that some clients (such as <code>MonotoneChain</code>s) may optimize away
56    * this call for segment pairs which they have determined do not intersect
57    * (e.g. by an disjoint envelope test).
58    */
59   public void processIntersections(
60       SegmentString e0,  int segIndex0,
61       SegmentString e1,  int segIndex1
62       )
63   {
64     // don't bother intersecting a segment with itself
65     if (e0 == e1 && segIndex0 == segIndex1) return;
66  
67     Coordinate p00 = e0.getCoordinates()[segIndex0];
68     Coordinate p01 = e0.getCoordinates()[segIndex0 + 1];
69     Coordinate p10 = e1.getCoordinates()[segIndex1];
70     Coordinate p11 = e1.getCoordinates()[segIndex1 + 1];
71  
72     li.computeIntersection(p00, p01, p10, p11);
73 //if (li.hasIntersection() && li.isProper()) Debug.println(li);
74  
75     if (li.hasIntersection()) {
76       if (li.isInteriorIntersection()) {
77         for (int intIndex = 0; intIndex < li.getIntersectionNum(); intIndex++) {
78           interiorIntersections.add(li.getIntersection(intIndex));
79         }
80         ((NodedSegmentString) e0).addIntersections(li, segIndex0, 0);
81         ((NodedSegmentString) e1).addIntersections(li, segIndex1, 1);
82       }
83     }
84   }
85   
86   /**
87    * Always process all intersections
88    * 
89    * @return false always
90    */
91   public boolean isDone() { return false; }
92  
93 }
94