IndexedFacetDistance.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import FacetSequenceTreeBuilder from './FacetSequenceTreeBuilder'
  2. import ItemDistance from '../../index/strtree/ItemDistance'
  3. export default class IndexedFacetDistance {
  4. constructor() {
  5. IndexedFacetDistance.constructor_.apply(this, arguments)
  6. }
  7. static constructor_() {
  8. this._cachedTree = null
  9. this._baseGeometry = null
  10. const geom = arguments[0]
  11. this._baseGeometry = geom
  12. this._cachedTree = FacetSequenceTreeBuilder.build(geom)
  13. }
  14. static distance(g1, g2) {
  15. const dist = new IndexedFacetDistance(g1)
  16. return dist.distance(g2)
  17. }
  18. static isWithinDistance(g1, g2, distance) {
  19. const dist = new IndexedFacetDistance(g1)
  20. return dist.isWithinDistance(g2, distance)
  21. }
  22. static nearestPoints(g1, g2) {
  23. const dist = new IndexedFacetDistance(g1)
  24. return dist.nearestPoints(g2)
  25. }
  26. static toPoints(locations) {
  27. if (locations === null) return null
  28. const nearestPts = [locations[0].getCoordinate(), locations[1].getCoordinate()]
  29. return nearestPts
  30. }
  31. distance(g) {
  32. const tree2 = FacetSequenceTreeBuilder.build(g)
  33. const obj = this._cachedTree.nearestNeighbour(tree2, IndexedFacetDistance.FACET_SEQ_DIST)
  34. const fs1 = obj[0]
  35. const fs2 = obj[1]
  36. return fs1.distance(fs2)
  37. }
  38. isWithinDistance(g, maxDistance) {
  39. const envDist = this._baseGeometry.getEnvelopeInternal().distance(g.getEnvelopeInternal())
  40. if (envDist > maxDistance) return false
  41. const tree2 = FacetSequenceTreeBuilder.build(g)
  42. return this._cachedTree.isWithinDistance(tree2, IndexedFacetDistance.FACET_SEQ_DIST, maxDistance)
  43. }
  44. nearestPoints(g) {
  45. const minDistanceLocation = this.nearestLocations(g)
  46. const nearestPts = IndexedFacetDistance.toPoints(minDistanceLocation)
  47. return nearestPts
  48. }
  49. nearestLocations(g) {
  50. const tree2 = FacetSequenceTreeBuilder.build(g)
  51. const obj = this._cachedTree.nearestNeighbour(tree2, IndexedFacetDistance.FACET_SEQ_DIST)
  52. const fs1 = obj[0]
  53. const fs2 = obj[1]
  54. return fs1.nearestLocations(fs2)
  55. }
  56. }
  57. class FacetSequenceDistance {
  58. distance(item1, item2) {
  59. const fs1 = item1.getItem()
  60. const fs2 = item2.getItem()
  61. return fs1.distance(fs2)
  62. }
  63. get interfaces_() {
  64. return [ItemDistance]
  65. }
  66. }
  67. IndexedFacetDistance.FacetSequenceDistance = FacetSequenceDistance
  68. IndexedFacetDistance.FACET_SEQ_DIST = new FacetSequenceDistance()