AABox.h 8.8 KB

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