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.planargraph;
034:
035: import java.util.*;
036: import com.vividsolutions.jts.geom.Coordinate;
037:
038: /**
039: * Represents a directed graph which is embeddable in a planar surface.
040: * <p>
041: * This class and the other classes in this package serve as a framework for
042: * building planar graphs for specific algorithms. This class must be
043: * subclassed to expose appropriate methods to construct the graph. This allows
044: * controlling the types of graph components ({@link DirectedEdge}s,
045: * {@link Edge}s and {@link Node}s) which can be added to the graph. An
046: * application which uses the graph framework will almost always provide
047: * subclasses for one or more graph components, which hold application-specific
048: * data and graph algorithms.
049: *
050: * @version 1.7
051: */
052: public abstract class PlanarGraph {
053: protected Set edges = new HashSet();
054: protected Set dirEdges = new HashSet();
055: protected NodeMap nodeMap = new NodeMap();
056:
057: /**
058: * Constructs a empty graph.
059: */
060: public PlanarGraph() {
061: }
062:
063: /**
064: * Returns the {@link Node} at the given location,
065: * or null if no {@link Node} was there.
066: *
067: * @param pt the location to query
068: * @return the node found
069: * @return <code>null</code> if this graph contains no node at the location
070: */
071: public Node findNode(Coordinate pt) {
072: return (Node) nodeMap.find(pt);
073: }
074:
075: /**
076: * Adds a node to the map, replacing any that is already at that location.
077: * Only subclasses can add Nodes, to ensure Nodes are of the right type.
078: * @return the added node
079: */
080: protected void add(Node node) {
081: nodeMap.add(node);
082: }
083:
084: /**
085: * Adds the Edge and its DirectedEdges with this PlanarGraph.
086: * Assumes that the Edge has already been created with its associated DirectEdges.
087: * Only subclasses can add Edges, to ensure the edges added are of the right class.
088: */
089: protected void add(Edge edge) {
090: edges.add(edge);
091: add(edge.getDirEdge(0));
092: add(edge.getDirEdge(1));
093: }
094:
095: /**
096: * Adds the Edge to this PlanarGraph; only subclasses can add DirectedEdges,
097: * to ensure the edges added are of the right class.
098: */
099: protected void add(DirectedEdge dirEdge) {
100: dirEdges.add(dirEdge);
101: }
102:
103: /**
104: * Returns an Iterator over the Nodes in this PlanarGraph.
105: */
106: public Iterator nodeIterator() {
107: return nodeMap.iterator();
108: }
109:
110: /**
111: * Returns the Nodes in this PlanarGraph.
112: */
113:
114: /**
115: * Tests whether this graph contains the given {@link Edge}
116: *
117: * @param e the edge to query
118: * @return <code>true</code> if the graph contains the edge
119: */
120: public boolean contains(Edge e) {
121: return edges.contains(e);
122: }
123:
124: /**
125: * Tests whether this graph contains the given {@link DirectedEdge}
126: *
127: * @param de the directed edge to query
128: * @return <code>true</code> if the graph contains the directed edge
129: */
130: public boolean contains(DirectedEdge de) {
131: return dirEdges.contains(de);
132: }
133:
134: public Collection getNodes() {
135: return nodeMap.values();
136: }
137:
138: /**
139: * Returns an Iterator over the DirectedEdges in this PlanarGraph, in the order in which they
140: * were added.
141: *
142: * @see #add(Edge)
143: * @see #add(DirectedEdge)
144: */
145: public Iterator dirEdgeIterator() {
146: return dirEdges.iterator();
147: }
148:
149: /**
150: * Returns an Iterator over the Edges in this PlanarGraph, in the order in which they
151: * were added.
152: *
153: * @see #add(Edge)
154: */
155: public Iterator edgeIterator() {
156: return edges.iterator();
157: }
158:
159: /**
160: * Returns the Edges that have been added to this PlanarGraph
161: * @see #add(Edge)
162: */
163: public Collection getEdges() {
164: return edges;
165: }
166:
167: /**
168: * Removes an {@link Edge} and its associated {@link DirectedEdge}s
169: * from their from-Nodes and from the graph.
170: * Note: This method does not remove the {@link Node}s associated
171: * with the {@link Edge}, even if the removal of the {@link Edge}
172: * reduces the degree of a {@link Node} to zero.
173: */
174: public void remove(Edge edge) {
175: remove(edge.getDirEdge(0));
176: remove(edge.getDirEdge(1));
177: edges.remove(edge);
178: edge.remove();
179: }
180:
181: /**
182: * Removes DirectedEdge from its from-Node and from this PlanarGraph.
183: * This method does not remove the Nodes associated with the DirectedEdge,
184: * even if the removal of the DirectedEdge reduces the degree of a Node to
185: * zero.
186: */
187: public void remove(DirectedEdge de) {
188: DirectedEdge sym = de.getSym();
189: if (sym != null)
190: sym.setSym(null);
191: de.getFromNode().getOutEdges().remove(de);
192: de.remove();
193: dirEdges.remove(de);
194: }
195:
196: /**
197: * Removes a node from the graph, along with any associated DirectedEdges and
198: * Edges.
199: */
200: public void remove(Node node) {
201: // unhook all directed edges
202: List outEdges = node.getOutEdges().getEdges();
203: for (Iterator i = outEdges.iterator(); i.hasNext();) {
204: DirectedEdge de = (DirectedEdge) i.next();
205: DirectedEdge sym = de.getSym();
206: // remove the diredge that points to this node
207: if (sym != null)
208: remove(sym);
209: // remove this diredge from the graph collection
210: dirEdges.remove(de);
211:
212: Edge edge = de.getEdge();
213: if (edge != null) {
214: edges.remove(edge);
215: }
216:
217: }
218: // remove the node from the graph
219: nodeMap.remove(node.getCoordinate());
220: node.remove();
221: }
222:
223: /**
224: * Returns all Nodes with the given number of Edges around it.
225: */
226: public List findNodesOfDegree(int degree) {
227: List nodesFound = new ArrayList();
228: for (Iterator i = nodeIterator(); i.hasNext();) {
229: Node node = (Node) i.next();
230: if (node.getDegree() == degree)
231: nodesFound.add(node);
232: }
233: return nodesFound;
234: }
235:
236: }
|