TopologyLocation.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import StringBuffer from '../../../../java/lang/StringBuffer'
  2. import Location from '../geom/Location'
  3. import Position from './Position'
  4. export default class TopologyLocation {
  5. constructor() {
  6. TopologyLocation.constructor_.apply(this, arguments)
  7. }
  8. static constructor_() {
  9. this.location = null
  10. if (arguments.length === 1) {
  11. if (arguments[0] instanceof Array) {
  12. const location = arguments[0]
  13. this.init(location.length)
  14. } else if (Number.isInteger(arguments[0])) {
  15. const on = arguments[0]
  16. this.init(1)
  17. this.location[Position.ON] = on
  18. } else if (arguments[0] instanceof TopologyLocation) {
  19. const gl = arguments[0]
  20. this.init(gl.location.length)
  21. if (gl !== null)
  22. for (let i = 0; i < this.location.length; i++)
  23. this.location[i] = gl.location[i]
  24. }
  25. } else if (arguments.length === 3) {
  26. const on = arguments[0], left = arguments[1], right = arguments[2]
  27. this.init(3)
  28. this.location[Position.ON] = on
  29. this.location[Position.LEFT] = left
  30. this.location[Position.RIGHT] = right
  31. }
  32. }
  33. setAllLocations(locValue) {
  34. for (let i = 0; i < this.location.length; i++)
  35. this.location[i] = locValue
  36. }
  37. isNull() {
  38. for (let i = 0; i < this.location.length; i++)
  39. if (this.location[i] !== Location.NONE) return false
  40. return true
  41. }
  42. setAllLocationsIfNull(locValue) {
  43. for (let i = 0; i < this.location.length; i++)
  44. if (this.location[i] === Location.NONE) this.location[i] = locValue
  45. }
  46. isLine() {
  47. return this.location.length === 1
  48. }
  49. merge(gl) {
  50. if (gl.location.length > this.location.length) {
  51. const newLoc = new Array(3).fill(null)
  52. newLoc[Position.ON] = this.location[Position.ON]
  53. newLoc[Position.LEFT] = Location.NONE
  54. newLoc[Position.RIGHT] = Location.NONE
  55. this.location = newLoc
  56. }
  57. for (let i = 0; i < this.location.length; i++)
  58. if (this.location[i] === Location.NONE && i < gl.location.length) this.location[i] = gl.location[i]
  59. }
  60. getLocations() {
  61. return this.location
  62. }
  63. flip() {
  64. if (this.location.length <= 1) return null
  65. const temp = this.location[Position.LEFT]
  66. this.location[Position.LEFT] = this.location[Position.RIGHT]
  67. this.location[Position.RIGHT] = temp
  68. }
  69. toString() {
  70. const buf = new StringBuffer()
  71. if (this.location.length > 1) buf.append(Location.toLocationSymbol(this.location[Position.LEFT]))
  72. buf.append(Location.toLocationSymbol(this.location[Position.ON]))
  73. if (this.location.length > 1) buf.append(Location.toLocationSymbol(this.location[Position.RIGHT]))
  74. return buf.toString()
  75. }
  76. setLocations(on, left, right) {
  77. this.location[Position.ON] = on
  78. this.location[Position.LEFT] = left
  79. this.location[Position.RIGHT] = right
  80. }
  81. get(posIndex) {
  82. if (posIndex < this.location.length) return this.location[posIndex]
  83. return Location.NONE
  84. }
  85. isArea() {
  86. return this.location.length > 1
  87. }
  88. isAnyNull() {
  89. for (let i = 0; i < this.location.length; i++)
  90. if (this.location[i] === Location.NONE) return true
  91. return false
  92. }
  93. setLocation() {
  94. if (arguments.length === 1) {
  95. const locValue = arguments[0]
  96. this.setLocation(Position.ON, locValue)
  97. } else if (arguments.length === 2) {
  98. const locIndex = arguments[0], locValue = arguments[1]
  99. this.location[locIndex] = locValue
  100. }
  101. }
  102. init(size) {
  103. this.location = new Array(size).fill(null)
  104. this.setAllLocations(Location.NONE)
  105. }
  106. isEqualOnSide(le, locIndex) {
  107. return this.location[locIndex] === le.location[locIndex]
  108. }
  109. allPositionsEqual(loc) {
  110. for (let i = 0; i < this.location.length; i++)
  111. if (this.location[i] !== loc) return false
  112. return true
  113. }
  114. }