diff --git a/Source/Core/Common/Matrix.cpp b/Source/Core/Common/Matrix.cpp index 57be1a3cc4..fe587de7cb 100644 --- a/Source/Core/Common/Matrix.cpp +++ b/Source/Core/Common/Matrix.cpp @@ -250,11 +250,7 @@ Matrix33 Matrix33::Inverted() const { const auto m = [this](int x, int y) { return data[y + x * 3]; }; - const auto det = m(0, 0) * (m(1, 1) * m(2, 2) - m(2, 1) * m(1, 2)) - - m(0, 1) * (m(1, 0) * m(2, 2) - m(1, 2) * m(2, 0)) + - m(0, 2) * (m(1, 0) * m(2, 1) - m(1, 1) * m(2, 0)); - - const auto invdet = 1 / det; + const auto invdet = 1 / Determinant(); Matrix33 result; @@ -273,6 +269,15 @@ Matrix33 Matrix33::Inverted() const return result; } +float Matrix33::Determinant() const +{ + const auto m = [this](int x, int y) { return data[y + x * 3]; }; + + return m(0, 0) * (m(1, 1) * m(2, 2) - m(2, 1) * m(1, 2)) - + m(0, 1) * (m(1, 0) * m(2, 2) - m(1, 2) * m(2, 0)) + + m(0, 2) * (m(1, 0) * m(2, 1) - m(1, 1) * m(2, 0)); +} + Matrix44 Matrix44::Identity() { Matrix44 mtx = {}; @@ -360,4 +365,17 @@ void Matrix44::Multiply(const Matrix44& a, const Vec4& vec, Vec4* result) result->data = MatrixMultiply<4, 4, 1>(a.data, vec.data); } +float Matrix44::Determinant() const +{ + const auto& m = data; + return m[12] * m[9] * m[6] * m[3] - m[8] * m[13] * m[6] * m[3] - m[12] * m[5] * m[10] * m[3] + + m[4] * m[13] * m[10] * m[3] + m[8] * m[5] * m[14] * m[3] - m[4] * m[9] * m[14] * m[3] - + m[12] * m[9] * m[2] * m[7] + m[8] * m[13] * m[2] * m[7] + m[12] * m[1] * m[10] * m[7] - + m[0] * m[13] * m[10] * m[7] - m[8] * m[1] * m[14] * m[7] + m[0] * m[9] * m[14] * m[7] + + m[12] * m[5] * m[2] * m[11] - m[4] * m[13] * m[2] * m[11] - m[12] * m[1] * m[6] * m[11] + + m[0] * m[13] * m[6] * m[11] + m[4] * m[1] * m[14] * m[11] - m[0] * m[5] * m[14] * m[11] - + m[8] * m[5] * m[2] * m[15] + m[4] * m[9] * m[2] * m[15] + m[8] * m[1] * m[6] * m[15] - + m[0] * m[9] * m[6] * m[15] - m[4] * m[1] * m[10] * m[15] + m[0] * m[5] * m[10] * m[15]; +} + } // namespace Common diff --git a/Source/Core/Common/Matrix.h b/Source/Core/Common/Matrix.h index c2bd78d36c..244ab2d5cb 100644 --- a/Source/Core/Common/Matrix.h +++ b/Source/Core/Common/Matrix.h @@ -392,6 +392,7 @@ public: static void Multiply(const Matrix33& a, const Vec3& vec, Vec3* result); Matrix33 Inverted() const; + float Determinant() const; Matrix33& operator*=(const Matrix33& rhs) { @@ -432,6 +433,8 @@ public: // For when a vec4 isn't needed a multiplication function that takes a Vec3 and w: Vec3 Transform(const Vec3& point, float w) const; + float Determinant() const; + Matrix44& operator*=(const Matrix44& rhs) { Multiply(*this, rhs, this);