Browse Source

Ability to multiply/divide Vector with arbitrary type.

Needed for e.g. colors, where the color is stored as Vector3<GLubyte>
and it is needed to multiply it with some floating-point value.
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
7ff3c6e40d
  1. 5
      src/Math/Test/VectorTest.cpp
  2. 24
      src/Math/Vector.h

5
src/Math/Test/VectorTest.cpp

@ -78,6 +78,11 @@ void VectorTest::multiplyDivide() {
QVERIFY(vec*-1.5f == multiplied);
QVERIFY(multiplied/-1.5f == vec);
Math::Vector<1, char> vecChar(32);
Math::Vector<1, char> multipliedChar(-48);
QVERIFY(vecChar*-1.5f == multipliedChar);
QVERIFY(multipliedChar/-1.5f == vecChar);
}
void VectorTest::addSubstract() {

24
src/Math/Vector.h

@ -135,8 +135,14 @@ template<size_t size, class T> class Vector {
return !operator==(other);
}
/** @brief Multiply vector */
inline Vector<size, T> operator*(T number) const {
/**
* @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.
*/
template<class U> inline Vector<size, T> operator*(U number) const {
return Vector<size, T>(*this)*=number;
}
@ -146,7 +152,7 @@ template<size_t size, class T> class Vector {
* More efficient than operator*(), because it does the computation
* in-place.
*/
Vector<size, T>& operator*=(T number) {
template<class U> Vector<size, T>& operator*=(U number) {
for(size_t i = 0; i != size; ++i)
(*this)[i] *= number;
@ -154,7 +160,7 @@ template<size_t size, class T> class Vector {
}
/** @brief Divide vector */
inline Vector<size, T> operator/(T number) const {
template<class U> inline Vector<size, T> operator/(U number) const {
return Vector<size, T>(*this)/=number;
}
@ -164,7 +170,7 @@ template<size_t size, class T> class Vector {
* More efficient than operator/(), because it does the computation
* in-place.
*/
Vector<size, T>& operator/=(T number) {
template<class U> Vector<size, T>& operator/=(U number) {
for(size_t i = 0; i != size; ++i)
(*this)[i] /= number;
@ -271,17 +277,17 @@ template<size_t size, class T> class Vector {
return *this; \
} \
\
inline Type<T> operator*(T number) const { \
template<class U> inline Type<T> operator*(U number) const { \
return Vector<size, T>::operator*(number); \
} \
inline Type<T>& operator*=(T number) { \
template<class U> inline Type<T>& operator*=(U number) { \
Vector<size, T>::operator*=(number); \
return *this; \
} \
inline Type<T> operator/(T number) const { \
template<class U> inline Type<T> operator/(U number) const { \
return Vector<size, T>::operator/(number); \
} \
inline Type<T>& operator/=(T number) { \
template<class U> inline Type<T>& operator/=(U number) { \
Vector<size, T>::operator/=(number); \
return *this; \
} \

Loading…
Cancel
Save