EdgeEndStar.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import StringBuffer from '../../../../java/lang/StringBuffer'
  2. import Location from '../geom/Location'
  3. import Position from './Position'
  4. import TopologyException from '../geom/TopologyException'
  5. import System from '../../../../java/lang/System'
  6. import SimplePointInAreaLocator from '../algorithm/locate/SimplePointInAreaLocator'
  7. import ArrayList from '../../../../java/util/ArrayList'
  8. import Assert from '../util/Assert'
  9. import TreeMap from '../../../../java/util/TreeMap'
  10. export default class EdgeEndStar {
  11. constructor() {
  12. EdgeEndStar.constructor_.apply(this, arguments)
  13. }
  14. static constructor_() {
  15. this._edgeMap = new TreeMap()
  16. this._edgeList = null
  17. this._ptInAreaLocation = [Location.NONE, Location.NONE]
  18. }
  19. getNextCW(ee) {
  20. this.getEdges()
  21. const i = this._edgeList.indexOf(ee)
  22. let iNextCW = i - 1
  23. if (i === 0) iNextCW = this._edgeList.size() - 1
  24. return this._edgeList.get(iNextCW)
  25. }
  26. propagateSideLabels(geomIndex) {
  27. let startLoc = Location.NONE
  28. for (let it = this.iterator(); it.hasNext(); ) {
  29. const e = it.next()
  30. const label = e.getLabel()
  31. if (label.isArea(geomIndex) && label.getLocation(geomIndex, Position.LEFT) !== Location.NONE) startLoc = label.getLocation(geomIndex, Position.LEFT)
  32. }
  33. if (startLoc === Location.NONE) return null
  34. let currLoc = startLoc
  35. for (let it = this.iterator(); it.hasNext(); ) {
  36. const e = it.next()
  37. const label = e.getLabel()
  38. if (label.getLocation(geomIndex, Position.ON) === Location.NONE) label.setLocation(geomIndex, Position.ON, currLoc)
  39. if (label.isArea(geomIndex)) {
  40. const leftLoc = label.getLocation(geomIndex, Position.LEFT)
  41. const rightLoc = label.getLocation(geomIndex, Position.RIGHT)
  42. if (rightLoc !== Location.NONE) {
  43. if (rightLoc !== currLoc) throw new TopologyException('side location conflict', e.getCoordinate())
  44. if (leftLoc === Location.NONE)
  45. Assert.shouldNeverReachHere('found single null side (at ' + e.getCoordinate() + ')')
  46. currLoc = leftLoc
  47. } else {
  48. Assert.isTrue(label.getLocation(geomIndex, Position.LEFT) === Location.NONE, 'found single null side')
  49. label.setLocation(geomIndex, Position.RIGHT, currLoc)
  50. label.setLocation(geomIndex, Position.LEFT, currLoc)
  51. }
  52. }
  53. }
  54. }
  55. getCoordinate() {
  56. const it = this.iterator()
  57. if (!it.hasNext()) return null
  58. const e = it.next()
  59. return e.getCoordinate()
  60. }
  61. print(out) {
  62. System.out.println('EdgeEndStar: ' + this.getCoordinate())
  63. for (let it = this.iterator(); it.hasNext(); ) {
  64. const e = it.next()
  65. e.print(out)
  66. }
  67. }
  68. isAreaLabelsConsistent(geomGraph) {
  69. this.computeEdgeEndLabels(geomGraph.getBoundaryNodeRule())
  70. return this.checkAreaLabelsConsistent(0)
  71. }
  72. checkAreaLabelsConsistent(geomIndex) {
  73. const edges = this.getEdges()
  74. if (edges.size() <= 0) return true
  75. const lastEdgeIndex = edges.size() - 1
  76. const startLabel = edges.get(lastEdgeIndex).getLabel()
  77. const startLoc = startLabel.getLocation(geomIndex, Position.LEFT)
  78. Assert.isTrue(startLoc !== Location.NONE, 'Found unlabelled area edge')
  79. let currLoc = startLoc
  80. for (let it = this.iterator(); it.hasNext(); ) {
  81. const e = it.next()
  82. const label = e.getLabel()
  83. Assert.isTrue(label.isArea(geomIndex), 'Found non-area edge')
  84. const leftLoc = label.getLocation(geomIndex, Position.LEFT)
  85. const rightLoc = label.getLocation(geomIndex, Position.RIGHT)
  86. if (leftLoc === rightLoc)
  87. return false
  88. if (rightLoc !== currLoc)
  89. return false
  90. currLoc = leftLoc
  91. }
  92. return true
  93. }
  94. findIndex(eSearch) {
  95. this.iterator()
  96. for (let i = 0; i < this._edgeList.size(); i++) {
  97. const e = this._edgeList.get(i)
  98. if (e === eSearch) return i
  99. }
  100. return -1
  101. }
  102. iterator() {
  103. return this.getEdges().iterator()
  104. }
  105. getEdges() {
  106. if (this._edgeList === null)
  107. this._edgeList = new ArrayList(this._edgeMap.values())
  108. return this._edgeList
  109. }
  110. getLocation(geomIndex, p, geom) {
  111. if (this._ptInAreaLocation[geomIndex] === Location.NONE)
  112. this._ptInAreaLocation[geomIndex] = SimplePointInAreaLocator.locate(p, geom[geomIndex].getGeometry())
  113. return this._ptInAreaLocation[geomIndex]
  114. }
  115. toString() {
  116. const buf = new StringBuffer()
  117. buf.append('EdgeEndStar: ' + this.getCoordinate())
  118. buf.append('\n')
  119. for (let it = this.iterator(); it.hasNext(); ) {
  120. const e = it.next()
  121. buf.append(e)
  122. buf.append('\n')
  123. }
  124. return buf.toString()
  125. }
  126. computeEdgeEndLabels(boundaryNodeRule) {
  127. for (let it = this.iterator(); it.hasNext(); ) {
  128. const ee = it.next()
  129. ee.computeLabel(boundaryNodeRule)
  130. }
  131. }
  132. computeLabelling(geomGraph) {
  133. this.computeEdgeEndLabels(geomGraph[0].getBoundaryNodeRule())
  134. this.propagateSideLabels(0)
  135. this.propagateSideLabels(1)
  136. const hasDimensionalCollapseEdge = [false, false]
  137. for (let it = this.iterator(); it.hasNext(); ) {
  138. const e = it.next()
  139. const label = e.getLabel()
  140. for (let geomi = 0; geomi < 2; geomi++)
  141. if (label.isLine(geomi) && label.getLocation(geomi) === Location.BOUNDARY) hasDimensionalCollapseEdge[geomi] = true
  142. }
  143. for (let it = this.iterator(); it.hasNext(); ) {
  144. const e = it.next()
  145. const label = e.getLabel()
  146. for (let geomi = 0; geomi < 2; geomi++)
  147. if (label.isAnyNull(geomi)) {
  148. let loc = Location.NONE
  149. if (hasDimensionalCollapseEdge[geomi]) {
  150. loc = Location.EXTERIOR
  151. } else {
  152. const p = e.getCoordinate()
  153. loc = this.getLocation(geomi, p, geomGraph)
  154. }
  155. label.setAllLocationsIfNull(geomi, loc)
  156. }
  157. }
  158. }
  159. getDegree() {
  160. return this._edgeMap.size()
  161. }
  162. insertEdgeEnd(e, obj) {
  163. this._edgeMap.put(e, obj)
  164. this._edgeList = null
  165. }
  166. }