Browse Source

most quaternion / matrix opperations are removed as there is no use case
and the math is not strictly correct, quaternions can only be multiplied
with vectors if they are on the left side (prevents misuse)

Kajetan Johannes Hammerle 3 years ago
parent
commit
aed1d3e7f9
5 changed files with 11 additions and 94 deletions
  1. 8 27
      math/Matrix.cpp
  2. 1 5
      math/Matrix.h
  3. 0 6
      math/Quaternion.cpp
  4. 0 4
      math/Quaternion.h
  5. 2 52
      tests/MatrixTests.cpp

+ 8 - 27
math/Matrix.cpp

@@ -107,32 +107,13 @@ Matrix& Matrix::rotateZ(float degrees) {
     return rotate(degrees, 0, 1);
 }
 
-Matrix& Matrix::operator*=(const Quaternion& q) {
-    const float* mp = getValues();
-    Vector3 a = Vector3(mp[0], mp[1], mp[2]) * q;
-    Vector3 b = Vector3(mp[4], mp[5], mp[6]) * q;
-    Vector3 c = Vector3(mp[8], mp[9], mp[10]) * q;
-    set(0, Vector4(a[0], a[1], a[2], mp[3]));
-    set(1, Vector4(b[0], b[1], b[2], mp[7]));
-    set(2, Vector4(c[0], c[1], c[2], mp[11]));
+Matrix& Matrix::rotate(const Quaternion& q) {
+    Vector3 a = q * Vector3(data[0][0], data[1][0], data[2][0]);
+    Vector3 b = q * Vector3(data[0][1], data[1][1], data[2][1]);
+    Vector3 c = q * Vector3(data[0][2], data[1][2], data[2][2]);
+    Vector3 d = q * Vector3(data[0][3], data[1][3], data[2][3]);
+    set(0, Vector4(a[0], b[0], c[0], d[0]));
+    set(1, Vector4(a[1], b[1], c[1], d[1]));
+    set(2, Vector4(a[2], b[2], c[2], d[2]));
     return *this;
-}
-
-Matrix Matrix::operator*(const Quaternion& q) const {
-    Matrix m(*this);
-    m *= q;
-    return m;
-}
-
-Matrix operator*(const Quaternion& q, const Matrix& m) {
-    const float* mp = m.getValues();
-    Vector3 a = q * Vector3(mp[0], mp[4], mp[8]);
-    Vector3 b = q * Vector3(mp[1], mp[5], mp[9]);
-    Vector3 c = q * Vector3(mp[2], mp[6], mp[10]);
-    Vector3 d = q * Vector3(mp[3], mp[7], mp[11]);
-    Matrix r(m);
-    r.set(0, Vector4(a[0], b[0], c[0], d[0]));
-    r.set(1, Vector4(a[1], b[1], c[1], d[1]));
-    r.set(2, Vector4(a[2], b[2], c[2], d[2]));
-    return r;
 }

+ 1 - 5
math/Matrix.h

@@ -34,9 +34,7 @@ public:
     Matrix& rotateX(float degrees);
     Matrix& rotateY(float degrees);
     Matrix& rotateZ(float degrees);
-    
-    Matrix& operator*=(const Quaternion& q);
-    Matrix operator*(const Quaternion& q) const;
+    Matrix& rotate(const Quaternion& q);
     
     template<int L>
     void toString(StringBuffer<L>& s) const {
@@ -47,6 +45,4 @@ public:
     }
 };
 
-Matrix operator*(const Quaternion& q, const Matrix& m);
-
 #endif

+ 0 - 6
math/Quaternion.cpp

@@ -39,10 +39,4 @@ Vector3 Quaternion::operator*(const Vector3& v) const {
     Vector3 qv = v * w + xyz.cross(v);
     Vector3 qvq = xyz * xyz.dot(v) + qv * w - qv.cross(xyz);
     return qvq;
-}
-
-Vector3 operator*(const Vector3& v, const Quaternion& q) {
-    Vector3 qv = v * q.w - q.xyz.cross(v);
-    Vector3 qvq = q.xyz * q.xyz.dot(v) + qv * q.w + qv.cross(q.xyz);
-    return qvq;
 }

+ 0 - 4
math/Quaternion.h

@@ -8,8 +8,6 @@ class Quaternion {
     Vector3 xyz;
     float w;
 
-    friend Vector3 operator*(const Vector3& v, const Quaternion& q);
-
 public:
     Quaternion();
     Quaternion(const Vector3& axis, float angle);
@@ -29,6 +27,4 @@ public:
     }
 };
 
-Vector3 operator*(const Vector3& v, const Quaternion& q);
-
 #endif

+ 2 - 52
tests/MatrixTests.cpp

@@ -155,60 +155,12 @@ static void testQuaternionMatrix(Test& test) {
 
     Matrix m;
     m.translate(Vector3(1.0f, 2.0f, 3.0f));
-    m = q1 * m;
-    m = q2 * m;
-    m = q3 * m;
+    m.rotate(q1).rotate(q2).rotate(q3);
     m.translate(Vector3(1.0f, 2.0f, 3.0f));
 
     Matrix check;
     check.translate(Vector3(1.0f, 2.0f, 3.0f));
-    check.rotateX(48.0f);
-    check.rotateY(52.0f);
-    check.rotateZ(60.0f);
-    check.translate(Vector3(1.0f, 2.0f, 3.0f));
-
-    for(int i = 0; i < 16; i++) {
-        test.checkFloat(check.getValues()[i], m.getValues()[i], eps, "mul matrix");
-    }
-}
-
-static void testMulSetQuaternion(Test& test) {
-    Quaternion q1(Vector3(1.0f, 0.0f, 0.0f), 48.0f);
-    Quaternion q2(Vector3(0.0f, 1.0f, 0.0f), 52.0f);
-    Quaternion q3(Vector3(0.0f, 0.0f, 1.0f), 60.0f);
-
-    Matrix m;
-    m *= q1;
-    m *= q2;
-    m *= q3;
-    m.translate(Vector3(1.0f, 2.0f, 3.0f));
-
-    Matrix check;
-    check.rotateZ(60.0f);
-    check.rotateY(52.0f);
-    check.rotateX(48.0f);
-    check.translate(Vector3(1.0f, 2.0f, 3.0f));
-
-    for(int i = 0; i < 16; i++) {
-        test.checkFloat(check.getValues()[i], m.getValues()[i], eps, "mul matrix");
-    }
-}
-
-static void testMatrixQuaternion(Test& test) {
-    Quaternion q1(Vector3(1.0f, 0.0f, 0.0f), 48.0f);
-    Quaternion q2(Vector3(0.0f, 1.0f, 0.0f), 52.0f);
-    Quaternion q3(Vector3(0.0f, 0.0f, 1.0f), 60.0f);
-
-    Matrix m;
-    m = m * q1;
-    m = m * q2;
-    m = m * q3;
-    m.translate(Vector3(1.0f, 2.0f, 3.0f));
-
-    Matrix check;
-    check.rotateZ(60.0f);
-    check.rotateY(52.0f);
-    check.rotateX(48.0f);
+    check.rotateX(48.0f).rotateY(52.0f).rotateZ(60.0f);
     check.translate(Vector3(1.0f, 2.0f, 3.0f));
 
     for(int i = 0; i < 16; i++) {
@@ -233,7 +185,5 @@ void MatrixTests::test() {
     testRotateZ(test);
     testToString(test);
     testQuaternionMatrix(test);
-    testMulSetQuaternion(test);
-    testMatrixQuaternion(test);
     test.finalize();
 }