private double ComputeDistancePointLine(Coordinate pt, FacetSequence facetSeq, GeometryLocation[] locs) { double minDistance = double.MaxValue; for (int i = facetSeq._start; i < facetSeq._end - 1; i++) { var q0 = facetSeq._pts.GetCoordinate(i); var q1 = facetSeq._pts.GetCoordinate(i + 1); double dist = DistanceComputer.PointToSegment(pt, q0, q1); if (dist < minDistance) { minDistance = dist; if (locs != null) { UpdateNearestLocationsPointLine(pt, facetSeq, i, q0, q1, locs); } if (minDistance <= 0) { return(minDistance); } } } return(minDistance); }
private double ComputeDistanceLineLine(FacetSequence facetSeq, GeometryLocation[] locs) { // both linear - compute minimum segment-segment distance double minDistance = double.MaxValue; for (int i = _start; i < _end - 1; i++) { var p0 = _pts.GetCoordinate(i); var p1 = _pts.GetCoordinate(i + 1); for (int j = facetSeq._start; j < facetSeq._end - 1; j++) { var q0 = facetSeq._pts.GetCoordinate(j); var q1 = facetSeq._pts.GetCoordinate(j + 1); double dist = DistanceComputer.SegmentToSegment(p0, p1, q0, q1); if (dist < minDistance) { minDistance = dist; if (locs != null) { UpdateNearestLocationsLineLine(i, p0, p1, facetSeq, j, q0, q1, locs); } if (minDistance <= 0) { return(minDistance); } } } } return(minDistance); }
/// <summary> /// Computes the distance between this and another /// <see cref="FacetSequence"/>. /// </summary> /// <param name="facetSeq">The sequence to compute the distance to.</param> /// <returns>The minimum distance between the sequences.</returns> public double Distance(FacetSequence facetSeq) { bool isPoint = IsPoint; bool isPointOther = facetSeq.IsPoint; double distance; if (isPoint && isPointOther) { var pt = _pts.GetCoordinate(_start); var seqPt = facetSeq._pts.GetCoordinate(facetSeq._start); distance = pt.Distance(seqPt); } else if (isPoint) { var pt = _pts.GetCoordinate(_start); distance = ComputeDistancePointLine(pt, facetSeq, null); } else if (isPointOther) { var seqPt = facetSeq._pts.GetCoordinate(facetSeq._start); distance = ComputeDistancePointLine(seqPt, this, null); } else { distance = ComputeDistanceLineLine(facetSeq, null); } return(distance); }
private void UpdateNearestLocationsPointLine(Coordinate pt, FacetSequence facetSeq, int i, Coordinate q0, Coordinate q1, GeometryLocation[] locs) { locs[0] = new GeometryLocation(_geom, _start, pt.Copy()); var seg = new LineSegment(q0, q1); var segClosestPoint = seg.ClosestPoint(pt); locs[1] = new GeometryLocation(facetSeq._geom, i, segClosestPoint.Copy()); }
private static void AddFacetSequences(Geometry geom, CoordinateSequence pts, List <FacetSequence> sections) { int i = 0; int size = pts.Count; while (i <= size - 1) { int end = i + FacetSequenceSize + 1; // if only one point remains after this section, include it in this // section if (end >= size - 1) { end = size; } var sect = new FacetSequence(geom, pts, i, end); sections.Add(sect); i = i + FacetSequenceSize; } }
/// <summary> /// Computes the locations of the nearest points between this sequence /// and another sequence. /// The locations are presented in the same order as the input sequences. /// </summary> /// <returns>A pair of <see cref="GeometryLocation"/>s for the nearest points.</returns> public GeometryLocation[] NearestLocations(FacetSequence facetSeq) { bool isPoint = IsPoint; bool isPointOther = facetSeq.IsPoint; var locs = new GeometryLocation[2]; if (isPoint && isPointOther) { // DEVIATION (minor): JTS uses "new Coordinate(GetCoordinate(int))", which is worse // than "GetCoordinateCopy(int)" for two reasons: 1) doesn't copy M (or, in NTS, Z), // and 2) might allocate two Coordinate instances instead of one. var pt = _pts.GetCoordinateCopy(_start); var seqPt = facetSeq._pts.GetCoordinateCopy(facetSeq._start); locs[0] = new GeometryLocation(_geom, _start, pt); locs[1] = new GeometryLocation(facetSeq._geom, facetSeq._start, seqPt); } else if (isPoint) { var pt = _pts.GetCoordinateCopy(_start); ComputeDistancePointLine(pt, facetSeq, locs); } else if (isPointOther) { var seqPt = facetSeq._pts.GetCoordinateCopy(facetSeq._start); ComputeDistancePointLine(seqPt, this, locs); // unflip the locations (locs[0], locs[1]) = (locs[1], locs[0]); } else { ComputeDistanceLineLine(facetSeq, locs); } return(locs); }
private void UpdateNearestLocationsLineLine(int i, Coordinate p0, Coordinate p1, FacetSequence facetSeq, int j, Coordinate q0, Coordinate q1, GeometryLocation[] locs) { var seg0 = new LineSegment(p0, p1); var seg1 = new LineSegment(q0, q1); var closestPt = seg0.ClosestPoints(seg1); locs[0] = new GeometryLocation(_geom, i, closestPt[0].Copy()); locs[1] = new GeometryLocation(facetSeq._geom, j, closestPt[1].Copy()); }