From 97e80f5df3610b2c09dd199c64e943e5602509e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 23 Dec 2010 23:13:46 +0100 Subject: [PATCH] Function for multiplying vector with matrix. --- src/Matrix.h | 13 +++++++++++++ src/Test/MatrixTest.cpp | 18 ++++++++++++++++++ src/Test/MatrixTest.h | 1 + 3 files changed, 32 insertions(+) diff --git a/src/Matrix.h b/src/Matrix.h index 1d2380100..45bda5aed 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -21,6 +21,7 @@ #include +#include "Vector.h" #include "constants.h" namespace Magnum { @@ -114,6 +115,18 @@ template class Matrix { return out; } + /** @brief Multiply vector operator */ + Vector operator*(const Vector& other) const { + Vector out; + + 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)); + } + + return out; + } + /** @brief Transposed matrix */ Matrix transposed() const { Matrix out; diff --git a/src/Test/MatrixTest.cpp b/src/Test/MatrixTest.cpp index aefcdab59..786668bd5 100644 --- a/src/Test/MatrixTest.cpp +++ b/src/Test/MatrixTest.cpp @@ -135,6 +135,24 @@ void MatrixTest::multiply() { QVERIFY(is); } +void MatrixTest::multiplyVector() { + int matrix[] = { + -3, -3, -1, 3, -5, + -1, -3, -5, 2, 3, + -1, -4, 3, -1, -2, + -5, -5, -1, -4, -1, + 1, 3, -3, -4, -1 + }; + + int vector[] = { 0, 5, 3, 4, 4 }; + + int expected[] = { -24, -35, -32, -25, 1 }; + + bool is = (Matrix(matrix)*Vector(vector) == Vector(expected)); + + QVERIFY(is); +} + void MatrixTest::transposed() { float original[] = { 0.0f, 1.0f, 2.0f, 3.0f, diff --git a/src/Test/MatrixTest.h b/src/Test/MatrixTest.h index 71058d001..39dfb99d4 100644 --- a/src/Test/MatrixTest.h +++ b/src/Test/MatrixTest.h @@ -29,6 +29,7 @@ class MatrixTest: public QObject { void copy(); void multiplyIdentity(); void multiply(); + void multiplyVector(); void transposed(); };