Browse Source

Moved type conversion constructor from Vector to RectangularMatrix.

vectorfields
Vladimír Vondruš 14 years ago
parent
commit
a385441c02
  1. 25
      src/Math/RectangularMatrix.h
  2. 12
      src/Math/Test/RectangularMatrixTest.cpp
  3. 1
      src/Math/Test/RectangularMatrixTest.h
  4. 11
      src/Math/Test/VectorTest.cpp
  5. 1
      src/Math/Test/VectorTest.h
  6. 26
      src/Math/Vector.h

25
src/Math/RectangularMatrix.h

@ -28,6 +28,8 @@
namespace Magnum { namespace Math {
template<size_t, size_t, class> class RectangularMatrix;
#ifndef DOXYGEN_GENERATING_OUTPUT
namespace Implementation {
template<size_t ...> struct Sequence {};
@ -39,6 +41,11 @@ namespace Implementation {
template<size_t ...sequence> struct GenerateSequence<0, sequence...> {
typedef Sequence<sequence...> Type;
};
/* Implementation for RectangularMatrix<cols, rows, T>::from(const RectangularMatrix<cols, rows, U>&) */
template<class T, class U, size_t c, size_t ...sequence> inline constexpr Math::RectangularMatrix<c, sizeof...(sequence)/c, T> rectangularMatrixFrom(Sequence<sequence...>, const Math::RectangularMatrix<c, sizeof...(sequence)/c, U>& matrix) {
return {T(matrix.data()[sequence])...};
}
}
#endif
@ -89,6 +96,21 @@ template<size_t c, size_t r, class T> class RectangularMatrix {
return from(typename Implementation::GenerateSequence<rows>::Type(), first, next...);
}
/**
* @brief %Matrix from another of different type
*
* Performs only default casting on the values, no rounding or
* anything else. Example usage:
* @code
* RectangularMatrix<4, 1, float> floatingPoint(1.3f, 2.7f, -15.0f, 7.0f);
* RectangularMatrix<4, 1, int> integral(RectangularMatrix<4, 1, int>::from(floatingPoint));
* // integral == {1, 2, -15, 7}
* @endcode
*/
template<class U> inline constexpr static RectangularMatrix<cols, rows, T> from(const RectangularMatrix<cols, rows, U>& other) {
return Implementation::rectangularMatrixFrom<T, U>(typename Implementation::GenerateSequence<cols*rows>::Type(), other);
}
/** @brief Zero-filled matrix constructor */
inline constexpr RectangularMatrix(): _data() {}
@ -383,6 +405,9 @@ template<size_t cols, size_t rows, class T> Corrade::Utility::Debug operator<<(C
} \
template<class ...U> inline constexpr static __VA_ARGS__ from(const Math::Vector<rows, T>& first, const U&... next) { \
return Math::RectangularMatrix<cols, rows, T>::from(first, next...); \
} \
template<class U> inline constexpr static RectangularMatrix<cols, rows, T> from(const Math::RectangularMatrix<cols, rows, U>& other) { \
return Math::RectangularMatrix<cols, rows, T>::from(other); \
} \
\
inline __VA_ARGS__& operator=(const Math::RectangularMatrix<cols, rows, T>& other) { \

12
src/Math/Test/RectangularMatrixTest.cpp

@ -29,11 +29,13 @@ namespace Magnum { namespace Math { namespace Test {
typedef RectangularMatrix<4, 3, float> Matrix4x3;
typedef RectangularMatrix<3, 4, float> Matrix3x4;
typedef RectangularMatrix<2, 2, float> Matrix2;
typedef RectangularMatrix<2, 2, int> Matrix2i;
typedef Vector<4, float> Vector4;
RectangularMatrixTest::RectangularMatrixTest() {
addTests(&RectangularMatrixTest::construct,
&RectangularMatrixTest::constructFromVectors,
&RectangularMatrixTest::constructFrom,
&RectangularMatrixTest::constructZero,
&RectangularMatrixTest::data,
@ -76,6 +78,16 @@ void RectangularMatrixTest::constructFromVectors() {
CORRADE_COMPARE(actual, expected);
}
void RectangularMatrixTest::constructFrom() {
Matrix2 floatingPoint(1.3f, 2.7f, -15.0f, 7.0f);
Matrix2 floatingPointRounded(1.0f, 2.0f, -15.0f, 7.0f);
Matrix2i integral(1, 2, -15, 7);
CORRADE_COMPARE(Matrix2i::from(floatingPoint), integral);
CORRADE_COMPARE(Matrix2::from(integral), floatingPointRounded);
}
void RectangularMatrixTest::constructZero() {
Matrix4x3 zero;

1
src/Math/Test/RectangularMatrixTest.h

@ -25,6 +25,7 @@ class RectangularMatrixTest: public Corrade::TestSuite::Tester<RectangularMatrix
void construct();
void constructFromVectors();
void constructFrom();
void constructZero();
void data();

11
src/Math/Test/VectorTest.cpp

@ -28,12 +28,10 @@ using namespace Corrade::Utility;
namespace Magnum { namespace Math { namespace Test {
typedef Vector<4, float> Vector4;
typedef Vector<4, int> Vector4i;
typedef Vector<3, float> Vector3;
VectorTest::VectorTest() {
addTests(&VectorTest::construct,
&VectorTest::constructFrom,
&VectorTest::dot,
&VectorTest::multiplyDivideComponentWise,
&VectorTest::dotSelf,
@ -55,15 +53,6 @@ void VectorTest::construct() {
CORRADE_COMPARE(Vector4::from(data), Vector4(1.0f, 2.0f, 3.0f, 4.0f));
}
void VectorTest::constructFrom() {
Vector4 floatingPoint(1.3f, 2.7f, -15.0f, 7.0f);
Vector4 floatingPointRounded(1.0f, 2.0f, -15.0f, 7.0f);
Vector4i integral(1, 2, -15, 7);
CORRADE_COMPARE(Vector4i::from(floatingPoint), integral);
CORRADE_COMPARE(Vector4::from(integral), floatingPointRounded);
}
void VectorTest::dot() {
CORRADE_COMPARE(Vector4::dot({1.0f, 0.5f, 0.75f, 1.5f}, {2.0f, 4.0f, 1.0f, 7.0f}), 15.25f);
}

1
src/Math/Test/VectorTest.h

@ -24,7 +24,6 @@ class VectorTest: public Corrade::TestSuite::Tester<VectorTest> {
VectorTest();
void construct();
void constructFrom();
void dot();
void multiplyDivideComponentWise();
void dotSelf();

26
src/Math/Vector.h

@ -23,17 +23,6 @@
namespace Magnum { namespace Math {
template<size_t size, class T> class Vector;
#ifndef DOXYGEN_GENERATING_OUTPUT
namespace Implementation {
/* Implementation for Vector<size, T>::from(const Vector<size, U>&) */
template<class T, class U, size_t ...sequence> inline constexpr Math::Vector<sizeof...(sequence), T> vectorFrom(Sequence<sequence...>, const Math::Vector<sizeof...(sequence), U>& vector) {
return {T(vector[sequence])...};
}
}
#endif
/**
@brief %Vector
@tparam s %Vector size
@ -46,21 +35,6 @@ template<size_t s, class T> class Vector: public RectangularMatrix<1, s, T> {
public:
const static size_t size = s; /**< @brief %Vector size */
/**
* @brief %Vector from another of different type
*
* Performs only default casting on the values, no rounding or
* anything else. Example usage:
* @code
* Vector<4, float> floatingPoint(1.3f, 2.7f, -15.0f, 7.0f);
* Vector<4, int> integral(Vector<4, int>::from(floatingPoint));
* // integral == {1, 2, -15, 7}
* @endcode
*/
template<class U> inline constexpr static Vector<size, T> from(const Vector<size, U>& other) {
return Implementation::vectorFrom<T, U>(typename Implementation::GenerateSequence<size>::Type(), other);
}
/**
* @brief Dot product
*

Loading…
Cancel
Save