use num::Zero; use num_complex::Complex; use simba::scalar::{RealField, SubsetOf, SupersetOf}; use simba::simd::{PrimitiveSimdValue, SimdRealField}; use crate::base::{Matrix2, Matrix3, Scalar}; use crate::geometry::{ AbstractRotation, Isometry, Rotation2, Similarity, SuperTCategoryOf, TAffine, Transform, Translation, UnitComplex, }; /* * This file provides the following conversions: * ============================================= * * UnitComplex -> UnitComplex * UnitComplex -> Rotation * UnitComplex -> Isometry<2> * UnitComplex -> Similarity<2> * UnitComplex -> Transform<2> * UnitComplex -> Matrix (homogeneous) * * NOTE: * UnitComplex -> Complex is already provided by: Unit -> T */ impl SubsetOf> for UnitComplex where T1: RealField, T2: RealField + SupersetOf, { #[inline] fn to_superset(&self) -> UnitComplex { UnitComplex::new_unchecked(self.as_ref().to_superset()) } #[inline] fn is_in_subset(uq: &UnitComplex) -> bool { crate::is_convertible::<_, Complex>(uq.as_ref()) } #[inline] fn from_superset_unchecked(uq: &UnitComplex) -> Self { Self::new_unchecked(crate::convert_ref_unchecked(uq.as_ref())) } } impl SubsetOf> for UnitComplex where T1: RealField, T2: RealField + 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 { crate::is_convertible::<_, Rotation2>(rot) } #[inline] fn from_superset_unchecked(rot: &Rotation2) -> Self { let q = UnitComplex::::from_rotation_matrix(rot); crate::convert_unchecked(q) } } impl SubsetOf> for UnitComplex where T1: RealField, T2: RealField + SupersetOf, R: AbstractRotation + SupersetOf, { #[inline] fn to_superset(&self) -> Isometry { Isometry::from_parts(Translation::identity(), crate::convert_ref(self)) } #[inline] fn is_in_subset(iso: &Isometry) -> bool { iso.translation.vector.is_zero() } #[inline] fn from_superset_unchecked(iso: &Isometry) -> Self { crate::convert_ref_unchecked(&iso.rotation) } } impl SubsetOf> for UnitComplex where T1: RealField, T2: RealField + SupersetOf, R: AbstractRotation + SupersetOf, { #[inline] fn to_superset(&self) -> Similarity { Similarity::from_isometry(crate::convert_ref(self), T2::one()) } #[inline] fn is_in_subset(sim: &Similarity) -> bool { sim.isometry.translation.vector.is_zero() && sim.scaling() == T2::one() } #[inline] fn from_superset_unchecked(sim: &Similarity) -> Self { crate::convert_ref_unchecked(&sim.isometry) } } impl SubsetOf> for UnitComplex where T1: RealField, T2: RealField + SupersetOf, C: SuperTCategoryOf, { #[inline] fn to_superset(&self) -> Transform { Transform::from_matrix_unchecked(self.clone().to_homogeneous().to_superset()) } #[inline] fn is_in_subset(t: &Transform) -> bool { >::is_in_subset(t.matrix()) } #[inline] fn from_superset_unchecked(t: &Transform) -> Self { Self::from_superset_unchecked(t.matrix()) } } impl> SubsetOf> for UnitComplex { #[inline] fn to_superset(&self) -> Matrix3 { self.clone().to_homogeneous().to_superset() } #[inline] fn is_in_subset(m: &Matrix3) -> bool { crate::is_convertible::<_, Rotation2>(m) } #[inline] fn from_superset_unchecked(m: &Matrix3) -> Self { let rot: Rotation2 = crate::convert_ref_unchecked(m); Self::from_rotation_matrix(&rot) } } impl From> for Rotation2 where T::Element: SimdRealField, { #[inline] fn from(q: UnitComplex) -> Self { q.to_rotation_matrix() } } impl From> for UnitComplex where T::Element: SimdRealField, { #[inline] fn from(q: Rotation2) -> Self { Self::from_rotation_matrix(&q) } } impl From> for Matrix3 where T::Element: SimdRealField, { #[inline] fn from(q: UnitComplex) -> Matrix3 { q.to_homogeneous() } } impl From> for Matrix2 where T::Element: SimdRealField, { #[inline] fn from(q: UnitComplex) -> Self { q.to_rotation_matrix().into_inner() } } impl From<[UnitComplex; 2]> for UnitComplex where T: From<[::Element; 2]>, T::Element: Scalar + Copy, { #[inline] fn from(arr: [UnitComplex; 2]) -> Self { Self::new_unchecked(Complex { re: T::from([arr[0].re, arr[1].re]), im: T::from([arr[0].im, arr[1].im]), }) } } impl From<[UnitComplex; 4]> for UnitComplex where T: From<[::Element; 4]>, T::Element: Scalar + Copy, { #[inline] fn from(arr: [UnitComplex; 4]) -> Self { Self::new_unchecked(Complex { re: T::from([arr[0].re, arr[1].re, arr[2].re, arr[3].re]), im: T::from([arr[0].im, arr[1].im, arr[2].im, arr[3].im]), }) } } impl From<[UnitComplex; 8]> for UnitComplex where T: From<[::Element; 8]>, T::Element: Scalar + Copy, { #[inline] fn from(arr: [UnitComplex; 8]) -> Self { Self::new_unchecked(Complex { re: T::from([ arr[0].re, arr[1].re, arr[2].re, arr[3].re, arr[4].re, arr[5].re, arr[6].re, arr[7].re, ]), im: T::from([ arr[0].im, arr[1].im, arr[2].im, arr[3].im, arr[4].im, arr[5].im, arr[6].im, arr[7].im, ]), }) } } impl From<[UnitComplex; 16]> for UnitComplex where T: From<[::Element; 16]>, T::Element: Scalar + Copy, { #[inline] fn from(arr: [UnitComplex; 16]) -> Self { Self::new_unchecked(Complex { re: T::from([ arr[0].re, arr[1].re, arr[2].re, arr[3].re, arr[4].re, arr[5].re, arr[6].re, arr[7].re, arr[8].re, arr[9].re, arr[10].re, arr[11].re, arr[12].re, arr[13].re, arr[14].re, arr[15].re, ]), im: T::from([ arr[0].im, arr[1].im, arr[2].im, arr[3].im, arr[4].im, arr[5].im, arr[6].im, arr[7].im, arr[8].im, arr[9].im, arr[10].im, arr[11].im, arr[12].im, arr[13].im, arr[14].im, arr[15].im, ]), }) } }