Plasma Engine  2.0
Loading...
Searching...
No Matches
Plane.h
1#pragma once
2
3#include <Foundation/Math/Vec3.h>
4#include <Foundation/Math/Vec4.h>
5
8{
16};
17
19template <typename Type>
21{
22public:
23 // Means this object can be copied using memcpy instead of copy construction.
24 PL_DECLARE_POD_TYPE();
25
26 using ComponentType = Type;
27
28 // *** Data ***
29public:
30 plVec3Template<Type> m_vNormal;
31 Type m_fNegDistance;
32
33
34 // *** Constructors ***
35public:
37 plPlaneTemplate(); // [tested]
38
40 [[nodiscard]] static plPlaneTemplate<Type> MakeInvalid();
41
45 [[nodiscard]] static plPlaneTemplate<Type> MakeFromNormalAndPoint(const plVec3Template<Type>& vNormal, const plVec3Template<Type>& vPointOnPlane);
46
53
54#if PL_ENABLED(PL_MATH_CHECK_FOR_NAN)
55 void AssertNotNaN() const
56 {
57 PL_ASSERT_ALWAYS(!IsNaN(), "This object contains NaN values. This can happen when you forgot to initialize it before using it. Please check that "
58 "all code-paths properly initialize this object.");
59 }
60#endif
61
64
66 plResult SetFromPoints(const plVec3Template<Type>& v1, const plVec3Template<Type>& v2, const plVec3Template<Type>& v3); // [tested]
67
69 plResult SetFromPoints(const plVec3Template<Type>* const pVertices); // [tested]
70
73 plResult SetFromPoints(const plVec3Template<Type>* const pVertices, plUInt32 uiMaxVertices); // [tested]
74
76 plResult SetFromDirections(const plVec3Template<Type>& vTangent1, const plVec3Template<Type>& vTangent2, const plVec3Template<Type>& vPointOnPlane); // [tested]
77
78 // *** Distance and Position ***
79public:
81 Type GetDistanceTo(const plVec3Template<Type>& vPoint) const; // [tested]
82
87 Type GetMinimumDistanceTo(const plVec3Template<Type>* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride = sizeof(plVec3Template<Type>)) const; // [tested]
88
90 Type GetMinimumDistanceTo(const plBoundingBoxTemplate<Type>& box) const; // [tested]
91
93 Type GetMaximumDistanceTo(const plBoundingBoxTemplate<Type>& box) const; // [tested]
94
99 void GetMinMaxDistanceTo(Type& out_fMin, Type& out_fMax, const plVec3Template<Type>* pPoints, plUInt32 uiNumPoints, plUInt32 uiStride = sizeof(plVec3Template<Type>)) const; // [tested]
100
102 plPositionOnPlane::Enum GetPointPosition(const plVec3Template<Type>& vPoint) const; // [tested]
103
105 plPositionOnPlane::Enum GetPointPosition(const plVec3Template<Type>& vPoint, Type fPlaneHalfWidth) const; // [tested]
106
108 plPositionOnPlane::Enum GetObjectPosition(const plVec3Template<Type>* const pPoints, plUInt32 uiVertices) const; // [tested]
109
111 plPositionOnPlane::Enum GetObjectPosition(const plVec3Template<Type>* const pPoints, plUInt32 uiVertices, Type fPlaneHalfWidth) const; // [tested]
112
115
118
120 [[nodiscard]] const plVec3Template<Type> ProjectOntoPlane(const plVec3Template<Type>& vPoint) const; // [tested]
121
123 [[nodiscard]] const plVec3Template<Type> Mirror(const plVec3Template<Type>& vPoint) const; // [tested]
124
126 const plVec3Template<Type> GetCoplanarDirection(const plVec3Template<Type>& vDirection) const; // [tested]
127
128 // *** Comparisons ***
129public:
131 bool IsIdentical(const plPlaneTemplate<Type>& rhs) const; // [tested]
132
134 bool IsEqual(const plPlaneTemplate<Type>& rhs, Type fEpsilon = plMath::DefaultEpsilon<Type>()) const; // [tested]
135
137 bool IsValid() const; // [tested]
138
140 bool IsNaN() const; // [tested]
141
143 bool IsFinite() const; // [tested]
144
145 // *** Modifications ***
146public:
148 void Transform(const plMat3Template<Type>& m); // [tested]
149
151 void Transform(const plMat4Template<Type>& m); // [tested]
152
154 void Flip(); // [tested]
155
157 bool FlipIfNecessary(const plVec3Template<Type>& vPoint, bool bPlaneShouldFacePoint = true); // [tested]
158
159 // *** Intersection Tests ***
160public:
169 [[nodiscard]] bool GetRayIntersection(const plVec3Template<Type>& vRayStartPos, const plVec3Template<Type>& vRayDir, Type* out_pIntersectionDinstance = nullptr, plVec3Template<Type>* out_pIntersection = nullptr) const; // [tested]
170
173 [[nodiscard]] bool GetRayIntersectionBiDirectional(const plVec3Template<Type>& vRayStartPos, const plVec3Template<Type>& vRayDir, Type* out_pIntersectionDistance = nullptr, plVec3Template<Type>* out_pIntersection = nullptr) const; // [tested]
174
177 [[nodiscard]] bool GetLineSegmentIntersection(const plVec3Template<Type>& vLineStartPos, const plVec3Template<Type>& vLineEndPos, Type* out_pHitFraction = nullptr, plVec3Template<Type>* out_pIntersection = nullptr) const; // [tested]
178
181
182 // *** Helper Functions ***
183public:
185 static plResult FindSupportPoints(const plVec3Template<Type>* const pVertices, plInt32 iMaxVertices, plInt32& out_i1, plInt32& out_i2, plInt32& out_i3); // [tested]
186};
187
189template <typename Type>
190bool operator==(const plPlaneTemplate<Type>& lhs, const plPlaneTemplate<Type>& rhs); // [tested]
191
193template <typename Type>
194bool operator!=(const plPlaneTemplate<Type>& lhs, const plPlaneTemplate<Type>& rhs); // [tested]
195
196#include <Foundation/Math/Implementation/Plane_inl.h>
An axis-aligned bounding box implementation.
Definition BoundingBox.h:12
An implementation of a bounding sphere.
Definition BoundingSphere.h:11
A 3x3 component matrix class.
Definition Mat3.h:9
A 4x4 component matrix class.
Definition Mat4.h:11
A 3-component vector class.
Definition Vec3.h:9
A 4-component vector class.
Definition Vec4.h:9
A class that represents a mathematical plane.
Definition Plane.h:21
bool IsValid() const
Checks whether the plane has valid values (not NaN, normalized normal).
Definition Plane_inl.h:223
static plPlaneTemplate< Type > MakeInvalid()
Returns an invalid plane with a zero normal.
Definition Plane_inl.h:17
static plPlaneTemplate< Type > MakeFromNormalAndPoint(const plVec3Template< Type > &vNormal, const plVec3Template< Type > &vPointOnPlane)
Creates a plane from a normal and a point on the plane.
Definition Plane_inl.h:26
bool IsNaN() const
Checks whether any component is NaN.
Definition Plane_inl.h:229
bool GetRayIntersection(const plVec3Template< Type > &vRayStartPos, const plVec3Template< Type > &vRayDir, Type *out_pIntersectionDinstance=nullptr, plVec3Template< Type > *out_pIntersection=nullptr) const
Returns true, if the ray hit the plane. The intersection time describes at which multiple of the ray ...
Definition Plane_inl.h:368
const plVec3Template< Type > Mirror(const plVec3Template< Type > &vPoint) const
Returns the mirrored point. E.g. on the other side of the plane, at the same distance.
Definition Plane_inl.h:173
const plVec3Template< Type > GetCoplanarDirection(const plVec3Template< Type > &vDirection) const
Take the given direction vector and returns a modified one that is coplanar to the plane.
Definition Plane_inl.h:179
bool FlipIfNecessary(const plVec3Template< Type > &vPoint, bool bPlaneShouldFacePoint=true)
Negates Normal/Distance to switch which side of the plane is front and back. Returns true,...
Definition Plane_inl.h:211
plPositionOnPlane::Enum GetObjectPosition(const plVec3Template< Type > *const pPoints, plUInt32 uiVertices) const
Returns on which side of the plane the set of points lies. Might be on both sides.
Definition Plane_inl.h:305
plResult SetFromDirections(const plVec3Template< Type > &vTangent1, const plVec3Template< Type > &vTangent2, const plVec3Template< Type > &vPointOnPlane)
Creates a plane from two direction vectors that span the plane, and one point on it.
Definition Plane_inl.h:73
Type GetDistanceTo(const plVec3Template< Type > &vPoint) const
Returns the distance of the point to the plane.
Definition Plane_inl.h:141
Type GetMaximumDistanceTo(const plBoundingBoxTemplate< Type > &box) const
Returns the maximum distance between given box and a plane.
Definition AllClasses_inl.h:165
bool GetLineSegmentIntersection(const plVec3Template< Type > &vLineStartPos, const plVec3Template< Type > &vLineEndPos, Type *out_pHitFraction=nullptr, plVec3Template< Type > *out_pIntersection=nullptr) const
Returns true, if there is any intersection with the plane between the line's start and end position....
Definition Plane_inl.h:417
plResult SetFromPoints(const plVec3Template< Type > &v1, const plVec3Template< Type > &v2, const plVec3Template< Type > &v3)
Creates the plane-equation from three points on the plane.
Definition Plane_inl.h:53
static plResult FindSupportPoints(const plVec3Template< Type > *const pVertices, plInt32 iMaxVertices, plInt32 &out_i1, plInt32 &out_i2, plInt32 &out_i3)
Returns three points from an unreliable set of points, that reliably form a plane....
Definition Plane_inl.h:259
bool IsIdentical(const plPlaneTemplate< Type > &rhs) const
Checks whether this plane and the other are identical.
Definition Plane_inl.h:187
static plResult GetPlanesIntersectionPoint(const plPlaneTemplate< Type > &p0, const plPlaneTemplate< Type > &p1, const plPlaneTemplate< Type > &p2, plVec3Template< Type > &out_vResult)
Computes the one point where all three planes intersect. Returns PL_FAILURE if no such point exists.
Definition Plane_inl.h:478
static plPlaneTemplate< Type > MakeFromPoints(const plVec3Template< Type > &v1, const plVec3Template< Type > &v2, const plVec3Template< Type > &v3)
Creates a plane from three points.
Definition Plane_inl.h:37
void GetMinMaxDistanceTo(Type &out_fMin, Type &out_fMax, const plVec3Template< Type > *pPoints, plUInt32 uiNumPoints, plUInt32 uiStride=sizeof(plVec3Template< Type >)) const
Returns the minimum and maximum distance that any of the given points had to the plane.
Definition Plane_inl.h:452
plPlaneTemplate()
Default constructor. Does not initialize the plane.
Definition Plane_inl.h:6
void Flip()
Negates Normal/Distance to switch which side of the plane is front and back.
Definition Plane_inl.h:134
bool IsFinite() const
Checks whether any component is Infinity.
Definition Plane_inl.h:235
const plVec3Template< Type > ProjectOntoPlane(const plVec3Template< Type > &vPoint) const
Projects a point onto a plane (along the planes normal).
Definition Plane_inl.h:167
plPositionOnPlane::Enum GetPointPosition(const plVec3Template< Type > &vPoint) const
Returns on which side of the plane the point lies.
Definition Plane_inl.h:147
Type GetMinimumDistanceTo(const plVec3Template< Type > *pPoints, plUInt32 uiNumPoints, plUInt32 uiStride=sizeof(plVec3Template< Type >)) const
Returns the minimum distance that any of the given points had to the plane.
Definition Plane_inl.h:431
void Transform(const plMat3Template< Type > &m)
Transforms the plane with the given matrix.
Definition Plane_inl.h:84
bool IsEqual(const plPlaneTemplate< Type > &rhs, Type fEpsilon=plMath::DefaultEpsilon< Type >()) const
Checks whether this plane and the other are equal within some threshold.
Definition Plane_inl.h:193
bool GetRayIntersectionBiDirectional(const plVec3Template< Type > &vRayStartPos, const plVec3Template< Type > &vRayDir, Type *out_pIntersectionDistance=nullptr, plVec3Template< Type > *out_pIntersection=nullptr) const
Returns true, if the ray intersects the plane. Intersection time and point are stored in the out-para...
Definition Plane_inl.h:394
plVec4Template< Type > GetAsVec4() const
Returns an plVec4 with the plane normal in x,y,z and the negative distance in w.
Definition Plane_inl.h:47
Describes on which side of a plane a point or an object is located.
Definition Plane.h:8
Enum
Definition Plane.h:10
@ Spanning
Something is spanning a plane, i.e. some points are on the front and some on the back.
Definition Plane.h:14
@ Back
Something is completely on the back side of a plane.
Definition Plane.h:11
@ OnPlane
Something is lying completely on a plane (all points)
Definition Plane.h:13
@ Front
Something is completely in front of a plane.
Definition Plane.h:12
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54