From 9e516b97a1779ee1b3572b8bc83bb0e5e3bf28e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 30 Dec 2010 00:41:19 +0100 Subject: [PATCH] Mathematically correct order of row, col parameters in Matrix functions. --- src/Math/Matrix.h | 12 ++++++------ src/Math/Matrix4.h | 18 +++++++++--------- src/Math/Test/MatrixTest.cpp | 14 +++++++------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Math/Matrix.h b/src/Math/Matrix.h index 3d14ad051..86c7af168 100644 --- a/src/Math/Matrix.h +++ b/src/Math/Matrix.h @@ -79,17 +79,17 @@ template class Matrix { } /** @brief Value at given position */ - inline T at(size_t col, size_t row) const { + inline T at(size_t row, size_t col) const { return _data[col*size+row]; } /** @brief Set value at given position */ - inline void set(size_t col, size_t row, T value) { + inline void set(size_t row, size_t col, T value) { _data[col*size+row] = value; } /** @brief Add value to given position */ - inline void add(size_t col, size_t row, T value) { + inline void add(size_t row, size_t col, T value) { _data[col*size+row] += value; } @@ -97,7 +97,7 @@ template class Matrix { inline bool operator==(const Matrix& other) const { for(size_t row = 0; row != size; ++row) { for(size_t col = 0; col != size; ++col) - if(std::abs(at(col, row) - other.at(col, row)) >= EPSILON) return false; + if(std::abs(at(row, col) - other.at(row, col)) >= EPSILON) return false; } return true; @@ -115,7 +115,7 @@ template class Matrix { for(size_t row = 0; row != size; ++row) { for(size_t col = 0; col != size; ++col) { for(size_t pos = 0; pos != size; ++pos) - out.add(col, row, at(pos, row)*other.at(col, pos)); + out.add(row, col, at(row, pos)*other.at(pos, col)); } } @@ -128,7 +128,7 @@ template class Matrix { for(size_t row = 0; row != size; ++row) { for(size_t pos = 0; pos != size; ++pos) - out.add(row, at(pos, row)*other.at(pos)); + out.add(row, at(row, pos)*other.at(pos)); } return out; diff --git a/src/Math/Matrix4.h b/src/Math/Matrix4.h index 69d2119e3..d39f21374 100644 --- a/src/Math/Matrix4.h +++ b/src/Math/Matrix4.h @@ -52,9 +52,9 @@ template class Matrix4: public Matrix { */ static Matrix4 translation(const Vector3& vec) { Matrix4 out; /* (Identity matrix) */ - out.set(3, 0, vec.x()); - out.set(3, 1, vec.y()); - out.set(3, 2, vec.z()); + out.set(0, 3, vec.x()); + out.set(1, 3, vec.y()); + out.set(2, 3, vec.z()); return out; } @@ -116,13 +116,13 @@ template class Matrix4: public Matrix { out.set(3, 3, T(1)); out.set(0, 0, cosine + xx*oneMinusCosine); - out.set(1, 0, xy*oneMinusCosine - vn.z()*sine); - out.set(2, 0, xz*oneMinusCosine + vn.y()*sine); - out.set(0, 1, xy*oneMinusCosine + vn.z()*sine); + out.set(0, 1, xy*oneMinusCosine - vn.z()*sine); + out.set(0, 2, xz*oneMinusCosine + vn.y()*sine); + out.set(1, 0, xy*oneMinusCosine + vn.z()*sine); out.set(1, 1, cosine + yy*oneMinusCosine); - out.set(2, 1, yz*oneMinusCosine - vn.x()*sine); - out.set(0, 2, xz*oneMinusCosine - vn.y()*sine); - out.set(1, 2, yz*oneMinusCosine + vn.x()*sine); + out.set(1, 2, yz*oneMinusCosine - vn.x()*sine); + out.set(2, 0, xz*oneMinusCosine - vn.y()*sine); + out.set(2, 1, yz*oneMinusCosine + vn.x()*sine); out.set(2, 2, cosine + zz*oneMinusCosine); return out; diff --git a/src/Math/Test/MatrixTest.cpp b/src/Math/Test/MatrixTest.cpp index c267100ee..b9ce6e866 100644 --- a/src/Math/Test/MatrixTest.cpp +++ b/src/Math/Test/MatrixTest.cpp @@ -54,12 +54,12 @@ void MatrixTest::constructZero() { void MatrixTest::data() { Matrix4 m(false); - m.set(2, 1, 1.0f); - m.set(1, 2, 1.0f); - m.add(1, 2, 0.5f); - QVERIFY(m.at(2, 1) == 1.0f); + m.set(2, 1, 1.0f); + m.add(2, 1, 0.5f); + + QVERIFY(m.at(1, 2) == 1.0f); float expected[] = { 0.0f, 0.0f, 0.0f, 0.0f, @@ -74,7 +74,7 @@ void MatrixTest::data() { void MatrixTest::copy() { Matrix4 m1(false); - m1.set(2, 3, 1.0f); + m1.set(3, 2, 1.0f); /* Copy */ Matrix4 m2(m1); @@ -83,11 +83,11 @@ void MatrixTest::copy() { m3 = m1; /* Change original */ - m1.set(3, 2, 1.0f); + m1.set(2, 3, 1.0f); /* Verify the copy is the same as original */ Matrix4 original(false); - original.set(2, 3, 1.0f); + original.set(3, 2, 1.0f); QVERIFY(m2 == original); QVERIFY(m3 == original);