Browse Source

Ability to construct Vector from another of different type.

vectorfields
Vladimír Vondruš 14 years ago
parent
commit
fec65aa6f5
  1. 11
      src/Math/Test/VectorTest.cpp
  2. 1
      src/Math/Test/VectorTest.h
  3. 25
      src/Math/Vector.h

11
src/Math/Test/VectorTest.cpp

@ -28,10 +28,12 @@ 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::data,
&VectorTest::copy,
&VectorTest::dot,
@ -56,6 +58,15 @@ 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::data() {
Vector4 v;
v[2] = 1.5f;

1
src/Math/Test/VectorTest.h

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

25
src/Math/Vector.h

@ -27,6 +27,8 @@
namespace Magnum { namespace Math {
template<size_t size, class T> class Vector;
#ifndef DOXYGEN_GENERATING_OUTPUT
namespace Implementation {
template<size_t ...> struct Sequence {};
@ -38,6 +40,11 @@ namespace Implementation {
template<size_t ...sequence> struct GenerateSequence<0, sequence...> {
typedef Sequence<sequence...> Type;
};
/* 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
@ -65,6 +72,21 @@ template<size_t size, class T> class Vector {
return *reinterpret_cast<const Vector<size, T>*>(data);
}
/**
* @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
*
@ -338,6 +360,9 @@ template<class T, size_t size> Corrade::Utility::Debug operator<<(Corrade::Utili
} \
inline constexpr static const Type<T>& from(const T* data) { \
return *reinterpret_cast<const Type<T>*>(data); \
} \
template<class U> inline constexpr static Type<T> from(const Vector<size, U>& other) { \
return Vector<size, T>::from(other); \
} \
\
inline Type<T>& operator=(const Type<T>& other) { \

Loading…
Cancel
Save