diff --git a/Engine/source/math/mMatrix.h b/Engine/source/math/mMatrix.h index 4591df37d..de2fd1af6 100644 --- a/Engine/source/math/mMatrix.h +++ b/Engine/source/math/mMatrix.h @@ -678,7 +678,7 @@ public: Matrix(const EulerF& e, const Point3F p); Matrix& set(const EulerF& e, const Point3F p); - Matrix inverse(); + Matrix& inverse(); Matrix& transpose(); void invert(); @@ -1374,7 +1374,7 @@ inline Matrix& Matrix::set(const E } template -inline Matrix Matrix::inverse() +inline Matrix& Matrix::inverse() { // TODO: insert return statement here AssertFatal(rows == cols, "Can only perform inverse on square matrices."); @@ -1382,7 +1382,6 @@ inline Matrix Matrix::inverse() // Create augmented matrix [this | I] Matrix augmentedMatrix; - Matrix resultMatrix; for (U32 i = 0; i < size; i++) { @@ -1418,7 +1417,8 @@ inline Matrix Matrix::inverse() // Early out if pivot is 0, return identity matrix. if (augmentedMatrix(i, i) == static_cast(0)) { - return Matrix(true); + this->identity(); + return *this; } DATA_TYPE pivotVal = augmentedMatrix(i, i); @@ -1447,11 +1447,11 @@ inline Matrix Matrix::inverse() { for (U32 j = 0; j < size; j++) { - resultMatrix(i, j) = augmentedMatrix(i, j + size); + (*this)(i, j) = augmentedMatrix(i, j + size); } } - return resultMatrix; + return (*this); } template diff --git a/Engine/source/testing/mathMatrixTest.cpp b/Engine/source/testing/mathMatrixTest.cpp index 833ccd6d8..5807989b5 100644 --- a/Engine/source/testing/mathMatrixTest.cpp +++ b/Engine/source/testing/mathMatrixTest.cpp @@ -114,12 +114,6 @@ TEST(MatrixTest, TestMulFunction) test.mul(test2); - // mulL result - /*(null) = | 0.5403 -0.4546 0.7081 0.0000 | - | 0.8415 0.2919 -0.4546 0.0000 | - | 0.0000 0.8415 0.5403 0.0000 | - | 4.3845 -0.8479 3.1714 1.0000 |*/ - EXPECT_NEAR(test(0, 0), 0.5403f, 0.001f); EXPECT_NEAR(test(0, 1), 0.8415f, 0.001f); EXPECT_NEAR(test(0, 2), 0.0f, 0.001f); EXPECT_NEAR(test(0, 3), 5.0f, 0.001f); EXPECT_NEAR(test(1, 0), -0.4546f, 0.001f); EXPECT_NEAR(test(1, 1), 0.2919f, 0.001f); EXPECT_NEAR(test(1, 2), 0.8415f, 0.001f); EXPECT_NEAR(test(1, 3), 2.0f, 0.001f); EXPECT_NEAR(test(2, 0), 0.7081f, 0.001f); EXPECT_NEAR(test(2, 1), -0.4546f, 0.001f); EXPECT_NEAR(test(2, 2), 0.5403f, 0.001f); EXPECT_NEAR(test(2, 3), 1.0f, 0.001f); @@ -170,3 +164,18 @@ TEST(MatrixTest, TestMulArgMatrixFunction) EXPECT_NEAR(testResult(3, 0), 0.0f, 0.001f); EXPECT_NEAR(testResult(3, 1), 0.0f, 0.001f); EXPECT_NEAR(testResult(3, 2), 0.0f, 0.001f); EXPECT_NEAR(testResult(3, 3), 1.0f, 0.001f); } +TEST(MatrixTest, TestInverse) +{ + MatrixF test(true); + test.setPosition(Point3F(5.0f, 2.0f, 1.0f)); + MatrixF test2(EulerF(1.0f, 0.0f, 1.0f)); + + test.mulL(test2); + + test.inverse(); + + EXPECT_NEAR(test(0, 0), 0.5403f, 0.001f); EXPECT_NEAR(test(0, 1), -0.4546f, 0.001f); EXPECT_NEAR(test(0, 2), 0.7081f, 0.001f); EXPECT_NEAR(test(0, 3), -5.0f, 0.001f); + EXPECT_NEAR(test(1, 0), 0.8415f, 0.001f); EXPECT_NEAR(test(1, 1), 0.2919f, 0.001f); EXPECT_NEAR(test(1, 2), -0.4546f, 0.001f); EXPECT_NEAR(test(1, 3), -2.0f, 0.001f); + EXPECT_NEAR(test(2, 0), 0.0, 0.001f); EXPECT_NEAR(test(2, 1), 0.8415f, 0.001f); EXPECT_NEAR(test(2, 2), 0.5403f, 0.001f); EXPECT_NEAR(test(2, 3), -1.0f, 0.001f); + EXPECT_NEAR(test(3, 0), 0.0f, 0.001f); EXPECT_NEAR(test(3, 1), 0.0f, 0.001f); EXPECT_NEAR(test(3, 2), 0.0f, 0.001f); EXPECT_NEAR(test(3, 3), 1.0f, 0.001f); +}