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:
034: package com.vividsolutions.jts.planargraph;
035:
036: import java.util.*;
037: import com.vividsolutions.jts.geom.Coordinate;
038:
039: /**
040: * A node in a {@link PlanarGraph}is a location where 0 or more {@link Edge}s
041: * meet. A node is connected to each of its incident Edges via an outgoing
042: * DirectedEdge. Some clients using a <code>PlanarGraph</code> may want to
043: * subclass <code>Node</code> to add their own application-specific
044: * data and methods.
045: *
046: * @version 1.7
047: */
048: public class Node extends GraphComponent {
049: /**
050: * Returns all Edges that connect the two nodes (which are assumed to be different).
051: */
052: public static Collection getEdgesBetween(Node node0, Node node1) {
053: List edges0 = DirectedEdge.toEdges(node0.getOutEdges()
054: .getEdges());
055: Set commonEdges = new HashSet(edges0);
056: List edges1 = DirectedEdge.toEdges(node1.getOutEdges()
057: .getEdges());
058: commonEdges.retainAll(edges1);
059: return commonEdges;
060: }
061:
062: /** The location of this Node */
063: protected Coordinate pt;
064:
065: /** The collection of DirectedEdges that leave this Node */
066: protected DirectedEdgeStar deStar;
067:
068: /**
069: * Constructs a Node with the given location.
070: */
071: public Node(Coordinate pt) {
072: this (pt, new DirectedEdgeStar());
073: }
074:
075: /**
076: * Constructs a Node with the given location and collection of outgoing DirectedEdges.
077: */
078: public Node(Coordinate pt, DirectedEdgeStar deStar) {
079: this .pt = pt;
080: this .deStar = deStar;
081: }
082:
083: /**
084: * Returns the location of this Node.
085: */
086: public Coordinate getCoordinate() {
087: return pt;
088: }
089:
090: /**
091: * Adds an outgoing DirectedEdge to this Node.
092: */
093: public void addOutEdge(DirectedEdge de) {
094: deStar.add(de);
095: }
096:
097: /**
098: * Returns the collection of DirectedEdges that leave this Node.
099: */
100: public DirectedEdgeStar getOutEdges() {
101: return deStar;
102: }
103:
104: /**
105: * Returns the number of edges around this Node.
106: */
107: public int getDegree() {
108: return deStar.getDegree();
109: }
110:
111: /**
112: * Returns the zero-based index of the given Edge, after sorting in ascending order
113: * by angle with the positive x-axis.
114: */
115: public int getIndex(Edge edge) {
116: return deStar.getIndex(edge);
117: }
118:
119: /**
120: * Removes this node from its containing graph.
121: */
122: void remove() {
123: pt = null;
124: }
125:
126: /**
127: * Tests whether this node has been removed from its containing graph
128: *
129: * @return <code>true</code> if this node is removed
130: */
131: public boolean isRemoved() {
132: return pt == null;
133: }
134:
135: }
|