Mat44.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Math/MathTypes.h>
  5. JPH_NAMESPACE_BEGIN
  6. /// Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
  7. class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) Mat44
  8. {
  9. public:
  10. JPH_OVERRIDE_NEW_DELETE
  11. // Underlying column type
  12. using Type = Vec4::Type;
  13. // Argument type
  14. using ArgType = Mat44Arg;
  15. /// Constructor
  16. Mat44() = default; ///< Intentionally not initialized for performance reasons
  17. JPH_INLINE Mat44(Vec4Arg inC1, Vec4Arg inC2, Vec4Arg inC3, Vec4Arg inC4);
  18. JPH_INLINE Mat44(Vec4Arg inC1, Vec4Arg inC2, Vec4Arg inC3, Vec3Arg inC4);
  19. Mat44(const Mat44 &inM2) = default;
  20. JPH_INLINE Mat44(Type inC1, Type inC2, Type inC3, Type inC4);
  21. /// Zero matrix
  22. static JPH_INLINE Mat44 sZero();
  23. /// Identity matrix
  24. static JPH_INLINE Mat44 sIdentity();
  25. /// Matrix filled with NaN's
  26. static JPH_INLINE Mat44 sNaN();
  27. /// Load 16 floats from memory
  28. static JPH_INLINE Mat44 sLoadFloat4x4(const Float4 *inV);
  29. /// Load 16 floats from memory, 16 bytes aligned
  30. static JPH_INLINE Mat44 sLoadFloat4x4Aligned(const Float4 *inV);
  31. /// Rotate around X, Y or Z axis (angle in radians)
  32. static JPH_INLINE Mat44 sRotationX(float inX);
  33. static JPH_INLINE Mat44 sRotationY(float inY);
  34. static JPH_INLINE Mat44 sRotationZ(float inZ);
  35. /// Rotate around arbitrary axis
  36. static JPH_INLINE Mat44 sRotation(Vec3Arg inAxis, float inAngle);
  37. /// Rotate from quaternion
  38. static JPH_INLINE Mat44 sRotation(QuatArg inQuat);
  39. /// Get matrix that translates
  40. static JPH_INLINE Mat44 sTranslation(Vec3Arg inV);
  41. /// Get matrix that rotates and translates
  42. static JPH_INLINE Mat44 sRotationTranslation(QuatArg inR, Vec3Arg inT);
  43. /// Get inverse matrix of sRotationTranslation
  44. static JPH_INLINE Mat44 sInverseRotationTranslation(QuatArg inR, Vec3Arg inT);
  45. /// Get matrix that scales uniformly
  46. static JPH_INLINE Mat44 sScale(float inScale);
  47. /// Get matrix that scales (produces a matrix with (inV, 1) on its diagonal)
  48. static JPH_INLINE Mat44 sScale(Vec3Arg inV);
  49. /// Get outer product of inV and inV2 (equivalent to \f$inV1 \otimes inV2\f$)
  50. static JPH_INLINE Mat44 sOuterProduct(Vec3Arg inV1, Vec3Arg inV2);
  51. /// Get matrix that represents a cross product \f$A \times B = \text{sCrossProduct}(A) \: B\f$
  52. static JPH_INLINE Mat44 sCrossProduct(Vec3Arg inV);
  53. /// Returns matrix ML so that \f$ML(q) \: p = q \: p\f$ (where p and q are quaternions)
  54. static JPH_INLINE Mat44 sQuatLeftMultiply(QuatArg inQ);
  55. /// Returns matrix MR so that \f$MR(q) \: p = p \: q\f$ (where p and q are quaternions)
  56. static JPH_INLINE Mat44 sQuatRightMultiply(QuatArg inQ);
  57. /// Returns a look at matrix that transforms from world space to view space
  58. /// @param inPos Position of the camera
  59. /// @param inTarget Target of the camera
  60. /// @param inUp Up vector
  61. static JPH_INLINE Mat44 sLookAt(Vec3Arg inPos, Vec3Arg inTarget, Vec3Arg inUp);
  62. /// Get float component by element index
  63. JPH_INLINE float operator () (uint inRow, uint inColumn) const { JPH_ASSERT(inRow < 4); JPH_ASSERT(inColumn < 4); return mCol[inColumn].mF32[inRow]; }
  64. JPH_INLINE float & operator () (uint inRow, uint inColumn) { JPH_ASSERT(inRow < 4); JPH_ASSERT(inColumn < 4); return mCol[inColumn].mF32[inRow]; }
  65. /// Comparsion
  66. JPH_INLINE bool operator == (Mat44Arg inM2) const;
  67. JPH_INLINE bool operator != (Mat44Arg inM2) const { return !(*this == inM2); }
  68. /// Test if two matrices are close
  69. JPH_INLINE bool IsClose(Mat44Arg inM2, float inMaxDistSq = 1.0e-12f) const;
  70. /// Multiply matrix by matrix
  71. JPH_INLINE Mat44 operator * (Mat44Arg inM) const;
  72. /// Multiply vector by matrix
  73. JPH_INLINE Vec3 operator * (Vec3Arg inV) const;
  74. JPH_INLINE Vec4 operator * (Vec4Arg inV) const;
  75. /// Multiply vector by only 3x3 part of the matrix
  76. JPH_INLINE Vec3 Multiply3x3(Vec3Arg inV) const;
  77. /// Multiply vector by only 3x3 part of the transpose of the matrix (\f$result = this^T \: inV\f$)
  78. JPH_INLINE Vec3 Multiply3x3Transposed(Vec3Arg inV) const;
  79. /// Multiply 3x3 matrix by 3x3 matrix
  80. JPH_INLINE Mat44 Multiply3x3(Mat44Arg inM) const;
  81. /// Multiply transpose of 3x3 matrix by 3x3 matrix (\f$result = this^T \: inM\f$)
  82. JPH_INLINE Mat44 Multiply3x3LeftTransposed(Mat44Arg inM) const;
  83. /// Multiply 3x3 matrix by the transpose of a 3x3 matrix (\f$result = this \: inM^T\f$)
  84. JPH_INLINE Mat44 Multiply3x3RightTransposed(Mat44Arg inM) const;
  85. /// Multiply matrix with float
  86. JPH_INLINE Mat44 operator * (float inV) const;
  87. friend JPH_INLINE Mat44 operator * (float inV, Mat44Arg inM) { return inM * inV; }
  88. /// Multiply matrix with float
  89. JPH_INLINE Mat44 & operator *= (float inV);
  90. /// Per element addition of matrix
  91. JPH_INLINE Mat44 operator + (Mat44Arg inM) const;
  92. /// Negate
  93. JPH_INLINE Mat44 operator - () const;
  94. /// Per element subtraction of matrix
  95. JPH_INLINE Mat44 operator - (Mat44Arg inM) const;
  96. /// Per element addition of matrix
  97. JPH_INLINE Mat44 & operator += (Mat44Arg inM);
  98. /// Access to the columns
  99. JPH_INLINE Vec3 GetAxisX() const { return Vec3(mCol[0]); }
  100. JPH_INLINE void SetAxisX(Vec3Arg inV) { mCol[0] = Vec4(inV, 0.0f); }
  101. JPH_INLINE Vec3 GetAxisY() const { return Vec3(mCol[1]); }
  102. JPH_INLINE void SetAxisY(Vec3Arg inV) { mCol[1] = Vec4(inV, 0.0f); }
  103. JPH_INLINE Vec3 GetAxisZ() const { return Vec3(mCol[2]); }
  104. JPH_INLINE void SetAxisZ(Vec3Arg inV) { mCol[2] = Vec4(inV, 0.0f); }
  105. JPH_INLINE Vec3 GetTranslation() const { return Vec3(mCol[3]); }
  106. JPH_INLINE void SetTranslation(Vec3Arg inV) { mCol[3] = Vec4(inV, 1.0f); }
  107. JPH_INLINE Vec3 GetDiagonal3() const { return Vec3(mCol[0][0], mCol[1][1], mCol[2][2]); }
  108. JPH_INLINE void SetDiagonal3(Vec3Arg inV) { mCol[0][0] = inV.GetX(); mCol[1][1] = inV.GetY(); mCol[2][2] = inV.GetZ(); }
  109. JPH_INLINE Vec4 GetDiagonal4() const { return Vec4(mCol[0][0], mCol[1][1], mCol[2][2], mCol[3][3]); }
  110. JPH_INLINE void SetDiagonal4(Vec4Arg inV) { mCol[0][0] = inV.GetX(); mCol[1][1] = inV.GetY(); mCol[2][2] = inV.GetZ(); mCol[3][3] = inV.GetW(); }
  111. JPH_INLINE Vec3 GetColumn3(uint inCol) const { JPH_ASSERT(inCol < 4); return Vec3(mCol[inCol]); }
  112. JPH_INLINE void SetColumn3(uint inCol, Vec3Arg inV) { JPH_ASSERT(inCol < 4); mCol[inCol] = Vec4(inV, inCol == 3? 1.0f : 0.0f); }
  113. JPH_INLINE Vec4 GetColumn4(uint inCol) const { JPH_ASSERT(inCol < 4); return mCol[inCol]; }
  114. JPH_INLINE void SetColumn4(uint inCol, Vec4Arg inV) { JPH_ASSERT(inCol < 4); mCol[inCol] = inV; }
  115. /// Store matrix to memory
  116. JPH_INLINE void StoreFloat4x4(Float4 *outV) const;
  117. /// Transpose matrix
  118. JPH_INLINE Mat44 Transposed() const;
  119. /// Transpose 3x3 subpart of matrix
  120. JPH_INLINE Mat44 Transposed3x3() const;
  121. /// Inverse 4x4 matrix
  122. JPH_INLINE Mat44 Inversed() const;
  123. /// Inverse 4x4 matrix when it only contains rotation and translation
  124. JPH_INLINE Mat44 InversedRotationTranslation() const;
  125. /// Get the determinant of a 3x3 matrix
  126. JPH_INLINE float GetDeterminant3x3() const;
  127. /// Get the adjoint of a 3x3 matrix
  128. JPH_INLINE Mat44 Adjointed3x3() const;
  129. /// Inverse 3x3 matrix
  130. JPH_INLINE Mat44 Inversed3x3() const;
  131. /// Get rotation part only (note: retains the first 3 values from the bottom row)
  132. JPH_INLINE Mat44 GetRotation() const;
  133. /// Get rotation part only (note: also clears the bottom row)
  134. JPH_INLINE Mat44 GetRotationSafe() const;
  135. /// Updates the rotation part of this matrix (the first 3 columns)
  136. JPH_INLINE void SetRotation(Mat44Arg inRotation);
  137. /// Convert to quaternion
  138. JPH_INLINE Quat GetQuaternion() const;
  139. /// Get matrix that transforms a direction with the same transform as this matrix (length is not preserved)
  140. JPH_INLINE Mat44 GetDirectionPreservingMatrix() const { return GetRotation().Inversed3x3().Transposed3x3(); }
  141. /// Pre multiply by translation matrix: result = this * Mat44::sTranslation(inTranslation)
  142. JPH_INLINE Mat44 PreTranslated(Vec3Arg inTranslation) const;
  143. /// Post multiply by translation matrix: result = Mat44::sTranslation(inTranslation) * this (i.e. add inTranslation to the 4-th column)
  144. JPH_INLINE Mat44 PostTranslated(Vec3Arg inTranslation) const;
  145. /// Scale a matrix: result = this * Mat44::sScale(inScale)
  146. JPH_INLINE Mat44 PreScaled(Vec3Arg inScale) const;
  147. /// Scale a matrix: result = Mat44::sScale(inScale) * this
  148. JPH_INLINE Mat44 PostScaled(Vec3Arg inScale) const;
  149. /// Decompose a matrix into a rotation & translation part and into a scale part so that:
  150. /// this = return_value * Mat44::sScale(outScale).
  151. /// This equation only holds when the matrix is orthogonal, if it is not the returned matrix
  152. /// will be made orthogonal using the modified Gram-Schmidt algorithm (see: https://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process)
  153. JPH_INLINE Mat44 Decompose(Vec3 &outScale) const;
  154. #ifndef JPH_DOUBLE_PRECISION
  155. /// In single precision mode just return the matrix itself
  156. JPH_INLINE Mat44 ToMat44() const { return *this; }
  157. #endif // !JPH_DOUBLE_PRECISION
  158. /// To String
  159. friend ostream & operator << (ostream &inStream, Mat44Arg inM)
  160. {
  161. inStream << inM.mCol[0] << ", " << inM.mCol[1] << ", " << inM.mCol[2] << ", " << inM.mCol[3];
  162. return inStream;
  163. }
  164. private:
  165. Vec4 mCol[4]; ///< Column
  166. };
  167. static_assert(is_trivial<Mat44>(), "Is supposed to be a trivial type!");
  168. JPH_NAMESPACE_END
  169. #include "Mat44.inl"