3#include <Foundation/Math/Mat4.h>
5template <
typename Type>
8#if PL_ENABLED(PL_MATH_CHECK_FOR_NAN)
11 m_vNormal.Set(TypeNaN);
12 m_fNegDistance = TypeNaN;
16template <
typename Type>
21 res.m_fNegDistance = 0;
25template <
typename Type>
28 PL_ASSERT_DEV(vNormal.
IsNormalized(),
"Normal must be normalized.");
31 res.m_vNormal = vNormal;
32 res.m_fNegDistance = -vNormal.
Dot(vPointOnPlane);
36template <
typename Type>
40 PL_VERIFY(res.m_vNormal.CalculateNormal(v1, v2, v3).Succeeded(),
"The 3 provided points do not form a plane");
42 res.m_fNegDistance = -res.m_vNormal.Dot(v1);
46template <
typename Type>
49 return plVec4(m_vNormal.x, m_vNormal.y, m_vNormal.z, m_fNegDistance);
52template <
typename Type>
55 if (m_vNormal.CalculateNormal(v1, v2, v3) == PL_FAILURE)
58 m_fNegDistance = -m_vNormal.Dot(v1);
62template <
typename Type>
65 if (m_vNormal.CalculateNormal(pVertices[0], pVertices[1], pVertices[2]) == PL_FAILURE)
68 m_fNegDistance = -m_vNormal.Dot(pVertices[0]);
72template <
typename Type>
79 m_fNegDistance = -vNormal.
Dot(vPointOnPlane);
83template <
typename Type>
93 PL_ASSERT_DEBUG(normalizeSucceeded,
"");
94 PL_IGNORE_UNUSED(normalizeSucceeded);
98 if (!plMath::IsFinite(m_fNegDistance))
100 m_vNormal = vTransformedNormal;
108template <
typename Type>
118 PL_ASSERT_DEBUG(normalizeSucceeded,
"");
119 PL_IGNORE_UNUSED(normalizeSucceeded);
123 if (!plMath::IsFinite(m_fNegDistance))
125 m_vNormal = vTransformedNormal;
133template <
typename Type>
136 m_fNegDistance = -m_fNegDistance;
137 m_vNormal = -m_vNormal;
140template <
typename Type>
143 return (m_vNormal.Dot(vPoint) + m_fNegDistance);
146template <
typename Type>
152template <
typename Type>
155 const Type f = m_vNormal.Dot(vPoint);
157 if (f + fPlaneHalfWidth < -m_fNegDistance)
160 if (f - fPlaneHalfWidth > -m_fNegDistance)
166template <
typename Type>
169 return vPoint - m_vNormal * (m_vNormal.Dot(vPoint) + m_fNegDistance);
172template <
typename Type>
175 return vPoint - (Type)2 * GetDistanceTo(vPoint) * m_vNormal;
178template <
typename Type>
186template <
typename Type>
189 return m_vNormal.
IsIdentical(rhs.m_vNormal) && m_fNegDistance == rhs.m_fNegDistance;
192template <
typename Type>
195 return m_vNormal.IsEqual(rhs.m_vNormal, fEpsilon) &&
plMath::IsEqual(m_fNegDistance, rhs.m_fNegDistance, fEpsilon);
198template <
typename Type>
204template <
typename Type>
210template <
typename Type>
222template <
typename Type>
225 return !IsNaN() && m_vNormal.IsNormalized(plMath::DefaultEpsilon<Type>());
228template <
typename Type>
231 return plMath::IsNaN(m_fNegDistance) || m_vNormal.IsNaN();
234template <
typename Type>
237 return m_vNormal.IsValid() && plMath::IsFinite(m_fNegDistance);
243template <
typename Type>
248 if (FindSupportPoints(pVertices, uiMaxVertices, iPoints[0], iPoints[1], iPoints[2]) == PL_FAILURE)
254 SetFromPoints(pVertices[iPoints[0]], pVertices[iPoints[1]], pVertices[iPoints[2]]).IgnoreResult();
258template <
typename Type>
263 bool bFoundSecond =
false;
266 while (i < iMaxVertices)
268 if (pVertices[i].IsEqual(v1, 0.001f) ==
false)
289 while (i < iMaxVertices)
292 if ((pVertices[i].IsEqual(v2, 0.001f) ==
false) && (
plMath::Abs((pVertices[i] - v2).GetNormalized().Dot(vDir1)) < (Type)0.999))
304template <
typename Type>
310 for (plUInt32 i = 0; i < uiVertices; ++i)
312 switch (GetPointPosition(pPoints[i]))
333template <
typename Type>
339 for (plUInt32 i = 0; i < uiVertices; ++i)
341 switch (GetPointPosition(pPoints[i], fPlaneHalfWidth))
367template <
typename Type>
370 PL_ASSERT_DEBUG(vRayStartPos.
IsValid(),
"Ray start position must be valid.");
371 PL_ASSERT_DEBUG(vRayDir.
IsValid(),
"Ray direction must be valid.");
373 const Type fPlaneSide = GetDistanceTo(vRayStartPos);
374 const Type fCosAlpha = m_vNormal.Dot(vRayDir);
382 const Type fTime = -fPlaneSide / fCosAlpha;
384 if (out_pIntersectionDistance)
385 *out_pIntersectionDistance = fTime;
387 if (out_pIntersection)
388 *out_pIntersection = vRayStartPos + fTime * vRayDir;
393template <
typename Type>
396 PL_ASSERT_DEBUG(vRayStartPos.
IsValid(),
"Ray start position must be valid.");
397 PL_ASSERT_DEBUG(vRayDir.
IsValid(),
"Ray direction must be valid.");
399 const Type fPlaneSide = GetDistanceTo(vRayStartPos);
400 const Type fCosAlpha = m_vNormal.Dot(vRayDir);
405 const Type fTime = -fPlaneSide / fCosAlpha;
407 if (out_pIntersectionDistance)
408 *out_pIntersectionDistance = fTime;
410 if (out_pIntersection)
411 *out_pIntersection = vRayStartPos + fTime * vRayDir;
416template <
typename Type>
421 if (!GetRayIntersection(vLineStartPos, vLineEndPos - vLineStartPos, &fTime, out_pIntersection))
424 if (out_pHitFraction)
425 *out_pHitFraction = fTime;
430template <
typename Type>
433 PL_ASSERT_DEBUG(pPoints !=
nullptr,
"Array may not be nullptr.");
434 PL_ASSERT_DEBUG(uiStride >=
sizeof(
plVec3Template<Type>),
"Stride must be at least sizeof(plVec3Template) to not have overlapping data.");
435 PL_ASSERT_DEBUG(uiNumPoints >= 1,
"Array must contain at least one point.");
441 for (plUInt32 i = 0; i < uiNumPoints; ++i)
443 fMinDist =
plMath::Min(m_vNormal.Dot(*pCurPoint), fMinDist);
448 return fMinDist + m_fNegDistance;
451template <
typename Type>
454 PL_ASSERT_DEBUG(pPoints !=
nullptr,
"Array may not be nullptr.");
455 PL_ASSERT_DEBUG(uiStride >=
sizeof(
plVec3Template<Type>),
"Stride must be at least sizeof(plVec3Template) to not have overlapping data.");
456 PL_ASSERT_DEBUG(uiNumPoints >= 1,
"Array must contain at least one point.");
463 for (plUInt32 i = 0; i < uiNumPoints; ++i)
465 const Type f = m_vNormal.
Dot(*pCurPoint);
473 out_fMin += m_fNegDistance;
474 out_fMax += m_fNegDistance;
477template <
typename Type>
489 out_vResult = (-p0.m_fNegDistance * n2.
CrossRH(n3) + -p1.m_fNegDistance * n3.
CrossRH(n1) + -p2.m_fNegDistance * n1.
CrossRH(n2)) / det;
494#include <Foundation/Math/Implementation/AllClasses_inl.h>
A 3x3 component matrix class.
Definition Mat3.h:9
const plVec3Template< Type > TransformDirection(const plVec3Template< Type > &v) const
Matrix-vector multiplication, assuming the 4th component of the vector is zero. So,...
Definition Mat3_inl.h:244
A 4x4 component matrix class.
Definition Mat4.h:11
const plVec3Template< Type > TransformDirection(const plVec3Template< Type > &v) const
Matrix-vector multiplication, assuming the 4th component of the vector is zero. So,...
Definition Mat4_inl.h:374
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
const plVec3Template< Type > CrossRH(const plVec3Template< Type > &rhs) const
Returns the Cross-product of the two vectors (NOT commutative, order DOES matter)
Definition Vec3_inl.h:299
bool IsIdentical(const plVec3Template< Type > &rhs) const
Equality Check (bitwise)
Definition Vec3_inl.h:422
bool IsValid() const
Checks that all components are finite numbers.
Definition Vec3_inl.h:157
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
PL_DECLARE_IF_FLOAT_TYPE plResult NormalizeIfNotZero(const plVec3Template< Type > &vFallback=plVec3Template< Type >(1, 0, 0), Type fEpsilon=plMath::SmallEpsilon< Type >())
Tries to normalize this vector. If the vector is too close to zero, PL_FAILURE is returned and the ve...
Definition Vec3_inl.h:101
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
PL_DECLARE_IF_FLOAT_TYPE void MakeOrthogonalTo(const plVec3Template< Type > &vNormal)
Modifies this direction vector to be orthogonal to the given (normalized) direction vector....
Definition Vec3_inl.h:260
A 4-component vector class.
Definition Vec4.h:9
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
constexpr PL_ALWAYS_INLINE T Sign(T f)
Returns the sign of f (i.e: -1, 1 or 0)
Definition Math_inl.h:14
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 Abs(T f)
Returns the absolute value of f.
Definition Math_inl.h:21
constexpr PL_ALWAYS_INLINE T Max(T f1, T f2)
Returns the greater value, f1 or f2.
Definition Math_inl.h:39
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
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
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
PL_ALWAYS_INLINE void IgnoreResult()
Used to silence compiler warnings, when success or failure doesn't matter.
Definition Types.h:69