Plasma Engine  2.0
Loading...
Searching...
No Matches
BoundingBox.h
1#pragma once
2
3#include <Foundation/Math/Vec3.h>
4
9
10template <typename Type>
12{
13public:
14 // Means this object can be copied using memcpy instead of copy construction.
15 PL_DECLARE_POD_TYPE();
16
17 using ComponentType = Type;
18
19public:
22
24 plBoundingBoxTemplate(const plVec3Template<Type>& vMin, const plVec3Template<Type>& vMax); // [tested]
25
26#if PL_ENABLED(PL_MATH_CHECK_FOR_NAN)
27 void AssertNotNaN() const
28 {
29 PL_ASSERT_ALWAYS(!IsNaN(), "This object contains NaN values. This can happen when you forgot to initialize it before using it. Please check that "
30 "all code-paths properly initialize this object.");
31 }
32#endif
33
35 [[nodiscard]] static plBoundingBoxTemplate<Type> MakeZero();
36
38 [[nodiscard]] static plBoundingBoxTemplate<Type> MakeInvalid(); // [tested]
39
41 [[nodiscard]] static plBoundingBoxTemplate<Type> MakeFromCenterAndHalfExtents(const plVec3Template<Type>& vCenter, const plVec3Template<Type>& vHalfExtents); // [tested]
42
44 [[nodiscard]] static plBoundingBoxTemplate<Type> MakeFromMinMax(const plVec3Template<Type>& vMin, const plVec3Template<Type>& vMax); // [tested]
45
47 [[nodiscard]] static plBoundingBoxTemplate<Type> MakeFromPoints(const plVec3Template<Type>* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride = sizeof(plVec3Template<Type>)); // [tested]
48
50 bool IsValid() const; // [tested]
51
53 bool IsNaN() const; // [tested]
54
56 void GetCorners(plVec3Template<Type>* out_pCorners) const; // [tested]
57
59 const plVec3Template<Type> GetCenter() const; // [tested]
60
62 const plVec3Template<Type> GetExtents() const; // [tested]
63
65 const plVec3Template<Type> GetHalfExtents() const; // [tested]
66
68 void ExpandToInclude(const plVec3Template<Type>& vPoint); // [tested]
69
71 void ExpandToInclude(const plBoundingBoxTemplate& rhs); // [tested]
72
74 void ExpandToInclude(const plVec3Template<Type>* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride = sizeof(plVec3Template<Type>)); // [tested]
75
77 void ExpandToCube(); // [tested]
78
80 void Grow(const plVec3Template<Type>& vDiff); // [tested]
81
83 bool Contains(const plVec3Template<Type>& vPoint) const; // [tested]
84
86 bool Contains(const plBoundingBoxTemplate& rhs) const; // [tested]
87
89 bool Contains(const plVec3Template<Type>* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride = sizeof(plVec3Template<Type>)) const; // [tested]
90
92 bool Contains(const plBoundingSphereTemplate<Type>& sphere) const; // [tested]
93
95 bool Overlaps(const plBoundingBoxTemplate& rhs) const; // [tested]
96
98 bool Overlaps(const plVec3Template<Type>* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride = sizeof(plVec3Template<Type>)) const; // [tested]
99
101 bool Overlaps(const plBoundingSphereTemplate<Type>& sphere) const; // [tested]
102
104 bool IsIdentical(const plBoundingBoxTemplate& rhs) const; // [tested]
105
107 bool IsEqual(const plBoundingBoxTemplate& rhs, Type fEpsilon = plMath::DefaultEpsilon<Type>()) const; // [tested]
108
110 void Translate(const plVec3Template<Type>& vDiff); // [tested]
111
113 void ScaleFromCenter(const plVec3Template<Type>& vScale); // [tested]
114
116 void ScaleFromOrigin(const plVec3Template<Type>& vScale); // [tested]
117
119 void TransformFromCenter(const plMat4Template<Type>& mTransform); // [tested]
120
122 void TransformFromOrigin(const plMat4Template<Type>& mTransform); // [tested]
123
126 const plVec3Template<Type> GetClampedPoint(const plVec3Template<Type>& vPoint) const; // [tested]
127
129 Type GetDistanceSquaredTo(const plVec3Template<Type>& vPoint) const; // [tested]
130
132 Type GetDistanceSquaredTo(const plBoundingBoxTemplate& rhs) const; // [tested]
133
135 Type GetDistanceTo(const plVec3Template<Type>& vPoint) const; // [tested]
136
138 Type GetDistanceTo(const plBoundingSphereTemplate<Type>& sphere) const; // [tested]
139
141 Type GetDistanceTo(const plBoundingBoxTemplate& rhs) const; // [tested]
142
145 bool GetRayIntersection(const plVec3Template<Type>& vStartPos, const plVec3Template<Type>& vRayDir, Type* out_pIntersectionDistance = nullptr,
146 plVec3Template<Type>* out_pIntersection = nullptr) const; // [tested]
147
150 bool GetLineSegmentIntersection(const plVec3Template<Type>& vStartPos, const plVec3Template<Type>& vEndPos, Type* out_pLineFraction = nullptr,
151 plVec3Template<Type>* out_pIntersection = nullptr) const; // [tested]
152
154 const plBoundingSphereTemplate<Type> GetBoundingSphere() const; // [tested]
155
156
157public:
160};
161
163template <typename Type>
164bool operator==(const plBoundingBoxTemplate<Type>& lhs, const plBoundingBoxTemplate<Type>& rhs); // [tested]
165
167template <typename Type>
168bool operator!=(const plBoundingBoxTemplate<Type>& lhs, const plBoundingBoxTemplate<Type>& rhs); // [tested]
169
170
171#include <Foundation/Math/Implementation/BoundingBox_inl.h>
An axis-aligned bounding box implementation.
Definition BoundingBox.h:12
void Translate(const plVec3Template< Type > &vDiff)
Moves the box by the given vector.
Definition BoundingBox_inl.h:263
Type GetDistanceSquaredTo(const plVec3Template< Type > &vPoint) const
Returns the squared minimum distance from the box's surface to the point. Zero if the point is inside...
Definition BoundingBox_inl.h:332
const plVec3Template< Type > GetCenter() const
Returns the center position of the box.
Definition BoundingBox_inl.h:78
void ExpandToCube()
If the box is not cubic all extents are set to the value of the maximum extent, such that the box bec...
Definition BoundingBox_inl.h:139
const plVec3Template< Type > GetExtents() const
Returns the extents of the box along each axis.
Definition BoundingBox_inl.h:84
void ExpandToInclude(const plVec3Template< Type > &vPoint)
Expands the box such that the given point is inside it.
Definition BoundingBox_inl.h:108
bool IsNaN() const
Checks whether any component is NaN.
Definition BoundingBox_inl.h:102
Type GetDistanceTo(const plVec3Template< Type > &vPoint) const
Returns the minimum distance from the box's surface to the point. Zero if the point is inside the box...
Definition BoundingBox_inl.h:324
void TransformFromCenter(const plMat4Template< Type > &mTransform)
Transforms the corners of the box in its local space. The center of the box does not change,...
Definition BoundingBox_inl.h:293
static plBoundingBoxTemplate< Type > MakeFromPoints(const plVec3Template< Type > *pPoints, plUInt32 uiNumPoints, plUInt32 uiStride=sizeof(plVec3Template< Type >))
Creates a box around the given set of points. If uiNumPoints is zero, the returned box is invalid (sa...
Definition BoundingBox_inl.h:54
void GetCorners(plVec3Template< Type > *out_pCorners) const
Writes the 8 different corners of the box to the given array.
Definition BoundingBox_inl.h:62
plBoundingBoxTemplate()
Default constructor does not initialize anything.
bool IsValid() const
Checks whether the box is in an invalid state.
Definition BoundingBox_inl.h:96
void ScaleFromCenter(const plVec3Template< Type > &vScale)
Scales the box along each axis, but keeps its center constant.
Definition BoundingBox_inl.h:270
static plBoundingBoxTemplate< Type > MakeFromMinMax(const plVec3Template< Type > &vMin, const plVec3Template< Type > &vMax)
Creates a box with the given minimum and maximum values.
Definition BoundingBox_inl.h:42
void TransformFromOrigin(const plMat4Template< Type > &mTransform)
Transforms the corners of the box and recomputes the AABB of those transformed points....
Definition BoundingBox_inl.h:306
const plVec3Template< Type > GetClampedPoint(const plVec3Template< Type > &vPoint) const
The given point is clamped to the volume of the box, i.e. it will be either inside the box or on its ...
Definition BoundingBox_inl.h:318
bool IsEqual(const plBoundingBoxTemplate &rhs, Type fEpsilon=plMath::DefaultEpsilon< Type >()) const
Checks whether this box and the other box are equal within some threshold.
Definition BoundingBox_inl.h:245
const plVec3Template< Type > GetHalfExtents() const
Returns the half extents of the box along each axis.
Definition BoundingBox_inl.h:90
static plBoundingBoxTemplate< Type > MakeZero()
Creates a box that is located at the origin and has zero size. This is a 'valid' box.
Definition BoundingBox_inl.h:15
bool Contains(const plVec3Template< Type > &vPoint) const
Checks whether the given point is inside the box.
Definition BoundingBox_inl.h:162
bool Overlaps(const plBoundingBoxTemplate &rhs) const
Checks whether this box overlaps with the given box.
Definition BoundingBox_inl.h:197
void Grow(const plVec3Template< Type > &vDiff)
Will increase the size of the box in all directions by the given amount (per axis).
Definition BoundingBox_inl.h:151
static plBoundingBoxTemplate< Type > MakeInvalid()
Creates a box that is in an invalid state. ExpandToInclude can then be used to make it into a boundin...
Definition BoundingBox_inl.h:24
bool GetRayIntersection(const plVec3Template< Type > &vStartPos, const plVec3Template< Type > &vRayDir, Type *out_pIntersectionDistance=nullptr, plVec3Template< Type > *out_pIntersection=nullptr) const
Returns whether the given ray intersects the box. Optionally returns the intersection distance and po...
Definition BoundingBox_inl.h:392
const plBoundingSphereTemplate< Type > GetBoundingSphere() const
Returns a bounding sphere that encloses this box.
Definition AllClasses_inl.h:29
void ScaleFromOrigin(const plVec3Template< Type > &vScale)
Scales the box's corners by the given factors, thus also moves the box around.
Definition BoundingBox_inl.h:282
bool IsIdentical(const plBoundingBoxTemplate &rhs) const
Checks whether this box and the other box are exactly identical.
Definition BoundingBox_inl.h:239
bool GetLineSegmentIntersection(const plVec3Template< Type > &vStartPos, const plVec3Template< Type > &vEndPos, Type *out_pLineFraction=nullptr, plVec3Template< Type > *out_pIntersection=nullptr) const
Checks whether the line segment intersects the box. Optionally returns the intersection point and the...
Definition BoundingBox_inl.h:485
static plBoundingBoxTemplate< Type > MakeFromCenterAndHalfExtents(const plVec3Template< Type > &vCenter, const plVec3Template< Type > &vHalfExtents)
Creates a box from a center point and half-extents for each axis.
Definition BoundingBox_inl.h:33
An implementation of a bounding sphere.
Definition BoundingSphere.h:11
A 4x4 component matrix class.
Definition Mat4.h:11
A 3-component vector class.
Definition Vec3.h:9