use num::Zero; use simba::scalar::{RealField, SubsetOf, SupersetOf}; use simba::simd::{PrimitiveSimdValue, SimdValue}; use crate::base::allocator::Allocator; use crate::base::dimension::{DimMin, DimNameAdd, DimNameSum, U1}; use crate::base::{Const, DefaultAllocator, Matrix2, Matrix3, Matrix4, OMatrix, SMatrix, Scalar}; use crate::geometry::{ AbstractRotation, Isometry, Rotation, Rotation2, Rotation3, Similarity, SuperTCategoryOf, TAffine, Transform, Translation, UnitComplex, UnitDualQuaternion, UnitQuaternion, }; /* * This file provides the following conversions: * ============================================= * * Rotation -> Rotation * Rotation3 -> UnitQuaternion * Rotation3 -> UnitDualQuaternion * Rotation2 -> UnitComplex * Rotation -> Isometry * Rotation -> Similarity * Rotation -> Transform * Rotation -> Matrix (homogeneous) */ impl SubsetOf> for Rotation where T1: RealField, T2: RealField + SupersetOf, { #[inline] fn to_superset(&self) -> Rotation { Rotation::from_matrix_unchecked(self.matrix().to_superset()) } #[inline] fn is_in_subset(rot: &Rotation) -> bool { crate::is_convertible::<_, SMatrix>(rot.matrix()) } #[inline] fn from_superset_unchecked(rot: &Rotation) -> Self { Rotation::from_matrix_unchecked(rot.matrix().to_subset_unchecked()) } } impl SubsetOf> for Rotation3 where T1: RealField, T2: RealField + SupersetOf, { #[inline] fn to_superset(&self) -> UnitQuaternion { let q = UnitQuaternion::::from_rotation_matrix(self); q.to_superset() } #[inline] fn is_in_subset(q: &UnitQuaternion) -> bool { crate::is_convertible::<_, UnitQuaternion>(q) } #[inline] fn from_superset_unchecked(q: &UnitQuaternion) -> Self { let q: UnitQuaternion = crate::convert_ref_unchecked(q); q.to_rotation_matrix() } } impl SubsetOf> for Rotation3 where T1: RealField, T2: RealField + SupersetOf, { #[inline] fn to_superset(&self) -> UnitDualQuaternion { let q = UnitQuaternion::::from_rotation_matrix(self); let dq = UnitDualQuaternion::from_rotation(q); dq.to_superset() } #[inline] fn is_in_subset(dq: &UnitDualQuaternion) -> bool { crate::is_convertible::<_, UnitQuaternion>(&dq.rotation()) && dq.translation().vector.is_zero() } #[inline] fn from_superset_unchecked(dq: &UnitDualQuaternion) -> Self { let dq: UnitDualQuaternion = crate::convert_ref_unchecked(dq); dq.rotation().to_rotation_matrix() } } impl SubsetOf> for Rotation2 where T1: RealField, T2: RealField + SupersetOf, { #[inline] fn to_superset(&self) -> UnitComplex { let q = UnitComplex::::from_rotation_matrix(self); q.to_superset() } #[inline] fn is_in_subset(q: &UnitComplex) -> bool { crate::is_convertible::<_, UnitComplex>(q) } #[inline] fn from_superset_unchecked(q: &UnitComplex) -> Self { let q: UnitComplex = crate::convert_ref_unchecked(q); q.to_rotation_matrix() } } impl SubsetOf> for Rotation 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 Rotation where T1: RealField, T2: RealField + SupersetOf, R: AbstractRotation + SupersetOf, { #[inline] fn to_superset(&self) -> Similarity { Similarity::from_parts(Translation::identity(), 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.rotation) } } impl SubsetOf> for Rotation where T1: RealField, T2: RealField + SupersetOf, C: SuperTCategoryOf, Const: DimNameAdd + DimMin, Output = Const>, // needed by .is_special_orthogonal() DefaultAllocator: Allocator, U1>, DimNameSum, U1>> + Allocator, U1>, DimNameSum, U1>>, // + Allocator<(usize, usize), D>, // Allocator // + Allocator { // needed by .is_special_orthogonal() #[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] fn from_superset_unchecked(t: &Transform) -> Self { Self::from_superset_unchecked(t.matrix()) } } impl SubsetOf, U1>, DimNameSum, U1>>> for Rotation where T1: RealField, T2: RealField + SupersetOf, Const: DimNameAdd + DimMin, Output = Const>, // needed by .is_special_orthogonal() DefaultAllocator: Allocator, U1>, DimNameSum, U1>> + Allocator, U1>, DimNameSum, U1>>, // + Allocator<(usize, usize), D>, // + Allocator // + Allocator { // needed by .is_special_orthogonal() #[inline] fn to_superset(&self) -> OMatrix, U1>, DimNameSum, U1>> { self.to_homogeneous().to_superset() } #[inline] fn is_in_subset(m: &OMatrix, U1>, DimNameSum, U1>>) -> bool { let rot = m.fixed_slice::(0, 0); let bottom = m.fixed_slice::<1, D>(D, 0); // Scalar types agree. m.iter().all(|e| SupersetOf::::is_in_subset(e)) && // The block part is a rotation. rot.is_special_orthogonal(T2::default_epsilon() * crate::convert(100.0)) && // The bottom row is (0, 0, ..., 1) bottom.iter().all(|e| e.is_zero()) && m[(D, D)] == T2::one() } #[inline] fn from_superset_unchecked( m: &OMatrix, U1>, DimNameSum, U1>>, ) -> Self { let r = m.fixed_slice::(0, 0); Self::from_matrix_unchecked(crate::convert_unchecked(r.into_owned())) } } impl From> for Matrix3 { #[inline] fn from(q: Rotation2) -> Self { q.to_homogeneous() } } impl From> for Matrix2 { #[inline] fn from(q: Rotation2) -> Self { q.into_inner() } } impl From> for Matrix4 { #[inline] fn from(q: Rotation3) -> Self { q.to_homogeneous() } } impl From> for Matrix3 { #[inline] fn from(q: Rotation3) -> Self { q.into_inner() } } impl From<[Rotation; 2]> for Rotation where T: From<[::Element; 2]>, T::Element: Scalar + Copy, { #[inline] fn from(arr: [Rotation; 2]) -> Self { Self::from_matrix_unchecked(OMatrix::from([arr[0].into_inner(), arr[1].into_inner()])) } } impl From<[Rotation; 4]> for Rotation where T: From<[::Element; 4]>, T::Element: Scalar + Copy, { #[inline] fn from(arr: [Rotation; 4]) -> Self { Self::from_matrix_unchecked(OMatrix::from([ arr[0].into_inner(), arr[1].into_inner(), arr[2].into_inner(), arr[3].into_inner(), ])) } } impl From<[Rotation; 8]> for Rotation where T: From<[::Element; 8]>, T::Element: Scalar + Copy, { #[inline] fn from(arr: [Rotation; 8]) -> Self { Self::from_matrix_unchecked(OMatrix::from([ arr[0].into_inner(), arr[1].into_inner(), arr[2].into_inner(), arr[3].into_inner(), arr[4].into_inner(), arr[5].into_inner(), arr[6].into_inner(), arr[7].into_inner(), ])) } } impl From<[Rotation; 16]> for Rotation where T: From<[::Element; 16]>, T::Element: Scalar + Copy, { #[inline] fn from(arr: [Rotation; 16]) -> Self { Self::from_matrix_unchecked(OMatrix::from([ arr[0].into_inner(), arr[1].into_inner(), arr[2].into_inner(), arr[3].into_inner(), arr[4].into_inner(), arr[5].into_inner(), arr[6].into_inner(), arr[7].into_inner(), arr[8].into_inner(), arr[9].into_inner(), arr[10].into_inner(), arr[11].into_inner(), arr[12].into_inner(), arr[13].into_inner(), arr[14].into_inner(), arr[15].into_inner(), ])) } }