5
* The JTS Topology Suite is a collection of Java classes that
6
* implement the fundamental operations required to validate a given
7
* geo-spatial data set to a known topological specification.
9
* Copyright (C) 2001 Vivid Solutions
11
* This library is free software; you can redistribute it and/or
12
* modify it under the terms of the GNU Lesser General Public
13
* License as published by the Free Software Foundation; either
14
* version 2.1 of the License, or (at your option) any later version.
16
* This library is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
* Lesser General Public License for more details.
21
* You should have received a copy of the GNU Lesser General Public
22
* License along with this library; if not, write to the Free Software
23
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
* For more information, contact:
29
* 2328 Government Street
34
* www.vividsolutions.com
36
package com.vividsolutions.jts.geomgraph;
38
import java.io.PrintStream;
40
import com.vividsolutions.jts.geom.Coordinate;
41
import com.vividsolutions.jts.util.Debug;
44
* A list of edge intersections along an Edge
47
public class EdgeIntersectionList
49
// a List of EdgeIntersections
50
//List list = new ArrayList(); // more efficient to use a LinkedList, but ArrayList is easier for debugging
51
private Map nodeMap = new TreeMap();
52
Edge edge; // the parent edge
54
public EdgeIntersectionList(Edge edge)
60
* Adds an intersection into the list, if it isn't already there.
61
* The input segmentIndex and dist are expected to be normalized.
62
* @return the EdgeIntersection found or added
64
public EdgeIntersection add(Coordinate intPt, int segmentIndex, double dist)
66
EdgeIntersection eiNew = new EdgeIntersection(intPt, segmentIndex, dist);
67
Object obj = nodeMap.get(eiNew);
68
EdgeIntersection ei = (EdgeIntersection) nodeMap.get(eiNew);
72
nodeMap.put(eiNew, eiNew);
76
public EdgeIntersection add(Coordinate intPt, int segmentIndex, double dist)
78
//Debug.println("adding edgeInt " + intPt + " " + segmentIndex + " " + dist);
79
ListIterator insertIt = list.listIterator();
80
boolean isInList = findInsertionPoint(segmentIndex, dist, insertIt);
83
ei = new EdgeIntersection(intPt, segmentIndex, dist);
87
ei = (EdgeIntersection) insertIt.next();
92
* returns an iterator of EdgeIntersections
94
public Iterator iterator() { return nodeMap.values().iterator(); }
96
public boolean isEmpty()
98
Iterator it = list.iterator();
99
return ! it.hasNext();
103
* This routine searches the list for the insertion point for the given intersection
104
* (which must be in normalized form).
105
* The intersection point may already be in the list - in this case, the intersection
107
* If the intersection is new, it is inserted into the list.
108
* The insertIt iterator is left pointing at the correct place
109
* to insert the intersection, if the intersection was not found.
111
* @return true if this intersection is already in the list
114
boolean findInsertionPoint(int segmentIndex, double dist, ListIterator insertIt)
116
// The insertIt position trails the findIt position by one
117
ListIterator findIt = list.listIterator();
118
boolean found = false;
119
while (findIt.hasNext()) {
120
EdgeIntersection ei = (EdgeIntersection) findIt.next();
121
int compare = ei.compare(segmentIndex, dist);
123
// intersection found - insertIt.next() will retrieve it
124
if (compare == 0) return true;
126
// this ei is past the intersection location, so intersection was not found
127
if (compare > 0) return false;
129
// this ei was before the intersection point, so move to next
135
public boolean isIntersection(Coordinate pt)
137
for (Iterator it = iterator(); it.hasNext(); ) {
138
EdgeIntersection ei = (EdgeIntersection) it.next();
139
if (ei.coord.equals(pt))
146
* Adds entries for the first and last points of the edge to the list
148
public void addEndpoints()
150
int maxSegIndex = edge.pts.length - 1;
151
add(edge.pts[0], 0, 0.0);
152
add(edge.pts[maxSegIndex], maxSegIndex, 0.0);
156
* Creates new edges for all the edges that the intersections in this
157
* list split the parent edge into.
158
* Adds the edges to the input list (this is so a single list
159
* can be used to accumulate all split edges for a Geometry).
161
public void addSplitEdges(List edgeList)
163
// ensure that the list has entries for the first and last point of the edge
166
Iterator it = iterator();
167
// there should always be at least two entries in the list
168
EdgeIntersection eiPrev = (EdgeIntersection) it.next();
169
while (it.hasNext()) {
170
EdgeIntersection ei = (EdgeIntersection) it.next();
171
Edge newEdge = createSplitEdge(eiPrev, ei);
172
edgeList.add(newEdge);
178
* Create a new "split edge" with the section of points between
179
* (and including) the two intersections.
180
* The label for the new edge is the same as the label for the parent edge.
182
Edge createSplitEdge(EdgeIntersection ei0, EdgeIntersection ei1)
184
//Debug.print("\ncreateSplitEdge"); Debug.print(ei0); Debug.print(ei1);
185
int npts = ei1.segmentIndex - ei0.segmentIndex + 2;
187
Coordinate lastSegStartPt = edge.pts[ei1.segmentIndex];
188
// if the last intersection point is not equal to the its segment start pt,
189
// add it to the points list as well.
190
// (This check is needed because the distance metric is not totally reliable!)
191
// The check for point equality is 2D only - Z values are ignored
192
boolean useIntPt1 = ei1.dist > 0.0 || ! ei1.coord.equals2D(lastSegStartPt);
197
Coordinate[] pts = new Coordinate[npts];
199
pts[ipt++] = new Coordinate(ei0.coord);
200
for (int i = ei0.segmentIndex + 1; i <= ei1.segmentIndex; i++) {
201
pts[ipt++] = edge.pts[i];
203
if (useIntPt1) pts[ipt] = ei1.coord;
204
return new Edge(pts, new Label(edge.label));
207
public void print(PrintStream out)
209
out.println("Intersections:");
210
for (Iterator it = iterator(); it.hasNext(); ) {
211
EdgeIntersection ei = (EdgeIntersection) it.next();