Ver código fonte

inverse function

fixed inverse function, was not returning correctly.
marauder2k7 1 ano atrás
pai
commit
8c19f6d8ca

+ 6 - 6
Engine/source/math/mMatrix.h

@@ -678,7 +678,7 @@ public:
    Matrix(const EulerF& e, const Point3F p);
    Matrix<DATA_TYPE, rows, cols>& set(const EulerF& e, const Point3F p);
 
-   Matrix<DATA_TYPE, rows, cols> inverse();
+   Matrix<DATA_TYPE, rows, cols>& inverse();
    Matrix<DATA_TYPE, rows, cols>& transpose();
    void invert();
 
@@ -1374,7 +1374,7 @@ inline Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::set(const E
 }
 
 template<typename DATA_TYPE, U32 rows, U32 cols>
-inline Matrix<DATA_TYPE, rows, cols> Matrix<DATA_TYPE, rows, cols>::inverse()
+inline Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::inverse()
 {
    // TODO: insert return statement here
    AssertFatal(rows == cols, "Can only perform inverse on square matrices.");
@@ -1382,7 +1382,6 @@ inline Matrix<DATA_TYPE, rows, cols> Matrix<DATA_TYPE, rows, cols>::inverse()
 
    // Create augmented matrix [this | I]
    Matrix<DATA_TYPE, size, 2 * size> augmentedMatrix;
-   Matrix<DATA_TYPE, size, size> resultMatrix;
 
    for (U32 i = 0; i < size; i++)
    {
@@ -1418,7 +1417,8 @@ inline Matrix<DATA_TYPE, rows, cols> Matrix<DATA_TYPE, rows, cols>::inverse()
       // Early out if pivot is 0, return identity matrix.
       if (augmentedMatrix(i, i) == static_cast<DATA_TYPE>(0))
       {
-         return Matrix<DATA_TYPE, rows, cols>(true);
+         this->identity();
+         return *this;
       }
 
       DATA_TYPE pivotVal = augmentedMatrix(i, i);
@@ -1447,11 +1447,11 @@ inline Matrix<DATA_TYPE, rows, cols> Matrix<DATA_TYPE, rows, cols>::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<typename DATA_TYPE, U32 rows, U32 cols>

+ 15 - 6
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);
+}