Plane.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 point and normal, double precision version that more accurately calculates the plane constant
  17. static Plane sFromPointAndNormal(DVec3Arg inPoint, Vec3Arg inNormal) { return Plane(Vec4(inNormal, -float(DVec3(inNormal).Dot(inPoint)))); }
  18. /// Create from 3 counter clockwise points
  19. static Plane sFromPointsCCW(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3) { return sFromPointAndNormal(inV1, (inV2 - inV1).Cross(inV3 - inV1).Normalized()); }
  20. // Properties
  21. Vec3 GetNormal() const { return Vec3(mNormalAndConstant); }
  22. void SetNormal(Vec3Arg inNormal) { mNormalAndConstant = Vec4(inNormal, mNormalAndConstant.GetW()); }
  23. float GetConstant() const { return mNormalAndConstant.GetW(); }
  24. void SetConstant(float inConstant) { mNormalAndConstant.SetW(inConstant); }
  25. /// Offset the plane (positive value means move it in the direction of the plane normal)
  26. Plane Offset(float inDistance) const { return Plane(mNormalAndConstant - Vec4(Vec3::sZero(), inDistance)); }
  27. /// Distance point to plane
  28. float SignedDistance(Vec3Arg inPoint) const { return inPoint.Dot(GetNormal()) + GetConstant(); }
  29. /// Returns intersection point between 3 planes
  30. static bool sIntersectPlanes(const Plane &inP1, const Plane &inP2, const Plane &inP3, Vec3 &outPoint)
  31. {
  32. // We solve the equation:
  33. // |ax, ay, az, aw| | x | | 0 |
  34. // |bx, by, bz, bw| * | y | = | 0 |
  35. // |cx, cy, cz, cw| | z | | 0 |
  36. // | 0, 0, 0, 1| | 1 | | 1 |
  37. // Where normal of plane 1 = (ax, ay, az), plane constant of 1 = aw, normal of plane 2 = (bx, by, bz) etc.
  38. // This involves inverting the matrix and multiplying it with [0, 0, 0, 1]
  39. // Fetch the normals and plane constants for the three planes
  40. Vec4 a = inP1.mNormalAndConstant;
  41. Vec4 b = inP2.mNormalAndConstant;
  42. Vec4 c = inP3.mNormalAndConstant;
  43. // Result is a vector that we have to divide by:
  44. float denominator = Vec3(a).Dot(Vec3(b).Cross(Vec3(c)));
  45. if (denominator == 0.0f)
  46. return false;
  47. // The numerator is:
  48. // [aw*(bz*cy-by*cz)+ay*(bw*cz-bz*cw)+az*(by*cw-bw*cy)]
  49. // [aw*(bx*cz-bz*cx)+ax*(bz*cw-bw*cz)+az*(bw*cx-bx*cw)]
  50. // [aw*(by*cx-bx*cy)+ax*(bw*cy-by*cw)+ay*(bx*cw-bw*cx)]
  51. Vec4 numerator =
  52. 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>())
  53. + 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>())
  54. + 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>());
  55. outPoint = Vec3(numerator) / denominator;
  56. return true;
  57. }
  58. private:
  59. Vec4 mNormalAndConstant; ///< XYZ = normal, W = constant, plane: x . normal + constant = 0
  60. };
  61. JPH_NAMESPACE_END