From 0e081388065253642a4a6d182e8d834bcccb4b14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 17 Aug 2012 11:26:51 +0200 Subject: [PATCH] Added number*Vector and number/Vector operators. --- src/Math/Test/VectorTest.cpp | 8 ++++++ src/Math/Vector.h | 51 ++++++++++++++++++++++++++++++++---- src/Math/Vector2.h | 2 ++ src/Math/Vector3.h | 2 ++ src/Math/Vector4.h | 2 ++ 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index 822355090..93f14bef3 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -107,12 +107,20 @@ void VectorTest::multiplyDivide() { Vector4 multiplied(-1.5f, -3.0f, -4.5f, -6.0f); CORRADE_COMPARE(vec*-1.5f, multiplied); + CORRADE_COMPARE(-1.5f*vec, multiplied); CORRADE_COMPARE(multiplied/-1.5f, vec); Math::Vector<1, char> vecChar(32); Math::Vector<1, char> multipliedChar(-48); CORRADE_COMPARE(vecChar*-1.5f, multipliedChar); CORRADE_COMPARE(multipliedChar/-1.5f, vecChar); + CORRADE_COMPARE(-1.5f*vecChar, multipliedChar); + + /* Divide vector with number and inverse */ + Vector4 divisor(1.0f, 2.0f, -4.0f, 8.0f); + Vector4 result(1.0f, 0.5f, -0.25f, 0.125f); + CORRADE_COMPARE(1.0f/divisor, result); + CORRADE_COMPARE(-1550.0f/multipliedChar, vecChar); } void VectorTest::multiplyDivideComponentWise() { diff --git a/src/Math/Vector.h b/src/Math/Vector.h index c4569f485..6121ab144 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -183,10 +183,7 @@ template class Vector { /** * @brief Multiply vector * - * Note that corresponding operator with swapped type order - * (multiplying number with vector) is not available, because it would - * cause ambiguity in some cases. - * @see operator*=(U) + * @see operator*=(U), operator*(U, const Vector&) */ #ifndef DOXYGEN_GENERATING_OUTPUT template inline typename std::enable_if::value, Vector>::type operator*(U number) const { @@ -238,7 +235,7 @@ template class Vector { /** * @brief Divide vector * - * @see operator/=(U) + * @see operator/=(U), operator/(U, const Vector&) */ #ifndef DOXYGEN_GENERATING_OUTPUT template inline typename std::enable_if::value, Vector>::type operator/(U number) const { @@ -405,6 +402,42 @@ template class Vector { T _data[size]; }; +/** @relates Vector +@brief Multiply number with vector + +@see Vector::operator*(U) const +*/ +#ifndef DOXYGEN_GENERATING_OUTPUT +template inline typename std::enable_if::value, Vector>::type operator*(U number, const Vector& vector) { +#else +template inline Vector operator*(U number, const Vector& vector) { +#endif + return vector*number; +} + +/** @relates Vector +@brief Divide vector with number and invert + +Example: +@code +Vector<4, float> vec(1.0f, 2.0f, -4.0f, 8.0f); +Vector<4, float> another = 1.0f/vec; // {1.0f, 0.5f, -0.25f, 0.128f} +@endcode +@see Vector::operator/(U) const +*/ +#ifndef DOXYGEN_GENERATING_OUTPUT +template typename std::enable_if::value, Vector>::type operator/(U number, const Vector& vector) { +#else +template Vector operator/(U number, const Vector& vector) { +#endif + Vector out; + + for(size_t i = 0; i != size; ++i) + out[i] = number/vector[i]; + + return out; +} + /** @debugoperator{Vector} */ template Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Vector& value) { debug << "Vector("; @@ -481,6 +514,14 @@ template Corrade::Utility::Debug operator<<(Corrade::Utili \ inline Type operator-() const { return Vector::operator-(); } \ inline Type normalized() const { return Vector::normalized(); } + +#define MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Type, size) \ + template inline Type operator*(U number, const Type& vector) { \ + return number*Vector(vector); \ + } \ + template inline Type operator/(U number, const Type& vector) { \ + return number/Vector(vector); \ + } #endif }} diff --git a/src/Math/Vector2.h b/src/Math/Vector2.h index 5920261c4..262fe58c8 100644 --- a/src/Math/Vector2.h +++ b/src/Math/Vector2.h @@ -48,6 +48,8 @@ template class Vector2: public Vector<2, T> { MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector2, 2) }; +MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Vector2, 2) + /** @debugoperator{Vector2} */ template Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Vector2& value) { return debug << static_cast&>(value); diff --git a/src/Math/Vector3.h b/src/Math/Vector3.h index d70e6dc08..29edd6df7 100644 --- a/src/Math/Vector3.h +++ b/src/Math/Vector3.h @@ -92,6 +92,8 @@ template class Vector3: public Vector<3, T> { MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector3, 3) }; +MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Vector3, 3) + /** @debugoperator{Vector3} */ template Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Vector3& value) { return debug << static_cast&>(value); diff --git a/src/Math/Vector4.h b/src/Math/Vector4.h index 32d4f3c51..88fb0f5d8 100644 --- a/src/Math/Vector4.h +++ b/src/Math/Vector4.h @@ -86,6 +86,8 @@ template class Vector4: public Vector<4, T> { MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector4, 4) }; +MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Vector4, 4) + /** @debugoperator{Vector4} */ template Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Vector4& value) { return debug << static_cast&>(value);