Plane.h 3.6 KB

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