2016-12-05 05:44:42 +08:00
|
|
|
#[cfg(feature = "arbitrary")]
|
|
|
|
use quickcheck::{Arbitrary, Gen};
|
|
|
|
|
|
|
|
use num::One;
|
|
|
|
use num_complex::Complex;
|
2018-10-22 13:00:10 +08:00
|
|
|
use rand::distributions::{Distribution, OpenClosed01, Standard};
|
2018-05-23 05:58:14 +08:00
|
|
|
use rand::Rng;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
use alga::general::Real;
|
2018-05-19 23:15:15 +08:00
|
|
|
use base::dimension::{U1, U2};
|
|
|
|
use base::storage::Storage;
|
2018-12-29 19:12:56 +08:00
|
|
|
use base::{Unit, Vector};
|
2018-11-05 17:34:58 +08:00
|
|
|
use geometry::{Rotation2, UnitComplex};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
impl<N: Real> UnitComplex<N> {
|
|
|
|
/// The unit complex number multiplicative identity.
|
2018-11-05 17:34:58 +08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # use nalgebra::UnitComplex;
|
|
|
|
/// let rot1 = UnitComplex::identity();
|
|
|
|
/// let rot2 = UnitComplex::new(1.7);
|
|
|
|
///
|
|
|
|
/// assert_eq!(rot1 * rot2, rot2);
|
|
|
|
/// assert_eq!(rot2 * rot1, rot2);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn identity() -> Self {
|
|
|
|
Self::new_unchecked(Complex::new(N::one(), N::zero()))
|
|
|
|
}
|
|
|
|
|
2018-11-05 17:34:58 +08:00
|
|
|
/// Builds the unit complex number corresponding to the rotation with the given angle.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use std::f32;
|
|
|
|
/// # use nalgebra::{UnitComplex, Point2};
|
|
|
|
/// let rot = UnitComplex::new(f32::consts::FRAC_PI_2);
|
|
|
|
///
|
|
|
|
/// assert_relative_eq!(rot * Point2::new(3.0, 4.0), Point2::new(-4.0, 3.0));
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn new(angle: N) -> Self {
|
2017-08-03 01:37:44 +08:00
|
|
|
let (sin, cos) = angle.sin_cos();
|
|
|
|
Self::from_cos_sin_unchecked(cos, sin)
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds the unit complex number corresponding to the rotation with the angle.
|
|
|
|
///
|
|
|
|
/// Same as `Self::new(angle)`.
|
2018-11-05 17:34:58 +08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use std::f32;
|
|
|
|
/// # use nalgebra::{UnitComplex, Point2};
|
|
|
|
/// let rot = UnitComplex::from_angle(f32::consts::FRAC_PI_2);
|
|
|
|
///
|
|
|
|
/// assert_relative_eq!(rot * Point2::new(3.0, 4.0), Point2::new(-4.0, 3.0));
|
|
|
|
/// ```
|
|
|
|
// FIXME: deprecate this.
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn from_angle(angle: N) -> Self {
|
|
|
|
Self::new(angle)
|
|
|
|
}
|
|
|
|
|
2018-09-24 12:48:42 +08:00
|
|
|
/// Builds the unit complex number from the sinus and cosinus of the rotation angle.
|
2017-08-03 01:37:44 +08:00
|
|
|
///
|
2018-11-05 17:34:58 +08:00
|
|
|
/// The input values are not checked to actually be cosines and sine of the same value.
|
|
|
|
/// Is is generally preferable to use the `::new(angle)` constructor instead.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use std::f32;
|
|
|
|
/// # use nalgebra::{UnitComplex, Vector2, Point2};
|
|
|
|
/// let angle = f32::consts::FRAC_PI_2;
|
|
|
|
/// let rot = UnitComplex::from_cos_sin_unchecked(angle.cos(), angle.sin());
|
|
|
|
///
|
|
|
|
/// assert_relative_eq!(rot * Point2::new(3.0, 4.0), Point2::new(-4.0, 3.0));
|
|
|
|
/// ```
|
2017-08-03 01:37:44 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn from_cos_sin_unchecked(cos: N, sin: N) -> Self {
|
|
|
|
UnitComplex::new_unchecked(Complex::new(cos, sin))
|
|
|
|
}
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Builds a unit complex rotation from an angle in radian wrapped in a 1-dimensional vector.
|
|
|
|
///
|
2018-11-05 17:34:58 +08:00
|
|
|
/// This is generally used in the context of generic programming. Using
|
|
|
|
/// the `::new(angle)` method instead is more common.
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2018-11-05 17:34:58 +08:00
|
|
|
pub fn from_scaled_axis<SB: Storage<N, U1>>(axisangle: Vector<N, U1, SB>) -> Self {
|
2016-12-05 05:44:42 +08:00
|
|
|
Self::from_angle(axisangle[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new unit complex number from a complex number.
|
|
|
|
///
|
|
|
|
/// The input complex number will be normalized.
|
|
|
|
#[inline]
|
|
|
|
pub fn from_complex(q: Complex<N>) -> Self {
|
2017-08-03 01:37:44 +08:00
|
|
|
Self::from_complex_and_get(q).0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new unit complex number from a complex number.
|
|
|
|
///
|
2018-11-05 17:34:58 +08:00
|
|
|
/// The input complex number will be normalized. Returns the norm of the complex number as well.
|
2017-08-03 01:37:44 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn from_complex_and_get(q: Complex<N>) -> (Self, N) {
|
|
|
|
let norm = (q.im * q.im + q.re * q.re).sqrt();
|
|
|
|
(Self::new_unchecked(q / norm), norm)
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds the unit complex number from the corresponding 2D rotation matrix.
|
2018-11-05 17:34:58 +08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # use nalgebra::{Rotation2, UnitComplex};
|
|
|
|
/// let rot = Rotation2::new(1.7);
|
|
|
|
/// let complex = UnitComplex::from_rotation_matrix(&rot);
|
|
|
|
/// assert_eq!(complex, UnitComplex::new(1.7));
|
|
|
|
/// ```
|
|
|
|
// FIXME: add UnitComplex::from(...) instead?
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2018-11-05 17:34:58 +08:00
|
|
|
pub fn from_rotation_matrix(rotmat: &Rotation2<N>) -> Self {
|
2016-12-05 05:44:42 +08:00
|
|
|
Self::new_unchecked(Complex::new(rotmat[(0, 0)], rotmat[(1, 0)]))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The unit complex needed to make `a` and `b` be collinear and point toward the same
|
|
|
|
/// direction.
|
2018-11-05 17:34:58 +08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::{Vector2, UnitComplex};
|
|
|
|
/// let a = Vector2::new(1.0, 2.0);
|
|
|
|
/// let b = Vector2::new(2.0, 1.0);
|
|
|
|
/// let rot = UnitComplex::rotation_between(&a, &b);
|
|
|
|
/// assert_relative_eq!(rot * a, b);
|
|
|
|
/// assert_relative_eq!(rot.inverse() * b, a);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn rotation_between<SB, SC>(a: &Vector<N, U2, SB>, b: &Vector<N, U2, SC>) -> Self
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2018-11-05 17:34:58 +08:00
|
|
|
SB: Storage<N, U2>,
|
|
|
|
SC: Storage<N, U2>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
Self::scaled_rotation_between(a, b, N::one())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The smallest rotation needed to make `a` and `b` collinear and point toward the same
|
|
|
|
/// direction, raised to the power `s`.
|
2018-11-05 17:34:58 +08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::{Vector2, UnitComplex};
|
|
|
|
/// let a = Vector2::new(1.0, 2.0);
|
|
|
|
/// let b = Vector2::new(2.0, 1.0);
|
|
|
|
/// let rot2 = UnitComplex::scaled_rotation_between(&a, &b, 0.2);
|
|
|
|
/// let rot5 = UnitComplex::scaled_rotation_between(&a, &b, 0.5);
|
|
|
|
/// assert_relative_eq!(rot2 * rot2 * rot2 * rot2 * rot2 * a, b, epsilon = 1.0e-6);
|
|
|
|
/// assert_relative_eq!(rot5 * rot5 * a, b, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2018-02-02 19:26:35 +08:00
|
|
|
pub fn scaled_rotation_between<SB, SC>(
|
|
|
|
a: &Vector<N, U2, SB>,
|
|
|
|
b: &Vector<N, U2, SC>,
|
|
|
|
s: N,
|
|
|
|
) -> Self
|
|
|
|
where
|
2018-11-05 17:34:58 +08:00
|
|
|
SB: Storage<N, U2>,
|
|
|
|
SC: Storage<N, U2>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2018-03-09 00:30:59 +08:00
|
|
|
// FIXME: code duplication with Rotation.
|
|
|
|
if let (Some(na), Some(nb)) = (
|
|
|
|
Unit::try_new(a.clone_owned(), N::zero()),
|
|
|
|
Unit::try_new(b.clone_owned(), N::zero()),
|
|
|
|
) {
|
|
|
|
Self::scaled_rotation_between_axis(&na, &nb, s)
|
2018-02-02 19:26:35 +08:00
|
|
|
} else {
|
2016-12-05 05:44:42 +08:00
|
|
|
Self::identity()
|
|
|
|
}
|
|
|
|
}
|
2018-03-09 00:30:59 +08:00
|
|
|
|
|
|
|
/// The unit complex needed to make `a` and `b` be collinear and point toward the same
|
|
|
|
/// direction.
|
2018-11-05 17:34:58 +08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::{Unit, Vector2, UnitComplex};
|
|
|
|
/// let a = Unit::new_normalize(Vector2::new(1.0, 2.0));
|
|
|
|
/// let b = Unit::new_normalize(Vector2::new(2.0, 1.0));
|
|
|
|
/// let rot = UnitComplex::rotation_between_axis(&a, &b);
|
|
|
|
/// assert_relative_eq!(rot * a, b);
|
|
|
|
/// assert_relative_eq!(rot.inverse() * b, a);
|
|
|
|
/// ```
|
2018-03-09 00:30:59 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn rotation_between_axis<SB, SC>(
|
|
|
|
a: &Unit<Vector<N, U2, SB>>,
|
|
|
|
b: &Unit<Vector<N, U2, SC>>,
|
|
|
|
) -> Self
|
|
|
|
where
|
|
|
|
SB: Storage<N, U2>,
|
|
|
|
SC: Storage<N, U2>,
|
|
|
|
{
|
|
|
|
Self::scaled_rotation_between_axis(a, b, N::one())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The smallest rotation needed to make `a` and `b` collinear and point toward the same
|
|
|
|
/// direction, raised to the power `s`.
|
2018-11-05 17:34:58 +08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::{Unit, Vector2, UnitComplex};
|
|
|
|
/// let a = Unit::new_normalize(Vector2::new(1.0, 2.0));
|
|
|
|
/// let b = Unit::new_normalize(Vector2::new(2.0, 1.0));
|
|
|
|
/// let rot2 = UnitComplex::scaled_rotation_between_axis(&a, &b, 0.2);
|
|
|
|
/// let rot5 = UnitComplex::scaled_rotation_between_axis(&a, &b, 0.5);
|
|
|
|
/// assert_relative_eq!(rot2 * rot2 * rot2 * rot2 * rot2 * a, b, epsilon = 1.0e-6);
|
|
|
|
/// assert_relative_eq!(rot5 * rot5 * a, b, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2018-03-09 00:30:59 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn scaled_rotation_between_axis<SB, SC>(
|
|
|
|
na: &Unit<Vector<N, U2, SB>>,
|
|
|
|
nb: &Unit<Vector<N, U2, SC>>,
|
|
|
|
s: N,
|
|
|
|
) -> Self
|
|
|
|
where
|
|
|
|
SB: Storage<N, U2>,
|
|
|
|
SC: Storage<N, U2>,
|
|
|
|
{
|
|
|
|
let sang = na.perp(&nb);
|
|
|
|
let cang = na.dot(&nb);
|
|
|
|
|
|
|
|
Self::from_angle(sang.atan2(cang) * s)
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<N: Real> One for UnitComplex<N> {
|
|
|
|
#[inline]
|
|
|
|
fn one() -> Self {
|
|
|
|
Self::identity()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 05:58:14 +08:00
|
|
|
impl<N: Real> Distribution<UnitComplex<N>> for Standard
|
2018-10-22 13:00:10 +08:00
|
|
|
where OpenClosed01: Distribution<N>
|
2018-05-23 05:58:14 +08:00
|
|
|
{
|
2018-07-08 07:03:08 +08:00
|
|
|
/// Generate a uniformly distributed random `UnitComplex`.
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2018-05-23 05:58:14 +08:00
|
|
|
fn sample<'a, R: Rng + ?Sized>(&self, rng: &mut R) -> UnitComplex<N> {
|
2018-07-08 07:03:08 +08:00
|
|
|
UnitComplex::from_angle(rng.sample(OpenClosed01) * N::two_pi())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
#[cfg(feature = "arbitrary")]
|
2016-12-05 05:44:42 +08:00
|
|
|
impl<N: Real + Arbitrary> Arbitrary for UnitComplex<N> {
|
|
|
|
#[inline]
|
|
|
|
fn arbitrary<G: Gen>(g: &mut G) -> Self {
|
|
|
|
UnitComplex::from_angle(N::arbitrary(g))
|
|
|
|
}
|
|
|
|
}
|