Commits

Jeff Hardy  committed 126ce1f

Add in-place operators to Quaternions and use them as the base operators.

  • Participants
  • Parent commits cb8cef5

Comments (0)

Files changed (3)

File Matrix/Matrix/Matrix.h

         template<class T, int Rows, int Cols>
         Matrix<T, Rows, Cols>& imul(Matrix<T, Rows, Cols> &l, const Matrix<T, Rows, Cols> &r)
         {
-            using namespace std;
+            using std::swap;
 
             Matrix<T, Rows, Cols> result = {};
 

File Matrix/Matrix/Quaternion.h

 #pragma once
 
 #include <cmath>
+#include <utility>
 
 namespace Math
 {
     template<class T>
     struct Quaternion
     {
+        static Quaternion<T> create(T r, T a, T b, T c)
+        {
+            Quaternion<T> result = {r, a, b, c};
+            return result;
+        }
+
+        Quaternion<T>& operator+=(const Quaternion<T> &q)
+        {
+            r += q.r; a += q.a; b += q.b; c += q.c;
+            return *this;
+        }
+
+        Quaternion<T>& operator-=(const Quaternion<T> &q)
+        {
+            r -= q.r; a -= q.a; b -= q.b; c -= q.c;
+            return *this;
+        }
+
+        Quaternion<T>& operator*=(const Quaternion<T> &q)
+        {
+            using std::swap;
+            Quaternion<T> result = {l.r * r.r - l.a * r.a - l.b * r.b - l.c * r.c,
+                                    l.r * r.a + l.a * r.r + l.b * r.c - l.c * r.b,
+                                    l.r * r.b + l.b * r.r + l.c * r.a - l.a * r.c,
+                                    l.r * r.c + l.c * r.r + l.a * r.b - l.b * r.a};
+
+            swap(*this, result);
+            return *this;
+        }
+
+        Quaternion<T>& operator*=(T s)
+        {
+            r *= s; a *= s; b *= s; c *= s;
+            return *this;
+        }
+
+        Quaternion<T>& operator/=(T s)
+        {
+            r /= s; a /= s; b /= s; c /= s;
+            return *this;
+        }
+
         T r, a, b, c;
     };
 
         return q;
     }
 
-    template<class T>
-    Quaternion<T> operator+(const Quaternion<T> &l, const Quaternion<T> &r)
-    {
-        Quaternion<T> result = { l.r + r.r, l.a +r.a, l.b + r.b, l.c + r.c };
-        return result;
-    }
-
-    template<class T>
-    Quaternion<T> operator-(const Quaternion<T> &l, const Quaternion<T> &r)
-    {
-        Quaternion<T> result = { l.r - r.r, l.a - r.a, l.b - r.b, l.c - r.c };
-        return result;
-    }
-
-    template<class T>
-    Quaternion<T> operator*(const Quaternion<T> &q, T s)
-    {
-        Quaternion<T> result = {q.r * s, q.a * s, q.b * s, q.c * s};
-        return result;
-    }
-
-    template<class T>
-    Quaternion<T> operator*(T s, const Quaternion<T> &q)
-    {
-        return q * s;
-    }
-
-    template<class T>
-    Quaternion<T> operator/(const Quaternion<T> &q, T s)
-    {
-        Quaternion<T> result = {q.r / s, q.a / s, q.b / s, q.c / s};
-        return result;
-    }
-
     // conjugate
     template<class T>
     Quaternion<T> operator*(const Quaternion<T> &q)
     }
 
     template<class T>
-    Quaternion<T> operator*(const Quaternion<T> &l, const Quaternion<T> &r)
+    Quaternion<T> operator+(Quaternion<T> l, const Quaternion<T> &r)
+    {
+        return l += r;
+    }
+
+    template<class T>
+    Quaternion<T> operator-(Quaternion<T> l, const Quaternion<T> &r)
+    {
+        return l -= r;
+    }
+
+    template<class T>
+    Quaternion<T> operator*(Quaternion<T> q, T s)
+    {
+        return q *= s;
+    }
+
+    template<class T>
+    Quaternion<T> operator*(T s, Quaternion<T> q)
+    {
+        return q *= s;
+    }
+
+    template<class T>
+    Quaternion<T> operator/(Quaternion<T> q, T s)
+    {
+        return q /= s;
+    }
+
+    template<class T>
+    Quaternion<T> operator*(Quaternion<T> l, const Quaternion<T> &r)
     {
         Quaternion<T> result = { l.r * r.r - l.a * r.a - l.b * r.b - l.c * r.c,
                                  l.r * r.a + l.a * r.r + l.b * r.c - l.c * r.b,

File Matrix/MatrixTests/MatrixTests.vcproj

 				>
 				<FileConfiguration
 					Name="Debug|Win32"
+					ExcludedFromBuild="true"
 					>
 					<Tool
 						Name="VCCLCompilerTool"
 				>
 				<FileConfiguration
 					Name="Debug|Win32"
-					ExcludedFromBuild="true"
 					>
 					<Tool
 						Name="VCCLCompilerTool"