use std::fmt; use num_complex::Complex; use alga::general::Real; use core::{Unit, SquareMatrix, Vector1}; use core::dimension::{U2, U3}; use core::allocator::{OwnedAllocator, Allocator}; use core::storage::OwnedStorage; use geometry::{RotationBase, OwnedRotation}; /// A complex number with a norm equal to 1. pub type UnitComplex = Unit>; impl UnitComplex { /// The rotation angle in `]-pi; pi]` of this unit complex number. #[inline] pub fn angle(&self) -> N { self.complex().im.atan2(self.complex().re) } /// The rotation angle returned as a 1-dimensional vector. #[inline] pub fn scaled_axis(&self) -> Vector1 { Vector1::new(self.angle()) } /// The underlying complex number. /// /// Same as `self.as_ref()`. #[inline] pub fn complex(&self) -> &Complex { self.as_ref() } /// Compute the conjugate of this unit complex number. #[inline] pub fn conjugate(&self) -> Self { UnitComplex::new_unchecked(self.as_ref().conj()) } /// Inverts this complex number if it is not zero. #[inline] pub fn inverse(&self) -> Self { self.conjugate() } /// The rotation angle needed to make `self` and `other` coincide. #[inline] pub fn angle_to(&self, other: &Self) -> N { let delta = self.rotation_to(other); delta.angle() } /// The unit complex number needed to make `self` and `other` coincide. /// /// The result is such that: `self.rotation_to(other) * self == other`. #[inline] pub fn rotation_to(&self, other: &Self) -> Self { other / self } /// Compute in-place the conjugate of this unit complex number. #[inline] pub fn conjugate_mut(&mut self) { let me = self.as_mut_unchecked(); me.im = -me.im; } /// Inverts in-place this unit complex number. #[inline] pub fn inverse_mut(&mut self) { self.conjugate_mut() } /// Raise this unit complex number to a given floating power. /// /// This returns the unit complex number that identifies a rotation angle equal to /// `self.angle() × n`. #[inline] pub fn powf(&self, n: N) -> Self { Self::from_angle(self.angle() * n) } /// Builds the rotation matrix corresponding to this unit complex number. #[inline] pub fn to_rotation_matrix(&self) -> RotationBase where S: OwnedStorage, S::Alloc: OwnedAllocator { let r = self.complex().re; let i = self.complex().im; RotationBase::from_matrix_unchecked( SquareMatrix::<_, U2, _>::new( r, -i, i, r ) ) } /// Converts this unit complex number into its equivalent homogeneous transformation matrix. #[inline] pub fn to_homogeneous(&self) -> SquareMatrix where S: OwnedStorage, S::Alloc: OwnedAllocator + Allocator { let r: OwnedRotation = self.to_rotation_matrix(); r.to_homogeneous() } } impl fmt::Display for UnitComplex { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "UnitComplex angle: {}", self.angle()) } }