diff --git a/src/Math/Complex.h b/src/Math/Complex.h index cdd512df7..1950a85c8 100644 --- a/src/Math/Complex.h +++ b/src/Math/Complex.h @@ -24,6 +24,7 @@ #include #include "Math/Matrix.h" +#include "Math/Vector2.h" namespace Magnum { namespace Math { @@ -94,6 +95,16 @@ template class Complex { */ inline constexpr /*implicit*/ Complex(T real, T imaginary = T(0)): _real(real), _imaginary(imaginary) {} + /** + * @brief Construct complex number from vector + * + * To be used in transformations later. @f[ + * c = v_x + iv_y + * @f] + * @see operator Vector2(), transformVector(), transformVectorNormalized() + */ + inline constexpr explicit Complex(const Vector2& vector): _real(vector.x()), _imaginary(vector.y()) {} + /** @brief Equality comparison */ inline bool operator==(const Complex& other) const { return MathTypeTraits::equals(_real, other._real) && @@ -111,6 +122,17 @@ template class Complex { /** @brief Imaginary part */ inline constexpr T imaginary() const { return _imaginary; } + /** + * @brief Convert complex number to vector + * + * @f[ + * \boldsymbol v = \begin{pmatrix} a \\ b \end{pmatrix} + * @f] + */ + inline constexpr explicit operator Vector2() const { + return {_real, _imaginary}; + } + /** * @brief Rotation angle of complex number * diff --git a/src/Math/Test/ComplexTest.cpp b/src/Math/Test/ComplexTest.cpp index c414a8f13..697b4a376 100644 --- a/src/Math/Test/ComplexTest.cpp +++ b/src/Math/Test/ComplexTest.cpp @@ -27,6 +27,7 @@ class ComplexTest: public Corrade::TestSuite::Tester { void construct(); void constructDefault(); + void constructFromVector(); void compare(); void constExpressions(); @@ -55,6 +56,7 @@ class ComplexTest: public Corrade::TestSuite::Tester { ComplexTest::ComplexTest() { addTests(&ComplexTest::construct, &ComplexTest::constructDefault, + &ComplexTest::constructFromVector, &ComplexTest::compare, &ComplexTest::constExpressions, @@ -100,6 +102,14 @@ void ComplexTest::constructDefault() { CORRADE_COMPARE(Complex().length(), 1.0f); } +void ComplexTest::constructFromVector() { + Vector2 vec(1.5f, -3.0f); + + Complex a(vec); + CORRADE_COMPARE(a, Complex(1.5f, -3.0f)); + CORRADE_COMPARE(Vector2(a), vec); +} + void ComplexTest::compare() { CORRADE_VERIFY(Complex(3.7f, -1.0f+MathTypeTraits::epsilon()/2) == Complex(3.7f, -1.0f)); CORRADE_VERIFY(Complex(3.7f, -1.0f+MathTypeTraits::epsilon()*2) != Complex(3.7f, -1.0f));