Class Edge

  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 package org.locationtech.jts.planargraph;
 14  
 15 /**
 16  * Represents an undirected edge of a {@link PlanarGraph}. An undirected edge
 17  * in fact simply acts as a central point of reference for two opposite
 18  * {@link DirectedEdge}s.
 19  * <p>
 20  * Usually a client using a <code>PlanarGraph</code> will subclass <code>Edge</code>
 21  * to add its own application-specific data and methods.
 22  *
 23  * @version 1.7
 24  */
 25 public class Edge
 26     extends GraphComponent
 27 {
 28  
 29   /**
 30    * The two DirectedEdges associated with this Edge.
 31    * Index 0 is forward, 1 is reverse.
 32    */
 33   protected DirectedEdge[] dirEdge;
 34  
 35   /**
 36    * Constructs an Edge whose DirectedEdges are not yet set. Be sure to call
 37    * {@link #setDirectedEdges(DirectedEdge, DirectedEdge)}
 38    */
 39   public Edge()
 40   {
 41   }
 42  
 43   /**
 44    * Constructs an Edge initialized with the given DirectedEdges, and for each
 45    * DirectedEdge: sets the Edge, sets the symmetric DirectedEdge, and adds
 46    * this Edge to its from-Node.
 47    */
 48   public Edge(DirectedEdge de0, DirectedEdge de1)
 49   {
 50     setDirectedEdges(de0, de1);
 51   }
 52  
 53   /**
 54    * Initializes this Edge's two DirectedEdges, and for each DirectedEdge: sets the
 55    * Edge, sets the symmetric DirectedEdge, and adds this Edge to its from-Node.
 56    */
 57   public void setDirectedEdges(DirectedEdge de0, DirectedEdge de1)
 58   {
 59     dirEdge = new DirectedEdge[] { de0, de1 };
 60     de0.setEdge(this);
 61     de1.setEdge(this);
 62     de0.setSym(de1);
 63     de1.setSym(de0);
 64     de0.getFromNode().addOutEdge(de0);
 65     de1.getFromNode().addOutEdge(de1);
 66   }
 67  
 68   /**
 69    * Returns one of the DirectedEdges associated with this Edge.
 70    * @param i 0 or 1.  0 returns the forward directed edge, 1 returns the reverse
 71    */
 72   public DirectedEdge getDirEdge(int i)
 73   {
 74     return dirEdge[i];
 75   }
 76  
 77   /**
 78    * Returns the {@link DirectedEdge} that starts from the given node, or null if the
 79    * node is not one of the two nodes associated with this Edge.
 80    */
 81   public DirectedEdge getDirEdge(Node fromNode)
 82   {
 83     if (dirEdge[0].getFromNode() == fromNode) return dirEdge[0];
 84     if (dirEdge[1].getFromNode() == fromNode) return dirEdge[1];
 85     // node not found
 86     // possibly should throw an exception here?
 87     return null;
 88   }
 89  
 90   /**
 91    * If <code>node</code> is one of the two nodes associated with this Edge,
 92    * returns the other node; otherwise returns null.
 93    */
 94   public Node getOppositeNode(Node node)
 95   {
 96     if (dirEdge[0].getFromNode() == node) return dirEdge[0].getToNode();
 97     if (dirEdge[1].getFromNode() == node) return dirEdge[1].getToNode();
 98     // node not found
 99     // possibly should throw an exception here?
100     return null;
101   }
102  
103   /**
104    * Removes this edge from its containing graph.
105    */
106   void remove() {
107     this.dirEdge = null;
108   }
109  
110   /**
111    * Tests whether this edge has been removed from its containing graph
112    *
113    * @return <code>true</code> if this edge is removed
114    */
115   public boolean isRemoved()
116   {
117     return dirEdge == null;
118   }
119  
120 }
121