Class DirectedEdgeStar

  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.ArrayList;
 18 import java.util.Collections;
 19 import java.util.Iterator;
 20 import java.util.List;
 21  
 22 import org.locationtech.jts.geom.Coordinate;
 23  
 24 /**
 25  * A sorted collection of {@link DirectedEdge}s which leave a {@link Node}
 26  * in a {@link PlanarGraph}.
 27  *
 28  * @version 1.7
 29  */
 30 public class DirectedEdgeStar
 31 {
 32  
 33   /**
 34    * The underlying list of outgoing DirectedEdges
 35    */
 36   protected List outEdges = new ArrayList();
 37   private boolean sorted = false;
 38  
 39   /**
 40    * Constructs a DirectedEdgeStar with no edges.
 41    */
 42   public DirectedEdgeStar() {
 43   }
 44   /**
 45    * Adds a new member to this DirectedEdgeStar.
 46    */
 47   public void add(DirectedEdge de)
 48   {
 49     outEdges.add(de);
 50     sorted = false;
 51   }
 52   /**
 53    * Drops a member of this DirectedEdgeStar.
 54    */
 55   public void remove(DirectedEdge de)
 56   {
 57     outEdges.remove(de);
 58   }
 59   /**
 60    * Returns an Iterator over the DirectedEdges, in ascending order by angle with the positive x-axis.
 61    */
 62   public Iterator iterator()
 63   {
 64     sortEdges();
 65     return outEdges.iterator();
 66   }
 67  
 68   /**
 69    * Returns the number of edges around the Node associated with this DirectedEdgeStar.
 70    */
 71   public int getDegree() { return outEdges.size(); }
 72  
 73   /**
 74    * Returns the coordinate for the node at which this star is based
 75    */
 76   public Coordinate getCoordinate()
 77   {
 78     Iterator it = iterator();
 79     if (! it.hasNext()) return null;
 80     DirectedEdge e = (DirectedEdge) it.next();
 81     return e.getCoordinate();
 82   }
 83  
 84   /**
 85    * Returns the DirectedEdges, in ascending order by angle with the positive x-axis.
 86    */
 87   public List getEdges()
 88   {
 89     sortEdges();
 90     return outEdges;
 91   }
 92  
 93   private void sortEdges()
 94   {
 95     if (! sorted) {
 96       Collections.sort(outEdges);
 97       sorted = true;
 98     }
 99   }
100   /**
101    * Returns the zero-based index of the given Edge, after sorting in ascending order
102    * by angle with the positive x-axis.
103    */
104   public int getIndex(Edge edge)
105   {
106     sortEdges();
107     for (int i = 0; i < outEdges.size(); i++) {
108       DirectedEdge de = (DirectedEdge) outEdges.get(i);
109       if (de.getEdge() == edge)
110         return i;
111     }
112     return -1;
113   }
114   /**
115    * Returns the zero-based index of the given DirectedEdge, after sorting in ascending order
116    * by angle with the positive x-axis.
117    */  
118   public int getIndex(DirectedEdge dirEdge)
119   {
120     sortEdges();
121     for (int i = 0; i < outEdges.size(); i++) {
122       DirectedEdge de = (DirectedEdge) outEdges.get(i);
123       if (de == dirEdge)
124         return i;
125     }
126     return -1;
127   }
128   /**
129    * Returns value of i modulo the number of edges in this DirectedEdgeStar
130    * (i.e. the remainder when i is divided by the number of edges)
131    * 
132    * @param i an integer (positive, negative or zero)
133    */
134   public int getIndex(int i)
135   {
136     int modi = i % outEdges.size();
137     //I don't think modi can be 0 (assuming i is positive) [Jon Aquino 10/28/2003] 
138     if (modi < 0modi += outEdges.size();
139     return modi;
140   }
141  
142   /**
143    * Returns the {@link DirectedEdge} on the left-hand (CCW) 
144    * side of the given {@link DirectedEdge} 
145    * (which must be a member of this DirectedEdgeStar). 
146    */
147   public DirectedEdge getNextEdge(DirectedEdge dirEdge)
148   {
149     int i = getIndex(dirEdge);
150     return (DirectedEdge) outEdges.get(getIndex(i + 1));
151   }
152   
153   /**
154    * Returns the {@link DirectedEdge} on the right-hand (CW) 
155    * side of the given {@link DirectedEdge} 
156    * (which must be a member of this DirectedEdgeStar). 
157    */
158   public DirectedEdge getNextCWEdge(DirectedEdge dirEdge)
159   {
160     int i = getIndex(dirEdge);
161     return (DirectedEdge) outEdges.get(getIndex(i - 1));
162   }
163 }
164