boundingBox.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import { DeepImmutable } from "../types";
  2. import { ArrayTools } from "../Misc/arrayTools";
  3. import { Matrix, Vector3 } from "../Maths/math.vector";
  4. import { BoundingSphere } from "../Culling/boundingSphere";
  5. import { ICullable } from "./boundingInfo";
  6. import { Epsilon } from '../Maths/math.constants';
  7. import { Plane } from '../Maths/math.plane';
  8. /**
  9. * Class used to store bounding box information
  10. */
  11. export class BoundingBox implements ICullable {
  12. /**
  13. * Gets the 8 vectors representing the bounding box in local space
  14. */
  15. public readonly vectors: Vector3[] = ArrayTools.BuildArray(8, Vector3.Zero);
  16. /**
  17. * Gets the center of the bounding box in local space
  18. */
  19. public readonly center: Vector3 = Vector3.Zero();
  20. /**
  21. * Gets the center of the bounding box in world space
  22. */
  23. public readonly centerWorld: Vector3 = Vector3.Zero();
  24. /**
  25. * Gets the extend size in local space
  26. */
  27. public readonly extendSize: Vector3 = Vector3.Zero();
  28. /**
  29. * Gets the extend size in world space
  30. */
  31. public readonly extendSizeWorld: Vector3 = Vector3.Zero();
  32. /**
  33. * Gets the OBB (object bounding box) directions
  34. */
  35. public readonly directions: Vector3[] = ArrayTools.BuildArray(3, Vector3.Zero);
  36. /**
  37. * Gets the 8 vectors representing the bounding box in world space
  38. */
  39. public readonly vectorsWorld: Vector3[] = ArrayTools.BuildArray(8, Vector3.Zero);
  40. /**
  41. * Gets the minimum vector in world space
  42. */
  43. public readonly minimumWorld: Vector3 = Vector3.Zero();
  44. /**
  45. * Gets the maximum vector in world space
  46. */
  47. public readonly maximumWorld: Vector3 = Vector3.Zero();
  48. /**
  49. * Gets the minimum vector in local space
  50. */
  51. public readonly minimum: Vector3 = Vector3.Zero();
  52. /**
  53. * Gets the maximum vector in local space
  54. */
  55. public readonly maximum: Vector3 = Vector3.Zero();
  56. private _worldMatrix: DeepImmutable<Matrix>;
  57. private static readonly TmpVector3 = ArrayTools.BuildArray(3, Vector3.Zero);
  58. /**
  59. * @hidden
  60. */
  61. public _tag: number;
  62. /**
  63. * Creates a new bounding box
  64. * @param min defines the minimum vector (in local space)
  65. * @param max defines the maximum vector (in local space)
  66. * @param worldMatrix defines the new world matrix
  67. */
  68. constructor(min: DeepImmutable<Vector3>, max: DeepImmutable<Vector3>, worldMatrix?: DeepImmutable<Matrix>) {
  69. this.reConstruct(min, max, worldMatrix);
  70. }
  71. // Methods
  72. /**
  73. * Recreates the entire bounding box from scratch as if we call the constructor in place
  74. * @param min defines the new minimum vector (in local space)
  75. * @param max defines the new maximum vector (in local space)
  76. * @param worldMatrix defines the new world matrix
  77. */
  78. public reConstruct(min: DeepImmutable<Vector3>, max: DeepImmutable<Vector3>, worldMatrix?: DeepImmutable<Matrix>) {
  79. const minX = min.x, minY = min.y, minZ = min.z, maxX = max.x, maxY = max.y, maxZ = max.z;
  80. const vectors = this.vectors;
  81. this.minimum.copyFromFloats(minX, minY, minZ);
  82. this.maximum.copyFromFloats(maxX, maxY, maxZ);
  83. vectors[0].copyFromFloats(minX, minY, minZ);
  84. vectors[1].copyFromFloats(maxX, maxY, maxZ);
  85. vectors[2].copyFromFloats(maxX, minY, minZ);
  86. vectors[3].copyFromFloats(minX, maxY, minZ);
  87. vectors[4].copyFromFloats(minX, minY, maxZ);
  88. vectors[5].copyFromFloats(maxX, maxY, minZ);
  89. vectors[6].copyFromFloats(minX, maxY, maxZ);
  90. vectors[7].copyFromFloats(maxX, minY, maxZ);
  91. // OBB
  92. max.addToRef(min, this.center).scaleInPlace(0.5);
  93. max.subtractToRef(min, this.extendSize).scaleInPlace(0.5);
  94. this._worldMatrix = worldMatrix || Matrix.IdentityReadOnly;
  95. this._update(this._worldMatrix);
  96. }
  97. /**
  98. * Scale the current bounding box by applying a scale factor
  99. * @param factor defines the scale factor to apply
  100. * @returns the current bounding box
  101. */
  102. public scale(factor: number): BoundingBox {
  103. const tmpVectors = BoundingBox.TmpVector3;
  104. const diff = this.maximum.subtractToRef(this.minimum, tmpVectors[0]);
  105. const len = diff.length();
  106. diff.normalizeFromLength(len);
  107. const distance = len * factor;
  108. const newRadius = diff.scaleInPlace(distance * 0.5);
  109. const min = this.center.subtractToRef(newRadius, tmpVectors[1]);
  110. const max = this.center.addToRef(newRadius, tmpVectors[2]);
  111. this.reConstruct(min, max, this._worldMatrix);
  112. return this;
  113. }
  114. /**
  115. * Gets the world matrix of the bounding box
  116. * @returns a matrix
  117. */
  118. public getWorldMatrix(): DeepImmutable<Matrix> {
  119. return this._worldMatrix;
  120. }
  121. /** @hidden */
  122. public _update(world: DeepImmutable<Matrix>): void {
  123. const minWorld = this.minimumWorld;
  124. const maxWorld = this.maximumWorld;
  125. const directions = this.directions;
  126. const vectorsWorld = this.vectorsWorld;
  127. const vectors = this.vectors;
  128. if (!world.isIdentity()) {
  129. minWorld.setAll(Number.MAX_VALUE);
  130. maxWorld.setAll(-Number.MAX_VALUE);
  131. for (let index = 0; index < 8; ++index) {
  132. const v = vectorsWorld[index];
  133. Vector3.TransformCoordinatesToRef(vectors[index], world, v);
  134. minWorld.minimizeInPlace(v);
  135. maxWorld.maximizeInPlace(v);
  136. }
  137. // Extend
  138. maxWorld.subtractToRef(minWorld, this.extendSizeWorld).scaleInPlace(0.5);
  139. maxWorld.addToRef(minWorld, this.centerWorld).scaleInPlace(0.5);
  140. }
  141. else {
  142. minWorld.copyFrom(this.minimum);
  143. maxWorld.copyFrom(this.maximum);
  144. for (let index = 0; index < 8; ++index) {
  145. vectorsWorld[index].copyFrom(vectors[index]);
  146. }
  147. // Extend
  148. this.extendSizeWorld.copyFrom(this.extendSize);
  149. this.centerWorld.copyFrom(this.center);
  150. }
  151. Vector3.FromArrayToRef(world.m, 0, directions[0]);
  152. Vector3.FromArrayToRef(world.m, 4, directions[1]);
  153. Vector3.FromArrayToRef(world.m, 8, directions[2]);
  154. this._worldMatrix = world;
  155. }
  156. /**
  157. * Tests if the bounding box is intersecting the frustum planes
  158. * @param frustumPlanes defines the frustum planes to test
  159. * @returns true if there is an intersection
  160. */
  161. public isInFrustum(frustumPlanes: Array<DeepImmutable<Plane>>): boolean {
  162. return BoundingBox.IsInFrustum(this.vectorsWorld, frustumPlanes);
  163. }
  164. /**
  165. * Tests if the bounding box is entirely inside the frustum planes
  166. * @param frustumPlanes defines the frustum planes to test
  167. * @returns true if there is an inclusion
  168. */
  169. public isCompletelyInFrustum(frustumPlanes: Array<DeepImmutable<Plane>>): boolean {
  170. return BoundingBox.IsCompletelyInFrustum(this.vectorsWorld, frustumPlanes);
  171. }
  172. /**
  173. * Tests if a point is inside the bounding box
  174. * @param point defines the point to test
  175. * @returns true if the point is inside the bounding box
  176. */
  177. public intersectsPoint(point: DeepImmutable<Vector3>): boolean {
  178. const min = this.minimumWorld;
  179. const max = this.maximumWorld;
  180. const minX = min.x, minY = min.y, minZ = min.z, maxX = max.x, maxY = max.y, maxZ = max.z;
  181. const pointX = point.x, pointY = point.y, pointZ = point.z;
  182. var delta = -Epsilon;
  183. if (maxX - pointX < delta || delta > pointX - minX) {
  184. return false;
  185. }
  186. if (maxY - pointY < delta || delta > pointY - minY) {
  187. return false;
  188. }
  189. if (maxZ - pointZ < delta || delta > pointZ - minZ) {
  190. return false;
  191. }
  192. return true;
  193. }
  194. /**
  195. * Tests if the bounding box intersects with a bounding sphere
  196. * @param sphere defines the sphere to test
  197. * @returns true if there is an intersection
  198. */
  199. public intersectsSphere(sphere: DeepImmutable<BoundingSphere>): boolean {
  200. return BoundingBox.IntersectsSphere(this.minimumWorld, this.maximumWorld, sphere.centerWorld, sphere.radiusWorld);
  201. }
  202. /**
  203. * Tests if the bounding box intersects with a box defined by a min and max vectors
  204. * @param min defines the min vector to use
  205. * @param max defines the max vector to use
  206. * @returns true if there is an intersection
  207. */
  208. public intersectsMinMax(min: DeepImmutable<Vector3>, max: DeepImmutable<Vector3>): boolean {
  209. const myMin = this.minimumWorld;
  210. const myMax = this.maximumWorld;
  211. const myMinX = myMin.x, myMinY = myMin.y, myMinZ = myMin.z, myMaxX = myMax.x, myMaxY = myMax.y, myMaxZ = myMax.z;
  212. const minX = min.x, minY = min.y, minZ = min.z, maxX = max.x, maxY = max.y, maxZ = max.z;
  213. if (myMaxX < minX || myMinX > maxX) {
  214. return false;
  215. }
  216. if (myMaxY < minY || myMinY > maxY) {
  217. return false;
  218. }
  219. if (myMaxZ < minZ || myMinZ > maxZ) {
  220. return false;
  221. }
  222. return true;
  223. }
  224. // Statics
  225. /**
  226. * Tests if two bounding boxes are intersections
  227. * @param box0 defines the first box to test
  228. * @param box1 defines the second box to test
  229. * @returns true if there is an intersection
  230. */
  231. public static Intersects(box0: DeepImmutable<BoundingBox>, box1: DeepImmutable<BoundingBox>): boolean {
  232. return box0.intersectsMinMax(box1.minimumWorld, box1.maximumWorld);
  233. }
  234. /**
  235. * Tests if a bounding box defines by a min/max vectors intersects a sphere
  236. * @param minPoint defines the minimum vector of the bounding box
  237. * @param maxPoint defines the maximum vector of the bounding box
  238. * @param sphereCenter defines the sphere center
  239. * @param sphereRadius defines the sphere radius
  240. * @returns true if there is an intersection
  241. */
  242. public static IntersectsSphere(minPoint: DeepImmutable<Vector3>, maxPoint: DeepImmutable<Vector3>, sphereCenter: DeepImmutable<Vector3>, sphereRadius: number): boolean {
  243. const vector = BoundingBox.TmpVector3[0];
  244. Vector3.ClampToRef(sphereCenter, minPoint, maxPoint, vector);
  245. var num = Vector3.DistanceSquared(sphereCenter, vector);
  246. return (num <= (sphereRadius * sphereRadius));
  247. }
  248. /**
  249. * Tests if a bounding box defined with 8 vectors is entirely inside frustum planes
  250. * @param boundingVectors defines an array of 8 vectors representing a bounding box
  251. * @param frustumPlanes defines the frustum planes to test
  252. * @return true if there is an inclusion
  253. */
  254. public static IsCompletelyInFrustum(boundingVectors: Array<DeepImmutable<Vector3>>, frustumPlanes: Array<DeepImmutable<Plane>>): boolean {
  255. for (var p = 0; p < 6; ++p) {
  256. const frustumPlane = frustumPlanes[p];
  257. for (var i = 0; i < 8; ++i) {
  258. if (frustumPlane.dotCoordinate(boundingVectors[i]) < 0) {
  259. return false;
  260. }
  261. }
  262. }
  263. return true;
  264. }
  265. /**
  266. * Tests if a bounding box defined with 8 vectors intersects frustum planes
  267. * @param boundingVectors defines an array of 8 vectors representing a bounding box
  268. * @param frustumPlanes defines the frustum planes to test
  269. * @return true if there is an intersection
  270. */
  271. public static IsInFrustum(boundingVectors: Array<DeepImmutable<Vector3>>, frustumPlanes: Array<DeepImmutable<Plane>>): boolean {
  272. for (var p = 0; p < 6; ++p) {
  273. let canReturnFalse = true;
  274. const frustumPlane = frustumPlanes[p];
  275. for (var i = 0; i < 8; ++i) {
  276. if (frustumPlane.dotCoordinate(boundingVectors[i]) >= 0) {
  277. canReturnFalse = false;
  278. break;
  279. }
  280. }
  281. if (canReturnFalse) {
  282. return false;
  283. }
  284. }
  285. return true;
  286. }
  287. }