AABox.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Geometry/Triangle.h>
  5. #include <Jolt/Geometry/IndexedTriangle.h>
  6. #include <Jolt/Geometry/Plane.h>
  7. #include <Jolt/Math/Mat44.h>
  8. JPH_NAMESPACE_BEGIN
  9. /// Axis aligned box
  10. class [[nodiscard]] AABox
  11. {
  12. public:
  13. JPH_OVERRIDE_NEW_DELETE
  14. /// Constructor
  15. AABox() : mMin(Vec3::sReplicate(FLT_MAX)), mMax(Vec3::sReplicate(-FLT_MAX)) { }
  16. AABox(Vec3Arg inMin, Vec3Arg inMax) : mMin(inMin), mMax(inMax) { }
  17. AABox(Vec3Arg inCenter, float inRadius) : mMin(inCenter - Vec3::sReplicate(inRadius)), mMax(inCenter + Vec3::sReplicate(inRadius)) { }
  18. /// Create box from 2 points
  19. static AABox sFromTwoPoints(Vec3Arg inP1, Vec3Arg inP2) { return AABox(Vec3::sMin(inP1, inP2), Vec3::sMax(inP1, inP2)); }
  20. /// Get bounding box of size 2 * FLT_MAX
  21. static AABox sBiggest()
  22. {
  23. return AABox(Vec3::sReplicate(-FLT_MAX), Vec3::sReplicate(FLT_MAX));
  24. }
  25. /// Comparison operators
  26. bool operator == (const AABox &inRHS) const { return mMin == inRHS.mMin && mMax == inRHS.mMax; }
  27. bool operator != (const AABox &inRHS) const { return mMin != inRHS.mMin || mMax != inRHS.mMax; }
  28. /// Reset the bounding box to an empty bounding box
  29. void SetEmpty()
  30. {
  31. mMin = Vec3::sReplicate(FLT_MAX);
  32. mMax = Vec3::sReplicate(-FLT_MAX);
  33. }
  34. /// Check if the bounding box is valid (max >= min)
  35. bool IsValid() const
  36. {
  37. return mMin.GetX() <= mMax.GetX() && mMin.GetY() <= mMax.GetY() && mMin.GetZ() <= mMax.GetZ();
  38. }
  39. /// Encapsulate point in bounding box
  40. void Encapsulate(Vec3Arg inPos)
  41. {
  42. mMin = Vec3::sMin(mMin, inPos);
  43. mMax = Vec3::sMax(mMax, inPos);
  44. }
  45. /// Encapsulate bounding box in bounding box
  46. void Encapsulate(const AABox &inRHS)
  47. {
  48. mMin = Vec3::sMin(mMin, inRHS.mMin);
  49. mMax = Vec3::sMax(mMax, inRHS.mMax);
  50. }
  51. /// Encapsulate triangle in bounding box
  52. void Encapsulate(const Triangle &inRHS)
  53. {
  54. Vec3 v = Vec3::sLoadFloat3Unsafe(inRHS.mV[0]);
  55. Encapsulate(v);
  56. v = Vec3::sLoadFloat3Unsafe(inRHS.mV[1]);
  57. Encapsulate(v);
  58. v = Vec3::sLoadFloat3Unsafe(inRHS.mV[2]);
  59. Encapsulate(v);
  60. }
  61. /// Encapsulate triangle in bounding box
  62. void Encapsulate(const VertexList &inVertices, const IndexedTriangle &inTriangle)
  63. {
  64. for (uint32 idx : inTriangle.mIdx)
  65. Encapsulate(Vec3(inVertices[idx]));
  66. }
  67. /// Intersect this bounding box with inOther, returns the intersection
  68. AABox Intersect(const AABox &inOther) const
  69. {
  70. return AABox(Vec3::sMax(mMin, inOther.mMin), Vec3::sMin(mMax, inOther.mMax));
  71. }
  72. /// Make sure that each edge of the bounding box has a minimal length
  73. void EnsureMinimalEdgeLength(float inMinEdgeLength)
  74. {
  75. Vec3 min_length = Vec3::sReplicate(inMinEdgeLength);
  76. mMax = Vec3::sSelect(mMax, mMin + min_length, Vec3::sLess(mMax - mMin, min_length));
  77. }
  78. /// Widen the box on both sides by inVector
  79. void ExpandBy(Vec3Arg inVector)
  80. {
  81. mMin -= inVector;
  82. mMax += inVector;
  83. }
  84. /// Get center of bounding box
  85. Vec3 GetCenter() const
  86. {
  87. return 0.5f * (mMin + mMax);
  88. }
  89. /// Get extent of bounding box (half of the size)
  90. Vec3 GetExtent() const
  91. {
  92. return 0.5f * (mMax - mMin);
  93. }
  94. /// Get size of bounding box
  95. Vec3 GetSize() const
  96. {
  97. return mMax - mMin;
  98. }
  99. /// Get surface area of bounding box
  100. float GetSurfaceArea() const
  101. {
  102. Vec3 extent = mMax - mMin;
  103. return 2.0f * (extent.GetX() * extent.GetY() + extent.GetX() * extent.GetZ() + extent.GetY() * extent.GetZ());
  104. }
  105. /// Get volume of bounding box
  106. float GetVolume() const
  107. {
  108. Vec3 extent = mMax - mMin;
  109. return extent.GetX() * extent.GetY() * extent.GetZ();
  110. }
  111. /// Check if this box contains another box
  112. bool Contains(const AABox &inOther) const
  113. {
  114. return UVec4::sAnd(Vec3::sLessOrEqual(mMin, inOther.mMin), Vec3::sGreaterOrEqual(mMax, inOther.mMax)).TestAllXYZTrue();
  115. }
  116. /// Check if this box contains a point
  117. bool Contains(Vec3Arg inOther) const
  118. {
  119. return UVec4::sAnd(Vec3::sLessOrEqual(mMin, inOther), Vec3::sGreaterOrEqual(mMax, inOther)).TestAllXYZTrue();
  120. }
  121. /// Check if this box overlaps with another box
  122. bool Overlaps(const AABox &inOther) const
  123. {
  124. return !UVec4::sOr(Vec3::sGreater(mMin, inOther.mMax), Vec3::sLess(mMax, inOther.mMin)).TestAnyXYZTrue();
  125. }
  126. /// Check if this box overlaps with a plane
  127. bool Overlaps(const Plane &inPlane) const
  128. {
  129. Vec3 normal = inPlane.GetNormal();
  130. float dist_normal = inPlane.SignedDistance(GetSupport(normal));
  131. float dist_min_normal = inPlane.SignedDistance(GetSupport(-normal));
  132. return dist_normal * dist_min_normal <= 0.0f; // If both support points are on the same side of the plane we don't overlap
  133. }
  134. /// Translate bounding box
  135. void Translate(Vec3Arg inTranslation)
  136. {
  137. mMin += inTranslation;
  138. mMax += inTranslation;
  139. }
  140. /// Transform bounding box
  141. AABox Transformed(Mat44Arg inMatrix) const
  142. {
  143. // Start with the translation of the matrix
  144. Vec3 new_min, new_max;
  145. new_min = new_max = inMatrix.GetTranslation();
  146. // Now find the extreme points by considering the product of the min and max with each column of inMatrix
  147. for (int c = 0; c < 3; ++c)
  148. {
  149. Vec3 col = inMatrix.GetColumn3(c);
  150. Vec3 a = col * mMin[c];
  151. Vec3 b = col * mMax[c];
  152. new_min += Vec3::sMin(a, b);
  153. new_max += Vec3::sMax(a, b);
  154. }
  155. // Return the new bounding box
  156. return AABox(new_min, new_max);
  157. }
  158. /// Scale this bounding box, can handle non-uniform and negative scaling
  159. AABox Scaled(Vec3Arg inScale) const
  160. {
  161. return AABox::sFromTwoPoints(mMin * inScale, mMax * inScale);
  162. }
  163. /// Calculate the support vector for this convex shape.
  164. Vec3 GetSupport(Vec3Arg inDirection) const
  165. {
  166. return Vec3::sSelect(mMax, mMin, Vec3::sLess(inDirection, Vec3::sZero()));
  167. }
  168. /// Get the vertices of the face that faces inDirection the most
  169. template <class VERTEX_ARRAY>
  170. void GetSupportingFace(Vec3Arg inDirection, VERTEX_ARRAY &outVertices) const
  171. {
  172. outVertices.resize(4);
  173. int axis = inDirection.Abs().GetHighestComponentIndex();
  174. if (inDirection[axis] < 0.0f)
  175. {
  176. switch (axis)
  177. {
  178. case 0:
  179. outVertices[0] = Vec3(mMax.GetX(), mMin.GetY(), mMin.GetZ());
  180. outVertices[1] = Vec3(mMax.GetX(), mMax.GetY(), mMin.GetZ());
  181. outVertices[2] = Vec3(mMax.GetX(), mMax.GetY(), mMax.GetZ());
  182. outVertices[3] = Vec3(mMax.GetX(), mMin.GetY(), mMax.GetZ());
  183. break;
  184. case 1:
  185. outVertices[0] = Vec3(mMin.GetX(), mMax.GetY(), mMin.GetZ());
  186. outVertices[1] = Vec3(mMin.GetX(), mMax.GetY(), mMax.GetZ());
  187. outVertices[2] = Vec3(mMax.GetX(), mMax.GetY(), mMax.GetZ());
  188. outVertices[3] = Vec3(mMax.GetX(), mMax.GetY(), mMin.GetZ());
  189. break;
  190. case 2:
  191. outVertices[0] = Vec3(mMin.GetX(), mMin.GetY(), mMax.GetZ());
  192. outVertices[1] = Vec3(mMax.GetX(), mMin.GetY(), mMax.GetZ());
  193. outVertices[2] = Vec3(mMax.GetX(), mMax.GetY(), mMax.GetZ());
  194. outVertices[3] = Vec3(mMin.GetX(), mMax.GetY(), mMax.GetZ());
  195. break;
  196. }
  197. }
  198. else
  199. {
  200. switch (axis)
  201. {
  202. case 0:
  203. outVertices[0] = Vec3(mMin.GetX(), mMin.GetY(), mMin.GetZ());
  204. outVertices[1] = Vec3(mMin.GetX(), mMin.GetY(), mMax.GetZ());
  205. outVertices[2] = Vec3(mMin.GetX(), mMax.GetY(), mMax.GetZ());
  206. outVertices[3] = Vec3(mMin.GetX(), mMax.GetY(), mMin.GetZ());
  207. break;
  208. case 1:
  209. outVertices[0] = Vec3(mMin.GetX(), mMin.GetY(), mMin.GetZ());
  210. outVertices[1] = Vec3(mMax.GetX(), mMin.GetY(), mMin.GetZ());
  211. outVertices[2] = Vec3(mMax.GetX(), mMin.GetY(), mMax.GetZ());
  212. outVertices[3] = Vec3(mMin.GetX(), mMin.GetY(), mMax.GetZ());
  213. break;
  214. case 2:
  215. outVertices[0] = Vec3(mMin.GetX(), mMin.GetY(), mMin.GetZ());
  216. outVertices[1] = Vec3(mMin.GetX(), mMax.GetY(), mMin.GetZ());
  217. outVertices[2] = Vec3(mMax.GetX(), mMax.GetY(), mMin.GetZ());
  218. outVertices[3] = Vec3(mMax.GetX(), mMin.GetY(), mMin.GetZ());
  219. break;
  220. }
  221. }
  222. }
  223. /// Get the closest point on or in this box to inPoint
  224. Vec3 GetClosestPoint(Vec3Arg inPoint) const
  225. {
  226. return Vec3::sMin(Vec3::sMax(inPoint, mMin), mMax);
  227. }
  228. /// Get the squared distance between inPoint and this box (will be 0 if in Point is inside the box)
  229. inline float GetSqDistanceTo(Vec3Arg inPoint) const
  230. {
  231. return (GetClosestPoint(inPoint) - inPoint).LengthSq();
  232. }
  233. /// Bounding box min and max
  234. Vec3 mMin;
  235. Vec3 mMax;
  236. };
  237. JPH_NAMESPACE_END