Commits

Anonymous committed 004539a

added (working) 3x3 determinant specialication

Comments (0)

Files changed (2)

neglect/cpplib/include/neglect/matrix.hpp

     }
 
     template <typename T>
+    T determinant(const matrix<T, 3, 3> &mat)
+    {
+        return (
+            mat(0, 0) * mat(1, 1) * mat(2, 2) +
+            mat(0, 1) * mat(1, 2) * mat(2, 0) +
+            mat(0, 2) * mat(1, 0) * mat(2, 1) -
+            mat(0, 0) * mat(1, 2) * mat(2, 1) -
+            mat(0, 1) * mat(1, 0) * mat(2, 2) -
+            mat(0, 2) * mat(1, 1) * mat(2, 0)
+        );
+    }
+
+    template <typename T>
     T determinant(const matrix<T, 2, 2> &mat)
     {
         return mat(0, 0) * mat(1, 1) - mat(0, 1) * mat(1, 0);

neglect/tests/src/test_matrix.cpp

 
     NUT_TESTCASE(matrix_determinant)
     {
-        mat4 m1;
-        m1[0] = vec4( 1,  2,  3,  4);
-        m1[1] = vec4( 5,  6,  7,  8);
-        m1[2] = vec4( 9, 10, 11, 12);
-        m1[3] = vec4(13, 14, 15, 16);
-        NUT_CHECK_EQUAL(determinant(m1), 0);
+        mat2 m2;
+        m2[0] = vec2(4, 3);
+        m2[1] = vec2(2, 5);
+        NUT_CHECK_EQUAL(determinant(m2), 14.0f);
+
+        mat3 m3;
+        m3[0] = vec3(2, 3, 0);
+        m3[1] = vec3(1, 2, 3);
+        m3[2] = vec3(4, 1, 2);
+        NUT_CHECK_EQUAL(determinant(m3), 32.0f);
+
+        mat4 m4;
+        m4[0] = vec4(1, 3, 4, 3);
+        m4[1] = vec4(3, 2, 1, 4);
+        m4[2] = vec4(2, 0, 2, 0);
+        m4[3] = vec4(4, 2, 3, 1);
+        NUT_CHECK_EQUAL(determinant(m4), 60.0f);
     }
 }