/// <summary> /// Add a <c>LineString</c> forming an edge of the polygon graph. /// </summary> /// <param name="line">The line to add.</param> public void AddEdge(ILineString line) { if (line.IsEmpty) { return; } Coordinate[] linePts = CoordinateArrays.RemoveRepeatedPoints(line.Coordinates); if (linePts.Length < 2) { return; } Coordinate startPt = linePts[0]; Coordinate endPt = linePts[linePts.Length - 1]; Node nStart = GetNode(startPt); Node nEnd = GetNode(endPt); DirectedEdge de0 = new PolygonizeDirectedEdge(nStart, nEnd, linePts[1], true); DirectedEdge de1 = new PolygonizeDirectedEdge(nEnd, nStart, linePts[linePts.Length - 2], false); Edge edge = new PolygonizeEdge(line); edge.SetDirectedEdges(de0, de1); Add(edge); }
/// <summary> /// Finds and removes all cut edges from the graph. /// </summary> /// <returns>A list of the <c>LineString</c>s forming the removed cut edges.</returns> public IList <ILineString> DeleteCutEdges() { ComputeNextCWEdges(); // label the current set of edgerings FindLabeledEdgeRings(dirEdges); /* * Cut Edges are edges where both dirEdges have the same label. * Delete them, and record them */ IList <ILineString> cutLines = new List <ILineString>(); foreach (PolygonizeDirectedEdge de in dirEdges) { if (de.IsMarked) { continue; } PolygonizeDirectedEdge sym = (PolygonizeDirectedEdge)de.Sym; if (de.Label == sym.Label) { de.Marked = true; sym.Marked = true; // save the line as a cut edge PolygonizeEdge e = (PolygonizeEdge)de.Edge; cutLines.Add(e.Line); } } return(cutLines); }
/// <summary> /// Marks all edges from the graph which are "dangles". /// Dangles are which are incident on a node with degree 1. /// This process is recursive, since removing a dangling edge /// may result in another edge becoming a dangle. /// In order to handle large recursion depths efficiently, /// an explicit recursion stack is used. /// </summary> /// <returns>A List containing the <see cref="ILineString"/>s that formed dangles.</returns> public ICollection <ILineString> DeleteDangles() { var nodesToRemove = FindNodesOfDegree(1); HashSet <ILineString> dangleLines = new HashSet <ILineString>(); Stack <Node> nodeStack = new Stack <Node>(); foreach (Node node in nodesToRemove) { nodeStack.Push(node); } while (nodeStack.Count != 0) { Node node = nodeStack.Pop(); DeleteAllEdges(node); IList <DirectedEdge> nodeOutEdges = node.OutEdges.Edges; foreach (PolygonizeDirectedEdge de in nodeOutEdges) { // delete this edge and its sym de.Marked = true; PolygonizeDirectedEdge sym = (PolygonizeDirectedEdge)de.Sym; if (sym != null) { sym.Marked = true; } // save the line as a dangle PolygonizeEdge e = (PolygonizeEdge)de.Edge; dangleLines.Add(e.Line); Node toNode = de.ToNode; // add the toNode to the list to be processed, if it is now a dangle if (GetDegreeNonDeleted(toNode) == 1) { nodeStack.Push(toNode); } } } var dangleArray = new ILineString[dangleLines.Count]; dangleLines.CopyTo(dangleArray, 0); return(new ReadOnlyCollection <ILineString>(dangleArray)); //new ArrayList(dangleLines.CastPlatform()); }
/// <summary> /// Add a <c>LineString</c> forming an edge of the polygon graph. /// </summary> /// <param name="line">The line to add.</param> public void AddEdge(ILineString line) { if (line.IsEmpty) return; Coordinate[] linePts = CoordinateArrays.RemoveRepeatedPoints(line.Coordinates); Coordinate startPt = linePts[0]; Coordinate endPt = linePts[linePts.Length - 1]; Node nStart = GetNode(startPt); Node nEnd = GetNode(endPt); DirectedEdge de0 = new PolygonizeDirectedEdge(nStart, nEnd, linePts[1], true); DirectedEdge de1 = new PolygonizeDirectedEdge(nEnd, nStart, linePts[linePts.Length - 2], false); Edge edge = new PolygonizeEdge(line); edge.SetDirectedEdges(de0, de1); Add(edge); }