octreeBlock.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * Class used to store a cell in an octree
  3. * @see http://doc.babylonjs.com/how_to/optimizing_your_scene_with_octrees
  4. */
  5. export class OctreeBlock<T> {
  6. /**
  7. * Gets the content of the current block
  8. */
  9. public entries = new Array<T>();
  10. /**
  11. * Gets the list of block children
  12. */
  13. public blocks: Array<OctreeBlock<T>>;
  14. private _depth: number;
  15. private _maxDepth: number;
  16. private _capacity: number;
  17. private _minPoint: Vector3;
  18. private _maxPoint: Vector3;
  19. private _boundingVectors = new Array<Vector3>();
  20. private _creationFunc: (entry: T, block: OctreeBlock<T>) => void;
  21. /**
  22. * Creates a new block
  23. * @param minPoint defines the minimum vector (in world space) of the block's bounding box
  24. * @param maxPoint defines the maximum vector (in world space) of the block's bounding box
  25. * @param capacity defines the maximum capacity of this block (if capacity is reached the block will be split into sub blocks)
  26. * @param depth defines the current depth of this block in the octree
  27. * @param maxDepth defines the maximal depth allowed (beyond this value, the capacity is ignored)
  28. * @param creationFunc defines a callback to call when an element is added to the block
  29. */
  30. constructor(minPoint: Vector3, maxPoint: Vector3, capacity: number, depth: number, maxDepth: number, creationFunc: (entry: T, block: OctreeBlock<T>) => void) {
  31. this._capacity = capacity;
  32. this._depth = depth;
  33. this._maxDepth = maxDepth;
  34. this._creationFunc = creationFunc;
  35. this._minPoint = minPoint;
  36. this._maxPoint = maxPoint;
  37. this._boundingVectors.push(minPoint.clone());
  38. this._boundingVectors.push(maxPoint.clone());
  39. this._boundingVectors.push(minPoint.clone());
  40. this._boundingVectors[2].x = maxPoint.x;
  41. this._boundingVectors.push(minPoint.clone());
  42. this._boundingVectors[3].y = maxPoint.y;
  43. this._boundingVectors.push(minPoint.clone());
  44. this._boundingVectors[4].z = maxPoint.z;
  45. this._boundingVectors.push(maxPoint.clone());
  46. this._boundingVectors[5].z = minPoint.z;
  47. this._boundingVectors.push(maxPoint.clone());
  48. this._boundingVectors[6].x = minPoint.x;
  49. this._boundingVectors.push(maxPoint.clone());
  50. this._boundingVectors[7].y = minPoint.y;
  51. }
  52. // Property
  53. /**
  54. * Gets the maximum capacity of this block (if capacity is reached the block will be split into sub blocks)
  55. */
  56. public get capacity(): number {
  57. return this._capacity;
  58. }
  59. /**
  60. * Gets the minimum vector (in world space) of the block's bounding box
  61. */
  62. public get minPoint(): Vector3 {
  63. return this._minPoint;
  64. }
  65. /**
  66. * Gets the maximum vector (in world space) of the block's bounding box
  67. */
  68. public get maxPoint(): Vector3 {
  69. return this._maxPoint;
  70. }
  71. // Methods
  72. /**
  73. * Add a new element to this block
  74. * @param entry defines the element to add
  75. */
  76. public addEntry(entry: T): void {
  77. if (this.blocks) {
  78. for (var index = 0; index < this.blocks.length; index++) {
  79. var block = this.blocks[index];
  80. block.addEntry(entry);
  81. }
  82. return;
  83. }
  84. this._creationFunc(entry, this);
  85. if (this.entries.length > this.capacity && this._depth < this._maxDepth) {
  86. this.createInnerBlocks();
  87. }
  88. }
  89. /**
  90. * Add an array of elements to this block
  91. * @param entries defines the array of elements to add
  92. */
  93. public addEntries(entries: T[]): void {
  94. for (var index = 0; index < entries.length; index++) {
  95. var mesh = entries[index];
  96. this.addEntry(mesh);
  97. }
  98. }
  99. /**
  100. * Test if the current block intersects the furstum planes and if yes, then add its content to the selection array
  101. * @param frustumPlanes defines the frustum planes to test
  102. * @param selection defines the array to store current content if selection is positive
  103. * @param allowDuplicate defines if the selection array can contains duplicated entries
  104. */
  105. public select(frustumPlanes: Plane[], selection: SmartArrayNoDuplicate<T>, allowDuplicate?: boolean): void {
  106. if (BoundingBox.IsInFrustum(this._boundingVectors, frustumPlanes)) {
  107. if (this.blocks) {
  108. for (var index = 0; index < this.blocks.length; index++) {
  109. var block = this.blocks[index];
  110. block.select(frustumPlanes, selection, allowDuplicate);
  111. }
  112. return;
  113. }
  114. if (allowDuplicate) {
  115. selection.concat(this.entries);
  116. } else {
  117. selection.concatWithNoDuplicate(this.entries);
  118. }
  119. }
  120. }
  121. /**
  122. * Test if the current block intersect with the given bounding sphere and if yes, then add its content to the selection array
  123. * @param sphereCenter defines the bounding sphere center
  124. * @param sphereRadius defines the bounding sphere radius
  125. * @param selection defines the array to store current content if selection is positive
  126. * @param allowDuplicate defines if the selection array can contains duplicated entries
  127. */
  128. public intersects(sphereCenter: Vector3, sphereRadius: number, selection: SmartArrayNoDuplicate<T>, allowDuplicate?: boolean): void {
  129. if (BoundingBox.IntersectsSphere(this._minPoint, this._maxPoint, sphereCenter, sphereRadius)) {
  130. if (this.blocks) {
  131. for (var index = 0; index < this.blocks.length; index++) {
  132. var block = this.blocks[index];
  133. block.intersects(sphereCenter, sphereRadius, selection, allowDuplicate);
  134. }
  135. return;
  136. }
  137. if (allowDuplicate) {
  138. selection.concat(this.entries);
  139. } else {
  140. selection.concatWithNoDuplicate(this.entries);
  141. }
  142. }
  143. }
  144. /**
  145. * Test if the current block intersect with the given ray and if yes, then add its content to the selection array
  146. * @param ray defines the ray to test with
  147. * @param selection defines the array to store current content if selection is positive
  148. */
  149. public intersectsRay(ray: Ray, selection: SmartArrayNoDuplicate<T>): void {
  150. if (ray.intersectsBoxMinMax(this._minPoint, this._maxPoint)) {
  151. if (this.blocks) {
  152. for (var index = 0; index < this.blocks.length; index++) {
  153. var block = this.blocks[index];
  154. block.intersectsRay(ray, selection);
  155. }
  156. return;
  157. }
  158. selection.concatWithNoDuplicate(this.entries);
  159. }
  160. }
  161. /**
  162. * Subdivide the content into child blocks (this block will then be empty)
  163. */
  164. public createInnerBlocks(): void {
  165. Octree._CreateBlocks(this._minPoint, this._maxPoint, this.entries, this._capacity, this._depth, this._maxDepth, this, this._creationFunc);
  166. }
  167. }