Browse Source

Moved add/subtract operators from Vector to RectangularMatrix.

vectorfields
Vladimír Vondruš 14 years ago
parent
commit
4bcc805616
  1. 1
      src/Math/Matrix.h
  2. 1
      src/Math/Matrix3.h
  3. 1
      src/Math/Matrix4.h
  4. 60
      src/Math/RectangularMatrix.h
  5. 22
      src/Math/Test/RectangularMatrixTest.cpp
  6. 3
      src/Math/Test/RectangularMatrixTest.h
  7. 10
      src/Math/Test/VectorTest.cpp
  8. 1
      src/Math/Test/VectorTest.h
  9. 52
      src/Math/Vector.h
  10. 1
      src/Math/Vector2.h
  11. 1
      src/Math/Vector3.h
  12. 1
      src/Math/Vector4.h

1
src/Math/Matrix.h

@ -142,6 +142,7 @@ template<size_t s, class T> class Matrix: public RectangularMatrix<s, s, T> {
} }
#endif #endif
MAGNUM_RECTANGULARMATRIX_SUBCLASS_IMPLEMENTATION(size, size, Matrix<size, T>) MAGNUM_RECTANGULARMATRIX_SUBCLASS_IMPLEMENTATION(size, size, Matrix<size, T>)
MAGNUM_RECTANGULARMATRIX_SUBCLASS_OPERATOR_IMPLEMENTATION(size, size, Matrix<size, T>)
}; };
/** @debugoperator{Magnum::Math::Matrix} */ /** @debugoperator{Magnum::Math::Matrix} */

1
src/Math/Matrix3.h

@ -96,6 +96,7 @@ template<class T> class Matrix3: public Matrix<3, T> {
inline constexpr Matrix3(const RectangularMatrix<3, 3, T>& other): Matrix<3, T>(other) {} inline constexpr Matrix3(const RectangularMatrix<3, 3, T>& other): Matrix<3, T>(other) {}
MAGNUM_MATRIX_SUBCLASS_IMPLEMENTATION(Matrix3, Vector3, 3) MAGNUM_MATRIX_SUBCLASS_IMPLEMENTATION(Matrix3, Vector3, 3)
MAGNUM_RECTANGULARMATRIX_SUBCLASS_OPERATOR_IMPLEMENTATION(3, 3, Matrix3<T>)
}; };
/** @debugoperator{Magnum::Math::Matrix3} */ /** @debugoperator{Magnum::Math::Matrix3} */

1
src/Math/Matrix4.h

@ -145,6 +145,7 @@ template<class T> class Matrix4: public Matrix<4, T> {
} }
MAGNUM_MATRIX_SUBCLASS_IMPLEMENTATION(Matrix4, Vector4, 4) MAGNUM_MATRIX_SUBCLASS_IMPLEMENTATION(Matrix4, Vector4, 4)
MAGNUM_RECTANGULARMATRIX_SUBCLASS_OPERATOR_IMPLEMENTATION(4, 4, Matrix4<T>)
}; };
/** @debugoperator{Magnum::Math::Matrix4} */ /** @debugoperator{Magnum::Math::Matrix4} */

60
src/Math/RectangularMatrix.h

@ -161,6 +161,50 @@ template<size_t c, size_t r, class T> class RectangularMatrix {
return !operator==(other); return !operator==(other);
} }
/**
* @brief Add matrix
*
* @see operator+=()
*/
inline RectangularMatrix<cols, rows, T> operator+(const RectangularMatrix<cols, rows, T>& other) const {
return RectangularMatrix<cols, rows, T>(*this)+=other;
}
/**
* @brief Add and assign matrix
*
* More efficient than operator+(), because it does the computation
* in-place.
*/
RectangularMatrix<cols, rows, T>& operator+=(const RectangularMatrix<cols, rows, T>& other) {
for(size_t i = 0; i != cols*rows; ++i)
_data[i] += other._data[i];
return *this;
}
/**
* @brief Subtract matrix
*
* @see operator-=()
*/
inline RectangularMatrix<cols, rows, T> operator-(const RectangularMatrix<cols, rows, T>& other) const {
return RectangularMatrix<cols, rows, T>(*this)-=other;
}
/**
* @brief Subtract and assign matrix
*
* More efficient than operator-(), because it does the computation
* in-place.
*/
RectangularMatrix<cols, rows, T>& operator-=(const RectangularMatrix<cols, rows, T>& other) {
for(size_t i = 0; i != cols*rows; ++i)
_data[i] -= other._data[i];
return *this;
}
/** @brief Multiply matrix */ /** @brief Multiply matrix */
template<size_t size> RectangularMatrix<size, rows, T> operator*(const RectangularMatrix<size, cols, T>& other) const { template<size_t size> RectangularMatrix<size, rows, T> operator*(const RectangularMatrix<size, cols, T>& other) const {
RectangularMatrix<size, rows, T> out; RectangularMatrix<size, rows, T> out;
@ -237,6 +281,22 @@ template<size_t cols, size_t rows, class T> Corrade::Utility::Debug operator<<(C
RectangularMatrix<cols, rows, T>::operator=(other); \ RectangularMatrix<cols, rows, T>::operator=(other); \
return *this; \ return *this; \
} }
#define MAGNUM_RECTANGULARMATRIX_SUBCLASS_OPERATOR_IMPLEMENTATION(cols, rows, ...) \
inline __VA_ARGS__ operator+(const RectangularMatrix<cols, rows, T>& other) const { \
return RectangularMatrix<cols, rows, T>::operator+(other); \
} \
inline __VA_ARGS__& operator+=(const RectangularMatrix<cols, rows, T>& other) { \
RectangularMatrix<cols, rows, T>::operator+=(other); \
return *this; \
} \
inline __VA_ARGS__ operator-(const RectangularMatrix<cols, rows, T>& other) const { \
return RectangularMatrix<cols, rows, T>::operator-(other); \
} \
inline __VA_ARGS__& operator-=(const RectangularMatrix<cols, rows, T>& other) { \
RectangularMatrix<cols, rows, T>::operator-=(other); \
return *this; \
}
#endif #endif
}} }}

22
src/Math/Test/RectangularMatrixTest.cpp

@ -35,8 +35,12 @@ RectangularMatrixTest::RectangularMatrixTest() {
&RectangularMatrixTest::constructFromVectors, &RectangularMatrixTest::constructFromVectors,
&RectangularMatrixTest::constructZero, &RectangularMatrixTest::constructZero,
&RectangularMatrixTest::data, &RectangularMatrixTest::data,
&RectangularMatrixTest::addSubtract,
&RectangularMatrixTest::multiply, &RectangularMatrixTest::multiply,
&RectangularMatrixTest::transposed, &RectangularMatrixTest::transposed,
&RectangularMatrixTest::debug, &RectangularMatrixTest::debug,
&RectangularMatrixTest::configuration); &RectangularMatrixTest::configuration);
} }
@ -105,6 +109,24 @@ void RectangularMatrixTest::data() {
CORRADE_COMPARE(m, expected); CORRADE_COMPARE(m, expected);
} }
void RectangularMatrixTest::addSubtract() {
Matrix4x3 a(0.0f, 1.0f, 3.0f,
4.0f, 5.0f, 7.0f,
8.0f, 9.0f, 11.0f,
12.0f, 13.0f, 15.0f);
Matrix4x3 b(-4.0f, 0.5f, 9.0f,
-9.0f, 11.0f, 0.25f,
0.0f, -8.0f, 19.0f,
-3.0f, -5.0f, 2.0f);
Matrix4x3 e(-4.0f, 1.5f, 12.0f,
-5.0f, 16.0f, 7.25f,
8.0f, 1.0f, 30.0f,
9.0f, 8.0f, 17.0f);
CORRADE_COMPARE(a + b, e);
CORRADE_COMPARE(e - b, a);
}
void RectangularMatrixTest::multiply() { void RectangularMatrixTest::multiply() {
RectangularMatrix<4, 6, int> left( RectangularMatrix<4, 6, int> left(
-5, 27, 10, 33, 0, -15, -5, 27, 10, 33, 0, -15,

3
src/Math/Test/RectangularMatrixTest.h

@ -27,7 +27,10 @@ class RectangularMatrixTest: public Corrade::TestSuite::Tester<RectangularMatrix
void constructFromVectors(); void constructFromVectors();
void constructZero(); void constructZero();
void data(); void data();
void addSubtract();
void multiply(); void multiply();
void transposed(); void transposed();
void debug(); void debug();

10
src/Math/Test/VectorTest.cpp

@ -37,7 +37,6 @@ VectorTest::VectorTest() {
&VectorTest::dot, &VectorTest::dot,
&VectorTest::multiplyDivide, &VectorTest::multiplyDivide,
&VectorTest::multiplyDivideComponentWise, &VectorTest::multiplyDivideComponentWise,
&VectorTest::addSubtract,
&VectorTest::dotSelf, &VectorTest::dotSelf,
&VectorTest::length, &VectorTest::length,
&VectorTest::normalized, &VectorTest::normalized,
@ -101,15 +100,6 @@ void VectorTest::multiplyDivideComponentWise() {
CORRADE_COMPARE(multiplied/multiplier, vec); 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);
Vector4 expected(0.0f, -6.5f, 9.0f, -3.5f);
CORRADE_COMPARE(a + b, expected);
CORRADE_COMPARE(expected - b, a);
}
void VectorTest::dotSelf() { void VectorTest::dotSelf() {
CORRADE_COMPARE(Vector4(1.0f, 2.0f, 3.0f, 4.0f).dot(), 30.0f); CORRADE_COMPARE(Vector4(1.0f, 2.0f, 3.0f, 4.0f).dot(), 30.0f);
} }

1
src/Math/Test/VectorTest.h

@ -28,7 +28,6 @@ class VectorTest: public Corrade::TestSuite::Tester<VectorTest> {
void dot(); void dot();
void multiplyDivide(); void multiplyDivide();
void multiplyDivideComponentWise(); void multiplyDivideComponentWise();
void addSubtract();
void dotSelf(); void dotSelf();
void length(); void length();
void normalized(); void normalized();

52
src/Math/Vector.h

@ -229,42 +229,6 @@ template<size_t s, class T> class Vector: public RectangularMatrix<1, s, T> {
return *this; return *this;
} }
/** @brief Add two vectors */
inline Vector<size, T> operator+(const Vector<size, T>& other) const {
return Vector<size, T>(*this)+=other;
}
/**
* @brief Add and assign vector
*
* More efficient than operator+(), because it does the computation
* in-place.
*/
Vector<size, T>& operator+=(const Vector<size, T>& other) {
for(size_t i = 0; i != size; ++i)
(*this)[i] += other[i];
return *this;
}
/** @brief Subtract two vectors */
inline Vector<size, T> operator-(const Vector<size, T>& other) const {
return Vector<size, T>(*this)-=other;
}
/**
* @brief Subtract and assign vector
*
* More efficient than operator-(), because it does the computation
* in-place.
*/
Vector<size, T>& operator-=(const Vector<size, T>& other) {
for(size_t i = 0; i != size; ++i)
(*this)[i] -= other[i];
return *this;
}
/** @brief Negative vector */ /** @brief Negative vector */
Vector<size, T> operator-() const { Vector<size, T> operator-() const {
Vector<size, T> out; Vector<size, T> out;
@ -344,6 +308,7 @@ template<size_t s, class T> class Vector: public RectangularMatrix<1, s, T> {
} }
MAGNUM_RECTANGULARMATRIX_SUBCLASS_IMPLEMENTATION(1, size, Vector<size, T>) MAGNUM_RECTANGULARMATRIX_SUBCLASS_IMPLEMENTATION(1, size, Vector<size, T>)
MAGNUM_RECTANGULARMATRIX_SUBCLASS_OPERATOR_IMPLEMENTATION(1, size, Vector<size, T>)
private: private:
/* Hiding unused things from RectangularMatrix */ /* Hiding unused things from RectangularMatrix */
@ -448,21 +413,6 @@ template<size_t size, class T> Corrade::Utility::Debug operator<<(Corrade::Utili
return *this; \ return *this; \
} \ } \
\ \
inline Type<T> operator+(const Vector<size, T>& other) const { \
return Vector<size, T>::operator+(other); \
} \
inline Type<T>& operator+=(const Vector<size, T>& other) { \
Vector<size, T>::operator+=(other); \
return *this; \
} \
inline Type<T> operator-(const Vector<size, T>& other) const { \
return Vector<size, T>::operator-(other); \
} \
inline Type<T>& operator-=(const Vector<size, T>& other) { \
Vector<size, T>::operator-=(other); \
return *this; \
} \
\
inline Type<T> operator-() const { return Vector<size, T>::operator-(); } \ inline Type<T> operator-() const { return Vector<size, T>::operator-(); } \
inline Type<T> normalized() const { return Vector<size, T>::normalized(); } inline Type<T> normalized() const { return Vector<size, T>::normalized(); }

1
src/Math/Vector2.h

@ -88,6 +88,7 @@ template<class T> class Vector2: public Vector<2, T> {
inline void setY(T value) { (*this)[1] = value; } /**< @brief Set Y component */ inline void setY(T value) { (*this)[1] = value; } /**< @brief Set Y component */
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector2, 2) MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector2, 2)
MAGNUM_RECTANGULARMATRIX_SUBCLASS_OPERATOR_IMPLEMENTATION(1, 2, Vector2<T>)
}; };
MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Vector2, 2) MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Vector2, 2)

1
src/Math/Vector3.h

@ -140,6 +140,7 @@ template<class T> class Vector3: public Vector<3, T> {
inline constexpr Vector2<T> xy() const { return Vector2<T>::from(Vector<3, T>::data()); } inline constexpr Vector2<T> xy() const { return Vector2<T>::from(Vector<3, T>::data()); }
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector3, 3) MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector3, 3)
MAGNUM_RECTANGULARMATRIX_SUBCLASS_OPERATOR_IMPLEMENTATION(1, 3, Vector3<T>)
}; };
MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Vector3, 3) MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Vector3, 3)

1
src/Math/Vector4.h

@ -88,6 +88,7 @@ template<class T> class Vector4: public Vector<4, T> {
inline constexpr Vector2<T> xy() const { return Vector2<T>::from(Vector<4, T>::data()); } inline constexpr Vector2<T> xy() const { return Vector2<T>::from(Vector<4, T>::data()); }
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector4, 4) MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector4, 4)
MAGNUM_RECTANGULARMATRIX_SUBCLASS_OPERATOR_IMPLEMENTATION(1, 3, Vector4<T>)
}; };
MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Vector4, 4) MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Vector4, 4)

Loading…
Cancel
Save