/** * Returns 1 if this DirectedEdge has a greater angle with the * positive x-axis than b", 0 if the DirectedEdges are collinear, and -1 otherwise. * <p> * Using the obvious algorithm of simply computing the angle is not robust, * since the angle calculation is susceptible to roundoff. A robust algorithm * is: * <ul> * <li>first compare the quadrants. If the quadrants are different, it it * trivial to determine which vector is "greater". * <li>if the vectors lie in the same quadrant, the robust * {@link CGAlgorithms#computeOrientation(Coordinate, Coordinate, Coordinate)} * function can be used to decide the relative orientation of the vectors. * </ul> */ public int compareTo(Object obj) { DirectedEdge de = (DirectedEdge) obj; return compareDirection(de); }
/** * Returns the coordinate for the node at wich this star is based */ public Coordinate getCoordinate() { Iterator it = iterator(); if (! it.hasNext()) return null; DirectedEdge e = (DirectedEdge) it.next(); return e.getCoordinate(); }
private void addReverseSubpath(DirectedEdge de, ListIterator lit, boolean expectedClosed) { // trace an unvisited path *backwards* from this de Node endNode = de.getToNode(); Node fromNode = null; while (true) { lit.add(de.getSym()); de.getEdge().setVisited(true); fromNode = de.getFromNode(); DirectedEdge unvisitedOutDE = findUnvisitedBestOrientedDE(fromNode); // this must terminate, since we are continually marking edges as visited if (unvisitedOutDE == null) break; de = unvisitedOutDE.getSym(); } if (expectedClosed) { // the path should end at the toNode of this de, otherwise we have an error Assert.isTrue(fromNode == endNode, "path not contiguous"); } }
/** * If <code>node</code> is one of the two nodes associated with this Edge, * returns the other node; otherwise returns null. */ public Node getOppositeNode(Node node) { if (dirEdge[0].getFromNode() == node) return dirEdge[0].getToNode(); if (dirEdge[1].getFromNode() == node) return dirEdge[1].getToNode(); // node not found // possibly should throw an exception here? return null; }
/** * Removes a {@link DirectedEdge} from its from-{@link Node} and from this graph. * This method does not remove the {@link Node}s associated with the DirectedEdge, * even if the removal of the DirectedEdge reduces the degree of a Node to zero. */ public void remove(DirectedEdge de) { DirectedEdge sym = de.getSym(); if (sym != null) sym.setSym(null); de.getFromNode().remove(de); de.remove(); dirEdges.remove(de); }
/** * Initializes this Edge's two DirectedEdges, and for each DirectedEdge: sets the * Edge, sets the symmetric DirectedEdge, and adds this Edge to its from-Node. */ public void setDirectedEdges(DirectedEdge de0, DirectedEdge de1) { dirEdge = new DirectedEdge[] { de0, de1 }; de0.setEdge(this); de1.setEdge(this); de0.setSym(de1); de1.setSym(de0); de0.getFromNode().addOutEdge(de0); de1.getFromNode().addOutEdge(de1); }
/** * Finds an {@link DirectedEdge} for an unvisited edge (if any), * choosing the dirEdge which preserves orientation, if possible. * * @param node the node to examine * @return the dirEdge found, or <code>null</code> if none were unvisited */ private static DirectedEdge findUnvisitedBestOrientedDE(Node node) { DirectedEdge wellOrientedDE = null; DirectedEdge unvisitedDE = null; for (Iterator i = node.getOutEdges().iterator(); i.hasNext(); ) { DirectedEdge de = (DirectedEdge) i.next(); if (! de.getEdge().isVisited()) { unvisitedDE = de; if (de.getEdgeDirection()) wellOrientedDE = de; } } if (wellOrientedDE != null) return wellOrientedDE; return unvisitedDE; }
Node startNode = startEdge.getFromNode(); Node endNode = endEdge.getToNode(); if (endEdge.getToNode().getDegree() == 1 && endEdge.getEdgeDirection() == false) { hasObviousStartNode = true; flipSeq = true; if (startEdge.getFromNode().getDegree() == 1 && startEdge.getEdgeDirection() == true) { hasObviousStartNode = true; flipSeq = false; if (startEdge.getFromNode().getDegree() == 1) flipSeq = true;
/** * Adds the argument node and all its out edges to the subgraph. * @param node the node to add * @param nodeStack the current set of nodes being traversed */ private void addEdges(Node node, Stack nodeStack, Subgraph subgraph) { node.setVisited(true); for (Iterator i = ((DirectedEdgeStar) node.getOutEdges()).iterator(); i.hasNext(); ) { DirectedEdge de = (DirectedEdge) i.next(); subgraph.add(de.getEdge()); Node toNode = de.getToNode(); if (! toNode.isVisited()) nodeStack.push(toNode); } }
private List findSequence(Subgraph graph) { GraphComponent.setVisited(graph.edgeIterator(), false); Node startNode = findLowestDegreeNode(graph); DirectedEdge startDE = (DirectedEdge) startNode.getOutEdges().iterator().next(); DirectedEdge startDESym = startDE.getSym(); List seq = new LinkedList(); ListIterator lit = seq.listIterator(); addReverseSubpath(startDESym, lit, false); while (lit.hasPrevious()) { DirectedEdge prev = (DirectedEdge) lit.previous(); DirectedEdge unvisitedOutDE = findUnvisitedBestOrientedDE(prev.getFromNode()); if (unvisitedOutDE != null) addReverseSubpath(unvisitedOutDE.getSym(), lit, true); } /** * At this point, we have a valid sequence of graph DirectedEdges, but it * is not necessarily appropriately oriented relative to the underlying * geometry. */ List orientedSeq = orient(seq); return orientedSeq; }
/** * Removes a node from the graph, along with any associated DirectedEdges and * Edges. */ public void remove(Node node) { // unhook all directed edges List outEdges = node.getOutEdges().getEdges(); for (Iterator i = outEdges.iterator(); i.hasNext(); ) { DirectedEdge de = (DirectedEdge) i.next(); DirectedEdge sym = de.getSym(); // remove the diredge that points to this node if (sym != null) remove(sym); // remove this diredge from the graph collection dirEdges.remove(de); Edge edge = de.getEdge(); if (edge != null) { edges.remove(edge); } } // remove the node from the graph nodeMap.remove(node.getCoordinate()); node.remove(); }
/** * Returns the {@link DirectedEdge} that starts from the given node, or null if the * node is not one of the two nodes associated with this Edge. */ public DirectedEdge getDirEdge(Node fromNode) { if (dirEdge[0].getFromNode() == fromNode) return dirEdge[0]; if (dirEdge[1].getFromNode() == fromNode) return dirEdge[1]; // node not found // possibly should throw an exception here? return null; }
/** * Returns the zero-based index of the given Edge, after sorting in ascending order * by angle with the positive x-axis. */ public int getIndex(Edge edge) { sortEdges(); for (int i = 0; i < outEdges.size(); i++) { DirectedEdge de = (DirectedEdge) outEdges.get(i); if (de.getEdge() == edge) return i; } return -1; } /**
/** * Reverse the sequence. * This requires reversing the order of the dirEdges, and flipping * each dirEdge as well * * @param seq a List of DirectedEdges, in sequential order * @return the reversed sequence */ private List reverse(List seq) { LinkedList newSeq = new LinkedList(); for (Iterator i = seq.iterator(); i.hasNext(); ) { DirectedEdge de = (DirectedEdge) i.next(); newSeq.addFirst(de.getSym()); } return newSeq; }
/** * Removes a {@link DirectedEdge} from its from-{@link Node} and from this graph. * This method does not remove the {@link Node}s associated with the DirectedEdge, * even if the removal of the DirectedEdge reduces the degree of a Node to zero. */ public void remove(DirectedEdge de) { DirectedEdge sym = de.getSym(); if (sym != null) sym.setSym(null); de.getFromNode().remove(de); de.remove(); dirEdges.remove(de); }
/** * Builds a geometry ({@link LineString} or {@link MultiLineString} ) * representing the sequence. * * @param sequences a List of Lists of DirectedEdges with * LineMergeEdges as their parent edges. * @return the sequenced geometry, or <code>null</code> if no sequence exists */ private Geometry buildSequencedGeometry(List sequences) { List lines = new ArrayList(); for (Iterator i1 = sequences.iterator(); i1.hasNext(); ) { List seq = (List) i1.next(); for (Iterator i2 = seq.iterator(); i2.hasNext(); ) { DirectedEdge de = (DirectedEdge) i2.next(); LineMergeEdge e = (LineMergeEdge) de.getEdge(); LineString line = e.getLine(); LineString lineToAdd = line; if (! de.getEdgeDirection() && ! line.isClosed()) lineToAdd = reverse(line); lines.add(lineToAdd); } } if (lines.size() == 0) return factory.createMultiLineString(new LineString[0]); return factory.buildGeometry(lines); }
/** * Initializes this Edge's two DirectedEdges, and for each DirectedEdge: sets the * Edge, sets the symmetric DirectedEdge, and adds this Edge to its from-Node. */ public void setDirectedEdges(DirectedEdge de0, DirectedEdge de1) { dirEdge = new DirectedEdge[] { de0, de1 }; de0.setEdge(this); de1.setEdge(this); de0.setSym(de1); de1.setSym(de0); de0.getFromNode().addOutEdge(de0); de1.getFromNode().addOutEdge(de1); }
Node startNode = startEdge.getFromNode(); Node endNode = endEdge.getToNode(); if (endEdge.getToNode().getDegree() == 1 && endEdge.getEdgeDirection() == false) { hasObviousStartNode = true; flipSeq = true; if (startEdge.getFromNode().getDegree() == 1 && startEdge.getEdgeDirection() == true) { hasObviousStartNode = true; flipSeq = false; if (startEdge.getFromNode().getDegree() == 1) flipSeq = true;
/** * If <code>node</code> is one of the two nodes associated with this Edge, * returns the other node; otherwise returns null. */ public Node getOppositeNode(Node node) { if (dirEdge[0].getFromNode() == node) return dirEdge[0].getToNode(); if (dirEdge[1].getFromNode() == node) return dirEdge[1].getToNode(); // node not found // possibly should throw an exception here? return null; }
/** * Adds the argument node and all its out edges to the subgraph. * @param node the node to add * @param nodeStack the current set of nodes being traversed */ private void addEdges(Node node, Stack nodeStack, Subgraph subgraph) { node.setVisited(true); for (Iterator i = ((DirectedEdgeStar) node.getOutEdges()).iterator(); i.hasNext(); ) { DirectedEdge de = (DirectedEdge) i.next(); subgraph.add(de.getEdge()); Node toNode = de.getToNode(); if (! toNode.isVisited()) nodeStack.push(toNode); } }