From e18a2cdfbfdf330e0ef498af9377cc44ebc5f61f Mon Sep 17 00:00:00 2001 From: josh Date: Wed, 22 May 2024 20:12:53 -0400 Subject: [PATCH] Fix Matrix3x3::Matrix3x3(Quaternion) --- include/J3ML/LinearAlgebra/Matrix4x4.h | 33 +++++++++----------------- src/J3ML/LinearAlgebra/Matrix3x3.cpp | 2 +- src/J3ML/LinearAlgebra/Matrix4x4.cpp | 29 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/include/J3ML/LinearAlgebra/Matrix4x4.h b/include/J3ML/LinearAlgebra/Matrix4x4.h index ce10841..ad59a34 100644 --- a/include/J3ML/LinearAlgebra/Matrix4x4.h +++ b/include/J3ML/LinearAlgebra/Matrix4x4.h @@ -297,16 +297,7 @@ namespace J3ML::LinearAlgebra { /// Identical to D3DXMatrixPerspectiveRH, except transposed to account for Matrix * vector convention used in J3ML. /// See http://msdn.microsoft.com/en-us/library/windows/desktop/bb205355(v=vs.85).aspx /// @note Use the M*v multiplication order to project points with this matrix. - static Matrix4x4 D3DPerspProjRH(float n, float f, float h, float v) - { - Matrix4x4 p; - p[0][0] = 2.f * n / h; p[0][1] = 0; p[0][2] = 0; p[0][3] = 0.f; - p[1][0] = 0; p[1][1] = 2.f * n / v; p[1][2] = 0; p[1][3] = 0.f; - p[2][0] = 0; p[2][1] = 0; p[2][2] = f / (f-n); p[2][3] = n * f / (n-f); - p[3][0] = 0; p[3][1] = 0; p[3][2] = 1.f; p[3][3] = 0.f; - - return p; - } + static Matrix4x4 D3DPerspProjRH(float n, float f, float h, float v); /// Computes a left-handled orthographic projection matrix for OpenGL. /// @note Use the M*v multiplication order to project points with this matrix. @@ -519,8 +510,15 @@ namespace J3ML::LinearAlgebra { Vector3 ExtractScale() const; + /// Returns true if this matrix only contains uniform scaling, compared to the given epsilon. + /// @note If the matrix does not really do any scaling, this function returns true (scaling uniformly by a factor of 1). + /// @note This function only examines the upper 3-by-3 part of this matrix. + /// @note This function assumes that this matrix does not contain projection (the fourth row of this matrix is [0 0 0 1]). bool HasUniformScale(float epsilon = 1e-3f) const; + + /// Returns true if the row vectors of 3x3-top-left submatrix are all perpendicular to each other. bool IsColOrthogonal3(float epsilon = 1e-3f) const; + /// Returns true if the column vector of 3x3-top-left submatrix are all perpendicular to each other. bool IsRowOrthogonal3(float epsilon = 1e-3f) const; bool IsColOrthogonal(float epsilon = 1e-3f) const; @@ -552,7 +550,7 @@ namespace J3ML::LinearAlgebra { /// Computes the Cholesky decomposition of this matrix. /// The returned matrix L satisfies L * transpose(L) = this; /// Returns true on success. - bool ColeskyDecompose(Matrix4x4 &outL) const; + bool CholeskyDecompose(Matrix4x4 &outL) const; /// Computes the LU decomposition of this matrix. /// This decomposition has the form 'this = L * U' @@ -561,10 +559,7 @@ namespace J3ML::LinearAlgebra { /// Inverts this matrix using the generic Gauss's method. /// @return Returns true on success, false otherwise. - bool Inverse(float epsilon = 1e-6f) - { - return InverseMatrix(*this, epsilon); - } + bool Inverse(float epsilon = 1e-6f); /// Returns an inverted copy of this matrix. /// If this matrix does not have an inverse, returns the matrix that was the result of running @@ -614,13 +609,7 @@ namespace J3ML::LinearAlgebra { bool InverseTranspose(); /// Returns the inverse transpose of this matrix. /// Use that matrix to transform covariant vectors (normal vectors). - Matrix4x4 InverseTransposed() const - { - Matrix4x4 copy = *this; - copy.Transpose(); - copy.Inverse(); - return copy; - } + Matrix4x4 InverseTransposed() const; /// Returns the sum of the diagonal elements of this matrix. float Trace() const; diff --git a/src/J3ML/LinearAlgebra/Matrix3x3.cpp b/src/J3ML/LinearAlgebra/Matrix3x3.cpp index c136424..f55932b 100644 --- a/src/J3ML/LinearAlgebra/Matrix3x3.cpp +++ b/src/J3ML/LinearAlgebra/Matrix3x3.cpp @@ -106,7 +106,7 @@ namespace J3ML::LinearAlgebra { } Matrix3x3::Matrix3x3(const Quaternion &orientation) { - + SetRotatePart(orientation); } float Matrix3x3::Determinant() const { diff --git a/src/J3ML/LinearAlgebra/Matrix4x4.cpp b/src/J3ML/LinearAlgebra/Matrix4x4.cpp index 6acbb18..e44c61f 100644 --- a/src/J3ML/LinearAlgebra/Matrix4x4.cpp +++ b/src/J3ML/LinearAlgebra/Matrix4x4.cpp @@ -816,4 +816,33 @@ namespace J3ML::LinearAlgebra { p[3][0] = 0; p[3][1] = 0; p[3][2] = 1.f; p[3][3] = 0.f; } + Matrix4x4 Matrix4x4::D3DPerspProjRH(float n, float f, float h, float v) { + Matrix4x4 p; + p[0][0] = 2.f * n / h; p[0][1] = 0; p[0][2] = 0; p[0][3] = 0.f; + p[1][0] = 0; p[1][1] = 2.f * n / v; p[1][2] = 0; p[1][3] = 0.f; + p[2][0] = 0; p[2][1] = 0; p[2][2] = f / (f-n); p[2][3] = n * f / (n-f); + p[3][0] = 0; p[3][1] = 0; p[3][2] = 1.f; p[3][3] = 0.f; + + return p; + } + + bool Matrix4x4::Inverse(float epsilon) { + return InverseMatrix(*this, epsilon); + } + + bool Matrix4x4::LUDecompose(Matrix4x4 &outLower, Matrix4x4 &outUpper) const { + return LUDecomposeMatrix(*this, outLower, outUpper); + } + + bool Matrix4x4::CholeskyDecompose(Matrix4x4 &outL) const { + return CholeskyDecomposeMatrix(*this, outL); + } + + Matrix4x4 Matrix4x4::InverseTransposed() const { + Matrix4x4 copy = *this; + copy.Transpose(); + copy.Inverse(); + return copy; + } + } \ No newline at end of file