Browse Source

Added arithmetic assign operators to Matrix and Vector.

Multiplication, division, addition and substraction is now done
primarily in assigning operators, as they do the computations in-place,
so they are more memory efficient than implementing them using e.g.
`*this = *this+other;`.

Updated unit test only for matrix multiplication, as vector unit tests
test arithmetic asssign operators too.
pull/279/head
Vladimír Vondruš 14 years ago
parent
commit
b8f09a0616
  1. 5
      src/Math/Matrix.h
  2. 6
      src/Math/Matrix3.h
  3. 6
      src/Math/Matrix4.h
  4. 2
      src/Math/Test/MatrixTest.cpp
  5. 68
      src/Math/Vector.h
  6. 24
      src/Math/Vector2.h
  7. 24
      src/Math/Vector3.h
  8. 24
      src/Math/Vector4.h

5
src/Math/Matrix.h

@ -144,6 +144,11 @@ template<class T, size_t size> class Matrix {
return out;
}
/** @brief Multiply and assign matrix operator */
inline Matrix<T, size>& operator*=(const Matrix<T, size>& other) {
return (*this = *this*other);
}
/** @brief Multiply vector operator */
Vector<T, size> operator*(const Vector<T, size>& other) const {
Vector<T, size> out;

6
src/Math/Matrix3.h

@ -58,6 +58,12 @@ template<class T> class Matrix3: public Matrix<T, 3> {
/** @copydoc Matrix::operator*(const Matrix<T, size>&) const */
inline Matrix3<T> operator*(const Matrix<T, 3>& other) const { return Matrix<T, 3>::operator*(other); }
/** @copydoc Matrix::operator*=() */
inline Matrix3<T>& operator*=(const Matrix<T, 3>& other) {
Matrix<T, 3>::operator*=(other);
return *this;
}
/** @copydoc Matrix::operator*(const Vector<T, size>&) const */
inline Vector3<T> operator*(const Vector<T, 3>& other) const { return Matrix<T, 3>::operator*(other); }

6
src/Math/Matrix4.h

@ -124,6 +124,12 @@ template<class T> class Matrix4: public Matrix<T, 4> {
/** @copydoc Matrix::operator*(const Matrix<T, size>&) const */
inline Matrix4<T> operator*(const Matrix<T, 4>& other) const { return Matrix<T, 4>::operator*(other); }
/** @copydoc Matrix::operator*=() */
inline Matrix4<T>& operator*=(const Matrix<T, 4>& other) {
Matrix<T, 4>::operator*=(other);
return *this;
}
/** @copydoc Matrix::operator*(const Vector<T, size>&) const */
inline Vector4<T> operator*(const Vector<T, 4>& other) const { return Matrix<T, 4>::operator*(other); }

2
src/Math/Test/MatrixTest.cpp

@ -158,7 +158,7 @@ void MatrixTest::multiply() {
-12, 8, -20, -26, -2
);
QVERIFY(left*right == expected);
QVERIFY((left *= right) == expected);
}
void MatrixTest::multiplyVector() {

68
src/Math/Vector.h

@ -124,43 +124,71 @@ template<class T, size_t size> class Vector {
}
/** @brief Multiply vector */
Vector<T, size> operator*(T number) const {
Vector<T, size> out;
inline Vector<T, size> operator*(T number) const {
return Vector<T, size>(*this)*=number;
}
/**
* @brief Multiply and assign vector
*
* More efficient than operator*(), because it does the computation
* in-place.
*/
Vector<T, size>& operator*=(T number) {
for(size_t i = 0; i != size; ++i)
out.set(i, at(i)*number);
return out;
set(i, at(i)*number);
return *this;
}
/** @brief Divide vector */
Vector<T, size> operator/(T number) const {
Vector<T, size> out;
inline Vector<T, size> operator/(T number) const {
return Vector<T, size>(*this)/=number;
}
/**
* @brief Divide and assign vector
*
* More efficient than operator/(), because it does the computation
* in-place.
*/
Vector<T, size>& operator/=(T number) {
for(size_t i = 0; i != size; ++i)
out.set(i, at(i)/number);
return out;
set(i, at(i)/number);
return *this;
}
/** @brief Add two vectors */
Vector<T, size> operator+(const Vector<T, size>& other) const {
Vector<T, size> out;
inline Vector<T, size> operator+(const Vector<T, size>& other) const {
return Vector<T, size>(*this)+=other;
}
/**
* @brief Add and assign vector
*
* More efficient than operator+(), because it does the computation
* in-place.
*/
Vector<T, size>& operator+=(const Vector<T, size>& other) {
for(size_t i = 0; i != size; ++i)
out.set(i, at(i)+other.at(i));
return out;
set(i, at(i)+other.at(i));
return *this;
}
/** @brief Substract two vectors */
Vector<T, size> operator-(const Vector<T, size>& other) const {
Vector<T, size> out;
inline Vector<T, size> operator-(const Vector<T, size>& other) const {
return Vector<T, size>(*this)-=other;
}
/**
* @brief Substract and assign vector
*
* More efficient than operator-(), because it does the computation
* in-place.
*/
Vector<T, size>& operator-=(const Vector<T, size>& other) {
for(size_t i = 0; i != size; ++i)
out.set(i, at(i)-other.at(i));
return out;
set(i, at(i)-other.at(i));
return *this;
}
/** @brief Negative vector */

24
src/Math/Vector2.h

@ -57,15 +57,39 @@ template<class T> class Vector2: public Vector<T, 2> {
/** @copydoc Vector::operator*(T) const */
inline Vector2<T> operator*(T number) const { return Vector<T, 2>::operator*(number); }
/** @copydoc Vector::operator*=() */
inline Vector2<T>& operator*=(T number) {
Vector<T, 2>::operator*=(number);
return *this;
}
/** @copydoc Vector::operator/() */
inline Vector2<T> operator/(T number) const { return Vector<T, 2>::operator/(number); }
/** @copydoc Vector::operator/=() */
inline Vector2<T>& operator/=(T number) {
Vector<T, 2>::operator/=(number);
return *this;
}
/** @copydoc Vector::operator+() */
inline Vector2<T> operator+(const Vector<T, 2>& other) const { return Vector<T, 2>::operator+(other); }
/** @copydoc Vector::operator+=() */
inline Vector2<T>& operator+=(const Vector<T, 2>& other) {
Vector<T, 2>::operator+=(other);
return *this;
}
/** @copydoc Vector::operator-(const Vector<T, size>&) const */
inline Vector2<T> operator-(const Vector<T, 2>& other) const { return Vector<T, 2>::operator-(other); }
/** @copydoc Vector::operator-=() */
inline Vector2<T>& operator-=(const Vector<T, 2>& other) {
Vector<T, 2>::operator-=(other);
return *this;
}
/** @copydoc Vector::operator-() */
inline Vector2<T> operator-() const { return Vector<T, 2>::operator-(); }

24
src/Math/Vector3.h

@ -91,15 +91,39 @@ template<class T> class Vector3: public Vector<T, 3> {
/** @copydoc Vector::operator*(T) const */
inline Vector3<T> operator*(T number) const { return Vector<T, 3>::operator*(number); }
/** @copydoc Vector::operator*=() */
inline Vector3<T>& operator*=(T number) {
Vector<T, 3>::operator*=(number);
return *this;
}
/** @copydoc Vector::operator/() */
inline Vector3<T> operator/(T number) const { return Vector<T, 3>::operator/(number); }
/** @copydoc Vector::operator/=() */
inline Vector3<T>& operator/=(T number) {
Vector<T, 3>::operator/=(number);
return *this;
}
/** @copydoc Vector::operator+() */
inline Vector3<T> operator+(const Vector<T, 3>& other) const { return Vector<T, 3>::operator+(other); }
/** @copydoc Vector::operator+=() */
inline Vector3<T>& operator+=(const Vector<T, 3>& other) {
Vector<T, 3>::operator+=(other);
return *this;
}
/** @copydoc Vector::operator-(const Vector<T, size>&) const */
inline Vector3<T> operator-(const Vector<T, 3>& other) const { return Vector<T, 3>::operator-(other); }
/** @copydoc Vector::operator-=() */
inline Vector3<T>& operator-=(const Vector<T, 3>& other) {
Vector<T, 3>::operator-=(other);
return *this;
}
/** @copydoc Vector::operator-() */
inline Vector3<T> operator-() const { return Vector<T, 3>::operator-(); }

24
src/Math/Vector4.h

@ -98,15 +98,39 @@ template<class T> class Vector4: public Vector<T, 4> {
/** @copydoc Vector::operator*(T) const */
inline Vector4<T> operator*(T number) const { return Vector<T, 4>::operator*(number); }
/** @copydoc Vector::operator*=() */
inline Vector4<T>& operator*=(T number) {
Vector<T, 4>::operator*=(number);
return *this;
}
/** @copydoc Vector::operator/() */
inline Vector4<T> operator/(T number) const { return Vector<T, 4>::operator/(number); }
/** @copydoc Vector::operator/=() */
inline Vector4<T>& operator/=(T number) {
Vector<T, 4>::operator/=(number);
return *this;
}
/** @copydoc Vector::operator+() */
inline Vector4<T> operator+(const Vector<T, 4>& other) const { return Vector<T, 4>::operator+(other); }
/** @copydoc Vector::operator+=() */
inline Vector4<T>& operator+=(const Vector<T, 4>& other) {
Vector<T, 4>::operator+=(other);
return *this;
}
/** @copydoc Vector::operator-(const Vector<T, size>&) const */
inline Vector4<T> operator-(const Vector<T, 4>& other) const { return Vector<T, 4>::operator-(other); }
/** @copydoc Vector::operator-=() */
inline Vector4<T>& operator-=(const Vector<T, 4>& other) {
Vector<T, 4>::operator-=(other);
return *this;
}
/** @copydoc Vector::operator-() */
inline Vector4<T> operator-() const { return Vector<T, 4>::operator-(); }

Loading…
Cancel
Save