Browse Source

Math: make all matrix and vector operators either member or friend.

Except for a vector and a row matrix multiplication operator, which
doesn't make sense to be a member. These are all now significantly
shorter thanks to not having to repeat that many template parameters,
and they can make use of direct access into the _data array for better
debug perf.
pull/168/head
Vladimír Vondruš 2 years ago
parent
commit
8b1026d457
  1. 8
      src/Magnum/Math/Color.h
  2. 83
      src/Magnum/Math/RectangularMatrix.h
  3. 2
      src/Magnum/Math/Test/VectorTest.cpp
  4. 1123
      src/Magnum/Math/Vector.h
  5. 4
      src/Magnum/Math/Vector2.h
  6. 4
      src/Magnum/Math/Vector3.h
  7. 4
      src/Magnum/Math/Vector4.h

8
src/Magnum/Math/Color.h

@ -715,10 +715,6 @@ template<class T> class Color3: public Vector3<T> {
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(3, Color3)
};
#ifndef DOXYGEN_GENERATING_OUTPUT
MAGNUM_VECTORn_OPERATOR_IMPLEMENTATION(3, Color3)
#endif
/**
@brief Color in linear RGBA color space
@ -1293,10 +1289,6 @@ extern template MAGNUM_EXPORT Debug& operator<<(Debug&, const ColorHsv<Float>&);
#endif
#endif
#ifndef DOXYGEN_GENERATING_OUTPUT
MAGNUM_VECTORn_OPERATOR_IMPLEMENTATION(4, Color4)
#endif
namespace Literals {
/** @relatesalso Magnum::Math::Color3

83
src/Magnum/Math/RectangularMatrix.h

@ -424,6 +424,22 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
return RectangularMatrix<cols, rows, T>(*this) *= scalar;
}
/**
* @brief Multiply a scalar with a matrix
*
* Same as @ref operator*(T) const.
*/
friend RectangularMatrix<cols, rows, T> operator*(
#ifdef DOXYGEN_GENERATING_OUTPUT
T
#else
typename std::common_type<T>::type
#endif
scalar, const RectangularMatrix<cols, rows, T>& matrix)
{
return matrix*scalar;
}
/**
* @brief Divide with a scalar and assign
*
@ -448,6 +464,30 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
return RectangularMatrix<cols, rows, T>(*this) /= scalar;
}
/**
* @brief Divide a matrix with a scalar and invert
*
* The computation is done column-wise. @f[
* \boldsymbol B_j = \frac a {\boldsymbol A_j}
* @f]
* @see @ref operator/(T) const
*/
friend RectangularMatrix<cols, rows, T> operator/(
#ifdef DOXYGEN_GENERATING_OUTPUT
T
#else
typename std::common_type<T>::type
#endif
scalar, const RectangularMatrix<cols, rows, T>& matrix)
{
RectangularMatrix<cols, rows, T> out{Magnum::NoInit};
for(std::size_t i = 0; i != cols; ++i)
out._data[i] = scalar/matrix._data[i];
return out;
}
/**
* @brief Multiply a matrix
*
@ -637,46 +677,6 @@ Convenience alternative to @cpp RectangularMatrix<4, 3, T> @ce. See
template<class T> using Matrix4x3 = RectangularMatrix<4, 3, T>;
#endif
/** @relates RectangularMatrix
@brief Multiply a scalar with a matrix
Same as @ref RectangularMatrix::operator*(T) const.
*/
template<std::size_t cols, std::size_t rows, class T> inline RectangularMatrix<cols, rows, T> operator*(
#ifdef DOXYGEN_GENERATING_OUTPUT
T
#else
typename std::common_type<T>::type
#endif
scalar, const RectangularMatrix<cols, rows, T>& matrix)
{
return matrix*scalar;
}
/** @relates RectangularMatrix
@brief Divide a matrix with a scalar and invert
The computation is done column-wise. @f[
\boldsymbol B_j = \frac a {\boldsymbol A_j}
@f]
@see @ref RectangularMatrix::operator/(T) const
*/
template<std::size_t cols, std::size_t rows, class T> inline RectangularMatrix<cols, rows, T> operator/(
#ifdef DOXYGEN_GENERATING_OUTPUT
T
#else
typename std::common_type<T>::type
#endif
scalar, const RectangularMatrix<cols, rows, T>& matrix)
{
RectangularMatrix<cols, rows, T> out{Magnum::NoInit};
for(std::size_t i = 0; i != cols; ++i)
out[i] = scalar/matrix[i];
return out;
}
/** @relates RectangularMatrix
@brief Multiply a vector with a rectangular matrix
@ -785,6 +785,9 @@ extern template MAGNUM_EXPORT Debug& operator<<(Debug&, const RectangularMatrix<
return Math::RectangularMatrix<cols, rows, T>::flippedRows(); \
} \
/* Unlike with Vector, these are kept non-member and non-friend as it'd mean
too many macro combinations otherwise */
#define MAGNUM_MATRIX_OPERATOR_IMPLEMENTATION(...) \
template<std::size_t size, class T> inline __VA_ARGS__ operator*(typename std::common_type<T>::type number, const __VA_ARGS__& matrix) { \
return number*static_cast<const Math::RectangularMatrix<size, size, T>&>(matrix); \

2
src/Magnum/Math/Test/VectorTest.cpp

@ -734,8 +734,6 @@ template<class T> class BasicVec2: public Math::Vector<2, T> {
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(2, BasicVec2)
};
MAGNUM_VECTORn_OPERATOR_IMPLEMENTATION(2, BasicVec2)
typedef BasicVec2<Float> Vec2;
typedef BasicVec2<Int> Vec2i;

1123
src/Magnum/Math/Vector.h

File diff suppressed because it is too large Load Diff

4
src/Magnum/Math/Vector2.h

@ -235,10 +235,6 @@ template<class T> class Vector2: public Vector<2, T> {
template<class U> friend U cross(const Vector2<U>&, const Vector2<U>&);
};
#ifndef DOXYGEN_GENERATING_OUTPUT
MAGNUM_VECTORn_OPERATOR_IMPLEMENTATION(2, Vector2)
#endif
namespace Implementation {
template<std::size_t, class> struct TypeForSize;
template<class T> struct TypeForSize<2, T> { typedef Math::Vector2<typename T::Type> Type; };

4
src/Magnum/Math/Vector3.h

@ -262,10 +262,6 @@ template<class T> class Vector3: public Vector<3, T> {
template<class U> friend Vector3<U> cross(const Vector3<U>&, const Vector3<U>&);
};
#ifndef DOXYGEN_GENERATING_OUTPUT
MAGNUM_VECTORn_OPERATOR_IMPLEMENTATION(3, Vector3)
#endif
#ifndef MAGNUM_NO_MATH_STRICT_WEAK_ORDERING
namespace Implementation {
template<class T> struct TypeForSize<3, T> { typedef Math::Vector3<typename T::Type> Type; };

4
src/Magnum/Math/Vector4.h

@ -284,10 +284,6 @@ template<class T> Vector4<T> planeEquation(const Vector3<T>& normal, const Vecto
return {normal, -Math::dot(normal, point)};
}
#ifndef DOXYGEN_GENERATING_OUTPUT
MAGNUM_VECTORn_OPERATOR_IMPLEMENTATION(4, Vector4)
#endif
#ifndef MAGNUM_NO_MATH_STRICT_WEAK_ORDERING
namespace Implementation {
template<class T> struct TypeForSize<4, T> { typedef Math::Vector4<typename T::Type> Type; };

Loading…
Cancel
Save