BoundablePair.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import IllegalArgumentException from '../../../../../java/lang/IllegalArgumentException'
  2. import AbstractNode from './AbstractNode'
  3. import EnvelopeDistance from './EnvelopeDistance'
  4. import Comparable from '../../../../../java/lang/Comparable'
  5. export default class BoundablePair {
  6. constructor() {
  7. BoundablePair.constructor_.apply(this, arguments)
  8. }
  9. static constructor_() {
  10. this._boundable1 = null
  11. this._boundable2 = null
  12. this._distance = null
  13. this._itemDistance = null
  14. const boundable1 = arguments[0], boundable2 = arguments[1], itemDistance = arguments[2]
  15. this._boundable1 = boundable1
  16. this._boundable2 = boundable2
  17. this._itemDistance = itemDistance
  18. this._distance = this.distance()
  19. }
  20. static area(b) {
  21. return b.getBounds().getArea()
  22. }
  23. static isComposite(item) {
  24. return item instanceof AbstractNode
  25. }
  26. maximumDistance() {
  27. return EnvelopeDistance.maximumDistance(this._boundable1.getBounds(), this._boundable2.getBounds())
  28. }
  29. expandToQueue(priQ, minDistance) {
  30. const isComp1 = BoundablePair.isComposite(this._boundable1)
  31. const isComp2 = BoundablePair.isComposite(this._boundable2)
  32. if (isComp1 && isComp2) {
  33. if (BoundablePair.area(this._boundable1) > BoundablePair.area(this._boundable2)) {
  34. this.expand(this._boundable1, this._boundable2, false, priQ, minDistance)
  35. return null
  36. } else {
  37. this.expand(this._boundable2, this._boundable1, true, priQ, minDistance)
  38. return null
  39. }
  40. } else if (isComp1) {
  41. this.expand(this._boundable1, this._boundable2, false, priQ, minDistance)
  42. return null
  43. } else if (isComp2) {
  44. this.expand(this._boundable2, this._boundable1, true, priQ, minDistance)
  45. return null
  46. }
  47. throw new IllegalArgumentException('neither boundable is composite')
  48. }
  49. isLeaves() {
  50. return !(BoundablePair.isComposite(this._boundable1) || BoundablePair.isComposite(this._boundable2))
  51. }
  52. compareTo(o) {
  53. const nd = o
  54. if (this._distance < nd._distance) return -1
  55. if (this._distance > nd._distance) return 1
  56. return 0
  57. }
  58. expand(bndComposite, bndOther, isFlipped, priQ, minDistance) {
  59. const children = bndComposite.getChildBoundables()
  60. for (let i = children.iterator(); i.hasNext(); ) {
  61. const child = i.next()
  62. let bp = null
  63. if (isFlipped)
  64. bp = new BoundablePair(bndOther, child, this._itemDistance)
  65. else
  66. bp = new BoundablePair(child, bndOther, this._itemDistance)
  67. if (bp.getDistance() < minDistance)
  68. priQ.add(bp)
  69. }
  70. }
  71. getBoundable(i) {
  72. if (i === 0) return this._boundable1
  73. return this._boundable2
  74. }
  75. getDistance() {
  76. return this._distance
  77. }
  78. distance() {
  79. if (this.isLeaves())
  80. return this._itemDistance.distance(this._boundable1, this._boundable2)
  81. return this._boundable1.getBounds().distance(this._boundable2.getBounds())
  82. }
  83. get interfaces_() {
  84. return [Comparable]
  85. }
  86. }