boundingSphere.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { DeepImmutable } from "../types";
  2. import { ArrayTools } from "../Misc/arrayTools";
  3. import { Matrix, Vector3, Plane } from "../Maths/math";
  4. /**
  5. * Class used to store bounding sphere information
  6. */
  7. export class BoundingSphere {
  8. /**
  9. * Gets the center of the bounding sphere in local space
  10. */
  11. public readonly center = Vector3.Zero();
  12. /**
  13. * Radius of the bounding sphere in local space
  14. */
  15. public radius: number;
  16. /**
  17. * Gets the center of the bounding sphere in world space
  18. */
  19. public readonly centerWorld = Vector3.Zero();
  20. /**
  21. * Radius of the bounding sphere in world space
  22. */
  23. public radiusWorld: number;
  24. /**
  25. * Gets the minimum vector in local space
  26. */
  27. public readonly minimum = Vector3.Zero();
  28. /**
  29. * Gets the maximum vector in local space
  30. */
  31. public readonly maximum = Vector3.Zero();
  32. private _worldMatrix: DeepImmutable<Matrix>;
  33. private static readonly TmpVector3 = ArrayTools.BuildArray(3, Vector3.Zero);
  34. /**
  35. * Creates a new bounding sphere
  36. * @param min defines the minimum vector (in local space)
  37. * @param max defines the maximum vector (in local space)
  38. * @param worldMatrix defines the new world matrix
  39. */
  40. constructor(min: DeepImmutable<Vector3>, max: DeepImmutable<Vector3>, worldMatrix?: DeepImmutable<Matrix>) {
  41. this.reConstruct(min, max, worldMatrix);
  42. }
  43. /**
  44. * Recreates the entire bounding sphere from scratch as if we call the constructor in place
  45. * @param min defines the new minimum vector (in local space)
  46. * @param max defines the new maximum vector (in local space)
  47. * @param worldMatrix defines the new world matrix
  48. */
  49. public reConstruct(min: DeepImmutable<Vector3>, max: DeepImmutable<Vector3>, worldMatrix?: DeepImmutable<Matrix>) {
  50. this.minimum.copyFrom(min);
  51. this.maximum.copyFrom(max);
  52. var distance = Vector3.Distance(min, max);
  53. max.addToRef(min, this.center).scaleInPlace(0.5);
  54. this.radius = distance * 0.5;
  55. this._update(worldMatrix || Matrix.IdentityReadOnly);
  56. }
  57. /**
  58. * Scale the current bounding sphere by applying a scale factor
  59. * @param factor defines the scale factor to apply
  60. * @returns the current bounding box
  61. */
  62. public scale(factor: number): BoundingSphere {
  63. const newRadius = this.radius * factor;
  64. const tmpVectors = BoundingSphere.TmpVector3;
  65. const tempRadiusVector = tmpVectors[0].setAll(newRadius);
  66. const min = this.center.subtractToRef(tempRadiusVector, tmpVectors[1]);
  67. const max = this.center.addToRef(tempRadiusVector, tmpVectors[2]);
  68. this.reConstruct(min, max, this._worldMatrix);
  69. return this;
  70. }
  71. /**
  72. * Gets the world matrix of the bounding box
  73. * @returns a matrix
  74. */
  75. public getWorldMatrix(): DeepImmutable<Matrix> {
  76. return this._worldMatrix;
  77. }
  78. // Methods
  79. /** @hidden */
  80. public _update(worldMatrix: DeepImmutable<Matrix>): void {
  81. if (!worldMatrix.isIdentity()) {
  82. Vector3.TransformCoordinatesToRef(this.center, worldMatrix, this.centerWorld);
  83. const tempVector = BoundingSphere.TmpVector3[0];
  84. Vector3.TransformNormalFromFloatsToRef(1.0, 1.0, 1.0, worldMatrix, tempVector);
  85. this.radiusWorld = Math.max(Math.abs(tempVector.x), Math.abs(tempVector.y), Math.abs(tempVector.z)) * this.radius;
  86. }
  87. else {
  88. this.centerWorld.copyFrom(this.center);
  89. this.radiusWorld = this.radius;
  90. }
  91. }
  92. /**
  93. * Tests if the bounding sphere is intersecting the frustum planes
  94. * @param frustumPlanes defines the frustum planes to test
  95. * @returns true if there is an intersection
  96. */
  97. public isInFrustum(frustumPlanes: Array<DeepImmutable<Plane>>): boolean {
  98. let center = this.centerWorld;
  99. let radius = this.radiusWorld;
  100. for (let i = 0; i < 6; i++) {
  101. if (frustumPlanes[i].dotCoordinate(center) <= -radius) {
  102. return false;
  103. }
  104. }
  105. return true;
  106. }
  107. /**
  108. * Tests if the bounding sphere center is in between the frustum planes.
  109. * Used for optimistic fast inclusion.
  110. * @param frustumPlanes defines the frustum planes to test
  111. * @returns true if the sphere center is in between the frustum planes
  112. */
  113. public isCenterInFrustum(frustumPlanes: Array<DeepImmutable<Plane>>): boolean {
  114. let center = this.centerWorld;
  115. for (let i = 0; i < 6; i++) {
  116. if (frustumPlanes[i].dotCoordinate(center) < 0) {
  117. return false;
  118. }
  119. }
  120. return true;
  121. }
  122. /**
  123. * Tests if a point is inside the bounding sphere
  124. * @param point defines the point to test
  125. * @returns true if the point is inside the bounding sphere
  126. */
  127. public intersectsPoint(point: DeepImmutable<Vector3>): boolean {
  128. const squareDistance = Vector3.DistanceSquared(this.centerWorld, point);
  129. if (this.radiusWorld * this.radiusWorld < squareDistance) {
  130. return false;
  131. }
  132. return true;
  133. }
  134. // Statics
  135. /**
  136. * Checks if two sphere intersct
  137. * @param sphere0 sphere 0
  138. * @param sphere1 sphere 1
  139. * @returns true if the speres intersect
  140. */
  141. public static Intersects(sphere0: DeepImmutable<BoundingSphere>, sphere1: DeepImmutable<BoundingSphere>): boolean {
  142. const squareDistance = Vector3.DistanceSquared(sphere0.centerWorld, sphere1.centerWorld);
  143. const radiusSum = sphere0.radiusWorld + sphere1.radiusWorld;
  144. if (radiusSum * radiusSum < squareDistance) {
  145. return false;
  146. }
  147. return true;
  148. }
  149. }