MCIndexSnapRounder.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 MCIndexPointSnapper from './MCIndexPointSnapper'
  10. import RobustLineIntersector from '../../algorithm/RobustLineIntersector'
  11. import InteriorIntersectionFinderAdder from '../InteriorIntersectionFinderAdder'
  12. export default class MCIndexSnapRounder {
  13. constructor() {
  14. MCIndexSnapRounder.constructor_.apply(this, arguments)
  15. }
  16. static constructor_() {
  17. this._pm = null
  18. this._li = null
  19. this._scaleFactor = null
  20. this._noder = null
  21. this._pointSnapper = null
  22. this._nodedSegStrings = null
  23. const pm = arguments[0]
  24. this._pm = pm
  25. this._li = new RobustLineIntersector()
  26. this._li.setPrecisionModel(pm)
  27. this._scaleFactor = pm.getScale()
  28. }
  29. checkCorrectness(inputSegmentStrings) {
  30. const resultSegStrings = NodedSegmentString.getNodedSubstrings(inputSegmentStrings)
  31. const nv = new NodingValidator(resultSegStrings)
  32. try {
  33. nv.checkValid()
  34. } catch (ex) {
  35. if (ex instanceof Exception)
  36. ex.printStackTrace()
  37. else throw ex
  38. } finally {}
  39. }
  40. getNodedSubstrings() {
  41. return NodedSegmentString.getNodedSubstrings(this._nodedSegStrings)
  42. }
  43. snapRound(segStrings, li) {
  44. const intersections = this.findInteriorIntersections(segStrings, li)
  45. this.computeIntersectionSnaps(intersections)
  46. this.computeVertexSnaps(segStrings)
  47. }
  48. findInteriorIntersections(segStrings, li) {
  49. const intFinderAdder = new InteriorIntersectionFinderAdder(li)
  50. this._noder.setSegmentIntersector(intFinderAdder)
  51. this._noder.computeNodes(segStrings)
  52. return intFinderAdder.getInteriorIntersections()
  53. }
  54. computeVertexSnaps() {
  55. if (hasInterface(arguments[0], Collection)) {
  56. const edges = arguments[0]
  57. for (let i0 = edges.iterator(); i0.hasNext(); ) {
  58. const edge0 = i0.next()
  59. this.computeVertexSnaps(edge0)
  60. }
  61. } else if (arguments[0] instanceof NodedSegmentString) {
  62. const e = arguments[0]
  63. const pts0 = e.getCoordinates()
  64. for (let i = 0; i < pts0.length; i++) {
  65. const hotPixel = new HotPixel(pts0[i], this._scaleFactor, this._li)
  66. const isNodeAdded = this._pointSnapper.snap(hotPixel, e, i)
  67. if (isNodeAdded)
  68. e.addIntersection(pts0[i], i)
  69. }
  70. }
  71. }
  72. computeNodes(inputSegmentStrings) {
  73. this._nodedSegStrings = inputSegmentStrings
  74. this._noder = new MCIndexNoder()
  75. this._pointSnapper = new MCIndexPointSnapper(this._noder.getIndex())
  76. this.snapRound(inputSegmentStrings, this._li)
  77. }
  78. computeIntersectionSnaps(snapPts) {
  79. for (let it = snapPts.iterator(); it.hasNext(); ) {
  80. const snapPt = it.next()
  81. const hotPixel = new HotPixel(snapPt, this._scaleFactor, this._li)
  82. this._pointSnapper.snap(hotPixel)
  83. }
  84. }
  85. get interfaces_() {
  86. return [Noder]
  87. }
  88. }