Depth.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import Location from '../geom/Location'
  2. import Position from './Position'
  3. export default class Depth {
  4. constructor() {
  5. Depth.constructor_.apply(this, arguments)
  6. }
  7. static constructor_() {
  8. this._depth = Array(2).fill().map(() => Array(3))
  9. for (let i = 0; i < 2; i++)
  10. for (let j = 0; j < 3; j++)
  11. this._depth[i][j] = Depth.NULL_VALUE
  12. }
  13. static depthAtLocation(location) {
  14. if (location === Location.EXTERIOR) return 0
  15. if (location === Location.INTERIOR) return 1
  16. return Depth.NULL_VALUE
  17. }
  18. getDepth(geomIndex, posIndex) {
  19. return this._depth[geomIndex][posIndex]
  20. }
  21. setDepth(geomIndex, posIndex, depthValue) {
  22. this._depth[geomIndex][posIndex] = depthValue
  23. }
  24. isNull() {
  25. if (arguments.length === 0) {
  26. for (let i = 0; i < 2; i++)
  27. for (let j = 0; j < 3; j++)
  28. if (this._depth[i][j] !== Depth.NULL_VALUE) return false
  29. return true
  30. } else if (arguments.length === 1) {
  31. const geomIndex = arguments[0]
  32. return this._depth[geomIndex][1] === Depth.NULL_VALUE
  33. } else if (arguments.length === 2) {
  34. const geomIndex = arguments[0], posIndex = arguments[1]
  35. return this._depth[geomIndex][posIndex] === Depth.NULL_VALUE
  36. }
  37. }
  38. normalize() {
  39. for (let i = 0; i < 2; i++)
  40. if (!this.isNull(i)) {
  41. let minDepth = this._depth[i][1]
  42. if (this._depth[i][2] < minDepth) minDepth = this._depth[i][2]
  43. if (minDepth < 0) minDepth = 0
  44. for (let j = 1; j < 3; j++) {
  45. let newValue = 0
  46. if (this._depth[i][j] > minDepth) newValue = 1
  47. this._depth[i][j] = newValue
  48. }
  49. }
  50. }
  51. getDelta(geomIndex) {
  52. return this._depth[geomIndex][Position.RIGHT] - this._depth[geomIndex][Position.LEFT]
  53. }
  54. getLocation(geomIndex, posIndex) {
  55. if (this._depth[geomIndex][posIndex] <= 0) return Location.EXTERIOR
  56. return Location.INTERIOR
  57. }
  58. toString() {
  59. return 'A: ' + this._depth[0][1] + ',' + this._depth[0][2] + ' B: ' + this._depth[1][1] + ',' + this._depth[1][2]
  60. }
  61. add() {
  62. if (arguments.length === 1) {
  63. const lbl = arguments[0]
  64. for (let i = 0; i < 2; i++)
  65. for (let j = 1; j < 3; j++) {
  66. const loc = lbl.getLocation(i, j)
  67. if (loc === Location.EXTERIOR || loc === Location.INTERIOR)
  68. if (this.isNull(i, j))
  69. this._depth[i][j] = Depth.depthAtLocation(loc)
  70. else this._depth[i][j] += Depth.depthAtLocation(loc)
  71. }
  72. } else if (arguments.length === 3) {
  73. const geomIndex = arguments[0], posIndex = arguments[1], location = arguments[2]
  74. if (location === Location.INTERIOR) this._depth[geomIndex][posIndex]++
  75. }
  76. }
  77. }
  78. Depth.NULL_VALUE = -1