3#include <Foundation/Math/Mat4.h>
5template <
typename Type>
8#if PL_ENABLED(PL_MATH_CHECK_FOR_NAN)
16template <
typename Type>
20 res.m_vCenter.SetZero();
25template <
typename Type>
29 res.m_vCenter = vCenter;
30 res.m_fRadius = -plMath::SmallEpsilon<Type>();
34template <
typename Type>
38 res.m_vCenter = vCenter;
39 res.m_fRadius = fRadius;
40 PL_ASSERT_DEBUG(res.
IsValid(),
"The sphere was created with invalid values.");
44template <
typename Type>
47 PL_ASSERT_DEBUG(pPoints !=
nullptr,
"The array must not be empty.");
49 PL_ASSERT_DEBUG(uiNumPoints > 0,
"The array must contain at least one point.");
55 for (plUInt32 i = 0; i < uiNumPoints; ++i)
61 vCenter /= (Type)uiNumPoints;
63 Type fMaxDistSQR = 0.0f;
66 for (plUInt32 i = 0; i < uiNumPoints; ++i)
68 const Type fDistSQR = (*pCur - vCenter).GetLengthSquared();
75 res.m_vCenter = vCenter;
78 PL_ASSERT_DEBUG(res.
IsValid(),
"The point cloud contained corrupted data.");
83template <
typename Type>
89template <
typename Type>
92 return (m_vCenter.IsValid() && m_fRadius >= 0.0f);
95template <
typename Type>
98 return (m_vCenter.IsNaN() || plMath::IsNaN(m_fRadius));
101template <
typename Type>
104 const Type fDistSQR = (vPoint - m_vCenter).GetLengthSquared();
110template <
typename Type>
113 const Type fReqRadius = (rhs.m_vCenter - m_vCenter).GetLength() + rhs.m_fRadius;
118template <
typename Type>
121 PL_ASSERT_DEBUG(IsValid(),
"Cannot grow a sphere that is invalid.");
125 PL_ASSERT_DEBUG(IsValid(),
"The grown sphere has become invalid.");
128template <
typename Type>
131 return (m_vCenter.IsIdentical(rhs.m_vCenter) && m_fRadius == rhs.m_fRadius);
134template <
typename Type>
137 return (m_vCenter.IsEqual(rhs.m_vCenter, fEpsilon) &&
plMath::IsEqual(m_fRadius, rhs.m_fRadius, fEpsilon));
140template <
typename Type>
146template <
typename Type>
152template <
typename Type>
155 m_vCenter += vTranslation;
158template <
typename Type>
161 PL_ASSERT_DEBUG(fScale >= 0.0f,
"Cannot invert the sphere.");
168template <
typename Type>
171 PL_ASSERT_DEBUG(vScale.x >= 0.0f,
"Cannot invert the sphere.");
172 PL_ASSERT_DEBUG(vScale.y >= 0.0f,
"Cannot invert the sphere.");
173 PL_ASSERT_DEBUG(vScale.z >= 0.0f,
"Cannot invert the sphere.");
175 m_vCenter = m_vCenter.CompMul(vScale);
179 m_fRadius *=
plMath::Max(vScale.x, vScale.y, vScale.z);
182template <
typename Type>
188 m_fRadius *=
plMath::Max(Scale.x, Scale.y, Scale.z);
191template <
typename Type>
197 m_fRadius *=
plMath::Max(Scale.x, Scale.y, Scale.z);
200template <
typename Type>
203 return (vPoint - m_vCenter).GetLength() - m_fRadius;
206template <
typename Type>
209 return (rhs.m_vCenter - m_vCenter).GetLength() - m_fRadius - rhs.m_fRadius;
212template <
typename Type>
215 return (vPoint - m_vCenter).GetLengthSquared() <=
plMath::Square(m_fRadius);
218template <
typename Type>
221 return (rhs.m_vCenter - m_vCenter).GetLength() + rhs.m_fRadius <= m_fRadius;
224template <
typename Type>
227 return (rhs.m_vCenter - m_vCenter).GetLengthSquared() <
plMath::Square(rhs.m_fRadius + m_fRadius);
230template <
typename Type>
244 return m_vCenter + m_fRadius * (vDir / fLength);
247template <
typename Type>
250 PL_ASSERT_DEBUG(pPoints !=
nullptr,
"The array must not be empty.");
251 PL_ASSERT_DEBUG(uiNumPoints > 0,
"The array must contain at least one point.");
258 for (plUInt32 i = 0; i < uiNumPoints; ++i)
260 if ((*pCur - m_vCenter).GetLengthSquared() > fRadiusSQR)
269template <
typename Type>
272 PL_ASSERT_DEBUG(pPoints !=
nullptr,
"The array must not be empty.");
273 PL_ASSERT_DEBUG(uiNumPoints > 0,
"The array must contain at least one point.");
280 for (plUInt32 i = 0; i < uiNumPoints; ++i)
282 if ((*pCur - m_vCenter).GetLengthSquared() <= fRadiusSQR)
291template <
typename Type>
294 PL_ASSERT_DEBUG(pPoints !=
nullptr,
"The array must not be empty.");
299 Type fMaxDistSQR = 0.0f;
301 for (plUInt32 i = 0; i < uiNumPoints; ++i)
303 const Type fDistSQR = (*pCur - m_vCenter).GetLengthSquared();
313template <
typename Type>
316 PL_ASSERT_DEBUG(pPoints !=
nullptr,
"The array must not be empty.");
317 PL_ASSERT_DEBUG(uiNumPoints > 0,
"The array must contain at least one point.");
324 for (plUInt32 i = 0; i < uiNumPoints; ++i)
326 const Type fDistSQR = (*pCur - m_vCenter).GetLengthSquared();
336template <
typename Type>
340 PL_ASSERT_DEBUG(vRayDirNormalized.
IsNormalized(),
"The ray direction must be normalized.");
347 const Type d = vRelPos.
Dot(vRayDirNormalized);
350 if (d < 0.0f && fRelPosLenSQR > fRadiusSQR)
360 Type fIntersectionTime;
362 if (fRelPosLenSQR > fRadiusSQR)
363 fIntersectionTime = d - q;
365 fIntersectionTime = d + q;
367 if (out_pIntersectionDistance)
368 *out_pIntersectionDistance = fIntersectionTime;
369 if (out_pIntersection)
370 *out_pIntersection = vRayStartPos + vRayDirNormalized * fIntersectionTime;
375template <
typename Type>
379 Type fIntersection = 0.0f;
385 if (!GetRayIntersection(vLineStartPos, vDirNorm, &fIntersection))
388 if (fIntersection > fLen)
391 if (out_pHitFraction)
392 *out_pHitFraction = fIntersection / fLen;
394 if (out_pIntersection)
395 *out_pIntersection = vLineStartPos + vDirNorm * fIntersection;
400#include <Foundation/Math/Implementation/AllClasses_inl.h>
An implementation of a bounding sphere.
Definition BoundingSphere.h:11
void Translate(const plVec3Template< Type > &vTranslation)
Moves the sphere by the given vector.
Definition BoundingSphere_inl.h:153
void Grow(Type fDiff)
Increases the size of the sphere by the given amount.
Definition BoundingSphere_inl.h:119
static plBoundingSphereTemplate< Type > MakeFromCenterAndRadius(const plVec3Template< Type > &vCenter, Type fRadius)
Creates a sphere with the provided center and radius.
Definition BoundingSphere_inl.h:35
void TransformFromCenter(const plMat4Template< Type > &mTransform)
Transforms the sphere with the given matrix from its own center. I.e. rotations have no effect,...
Definition BoundingSphere_inl.h:192
bool IsZero(Type fEpsilon=plMath::DefaultEpsilon< Type >()) const
Checks whether the sphere is all zero.
Definition BoundingSphere_inl.h:84
void TransformFromOrigin(const plMat4Template< Type > &mTransform)
Transforms the sphere with the given matrix from the world origin. I.e. scalings and rotations will i...
Definition BoundingSphere_inl.h:183
bool IsIdentical(const plBoundingSphereTemplate &rhs) const
Tests whether two spheres are identical.
Definition BoundingSphere_inl.h:129
void ScaleFromCenter(Type fScale)
Scales the sphere's size, does not change its center position.
Definition BoundingSphere_inl.h:159
bool GetLineSegmentIntersection(const plVec3Template< Type > &vLineStartPos, const plVec3Template< Type > &vLineEndPos, Type *out_pHitFraction=nullptr, plVec3Template< Type > *out_pIntersection=nullptr) const
Returns true if the line segment intersects the sphere.
Definition BoundingSphere_inl.h:376
bool IsValid() const
Returns whether the sphere has valid values.
Definition BoundingSphere_inl.h:90
Type GetDistanceTo(const plVec3Template< Type > &vPoint) const
Computes the distance of the point to the sphere's surface. Returns negative values for points inside...
Definition BoundingSphere_inl.h:201
static plBoundingSphereTemplate< Type > MakeFromPoints(const plVec3Template< Type > *pPoints, plUInt32 uiNumPoints, plUInt32 uiStride=sizeof(plVec3Template< Type >))
Creates a bounding sphere around the provided points.
Definition BoundingSphere_inl.h:45
void ScaleFromOrigin(const plVec3Template< Type > &vScale)
Scales the sphere in world unites, meaning its center position will change as well.
Definition BoundingSphere_inl.h:169
const plVec3Template< Type > GetClampedPoint(const plVec3Template< Type > &vPoint)
Clamps the given position to the volume of the sphere. The resulting point will always be inside the ...
Definition BoundingSphere_inl.h:231
bool Overlaps(const plVec3Template< Type > *pPoints, plUInt32 uiNumPoints, plUInt32 uiStride=sizeof(plVec3Template< Type >)) const
Checks whether any of the given points is inside the sphere.
Definition BoundingSphere_inl.h:270
void ExpandToInclude(const plVec3Template< Type > &vPoint)
Increases the sphere's radius to include this point. Does NOT change its position,...
Definition BoundingSphere_inl.h:102
static plBoundingSphereTemplate< Type > MakeZero()
Creates a sphere at the origin with radius zero.
Definition BoundingSphere_inl.h:17
static plBoundingSphereTemplate< Type > MakeInvalid(const plVec3Template< Type > &vCenter=plVec3Template< Type >::MakeZero())
Creates an 'invalid' sphere, with its center at the given position and a negative radius.
Definition BoundingSphere_inl.h:26
bool IsNaN() const
Returns whether any value is NaN.
Definition BoundingSphere_inl.h:96
bool GetRayIntersection(const plVec3Template< Type > &vRayStartPos, const plVec3Template< Type > &vRayDir, Type *out_pIntersectionDistance=nullptr, plVec3Template< Type > *out_pIntersection=nullptr) const
Computes the intersection of a ray with this sphere. Returns true if there was an intersection....
Definition BoundingSphere_inl.h:337
plBoundingSphereTemplate()
Default constructor does not initialize any data.
Definition BoundingSphere_inl.h:6
bool IsEqual(const plBoundingSphereTemplate &rhs, Type fEpsilon=plMath::DefaultEpsilon< Type >()) const
Tests whether two spheres are equal within some threshold.
Definition BoundingSphere_inl.h:135
bool Contains(const plVec3Template< Type > &vPoint) const
Returns true if the given point is inside the sphere.
Definition BoundingSphere_inl.h:213
A 4x4 component matrix class.
Definition Mat4.h:11
const plVec3Template< Type > GetTranslationVector() const
Returns the first 3 components of the last column.
Definition Mat4_inl.h:430
const plVec3Template< Type > TransformPosition(const plVec3Template< Type > &v) const
Matrix-vector multiplication, assuming the 4th component of the vector is one (default behavior).
Definition Mat4_inl.h:347
const plVec3Template< Type > GetScalingFactors() const
Returns the 3 scaling factors that are encoded in the matrix.
Definition Mat4_inl.h:737
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.
A 3-component vector class.
Definition Vec3.h:9
PL_DECLARE_IF_FLOAT_TYPE bool IsNormalized(Type fEpsilon=plMath::HugeEpsilon< Type >()) const
Returns, whether the squared length of this vector is between 0.999f and 1.001f.
Definition Vec3_inl.h:121
Type GetLengthSquared() const
Returns the squared length. Faster, since no square-root is taken. Useful, if one only wants to compa...
Definition Vec3_inl.h:70
PL_DECLARE_IF_FLOAT_TYPE Type GetLengthAndNormalize()
Normalizes this vector and returns its previous length in one operation. More efficient than calling ...
Definition Vec3_inl.h:78
Type Dot(const plVec3Template< Type > &rhs) const
Returns the Dot-product of the two vectors (commutative, order does not matter)
Definition Vec3_inl.h:290
constexpr PL_ALWAYS_INLINE T Square(T f)
Returns f * f.
Definition Math_inl.h:8
constexpr PL_ALWAYS_INLINE T Min(T f1, T f2)
Returns the smaller value, f1 or f2.
Definition Math_inl.h:27
constexpr TYPE MaxValue()
Returns the largest possible positive value (that is not infinity).
constexpr TYPE NaN()
Returns the value for NaN as the template type. Returns zero, if the type does not support NaN.
Definition Constants_inl.h:58
PL_ALWAYS_INLINE double Sqrt(double f)
Returns the square root of f.
Definition MathDouble_inl.h:99
bool IsZero(Type f, Type fEpsilon)
Checks whether the given number is close to zero.
Definition Math_inl.h:288
constexpr bool IsEqual(Type lhs, Type rhs, Type fEpsilon)
Checks, whether fValue is in the range [fDesired - fMaxImprecision; fDesired + fMaxImprecision].
Definition Math_inl.h:276
constexpr PL_ALWAYS_INLINE T Max(T f1, T f2)
Returns the greater value, f1 or f2.
Definition Math_inl.h:39