Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Plane.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
10class [[nodiscard]] Plane
11{
12public:
14
16 Plane() = default;
17 explicit Plane(Vec4Arg inNormalAndConstant) : mNormalAndConstant(inNormalAndConstant) { }
18 Plane(Vec3Arg inNormal, float inConstant) : mNormalAndConstant(inNormal, inConstant) { }
19
21 static Plane sFromPointAndNormal(Vec3Arg inPoint, Vec3Arg inNormal) { return Plane(Vec4(inNormal, -inNormal.Dot(inPoint))); }
22
24 static Plane sFromPointAndNormal(DVec3Arg inPoint, Vec3Arg inNormal) { return Plane(Vec4(inNormal, -float(DVec3(inNormal).Dot(inPoint)))); }
25
27 static Plane sFromPointsCCW(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3) { return sFromPointAndNormal(inV1, (inV2 - inV1).Cross(inV3 - inV1).Normalized()); }
28
29 // Properties
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
36 Plane Offset(float inDistance) const { return Plane(mNormalAndConstant - Vec4(Vec3::sZero(), inDistance)); }
37
39 inline Plane GetTransformed(Mat44Arg inTransform) const
40 {
41 Vec3 transformed_normal = inTransform.Multiply3x3(GetNormal());
42 return Plane(transformed_normal, GetConstant() - inTransform.GetTranslation().Dot(transformed_normal));
43 }
44
46 float SignedDistance(Vec3Arg inPoint) const { return inPoint.Dot(GetNormal()) + GetConstant(); }
47
49 static bool sIntersectPlanes(const Plane &inP1, const Plane &inP2, const Plane &inP3, Vec3 &outPoint)
50 {
51 // We solve the equation:
52 // |ax, ay, az, aw| | x | | 0 |
53 // |bx, by, bz, bw| * | y | = | 0 |
54 // |cx, cy, cz, cw| | z | | 0 |
55 // | 0, 0, 0, 1| | 1 | | 1 |
56 // Where normal of plane 1 = (ax, ay, az), plane constant of 1 = aw, normal of plane 2 = (bx, by, bz) etc.
57 // This involves inverting the matrix and multiplying it with [0, 0, 0, 1]
58
59 // Fetch the normals and plane constants for the three planes
60 Vec4 a = inP1.mNormalAndConstant;
61 Vec4 b = inP2.mNormalAndConstant;
62 Vec4 c = inP3.mNormalAndConstant;
63
64 // Result is a vector that we have to divide by:
65 float denominator = Vec3(a).Dot(Vec3(b).Cross(Vec3(c)));
66 if (denominator == 0.0f)
67 return false;
68
69 // The numerator is:
70 // [aw*(bz*cy-by*cz)+ay*(bw*cz-bz*cw)+az*(by*cw-bw*cy)]
71 // [aw*(bx*cz-bz*cx)+ax*(bz*cw-bw*cz)+az*(bw*cx-bx*cw)]
72 // [aw*(by*cx-bx*cy)+ax*(bw*cy-by*cw)+ay*(bx*cw-bw*cx)]
73 Vec4 numerator =
77
78 outPoint = Vec3(numerator) / denominator;
79 return true;
80 }
81
82private:
83 Vec4 mNormalAndConstant;
84};
85
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
@ SWIZZLE_Z
Use the Z component.
Definition: Swizzle.h:14
@ SWIZZLE_W
Use the W component.
Definition: Swizzle.h:15
@ SWIZZLE_X
Use the X component.
Definition: Swizzle.h:12
@ SWIZZLE_UNUSED
We always use the Z component when we don't specifically want to initialize a value,...
Definition: Swizzle.h:16
@ SWIZZLE_Y
Use the Y component.
Definition: Swizzle.h:13
Definition: DVec3.h:14
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
JPH_INLINE Vec3 Multiply3x3(Vec3Arg inV) const
Multiply vector by only 3x3 part of the matrix.
Definition: Mat44.inl:316
JPH_INLINE Vec3 GetTranslation() const
Definition: Mat44.h:152
An infinite plane described by the formula X . Normal + Constant = 0.
Definition: Plane.h:11
Plane(Vec3Arg inNormal, float inConstant)
Definition: Plane.h:18
Plane GetTransformed(Mat44Arg inTransform) const
Transform the plane by a matrix.
Definition: Plane.h:39
void SetConstant(float inConstant)
Definition: Plane.h:33
float GetConstant() const
Definition: Plane.h:32
static Plane sFromPointAndNormal(DVec3Arg inPoint, Vec3Arg inNormal)
Create from point and normal, double precision version that more accurately calculates the plane cons...
Definition: Plane.h:24
Vec3 GetNormal() const
Definition: Plane.h:30
Plane(Vec4Arg inNormalAndConstant)
Definition: Plane.h:17
float SignedDistance(Vec3Arg inPoint) const
Distance point to plane.
Definition: Plane.h:46
void SetNormal(Vec3Arg inNormal)
Definition: Plane.h:31
static Plane sFromPointAndNormal(Vec3Arg inPoint, Vec3Arg inNormal)
Create from point and normal.
Definition: Plane.h:21
static bool sIntersectPlanes(const Plane &inP1, const Plane &inP2, const Plane &inP3, Vec3 &outPoint)
Returns intersection point between 3 planes.
Definition: Plane.h:49
JPH_OVERRIDE_NEW_DELETE Plane()=default
Constructor.
static Plane sFromPointsCCW(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3)
Create from 3 counter clockwise points.
Definition: Plane.h:27
Plane Offset(float inDistance) const
Offset the plane (positive value means move it in the direction of the plane normal)
Definition: Plane.h:36
Definition: Vec3.h:16
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition: Vec3.inl:637
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107
Definition: Vec4.h:14
JPH_INLINE Vec4 SplatW() const
Replicate the W component to all components.
Definition: Vec4.inl:580
JPH_INLINE Vec4 Swizzle() const
Swizzle the elements in inV.