QuadEdge.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import WKTWriter from '../../io/WKTWriter'
  2. import LineSegment from '../../geom/LineSegment'
  3. export default class QuadEdge {
  4. constructor() {
  5. QuadEdge.constructor_.apply(this, arguments)
  6. }
  7. static constructor_() {
  8. this._rot = null
  9. this._vertex = null
  10. this._next = null
  11. this._data = null
  12. }
  13. static makeEdge(o, d) {
  14. const q0 = new QuadEdge()
  15. const q1 = new QuadEdge()
  16. const q2 = new QuadEdge()
  17. const q3 = new QuadEdge()
  18. q0._rot = q1
  19. q1._rot = q2
  20. q2._rot = q3
  21. q3._rot = q0
  22. q0.setNext(q0)
  23. q1.setNext(q3)
  24. q2.setNext(q2)
  25. q3.setNext(q1)
  26. const base = q0
  27. base.setOrig(o)
  28. base.setDest(d)
  29. return base
  30. }
  31. static swap(e) {
  32. const a = e.oPrev()
  33. const b = e.sym().oPrev()
  34. QuadEdge.splice(e, a)
  35. QuadEdge.splice(e.sym(), b)
  36. QuadEdge.splice(e, a.lNext())
  37. QuadEdge.splice(e.sym(), b.lNext())
  38. e.setOrig(a.dest())
  39. e.setDest(b.dest())
  40. }
  41. static splice(a, b) {
  42. const alpha = a.oNext().rot()
  43. const beta = b.oNext().rot()
  44. const t1 = b.oNext()
  45. const t2 = a.oNext()
  46. const t3 = beta.oNext()
  47. const t4 = alpha.oNext()
  48. a.setNext(t1)
  49. b.setNext(t2)
  50. alpha.setNext(t3)
  51. beta.setNext(t4)
  52. }
  53. static connect(a, b) {
  54. const e = QuadEdge.makeEdge(a.dest(), b.orig())
  55. QuadEdge.splice(e, a.lNext())
  56. QuadEdge.splice(e.sym(), b)
  57. return e
  58. }
  59. equalsNonOriented(qe) {
  60. if (this.equalsOriented(qe)) return true
  61. if (this.equalsOriented(qe.sym())) return true
  62. return false
  63. }
  64. toLineSegment() {
  65. return new LineSegment(this._vertex.getCoordinate(), this.dest().getCoordinate())
  66. }
  67. dest() {
  68. return this.sym().orig()
  69. }
  70. oNext() {
  71. return this._next
  72. }
  73. equalsOriented(qe) {
  74. if (this.orig().getCoordinate().equals2D(qe.orig().getCoordinate()) && this.dest().getCoordinate().equals2D(qe.dest().getCoordinate())) return true
  75. return false
  76. }
  77. dNext() {
  78. return this.sym().oNext().sym()
  79. }
  80. lPrev() {
  81. return this._next.sym()
  82. }
  83. rPrev() {
  84. return this.sym().oNext()
  85. }
  86. rot() {
  87. return this._rot
  88. }
  89. oPrev() {
  90. return this._rot._next._rot
  91. }
  92. sym() {
  93. return this._rot._rot
  94. }
  95. setOrig(o) {
  96. this._vertex = o
  97. }
  98. lNext() {
  99. return this.invRot().oNext().rot()
  100. }
  101. getLength() {
  102. return this.orig().getCoordinate().distance(this.dest().getCoordinate())
  103. }
  104. invRot() {
  105. return this._rot.sym()
  106. }
  107. setDest(d) {
  108. this.sym().setOrig(d)
  109. }
  110. setData(data) {
  111. this._data = data
  112. }
  113. getData() {
  114. return this._data
  115. }
  116. delete() {
  117. this._rot = null
  118. }
  119. orig() {
  120. return this._vertex
  121. }
  122. rNext() {
  123. return this._rot._next.invRot()
  124. }
  125. toString() {
  126. const p0 = this._vertex.getCoordinate()
  127. const p1 = this.dest().getCoordinate()
  128. return WKTWriter.toLineString(p0, p1)
  129. }
  130. isLive() {
  131. return this._rot !== null
  132. }
  133. getPrimary() {
  134. if (this.orig().getCoordinate().compareTo(this.dest().getCoordinate()) <= 0) return this; else return this.sym()
  135. }
  136. dPrev() {
  137. return this.invRot().oNext().invRot()
  138. }
  139. setNext(next) {
  140. this._next = next
  141. }
  142. }