Path: blob/master/thirdparty/jolt_physics/Jolt/Math/DMat44.h
9913 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2022 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Math/MathTypes.h>78JPH_NAMESPACE_BEGIN910/// Holds a 4x4 matrix of floats with the last column consisting of doubles11class [[nodiscard]] alignas(JPH_DVECTOR_ALIGNMENT) DMat4412{13public:14JPH_OVERRIDE_NEW_DELETE1516// Underlying column type17using Type = Vec4::Type;18using DType = DVec3::Type;19using DTypeArg = DVec3::TypeArg;2021// Argument type22using ArgType = DMat44Arg;2324/// Constructor25DMat44() = default; ///< Intentionally not initialized for performance reasons26JPH_INLINE DMat44(Vec4Arg inC1, Vec4Arg inC2, Vec4Arg inC3, DVec3Arg inC4);27DMat44(const DMat44 &inM2) = default;28DMat44 & operator = (const DMat44 &inM2) = default;29JPH_INLINE explicit DMat44(Mat44Arg inM);30JPH_INLINE DMat44(Mat44Arg inRot, DVec3Arg inT);31JPH_INLINE DMat44(Type inC1, Type inC2, Type inC3, DTypeArg inC4);3233/// Zero matrix34static JPH_INLINE DMat44 sZero();3536/// Identity matrix37static JPH_INLINE DMat44 sIdentity();3839/// Rotate from quaternion40static JPH_INLINE DMat44 sRotation(QuatArg inQuat) { return DMat44(Mat44::sRotation(inQuat), DVec3::sZero()); }4142/// Get matrix that translates43static JPH_INLINE DMat44 sTranslation(DVec3Arg inV) { return DMat44(Vec4(1, 0, 0, 0), Vec4(0, 1, 0, 0), Vec4(0, 0, 1, 0), inV); }4445/// Get matrix that rotates and translates46static JPH_INLINE DMat44 sRotationTranslation(QuatArg inR, DVec3Arg inT) { return DMat44(Mat44::sRotation(inR), inT); }4748/// Get inverse matrix of sRotationTranslation49static JPH_INLINE DMat44 sInverseRotationTranslation(QuatArg inR, DVec3Arg inT);5051/// Get matrix that scales (produces a matrix with (inV, 1) on its diagonal)52static JPH_INLINE DMat44 sScale(Vec3Arg inV) { return DMat44(Mat44::sScale(inV), DVec3::sZero()); }5354/// Convert to Mat44 rounding to nearest55JPH_INLINE Mat44 ToMat44() const { return Mat44(mCol[0], mCol[1], mCol[2], Vec3(mCol3)); }5657/// Comparison58JPH_INLINE bool operator == (DMat44Arg inM2) const;59JPH_INLINE bool operator != (DMat44Arg inM2) const { return !(*this == inM2); }6061/// Test if two matrices are close62JPH_INLINE bool IsClose(DMat44Arg inM2, float inMaxDistSq = 1.0e-12f) const;6364/// Multiply matrix by matrix65JPH_INLINE DMat44 operator * (Mat44Arg inM) const;6667/// Multiply matrix by matrix68JPH_INLINE DMat44 operator * (DMat44Arg inM) const;6970/// Multiply vector by matrix71JPH_INLINE DVec3 operator * (Vec3Arg inV) const;7273/// Multiply vector by matrix74JPH_INLINE DVec3 operator * (DVec3Arg inV) const;7576/// Multiply vector by only 3x3 part of the matrix77JPH_INLINE Vec3 Multiply3x3(Vec3Arg inV) const { return GetRotation().Multiply3x3(inV); }7879/// Multiply vector by only 3x3 part of the matrix80JPH_INLINE DVec3 Multiply3x3(DVec3Arg inV) const;8182/// Multiply vector by only 3x3 part of the transpose of the matrix (\f$result = this^T \: inV\f$)83JPH_INLINE Vec3 Multiply3x3Transposed(Vec3Arg inV) const { return GetRotation().Multiply3x3Transposed(inV); }8485/// Scale a matrix: result = this * Mat44::sScale(inScale)86JPH_INLINE DMat44 PreScaled(Vec3Arg inScale) const;8788/// Scale a matrix: result = Mat44::sScale(inScale) * this89JPH_INLINE DMat44 PostScaled(Vec3Arg inScale) const;9091/// Pre multiply by translation matrix: result = this * Mat44::sTranslation(inTranslation)92JPH_INLINE DMat44 PreTranslated(Vec3Arg inTranslation) const;9394/// Pre multiply by translation matrix: result = this * Mat44::sTranslation(inTranslation)95JPH_INLINE DMat44 PreTranslated(DVec3Arg inTranslation) const;9697/// Post multiply by translation matrix: result = Mat44::sTranslation(inTranslation) * this (i.e. add inTranslation to the 4-th column)98JPH_INLINE DMat44 PostTranslated(Vec3Arg inTranslation) const;99100/// Post multiply by translation matrix: result = Mat44::sTranslation(inTranslation) * this (i.e. add inTranslation to the 4-th column)101JPH_INLINE DMat44 PostTranslated(DVec3Arg inTranslation) const;102103/// Access to the columns104JPH_INLINE Vec3 GetAxisX() const { return Vec3(mCol[0]); }105JPH_INLINE void SetAxisX(Vec3Arg inV) { mCol[0] = Vec4(inV, 0.0f); }106JPH_INLINE Vec3 GetAxisY() const { return Vec3(mCol[1]); }107JPH_INLINE void SetAxisY(Vec3Arg inV) { mCol[1] = Vec4(inV, 0.0f); }108JPH_INLINE Vec3 GetAxisZ() const { return Vec3(mCol[2]); }109JPH_INLINE void SetAxisZ(Vec3Arg inV) { mCol[2] = Vec4(inV, 0.0f); }110JPH_INLINE DVec3 GetTranslation() const { return mCol3; }111JPH_INLINE void SetTranslation(DVec3Arg inV) { mCol3 = inV; }112JPH_INLINE Vec3 GetColumn3(uint inCol) const { JPH_ASSERT(inCol < 3); return Vec3(mCol[inCol]); }113JPH_INLINE void SetColumn3(uint inCol, Vec3Arg inV) { JPH_ASSERT(inCol < 3); mCol[inCol] = Vec4(inV, 0.0f); }114JPH_INLINE Vec4 GetColumn4(uint inCol) const { JPH_ASSERT(inCol < 3); return mCol[inCol]; }115JPH_INLINE void SetColumn4(uint inCol, Vec4Arg inV) { JPH_ASSERT(inCol < 3); mCol[inCol] = inV; }116117/// Transpose 3x3 subpart of matrix118JPH_INLINE Mat44 Transposed3x3() const { return GetRotation().Transposed3x3(); }119120/// Inverse 4x4 matrix121JPH_INLINE DMat44 Inversed() const;122123/// Inverse 4x4 matrix when it only contains rotation and translation124JPH_INLINE DMat44 InversedRotationTranslation() const;125126/// Get rotation part only (note: retains the first 3 values from the bottom row)127JPH_INLINE Mat44 GetRotation() const { return Mat44(mCol[0], mCol[1], mCol[2], Vec4(0, 0, 0, 1)); }128129/// Updates the rotation part of this matrix (the first 3 columns)130JPH_INLINE void SetRotation(Mat44Arg inRotation);131132/// Convert to quaternion133JPH_INLINE Quat GetQuaternion() const { return GetRotation().GetQuaternion(); }134135/// Get matrix that transforms a direction with the same transform as this matrix (length is not preserved)136JPH_INLINE Mat44 GetDirectionPreservingMatrix() const { return GetRotation().Inversed3x3().Transposed3x3(); }137138/// Works identical to Mat44::Decompose139JPH_INLINE DMat44 Decompose(Vec3 &outScale) const { return DMat44(GetRotation().Decompose(outScale), mCol3); }140141/// To String142friend ostream & operator << (ostream &inStream, DMat44Arg inM)143{144inStream << inM.mCol[0] << ", " << inM.mCol[1] << ", " << inM.mCol[2] << ", " << inM.mCol3;145return inStream;146}147148private:149Vec4 mCol[3]; ///< Rotation columns150DVec3 mCol3; ///< Translation column, 4th element is assumed to be 1151};152153static_assert(std::is_trivial<DMat44>(), "Is supposed to be a trivial type!");154155JPH_NAMESPACE_END156157#include "DMat44.inl"158159160