Browse Source

Math: ability to convert dual complex from/to external representation.

Still undocumented feature.
pull/107/head
Vladimír Vondruš 11 years ago
parent
commit
3afb810099
  1. 12
      src/Magnum/Math/DualComplex.h
  2. 42
      src/Magnum/Math/Test/DualComplexTest.cpp

12
src/Magnum/Math/DualComplex.h

@ -35,6 +35,10 @@
namespace Magnum { namespace Math {
namespace Implementation {
template<class, class> struct DualComplexConverter;
}
/**
@brief Dual complex number
@tparam T Underlying data type
@ -127,9 +131,17 @@ template<class T> class DualComplex: public Dual<Complex<T>> {
constexpr explicit DualComplex(const Vector2<T>& vector): Dual<Complex<T>>({}, Complex<T>(vector)) {}
#endif
/** @brief Construct dual complex number from external representation */
template<class U, class V = decltype(Implementation::DualComplexConverter<T, U>::from(std::declval<U>()))> constexpr explicit DualComplex(const U& other): DualComplex{Implementation::DualComplexConverter<T, U>::from(other)} {}
/** @brief Copy constructor */
constexpr DualComplex(const Dual<Complex<T>>& other): Dual<Complex<T>>(other) {}
/** @brief Convert dual complex number to external representation */
template<class U, class V = decltype(Implementation::DualComplexConverter<T, U>::to(std::declval<DualComplex<T>>()))> constexpr explicit operator U() const {
return Implementation::DualComplexConverter<T, U>::to(*this);
}
/**
* @brief Whether the dual complex number is normalized
*

42
src/Magnum/Math/Test/DualComplexTest.cpp

@ -29,7 +29,27 @@
#include "Magnum/Math/DualComplex.h"
#include "Magnum/Math/DualQuaternion.h"
namespace Magnum { namespace Math { namespace Test {
struct DualCmpl {
float re, im, x, y;
};
namespace Magnum { namespace Math {
namespace Implementation {
template<> struct DualComplexConverter<Float, DualCmpl> {
constexpr static DualComplex<Float> from(const DualCmpl& other) {
return {{other.re, other.im}, {other.x, other.y}};
}
constexpr static DualCmpl to(const DualComplex<Float>& other) {
return {other.real().real(), other.real().imaginary(), other.dual().real(), other.dual().imaginary() };
}
};
}
namespace Test {
struct DualComplexTest: Corrade::TestSuite::Tester {
explicit DualComplexTest();
@ -38,6 +58,7 @@ struct DualComplexTest: Corrade::TestSuite::Tester {
void constructDefault();
void constructFromVector();
void constructCopy();
void convert();
void isNormalized();
@ -75,6 +96,7 @@ DualComplexTest::DualComplexTest() {
&DualComplexTest::constructDefault,
&DualComplexTest::constructFromVector,
&DualComplexTest::constructCopy,
&DualComplexTest::convert,
&DualComplexTest::isNormalized,
@ -132,6 +154,24 @@ void DualComplexTest::constructCopy() {
CORRADE_COMPARE(b, DualComplex({-1.0f, 2.5f}, {3.0f, -7.5f}));
}
void DualComplexTest::convert() {
constexpr DualCmpl a{1.5f, -3.5f, 7.0f, -0.5f};
constexpr DualComplex b{{1.5f, -3.5f}, {7.0f, -0.5f}};
constexpr DualComplex c{a};
CORRADE_COMPARE(c, b);
constexpr DualCmpl d(b);
CORRADE_COMPARE(d.re, a.re);
CORRADE_COMPARE(d.im, a.im);
CORRADE_COMPARE(d.x, a.x);
CORRADE_COMPARE(d.y, a.y);
/* Implicit conversion is not allowed */
CORRADE_VERIFY(!(std::is_convertible<DualCmpl, DualComplex>::value));
CORRADE_VERIFY(!(std::is_convertible<DualComplex, DualCmpl>::value));
}
void DualComplexTest::isNormalized() {
CORRADE_VERIFY(!DualComplex({2.0f, 1.0f}, {}).isNormalized());
CORRADE_VERIFY((DualComplex::rotation(Deg(23.0f))*DualComplex::translation({6.0f, 3.0f})).isNormalized());

Loading…
Cancel
Save