SubgraphDepthLocater.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import hasInterface from '../../../../../hasInterface'
  2. import Position from '../../geomgraph/Position'
  3. import Coordinate from '../../geom/Coordinate'
  4. import Orientation from '../../algorithm/Orientation'
  5. import Collections from '../../../../../java/util/Collections'
  6. import DirectedEdge from '../../geomgraph/DirectedEdge'
  7. import LineSegment from '../../geom/LineSegment'
  8. import Comparable from '../../../../../java/lang/Comparable'
  9. import ArrayList from '../../../../../java/util/ArrayList'
  10. import List from '../../../../../java/util/List'
  11. export default class SubgraphDepthLocater {
  12. constructor() {
  13. SubgraphDepthLocater.constructor_.apply(this, arguments)
  14. }
  15. static constructor_() {
  16. this._subgraphs = null
  17. this._seg = new LineSegment()
  18. const subgraphs = arguments[0]
  19. this._subgraphs = subgraphs
  20. }
  21. findStabbedSegments() {
  22. if (arguments.length === 1) {
  23. const stabbingRayLeftPt = arguments[0]
  24. const stabbedSegments = new ArrayList()
  25. for (let i = this._subgraphs.iterator(); i.hasNext(); ) {
  26. const bsg = i.next()
  27. const env = bsg.getEnvelope()
  28. if (stabbingRayLeftPt.y < env.getMinY() || stabbingRayLeftPt.y > env.getMaxY()) continue
  29. this.findStabbedSegments(stabbingRayLeftPt, bsg.getDirectedEdges(), stabbedSegments)
  30. }
  31. return stabbedSegments
  32. } else if (arguments.length === 3) {
  33. if (hasInterface(arguments[2], List) && (arguments[0] instanceof Coordinate && arguments[1] instanceof DirectedEdge)) {
  34. const stabbingRayLeftPt = arguments[0], dirEdge = arguments[1], stabbedSegments = arguments[2]
  35. const pts = dirEdge.getEdge().getCoordinates()
  36. for (let i = 0; i < pts.length - 1; i++) {
  37. this._seg.p0 = pts[i]
  38. this._seg.p1 = pts[i + 1]
  39. if (this._seg.p0.y > this._seg.p1.y) this._seg.reverse()
  40. const maxx = Math.max(this._seg.p0.x, this._seg.p1.x)
  41. if (maxx < stabbingRayLeftPt.x) continue
  42. if (this._seg.isHorizontal()) continue
  43. if (stabbingRayLeftPt.y < this._seg.p0.y || stabbingRayLeftPt.y > this._seg.p1.y) continue
  44. if (Orientation.index(this._seg.p0, this._seg.p1, stabbingRayLeftPt) === Orientation.RIGHT) continue
  45. let depth = dirEdge.getDepth(Position.LEFT)
  46. if (!this._seg.p0.equals(pts[i])) depth = dirEdge.getDepth(Position.RIGHT)
  47. const ds = new DepthSegment(this._seg, depth)
  48. stabbedSegments.add(ds)
  49. }
  50. } else if (hasInterface(arguments[2], List) && (arguments[0] instanceof Coordinate && hasInterface(arguments[1], List))) {
  51. const stabbingRayLeftPt = arguments[0], dirEdges = arguments[1], stabbedSegments = arguments[2]
  52. for (let i = dirEdges.iterator(); i.hasNext(); ) {
  53. const de = i.next()
  54. if (!de.isForward()) continue
  55. this.findStabbedSegments(stabbingRayLeftPt, de, stabbedSegments)
  56. }
  57. }
  58. }
  59. }
  60. getDepth(p) {
  61. const stabbedSegments = this.findStabbedSegments(p)
  62. if (stabbedSegments.size() === 0) return 0
  63. const ds = Collections.min(stabbedSegments)
  64. return ds._leftDepth
  65. }
  66. }
  67. class DepthSegment {
  68. constructor() {
  69. DepthSegment.constructor_.apply(this, arguments)
  70. }
  71. static constructor_() {
  72. this._upwardSeg = null
  73. this._leftDepth = null
  74. const seg = arguments[0], depth = arguments[1]
  75. this._upwardSeg = new LineSegment(seg)
  76. this._leftDepth = depth
  77. }
  78. compareTo(obj) {
  79. const other = obj
  80. if (this._upwardSeg.minX() >= other._upwardSeg.maxX()) return 1
  81. if (this._upwardSeg.maxX() <= other._upwardSeg.minX()) return -1
  82. let orientIndex = this._upwardSeg.orientationIndex(other._upwardSeg)
  83. if (orientIndex !== 0) return orientIndex
  84. orientIndex = -1 * other._upwardSeg.orientationIndex(this._upwardSeg)
  85. if (orientIndex !== 0) return orientIndex
  86. return this._upwardSeg.compareTo(other._upwardSeg)
  87. }
  88. compareX(seg0, seg1) {
  89. const compare0 = seg0.p0.compareTo(seg1.p0)
  90. if (compare0 !== 0) return compare0
  91. return seg0.p1.compareTo(seg1.p1)
  92. }
  93. toString() {
  94. return this._upwardSeg.toString()
  95. }
  96. get interfaces_() {
  97. return [Comparable]
  98. }
  99. }
  100. SubgraphDepthLocater.DepthSegment = DepthSegment