boundingBox.ts 12 KB

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