ScaledNoder.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import hasInterface from '../../../../hasInterface'
  2. import Collection from '../../../../java/util/Collection'
  3. import Noder from './Noder'
  4. import Coordinate from '../geom/Coordinate'
  5. import NodedSegmentString from './NodedSegmentString'
  6. import System from '../../../../java/lang/System'
  7. import CoordinateArrays from '../geom/CoordinateArrays'
  8. import ArrayList from '../../../../java/util/ArrayList'
  9. export default class ScaledNoder {
  10. constructor() {
  11. ScaledNoder.constructor_.apply(this, arguments)
  12. }
  13. static constructor_() {
  14. this._noder = null
  15. this._scaleFactor = null
  16. this._offsetX = null
  17. this._offsetY = null
  18. this._isScaled = false
  19. if (arguments.length === 2) {
  20. const noder = arguments[0], scaleFactor = arguments[1]
  21. ScaledNoder.constructor_.call(this, noder, scaleFactor, 0, 0)
  22. } else if (arguments.length === 4) {
  23. const noder = arguments[0], scaleFactor = arguments[1], offsetX = arguments[2], offsetY = arguments[3]
  24. this._noder = noder
  25. this._scaleFactor = scaleFactor
  26. this._isScaled = !this.isIntegerPrecision()
  27. }
  28. }
  29. rescale() {
  30. if (hasInterface(arguments[0], Collection)) {
  31. const segStrings = arguments[0]
  32. for (let i = segStrings.iterator(); i.hasNext(); ) {
  33. const ss = i.next()
  34. this.rescale(ss.getCoordinates())
  35. }
  36. } else if (arguments[0] instanceof Array) {
  37. const pts = arguments[0]
  38. for (let i = 0; i < pts.length; i++) {
  39. pts[i].x = pts[i].x / this._scaleFactor + this._offsetX
  40. pts[i].y = pts[i].y / this._scaleFactor + this._offsetY
  41. }
  42. if (pts.length === 2 && pts[0].equals2D(pts[1]))
  43. System.out.println(pts)
  44. }
  45. }
  46. scale() {
  47. if (hasInterface(arguments[0], Collection)) {
  48. const segStrings = arguments[0]
  49. const nodedSegmentStrings = new ArrayList(segStrings.size())
  50. for (let i = segStrings.iterator(); i.hasNext(); ) {
  51. const ss = i.next()
  52. nodedSegmentStrings.add(new NodedSegmentString(this.scale(ss.getCoordinates()), ss.getData()))
  53. }
  54. return nodedSegmentStrings
  55. } else if (arguments[0] instanceof Array) {
  56. const pts = arguments[0]
  57. const roundPts = new Array(pts.length).fill(null)
  58. for (let i = 0; i < pts.length; i++)
  59. roundPts[i] = new Coordinate(Math.round((pts[i].x - this._offsetX) * this._scaleFactor), Math.round((pts[i].y - this._offsetY) * this._scaleFactor), pts[i].getZ())
  60. const roundPtsNoDup = CoordinateArrays.removeRepeatedPoints(roundPts)
  61. return roundPtsNoDup
  62. }
  63. }
  64. isIntegerPrecision() {
  65. return this._scaleFactor === 1.0
  66. }
  67. getNodedSubstrings() {
  68. const splitSS = this._noder.getNodedSubstrings()
  69. if (this._isScaled) this.rescale(splitSS)
  70. return splitSS
  71. }
  72. computeNodes(inputSegStrings) {
  73. let intSegStrings = inputSegStrings
  74. if (this._isScaled) intSegStrings = this.scale(inputSegStrings)
  75. this._noder.computeNodes(intSegStrings)
  76. }
  77. get interfaces_() {
  78. return [Noder]
  79. }
  80. }