PlanarGraph.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import Location from '../geom/Location'
  2. import Coordinate from '../geom/Coordinate'
  3. import Node from './Node'
  4. import NodeMap from './NodeMap'
  5. import Orientation from '../algorithm/Orientation'
  6. import DirectedEdge from './DirectedEdge'
  7. import System from '../../../../java/lang/System'
  8. import ArrayList from '../../../../java/util/ArrayList'
  9. import Quadrant from './Quadrant'
  10. import NodeFactory from './NodeFactory'
  11. export default class PlanarGraph {
  12. constructor() {
  13. PlanarGraph.constructor_.apply(this, arguments)
  14. }
  15. static constructor_() {
  16. this._edges = new ArrayList()
  17. this._nodes = null
  18. this._edgeEndList = new ArrayList()
  19. if (arguments.length === 0) {
  20. this._nodes = new NodeMap(new NodeFactory())
  21. } else if (arguments.length === 1) {
  22. const nodeFact = arguments[0]
  23. this._nodes = new NodeMap(nodeFact)
  24. }
  25. }
  26. static linkResultDirectedEdges(nodes) {
  27. for (let nodeit = nodes.iterator(); nodeit.hasNext(); ) {
  28. const node = nodeit.next()
  29. node.getEdges().linkResultDirectedEdges()
  30. }
  31. }
  32. printEdges(out) {
  33. out.println('Edges:')
  34. for (let i = 0; i < this._edges.size(); i++) {
  35. out.println('edge ' + i + ':')
  36. const e = this._edges.get(i)
  37. e.print(out)
  38. e.eiList.print(out)
  39. }
  40. }
  41. find(coord) {
  42. return this._nodes.find(coord)
  43. }
  44. addNode() {
  45. if (arguments[0] instanceof Node) {
  46. const node = arguments[0]
  47. return this._nodes.addNode(node)
  48. } else if (arguments[0] instanceof Coordinate) {
  49. const coord = arguments[0]
  50. return this._nodes.addNode(coord)
  51. }
  52. }
  53. getNodeIterator() {
  54. return this._nodes.iterator()
  55. }
  56. linkResultDirectedEdges() {
  57. for (let nodeit = this._nodes.iterator(); nodeit.hasNext(); ) {
  58. const node = nodeit.next()
  59. node.getEdges().linkResultDirectedEdges()
  60. }
  61. }
  62. debugPrintln(o) {
  63. System.out.println(o)
  64. }
  65. isBoundaryNode(geomIndex, coord) {
  66. const node = this._nodes.find(coord)
  67. if (node === null) return false
  68. const label = node.getLabel()
  69. if (label !== null && label.getLocation(geomIndex) === Location.BOUNDARY) return true
  70. return false
  71. }
  72. linkAllDirectedEdges() {
  73. for (let nodeit = this._nodes.iterator(); nodeit.hasNext(); ) {
  74. const node = nodeit.next()
  75. node.getEdges().linkAllDirectedEdges()
  76. }
  77. }
  78. matchInSameDirection(p0, p1, ep0, ep1) {
  79. if (!p0.equals(ep0)) return false
  80. if (Orientation.index(p0, p1, ep1) === Orientation.COLLINEAR && Quadrant.quadrant(p0, p1) === Quadrant.quadrant(ep0, ep1)) return true
  81. return false
  82. }
  83. getEdgeEnds() {
  84. return this._edgeEndList
  85. }
  86. debugPrint(o) {
  87. System.out.print(o)
  88. }
  89. getEdgeIterator() {
  90. return this._edges.iterator()
  91. }
  92. findEdgeInSameDirection(p0, p1) {
  93. for (let i = 0; i < this._edges.size(); i++) {
  94. const e = this._edges.get(i)
  95. const eCoord = e.getCoordinates()
  96. if (this.matchInSameDirection(p0, p1, eCoord[0], eCoord[1])) return e
  97. if (this.matchInSameDirection(p0, p1, eCoord[eCoord.length - 1], eCoord[eCoord.length - 2])) return e
  98. }
  99. return null
  100. }
  101. insertEdge(e) {
  102. this._edges.add(e)
  103. }
  104. findEdgeEnd(e) {
  105. for (let i = this.getEdgeEnds().iterator(); i.hasNext(); ) {
  106. const ee = i.next()
  107. if (ee.getEdge() === e) return ee
  108. }
  109. return null
  110. }
  111. addEdges(edgesToAdd) {
  112. for (let it = edgesToAdd.iterator(); it.hasNext(); ) {
  113. const e = it.next()
  114. this._edges.add(e)
  115. const de1 = new DirectedEdge(e, true)
  116. const de2 = new DirectedEdge(e, false)
  117. de1.setSym(de2)
  118. de2.setSym(de1)
  119. this.add(de1)
  120. this.add(de2)
  121. }
  122. }
  123. add(e) {
  124. this._nodes.add(e)
  125. this._edgeEndList.add(e)
  126. }
  127. getNodes() {
  128. return this._nodes.values()
  129. }
  130. findEdge(p0, p1) {
  131. for (let i = 0; i < this._edges.size(); i++) {
  132. const e = this._edges.get(i)
  133. const eCoord = e.getCoordinates()
  134. if (p0.equals(eCoord[0]) && p1.equals(eCoord[1])) return e
  135. }
  136. return null
  137. }
  138. }