Commits

Anonymous committed 1074c75

Added dot() function for quaternions.

Comments (0)

Files changed (2)

neglect/cpplib/include/neglect/quaternion.hpp

         return quaternion<T>(q.w, -q.vec);
     }
 
+    /* calculate the dot product */
+    template <typename T>
+    T dot(const quaternion<T> &lhs,
+          const quaternion<T> &rhs)
+    {
+        return lhs.w * rhs.w + dot(lhs.vec, rhs.vec);
+    }
+
     typedef quaternion<float> quat;
     typedef quaternion<int> quati;
 }

neglect/tests/src/test_quaternion.cpp

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