Browse Source

Math: ability to convert Vector types from/to external types.

pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
297d102200
  1. 34
      src/Math/Test/Vector2Test.cpp
  2. 35
      src/Math/Test/Vector3Test.cpp
  3. 36
      src/Math/Test/Vector4Test.cpp
  4. 35
      src/Math/Test/VectorTest.cpp
  5. 14
      src/Math/Vector.h
  6. 3
      src/Math/Vector2.h
  7. 3
      src/Math/Vector3.h
  8. 3
      src/Math/Vector4.h

34
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);

35
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);

36
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);

35
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;

14
src/Math/Vector.h

@ -33,6 +33,12 @@
namespace Magnum { namespace Math {
#ifndef DOXYGEN_GENERATING_OUTPUT
namespace Implementation {
template<std::size_t, class, class> struct VectorConverter;
}
#endif
/**
@brief %Vector
@tparam size %Vector size
@ -146,12 +152,20 @@ template<std::size_t size, class T> class Vector {
}
#endif
/** @brief Construct vector from external representation */
template<class U, class V = decltype(Implementation::VectorConverter<size, T, U>::from(std::declval<U>()))> inline constexpr explicit Vector(const U& other): Vector(Implementation::VectorConverter<size, T, U>::from(other)) {}
/** @brief Copy constructor */
inline constexpr Vector(const Vector<size, T>&) = default;
/** @brief Assignment operator */
inline Vector<size, T>& operator=(const Vector<size, T>&) = default;
/** @brief Convert vector to external representation */
template<class U, class V = decltype(Implementation::VectorConverter<size, T, U>::to(std::declval<Vector<size, T>>()))> inline constexpr explicit operator U() const {
return Implementation::VectorConverter<size, T, U>::to(*this);
}
/**
* @brief Raw data
* @return One-dimensional array of `size*size` length.

3
src/Math/Vector2.h

@ -89,6 +89,9 @@ template<class T> class Vector2: public Vector<2, T> {
/** @copydoc Vector::Vector(const Vector<size, U>&) */
template<class U> inline constexpr explicit Vector2(const Vector<2, U>& other): Vector<2, T>(other) {}
/** @brief Construct vector from external representation */
template<class U, class V = decltype(Implementation::VectorConverter<2, T, U>::from(std::declval<U>()))> 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) {}

3
src/Math/Vector3.h

@ -130,6 +130,9 @@ template<class T> class Vector3: public Vector<3, T> {
/** @copydoc Vector::Vector(const Vector<size, U>&) */
template<class U> inline constexpr explicit Vector3(const Vector<3, U>& other): Vector<3, T>(other) {}
/** @brief Construct vector from external representation */
template<class U, class V = decltype(Implementation::VectorConverter<3, T, U>::from(std::declval<U>()))> 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) {}

3
src/Math/Vector4.h

@ -60,6 +60,9 @@ template<class T> class Vector4: public Vector<4, T> {
/** @copydoc Vector::Vector(const Vector<size, U>&) */
template<class U> inline constexpr explicit Vector4(const Vector<4, U>& other): Vector<4, T>(other) {}
/** @brief Construct vector from external representation */
template<class U, class V = decltype(Implementation::VectorConverter<4, T, U>::from(std::declval<U>()))> 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) {}

Loading…
Cancel
Save