Plasma Engine  2.0
Loading...
Searching...
No Matches
SimdBBox_inl.h
1#pragma once
2
3PL_ALWAYS_INLINE plSimdBBox::plSimdBBox() = default;
4
5PL_ALWAYS_INLINE plSimdBBox::plSimdBBox(const plSimdVec4f& vMin, const plSimdVec4f& vMax)
6 : m_Min(vMin)
7 , m_Max(vMax)
8{
9}
10
15
20
21PL_ALWAYS_INLINE plSimdBBox plSimdBBox::MakeFromCenterAndHalfExtents(const plSimdVec4f& vCenter, const plSimdVec4f& vHalfExtents)
22{
23 return plSimdBBox(vCenter - vHalfExtents, vCenter + vHalfExtents);
24}
25
26PL_ALWAYS_INLINE plSimdBBox plSimdBBox::MakeFromMinMax(const plSimdVec4f& vMin, const plSimdVec4f& vMax)
27{
28 return plSimdBBox(vMin, vMax);
29}
30
31PL_ALWAYS_INLINE plSimdBBox plSimdBBox::MakeFromPoints(const plSimdVec4f* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride /*= sizeof(plSimdVec4f)*/)
32{
34 box.ExpandToInclude(pPoints, uiNumPoints, uiStride);
35 return box;
36}
37
38PL_ALWAYS_INLINE void plSimdBBox::SetInvalid()
39{
40 m_Min.Set(plMath::MaxValue<float>());
41 m_Max.Set(-plMath::MaxValue<float>());
42}
43
44PL_ALWAYS_INLINE void plSimdBBox::SetCenterAndHalfExtents(const plSimdVec4f& vCenter, const plSimdVec4f& vHalfExtents)
45{
46 m_Min = vCenter - vHalfExtents;
47 m_Max = vCenter + vHalfExtents;
48}
49
50PL_ALWAYS_INLINE void plSimdBBox::SetFromPoints(const plSimdVec4f* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride)
51{
52 *this = MakeInvalid();
53 ExpandToInclude(pPoints, uiNumPoints, uiStride);
54}
55
56PL_ALWAYS_INLINE bool plSimdBBox::IsValid() const
57{
58 return m_Min.IsValid<3>() && m_Max.IsValid<3>() && (m_Min <= m_Max).AllSet<3>();
59}
60
61PL_ALWAYS_INLINE bool plSimdBBox::IsNaN() const
62{
63 return m_Min.IsNaN<3>() || m_Max.IsNaN<3>();
64}
65
66PL_ALWAYS_INLINE plSimdVec4f plSimdBBox::GetCenter() const
67{
68 return (m_Min + m_Max) * plSimdFloat(0.5f);
69}
70
71PL_ALWAYS_INLINE plSimdVec4f plSimdBBox::GetExtents() const
72{
73 return m_Max - m_Min;
74}
75
77{
78 return (m_Max - m_Min) * plSimdFloat(0.5f);
79}
80
81PL_ALWAYS_INLINE void plSimdBBox::ExpandToInclude(const plSimdVec4f& vPoint)
82{
83 m_Min = m_Min.CompMin(vPoint);
84 m_Max = m_Max.CompMax(vPoint);
85}
86
87inline void plSimdBBox::ExpandToInclude(const plSimdVec4f* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride)
88{
89 PL_ASSERT_DEBUG(pPoints != nullptr, "Array may not be nullptr.");
90 PL_ASSERT_DEBUG(uiStride >= sizeof(plSimdVec4f), "Data may not overlap.");
91
92 const plSimdVec4f* pCur = pPoints;
93
94 for (plUInt32 i = 0; i < uiNumPoints; ++i)
95 {
96 ExpandToInclude(*pCur);
97
98 pCur = plMemoryUtils::AddByteOffset(pCur, uiStride);
99 }
100}
101
102PL_ALWAYS_INLINE void plSimdBBox::ExpandToInclude(const plSimdBBox& rhs)
103{
104 m_Min = m_Min.CompMin(rhs.m_Min);
105 m_Max = m_Max.CompMax(rhs.m_Max);
106}
107
109{
110 const plSimdVec4f center = GetCenter();
111 const plSimdVec4f halfExtents = center - m_Min;
112
113 *this = plSimdBBox::MakeFromCenterAndHalfExtents(center, plSimdVec4f(halfExtents.HorizontalMax<3>()));
114}
115
116PL_ALWAYS_INLINE bool plSimdBBox::Contains(const plSimdVec4f& vPoint) const
117{
118 return ((vPoint >= m_Min) && (vPoint <= m_Max)).AllSet<3>();
119}
120
121PL_ALWAYS_INLINE bool plSimdBBox::Contains(const plSimdBBox& rhs) const
122{
123 return Contains(rhs.m_Min) && Contains(rhs.m_Max);
124}
125
126inline bool plSimdBBox::Contains(const plSimdBSphere& rhs) const
127{
129
130 return Contains(otherBox);
131}
132
133PL_ALWAYS_INLINE bool plSimdBBox::Overlaps(const plSimdBBox& rhs) const
134{
135 return ((m_Max > rhs.m_Min) && (m_Min < rhs.m_Max)).AllSet<3>();
136}
137
138inline bool plSimdBBox::Overlaps(const plSimdBSphere& rhs) const
139{
140 // check whether the closest point between box and sphere is inside the sphere (it is definitely inside the box)
141 return rhs.Contains(GetClampedPoint(rhs.GetCenter()));
142}
143
144PL_ALWAYS_INLINE void plSimdBBox::Grow(const plSimdVec4f& vDiff)
145{
146 m_Max += vDiff;
147 m_Min -= vDiff;
148}
149
150PL_ALWAYS_INLINE void plSimdBBox::Translate(const plSimdVec4f& vDiff)
151{
152 m_Min += vDiff;
153 m_Max += vDiff;
154}
155
156PL_ALWAYS_INLINE void plSimdBBox::Transform(const plSimdTransform& t)
157{
158 Transform(t.GetAsMat4());
159}
160
161PL_ALWAYS_INLINE void plSimdBBox::Transform(const plSimdMat4f& mMat)
162{
163 const plSimdVec4f center = GetCenter();
164 const plSimdVec4f halfExtents = center - m_Min;
165
166 const plSimdVec4f newCenter = mMat.TransformPosition(center);
167
168 plSimdVec4f newHalfExtents = mMat.m_col0.Abs() * halfExtents.x();
169 newHalfExtents += mMat.m_col1.Abs() * halfExtents.y();
170 newHalfExtents += mMat.m_col2.Abs() * halfExtents.z();
171
172 *this = plSimdBBox::MakeFromCenterAndHalfExtents(newCenter, newHalfExtents);
173}
174
175PL_ALWAYS_INLINE plSimdVec4f plSimdBBox::GetClampedPoint(const plSimdVec4f& vPoint) const
176{
177 return vPoint.CompMin(m_Max).CompMax(m_Min);
178}
179
181{
182 const plSimdVec4f vClamped = GetClampedPoint(vPoint);
183
184 return (vPoint - vClamped).GetLengthSquared<3>();
185}
186
188{
189 const plSimdVec4f vClamped = GetClampedPoint(vPoint);
190
191 return (vPoint - vClamped).GetLength<3>();
192}
193
194PL_ALWAYS_INLINE bool plSimdBBox::operator==(const plSimdBBox& rhs) const
195{
196 return ((m_Min == rhs.m_Min) && (m_Max == rhs.m_Max)).AllSet<3>();
197}
198
199PL_ALWAYS_INLINE bool plSimdBBox::operator!=(const plSimdBBox& rhs) const
200{
201 return ((m_Min != rhs.m_Min) || (m_Max != rhs.m_Max)).AnySet<3>();
202}
static T * AddByteOffset(T *pPtr, std::ptrdiff_t offset)
Returns the address stored in ptr plus the given byte offset iOffset, cast to type T.
Definition SimdBBox.h:6
void SetFromPoints(const plSimdVec4f *pPoints, plUInt32 uiNumPoints, plUInt32 uiStride=sizeof(plSimdVec4f))
Creates a new bounding-box around the given set of points.
Definition SimdBBox_inl.h:50
plSimdVec4f GetExtents() const
Returns the extents of the box along each axis.
Definition SimdBBox_inl.h:71
static plSimdBBox MakeFromPoints(const plSimdVec4f *pPoints, plUInt32 uiNumPoints, plUInt32 uiStride=sizeof(plSimdVec4f))
Creates a box around the given set of points. If uiNumPoints is zero, the returned box is invalid (sa...
Definition SimdBBox_inl.h:31
void SetInvalid()
Resets the box to an invalid state. ExpandToInclude can then be used to make it into a bounding box f...
Definition SimdBBox_inl.h:38
void SetCenterAndHalfExtents(const plSimdVec4f &vCenter, const plSimdVec4f &vHalfExtents)
Sets the box from a center point and half-extents for each axis.
Definition SimdBBox_inl.h:44
bool Overlaps(const plSimdBBox &rhs) const
Checks whether this box overlaps with the given box.
Definition SimdBBox_inl.h:133
static plSimdBBox MakeZero()
Creates a box that is located at the origin and has zero size. This is a 'valid' box.
Definition SimdBBox_inl.h:11
bool IsNaN() const
Checks whether any component is NaN.
Definition SimdBBox_inl.h:61
static plSimdBBox MakeFromMinMax(const plSimdVec4f &vMin, const plSimdVec4f &vMax)
Creates a box with the given minimum and maximum values.
Definition SimdBBox_inl.h:26
plSimdBBox()
Default constructor does not initialize anything.
plSimdVec4f GetHalfExtents() const
Returns the half extents of the box along each axis.
Definition SimdBBox_inl.h:76
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 SimdBBox_inl.h:108
bool IsValid() const
Checks whether the box is in an invalid state.
Definition SimdBBox_inl.h:56
void Transform(const plSimdTransform &transform)
Transforms the corners of the box and recomputes the aabb of those transformed points.
Definition SimdBBox_inl.h:156
plSimdVec4f GetClampedPoint(const plSimdVec4f &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 SimdBBox_inl.h:175
void ExpandToInclude(const plSimdVec4f &vPoint)
Expands the box such that the given point is inside it.
Definition SimdBBox_inl.h:81
plSimdVec4f GetCenter() const
Returns the center position of the box.
Definition SimdBBox_inl.h:66
static plSimdBBox MakeInvalid()
Creates a box that is in an invalid state. ExpandToInclude can then be used to make it into a boundin...
Definition SimdBBox_inl.h:16
plSimdFloat GetDistanceSquaredTo(const plSimdVec4f &vPoint) const
Returns the squared minimum distance from the box's surface to the point. Zero if the point is inside...
Definition SimdBBox_inl.h:180
bool Contains(const plSimdVec4f &vPoint) const
Checks whether the given point is inside the box.
Definition SimdBBox_inl.h:116
void Translate(const plSimdVec4f &vDiff)
Moves the box by the given vector.
Definition SimdBBox_inl.h:150
static plSimdBBox MakeFromCenterAndHalfExtents(const plSimdVec4f &vCenter, const plSimdVec4f &vHalfExtents)
Creates a box from a center point and half-extents for each axis.
Definition SimdBBox_inl.h:21
void Grow(const plSimdVec4f &vDiff)
Will increase the size of the box in all directions by the given amount (per axis).
Definition SimdBBox_inl.h:144
plSimdFloat GetDistanceTo(const plSimdVec4f &vPoint) const
Returns the minimum distance from the box's surface to the point. Zero if the point is inside the box...
Definition SimdBBox_inl.h:187
Definition SimdBSphere.h:6
plSimdFloat GetRadius() const
Returns the radius.
Definition SimdBSphere_inl.h:85
bool Contains(const plSimdVec4f &vPoint) const
Returns true if the given point is inside the sphere.
Definition SimdBSphere_inl.h:160
plSimdVec4f GetCenter() const
Returns the center.
Definition SimdBSphere_inl.h:80
Definition SimdFloat.h:7
A 4x4 matrix class.
Definition SimdMat4f.h:7
plSimdVec4f TransformPosition(const plSimdVec4f &v) const
Matrix-vector multiplication, assuming the 4th component of the vector is one (default behavior).
Definition SimdMat4f_inl.h:141
Definition SimdTransform.h:6
plSimdMat4f GetAsMat4() const
Returns the transformation as a matrix.
Definition SimdTransform_inl.h:90
A 4-component SIMD vector class.
Definition SimdVec4f.h:8
static plSimdVec4f MakeZero()
Creates an plSimdVec4f that is initialized to zero.
Definition SimdVec4f_inl.h:8
constexpr TYPE MaxValue()
Returns the largest possible positive value (that is not infinity).