Browse Source

Fixed Matrix multiplication & test, documentation updates.

vectorfields
Vladimír Vondruš 16 years ago
parent
commit
8957b21719
  1. 14
      src/Matrix.h
  2. 43
      src/Test/MatrixTest.cpp

14
src/Matrix.h

@ -39,7 +39,7 @@ template<class T, size_t size> class Matrix {
/** /**
* @brief Constructor * @brief Constructor
* @param data One-dimensional array of size*size length in column-major order. * @param data One-dimensional array of @c size*size length in column-major order.
*/ */
inline Matrix(const T* data) { setData(data); } inline Matrix(const T* data) { setData(data); }
@ -56,13 +56,13 @@ template<class T, size_t size> class Matrix {
/** /**
* @brief Raw data * @brief Raw data
* @return One-dimensional array of size*size length in column-major order. * @return One-dimensional array of @c size*size length in column-major order.
*/ */
inline const T* data() const { return _data; } inline const T* data() const { return _data; }
/** /**
* @brief Set raw data * @brief Set raw data
* @param data One-dimensional array of size*size length in column-major order. * @param data One-dimensional array of @c size*size length in column-major order.
*/ */
inline void setData(const T* data) { inline void setData(const T* data) {
memcpy(_data, data, size*size*sizeof(T)); memcpy(_data, data, size*size*sizeof(T));
@ -93,14 +93,14 @@ template<class T, size_t size> class Matrix {
return !operator==(other); return !operator==(other);
} }
/** @brief Multiply operator */ /** @brief Multiply matrix operator */
Matrix<T, size> operator*(const Matrix<T, size>& other) const { Matrix<T, size> operator*(const Matrix<T, size>& other) const {
Matrix<T, size> out(false); Matrix<T, size> out(false);
for(size_t col = 0; col != size; ++col) { for(size_t row = 0; row != size; ++row) {
for(size_t row = 0; row != size; ++row) { for(size_t col = 0; col != size; ++col) {
for(size_t pos = 0; pos != size; ++pos) for(size_t pos = 0; pos != size; ++pos)
out.add(col, row, at(col, pos)*other.at(pos, row)); out.add(col, row, at(pos, row)*other.at(col, pos));
} }
} }

43
src/Test/MatrixTest.cpp

@ -79,7 +79,7 @@ void MatrixTest::copy() {
/* Copy */ /* Copy */
Matrix4 m2(m1); Matrix4 m2(m1);
Matrix4 m3; Matrix4 m3;
m3.set(0, 0, 1.0f); m3.set(0, 0, 1.0f); /* this line is here so it's not optimized to Matrix4 m3(m1) */
m3 = m1; m3 = m1;
/* Change original */ /* Change original */
@ -89,7 +89,8 @@ void MatrixTest::copy() {
Matrix4 original(false); Matrix4 original(false);
original.set(2, 3, 1.0f); original.set(2, 3, 1.0f);
QVERIFY(m2 == original && m3 == original); QVERIFY(m2 == original);
QVERIFY(m3 == original);
} }
void MatrixTest::multiplyIdentity() { void MatrixTest::multiplyIdentity() {
@ -105,31 +106,31 @@ void MatrixTest::multiplyIdentity() {
} }
void MatrixTest::multiply() { void MatrixTest::multiply() {
float left[] = { int left[] = {
-3, -1, -1, -5, 1, -3, -3, -1, 3, -5,
-3, -3, -4, -5, 3, -1, -3, -5, 2, 3,
-1, -5, 3, -1, -3, -1, -4, 3, -1, -2,
3, 2, -1, -4, -4, -5, -5, -1, -4, -1,
-5, 3, -2, -1, -1 1, 3, -3, -4, -1
}; };
float right[] = { int right[] = {
0, 5, 3, -3, 0, 0, 5, 3, 4, 4,
5, 5, 2, 0, -1, 5, 5, 0, 0, -2,
3, 0, -4, -1, -4, 3, 2, -4, -3, 0,
4, 0, -3, 2, 4, -3, 0, -1, 2, -1,
4, -2, 0, -1, 3 0, -1, -4, 4, 3
}; };
float expected[] = { int expected[] = {
-24, -22, 8, -1, -12, -24, -35, -32, -25, 1,
-35, -36, 16, 0, 8, -22, -36, -24, 33, -8,
-32, -24, -22, 1, -20, 8, 16, -22, 29, 2,
-25, 33, 29, -12, -26, -1, 0, 1, -12, 16,
1, -8, 2, 16, -2 -12, 8, -20, -26, -2
}; };
bool is = Matrix<int, 5>(left)*Matrix<int, 5>(right) == Matrix<int, 5>(expected); bool is = (Matrix<int, 5>(left)*Matrix<int, 5>(right) == Matrix<int, 5>(expected));
QVERIFY(is); QVERIFY(is);
} }

Loading…
Cancel
Save