001: /*
002: * The JTS Topology Suite is a collection of Java classes that
003: * implement the fundamental operations required to validate a given
004: * geo-spatial data set to a known topological specification.
005: *
006: * Copyright (C) 2001 Vivid Solutions
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this library; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: *
022: * For more information, contact:
023: *
024: * Vivid Solutions
025: * Suite #1A
026: * 2328 Government Street
027: * Victoria BC V8T 5G5
028: * Canada
029: *
030: * (250)385-6040
031: * www.vividsolutions.com
032: */
033: package com.vividsolutions.jts.geomgraph;
034:
035: /**
036: * @version 1.7
037: */
038: import java.io.PrintStream;
039: import java.util.*;
040: import com.vividsolutions.jts.util.*;
041: import com.vividsolutions.jts.algorithm.*;
042: import com.vividsolutions.jts.geom.*;
043:
044: /**
045: * The computation of the <code>IntersectionMatrix</code> relies on the use of a structure
046: * called a "topology graph". The topology graph contains nodes and edges
047: * corresponding to the nodes and line segments of a <code>Geometry</code>. Each
048: * node and edge in the graph is labeled with its topological location relative to
049: * the source geometry.
050: * <P>
051: * Note that there is no requirement that points of self-intersection be a vertex.
052: * Thus to obtain a correct topology graph, <code>Geometry</code>s must be
053: * self-noded before constructing their graphs.
054: * <P>
055: * Two fundamental operations are supported by topology graphs:
056: * <UL>
057: * <LI>Computing the intersections between all the edges and nodes of a single graph
058: * <LI>Computing the intersections between the edges and nodes of two different graphs
059: * </UL>
060: *
061: * @version 1.7
062: */
063: public class PlanarGraph {
064:
065: public static final CGAlgorithms cga = new CGAlgorithms();
066:
067: /**
068: * For nodes in the Collection, link the DirectedEdges at the node that are in the result.
069: * This allows clients to link only a subset of nodes in the graph, for
070: * efficiency (because they know that only a subset is of interest).
071: */
072: public static void linkResultDirectedEdges(Collection nodes) {
073: for (Iterator nodeit = nodes.iterator(); nodeit.hasNext();) {
074: Node node = (Node) nodeit.next();
075: ((DirectedEdgeStar) node.getEdges())
076: .linkResultDirectedEdges();
077: }
078: }
079:
080: protected List edges = new ArrayList();
081: protected NodeMap nodes;
082: protected List edgeEndList = new ArrayList();
083:
084: public PlanarGraph(NodeFactory nodeFact) {
085: nodes = new NodeMap(nodeFact);
086: }
087:
088: public PlanarGraph() {
089: nodes = new NodeMap(new NodeFactory());
090: }
091:
092: public Iterator getEdgeIterator() {
093: return edges.iterator();
094: }
095:
096: public Collection getEdgeEnds() {
097: return edgeEndList;
098: }
099:
100: public boolean isBoundaryNode(int geomIndex, Coordinate coord) {
101: Node node = nodes.find(coord);
102: if (node == null)
103: return false;
104: Label label = node.getLabel();
105: if (label != null
106: && label.getLocation(geomIndex) == Location.BOUNDARY)
107: return true;
108: return false;
109: }
110:
111: protected void insertEdge(Edge e) {
112: edges.add(e);
113: }
114:
115: public void add(EdgeEnd e) {
116: nodes.add(e);
117: edgeEndList.add(e);
118: }
119:
120: public Iterator getNodeIterator() {
121: return nodes.iterator();
122: }
123:
124: public Collection getNodes() {
125: return nodes.values();
126: }
127:
128: public Node addNode(Node node) {
129: return nodes.addNode(node);
130: }
131:
132: public Node addNode(Coordinate coord) {
133: return nodes.addNode(coord);
134: }
135:
136: /**
137: * @return the node if found; null otherwise
138: */
139: public Node find(Coordinate coord) {
140: return nodes.find(coord);
141: }
142:
143: /**
144: * Add a set of edges to the graph. For each edge two DirectedEdges
145: * will be created. DirectedEdges are NOT linked by this method.
146: */
147: public void addEdges(List edgesToAdd) {
148: // create all the nodes for the edges
149: for (Iterator it = edgesToAdd.iterator(); it.hasNext();) {
150: Edge e = (Edge) it.next();
151: edges.add(e);
152:
153: DirectedEdge de1 = new DirectedEdge(e, true);
154: DirectedEdge de2 = new DirectedEdge(e, false);
155: de1.setSym(de2);
156: de2.setSym(de1);
157:
158: add(de1);
159: add(de2);
160: }
161: }
162:
163: /**
164: * Link the DirectedEdges at the nodes of the graph.
165: * This allows clients to link only a subset of nodes in the graph, for
166: * efficiency (because they know that only a subset is of interest).
167: */
168: public void linkResultDirectedEdges() {
169: for (Iterator nodeit = nodes.iterator(); nodeit.hasNext();) {
170: Node node = (Node) nodeit.next();
171: ((DirectedEdgeStar) node.getEdges())
172: .linkResultDirectedEdges();
173: }
174: }
175:
176: /**
177: * Link the DirectedEdges at the nodes of the graph.
178: * This allows clients to link only a subset of nodes in the graph, for
179: * efficiency (because they know that only a subset is of interest).
180: */
181: public void linkAllDirectedEdges() {
182: for (Iterator nodeit = nodes.iterator(); nodeit.hasNext();) {
183: Node node = (Node) nodeit.next();
184: ((DirectedEdgeStar) node.getEdges()).linkAllDirectedEdges();
185: }
186: }
187:
188: /**
189: * Returns the EdgeEnd which has edge e as its base edge
190: * (MD 18 Feb 2002 - this should return a pair of edges)
191: *
192: * @return the edge, if found
193: * <code>null</code> if the edge was not found
194: */
195: public EdgeEnd findEdgeEnd(Edge e) {
196: for (Iterator i = getEdgeEnds().iterator(); i.hasNext();) {
197: EdgeEnd ee = (EdgeEnd) i.next();
198: if (ee.getEdge() == e)
199: return ee;
200: }
201: return null;
202: }
203:
204: /**
205: * Returns the edge whose first two coordinates are p0 and p1
206: *
207: * @return the edge, if found
208: * <code>null</code> if the edge was not found
209: */
210: public Edge findEdge(Coordinate p0, Coordinate p1) {
211: for (int i = 0; i < edges.size(); i++) {
212: Edge e = (Edge) edges.get(i);
213: Coordinate[] eCoord = e.getCoordinates();
214: if (p0.equals(eCoord[0]) && p1.equals(eCoord[1]))
215: return e;
216: }
217: return null;
218: }
219:
220: /**
221: * Returns the edge which starts at p0 and whose first segment is
222: * parallel to p1
223: *
224: * @return the edge, if found
225: * <code>null</code> if the edge was not found
226: */
227: public Edge findEdgeInSameDirection(Coordinate p0, Coordinate p1) {
228: for (int i = 0; i < edges.size(); i++) {
229: Edge e = (Edge) edges.get(i);
230:
231: Coordinate[] eCoord = e.getCoordinates();
232: if (matchInSameDirection(p0, p1, eCoord[0], eCoord[1]))
233: return e;
234:
235: if (matchInSameDirection(p0, p1, eCoord[eCoord.length - 1],
236: eCoord[eCoord.length - 2]))
237: return e;
238: }
239: return null;
240: }
241:
242: /**
243: * The coordinate pairs match if they define line segments lying in the same direction.
244: * E.g. the segments are parallel and in the same quadrant
245: * (as opposed to parallel and opposite!).
246: */
247: private boolean matchInSameDirection(Coordinate p0, Coordinate p1,
248: Coordinate ep0, Coordinate ep1) {
249: if (!p0.equals(ep0))
250: return false;
251:
252: if (CGAlgorithms.computeOrientation(p0, p1, ep1) == CGAlgorithms.COLLINEAR
253: && Quadrant.quadrant(p0, p1) == Quadrant.quadrant(ep0,
254: ep1))
255: return true;
256: return false;
257: }
258:
259: public void printEdges(PrintStream out) {
260: out.println("Edges:");
261: for (int i = 0; i < edges.size(); i++) {
262: out.println("edge " + i + ":");
263: Edge e = (Edge) edges.get(i);
264: e.print(out);
265: e.eiList.print(out);
266: }
267: }
268:
269: void debugPrint(Object o) {
270: System.out.print(o);
271: }
272:
273: void debugPrintln(Object o) {
274: System.out.println(o);
275: }
276:
277: }
|