Path: blob/master/thirdparty/jolt_physics/Jolt/Geometry/Plane.h
21760 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56JPH_NAMESPACE_BEGIN78/// An infinite plane described by the formula X . Normal + Constant = 0.9class [[nodiscard]] Plane10{11public:12JPH_OVERRIDE_NEW_DELETE1314/// Constructor15Plane() = default;16explicit Plane(Vec4Arg inNormalAndConstant) : mNormalAndConstant(inNormalAndConstant) { }17Plane(Vec3Arg inNormal, float inConstant) : mNormalAndConstant(inNormal, inConstant) { }1819/// Create from point and normal20static Plane sFromPointAndNormal(Vec3Arg inPoint, Vec3Arg inNormal) { return Plane(Vec4(inNormal, -inNormal.Dot(inPoint))); }2122/// Create from point and normal, double precision version that more accurately calculates the plane constant23static Plane sFromPointAndNormal(DVec3Arg inPoint, Vec3Arg inNormal) { return Plane(Vec4(inNormal, -float(DVec3(inNormal).Dot(inPoint)))); }2425/// Create from 3 counter clockwise points26static Plane sFromPointsCCW(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3) { return sFromPointAndNormal(inV1, (inV2 - inV1).Cross(inV3 - inV1).Normalized()); }2728// Properties29Vec3 GetNormal() const { return Vec3(mNormalAndConstant); }30void SetNormal(Vec3Arg inNormal) { mNormalAndConstant = Vec4(inNormal, mNormalAndConstant.GetW()); }31float GetConstant() const { return mNormalAndConstant.GetW(); }32void SetConstant(float inConstant) { mNormalAndConstant.SetW(inConstant); }3334/// Store as 4 floats35void StoreFloat4(Float4 *outV) const { mNormalAndConstant.StoreFloat4(outV); }3637/// Offset the plane (positive value means move it in the direction of the plane normal)38Plane Offset(float inDistance) const { return Plane(mNormalAndConstant - Vec4(Vec3::sZero(), inDistance)); }3940/// Transform the plane by a matrix41inline Plane GetTransformed(Mat44Arg inTransform) const42{43Vec3 transformed_normal = inTransform.Multiply3x3(GetNormal());44return Plane(transformed_normal, GetConstant() - inTransform.GetTranslation().Dot(transformed_normal));45}4647/// Scale the plane, can handle non-uniform and negative scaling48inline Plane Scaled(Vec3Arg inScale) const49{50Vec3 scaled_normal = GetNormal() / inScale;51float scaled_normal_length = scaled_normal.Length();52return Plane(scaled_normal / scaled_normal_length, GetConstant() / scaled_normal_length);53}5455/// Distance point to plane56float SignedDistance(Vec3Arg inPoint) const { return inPoint.Dot(GetNormal()) + GetConstant(); }5758/// Project inPoint onto the plane59Vec3 ProjectPointOnPlane(Vec3Arg inPoint) const { return inPoint - GetNormal() * SignedDistance(inPoint); }6061/// Returns intersection point between 3 planes62static bool sIntersectPlanes(const Plane &inP1, const Plane &inP2, const Plane &inP3, Vec3 &outPoint)63{64// We solve the equation:65// |ax, ay, az, aw| | x | | 0 |66// |bx, by, bz, bw| * | y | = | 0 |67// |cx, cy, cz, cw| | z | | 0 |68// | 0, 0, 0, 1| | 1 | | 1 |69// Where normal of plane 1 = (ax, ay, az), plane constant of 1 = aw, normal of plane 2 = (bx, by, bz) etc.70// This involves inverting the matrix and multiplying it with [0, 0, 0, 1]7172// Fetch the normals and plane constants for the three planes73Vec4 a = inP1.mNormalAndConstant;74Vec4 b = inP2.mNormalAndConstant;75Vec4 c = inP3.mNormalAndConstant;7677// Result is a vector that we have to divide by:78float denominator = Vec3(a).Dot(Vec3(b).Cross(Vec3(c)));79if (denominator == 0.0f)80return false;8182// The numerator is:83// [aw*(bz*cy-by*cz)+ay*(bw*cz-bz*cw)+az*(by*cw-bw*cy)]84// [aw*(bx*cz-bz*cx)+ax*(bz*cw-bw*cz)+az*(bw*cx-bx*cw)]85// [aw*(by*cx-bx*cy)+ax*(bw*cy-by*cw)+ay*(bx*cw-bw*cx)]86Vec4 numerator =87a.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>())88+ 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>())89+ 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>());9091outPoint = Vec3(numerator) / denominator;92return true;93}9495private:96#ifdef JPH_OBJECT_STREAM97friend void CreateRTTIPlane(class RTTI &); // For JPH_IMPLEMENT_SERIALIZABLE_OUTSIDE_CLASS98#endif99100Vec4 mNormalAndConstant; ///< XYZ = normal, W = constant, plane: x . normal + constant = 0101};102103JPH_NAMESPACE_END104105106