Browse Source

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

Still undocumented feature.
pull/107/head
Vladimír Vondruš 11 years ago
parent
commit
690a3ad408
  1. 10
      src/Magnum/Math/Complex.h
  2. 40
      src/Magnum/Math/Test/ComplexTest.cpp

10
src/Magnum/Math/Complex.h

@ -43,6 +43,8 @@ namespace Implementation {
template<class T> constexpr static Complex<T> complexFromMatrix(const Matrix2x2<T>& matrix) {
return {matrix[0][0], matrix[0][1]};
}
template<class, class> struct ComplexConverter;
}
/** @relatesalso Complex
@ -159,6 +161,14 @@ template<class T> class Complex {
*/
constexpr explicit Complex(const Vector2<T>& vector): _real(vector.x()), _imaginary(vector.y()) {}
/** @brief Construct complex number from external representation */
template<class U, class V = decltype(Implementation::ComplexConverter<T, U>::from(std::declval<U>()))> constexpr explicit Complex(const U& other): Complex{Implementation::ComplexConverter<T, U>::from(other)} {}
/** @brief Convert complex number to external representation */
template<class U, class V = decltype(Implementation::ComplexConverter<T, U>::to(std::declval<Complex<T>>()))> constexpr explicit operator U() const {
return Implementation::ComplexConverter<T, U>::to(*this);
}
/** @brief Equality comparison */
bool operator==(const Complex<T>& other) const {
return TypeTraits<T>::equals(_real, other._real) &&

40
src/Magnum/Math/Test/ComplexTest.cpp

@ -29,7 +29,27 @@
#include "Magnum/Math/Complex.h"
#include "Magnum/Math/Matrix3.h"
namespace Magnum { namespace Math { namespace Test {
struct Cmpl {
float re, im;
};
namespace Magnum { namespace Math {
namespace Implementation {
template<> struct ComplexConverter<Float, Cmpl> {
constexpr static Complex<Float> from(const Cmpl& other) {
return {other.re, other.im};
}
constexpr static Cmpl to(const Complex<Float>& other) {
return {other.real(), other.imaginary()};
}
};
}
namespace Test {
struct ComplexTest: Corrade::TestSuite::Tester {
explicit ComplexTest();
@ -38,6 +58,7 @@ struct ComplexTest: Corrade::TestSuite::Tester {
void constructDefault();
void constructFromVector();
void constructCopy();
void convert();
void compare();
void isNormalized();
@ -69,6 +90,7 @@ ComplexTest::ComplexTest() {
&ComplexTest::constructDefault,
&ComplexTest::constructFromVector,
&ComplexTest::constructCopy,
&ComplexTest::convert,
&ComplexTest::compare,
&ComplexTest::isNormalized,
@ -138,6 +160,22 @@ void ComplexTest::constructCopy() {
CORRADE_COMPARE(b, Complex(2.5f, -5.0f));
}
void ComplexTest::convert() {
constexpr Cmpl a{1.5f, -3.5f};
constexpr Complex b{1.5f, -3.5f};
constexpr Complex c(a);
CORRADE_COMPARE(c, b);
constexpr Cmpl d(b);
CORRADE_COMPARE(d.re, a.re);
CORRADE_COMPARE(d.im, a.im);
/* Implicit conversion is not allowed */
CORRADE_VERIFY(!(std::is_convertible<Cmpl, Complex>::value));
CORRADE_VERIFY(!(std::is_convertible<Complex, Cmpl>::value));
}
void ComplexTest::compare() {
CORRADE_VERIFY(Complex(3.7f, -1.0f+TypeTraits<Float>::epsilon()/2) == Complex(3.7f, -1.0f));
CORRADE_VERIFY(Complex(3.7f, -1.0f+TypeTraits<Float>::epsilon()*2) != Complex(3.7f, -1.0f));

Loading…
Cancel
Save