From a385441c02a7c01558b7ce1043165aeb296515d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 17 Sep 2012 15:09:21 +0200 Subject: [PATCH] Moved type conversion constructor from Vector to RectangularMatrix. --- src/Math/RectangularMatrix.h | 25 ++++++++++++++++++++++++ src/Math/Test/RectangularMatrixTest.cpp | 12 ++++++++++++ src/Math/Test/RectangularMatrixTest.h | 1 + src/Math/Test/VectorTest.cpp | 11 ----------- src/Math/Test/VectorTest.h | 1 - src/Math/Vector.h | 26 ------------------------- 6 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/Math/RectangularMatrix.h b/src/Math/RectangularMatrix.h index ef195df05..044574fa5 100644 --- a/src/Math/RectangularMatrix.h +++ b/src/Math/RectangularMatrix.h @@ -28,6 +28,8 @@ namespace Magnum { namespace Math { +template class RectangularMatrix; + #ifndef DOXYGEN_GENERATING_OUTPUT namespace Implementation { template struct Sequence {}; @@ -39,6 +41,11 @@ namespace Implementation { template struct GenerateSequence<0, sequence...> { typedef Sequence Type; }; + + /* Implementation for RectangularMatrix::from(const RectangularMatrix&) */ + template inline constexpr Math::RectangularMatrix rectangularMatrixFrom(Sequence, const Math::RectangularMatrix& matrix) { + return {T(matrix.data()[sequence])...}; + } } #endif @@ -89,6 +96,21 @@ template class RectangularMatrix { return from(typename Implementation::GenerateSequence::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 inline constexpr static RectangularMatrix from(const RectangularMatrix& other) { + return Implementation::rectangularMatrixFrom(typename Implementation::GenerateSequence::Type(), other); + } + /** @brief Zero-filled matrix constructor */ inline constexpr RectangularMatrix(): _data() {} @@ -383,6 +405,9 @@ template Corrade::Utility::Debug operator<<(C } \ template inline constexpr static __VA_ARGS__ from(const Math::Vector& first, const U&... next) { \ return Math::RectangularMatrix::from(first, next...); \ + } \ + template inline constexpr static RectangularMatrix from(const Math::RectangularMatrix& other) { \ + return Math::RectangularMatrix::from(other); \ } \ \ inline __VA_ARGS__& operator=(const Math::RectangularMatrix& other) { \ diff --git a/src/Math/Test/RectangularMatrixTest.cpp b/src/Math/Test/RectangularMatrixTest.cpp index da0762179..b575941c7 100644 --- a/src/Math/Test/RectangularMatrixTest.cpp +++ b/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; diff --git a/src/Math/Test/RectangularMatrixTest.h b/src/Math/Test/RectangularMatrixTest.h index 0eae0204a..469762177 100644 --- a/src/Math/Test/RectangularMatrixTest.h +++ b/src/Math/Test/RectangularMatrixTest.h @@ -25,6 +25,7 @@ class RectangularMatrixTest: public Corrade::TestSuite::Tester 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); } diff --git a/src/Math/Test/VectorTest.h b/src/Math/Test/VectorTest.h index e31af0aa0..1af4b7122 100644 --- a/src/Math/Test/VectorTest.h +++ b/src/Math/Test/VectorTest.h @@ -24,7 +24,6 @@ class VectorTest: public Corrade::TestSuite::Tester { VectorTest(); void construct(); - void constructFrom(); void dot(); void multiplyDivideComponentWise(); void dotSelf(); diff --git a/src/Math/Vector.h b/src/Math/Vector.h index 27d0c587a..e427c6098 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -23,17 +23,6 @@ namespace Magnum { namespace Math { -template class Vector; - -#ifndef DOXYGEN_GENERATING_OUTPUT -namespace Implementation { - /* Implementation for Vector::from(const Vector&) */ - template inline constexpr Math::Vector vectorFrom(Sequence, const Math::Vector& vector) { - return {T(vector[sequence])...}; - } -} -#endif - /** @brief %Vector @tparam s %Vector size @@ -46,21 +35,6 @@ template 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 inline constexpr static Vector from(const Vector& other) { - return Implementation::vectorFrom(typename Implementation::GenerateSequence::Type(), other); - } - /** * @brief Dot product *