Browse Source

Replaced clutter in Vector and Matrix subclasses with a macro.

Each function which returned e.g. Vector<size, T> was in subclasses
overloaded with function returning e.g. Vector3<T>, so the user is able
to use subclass-specific functions. It was nightmare to maintain and it
cluttered the documentation a lot.
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
fb8825b081
  1. 37
      src/Math/Matrix.h
  2. 45
      src/Math/Matrix3.h
  3. 47
      src/Math/Matrix4.h
  4. 46
      src/Math/Vector.h
  5. 60
      src/Math/Vector2.h
  6. 60
      src/Math/Vector3.h
  7. 60
      src/Math/Vector4.h

37
src/Math/Matrix.h

@ -263,6 +263,43 @@ template<size_t size, class T> class Matrix {
}; };
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT
#define MAGNUM_MATRIX_SUBCLASS_IMPLEMENTATION(Type, VectorType, size) \
inline constexpr static Type<T>& from(T* data) { \
return *reinterpret_cast<Type<T>*>(data); \
} \
inline constexpr static const Type<T>& from(const T* data) { \
return *reinterpret_cast<const Type<T>*>(data); \
} \
template<class ...U> inline constexpr static Type<T> from(const Vector<size, T>& first, const U&... next) { \
return Matrix<size, T>::from(first, next...); \
} \
\
inline Type<T>& operator=(const Type<T>& other) { \
Matrix<size, T>::operator=(other); \
return *this; \
} \
\
inline VectorType<T>& operator[](size_t col) { \
return VectorType<T>::from(Matrix<size, T>::data()+col*size); \
} \
inline constexpr const VectorType<T>& operator[](size_t col) const { \
return VectorType<T>::from(Matrix<size, T>::data()+col*size); \
} \
\
inline Type<T> operator*(const Matrix<size, T>& other) const { \
return Matrix<size, T>::operator*(other); \
} \
inline Type<T>& operator*=(const Matrix<size, T>& other) { \
Matrix<size, T>::operator*=(other); \
return *this; \
} \
inline VectorType<T> operator*(const Vector<size, T>& other) const { \
return Matrix<size, T>::operator*(other); \
} \
\
inline Type<T> transposed() const { return Matrix<size, T>::transposed(); } \
inline Type<T> inverted() const { return Matrix<size, T>::inverted(); }
namespace Implementation { namespace Implementation {
template<size_t size, class T> class MatrixDeterminant { template<size_t size, class T> class MatrixDeterminant {

45
src/Math/Matrix3.h

@ -27,21 +27,6 @@ namespace Magnum { namespace Math {
/** @brief 3x3 matrix */ /** @brief 3x3 matrix */
template<class T> class Matrix3: public Matrix<3, T> { template<class T> class Matrix3: public Matrix<3, T> {
public: public:
/** @copydoc Matrix::from(T*) */
inline constexpr static Matrix3<T>& from(T* data) {
return *reinterpret_cast<Matrix3<T>*>(data);
}
/** @copydoc Matrix::from(const T*) */
inline constexpr static const Matrix3<T>& from(const T* data) {
return *reinterpret_cast<const Matrix3<T>*>(data);
}
/** @copydoc Matrix::from(const Vector<size, T>&, const U&...) */
template<class ...U> inline constexpr static Matrix3<T> from(const Vector<3, T>& first, const U&... next) {
return Matrix<3, T>::from(first, next...);
}
/** @copydoc Matrix::Matrix(ZeroType) */ /** @copydoc Matrix::Matrix(ZeroType) */
inline constexpr explicit Matrix3(typename Matrix<3, T>::ZeroType): Matrix<3, T>(Matrix<3, T>::Zero) {} inline constexpr explicit Matrix3(typename Matrix<3, T>::ZeroType): Matrix<3, T>(Matrix<3, T>::Zero) {}
@ -66,35 +51,7 @@ template<class T> class Matrix3: public Matrix<3, T> {
/** @copydoc Matrix::Matrix(const Matrix<size, T>&) */ /** @copydoc Matrix::Matrix(const Matrix<size, T>&) */
inline constexpr Matrix3(const Matrix<3, T>& other): Matrix<3, T>(other) {} inline constexpr Matrix3(const Matrix<3, T>& other): Matrix<3, T>(other) {}
/** @copydoc Matrix::operator=() */ MAGNUM_MATRIX_SUBCLASS_IMPLEMENTATION(Matrix3, Vector3, 3)
inline Matrix3<T>& operator=(const Matrix3<T>& other) {
Matrix<3, T>::operator=(other);
return *this;
}
/** @copydoc Matrix::operator[](size_t) */
inline Vector3<T>& operator[](size_t col) { return Vector3<T>::from(Matrix<3, T>::data()+col*3); }
/** @copydoc Matrix::operator[](size_t) const */
inline constexpr const Vector3<T>& operator[](size_t col) const { return Vector3<T>::from(Matrix<3, T>::data()+col*3); }
/** @copydoc Matrix::operator*(const Matrix<size, T>&) const */
inline Matrix3<T> operator*(const Matrix<3, T>& other) const { return Matrix<3, T>::operator*(other); }
/** @copydoc Matrix::operator*=() */
inline Matrix3<T>& operator*=(const Matrix<3, T>& other) {
Matrix<3, T>::operator*=(other);
return *this;
}
/** @copydoc Matrix::operator*(const Vector<size, T>&) const */
inline Vector3<T> operator*(const Vector<3, T>& other) const { return Matrix<3, T>::operator*(other); }
/** @copydoc Matrix::transposed() */
inline Matrix3<T> transposed() const { return Matrix<3, T>::transposed(); }
/** @copydoc Matrix::inverted() */
inline Matrix3<T> inverted() const { return Matrix<3, T>::inverted(); }
}; };
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT

47
src/Math/Matrix4.h

@ -32,21 +32,6 @@ namespace Magnum { namespace Math {
*/ */
template<class T> class Matrix4: public Matrix<4, T> { template<class T> class Matrix4: public Matrix<4, T> {
public: public:
/** @copydoc Matrix::from(T*) */
inline constexpr static Matrix4<T>& from(T* data) {
return *reinterpret_cast<Matrix4<T>*>(data);
}
/** @copydoc Matrix::from(const T*) */
inline constexpr static const Matrix4<T>& from(const T* data) {
return *reinterpret_cast<const Matrix4<T>*>(data);
}
/** @copydoc Matrix::from(const Vector<size, T>&, const U&...) */
template<class ...U> inline constexpr static Matrix4<T> from(const Vector<4, T>& first, const U&... next) {
return Matrix<4, T>::from(first, next...);
}
/** /**
* @brief Translation matrix * @brief Translation matrix
* @param vec Translation vector * @param vec Translation vector
@ -134,39 +119,9 @@ template<class T> class Matrix4: public Matrix<4, T> {
/** @copydoc Matrix::Matrix(const Matrix<size, T>&) */ /** @copydoc Matrix::Matrix(const Matrix<size, T>&) */
inline constexpr Matrix4(const Matrix<4, T>& other): Matrix<4, T>(other) {} inline constexpr Matrix4(const Matrix<4, T>& other): Matrix<4, T>(other) {}
/** @copydoc Matrix::operator=() */
inline Matrix4<T>& operator=(const Matrix4<T>& other) {
Matrix<4, T>::operator=(other);
return *this;
}
/** @copydoc Matrix::operator[](size_t) */
inline Vector4<T>& operator[](size_t col) { return Vector4<T>::from(Matrix<4, T>::data()+col*4); }
/** @copydoc Matrix::operator[](size_t) const */
inline constexpr const Vector4<T>& operator[](size_t col) const { return Vector4<T>::from(Matrix<4, T>::data()+col*4); }
/** @copydoc Matrix::operator*(const Matrix<size, T>&) const */
inline Matrix4<T> operator*(const Matrix<4, T>& other) const { return Matrix<4, T>::operator*(other); }
/** @copydoc Matrix::operator*=() */
inline Matrix4<T>& operator*=(const Matrix<4, T>& other) {
Matrix<4, T>::operator*=(other);
return *this;
}
/** @copydoc Matrix::operator*(const Vector<size, T>&) const */
inline Vector4<T> operator*(const Vector<4, T>& other) const { return Matrix<4, T>::operator*(other); }
/** @copydoc Matrix::transposed() */
inline Matrix4<T> transposed() const { return Matrix<4, T>::transposed(); }
/** @copydoc Matrix::ij() */ /** @copydoc Matrix::ij() */
inline Matrix3<T> ij(size_t skipRow, size_t skipCol) const { return Matrix<4, T>::ij(skipRow, skipCol); } inline Matrix3<T> ij(size_t skipRow, size_t skipCol) const { return Matrix<4, T>::ij(skipRow, skipCol); }
/** @copydoc Matrix::inverted() */
inline Matrix4<T> inverted() const { return Matrix<4, T>::inverted(); }
/** @brief Rotation and scaling part of the matrix */ /** @brief Rotation and scaling part of the matrix */
inline Matrix3<T> rotationScaling() const { inline Matrix3<T> rotationScaling() const {
return Matrix3<T>::from( return Matrix3<T>::from(
@ -182,6 +137,8 @@ template<class T> class Matrix4: public Matrix<4, T> {
(*this)[1].xyz().normalized(), (*this)[1].xyz().normalized(),
(*this)[2].xyz().normalized()); (*this)[2].xyz().normalized());
} }
MAGNUM_MATRIX_SUBCLASS_IMPLEMENTATION(Matrix4, Vector4, 4)
}; };
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT

46
src/Math/Vector.h

@ -258,6 +258,52 @@ template<size_t size, class T> class Vector {
}; };
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT
#define MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Type, size) \
inline constexpr static Type<T>& from(T* data) { \
return *reinterpret_cast<Type<T>*>(data); \
} \
inline constexpr static const Type<T>& from(const T* data) { \
return *reinterpret_cast<const Type<T>*>(data); \
} \
\
inline Type<T>& operator=(const Type<T>& other) { \
Vector<size, T>::operator=(other); \
return *this; \
} \
\
inline Type<T> operator*(T number) const { \
return Vector<size, T>::operator*(number); \
} \
inline Type<T>& operator*=(T number) { \
Vector<size, T>::operator*=(number); \
return *this; \
} \
inline Type<T> operator/(T number) const { \
return Vector<size, T>::operator/(number); \
} \
inline Type<T>& operator/=(T number) { \
Vector<size, T>::operator/=(number); \
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> normalized() const { return Vector<size, T>::normalized(); }
template<class T, size_t size> Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Vector<size, T>& value) { template<class T, size_t size> Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Vector<size, T>& value) {
debug << "Vector("; debug << "Vector(";
debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, false); debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, false);

60
src/Math/Vector2.h

@ -26,20 +26,10 @@ namespace Magnum { namespace Math {
/** @brief Two-component vector */ /** @brief Two-component vector */
template<class T> class Vector2: public Vector<2, T> { template<class T> class Vector2: public Vector<2, T> {
public: public:
/** @copydoc Vector::from(T*) */
inline constexpr static Vector2<T>& from(T* data) {
return *reinterpret_cast<Vector2<T>*>(data);
}
/** @copydoc Vector::from(const T*) */
inline constexpr static const Vector2<T>& from(const T* data) {
return *reinterpret_cast<const Vector2<T>*>(data);
}
/** @copydoc Vector::Vector(T) */ /** @copydoc Vector::Vector(T) */
inline constexpr explicit Vector2(T value = T()): Vector<2, T>(value, value) {} inline constexpr explicit Vector2(T value = T()): Vector<2, T>(value, value) {}
/** @copydoc Vector::Vector(const Vector&) */ /** @copydoc Vector::Vector(const Vector&) */
inline constexpr Vector2(const Vector<2, T>& other): Vector<2, T>(other) {} inline constexpr Vector2(const Vector<2, T>& other): Vector<2, T>(other) {}
/** /**
@ -55,53 +45,7 @@ template<class T> class Vector2: public Vector<2, T> {
inline void setX(T value) { (*this)[0] = value; } /**< @brief Set X component */ inline void setX(T value) { (*this)[0] = value; } /**< @brief Set X component */
inline void setY(T value) { (*this)[1] = value; } /**< @brief Set Y component */ inline void setY(T value) { (*this)[1] = value; } /**< @brief Set Y component */
/** @copydoc Vector::operator=() */ MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector2, 2)
inline Vector2<T>& operator=(const Vector2<T>& other) {
Vector<2, T>::operator=(other);
return *this;
}
/** @copydoc Vector::operator*(T) const */
inline Vector2<T> operator*(T number) const { return Vector<2, T>::operator*(number); }
/** @copydoc Vector::operator*=() */
inline Vector2<T>& operator*=(T number) {
Vector<2, T>::operator*=(number);
return *this;
}
/** @copydoc Vector::operator/() */
inline Vector2<T> operator/(T number) const { return Vector<2, T>::operator/(number); }
/** @copydoc Vector::operator/=() */
inline Vector2<T>& operator/=(T number) {
Vector<2, T>::operator/=(number);
return *this;
}
/** @copydoc Vector::operator+() */
inline Vector2<T> operator+(const Vector<2, T>& other) const { return Vector<2, T>::operator+(other); }
/** @copydoc Vector::operator+=() */
inline Vector2<T>& operator+=(const Vector<2, T>& other) {
Vector<2, T>::operator+=(other);
return *this;
}
/** @copydoc Vector::operator-(const Vector<size, T>&) const */
inline Vector2<T> operator-(const Vector<2, T>& other) const { return Vector<2, T>::operator-(other); }
/** @copydoc Vector::operator-=() */
inline Vector2<T>& operator-=(const Vector<2, T>& other) {
Vector<2, T>::operator-=(other);
return *this;
}
/** @copydoc Vector::operator-() */
inline Vector2<T> operator-() const { return Vector<2, T>::operator-(); }
/** @copydoc Vector::normalized() */
inline Vector2<T> normalized() const { return Vector<2, T>::normalized(); }
}; };
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT

60
src/Math/Vector3.h

@ -26,16 +26,6 @@ namespace Magnum { namespace Math {
/** @brief Three-component vector */ /** @brief Three-component vector */
template<class T> class Vector3: public Vector<3, T> { template<class T> class Vector3: public Vector<3, T> {
public: public:
/** @copydoc Vector::from(T*) */
inline constexpr static Vector3<T>& from(T* data) {
return *reinterpret_cast<Vector3<T>*>(data);
}
/** @copydoc Vector::from(const T*) */
inline constexpr static const Vector3<T>& from(const T* data) {
return *reinterpret_cast<const Vector3<T>*>(data);
}
/** @brief %Vector in direction of X axis */ /** @brief %Vector in direction of X axis */
inline constexpr static Vector3<T> xAxis(T length = T(1)) { return Vector3<T>(length, T(), T()); } inline constexpr static Vector3<T> xAxis(T length = T(1)) { return Vector3<T>(length, T(), T()); }
@ -62,7 +52,7 @@ template<class T> class Vector3: public Vector<3, T> {
/** @copydoc Vector::Vector(T) */ /** @copydoc Vector::Vector(T) */
inline constexpr explicit Vector3(T value = T()): Vector<3, T>(value, value, value) {} inline constexpr explicit Vector3(T value = T()): Vector<3, T>(value, value, value) {}
/** @copydoc Vector::Vector(const Vector&) */ /** @copydoc Vector::Vector(const Vector&) */
inline constexpr Vector3(const Vector<3, T>& other): Vector<3, T>(other) {} inline constexpr Vector3(const Vector<3, T>& other): Vector<3, T>(other) {}
/** /**
@ -96,53 +86,7 @@ template<class T> class Vector3: public Vector<3, T> {
inline void setG(T value) { setY(value); } /**< @brief Set G component */ inline void setG(T value) { setY(value); } /**< @brief Set G component */
inline void setB(T value) { setZ(value); } /**< @brief Set B component */ inline void setB(T value) { setZ(value); } /**< @brief Set B component */
/** @copydoc Vector::operator=() */ MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector3, 3)
inline Vector3<T>& operator=(const Vector3<T>& other) {
Vector<3, T>::operator=(other);
return *this;
}
/** @copydoc Vector::operator*(T) const */
inline Vector3<T> operator*(T number) const { return Vector<3, T>::operator*(number); }
/** @copydoc Vector::operator*=() */
inline Vector3<T>& operator*=(T number) {
Vector<3, T>::operator*=(number);
return *this;
}
/** @copydoc Vector::operator/() */
inline Vector3<T> operator/(T number) const { return Vector<3, T>::operator/(number); }
/** @copydoc Vector::operator/=() */
inline Vector3<T>& operator/=(T number) {
Vector<3, T>::operator/=(number);
return *this;
}
/** @copydoc Vector::operator+() */
inline Vector3<T> operator+(const Vector<3, T>& other) const { return Vector<3, T>::operator+(other); }
/** @copydoc Vector::operator+=() */
inline Vector3<T>& operator+=(const Vector<3, T>& other) {
Vector<3, T>::operator+=(other);
return *this;
}
/** @copydoc Vector::operator-(const Vector<size, T>&) const */
inline Vector3<T> operator-(const Vector<3, T>& other) const { return Vector<3, T>::operator-(other); }
/** @copydoc Vector::operator-=() */
inline Vector3<T>& operator-=(const Vector<3, T>& other) {
Vector<3, T>::operator-=(other);
return *this;
}
/** @copydoc Vector::operator-() */
inline Vector3<T> operator-() const { return Vector<3, T>::operator-(); }
/** @copydoc Vector::normalized() */
inline Vector3<T> normalized() const { return Vector<3, T>::normalized(); }
}; };
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT

60
src/Math/Vector4.h

@ -26,16 +26,6 @@ namespace Magnum { namespace Math {
/** @brief Four-component vector */ /** @brief Four-component vector */
template<class T> class Vector4: public Vector<4, T> { template<class T> class Vector4: public Vector<4, T> {
public: public:
/** @copydoc Vector::from(T*) */
inline constexpr static Vector4<T>& from(T* data) {
return *reinterpret_cast<Vector4<T>*>(data);
}
/** @copydoc Vector::from(const T*) */
inline constexpr static const Vector4<T>& from(const T* data) {
return *reinterpret_cast<const Vector4<T>*>(data);
}
/** /**
* @copydoc Vector::Vector * @copydoc Vector::Vector
* *
@ -46,7 +36,7 @@ template<class T> class Vector4: public Vector<4, T> {
/** @copydoc Vector::Vector(T) */ /** @copydoc Vector::Vector(T) */
inline constexpr explicit Vector4(T value): Vector<4, T>(value, value, value, value) {} inline constexpr explicit Vector4(T value): Vector<4, T>(value, value, value, value) {}
/** @copydoc Vector::Vector(const Vector&) */ /** @copydoc Vector::Vector(const Vector&) */
inline constexpr Vector4(const Vector<4, T>& other): Vector<4, T>(other) {} inline constexpr Vector4(const Vector<4, T>& other): Vector<4, T>(other) {}
/** /**
@ -97,53 +87,7 @@ template<class T> class Vector4: public Vector<4, T> {
*/ */
inline constexpr Vector3<T> rgb() const { return xyz(); } inline constexpr Vector3<T> rgb() const { return xyz(); }
/** @copydoc Vector::operator=() */ MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector4, 4)
inline Vector4<T>& operator=(const Vector4<T>& other) {
Vector<4, T>::operator=(other);
return *this;
}
/** @copydoc Vector::operator*(T) const */
inline Vector4<T> operator*(T number) const { return Vector<4, T>::operator*(number); }
/** @copydoc Vector::operator*=() */
inline Vector4<T>& operator*=(T number) {
Vector<4, T>::operator*=(number);
return *this;
}
/** @copydoc Vector::operator/() */
inline Vector4<T> operator/(T number) const { return Vector<4, T>::operator/(number); }
/** @copydoc Vector::operator/=() */
inline Vector4<T>& operator/=(T number) {
Vector<4, T>::operator/=(number);
return *this;
}
/** @copydoc Vector::operator+() */
inline Vector4<T> operator+(const Vector<4, T>& other) const { return Vector<4, T>::operator+(other); }
/** @copydoc Vector::operator+=() */
inline Vector4<T>& operator+=(const Vector<4, T>& other) {
Vector<4, T>::operator+=(other);
return *this;
}
/** @copydoc Vector::operator-(const Vector<size, T>&) const */
inline Vector4<T> operator-(const Vector<4, T>& other) const { return Vector<4, T>::operator-(other); }
/** @copydoc Vector::operator-=() */
inline Vector4<T>& operator-=(const Vector<4, T>& other) {
Vector<4, T>::operator-=(other);
return *this;
}
/** @copydoc Vector::operator-() */
inline Vector4<T> operator-() const { return Vector<4, T>::operator-(); }
/** @copydoc Vector::normalized() */
inline Vector4<T> normalized() const { return Vector<4, T>::normalized(); }
}; };
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT

Loading…
Cancel
Save