/// <summary> /// Initializes a new instance of the <see cref="SegmentNode"/> class. /// </summary> /// <param name="segString"></param> /// <param name="coord"></param> /// <param name="segmentIndex"></param> /// <param name="segmentOctant"></param> public SegmentNode(SegmentString segString, Coordinate coord, int segmentIndex, OctantDirection segmentOctant) { Coordinate = new Coordinate(coord); SegmentIndex = segmentIndex; _segmentOctant = segmentOctant; _isInterior = !coord.Equals2D(segString.GetCoordinate(segmentIndex)); }
/// <summary> /// This method is called by clients /// of the <see cref="ISegmentIntersector" /> class to process /// intersections for two segments of the <see cref="SegmentString" />s being intersected. /// Notice that some clients (such as <see cref="MonotoneChain" />s) may optimize away /// this call for segment pairs which they have determined do not intersect /// (e.g. by an disjoint envelope test). /// </summary> /// <param name="e0"></param> /// <param name="segIndex0"></param> /// <param name="e1"></param> /// <param name="segIndex1"></param> public void ProcessIntersections(SegmentString e0, int segIndex0, SegmentString e1, int segIndex1) { // don't bother intersecting a segment with itself if (e0 == e1 && segIndex0 == segIndex1) { return; } Coordinate p00 = e0.Coordinates[segIndex0]; Coordinate p01 = e0.Coordinates[segIndex0 + 1]; Coordinate p10 = e1.Coordinates[segIndex1]; Coordinate p11 = e1.Coordinates[segIndex1 + 1]; _li.ComputeIntersection(p00, p01, p10, p11); if (_li.HasIntersection) { if (_li.IsInteriorIntersection()) { for (int intIndex = 0; intIndex < _li.IntersectionNum; intIndex++) { _interiorIntersections.Add(_li.GetIntersection(intIndex)); } e0.AddIntersections(_li, segIndex0); e1.AddIntersections(_li, segIndex1); } } }
/// <summary> /// This method is called by clients /// of the <see cref="ISegmentIntersector" /> class to process /// intersections for two segments of the <see cref="SegmentString" />s being intersected. /// Notice that some clients (such as <see cref="MonotoneChain" />s) may optimize away /// this call for segment pairs which they have determined do not intersect /// (e.g. by an disjoint envelope test). /// </summary> /// <param name="e0"></param> /// <param name="segIndex0"></param> /// <param name="e1"></param> /// <param name="segIndex1"></param> public void ProcessIntersections(SegmentString e0, int segIndex0, SegmentString e1, int segIndex1) { // don't bother intersecting a segment with itself if (e0 == e1 && segIndex0 == segIndex1) return; Coordinate p00 = e0.Coordinates[segIndex0]; Coordinate p01 = e0.Coordinates[segIndex0 + 1]; Coordinate p10 = e1.Coordinates[segIndex1]; Coordinate p11 = e1.Coordinates[segIndex1 + 1]; _li.ComputeIntersection(p00, p01, p10, p11); if (_li.HasIntersection) { if (_li.IsInteriorIntersection()) { for (int intIndex = 0; intIndex < _li.IntersectionNum; intIndex++) _interiorIntersections.Add(_li.GetIntersection(intIndex)); e0.AddIntersections(_li, segIndex0); e1.AddIntersections(_li, segIndex1); } } }
/// <summary> /// /// </summary> /// <param name="mc1"></param> /// <param name="start1"></param> /// <param name="mc2"></param> /// <param name="start2"></param> public override void Overlap(MonotoneChain mc1, int start1, MonotoneChain mc2, int start2) { SegmentString ss1 = (SegmentString)mc1.Context; SegmentString ss2 = (SegmentString)mc2.Context; _si.ProcessIntersections(ss1, start1, ss2, start2); }
/// <summary> /// /// </summary> /// <param name="segStrings"></param> /// <returns></returns> private IList Scale(ICollection segStrings) { return(CollectionUtil.Transform(segStrings, delegate(object obj) { SegmentString ss = (SegmentString)obj; return new SegmentString(Scale(ss.Coordinates), ss.Data); })); }
/// <summary> /// Checks if a segment string contains a segment pattern a-b-a (which implies a self-intersection). /// </summary> private void CheckCollapses() { foreach (object obj in _segStrings) { SegmentString ss = (SegmentString)obj; CheckCollapses(ss); } }
/// <summary> /// /// </summary> /// <param name="e0"></param> /// <param name="e1"></param> private void ComputeIntersects(SegmentString e0, SegmentString e1) { IList<Coordinate> pts0 = e0.Coordinates; IList<Coordinate> pts1 = e1.Coordinates; for (int i0 = 0; i0 < pts0.Count - 1; i0++) for (int i1 = 0; i1 < pts1.Count - 1; i1++) SegmentIntersector.ProcessIntersections(e0, i0, e1, i1); }
/// <summary> /// /// </summary> /// <param name="segStrings"></param> /// <param name="resultEdgelist"></param> public static void GetNodedSubstrings(IList segStrings, IList resultEdgelist) { foreach (object obj in segStrings) { SegmentString ss = (SegmentString)obj; ss.NodeList.AddSplitEdges(resultEdgelist); } }
/// <summary> /// /// </summary> /// <param name="segStrings"></param> private void Rescale(ICollection segStrings) { CollectionUtil.Apply(segStrings, delegate(object obj) { SegmentString ss = (SegmentString)obj; Rescale(ss.Coordinates); return(null); }); }
/// <summary> /// /// </summary> /// <param name="ss"></param> private static void CheckCollapses(SegmentString ss) { IList <Coordinate> pts = ss.Coordinates; for (int i = 0; i < pts.Count - 2; i++) { CheckCollapse(pts[i], pts[i + 1], pts[i + 2]); } }
/// <summary> /// Checks for intersections between an endpoint of a segment string /// and an interior vertex of another segment string /// </summary> private void CheckEndPtVertexIntersections() { foreach (object obj in _segStrings) { SegmentString ss = (SegmentString)obj; IList <Coordinate> pts = ss.Coordinates; CheckEndPtVertexIntersections(pts[0], _segStrings); CheckEndPtVertexIntersections(pts[pts.Count - 1], _segStrings); } }
/// <summary> /// /// </summary> /// <param name="segStr"></param> private void Add(SegmentString segStr) { IList segChains = MonotoneChainBuilder.GetChains(segStr.Coordinates, segStr); foreach (object obj in segChains) { MonotoneChain mc = (MonotoneChain)obj; mc.Id = _idCounter++; _index.Insert(mc.Envelope, mc); _monoChains.Add(mc); } }
/// <summary> /// Checks all pairs of segments for intersections at an interior point of a segment. /// </summary> private void CheckInteriorIntersections() { foreach (object obj0 in _segStrings) { SegmentString ss0 = (SegmentString)obj0; foreach (object obj1 in _segStrings) { SegmentString ss1 = (SegmentString)obj1; CheckInteriorIntersections(ss0, ss1); } } }
/// <summary> /// /// </summary> /// <param name="ss0"></param> /// <param name="ss1"></param> private void CheckInteriorIntersections(SegmentString ss0, SegmentString ss1) { IList <Coordinate> pts0 = ss0.Coordinates; IList <Coordinate> pts1 = ss1.Coordinates; for (int i0 = 0; i0 < pts0.Count - 1; i0++) { for (int i1 = 0; i1 < pts1.Count - 1; i1++) { CheckInteriorIntersections(ss0, i0, ss1, i1); } } }
/// <summary> /// Computes the noding for a collection of <see cref="SegmentString" />s. /// Some Noders may add all these nodes to the input <see cref="SegmentString" />s; /// others may only add some or none at all. /// </summary> /// <param name="inputSegStrings"></param> public override void ComputeNodes(IList inputSegStrings) { _nodedSegStrings = inputSegStrings; foreach (object obj0 in inputSegStrings) { SegmentString edge0 = (SegmentString)obj0; foreach (object obj1 in inputSegStrings) { SegmentString edge1 = (SegmentString)obj1; ComputeIntersects(edge0, edge1); } } }
/// <summary> /// /// </summary> /// <param name="e0"></param> /// <param name="e1"></param> private void ComputeIntersects(SegmentString e0, SegmentString e1) { IList <Coordinate> pts0 = e0.Coordinates; IList <Coordinate> pts1 = e1.Coordinates; for (int i0 = 0; i0 < pts0.Count - 1; i0++) { for (int i1 = 0; i1 < pts1.Count - 1; i1++) { SegmentIntersector.ProcessIntersections(e0, i0, e1, i1); } } }
/// <summary> /// /// </summary> /// <param name="testPt"></param> /// <param name="segStrings"></param> private static void CheckEndPtVertexIntersections(Coordinate testPt, IList segStrings) { foreach (object obj in segStrings) { SegmentString ss = (SegmentString)obj; IList <Coordinate> pts = ss.Coordinates; for (int j = 1; j < pts.Count - 1; j++) { if (pts[j].Equals(testPt)) { throw new Exception("found endpt/interior pt intersection at index " + j + " :pt " + testPt); } } } }
/// <summary> /// Dissolve the given <see cref="SegmentString" />. /// </summary> /// <param name="segString"></param> public void Dissolve(SegmentString segString) { OrientedCoordinateArray oca = new OrientedCoordinateArray(segString.Coordinates); SegmentString existing = FindMatching(oca); if (existing == null) { Add(oca, segString); } else { if (_merger != null) { bool isSameOrientation = Equals(existing.Coordinates, segString.Coordinates); _merger.Merge(existing, segString, isSameOrientation); } } }
/// <summary> /// This method is called by clients /// of the <see cref="ISegmentIntersector" /> class to process /// intersections for two segments of the <see cref="SegmentString" /> being intersected. /// Notice that some clients (such as <see cref="MonotoneChain" />s) may optimize away /// this call for segment pairs which they have determined do not intersect /// (e.g. by an disjoint envelope test). /// </summary> /// <param name="e0"></param> /// <param name="segIndex0"></param> /// <param name="e1"></param> /// <param name="segIndex1"></param> public void ProcessIntersections(SegmentString e0, int segIndex0, SegmentString e1, int segIndex1) { if (e0 == e1 && segIndex0 == segIndex1) { return; } _numTests++; Coordinate p00 = e0.Coordinates[segIndex0]; Coordinate p01 = e0.Coordinates[segIndex0 + 1]; Coordinate p10 = e1.Coordinates[segIndex1]; Coordinate p11 = e1.Coordinates[segIndex1 + 1]; _li.ComputeIntersection(p00, p01, p10, p11); if (!_li.HasIntersection) { return; } _numIntersections++; if (_li.IsInteriorIntersection()) { NumInteriorIntersections++; _hasInterior = true; } // if the segments are adjacent they have at least one trivial intersection, // the shared endpoint. Don't bother adding it if it is the // only intersection. if (IsTrivialIntersection(e0, segIndex0, e1, segIndex1)) { return; } _hasIntersection = true; e0.AddIntersections(_li, segIndex0); e1.AddIntersections(_li, segIndex1); if (!_li.IsProper) { return; } _numProperIntersections++; _hasProper = true; _hasProperInterior = true; }
/// <summary> /// /// </summary> /// <param name="e0"></param> /// <param name="segIndex0"></param> /// <param name="e1"></param> /// <param name="segIndex1"></param> private void CheckInteriorIntersections(SegmentString e0, int segIndex0, SegmentString e1, int segIndex1) { if (e0 == e1 && segIndex0 == segIndex1) { return; } Coordinate p00 = e0.Coordinates[segIndex0]; Coordinate p01 = e0.Coordinates[segIndex0 + 1]; Coordinate p10 = e1.Coordinates[segIndex1]; Coordinate p11 = e1.Coordinates[segIndex1 + 1]; _li.ComputeIntersection(p00, p01, p10, p11); if (_li.HasIntersection) { if (_li.IsProper || HasInteriorIntersection(_li, p00, p01) || HasInteriorIntersection(_li, p10, p11)) { throw new Exception("found non-noded intersection at " + p00 + "-" + p01 + " and " + p10 + "-" + p11); } } }
/// <summary> /// Creates new edges for all the edges that the intersections in this /// list split the parent edge into. /// Adds the edges to the provided argument list /// (this is so a single list can be used to accumulate all split edges /// for a set of <see cref="SegmentString" />s). /// </summary> /// <param name="edgeList"></param> public void AddSplitEdges(IList edgeList) { // ensure that the list has entries for the first and last point of the edge AddEndPoints(); AddCollapsedNodes(); IEnumerator ie = GetEnumerator(); ie.MoveNext(); // there should always be at least two entries in the list, since the endpoints are nodes SegmentNode eiPrev = (SegmentNode)ie.Current; while (ie.MoveNext()) { SegmentNode ei = (SegmentNode)ie.Current; SegmentString newEdge = CreateSplitEdge(eiPrev, ei); edgeList.Add(newEdge); eiPrev = ei; } // CheckSplitEdgesCorrectness(testingSplitEdges); }
/// <summary> /// A trivial intersection is an apparent self-intersection which in fact /// is simply the point shared by adjacent line segments. /// Notice that closed edges require a special check for the point shared by the beginning and end segments. /// </summary> /// <param name="e0"></param> /// <param name="segIndex0"></param> /// <param name="e1"></param> /// <param name="segIndex1"></param> /// <returns></returns> private bool IsTrivialIntersection(SegmentString e0, int segIndex0, SegmentString e1, int segIndex1) { if (e0 == e1) { if (_li.IntersectionNum == 1) { if (IsAdjacentSegments(segIndex0, segIndex1)) { return(true); } if (e0.IsClosed) { int maxSegIndex = e0.Count - 1; if ((segIndex0 == 0 && segIndex1 == maxSegIndex) || (segIndex1 == 0 && segIndex0 == maxSegIndex)) { return(true); } } } } return(false); }
/// <summary> /// Initializes a new instance of the <see cref="SegmentNodeList"/> class. /// </summary> /// <param name="edge">The edge.</param> public SegmentNodeList(SegmentString edge) { _edge = edge; }
private NodeVertexIterator(SegmentNodeList nodeList) { _nodeList = nodeList; _edge = nodeList.Edge; _nodeIt = nodeList.GetEnumerator(); }
/// <summary> /// Returns a <see cref="IList"/> of fully noded <see cref="SegmentString"/>s. /// The <see cref="SegmentString"/>s have the same context as their parent. /// </summary> /// <returns></returns> public override IList GetNodedSubstrings() { return(SegmentString.GetNodedSubstrings(_nodedSegStrings)); }
/// <summary> /// /// </summary> /// <param name="e0"></param> /// <param name="segIndex0"></param> /// <param name="e1"></param> /// <param name="segIndex1"></param> private void CheckInteriorIntersections(SegmentString e0, int segIndex0, SegmentString e1, int segIndex1) { if (e0 == e1 && segIndex0 == segIndex1) return; Coordinate p00 = e0.Coordinates[segIndex0]; Coordinate p01 = e0.Coordinates[segIndex0 + 1]; Coordinate p10 = e1.Coordinates[segIndex1]; Coordinate p11 = e1.Coordinates[segIndex1 + 1]; _li.ComputeIntersection(p00, p01, p10, p11); if (_li.HasIntersection) if (_li.IsProper || HasInteriorIntersection(_li, p00, p01) || HasInteriorIntersection(_li, p10, p11)) throw new Exception("found non-noded intersection at " + p00 + "-" + p01 + " and " + p10 + "-" + p11); }
/// <summary> /// /// </summary> /// <param name="ss"></param> private static void CheckCollapses(SegmentString ss) { IList<Coordinate> pts = ss.Coordinates; for (int i = 0; i < pts.Count - 2; i++) CheckCollapse(pts[i], pts[i + 1], pts[i + 2]); }
/// <summary> /// /// </summary> /// <param name="oca"></param> /// <param name="segString"></param> private void Add(OrientedCoordinateArray oca, SegmentString segString) { _ocaMap.Add(oca, segString); }
/// <summary> /// This method is called by clients /// of the <see cref="ISegmentIntersector" /> class to process /// intersections for two segments of the <see cref="SegmentString" /> being intersected. /// Notice that some clients (such as <see cref="MonotoneChain" />s) may optimize away /// this call for segment pairs which they have determined do not intersect /// (e.g. by an disjoint envelope test). /// </summary> /// <param name="e0"></param> /// <param name="segIndex0"></param> /// <param name="e1"></param> /// <param name="segIndex1"></param> public void ProcessIntersections(SegmentString e0, int segIndex0, SegmentString e1, int segIndex1) { if (e0 == e1 && segIndex0 == segIndex1) return; _numTests++; Coordinate p00 = e0.Coordinates[segIndex0]; Coordinate p01 = e0.Coordinates[segIndex0 + 1]; Coordinate p10 = e1.Coordinates[segIndex1]; Coordinate p11 = e1.Coordinates[segIndex1 + 1]; _li.ComputeIntersection(p00, p01, p10, p11); if (!_li.HasIntersection) return; _numIntersections++; if (_li.IsInteriorIntersection()) { NumInteriorIntersections++; _hasInterior = true; } // if the segments are adjacent they have at least one trivial intersection, // the shared endpoint. Don't bother adding it if it is the // only intersection. if (IsTrivialIntersection(e0, segIndex0, e1, segIndex1)) return; _hasIntersection = true; e0.AddIntersections(_li, segIndex0); e1.AddIntersections(_li, segIndex1); if (!_li.IsProper) return; _numProperIntersections++; _hasProper = true; _hasProperInterior = true; }
/// <summary> /// Creates a {SegmentString} for a coordinate list which is a raw offset curve, /// and adds it to the list of buffer curves. /// The SegmentString is tagged with a Label giving the topology of the curve. /// The curve may be oriented in either direction. /// If the curve is oriented CW, the locations will be: /// Left: Location.Exterior. /// Right: Location.Interior. /// </summary> private void AddCurve(IList<Coordinate> coord, LocationType leftLoc, LocationType rightLoc) { // don't add null curves! if (coord.Count < 2) return; // add the edge for a coordinate list which is a raw offset curve SegmentString e = new SegmentString(coord, new Label(0, LocationType.Boundary, leftLoc, rightLoc)); _curveList.Add(e); }
/// <summary> /// /// </summary> /// <param name="ss0"></param> /// <param name="ss1"></param> private void CheckInteriorIntersections(SegmentString ss0, SegmentString ss1) { IList<Coordinate> pts0 = ss0.Coordinates; IList<Coordinate> pts1 = ss1.Coordinates; for (int i0 = 0; i0 < pts0.Count - 1; i0++) for (int i1 = 0; i1 < pts1.Count - 1; i1++) CheckInteriorIntersections(ss0, i0, ss1, i1); }
/// <summary> /// Dissolve the given <see cref="SegmentString" />. /// </summary> /// <param name="segString"></param> public void Dissolve(SegmentString segString) { OrientedCoordinateArray oca = new OrientedCoordinateArray(segString.Coordinates); SegmentString existing = FindMatching(oca); if (existing == null) Add(oca, segString); else { if (_merger != null) { bool isSameOrientation = Equals(existing.Coordinates, segString.Coordinates); _merger.Merge(existing, segString, isSameOrientation); } } }
/// <summary> /// A trivial intersection is an apparent self-intersection which in fact /// is simply the point shared by adjacent line segments. /// Notice that closed edges require a special check for the point shared by the beginning and end segments. /// </summary> /// <param name="e0"></param> /// <param name="segIndex0"></param> /// <param name="e1"></param> /// <param name="segIndex1"></param> /// <returns></returns> private bool IsTrivialIntersection(SegmentString e0, int segIndex0, SegmentString e1, int segIndex1) { if (e0 == e1) { if (_li.IntersectionNum == 1) { if (IsAdjacentSegments(segIndex0, segIndex1)) return true; if (e0.IsClosed) { int maxSegIndex = e0.Count - 1; if ((segIndex0 == 0 && segIndex1 == maxSegIndex) || (segIndex1 == 0 && segIndex0 == maxSegIndex)) return true; } } } return false; }