| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
JPH_NAMESPACE_BEGIN |
| 8 |
|
| 9 |
|
| 10 |
class [[nodiscard]] Plane |
| 11 |
{ |
| 12 |
public: |
| 13 |
JPH_OVERRIDE_NEW_DELETE |
| 14 |
|
| 15 |
|
| 16 |
Plane() = default; |
| 17 |
explicit Plane(Vec4Arg inNormalAndConstant) : mNormalAndConstant(inNormalAndConstant) { } |
| 18 |
Plane(Vec3Arg inNormal, float inConstant) : mNormalAndConstant(inNormal, inConstant) { } |
| 19 |
|
| 20 |
|
| 21 |
static Plane sFromPointAndNormal(Vec3Arg inPoint, Vec3Arg inNormal) { return Plane(Vec4(inNormal, -inNormal.Dot(inPoint))); } |
| 22 |
|
| 23 |
|
| 24 |
static Plane sFromPointAndNormal(DVec3Arg inPoint, Vec3Arg inNormal) { return Plane(Vec4(inNormal, -float(DVec3(inNormal).Dot(inPoint)))); } |
| 25 |
|
| 26 |
|
| 27 |
static Plane sFromPointsCCW(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3) { return sFromPointAndNormal(inV1, (inV2 - inV1).Cross(inV3 - inV1).Normalized()); } |
| 28 |
|
| 29 |
|
| 30 |
Vec3 GetNormal() const { return Vec3(mNormalAndConstant); } |
| 31 |
void SetNormal(Vec3Arg inNormal) { mNormalAndConstant = Vec4(inNormal, mNormalAndConstant.GetW()); } |
| 32 |
float GetConstant() const { return mNormalAndConstant.GetW(); } |
| 33 |
void SetConstant(float inConstant) { mNormalAndConstant.SetW(inConstant); } |
| 34 |
|
| 35 |
|
| 36 |
void StoreFloat4(Float4 *outV) const { mNormalAndConstant.StoreFloat4(outV); } |
| 37 |
|
| 38 |
|
| 39 |
Plane Offset(float inDistance) const { return Plane(mNormalAndConstant - Vec4(Vec3::sZero(), inDistance)); } |
| 40 |
|
| 41 |
|
| 42 |
inline Plane GetTransformed(Mat44Arg inTransform) const |
| 43 |
{ |
| 44 |
Vec3 transformed_normal = inTransform.Multiply3x3(GetNormal()); |
| 45 |
return Plane(transformed_normal, GetConstant() - inTransform.GetTranslation().Dot(transformed_normal)); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
inline Plane Scaled(Vec3Arg inScale) const |
| 50 |
{ |
| 51 |
Vec3 scaled_normal = GetNormal() / inScale; |
| 52 |
float scaled_normal_length = scaled_normal.Length(); |
| 53 |
return Plane(scaled_normal / scaled_normal_length, GetConstant() / scaled_normal_length); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
float SignedDistance(Vec3Arg inPoint) const { return inPoint.Dot(GetNormal()) + GetConstant(); } |
| 58 |
|
| 59 |
|
| 60 |
Vec3 ProjectPointOnPlane(Vec3Arg inPoint) const { return inPoint - GetNormal() * SignedDistance(inPoint); } |
| 61 |
|
| 62 |
|
| 63 |
static bool sIntersectPlanes(const Plane &inP1, const Plane &inP2, const Plane &inP3, Vec3 &outPoint) |
| 64 |
{ |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
Vec4 a = inP1.mNormalAndConstant; |
| 75 |
Vec4 b = inP2.mNormalAndConstant; |
| 76 |
Vec4 c = inP3.mNormalAndConstant; |
| 77 |
|
| 78 |
|
| 79 |
float denominator = Vec3(a).Dot(Vec3(b).Cross(Vec3(c))); |
| 80 |
if (denominator == 0.0f) |
| 81 |
return false; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
Vec4 numerator = |
| 88 |
a.SplatW() * (b.Swizzle<SWIZZLE_Z, SWIZZLE_X, SWIZZLE_Y, SWIZZLE_UNUSED>() * c.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X, SWIZZLE_UNUSED>() - b.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X, SWIZZLE_UNUSED>() * c.Swizzle<SWIZZLE_Z, SWIZZLE_X, SWIZZLE_Y, SWIZZLE_UNUSED>()) |
| 89 |
+ a.Swizzle<SWIZZLE_Y, SWIZZLE_X, SWIZZLE_X, SWIZZLE_UNUSED>() * (b.Swizzle<SWIZZLE_W, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_UNUSED>() * c.Swizzle<SWIZZLE_Z, SWIZZLE_W, SWIZZLE_Y, SWIZZLE_UNUSED>() - b.Swizzle<SWIZZLE_Z, SWIZZLE_W, SWIZZLE_Y, SWIZZLE_UNUSED>() * c.Swizzle<SWIZZLE_W, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_UNUSED>()) |
| 90 |
+ a.Swizzle<SWIZZLE_Z, SWIZZLE_Z, SWIZZLE_Y, SWIZZLE_UNUSED>() * (b.Swizzle<SWIZZLE_Y, SWIZZLE_W, SWIZZLE_X, SWIZZLE_UNUSED>() * c.Swizzle<SWIZZLE_W, SWIZZLE_X, SWIZZLE_W, SWIZZLE_UNUSED>() - b.Swizzle<SWIZZLE_W, SWIZZLE_X, SWIZZLE_W, SWIZZLE_UNUSED>() * c.Swizzle<SWIZZLE_Y, SWIZZLE_W, SWIZZLE_X, SWIZZLE_UNUSED>()); |
| 91 |
|
| 92 |
outPoint = Vec3(numerator) / denominator; |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
private: |
| 97 |
#ifdef JPH_OBJECT_STREAM |
| 98 |
friend void CreateRTTIPlane(class RTTI &); |
| 99 |
#endif |
| 100 |
|
| 101 |
Vec4 mNormalAndConstant; |
| 102 |
}; |
| 103 |
|
| 104 |
JPH_NAMESPACE_END |