Plasma Engine  2.0
Loading...
Searching...
No Matches
SimdBBoxSphere_inl.h
1#pragma once
2
3PL_ALWAYS_INLINE plSimdBBoxSphere::plSimdBBoxSphere() = default;
4
5PL_ALWAYS_INLINE plSimdBBoxSphere::plSimdBBoxSphere(const plSimdVec4f& vCenter, const plSimdVec4f& vBoxHalfExtents, const plSimdFloat& fSphereRadius)
6 : m_CenterAndRadius(vCenter)
7 , m_BoxHalfExtents(vBoxHalfExtents)
8{
9 m_CenterAndRadius.SetW(fSphereRadius);
10}
11
13{
14 *this = MakeFromBoxAndSphere(box, sphere);
15}
16
18 : m_CenterAndRadius(box.GetCenter())
19 , m_BoxHalfExtents(m_CenterAndRadius - box.m_Min)
20{
21 m_CenterAndRadius.SetW(m_BoxHalfExtents.GetLength<3>());
22}
23
25 : m_CenterAndRadius(sphere.m_CenterAndRadius)
26 , m_BoxHalfExtents(plSimdVec4f(sphere.GetRadius()))
27{
28}
29
31{
33 res.m_CenterAndRadius = plSimdVec4f::MakeZero();
34 res.m_BoxHalfExtents = plSimdVec4f::MakeZero();
35 return res;
36}
37
39{
41 res.m_CenterAndRadius.Set(0.0f, 0.0f, 0.0f, -plMath::SmallEpsilon<float>());
42 res.m_BoxHalfExtents.Set(-plMath::MaxValue<float>());
43 return res;
44}
45
46PL_ALWAYS_INLINE plSimdBBoxSphere plSimdBBoxSphere::MakeFromCenterExtents(const plSimdVec4f& vCenter, const plSimdVec4f& vBoxHalfExtents, const plSimdFloat& fSphereRadius)
47{
49 res.m_CenterAndRadius = vCenter;
50 res.m_BoxHalfExtents = vBoxHalfExtents;
51 res.m_CenterAndRadius.SetW(fSphereRadius);
52 return res;
53}
54
55inline plSimdBBoxSphere plSimdBBoxSphere::MakeFromPoints(const plSimdVec4f* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride /*= sizeof(plSimdVec4f)*/)
56{
57 const plSimdBBox box = plSimdBBox::MakeFromPoints(pPoints, uiNumPoints, uiStride);
58
60
61 res.m_CenterAndRadius = box.GetCenter();
62 res.m_BoxHalfExtents = res.m_CenterAndRadius - box.m_Min;
63
64 plSimdBSphere sphere(res.m_CenterAndRadius, plSimdFloat::MakeZero());
65 sphere.ExpandToInclude(pPoints, uiNumPoints, uiStride);
66
67 res.m_CenterAndRadius.SetW(sphere.GetRadius());
68
69 return res;
70}
71
73{
74 return plSimdBBoxSphere(box);
75}
76
78{
79 return plSimdBBoxSphere(sphere);
80}
81
83{
85 res.m_CenterAndRadius = box.GetCenter();
86 res.m_BoxHalfExtents = res.m_CenterAndRadius - box.m_Min;
87 res.m_CenterAndRadius.SetW(res.m_BoxHalfExtents.GetLength<3>().Min((sphere.GetCenter() - res.m_CenterAndRadius).GetLength<3>() + sphere.GetRadius()));
88 return res;
89}
90
91PL_ALWAYS_INLINE void plSimdBBoxSphere::SetInvalid()
92{
93 m_CenterAndRadius.Set(0.0f, 0.0f, 0.0f, -plMath::SmallEpsilon<float>());
94 m_BoxHalfExtents.Set(-plMath::MaxValue<float>());
95}
96
97PL_ALWAYS_INLINE bool plSimdBBoxSphere::IsValid() const
98{
99 return m_CenterAndRadius.IsValid<4>() && m_CenterAndRadius.w() >= plSimdFloat::MakeZero() && m_BoxHalfExtents.IsValid<3>() &&
100 (m_BoxHalfExtents >= plSimdVec4f::MakeZero()).AllSet<3>();
101}
102
103inline bool plSimdBBoxSphere::IsNaN() const
104{
105 return m_CenterAndRadius.IsNaN<4>() || m_BoxHalfExtents.IsNaN<3>();
106}
107
108PL_ALWAYS_INLINE void plSimdBBoxSphere::SetFromPoints(const plSimdVec4f* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride)
109{
110 *this = MakeFromPoints(pPoints, uiNumPoints, uiStride);
111}
112
113PL_ALWAYS_INLINE plSimdBBox plSimdBBoxSphere::GetBox() const
114{
115 return plSimdBBox::MakeFromCenterAndHalfExtents(m_CenterAndRadius, m_BoxHalfExtents);
116}
117
119{
120 plSimdBSphere sphere;
121 sphere.m_CenterAndRadius = m_CenterAndRadius;
122 return sphere;
123}
124
126{
127 plSimdBBox box = GetBox();
128 box.ExpandToInclude(rhs.GetBox());
129
130 plSimdVec4f center = box.GetCenter();
131 plSimdVec4f boxHalfExtents = center - box.m_Min;
132 plSimdFloat tmpRadius = boxHalfExtents.GetLength<3>();
133
134 const plSimdFloat fSphereRadiusA = (m_CenterAndRadius - center).GetLength<3>() + m_CenterAndRadius.w();
135 const plSimdFloat fSphereRadiusB = (rhs.m_CenterAndRadius - center).GetLength<3>() + rhs.m_CenterAndRadius.w();
136
137 m_CenterAndRadius = center;
138 m_CenterAndRadius.SetW(tmpRadius.Min(fSphereRadiusA.Max(fSphereRadiusB)));
139 m_BoxHalfExtents = boxHalfExtents;
140}
141
142PL_ALWAYS_INLINE void plSimdBBoxSphere::Transform(const plSimdTransform& t)
143{
144 Transform(t.GetAsMat4());
145}
146
147PL_ALWAYS_INLINE void plSimdBBoxSphere::Transform(const plSimdMat4f& mMat)
148{
149 plSimdFloat radius = m_CenterAndRadius.w();
150 m_CenterAndRadius = mMat.TransformPosition(m_CenterAndRadius);
151
152 plSimdFloat maxRadius = mMat.m_col0.Dot<3>(mMat.m_col0);
153 maxRadius = maxRadius.Max(mMat.m_col1.Dot<3>(mMat.m_col1));
154 maxRadius = maxRadius.Max(mMat.m_col2.Dot<3>(mMat.m_col2));
155 radius *= maxRadius.GetSqrt();
156
157 m_CenterAndRadius.SetW(radius);
158
159 plSimdVec4f newHalfExtents = mMat.m_col0.Abs() * m_BoxHalfExtents.x();
160 newHalfExtents += mMat.m_col1.Abs() * m_BoxHalfExtents.y();
161 newHalfExtents += mMat.m_col2.Abs() * m_BoxHalfExtents.z();
162
163 m_BoxHalfExtents = newHalfExtents.CompMin(plSimdVec4f(radius));
164}
165
166PL_ALWAYS_INLINE bool plSimdBBoxSphere::operator==(const plSimdBBoxSphere& rhs) const
167{
168 return (m_CenterAndRadius == rhs.m_CenterAndRadius).AllSet<4>() && (m_BoxHalfExtents == rhs.m_BoxHalfExtents).AllSet<3>();
169}
170
171PL_ALWAYS_INLINE bool plSimdBBoxSphere::operator!=(const plSimdBBoxSphere& rhs) const
172{
173 return !(*this == rhs);
174}
Definition SimdBBox.h:6
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 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 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
Definition SimdBBoxSphere.h:6
plSimdBSphere GetSphere() const
Returns the bounding sphere.
Definition SimdBBoxSphere_inl.h:118
static plSimdBBoxSphere MakeFromPoints(const plSimdVec4f *pPoints, plUInt32 uiNumPoints, plUInt32 uiStride=sizeof(plSimdVec4f))
Creates an object that contains all the provided points.
Definition SimdBBoxSphere_inl.h:55
static plSimdBBoxSphere MakeZero()
Creates an object with all zero values. These are valid bounds around the origin with no volume.
Definition SimdBBoxSphere_inl.h:30
plSimdBBoxSphere()
Default constructor does not initialize anything.
void SetInvalid()
Resets the bounds to an invalid state.
Definition SimdBBoxSphere_inl.h:91
void Transform(const plSimdTransform &t)
Transforms the bounds in its local space.
Definition SimdBBoxSphere_inl.h:142
void ExpandToInclude(const plSimdBBoxSphere &rhs)
Expands the bounds such that the given bounds are inside it.
Definition SimdBBoxSphere_inl.h:125
bool IsNaN() const
Checks whether any component is NaN.
Definition SimdBBoxSphere_inl.h:103
void SetFromPoints(const plSimdVec4f *pPoints, plUInt32 uiNumPoints, plUInt32 uiStride=sizeof(plSimdVec4f))
Calculates the bounds from given set of points.
Definition SimdBBoxSphere_inl.h:108
static plSimdBBoxSphere MakeFromBox(const plSimdBBox &box)
Creates an object from another bounding box.
Definition SimdBBoxSphere_inl.h:72
static plSimdBBoxSphere MakeFromBoxAndSphere(const plSimdBBox &box, const plSimdBSphere &sphere)
Creates an object from another bounding box and a sphere.
Definition SimdBBoxSphere_inl.h:82
static plSimdBBoxSphere MakeFromSphere(const plSimdBSphere &sphere)
Creates an object from another bounding sphere.
Definition SimdBBoxSphere_inl.h:77
plSimdBBox GetBox() const
Returns the bounding box.
Definition SimdBBoxSphere_inl.h:113
static plSimdBBoxSphere MakeFromCenterExtents(const plSimdVec4f &vCenter, const plSimdVec4f &vBoxHalfExtents, const plSimdFloat &fSphereRadius)
Creates an object from the given center point and extents.
Definition SimdBBoxSphere_inl.h:46
static plSimdBBoxSphere MakeInvalid()
Creates an 'invalid' object, ie one with negative extents/radius. Invalid objects can be made valid t...
Definition SimdBBoxSphere_inl.h:38
bool IsValid() const
Checks whether the bounds is in an invalid state.
Definition SimdBBoxSphere_inl.h:97
Definition SimdBSphere.h:6
plSimdFloat GetRadius() const
Returns the radius.
Definition SimdBSphere_inl.h:85
void ExpandToInclude(const plSimdVec4f &vPoint)
Increases the sphere's radius to include this point.
Definition SimdBSphere_inl.h:95
plSimdVec4f GetCenter() const
Returns the center.
Definition SimdBSphere_inl.h:80
Definition SimdFloat.h:7
static plSimdFloat MakeZero()
Creates an plSimdFloat that is initialized to zero.
Definition FPUFloat_inl.h:36
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).