Home Reference Source

src/octree/world/KeyIterator.js

  1. import { Vector3 } from "three";
  2.  
  3. /**
  4. * A key range iterator.
  5. *
  6. * @implements {Iterator}
  7. * @implements {Iterable}
  8. */
  9.  
  10. export class KeyIterator {
  11.  
  12. /**
  13. * Constructs a new key iterator.
  14. *
  15. * This iterator returns all keys in the specified coordinate range, including
  16. * those at min and max.
  17. *
  18. * @param {KeyDesign} keyDesign - A key design.
  19. * @param {Vector3} min - The lower index bounds (zero-based unsigned integer coordinates).
  20. * @param {Vector3} max - The upper index bounds (zero-based unsigned integer coordinates).
  21. */
  22.  
  23. constructor(keyDesign, min, max) {
  24.  
  25. /**
  26. * The key design.
  27. *
  28. * @type {KeyDesign}
  29. * @private
  30. */
  31.  
  32. this.keyDesign = keyDesign;
  33.  
  34. /**
  35. * The lower index bounds.
  36. *
  37. * @type {Vector3}
  38. * @private
  39. */
  40.  
  41. this.min = min;
  42.  
  43. /**
  44. * The upper index bounds.
  45. *
  46. * @type {Vector3}
  47. * @private
  48. */
  49.  
  50. this.max = max;
  51.  
  52. /**
  53. * The base key coordinates.
  54. *
  55. * @type {Vector3}
  56. * @private
  57. */
  58.  
  59. this.keyBase = new Vector3();
  60.  
  61. /**
  62. * The current key iteration coordinates.
  63. *
  64. * @type {Vector3}
  65. * @private
  66. */
  67.  
  68. this.key = new Vector3();
  69.  
  70. /**
  71. * The iteration limits.
  72. *
  73. * @type {Vector3}
  74. * @private
  75. */
  76.  
  77. this.limit = new Vector3();
  78.  
  79. /**
  80. * An iterator result.
  81. *
  82. * @type {IteratorResult}
  83. * @private
  84. */
  85.  
  86. this.result = {
  87. value: null,
  88. done: false
  89. };
  90.  
  91. this.reset();
  92.  
  93. }
  94.  
  95. /**
  96. * Resets this iterator.
  97. *
  98. * @return {KeyIterator} This iterator.
  99. */
  100.  
  101. reset() {
  102.  
  103. const keyDesign = this.keyDesign;
  104. const min = this.min;
  105. const max = this.max;
  106.  
  107. if(min.x <= max.x && min.y <= max.y && min.z <= max.z) {
  108.  
  109. this.keyBase.set(min.x, min.y * keyDesign.rangeX, min.z * keyDesign.rangeXY);
  110. this.limit.set(max.x, max.y * keyDesign.rangeX, max.z * keyDesign.rangeXY);
  111. this.key.copy(this.keyBase);
  112.  
  113. } else {
  114.  
  115. // The range is invalid. Return no keys.
  116. this.keyBase.set(1, 1, 1);
  117. this.limit.set(0, 0, 0);
  118. this.key.copy(this.keyBase);
  119.  
  120. console.error("Invalid key range", min, max);
  121.  
  122. }
  123.  
  124. this.result.value = null;
  125. this.result.done = false;
  126.  
  127. return this;
  128.  
  129. }
  130.  
  131. /**
  132. * Iterates over the key range.
  133. *
  134. * @return {IteratorResult} The next key.
  135. */
  136.  
  137. next() {
  138.  
  139. const result = this.result;
  140. const keyDesign = this.keyDesign;
  141. const keyBase = this.keyBase;
  142. const limit = this.limit;
  143. const key = this.key;
  144.  
  145. if(key.z <= limit.z) {
  146.  
  147. // Put the key pieces together.
  148. result.value = key.z + key.y + key.x;
  149.  
  150. // Advance the key coordinates.
  151. ++key.x;
  152.  
  153. if(key.x > limit.x) {
  154.  
  155. key.x = keyBase.x;
  156. key.y += keyDesign.rangeX;
  157.  
  158. if(key.y > limit.y) {
  159.  
  160. key.y = keyBase.y;
  161. key.z += keyDesign.rangeXY;
  162.  
  163. }
  164.  
  165. }
  166.  
  167. } else {
  168.  
  169. result.value = null;
  170. result.done = true;
  171.  
  172. }
  173.  
  174. return result;
  175.  
  176. }
  177.  
  178. /**
  179. * Called when this iterator will no longer be run to completion.
  180. *
  181. * @param {Object} value - An interator result value.
  182. * @return {IteratorResult} - A premature completion result.
  183. */
  184.  
  185. return(value) {
  186.  
  187. this.result.value = value;
  188. this.result.done = true;
  189.  
  190. return this.result;
  191.  
  192. }
  193.  
  194. /**
  195. * Returns this iterator.
  196. *
  197. * @return {KeyIterator} An iterator.
  198. */
  199.  
  200. [Symbol.iterator]() {
  201.  
  202. return this;
  203.  
  204. }
  205.  
  206. }