Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
AABox.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
10#include <Jolt/Math/Mat44.h>
11
13
15class [[nodiscard]] AABox
16{
17public:
19
21 AABox() : mMin(Vec3::sReplicate(FLT_MAX)), mMax(Vec3::sReplicate(-FLT_MAX)) { }
22 AABox(Vec3Arg inMin, Vec3Arg inMax) : mMin(inMin), mMax(inMax) { }
23 AABox(DVec3Arg inMin, DVec3Arg inMax) : mMin(inMin.ToVec3RoundDown()), mMax(inMax.ToVec3RoundUp()) { }
24 AABox(Vec3Arg inCenter, float inRadius) : mMin(inCenter - Vec3::sReplicate(inRadius)), mMax(inCenter + Vec3::sReplicate(inRadius)) { }
25
27 static AABox sFromTwoPoints(Vec3Arg inP1, Vec3Arg inP2) { return AABox(Vec3::sMin(inP1, inP2), Vec3::sMax(inP1, inP2)); }
28
30 static AABox sBiggest()
31 {
32 return AABox(Vec3::sReplicate(-FLT_MAX), Vec3::sReplicate(FLT_MAX));
33 }
34
36 bool operator == (const AABox &inRHS) const { return mMin == inRHS.mMin && mMax == inRHS.mMax; }
37 bool operator != (const AABox &inRHS) const { return mMin != inRHS.mMin || mMax != inRHS.mMax; }
38
40 void SetEmpty()
41 {
42 mMin = Vec3::sReplicate(FLT_MAX);
43 mMax = Vec3::sReplicate(-FLT_MAX);
44 }
45
47 bool IsValid() const
48 {
49 return mMin.GetX() <= mMax.GetX() && mMin.GetY() <= mMax.GetY() && mMin.GetZ() <= mMax.GetZ();
50 }
51
53 void Encapsulate(Vec3Arg inPos)
54 {
55 mMin = Vec3::sMin(mMin, inPos);
56 mMax = Vec3::sMax(mMax, inPos);
57 }
58
60 void Encapsulate(const AABox &inRHS)
61 {
62 mMin = Vec3::sMin(mMin, inRHS.mMin);
63 mMax = Vec3::sMax(mMax, inRHS.mMax);
64 }
65
67 void Encapsulate(const Triangle &inRHS)
68 {
69 Vec3 v = Vec3::sLoadFloat3Unsafe(inRHS.mV[0]);
70 Encapsulate(v);
71 v = Vec3::sLoadFloat3Unsafe(inRHS.mV[1]);
72 Encapsulate(v);
73 v = Vec3::sLoadFloat3Unsafe(inRHS.mV[2]);
74 Encapsulate(v);
75 }
76
78 void Encapsulate(const VertexList &inVertices, const IndexedTriangle &inTriangle)
79 {
80 for (uint32 idx : inTriangle.mIdx)
81 Encapsulate(Vec3(inVertices[idx]));
82 }
83
85 AABox Intersect(const AABox &inOther) const
86 {
87 return AABox(Vec3::sMax(mMin, inOther.mMin), Vec3::sMin(mMax, inOther.mMax));
88 }
89
91 void EnsureMinimalEdgeLength(float inMinEdgeLength)
92 {
93 Vec3 min_length = Vec3::sReplicate(inMinEdgeLength);
94 mMax = Vec3::sSelect(mMax, mMin + min_length, Vec3::sLess(mMax - mMin, min_length));
95 }
96
98 void ExpandBy(Vec3Arg inVector)
99 {
100 mMin -= inVector;
101 mMax += inVector;
102 }
103
106 {
107 return 0.5f * (mMin + mMax);
108 }
109
112 {
113 return 0.5f * (mMax - mMin);
114 }
115
117 Vec3 GetSize() const
118 {
119 return mMax - mMin;
120 }
121
123 float GetSurfaceArea() const
124 {
125 Vec3 extent = mMax - mMin;
126 return 2.0f * (extent.GetX() * extent.GetY() + extent.GetX() * extent.GetZ() + extent.GetY() * extent.GetZ());
127 }
128
130 float GetVolume() const
131 {
132 Vec3 extent = mMax - mMin;
133 return extent.GetX() * extent.GetY() * extent.GetZ();
134 }
135
137 bool Contains(const AABox &inOther) const
138 {
139 return UVec4::sAnd(Vec3::sLessOrEqual(mMin, inOther.mMin), Vec3::sGreaterOrEqual(mMax, inOther.mMax)).TestAllXYZTrue();
140 }
141
143 bool Contains(Vec3Arg inOther) const
144 {
145 return UVec4::sAnd(Vec3::sLessOrEqual(mMin, inOther), Vec3::sGreaterOrEqual(mMax, inOther)).TestAllXYZTrue();
146 }
147
149 bool Contains(DVec3Arg inOther) const
150 {
151 return Contains(Vec3(inOther));
152 }
153
155 bool Overlaps(const AABox &inOther) const
156 {
157 return !UVec4::sOr(Vec3::sGreater(mMin, inOther.mMax), Vec3::sLess(mMax, inOther.mMin)).TestAnyXYZTrue();
158 }
159
161 bool Overlaps(const Plane &inPlane) const
162 {
163 Vec3 normal = inPlane.GetNormal();
164 float dist_normal = inPlane.SignedDistance(GetSupport(normal));
165 float dist_min_normal = inPlane.SignedDistance(GetSupport(-normal));
166 return dist_normal * dist_min_normal <= 0.0f; // If both support points are on the same side of the plane we don't overlap
167 }
168
170 void Translate(Vec3Arg inTranslation)
171 {
172 mMin += inTranslation;
173 mMax += inTranslation;
174 }
175
177 void Translate(DVec3Arg inTranslation)
178 {
179 mMin = (DVec3(mMin) + inTranslation).ToVec3RoundDown();
180 mMax = (DVec3(mMax) + inTranslation).ToVec3RoundUp();
181 }
182
184 AABox Transformed(Mat44Arg inMatrix) const
185 {
186 // Start with the translation of the matrix
187 Vec3 new_min, new_max;
188 new_min = new_max = inMatrix.GetTranslation();
189
190 // Now find the extreme points by considering the product of the min and max with each column of inMatrix
191 for (int c = 0; c < 3; ++c)
192 {
193 Vec3 col = inMatrix.GetColumn3(c);
194
195 Vec3 a = col * mMin[c];
196 Vec3 b = col * mMax[c];
197
198 new_min += Vec3::sMin(a, b);
199 new_max += Vec3::sMax(a, b);
200 }
201
202 // Return the new bounding box
203 return AABox(new_min, new_max);
204 }
205
208 {
209 AABox transformed = Transformed(inMatrix.GetRotation());
210 transformed.Translate(inMatrix.GetTranslation());
211 return transformed;
212 }
213
215 AABox Scaled(Vec3Arg inScale) const
216 {
217 return AABox::sFromTwoPoints(mMin * inScale, mMax * inScale);
218 }
219
221 Vec3 GetSupport(Vec3Arg inDirection) const
222 {
223 return Vec3::sSelect(mMax, mMin, Vec3::sLess(inDirection, Vec3::sZero()));
224 }
225
227 template <class VERTEX_ARRAY>
228 void GetSupportingFace(Vec3Arg inDirection, VERTEX_ARRAY &outVertices) const
229 {
230 outVertices.resize(4);
231
232 int axis = inDirection.Abs().GetHighestComponentIndex();
233 if (inDirection[axis] < 0.0f)
234 {
235 switch (axis)
236 {
237 case 0:
238 outVertices[0] = Vec3(mMax.GetX(), mMin.GetY(), mMin.GetZ());
239 outVertices[1] = Vec3(mMax.GetX(), mMax.GetY(), mMin.GetZ());
240 outVertices[2] = Vec3(mMax.GetX(), mMax.GetY(), mMax.GetZ());
241 outVertices[3] = Vec3(mMax.GetX(), mMin.GetY(), mMax.GetZ());
242 break;
243
244 case 1:
245 outVertices[0] = Vec3(mMin.GetX(), mMax.GetY(), mMin.GetZ());
246 outVertices[1] = Vec3(mMin.GetX(), mMax.GetY(), mMax.GetZ());
247 outVertices[2] = Vec3(mMax.GetX(), mMax.GetY(), mMax.GetZ());
248 outVertices[3] = Vec3(mMax.GetX(), mMax.GetY(), mMin.GetZ());
249 break;
250
251 case 2:
252 outVertices[0] = Vec3(mMin.GetX(), mMin.GetY(), mMax.GetZ());
253 outVertices[1] = Vec3(mMax.GetX(), mMin.GetY(), mMax.GetZ());
254 outVertices[2] = Vec3(mMax.GetX(), mMax.GetY(), mMax.GetZ());
255 outVertices[3] = Vec3(mMin.GetX(), mMax.GetY(), mMax.GetZ());
256 break;
257 }
258 }
259 else
260 {
261 switch (axis)
262 {
263 case 0:
264 outVertices[0] = Vec3(mMin.GetX(), mMin.GetY(), mMin.GetZ());
265 outVertices[1] = Vec3(mMin.GetX(), mMin.GetY(), mMax.GetZ());
266 outVertices[2] = Vec3(mMin.GetX(), mMax.GetY(), mMax.GetZ());
267 outVertices[3] = Vec3(mMin.GetX(), mMax.GetY(), mMin.GetZ());
268 break;
269
270 case 1:
271 outVertices[0] = Vec3(mMin.GetX(), mMin.GetY(), mMin.GetZ());
272 outVertices[1] = Vec3(mMax.GetX(), mMin.GetY(), mMin.GetZ());
273 outVertices[2] = Vec3(mMax.GetX(), mMin.GetY(), mMax.GetZ());
274 outVertices[3] = Vec3(mMin.GetX(), mMin.GetY(), mMax.GetZ());
275 break;
276
277 case 2:
278 outVertices[0] = Vec3(mMin.GetX(), mMin.GetY(), mMin.GetZ());
279 outVertices[1] = Vec3(mMin.GetX(), mMax.GetY(), mMin.GetZ());
280 outVertices[2] = Vec3(mMax.GetX(), mMax.GetY(), mMin.GetZ());
281 outVertices[3] = Vec3(mMax.GetX(), mMin.GetY(), mMin.GetZ());
282 break;
283 }
284 }
285 }
286
289 {
290 return Vec3::sMin(Vec3::sMax(inPoint, mMin), mMax);
291 }
292
294 inline float GetSqDistanceTo(Vec3Arg inPoint) const
295 {
296 return (GetClosestPoint(inPoint) - inPoint).LengthSq();
297 }
298
302};
303
#define JPH_NAMESPACE_END
Definition Core.h:377
std::uint32_t uint32
Definition Core.h:451
#define JPH_NAMESPACE_BEGIN
Definition Core.h:371
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:31
Axis aligned box.
Definition AABox.h:16
Vec3 GetExtent() const
Get extent of bounding box (half of the size)
Definition AABox.h:111
JPH_OVERRIDE_NEW_DELETE AABox()
Constructor.
Definition AABox.h:21
static AABox sBiggest()
Get bounding box of size 2 * FLT_MAX.
Definition AABox.h:30
void Translate(DVec3Arg inTranslation)
Translate bounding box.
Definition AABox.h:177
AABox Scaled(Vec3Arg inScale) const
Scale this bounding box, can handle non-uniform and negative scaling.
Definition AABox.h:215
bool Contains(Vec3Arg inOther) const
Check if this box contains a point.
Definition AABox.h:143
Vec3 GetSize() const
Get size of bounding box.
Definition AABox.h:117
void EnsureMinimalEdgeLength(float inMinEdgeLength)
Make sure that each edge of the bounding box has a minimal length.
Definition AABox.h:91
Vec3 mMin
Bounding box min and max.
Definition AABox.h:300
void SetEmpty()
Reset the bounding box to an empty bounding box.
Definition AABox.h:40
AABox Intersect(const AABox &inOther) const
Intersect this bounding box with inOther, returns the intersection.
Definition AABox.h:85
void ExpandBy(Vec3Arg inVector)
Widen the box on both sides by inVector.
Definition AABox.h:98
static AABox sFromTwoPoints(Vec3Arg inP1, Vec3Arg inP2)
Create box from 2 points.
Definition AABox.h:27
void Encapsulate(const AABox &inRHS)
Encapsulate bounding box in bounding box.
Definition AABox.h:60
AABox(Vec3Arg inMin, Vec3Arg inMax)
Definition AABox.h:22
void Encapsulate(const VertexList &inVertices, const IndexedTriangle &inTriangle)
Encapsulate triangle in bounding box.
Definition AABox.h:78
AABox Transformed(DMat44Arg inMatrix) const
Transform bounding box.
Definition AABox.h:207
void GetSupportingFace(Vec3Arg inDirection, VERTEX_ARRAY &outVertices) const
Get the vertices of the face that faces inDirection the most.
Definition AABox.h:228
float GetSqDistanceTo(Vec3Arg inPoint) const
Get the squared distance between inPoint and this box (will be 0 if in Point is inside the box)
Definition AABox.h:294
AABox(Vec3Arg inCenter, float inRadius)
Definition AABox.h:24
float GetVolume() const
Get volume of bounding box.
Definition AABox.h:130
Vec3 GetClosestPoint(Vec3Arg inPoint) const
Get the closest point on or in this box to inPoint.
Definition AABox.h:288
Vec3 GetSupport(Vec3Arg inDirection) const
Calculate the support vector for this convex shape.
Definition AABox.h:221
AABox Transformed(Mat44Arg inMatrix) const
Transform bounding box.
Definition AABox.h:184
bool Overlaps(const Plane &inPlane) const
Check if this box overlaps with a plane.
Definition AABox.h:161
bool Overlaps(const AABox &inOther) const
Check if this box overlaps with another box.
Definition AABox.h:155
AABox(DVec3Arg inMin, DVec3Arg inMax)
Definition AABox.h:23
float GetSurfaceArea() const
Get surface area of bounding box.
Definition AABox.h:123
void Translate(Vec3Arg inTranslation)
Translate bounding box.
Definition AABox.h:170
bool IsValid() const
Check if the bounding box is valid (max >= min)
Definition AABox.h:47
void Encapsulate(Vec3Arg inPos)
Encapsulate point in bounding box.
Definition AABox.h:53
Vec3 GetCenter() const
Get center of bounding box.
Definition AABox.h:105
bool Contains(const AABox &inOther) const
Check if this box contains another box.
Definition AABox.h:137
bool Contains(DVec3Arg inOther) const
Check if this box contains a point.
Definition AABox.h:149
void Encapsulate(const Triangle &inRHS)
Encapsulate triangle in bounding box.
Definition AABox.h:67
Vec3 mMax
Definition AABox.h:301
Holds a 4x4 matrix of floats with the last column consisting of doubles.
Definition DMat44.h:13
JPH_INLINE DVec3 GetTranslation() const
Definition DMat44.h:111
JPH_INLINE Mat44 GetRotation() const
Get rotation part only (note: retains the first 3 values from the bottom row)
Definition DMat44.h:128
Definition DVec3.h:14
Triangle with 32-bit indices and material index.
Definition IndexedTriangle.h:73
uint32 mIdx[3]
Definition IndexedTriangle.h:68
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition Mat44.h:13
JPH_INLINE Vec3 GetColumn3(uint inCol) const
Definition Mat44.h:158
JPH_INLINE Vec3 GetTranslation() const
Definition Mat44.h:152
An infinite plane described by the formula X . Normal + Constant = 0.
Definition Plane.h:11
Vec3 GetNormal() const
Definition Plane.h:30
float SignedDistance(Vec3Arg inPoint) const
Distance point to plane.
Definition Plane.h:46
A simple triangle and its material.
Definition Triangle.h:11
Float3 mV[3]
Vertices.
Definition Triangle.h:28
JPH_INLINE bool TestAnyXYZTrue() const
Test if any of X, Y or Z components are true (true is when highest bit of component is set)
Definition UVec4.inl:395
static JPH_INLINE UVec4 sAnd(UVec4Arg inV1, UVec4Arg inV2)
Logical and (component wise)
Definition UVec4.inl:194
static JPH_INLINE UVec4 sOr(UVec4Arg inV1, UVec4Arg inV2)
Logical or (component wise)
Definition UVec4.inl:166
JPH_INLINE bool TestAllXYZTrue() const
Test if X, Y and Z components are true (true is when highest bit of component is set)
Definition UVec4.inl:405
Definition Vec3.h:17
static JPH_INLINE Vec3 sMax(Vec3Arg inV1, Vec3Arg inV2)
Return the maximum of each of the components.
Definition Vec3.inl:159
static JPH_INLINE Vec3 sMin(Vec3Arg inV1, Vec3Arg inV2)
Return the minimum value of each of the components.
Definition Vec3.inl:146
JPH_INLINE float GetX() const
Get individual components.
Definition Vec3.h:124
static JPH_INLINE UVec4 sGreaterOrEqual(Vec3Arg inV1, Vec3Arg inV2)
Greater than or equal (component wise)
Definition Vec3.inl:237
static JPH_INLINE UVec4 sLessOrEqual(Vec3Arg inV1, Vec3Arg inV2)
Less than or equal (component wise)
Definition Vec3.inl:207
JPH_INLINE Vec3 Abs() const
Return the absolute value of each of the components.
Definition Vec3.inl:572
static JPH_INLINE UVec4 sGreater(Vec3Arg inV1, Vec3Arg inV2)
Greater than (component wise)
Definition Vec3.inl:222
static JPH_INLINE Vec3 sSelect(Vec3Arg inV1, Vec3Arg inV2, UVec4Arg inControl)
Component wise select, returns inV1 when highest bit of inControl = 0 and inV2 when highest bit of in...
Definition Vec3.inl:269
JPH_INLINE float GetY() const
Definition Vec3.h:125
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition Vec3.inl:107
static JPH_INLINE UVec4 sLess(Vec3Arg inV1, Vec3Arg inV2)
Less than (component wise)
Definition Vec3.inl:192
static JPH_INLINE Vec3 sReplicate(float inV)
Replicate inV across all components.
Definition Vec3.inl:118
static JPH_INLINE Vec3 sLoadFloat3Unsafe(const Float3 &inV)
Load 3 floats from memory (reads 32 bits extra which it doesn't use)
Definition Vec3.inl:134
JPH_INLINE float GetZ() const
Definition Vec3.h:126
JPH_INLINE int GetHighestComponentIndex() const
Get index of component with highest value.
Definition Vec3.inl:567