Plane.h 4.3 KB

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