IntersectionAdder.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import SegmentIntersector from './SegmentIntersector'
  2. export default class IntersectionAdder {
  3. constructor() {
  4. IntersectionAdder.constructor_.apply(this, arguments)
  5. }
  6. static constructor_() {
  7. this._hasIntersection = false
  8. this._hasProper = false
  9. this._hasProperInterior = false
  10. this._hasInterior = false
  11. this._properIntersectionPoint = null
  12. this._li = null
  13. this._isSelfIntersection = null
  14. this.numIntersections = 0
  15. this.numInteriorIntersections = 0
  16. this.numProperIntersections = 0
  17. this.numTests = 0
  18. const li = arguments[0]
  19. this._li = li
  20. }
  21. static isAdjacentSegments(i1, i2) {
  22. return Math.abs(i1 - i2) === 1
  23. }
  24. isTrivialIntersection(e0, segIndex0, e1, segIndex1) {
  25. if (e0 === e1)
  26. if (this._li.getIntersectionNum() === 1) {
  27. if (IntersectionAdder.isAdjacentSegments(segIndex0, segIndex1)) return true
  28. if (e0.isClosed()) {
  29. const maxSegIndex = e0.size() - 1
  30. if (segIndex0 === 0 && segIndex1 === maxSegIndex || segIndex1 === 0 && segIndex0 === maxSegIndex)
  31. return true
  32. }
  33. }
  34. return false
  35. }
  36. getProperIntersectionPoint() {
  37. return this._properIntersectionPoint
  38. }
  39. hasProperInteriorIntersection() {
  40. return this._hasProperInterior
  41. }
  42. getLineIntersector() {
  43. return this._li
  44. }
  45. hasProperIntersection() {
  46. return this._hasProper
  47. }
  48. processIntersections(e0, segIndex0, e1, segIndex1) {
  49. if (e0 === e1 && segIndex0 === segIndex1) return null
  50. this.numTests++
  51. const p00 = e0.getCoordinates()[segIndex0]
  52. const p01 = e0.getCoordinates()[segIndex0 + 1]
  53. const p10 = e1.getCoordinates()[segIndex1]
  54. const p11 = e1.getCoordinates()[segIndex1 + 1]
  55. this._li.computeIntersection(p00, p01, p10, p11)
  56. if (this._li.hasIntersection()) {
  57. this.numIntersections++
  58. if (this._li.isInteriorIntersection()) {
  59. this.numInteriorIntersections++
  60. this._hasInterior = true
  61. }
  62. if (!this.isTrivialIntersection(e0, segIndex0, e1, segIndex1)) {
  63. this._hasIntersection = true
  64. e0.addIntersections(this._li, segIndex0, 0)
  65. e1.addIntersections(this._li, segIndex1, 1)
  66. if (this._li.isProper()) {
  67. this.numProperIntersections++
  68. this._hasProper = true
  69. this._hasProperInterior = true
  70. }
  71. }
  72. }
  73. }
  74. hasIntersection() {
  75. return this._hasIntersection
  76. }
  77. isDone() {
  78. return false
  79. }
  80. hasInteriorIntersection() {
  81. return this._hasInterior
  82. }
  83. get interfaces_() {
  84. return [SegmentIntersector]
  85. }
  86. }