LocationIndexOfPoint.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import LinearIterator from './LinearIterator'
  2. import LinearLocation from './LinearLocation'
  3. import Double from '../../../../java/lang/Double'
  4. import LineSegment from '../geom/LineSegment'
  5. import Assert from '../util/Assert'
  6. export default class LocationIndexOfPoint {
  7. constructor() {
  8. LocationIndexOfPoint.constructor_.apply(this, arguments)
  9. }
  10. static constructor_() {
  11. this._linearGeom = null
  12. const linearGeom = arguments[0]
  13. this._linearGeom = linearGeom
  14. }
  15. static indexOf(linearGeom, inputPt) {
  16. const locater = new LocationIndexOfPoint(linearGeom)
  17. return locater.indexOf(inputPt)
  18. }
  19. static indexOfAfter(linearGeom, inputPt, minIndex) {
  20. const locater = new LocationIndexOfPoint(linearGeom)
  21. return locater.indexOfAfter(inputPt, minIndex)
  22. }
  23. indexOf(inputPt) {
  24. return this.indexOfFromStart(inputPt, null)
  25. }
  26. indexOfFromStart(inputPt, minIndex) {
  27. let minDistance = Double.MAX_VALUE
  28. let minComponentIndex = 0
  29. let minSegmentIndex = 0
  30. let minFrac = -1.0
  31. const seg = new LineSegment()
  32. for (let it = new LinearIterator(this._linearGeom); it.hasNext(); it.next())
  33. if (!it.isEndOfLine()) {
  34. seg.p0 = it.getSegmentStart()
  35. seg.p1 = it.getSegmentEnd()
  36. const segDistance = seg.distance(inputPt)
  37. const segFrac = seg.segmentFraction(inputPt)
  38. const candidateComponentIndex = it.getComponentIndex()
  39. const candidateSegmentIndex = it.getVertexIndex()
  40. if (segDistance < minDistance)
  41. if (minIndex === null || minIndex.compareLocationValues(candidateComponentIndex, candidateSegmentIndex, segFrac) < 0) {
  42. minComponentIndex = candidateComponentIndex
  43. minSegmentIndex = candidateSegmentIndex
  44. minFrac = segFrac
  45. minDistance = segDistance
  46. }
  47. }
  48. if (minDistance === Double.MAX_VALUE)
  49. return new LinearLocation(minIndex)
  50. const loc = new LinearLocation(minComponentIndex, minSegmentIndex, minFrac)
  51. return loc
  52. }
  53. indexOfAfter(inputPt, minIndex) {
  54. if (minIndex === null) return this.indexOf(inputPt)
  55. const endLoc = LinearLocation.getEndLocation(this._linearGeom)
  56. if (endLoc.compareTo(minIndex) <= 0) return endLoc
  57. const closestAfter = this.indexOfFromStart(inputPt, minIndex)
  58. Assert.isTrue(closestAfter.compareTo(minIndex) >= 0, 'computed location is before specified minimum location')
  59. return closestAfter
  60. }
  61. }