SimpleSnapRounder.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import NodingValidator from '../NodingValidator'
  2. import hasInterface from '../../../../../hasInterface'
  3. import Collection from '../../../../../java/util/Collection'
  4. import Noder from '../Noder'
  5. import MCIndexNoder from '../MCIndexNoder'
  6. import NodedSegmentString from '../NodedSegmentString'
  7. import HotPixel from './HotPixel'
  8. import Exception from '../../../../../java/lang/Exception'
  9. import RobustLineIntersector from '../../algorithm/RobustLineIntersector'
  10. import InteriorIntersectionFinderAdder from '../InteriorIntersectionFinderAdder'
  11. export default class SimpleSnapRounder {
  12. constructor() {
  13. SimpleSnapRounder.constructor_.apply(this, arguments)
  14. }
  15. static constructor_() {
  16. this._pm = null
  17. this._li = null
  18. this._scaleFactor = null
  19. this._nodedSegStrings = null
  20. const pm = arguments[0]
  21. this._pm = pm
  22. this._li = new RobustLineIntersector()
  23. this._li.setPrecisionModel(pm)
  24. this._scaleFactor = pm.getScale()
  25. }
  26. checkCorrectness(inputSegmentStrings) {
  27. const resultSegStrings = NodedSegmentString.getNodedSubstrings(inputSegmentStrings)
  28. const nv = new NodingValidator(resultSegStrings)
  29. try {
  30. nv.checkValid()
  31. } catch (ex) {
  32. if (ex instanceof Exception)
  33. ex.printStackTrace()
  34. else throw ex
  35. } finally {}
  36. }
  37. getNodedSubstrings() {
  38. return NodedSegmentString.getNodedSubstrings(this._nodedSegStrings)
  39. }
  40. snapRound(segStrings, li) {
  41. const intersections = this.findInteriorIntersections(segStrings, li)
  42. this.computeSnaps(segStrings, intersections)
  43. this.computeVertexSnaps(segStrings)
  44. }
  45. findInteriorIntersections(segStrings, li) {
  46. const intFinderAdder = new InteriorIntersectionFinderAdder(li)
  47. const noder = new MCIndexNoder()
  48. noder.setSegmentIntersector(intFinderAdder)
  49. noder.computeNodes(segStrings)
  50. return intFinderAdder.getInteriorIntersections()
  51. }
  52. computeVertexSnaps() {
  53. if (arguments.length === 1) {
  54. const edges = arguments[0]
  55. for (let i0 = edges.iterator(); i0.hasNext(); ) {
  56. const edge0 = i0.next()
  57. for (let i1 = edges.iterator(); i1.hasNext(); ) {
  58. const edge1 = i1.next()
  59. this.computeVertexSnaps(edge0, edge1)
  60. }
  61. }
  62. } else if (arguments.length === 2) {
  63. const e0 = arguments[0], e1 = arguments[1]
  64. const pts0 = e0.getCoordinates()
  65. const pts1 = e1.getCoordinates()
  66. for (let i0 = 0; i0 < pts0.length - 1; i0++) {
  67. const hotPixel = new HotPixel(pts0[i0], this._scaleFactor, this._li)
  68. for (let i1 = 0; i1 < pts1.length - 1; i1++) {
  69. if (e0 === e1)
  70. if (i0 === i1) continue
  71. const isNodeAdded = hotPixel.addSnappedNode(e1, i1)
  72. if (isNodeAdded)
  73. e0.addIntersection(pts0[i0], i0)
  74. }
  75. }
  76. }
  77. }
  78. computeNodes(inputSegmentStrings) {
  79. this._nodedSegStrings = inputSegmentStrings
  80. this.snapRound(inputSegmentStrings, this._li)
  81. }
  82. computeSnaps() {
  83. if (hasInterface(arguments[0], Collection) && hasInterface(arguments[1], Collection)) {
  84. const segStrings = arguments[0], snapPts = arguments[1]
  85. for (let i0 = segStrings.iterator(); i0.hasNext(); ) {
  86. const ss = i0.next()
  87. this.computeSnaps(ss, snapPts)
  88. }
  89. } else if (arguments[0] instanceof NodedSegmentString && hasInterface(arguments[1], Collection)) {
  90. const ss = arguments[0], snapPts = arguments[1]
  91. for (let it = snapPts.iterator(); it.hasNext(); ) {
  92. const snapPt = it.next()
  93. const hotPixel = new HotPixel(snapPt, this._scaleFactor, this._li)
  94. for (let i = 0; i < ss.size() - 1; i++)
  95. hotPixel.addSnappedNode(ss, i)
  96. }
  97. }
  98. }
  99. get interfaces_() {
  100. return [Noder]
  101. }
  102. }