Browse Source

Added number*Vector and number/Vector operators.

pull/279/head
Vladimír Vondruš 14 years ago
parent
commit
0e08138806
  1. 8
      src/Math/Test/VectorTest.cpp
  2. 51
      src/Math/Vector.h
  3. 2
      src/Math/Vector2.h
  4. 2
      src/Math/Vector3.h
  5. 2
      src/Math/Vector4.h

8
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() {

51
src/Math/Vector.h

@ -183,10 +183,7 @@ template<size_t size, class T> 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<size, T>&)
*/
#ifndef DOXYGEN_GENERATING_OUTPUT
template<class U> inline typename std::enable_if<std::is_arithmetic<U>::value, Vector<size, T>>::type operator*(U number) const {
@ -238,7 +235,7 @@ template<size_t size, class T> class Vector {
/**
* @brief Divide vector
*
* @see operator/=(U)
* @see operator/=(U), operator/(U, const Vector<size, T>&)
*/
#ifndef DOXYGEN_GENERATING_OUTPUT
template<class U> inline typename std::enable_if<std::is_arithmetic<U>::value, Vector<size, T>>::type operator/(U number) const {
@ -405,6 +402,42 @@ template<size_t size, class T> class Vector {
T _data[size];
};
/** @relates Vector
@brief Multiply number with vector
@see Vector::operator*(U) const
*/
#ifndef DOXYGEN_GENERATING_OUTPUT
template<size_t size, class T, class U> inline typename std::enable_if<std::is_arithmetic<U>::value, Vector<size, T>>::type operator*(U number, const Vector<size, T>& vector) {
#else
template<size_t size, class T, class U> inline Vector<size, T> operator*(U number, const Vector<size, T>& 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<size_t size, class T, class U> typename std::enable_if<std::is_arithmetic<U>::value, Vector<size, T>>::type operator/(U number, const Vector<size, T>& vector) {
#else
template<size_t size, class T, class U> Vector<size, T> operator/(U number, const Vector<size, T>& vector) {
#endif
Vector<size, T> out;
for(size_t i = 0; i != size; ++i)
out[i] = number/vector[i];
return out;
}
/** @debugoperator{Vector} */
template<class T, size_t size> Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Vector<size, T>& value) {
debug << "Vector(";
@ -481,6 +514,14 @@ template<class T, size_t size> Corrade::Utility::Debug operator<<(Corrade::Utili
\
inline Type<T> operator-() const { return Vector<size, T>::operator-(); } \
inline Type<T> normalized() const { return Vector<size, T>::normalized(); }
#define MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Type, size) \
template<class T, class U> inline Type<T> operator*(U number, const Type<T>& vector) { \
return number*Vector<size, T>(vector); \
} \
template<class T, class U> inline Type<T> operator/(U number, const Type<T>& vector) { \
return number/Vector<size, T>(vector); \
}
#endif
}}

2
src/Math/Vector2.h

@ -48,6 +48,8 @@ template<class T> class Vector2: public Vector<2, T> {
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector2, 2)
};
MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Vector2, 2)
/** @debugoperator{Vector2} */
template<class T> Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Vector2<T>& value) {
return debug << static_cast<const Magnum::Math::Vector<2, T>&>(value);

2
src/Math/Vector3.h

@ -92,6 +92,8 @@ template<class T> class Vector3: public Vector<3, T> {
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector3, 3)
};
MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Vector3, 3)
/** @debugoperator{Vector3} */
template<class T> Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Vector3<T>& value) {
return debug << static_cast<const Magnum::Math::Vector<3, T>&>(value);

2
src/Math/Vector4.h

@ -86,6 +86,8 @@ template<class T> class Vector4: public Vector<4, T> {
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector4, 4)
};
MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Vector4, 4)
/** @debugoperator{Vector4} */
template<class T> Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Vector4<T>& value) {
return debug << static_cast<const Magnum::Math::Vector<4, T>&>(value);

Loading…
Cancel
Save