Commits

Anonymous committed 70f674e

Added unary minus functions to quaternions and matrices.

  • Participants
  • Parent commits 1074c75

Comments (0)

Files changed (4)

File neglect/cpplib/include/neglect/matrix.hpp

             return m_cols[row];
         }
 
+        matrix<T, M, N> operator-() const
+        {
+            matrix<T, M, N> rv;
+            T *rv_ptr = rv.ptr();
+            const T *this_ptr = this->ptr();
+            for (size_t i = 0; i < (N * M); i++)
+                rv_ptr[i] = -this_ptr[i];
+            return rv;
+        }
+
     private:
         neglect::vector<T, M> m_cols[N];
     };

File neglect/cpplib/include/neglect/quaternion.hpp

         {
             w = vec.x = vec.y = vec.z = T();
         }
+
+        quaternion<T> operator-() const
+        {
+            return quaternion<T>(-w, -vec);
+        }
     };
 
 

File neglect/tests/src/test_matrix.cpp

         m[3][3] = 25.0f;
         NUT_CHECK(!is_affine(m));
     }
+
+    NUT_TESTCASE(unary_minus)
+    {
+        mat2 m;
+        m[0] = vec2(1, 1);
+        m[1] = vec2(2, 1);
+        mat2 expected;
+        expected[0] = vec2(-1, -1);
+        expected[1] = vec2(-2, -1);
+        NUT_CHECK_EQUAL(-m, expected);
+    }
 }

File neglect/tests/src/test_quaternion.cpp

         quat q2(3.0f, 1.0f, 4.0f, 3.0f);
         NUT_REQUIRE_EQUAL(dot(q1, q2), 29.0f);
     }
+
+    NUT_TESTCASE(unary_minus)
+    {
+        quat q1(1.0f, 2.0f, 3.0f, 4.0f);
+        quat q2(-1.0f, -2.0f, -3.0f, -4.0f);
+        NUT_REQUIRE_EQUAL(-q1, q2);
+    }
 }