CommonBitsOp.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import CommonBitsRemover from './CommonBitsRemover'
  2. export default class CommonBitsOp {
  3. constructor() {
  4. CommonBitsOp.constructor_.apply(this, arguments)
  5. }
  6. static constructor_() {
  7. this._returnToOriginalPrecision = true
  8. this._cbr = null
  9. if (arguments.length === 0) {
  10. CommonBitsOp.constructor_.call(this, true)
  11. } else if (arguments.length === 1) {
  12. const returnToOriginalPrecision = arguments[0]
  13. this._returnToOriginalPrecision = returnToOriginalPrecision
  14. }
  15. }
  16. computeResultPrecision(result) {
  17. if (this._returnToOriginalPrecision) this._cbr.addCommonBits(result)
  18. return result
  19. }
  20. union(geom0, geom1) {
  21. const geom = this.removeCommonBits(geom0, geom1)
  22. return this.computeResultPrecision(geom[0].union(geom[1]))
  23. }
  24. intersection(geom0, geom1) {
  25. const geom = this.removeCommonBits(geom0, geom1)
  26. return this.computeResultPrecision(geom[0].intersection(geom[1]))
  27. }
  28. removeCommonBits() {
  29. if (arguments.length === 1) {
  30. const geom0 = arguments[0]
  31. this._cbr = new CommonBitsRemover()
  32. this._cbr.add(geom0)
  33. const geom = this._cbr.removeCommonBits(geom0.copy())
  34. return geom
  35. } else if (arguments.length === 2) {
  36. const geom0 = arguments[0], geom1 = arguments[1]
  37. this._cbr = new CommonBitsRemover()
  38. this._cbr.add(geom0)
  39. this._cbr.add(geom1)
  40. const geom = new Array(2).fill(null)
  41. geom[0] = this._cbr.removeCommonBits(geom0.copy())
  42. geom[1] = this._cbr.removeCommonBits(geom1.copy())
  43. return geom
  44. }
  45. }
  46. buffer(geom0, distance) {
  47. const geom = this.removeCommonBits(geom0)
  48. return this.computeResultPrecision(geom.buffer(distance))
  49. }
  50. symDifference(geom0, geom1) {
  51. const geom = this.removeCommonBits(geom0, geom1)
  52. return this.computeResultPrecision(geom[0].symDifference(geom[1]))
  53. }
  54. difference(geom0, geom1) {
  55. const geom = this.removeCommonBits(geom0, geom1)
  56. return this.computeResultPrecision(geom[0].difference(geom[1]))
  57. }
  58. }