MCIndexPointSnapper.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import MonotoneChainSelectAction from '../../index/chain/MonotoneChainSelectAction'
  2. import MonotoneChain from '../../index/chain/MonotoneChain'
  3. import ItemVisitor from '../../index/ItemVisitor'
  4. export default class MCIndexPointSnapper {
  5. constructor() {
  6. MCIndexPointSnapper.constructor_.apply(this, arguments)
  7. }
  8. static constructor_() {
  9. this._index = null
  10. const index = arguments[0]
  11. this._index = index
  12. }
  13. snap() {
  14. if (arguments.length === 1) {
  15. const hotPixel = arguments[0]
  16. return this.snap(hotPixel, null, -1)
  17. } else if (arguments.length === 3) {
  18. const hotPixel = arguments[0], parentEdge = arguments[1], hotPixelVertexIndex = arguments[2]
  19. const pixelEnv = hotPixel.getSafeEnvelope()
  20. const hotPixelSnapAction = new HotPixelSnapAction(hotPixel, parentEdge, hotPixelVertexIndex)
  21. this._index.query(pixelEnv, new (class {
  22. get interfaces_() {
  23. return [ItemVisitor]
  24. }
  25. visitItem(item) {
  26. const testChain = item
  27. testChain.select(pixelEnv, hotPixelSnapAction)
  28. }
  29. })())
  30. return hotPixelSnapAction.isNodeAdded()
  31. }
  32. }
  33. }
  34. class HotPixelSnapAction extends MonotoneChainSelectAction {
  35. constructor() {
  36. super()
  37. HotPixelSnapAction.constructor_.apply(this, arguments)
  38. }
  39. static constructor_() {
  40. this._hotPixel = null
  41. this._parentEdge = null
  42. this._hotPixelVertexIndex = null
  43. this._isNodeAdded = false
  44. const hotPixel = arguments[0], parentEdge = arguments[1], hotPixelVertexIndex = arguments[2]
  45. this._hotPixel = hotPixel
  46. this._parentEdge = parentEdge
  47. this._hotPixelVertexIndex = hotPixelVertexIndex
  48. }
  49. isNodeAdded() {
  50. return this._isNodeAdded
  51. }
  52. select() {
  53. if (arguments.length === 2 && (Number.isInteger(arguments[1]) && arguments[0] instanceof MonotoneChain)) {
  54. const mc = arguments[0], startIndex = arguments[1]
  55. const ss = mc.getContext()
  56. if (this._parentEdge === ss)
  57. if (startIndex === this._hotPixelVertexIndex || startIndex + 1 === this._hotPixelVertexIndex) return null
  58. this._isNodeAdded |= this._hotPixel.addSnappedNode(ss, startIndex)
  59. } else {
  60. return super.select.apply(this, arguments)
  61. }
  62. }
  63. }
  64. MCIndexPointSnapper.HotPixelSnapAction = HotPixelSnapAction