Plasma Engine  2.0
Loading...
Searching...
No Matches
BoundingSphere.h
1#pragma once
2
3#include <Foundation/Math/Vec3.h>
4
9template <typename Type>
11{
12public:
13 // Means this object can be copied using memcpy instead of copy construction.
14 PL_DECLARE_POD_TYPE();
15
16 using ComponentType = Type;
17
18public:
21
22
24 [[nodiscard]] static plBoundingSphereTemplate<Type> MakeZero();
25
31
33 [[nodiscard]] static plBoundingSphereTemplate<Type> MakeFromCenterAndRadius(const plVec3Template<Type>& vCenter, Type fRadius);
34
39 [[nodiscard]] static plBoundingSphereTemplate<Type> MakeFromPoints(const plVec3Template<Type>* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride = sizeof(plVec3Template<Type>));
40
41#if PL_ENABLED(PL_MATH_CHECK_FOR_NAN)
42 void AssertNotNaN() const
43 {
44 PL_ASSERT_ALWAYS(!IsNaN(), "This object contains NaN values. This can happen when you forgot to initialize it before using it. Please check that "
45 "all code-paths properly initialize this object.");
46 }
47#endif
48
50 bool IsZero(Type fEpsilon = plMath::DefaultEpsilon<Type>()) const; // [tested]
51
53 bool IsValid() const; // [tested]
54
56 bool IsNaN() const; // [tested]
57
60 void ExpandToInclude(const plVec3Template<Type>& vPoint); // [tested]
61
64 void ExpandToInclude(const plVec3Template<Type>* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride = sizeof(plVec3Template<Type>)); // [tested]
65
67 void ExpandToInclude(const plBoundingSphereTemplate& rhs); // [tested]
68
70 void ExpandToInclude(const plBoundingBoxTemplate<Type>& rhs); // [tested]
71
73 void Grow(Type fDiff); // [tested]
74
76 bool IsIdentical(const plBoundingSphereTemplate& rhs) const; // [tested]
77
79 bool IsEqual(const plBoundingSphereTemplate& rhs, Type fEpsilon = plMath::DefaultEpsilon<Type>()) const; // [tested]
80
82 void Translate(const plVec3Template<Type>& vTranslation); // [tested]
83
85 void ScaleFromCenter(Type fScale); // [tested]
86
88 void ScaleFromOrigin(const plVec3Template<Type>& vScale); // [tested]
89
91 void TransformFromOrigin(const plMat4Template<Type>& mTransform); // [tested]
92
95 void TransformFromCenter(const plMat4Template<Type>& mTransform); // [tested]
96
98 Type GetDistanceTo(const plVec3Template<Type>& vPoint) const; // [tested]
99
102 Type GetDistanceTo(const plBoundingSphereTemplate& rhs) const; // [tested]
103
105 Type GetDistanceTo(const plBoundingBoxTemplate<Type>& rhs) const; // [tested]
106
108 Type GetDistanceTo(const plVec3Template<Type>* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride = sizeof(plVec3Template<Type>)) const; // [tested]
109
111 bool Contains(const plVec3Template<Type>& vPoint) const; // [tested]
112
114 bool Contains(const plVec3Template<Type>* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride = sizeof(plVec3Template<Type>)) const; // [tested]
115
117 bool Contains(const plBoundingSphereTemplate& rhs) const; // [tested]
118
120 bool Contains(const plBoundingBoxTemplate<Type>& rhs) const; // [tested]
121
123 bool Overlaps(const plVec3Template<Type>* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride = sizeof(plVec3Template<Type>)) const; // [tested]
124
126 bool Overlaps(const plBoundingSphereTemplate& rhs) const; // [tested]
127
129 bool Overlaps(const plBoundingBoxTemplate<Type>& rhs) const; // [tested]
130
132 const plBoundingBoxTemplate<Type> GetBoundingBox() const; // [tested]
133
136 [[nodiscard]] const plVec3Template<Type> GetClampedPoint(const plVec3Template<Type>& vPoint); // [tested]
137
141 [[nodiscard]] bool GetRayIntersection(const plVec3Template<Type>& vRayStartPos, const plVec3Template<Type>& vRayDir, Type* out_pIntersectionDistance = nullptr,
142 plVec3Template<Type>* out_pIntersection = nullptr) const; // [tested]
143
145 [[nodiscard]] bool GetLineSegmentIntersection(const plVec3Template<Type>& vLineStartPos, const plVec3Template<Type>& vLineEndPos,
146 Type* out_pHitFraction = nullptr, plVec3Template<Type>* out_pIntersection = nullptr) const; // [tested]
147
148
149public:
150 plVec3Template<Type> m_vCenter;
151 Type m_fRadius;
152};
153
155template <typename Type>
156bool operator==(const plBoundingSphereTemplate<Type>& lhs, const plBoundingSphereTemplate<Type>& rhs); // [tested]
157
159template <typename Type>
160bool operator!=(const plBoundingSphereTemplate<Type>& lhs, const plBoundingSphereTemplate<Type>& rhs); // [tested]
161
162
163#include <Foundation/Math/Implementation/BoundingSphere_inl.h>
An axis-aligned bounding box implementation.
Definition BoundingBox.h:12
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
const plBoundingBoxTemplate< Type > GetBoundingBox() const
Returns a bounding box that encloses this sphere.
Definition AllClasses_inl.h:88
A 4x4 component matrix class.
Definition Mat4.h:11
A 3-component vector class.
Definition Vec3.h:9