use num::Zero; use num_complex::Complex; use alga::general::{Real, SubsetOf, SupersetOf}; use alga::linear::Rotation as AlgaRotation; use base::dimension::U2; use base::Matrix3; use geometry::{ Isometry, Point2, Rotation2, Similarity, SuperTCategoryOf, TAffine, Transform, Translation, UnitComplex, }; /* * This file provides the following conversions: * ============================================= * * UnitComplex -> UnitComplex * UnitComplex -> Rotation * UnitComplex -> Isometry * UnitComplex -> Similarity * UnitComplex -> Transform * UnitComplex -> Matrix (homogeneous) * * NOTE: * UnitComplex -> Complex is already provided by: Unit -> T */ impl SubsetOf> for UnitComplex where N1: Real, N2: Real + SupersetOf, { #[inline] fn to_superset(&self) -> UnitComplex { UnitComplex::new_unchecked(self.as_ref().to_superset()) } #[inline] fn is_in_subset(uq: &UnitComplex) -> bool { ::is_convertible::<_, Complex>(uq.as_ref()) } #[inline] unsafe fn from_superset_unchecked(uq: &UnitComplex) -> Self { Self::new_unchecked(::convert_ref_unchecked(uq.as_ref())) } } impl SubsetOf> for UnitComplex where N1: Real, N2: Real + SupersetOf, { #[inline] fn to_superset(&self) -> Rotation2 { let q: UnitComplex = self.to_superset(); q.to_rotation_matrix().to_superset() } #[inline] fn is_in_subset(rot: &Rotation2) -> bool { ::is_convertible::<_, Rotation2>(rot) } #[inline] unsafe fn from_superset_unchecked(rot: &Rotation2) -> Self { let q = UnitComplex::::from_rotation_matrix(rot); ::convert_unchecked(q) } } impl SubsetOf> for UnitComplex where N1: Real, N2: Real + SupersetOf, R: AlgaRotation> + SupersetOf, { #[inline] fn to_superset(&self) -> Isometry { Isometry::from_parts(Translation::identity(), ::convert_ref(self)) } #[inline] fn is_in_subset(iso: &Isometry) -> bool { iso.translation.vector.is_zero() } #[inline] unsafe fn from_superset_unchecked(iso: &Isometry) -> Self { ::convert_ref_unchecked(&iso.rotation) } } impl SubsetOf> for UnitComplex where N1: Real, N2: Real + SupersetOf, R: AlgaRotation> + SupersetOf, { #[inline] fn to_superset(&self) -> Similarity { Similarity::from_isometry(::convert_ref(self), N2::one()) } #[inline] fn is_in_subset(sim: &Similarity) -> bool { sim.isometry.translation.vector.is_zero() && sim.scaling() == N2::one() } #[inline] unsafe fn from_superset_unchecked(sim: &Similarity) -> Self { ::convert_ref_unchecked(&sim.isometry) } } impl SubsetOf> for UnitComplex where N1: Real, N2: Real + SupersetOf, C: SuperTCategoryOf, { #[inline] fn to_superset(&self) -> Transform { Transform::from_matrix_unchecked(self.to_homogeneous().to_superset()) } #[inline] fn is_in_subset(t: &Transform) -> bool { >::is_in_subset(t.matrix()) } #[inline] unsafe fn from_superset_unchecked(t: &Transform) -> Self { Self::from_superset_unchecked(t.matrix()) } } impl> SubsetOf> for UnitComplex { #[inline] fn to_superset(&self) -> Matrix3 { self.to_homogeneous().to_superset() } #[inline] fn is_in_subset(m: &Matrix3) -> bool { ::is_convertible::<_, Rotation2>(m) } #[inline] unsafe fn from_superset_unchecked(m: &Matrix3) -> Self { let rot: Rotation2 = ::convert_ref_unchecked(m); Self::from_rotation_matrix(&rot) } }