Class Node

  1  
  2 /*
  3  * Copyright (c) 2016 Vivid Solutions.
  4  *
  5  * All rights reserved. This program and the accompanying materials
  6  * are made available under the terms of the Eclipse Public License 2.0
  7  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
  9  * and the Eclipse Distribution License is available at
 10  *
 11  * http://www.eclipse.org/org/documents/edl-v10.php.
 12  */
 13  
 14  
 15 package org.locationtech.jts.planargraph;
 16  
 17 import java.util.Collection;
 18 import java.util.HashSet;
 19 import java.util.List;
 20 import java.util.Set;
 21  
 22 import org.locationtech.jts.geom.Coordinate;
 23  
 24 /**
 25  * A node in a {@link PlanarGraph}is a location where 0 or more {@link Edge}s
 26  * meet. A node is connected to each of its incident Edges via an outgoing
 27  * DirectedEdge. Some clients using a <code>PlanarGraph</code> may want to
 28  * subclass <code>Node</code> to add their own application-specific
 29  * data and methods.
 30  *
 31  * @version 1.7
 32  */
 33 public class Node
 34     extends GraphComponent
 35 {
 36   /**
 37    * Returns all Edges that connect the two nodes (which are assumed to be different).
 38    */
 39   public static Collection getEdgesBetween(Node node0, Node node1)
 40   {
 41     List edges0 = DirectedEdge.toEdges(node0.getOutEdges().getEdges());
 42     Set commonEdges = new HashSet(edges0);
 43     List edges1 = DirectedEdge.toEdges(node1.getOutEdges().getEdges());
 44     commonEdges.retainAll(edges1);
 45     return commonEdges;
 46   }
 47  
 48   /** The location of this Node */
 49   protected Coordinate pt;
 50  
 51   /** The collection of DirectedEdges that leave this Node */
 52   protected DirectedEdgeStar deStar;
 53  
 54   /**
 55    * Constructs a Node with the given location.
 56    */
 57   public Node(Coordinate pt)
 58   {
 59     this(pt, new DirectedEdgeStar());
 60   }
 61  
 62   /**
 63    * Constructs a Node with the given location and collection of outgoing DirectedEdges.
 64    */
 65   public Node(Coordinate pt, DirectedEdgeStar deStar)
 66   {
 67     this.pt = pt;
 68     this.deStar = deStar;
 69   }
 70  
 71   /**
 72    * Returns the location of this Node.
 73    */
 74   public Coordinate getCoordinate() { return pt; }
 75  
 76   /**
 77    * Adds an outgoing DirectedEdge to this Node.
 78    */
 79   public void addOutEdge(DirectedEdge de)
 80   {
 81     deStar.add(de);
 82   }
 83  
 84   /**
 85    * Returns the collection of DirectedEdges that leave this Node.
 86    */
 87   public DirectedEdgeStar getOutEdges() { return deStar; }
 88   /**
 89    * Returns the number of edges around this Node.
 90    */
 91   public int getDegree() { return deStar.getDegree(); }
 92   /**
 93    * Returns the zero-based index of the given Edge, after sorting in ascending order
 94    * by angle with the positive x-axis.
 95    */
 96   public int getIndex(Edge edge)
 97   {
 98     return deStar.getIndex(edge);
 99   }
100  
101   /**
102    * Removes a {@link DirectedEdge} incident on this node.
103    * Does not change the state of the directed edge.
104    */
105   public void remove(DirectedEdge de)
106   {
107     deStar.remove(de);
108   }
109  
110   /**
111    * Removes this node from its containing graph.
112    */
113   void remove() {
114     pt = null;
115   }
116  
117  
118   /**
119    * Tests whether this node has been removed from its containing graph
120    *
121    * @return <code>true</code> if this node is removed
122    */
123   public boolean isRemoved()
124   {
125     return pt == null;
126   }
127  
128 }
129