From 297d102200f39886836af852603a7da0d1c4f146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 8 Mar 2013 16:56:23 +0100 Subject: [PATCH] Math: ability to convert Vector types from/to external types. --- src/Math/Test/Vector2Test.cpp | 34 ++++++++++++++++++++++++++++++++- src/Math/Test/Vector3Test.cpp | 35 +++++++++++++++++++++++++++++++++- src/Math/Test/Vector4Test.cpp | 36 ++++++++++++++++++++++++++++++++++- src/Math/Test/VectorTest.cpp | 35 +++++++++++++++++++++++++++++++++- src/Math/Vector.h | 14 ++++++++++++++ src/Math/Vector2.h | 3 +++ src/Math/Vector3.h | 3 +++ src/Math/Vector4.h | 3 +++ 8 files changed, 159 insertions(+), 4 deletions(-) diff --git a/src/Math/Test/Vector2Test.cpp b/src/Math/Test/Vector2Test.cpp index 3367b9814..1d0b4b17d 100644 --- a/src/Math/Test/Vector2Test.cpp +++ b/src/Math/Test/Vector2Test.cpp @@ -19,7 +19,27 @@ #include "Math/Vector2.h" -namespace Magnum { namespace Math { namespace Test { +struct Vec2 { + float x, y; +}; + +namespace Magnum { namespace Math { + +namespace Implementation { + +template<> struct VectorConverter<2, float, Vec2> { + inline constexpr static Vector<2, float> from(const Vec2& other) { + return {other.x, other.y}; + } + + inline constexpr static Vec2 to(const Vector<2, float>& other) { + return {other[0], other[1]}; + } +}; + +} + +namespace Test { class Vector2Test: public Corrade::TestSuite::Tester { public: @@ -31,6 +51,8 @@ class Vector2Test: public Corrade::TestSuite::Tester { void constructConversion(); void constructCopy(); + void convert(); + void access(); void axes(); void scales(); @@ -49,6 +71,8 @@ Vector2Test::Vector2Test() { &Vector2Test::constructConversion, &Vector2Test::constructCopy, + &Vector2Test::convert, + &Vector2Test::access, &Vector2Test::axes, &Vector2Test::scales, @@ -92,6 +116,14 @@ void Vector2Test::constructCopy() { CORRADE_COMPARE(b, Vector2(1.5f, 2.5f)); } +void Vector2Test::convert() { + Vec2 a{1.5f, 2.0f}; + Vector2 b(1.5f, 2.0f); + CORRADE_COMPARE(Vector2(a), b); + CORRADE_COMPARE(Vec2(b).x, a.x); + CORRADE_COMPARE(Vec2(b).y, a.y); +} + void Vector2Test::access() { Vector2 vec(1.0f, -2.0f); CORRADE_COMPARE(vec.x(), 1.0f); diff --git a/src/Math/Test/Vector3Test.cpp b/src/Math/Test/Vector3Test.cpp index b046c9b32..76dc9b863 100644 --- a/src/Math/Test/Vector3Test.cpp +++ b/src/Math/Test/Vector3Test.cpp @@ -19,7 +19,27 @@ #include "Math/Vector3.h" -namespace Magnum { namespace Math { namespace Test { +struct Vec3 { + float x, y, z; +}; + +namespace Magnum { namespace Math { + +namespace Implementation { + +template<> struct VectorConverter<3, float, Vec3> { + inline constexpr static Vector<3, Float> from(const Vec3& other) { + return {other.x, other.y, other.z}; + } + + inline constexpr static Vec3 to(const Vector<3, Float>& other) { + return {other[0], other[1], other[2]}; + } +}; + +} + +namespace Test { class Vector3Test: public Corrade::TestSuite::Tester { public: @@ -32,6 +52,8 @@ class Vector3Test: public Corrade::TestSuite::Tester { void constructConversion(); void constructCopy(); + void convert(); + void access(); void cross(); void axes(); @@ -54,6 +76,8 @@ Vector3Test::Vector3Test() { &Vector3Test::constructConversion, &Vector3Test::constructCopy, + &Vector3Test::convert, + &Vector3Test::access, &Vector3Test::cross, &Vector3Test::axes, @@ -105,6 +129,15 @@ void Vector3Test::constructCopy() { CORRADE_COMPARE(b, Vector3(1.0f, 2.5f, -3.0f)); } +void Vector3Test::convert() { + Vec3 a{1.5f, 2.0f, -3.5f}; + Vector3 b(1.5f, 2.0f, -3.5f); + CORRADE_COMPARE(Vector3(a), b); + CORRADE_COMPARE(Vec3(b).x, a.x); + CORRADE_COMPARE(Vec3(b).y, a.y); + CORRADE_COMPARE(Vec3(b).z, a.z); +} + void Vector3Test::access() { Vector3 vec(1.0f, -2.0f, 5.0f); CORRADE_COMPARE(vec.x(), 1.0f); diff --git a/src/Math/Test/Vector4Test.cpp b/src/Math/Test/Vector4Test.cpp index df5407ff8..53467e4a4 100644 --- a/src/Math/Test/Vector4Test.cpp +++ b/src/Math/Test/Vector4Test.cpp @@ -19,7 +19,27 @@ #include "Math/Vector4.h" -namespace Magnum { namespace Math { namespace Test { +struct Vec4 { + float x, y, z, w; +}; + +namespace Magnum { namespace Math { + +namespace Implementation { + +template<> struct VectorConverter<4, float, Vec4> { + inline constexpr static Vector<4, Float> from(const Vec4& other) { + return {other.x, other.y, other.z, other.w}; + } + + inline constexpr static Vec4 to(const Vector<4, Float>& other) { + return {other[0], other[1], other[2], other[3]}; + } +}; + +} + +namespace Test { class Vector4Test: public Corrade::TestSuite::Tester { public: @@ -32,6 +52,8 @@ class Vector4Test: public Corrade::TestSuite::Tester { void constructConversion(); void constructCopy(); + void convert(); + void access(); void threeComponent(); void twoComponent(); @@ -53,6 +75,8 @@ Vector4Test::Vector4Test() { &Vector4Test::constructConversion, &Vector4Test::constructCopy, + &Vector4Test::convert, + &Vector4Test::access, &Vector4Test::threeComponent, &Vector4Test::twoComponent, @@ -102,6 +126,16 @@ void Vector4Test::constructCopy() { CORRADE_COMPARE(b, Vector4(1.0f, -2.5f, 3.0f, 4.1f)); } +void Vector4Test::convert() { + Vec4 a{1.5f, 2.0f, -3.5f, -0.5f}; + Vector4 b(1.5f, 2.0f, -3.5f, -0.5f); + CORRADE_COMPARE(Vector4(a), b); + CORRADE_COMPARE(Vec4(b).x, a.x); + CORRADE_COMPARE(Vec4(b).y, a.y); + CORRADE_COMPARE(Vec4(b).z, a.z); + CORRADE_COMPARE(Vec4(b).w, a.w); +} + void Vector4Test::access() { Vector4 vec(1.0f, -2.0f, 5.0f, 0.5f); CORRADE_COMPARE(vec.x(), 1.0f); diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index 5086c0de8..b874e64da 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -19,7 +19,27 @@ #include "Math/Vector.h" -namespace Magnum { namespace Math { namespace Test { +struct Vec3 { + float x, y, z; +}; + +namespace Magnum { namespace Math { + +namespace Implementation { + +template<> struct VectorConverter<3, float, Vec3> { + inline static Vector<3, Float> from(const Vec3& other) { + return {other.x, other.y, other.z}; + } + + inline static Vec3 to(const Vector<3, Float>& other) { + return {other[0], other[1], other[2]}; + } +}; + +} + +namespace Test { class VectorTest: public Corrade::TestSuite::Tester { public: @@ -32,6 +52,8 @@ class VectorTest: public Corrade::TestSuite::Tester { void constructOneComponent(); void constructConversion(); void constructCopy(); + + void convert(); void data(); void negative(); @@ -75,6 +97,8 @@ VectorTest::VectorTest() { &VectorTest::constructOneComponent, &VectorTest::constructConversion, &VectorTest::constructCopy, + + &VectorTest::convert, &VectorTest::data, &VectorTest::negative, @@ -155,6 +179,15 @@ void VectorTest::constructCopy() { CORRADE_COMPARE(b, Vector4(1.0f, 3.5f, 4.0f, -2.7f)); } +void VectorTest::convert() { + Vec3 a{1.5f, 2.0f, -3.5f}; + Vector3 b(1.5f, 2.0f, -3.5f); + CORRADE_COMPARE(Vector3(a), b); + CORRADE_COMPARE(Vec3(b).x, a.x); + CORRADE_COMPARE(Vec3(b).y, a.y); + CORRADE_COMPARE(Vec3(b).z, a.z); +} + void VectorTest::data() { Vector4 vector(4.0f, 5.0f, 6.0f, 7.0f); vector[2] = 1.0f; diff --git a/src/Math/Vector.h b/src/Math/Vector.h index d496abcce..37be813b6 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -33,6 +33,12 @@ namespace Magnum { namespace Math { +#ifndef DOXYGEN_GENERATING_OUTPUT +namespace Implementation { + template struct VectorConverter; +} +#endif + /** @brief %Vector @tparam size %Vector size @@ -146,12 +152,20 @@ template class Vector { } #endif + /** @brief Construct vector from external representation */ + template::from(std::declval()))> inline constexpr explicit Vector(const U& other): Vector(Implementation::VectorConverter::from(other)) {} + /** @brief Copy constructor */ inline constexpr Vector(const Vector&) = default; /** @brief Assignment operator */ inline Vector& operator=(const Vector&) = default; + /** @brief Convert vector to external representation */ + template::to(std::declval>()))> inline constexpr explicit operator U() const { + return Implementation::VectorConverter::to(*this); + } + /** * @brief Raw data * @return One-dimensional array of `size*size` length. diff --git a/src/Math/Vector2.h b/src/Math/Vector2.h index 397d9bc16..8abef7731 100644 --- a/src/Math/Vector2.h +++ b/src/Math/Vector2.h @@ -89,6 +89,9 @@ template class Vector2: public Vector<2, T> { /** @copydoc Vector::Vector(const Vector&) */ template inline constexpr explicit Vector2(const Vector<2, U>& other): Vector<2, T>(other) {} + /** @brief Construct vector from external representation */ + template::from(std::declval()))> inline constexpr explicit Vector2(const U& other): Vector<2, T>(Implementation::VectorConverter<2, T, U>::from(other)) {} + /** @brief Copy constructor */ inline constexpr Vector2(const Vector<2, T>& other): Vector<2, T>(other) {} diff --git a/src/Math/Vector3.h b/src/Math/Vector3.h index 60a116a9b..ff268d8b7 100644 --- a/src/Math/Vector3.h +++ b/src/Math/Vector3.h @@ -130,6 +130,9 @@ template class Vector3: public Vector<3, T> { /** @copydoc Vector::Vector(const Vector&) */ template inline constexpr explicit Vector3(const Vector<3, U>& other): Vector<3, T>(other) {} + /** @brief Construct vector from external representation */ + template::from(std::declval()))> inline constexpr explicit Vector3(const U& other): Vector<3, T>(Implementation::VectorConverter<3, T, U>::from(other)) {} + /** @brief Copy constructor */ inline constexpr Vector3(const Vector<3, T>& other): Vector<3, T>(other) {} diff --git a/src/Math/Vector4.h b/src/Math/Vector4.h index 5a4fa4f09..7e36c86de 100644 --- a/src/Math/Vector4.h +++ b/src/Math/Vector4.h @@ -60,6 +60,9 @@ template class Vector4: public Vector<4, T> { /** @copydoc Vector::Vector(const Vector&) */ template inline constexpr explicit Vector4(const Vector<4, U>& other): Vector<4, T>(other) {} + /** @brief Construct vector from external representation */ + template::from(std::declval()))> inline constexpr explicit Vector4(const U& other): Vector<4, T>(Implementation::VectorConverter<4, T, U>::from(other)) {} + /** @brief Copy constructor */ inline constexpr Vector4(const Vector<4, T>& other): Vector<4, T>(other) {}