SegmentIntersectionDetector.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import SegmentIntersector from './SegmentIntersector'
  2. import RobustLineIntersector from '../algorithm/RobustLineIntersector'
  3. export default class SegmentIntersectionDetector {
  4. constructor() {
  5. SegmentIntersectionDetector.constructor_.apply(this, arguments)
  6. }
  7. static constructor_() {
  8. this._li = null
  9. this._findProper = false
  10. this._findAllTypes = false
  11. this._hasIntersection = false
  12. this._hasProperIntersection = false
  13. this._hasNonProperIntersection = false
  14. this._intPt = null
  15. this._intSegments = null
  16. if (arguments.length === 0) {
  17. SegmentIntersectionDetector.constructor_.call(this, new RobustLineIntersector())
  18. } else if (arguments.length === 1) {
  19. const li = arguments[0]
  20. this._li = li
  21. }
  22. }
  23. getIntersectionSegments() {
  24. return this._intSegments
  25. }
  26. setFindAllIntersectionTypes(findAllTypes) {
  27. this._findAllTypes = findAllTypes
  28. }
  29. hasProperIntersection() {
  30. return this._hasProperIntersection
  31. }
  32. getIntersection() {
  33. return this._intPt
  34. }
  35. processIntersections(e0, segIndex0, e1, segIndex1) {
  36. if (e0 === e1 && segIndex0 === segIndex1) return null
  37. const p00 = e0.getCoordinates()[segIndex0]
  38. const p01 = e0.getCoordinates()[segIndex0 + 1]
  39. const p10 = e1.getCoordinates()[segIndex1]
  40. const p11 = e1.getCoordinates()[segIndex1 + 1]
  41. this._li.computeIntersection(p00, p01, p10, p11)
  42. if (this._li.hasIntersection()) {
  43. this._hasIntersection = true
  44. const isProper = this._li.isProper()
  45. if (isProper) this._hasProperIntersection = true
  46. if (!isProper) this._hasNonProperIntersection = true
  47. let saveLocation = true
  48. if (this._findProper && !isProper) saveLocation = false
  49. if (this._intPt === null || saveLocation) {
  50. this._intPt = this._li.getIntersection(0)
  51. this._intSegments = new Array(4).fill(null)
  52. this._intSegments[0] = p00
  53. this._intSegments[1] = p01
  54. this._intSegments[2] = p10
  55. this._intSegments[3] = p11
  56. }
  57. }
  58. }
  59. hasIntersection() {
  60. return this._hasIntersection
  61. }
  62. isDone() {
  63. if (this._findAllTypes)
  64. return this._hasProperIntersection && this._hasNonProperIntersection
  65. if (this._findProper)
  66. return this._hasProperIntersection
  67. return this._hasIntersection
  68. }
  69. hasNonProperIntersection() {
  70. return this._hasNonProperIntersection
  71. }
  72. setFindProper(findProper) {
  73. this._findProper = findProper
  74. }
  75. get interfaces_() {
  76. return [SegmentIntersector]
  77. }
  78. }