IteratedNoder.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import Noder from './Noder'
  2. import MCIndexNoder from './MCIndexNoder'
  3. import TopologyException from '../geom/TopologyException'
  4. import RobustLineIntersector from '../algorithm/RobustLineIntersector'
  5. import IntersectionAdder from './IntersectionAdder'
  6. export default class IteratedNoder {
  7. constructor() {
  8. IteratedNoder.constructor_.apply(this, arguments)
  9. }
  10. static constructor_() {
  11. this._pm = null
  12. this._li = null
  13. this._nodedSegStrings = null
  14. this._maxIter = IteratedNoder.MAX_ITER
  15. const pm = arguments[0]
  16. this._li = new RobustLineIntersector()
  17. this._pm = pm
  18. this._li.setPrecisionModel(pm)
  19. }
  20. setMaximumIterations(maxIter) {
  21. this._maxIter = maxIter
  22. }
  23. node(segStrings, numInteriorIntersections) {
  24. const si = new IntersectionAdder(this._li)
  25. const noder = new MCIndexNoder()
  26. noder.setSegmentIntersector(si)
  27. noder.computeNodes(segStrings)
  28. this._nodedSegStrings = noder.getNodedSubstrings()
  29. numInteriorIntersections[0] = si.numInteriorIntersections
  30. }
  31. computeNodes(segStrings) {
  32. const numInteriorIntersections = new Array(1).fill(null)
  33. this._nodedSegStrings = segStrings
  34. let nodingIterationCount = 0
  35. let lastNodesCreated = -1
  36. do {
  37. this.node(this._nodedSegStrings, numInteriorIntersections)
  38. nodingIterationCount++
  39. const nodesCreated = numInteriorIntersections[0]
  40. if (lastNodesCreated > 0 && nodesCreated >= lastNodesCreated && nodingIterationCount > this._maxIter)
  41. throw new TopologyException('Iterated noding failed to converge after ' + nodingIterationCount + ' iterations')
  42. lastNodesCreated = nodesCreated
  43. } while (lastNodesCreated > 0)
  44. }
  45. getNodedSubstrings() {
  46. return this._nodedSegStrings
  47. }
  48. get interfaces_() {
  49. return [Noder]
  50. }
  51. }
  52. IteratedNoder.MAX_ITER = 5