From a4c80d18ed500d1edce8c842f1f50396a56b4b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 17 Aug 2012 02:03:43 +0200 Subject: [PATCH] Added component-wise multiplication and division to Vector. --- src/Math/Test/VectorTest.cpp | 10 +++++++ src/Math/Test/VectorTest.h | 1 + src/Math/Vector.h | 58 ++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index eb9c09d87..822355090 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -38,6 +38,7 @@ VectorTest::VectorTest() { &VectorTest::copy, &VectorTest::dot, &VectorTest::multiplyDivide, + &VectorTest::multiplyDivideComponentWise, &VectorTest::addSubtract, &VectorTest::dotSelf, &VectorTest::length, @@ -114,6 +115,15 @@ void VectorTest::multiplyDivide() { CORRADE_COMPARE(multipliedChar/-1.5f, vecChar); } +void VectorTest::multiplyDivideComponentWise() { + Vector4 vec(1.0f, 2.0f, 3.0f, 4.0f); + Vector4 multiplier(7.0f, -4.0f, -1.5f, 1.0f); + Vector4 multiplied(7.0f, -8.0f, -4.5f, 4.0f); + + CORRADE_COMPARE(vec*multiplier, multiplied); + CORRADE_COMPARE(multiplied/multiplier, vec); +} + void VectorTest::addSubtract() { Vector4 a(0.5f, -7.5f, 9.0f, -11.0f); Vector4 b(-0.5, 1.0f, 0.0f, 7.5f); diff --git a/src/Math/Test/VectorTest.h b/src/Math/Test/VectorTest.h index 29404c289..75a9d3cc7 100644 --- a/src/Math/Test/VectorTest.h +++ b/src/Math/Test/VectorTest.h @@ -29,6 +29,7 @@ class VectorTest: public Corrade::TestSuite::Tester { void copy(); void dot(); void multiplyDivide(); + void multiplyDivideComponentWise(); void addSubtract(); void dotSelf(); void length(); diff --git a/src/Math/Vector.h b/src/Math/Vector.h index 009adc3c3..88adce375 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -205,6 +205,28 @@ template class Vector { return *this; } + /** + * @brief Multiply vector component-wise + * + * @see operator*=(const Vector&) + */ + Vector operator*(const Vector& other) const { + return Vector(*this)*=other; + } + + /** + * @brief Multiply vector component-wise and assign + * + * More efficient than operator*(const Vector&) const, + * because it does the computation in-place. + */ + Vector& operator*=(const Vector& other) { + for(size_t i = 0; i != size; ++i) + (*this)[i] *= other[i]; + + return *this; + } + /** * @brief Divide vector * @@ -227,6 +249,28 @@ template class Vector { return *this; } + /** + * @brief Divide vector component-wise + * + * @see operator/=(const Vector&) + */ + Vector operator/(const Vector& other) const { + return Vector(*this)/=other; + } + + /** + * @brief Divide vector component-wise and assign + * + * More efficient than operator/(const Vector&) const, + * because it does the computation in-place. + */ + Vector& operator/=(const Vector& other) { + for(size_t i = 0; i != size; ++i) + (*this)[i] /= other[i]; + + return *this; + } + /** @brief Add two vectors */ inline Vector operator+(const Vector& other) const { return Vector(*this)+=other; @@ -382,12 +426,26 @@ template Corrade::Utility::Debug operator<<(Corrade::Utili Vector::operator*=(number); \ return *this; \ } \ + inline Type operator*(const Vector& other) const { \ + return Vector::operator*(other); \ + } \ + inline Type& operator*=(const Vector& other) { \ + Vector::operator*=(other); \ + return *this; \ + } \ template inline Type operator/(U number) const { \ return Vector::operator/(number); \ } \ template inline Type& operator/=(U number) { \ Vector::operator/=(number); \ return *this; \ + } \ + inline Type operator/(const Vector& other) const { \ + return Vector::operator/(other); \ + } \ + inline Type& operator/=(const Vector& other) { \ + Vector::operator/=(other); \ + return *this; \ } \ \ inline Type operator+(const Vector& other) const { \