mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
454 lines
14 KiB
454 lines
14 KiB
|
13 years ago
|
#ifndef Magnum_Math_Complex_h
|
||
|
|
#define Magnum_Math_Complex_h
|
||
|
|
/*
|
||
|
|
This file is part of Magnum.
|
||
|
|
|
||
|
13 years ago
|
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
|
||
|
|
|
||
|
|
Permission is hereby granted, free of charge, to any person obtaining a
|
||
|
|
copy of this software and associated documentation files (the "Software"),
|
||
|
|
to deal in the Software without restriction, including without limitation
|
||
|
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||
|
|
and/or sell copies of the Software, and to permit persons to whom the
|
||
|
|
Software is furnished to do so, subject to the following conditions:
|
||
|
|
|
||
|
|
The above copyright notice and this permission notice shall be included
|
||
|
|
in all copies or substantial portions of the Software.
|
||
|
|
|
||
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||
|
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||
|
|
DEALINGS IN THE SOFTWARE.
|
||
|
13 years ago
|
*/
|
||
|
|
|
||
|
|
/** @file
|
||
|
|
* @brief Class Magnum::Math::Complex
|
||
|
|
*/
|
||
|
|
|
||
|
13 years ago
|
#include <limits>
|
||
|
|
#include <Utility/Assert.h>
|
||
|
13 years ago
|
#include <Utility/Debug.h>
|
||
|
|
|
||
|
13 years ago
|
#include "Math/Matrix.h"
|
||
|
13 years ago
|
#include "Math/Vector2.h"
|
||
|
13 years ago
|
|
||
|
|
namespace Magnum { namespace Math {
|
||
|
|
|
||
|
13 years ago
|
namespace Implementation {
|
||
|
13 years ago
|
/* No assertions fired, for internal use. Not private member because used
|
||
|
|
from outside the class. */
|
||
|
13 years ago
|
template<class T> constexpr static Complex<T> complexFromMatrix(const Matrix<2, T>& matrix) {
|
||
|
13 years ago
|
return {matrix[0][0], matrix[0][1]};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
@brief %Complex number
|
||
|
|
@tparam T Data type
|
||
|
|
|
||
|
13 years ago
|
Represents 2D rotation. See @ref transformations for brief introduction.
|
||
|
13 years ago
|
@see Magnum::Complex, Magnum::Complexd, Matrix3
|
||
|
13 years ago
|
*/
|
||
|
|
template<class T> class Complex {
|
||
|
|
public:
|
||
|
|
typedef T Type; /**< @brief Underlying data type */
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Dot product
|
||
|
|
*
|
||
|
|
* @f[
|
||
|
13 years ago
|
* c_0 \cdot c_1 = a_0 a_1 + b_0 b_1
|
||
|
13 years ago
|
* @f]
|
||
|
|
* @see dot() const
|
||
|
|
*/
|
||
|
13 years ago
|
static T dot(const Complex<T>& a, const Complex<T>& b) {
|
||
|
13 years ago
|
return a._real*b._real + a._imaginary*b._imaginary;
|
||
|
13 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Angle between normalized complex numbers
|
||
|
|
*
|
||
|
|
* Expects that both complex numbers are normalized. @f[
|
||
|
|
* \theta = acos \left( \frac{Re(c_0 \cdot c_1))}{|c_0| |c_1|} \right) = acos (a_0 a_1 + b_0 b_1)
|
||
|
|
* @f]
|
||
|
13 years ago
|
* @see isNormalized(), Quaternion::angle(), Vector::angle()
|
||
|
13 years ago
|
*/
|
||
|
13 years ago
|
static Rad<T> angle(const Complex<T>& normalizedA, const Complex<T>& normalizedB) {
|
||
|
13 years ago
|
CORRADE_ASSERT(normalizedA.isNormalized() && normalizedB.isNormalized(),
|
||
|
13 years ago
|
"Math::Complex::angle(): complex numbers must be normalized", Rad<T>(std::numeric_limits<T>::quiet_NaN()));
|
||
|
|
return Rad<T>(std::acos(normalizedA._real*normalizedB._real + normalizedA._imaginary*normalizedB._imaginary));
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Rotation complex number
|
||
|
|
* @param angle Rotation angle (counterclockwise)
|
||
|
|
*
|
||
|
|
* @f[
|
||
|
|
* c = cos \theta + i sin \theta
|
||
|
|
* @f]
|
||
|
13 years ago
|
* @see angle(), Matrix3::rotation(), Quaternion::rotation()
|
||
|
13 years ago
|
*/
|
||
|
13 years ago
|
static Complex<T> rotation(Rad<T> angle) {
|
||
|
13 years ago
|
return {std::cos(T(angle)), std::sin(T(angle))};
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Create complex number from rotation matrix
|
||
|
|
*
|
||
|
|
* Expects that the matrix is orthogonal (i.e. pure rotation).
|
||
|
|
* @see toMatrix(), DualComplex::fromMatrix(), Matrix::isOrthogonal()
|
||
|
|
*/
|
||
|
13 years ago
|
static Complex<T> fromMatrix(const Matrix<2, T>& matrix) {
|
||
|
13 years ago
|
CORRADE_ASSERT(matrix.isOrthogonal(),
|
||
|
|
"Math::Complex::fromMatrix(): the matrix is not orthogonal", {});
|
||
|
|
return Implementation::complexFromMatrix(matrix);
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Default constructor
|
||
|
|
*
|
||
|
13 years ago
|
* Constructs unit complex number. @f[
|
||
|
|
* c = 1 + i0
|
||
|
13 years ago
|
* @f]
|
||
|
|
*/
|
||
|
13 years ago
|
constexpr /*implicit*/ Complex(): _real(T(1)), _imaginary(T(0)) {}
|
||
|
13 years ago
|
|
||
|
|
/**
|
||
|
13 years ago
|
* @brief Construct complex number from real and imaginary part
|
||
|
13 years ago
|
*
|
||
|
|
* @f[
|
||
|
|
* c = a + ib
|
||
|
|
* @f]
|
||
|
|
*/
|
||
|
13 years ago
|
constexpr /*implicit*/ Complex(T real, T imaginary): _real(real), _imaginary(imaginary) {}
|
||
|
13 years ago
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Construct complex number from vector
|
||
|
|
*
|
||
|
|
* To be used in transformations later. @f[
|
||
|
|
* c = v_x + iv_y
|
||
|
|
* @f]
|
||
|
13 years ago
|
* @see operator Vector2(), transformVector()
|
||
|
13 years ago
|
*/
|
||
|
13 years ago
|
constexpr explicit Complex(const Vector2<T>& vector): _real(vector.x()), _imaginary(vector.y()) {}
|
||
|
13 years ago
|
|
||
|
13 years ago
|
/** @brief Equality comparison */
|
||
|
13 years ago
|
bool operator==(const Complex<T>& other) const {
|
||
|
13 years ago
|
return TypeTraits<T>::equals(_real, other._real) &&
|
||
|
|
TypeTraits<T>::equals(_imaginary, other._imaginary);
|
||
|
13 years ago
|
}
|
||
|
|
|
||
|
|
/** @brief Non-equality comparison */
|
||
|
13 years ago
|
bool operator!=(const Complex<T>& other) const {
|
||
|
13 years ago
|
return !operator==(other);
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Whether the complex number is normalized
|
||
|
|
*
|
||
|
|
* Complex number is normalized if it has unit length: @f[
|
||
|
13 years ago
|
* |c \cdot c - 1| < 2 \epsilon + \epsilon^2 \cong 2 \epsilon
|
||
|
13 years ago
|
* @f]
|
||
|
|
* @see dot(), normalized()
|
||
|
|
*/
|
||
|
13 years ago
|
bool isNormalized() const {
|
||
|
13 years ago
|
return Implementation::isNormalizedSquared(dot());
|
||
|
13 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
/** @brief Real part */
|
||
|
13 years ago
|
constexpr T real() const { return _real; }
|
||
|
13 years ago
|
|
||
|
|
/** @brief Imaginary part */
|
||
|
13 years ago
|
constexpr T imaginary() const { return _imaginary; }
|
||
|
13 years ago
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Convert complex number to vector
|
||
|
|
*
|
||
|
|
* @f[
|
||
|
|
* \boldsymbol v = \begin{pmatrix} a \\ b \end{pmatrix}
|
||
|
|
* @f]
|
||
|
|
*/
|
||
|
13 years ago
|
constexpr explicit operator Vector2<T>() const {
|
||
|
13 years ago
|
return {_real, _imaginary};
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Rotation angle of complex number
|
||
|
|
*
|
||
|
|
* @f[
|
||
|
|
* \theta = atan2(b, a)
|
||
|
|
* @f]
|
||
|
13 years ago
|
* @see rotation()
|
||
|
13 years ago
|
*/
|
||
|
13 years ago
|
Rad<T> angle() const {
|
||
|
13 years ago
|
return Rad<T>(std::atan2(_imaginary, _real));
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Convert complex number to rotation matrix
|
||
|
|
*
|
||
|
|
* @f[
|
||
|
|
* M = \begin{pmatrix}
|
||
|
|
* a & -b \\
|
||
|
|
* b & a
|
||
|
|
* \end{pmatrix}
|
||
|
|
* @f]
|
||
|
13 years ago
|
* @see fromMatrix(), DualComplex::toMatrix(),
|
||
|
|
* Matrix3::from(const Matrix<2, T>&, const Vector2<T>&)
|
||
|
13 years ago
|
*/
|
||
|
13 years ago
|
Matrix<2, T> toMatrix() const {
|
||
|
13 years ago
|
return {Vector<2, T>(_real, _imaginary),
|
||
|
|
Vector<2, T>(-_imaginary, _real)};
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
13 years ago
|
* @brief Add complex number and assign
|
||
|
13 years ago
|
*
|
||
|
|
* The computation is done in-place. @f[
|
||
|
13 years ago
|
* c_0 + c_1 = (a_0 + a_1) + i(b_0 + b_1)
|
||
|
13 years ago
|
* @f]
|
||
|
|
*/
|
||
|
13 years ago
|
Complex<T>& operator+=(const Complex<T>& other) {
|
||
|
13 years ago
|
_real += other._real;
|
||
|
|
_imaginary += other._imaginary;
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
13 years ago
|
* @brief Add complex number
|
||
|
13 years ago
|
*
|
||
|
|
* @see operator+=()
|
||
|
|
*/
|
||
|
13 years ago
|
Complex<T> operator+(const Complex<T>& other) const {
|
||
|
13 years ago
|
return Complex<T>(*this) += other;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
13 years ago
|
* @brief Negated complex number
|
||
|
13 years ago
|
*
|
||
|
|
* @f[
|
||
|
|
* -c = -a -ib
|
||
|
|
* @f]
|
||
|
|
*/
|
||
|
13 years ago
|
Complex<T> operator-() const {
|
||
|
13 years ago
|
return {-_real, -_imaginary};
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
13 years ago
|
* @brief Subtract complex number and assign
|
||
|
13 years ago
|
*
|
||
|
|
* The computation is done in-place. @f[
|
||
|
13 years ago
|
* c_0 - c_1 = (a_0 - a_1) + i(b_0 - b_1)
|
||
|
13 years ago
|
* @f]
|
||
|
|
*/
|
||
|
13 years ago
|
Complex<T>& operator-=(const Complex<T>& other) {
|
||
|
13 years ago
|
_real -= other._real;
|
||
|
|
_imaginary -= other._imaginary;
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
13 years ago
|
* @brief Subtract complex number
|
||
|
13 years ago
|
*
|
||
|
|
* @see operator-=()
|
||
|
|
*/
|
||
|
13 years ago
|
Complex<T> operator-(const Complex<T>& other) const {
|
||
|
13 years ago
|
return Complex<T>(*this) -= other;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Multiply with scalar and assign
|
||
|
|
*
|
||
|
|
* The computation is done in-place. @f[
|
||
|
|
* c \cdot t = ta + itb
|
||
|
|
* @f]
|
||
|
|
*/
|
||
|
13 years ago
|
Complex<T>& operator*=(T scalar) {
|
||
|
13 years ago
|
_real *= scalar;
|
||
|
|
_imaginary *= scalar;
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Multiply with scalar
|
||
|
|
*
|
||
|
|
* @see operator*=(T)
|
||
|
|
*/
|
||
|
13 years ago
|
Complex<T> operator*(T scalar) const {
|
||
|
13 years ago
|
return Complex<T>(*this) *= scalar;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Divide with scalar and assign
|
||
|
|
*
|
||
|
|
* The computation is done in-place. @f[
|
||
|
|
* \frac c t = \frac a t + i \frac b t
|
||
|
|
* @f]
|
||
|
|
*/
|
||
|
13 years ago
|
Complex<T>& operator/=(T scalar) {
|
||
|
13 years ago
|
_real /= scalar;
|
||
|
|
_imaginary /= scalar;
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Divide with scalar
|
||
|
|
*
|
||
|
|
* @see operator/=(T)
|
||
|
|
*/
|
||
|
13 years ago
|
Complex<T> operator/(T scalar) const {
|
||
|
13 years ago
|
return Complex<T>(*this) /= scalar;
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Multiply with complex number
|
||
|
|
*
|
||
|
|
* @f[
|
||
|
|
* c_0 c_1 = (a_0 + ib_0)(a_1 + ib_1) = (a_0 a_1 - b_0 b_1) + i(a_1 b_0 + a_0 b_1)
|
||
|
|
* @f]
|
||
|
|
*/
|
||
|
13 years ago
|
Complex<T> operator*(const Complex<T>& other) const {
|
||
|
13 years ago
|
return {_real*other._real - _imaginary*other._imaginary,
|
||
|
|
_imaginary*other._real + _real*other._imaginary};
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Dot product of the complex number
|
||
|
|
*
|
||
|
|
* Should be used instead of length() for comparing complex number length
|
||
|
|
* with other values, because it doesn't compute the square root. @f[
|
||
|
13 years ago
|
* c \cdot c = a^2 + b^2
|
||
|
13 years ago
|
* @f]
|
||
|
13 years ago
|
* @see dot(const Complex&, const Complex&), isNormalized()
|
||
|
13 years ago
|
*/
|
||
|
13 years ago
|
T dot() const {
|
||
|
13 years ago
|
return dot(*this, *this);
|
||
|
13 years ago
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief %Complex number length
|
||
|
|
*
|
||
|
|
* See also dot() const which is faster for comparing length with other
|
||
|
|
* values. @f[
|
||
|
13 years ago
|
* |c| = \sqrt{c \cdot c}
|
||
|
13 years ago
|
* @f]
|
||
|
13 years ago
|
* @see isNormalized()
|
||
|
13 years ago
|
*/
|
||
|
13 years ago
|
T length() const {
|
||
|
13 years ago
|
/** @todo Remove when NaCl's newlib has this fixed */
|
||
|
|
#ifndef CORRADE_TARGET_NACL_NEWLIB
|
||
|
13 years ago
|
return std::hypot(_real, _imaginary);
|
||
|
13 years ago
|
#else
|
||
|
|
return std::sqrt(dot());
|
||
|
|
#endif
|
||
|
13 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Normalized complex number (of unit length)
|
||
|
|
*
|
||
|
|
* @see isNormalized()
|
||
|
|
*/
|
||
|
13 years ago
|
Complex<T> normalized() const {
|
||
|
13 years ago
|
return (*this)/length();
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Conjugated complex number
|
||
|
|
*
|
||
|
|
* @f[
|
||
|
13 years ago
|
* c^* = a - ib
|
||
|
13 years ago
|
* @f]
|
||
|
|
*/
|
||
|
13 years ago
|
Complex<T> conjugated() const {
|
||
|
13 years ago
|
return {_real, -_imaginary};
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Inverted complex number
|
||
|
|
*
|
||
|
|
* See invertedNormalized() which is faster for normalized
|
||
|
|
* complex numbers. @f[
|
||
|
13 years ago
|
* c^{-1} = \frac{c^*}{|c|^2} = \frac{c^*}{c \cdot c}
|
||
|
13 years ago
|
* @f]
|
||
|
|
*/
|
||
|
13 years ago
|
Complex<T> inverted() const {
|
||
|
13 years ago
|
return conjugated()/dot();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Inverted normalized complex number
|
||
|
|
*
|
||
|
|
* Equivalent to conjugated(). Expects that the complex number is
|
||
|
|
* normalized. @f[
|
||
|
13 years ago
|
* c^{-1} = \frac{c^*}{c \cdot c} = c^*
|
||
|
13 years ago
|
* @f]
|
||
|
13 years ago
|
* @see isNormalized(), inverted()
|
||
|
13 years ago
|
*/
|
||
|
13 years ago
|
Complex<T> invertedNormalized() const {
|
||
|
13 years ago
|
CORRADE_ASSERT(isNormalized(),
|
||
|
13 years ago
|
"Math::Complex::invertedNormalized(): complex number must be normalized",
|
||
|
13 years ago
|
Complex<T>(std::numeric_limits<T>::quiet_NaN(), {}));
|
||
|
13 years ago
|
return conjugated();
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Rotate vector with complex number
|
||
|
|
*
|
||
|
13 years ago
|
* @f[
|
||
|
|
* v' = c v = c (v_x + iv_y)
|
||
|
13 years ago
|
* @f]
|
||
|
|
* @see Complex(const Vector2&), operator Vector2(), Matrix3::transformVector()
|
||
|
|
*/
|
||
|
13 years ago
|
Vector2<T> transformVector(const Vector2<T>& vector) const {
|
||
|
13 years ago
|
return Vector2<T>((*this)*Complex<T>(vector));
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
private:
|
||
|
|
T _real, _imaginary;
|
||
|
|
};
|
||
|
|
|
||
|
|
/** @relates Complex
|
||
|
|
@brief Multiply scalar with complex
|
||
|
|
|
||
|
|
Same as Complex::operator*(T) const.
|
||
|
|
*/
|
||
|
|
template<class T> inline Complex<T> operator*(T scalar, const Complex<T>& complex) {
|
||
|
|
return complex*scalar;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @relates Complex
|
||
|
|
@brief Divide complex with number and invert
|
||
|
|
|
||
|
|
@f[
|
||
|
|
\frac t c = \frac t a + i \frac t b
|
||
|
|
@f]
|
||
|
|
@see Complex::operator/()
|
||
|
|
*/
|
||
|
|
template<class T> inline Complex<T> operator/(T scalar, const Complex<T>& complex) {
|
||
|
|
return {scalar/complex.real(), scalar/complex.imaginary()};
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @debugoperator{Magnum::Math::Complex} */
|
||
|
|
template<class T> Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Complex<T>& value) {
|
||
|
|
debug << "Complex(";
|
||
|
|
debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, false);
|
||
|
|
debug << value.real() << ", " << value.imaginary() << ")";
|
||
|
|
debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, true);
|
||
|
|
return debug;
|
||
|
|
}
|
||
|
|
|
||
|
|
/* Explicit instantiation for commonly used types */
|
||
|
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
||
|
13 years ago
|
extern template Corrade::Utility::Debug MAGNUM_EXPORT operator<<(Corrade::Utility::Debug, const Complex<Float>&);
|
||
|
13 years ago
|
#ifndef MAGNUM_TARGET_GLES
|
||
|
13 years ago
|
extern template Corrade::Utility::Debug MAGNUM_EXPORT operator<<(Corrade::Utility::Debug, const Complex<Double>&);
|
||
|
13 years ago
|
#endif
|
||
|
|
#endif
|
||
|
|
|
||
|
|
}}
|
||
|
|
|
||
|
|
#endif
|