Plane.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. JPH_NAMESPACE_BEGIN
  5. /// An infinite plane described by the formula X . Normal + Constant = 0.
  6. class [[nodiscard]] Plane
  7. {
  8. public:
  9. JPH_OVERRIDE_NEW_DELETE
  10. /// Constructor
  11. Plane() = default;
  12. explicit Plane(Vec4Arg inNormalAndConstant) : mNormalAndConstant(inNormalAndConstant) { }
  13. Plane(Vec3Arg inNormal, float inConstant) : mNormalAndConstant(inNormal, inConstant) { }
  14. /// Create from point and normal
  15. static Plane sFromPointAndNormal(Vec3Arg inPoint, Vec3Arg inNormal) { return Plane(Vec4(inNormal, -inNormal.Dot(inPoint))); }
  16. /// Create from 3 counter clockwise points
  17. static Plane sFromPointsCCW(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3) { return sFromPointAndNormal(inV1, (inV2 - inV1).Cross(inV3 - inV1).Normalized()); }
  18. // Properties
  19. Vec3 GetNormal() const { return Vec3(mNormalAndConstant); }
  20. void SetNormal(Vec3Arg inNormal) { mNormalAndConstant = Vec4(inNormal, mNormalAndConstant.GetW()); }
  21. float GetConstant() const { return mNormalAndConstant.GetW(); }
  22. void SetConstant(float inConstant) { mNormalAndConstant.SetW(inConstant); }
  23. /// Offset the plane (positive value means move it in the direction of the plane normal)
  24. Plane Offset(float inDistance) const { return Plane(mNormalAndConstant - Vec4(Vec3::sZero(), inDistance)); }
  25. /// Distance point to plane
  26. float SignedDistance(Vec3Arg inPoint) const { return inPoint.Dot(GetNormal()) + GetConstant(); }
  27. /// Returns intersection point between 3 planes
  28. static bool sIntersectPlanes(const Plane &inP1, const Plane &inP2, const Plane &inP3, Vec3 &outPoint)
  29. {
  30. // We solve the equation:
  31. // |ax, ay, az, aw| | x | | 0 |
  32. // |bx, by, bz, bw| * | y | = | 0 |
  33. // |cx, cy, cz, cw| | z | | 0 |
  34. // | 0, 0, 0, 1| | 1 | | 1 |
  35. // Where normal of plane 1 = (ax, ay, az), plane constant of 1 = aw, normal of plane 2 = (bx, by, bz) etc.
  36. // This involves inverting the matrix and multiplying it with [0, 0, 0, 1]
  37. // Fetch the normals and plane constants for the three planes
  38. Vec4 a = inP1.mNormalAndConstant;
  39. Vec4 b = inP2.mNormalAndConstant;
  40. Vec4 c = inP3.mNormalAndConstant;
  41. // Result is a vector that we have to divide by:
  42. float denominator = Vec3(a).Dot(Vec3(b).Cross(Vec3(c)));
  43. if (denominator == 0.0f)
  44. return false;
  45. // The numerator is:
  46. // [aw*(bz*cy-by*cz)+ay*(bw*cz-bz*cw)+az*(by*cw-bw*cy)]
  47. // [aw*(bx*cz-bz*cx)+ax*(bz*cw-bw*cz)+az*(bw*cx-bx*cw)]
  48. // [aw*(by*cx-bx*cy)+ax*(bw*cy-by*cw)+ay*(bx*cw-bw*cx)]
  49. Vec4 numerator =
  50. 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>())
  51. + 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>())
  52. + 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>());
  53. outPoint = Vec3(numerator) / denominator;
  54. return true;
  55. }
  56. private:
  57. Vec4 mNormalAndConstant; ///< XYZ = normal, W = constant, plane: x . normal + constant = 0
  58. };
  59. JPH_NAMESPACE_END