use num::{One, Zero}; use simba::scalar::{RealField, SubsetOf, SupersetOf}; use simba::simd::PrimitiveSimdValue; use crate::base::allocator::Allocator; use crate::base::dimension::{DimNameAdd, DimNameSum, U1}; use crate::base::{Const, DefaultAllocator, DimName, OMatrix, OVector, SVector, Scalar}; use crate::geometry::{ AbstractRotation, Isometry, Similarity, SuperTCategoryOf, TAffine, Transform, Translation, Translation3, UnitDualQuaternion, UnitQuaternion, }; use crate::Point; /* * This file provides the following conversions: * ============================================= * * Translation -> Translation * Translation -> Isometry * Translation3 -> UnitDualQuaternion * Translation -> Similarity * Translation -> Transform * Translation -> Matrix (homogeneous) */ impl SubsetOf> for Translation where T2: SupersetOf, { #[inline] fn to_superset(&self) -> Translation { Translation::from(self.vector.to_superset()) } #[inline] fn is_in_subset(rot: &Translation) -> bool { crate::is_convertible::<_, SVector>(&rot.vector) } #[inline] fn from_superset_unchecked(rot: &Translation) -> Self { Translation { vector: rot.vector.to_subset_unchecked(), } } } impl SubsetOf> for Translation where T1: RealField, T2: RealField + SupersetOf, R: AbstractRotation, { #[inline] fn to_superset(&self) -> Isometry { Isometry::from_parts(self.to_superset(), R::identity()) } #[inline] fn is_in_subset(iso: &Isometry) -> bool { iso.rotation == R::identity() } #[inline] fn from_superset_unchecked(iso: &Isometry) -> Self { Self::from_superset_unchecked(&iso.translation) } } impl SubsetOf> for Translation3 where T1: RealField, T2: RealField + SupersetOf, { #[inline] fn to_superset(&self) -> UnitDualQuaternion { let dq = UnitDualQuaternion::::from_parts(*self, UnitQuaternion::identity()); dq.to_superset() } #[inline] fn is_in_subset(dq: &UnitDualQuaternion) -> bool { crate::is_convertible::<_, Translation>(&dq.translation()) && dq.rotation() == UnitQuaternion::identity() } #[inline] fn from_superset_unchecked(dq: &UnitDualQuaternion) -> Self { let dq: UnitDualQuaternion = crate::convert_ref_unchecked(dq); dq.translation() } } impl SubsetOf> for Translation where T1: RealField, T2: RealField + SupersetOf, R: AbstractRotation, { #[inline] fn to_superset(&self) -> Similarity { Similarity::from_parts(self.to_superset(), R::identity(), T2::one()) } #[inline] fn is_in_subset(sim: &Similarity) -> bool { sim.isometry.rotation == R::identity() && sim.scaling() == T2::one() } #[inline] fn from_superset_unchecked(sim: &Similarity) -> Self { Self::from_superset_unchecked(&sim.isometry.translation) } } impl SubsetOf> for Translation where T1: RealField, T2: RealField + SupersetOf, C: SuperTCategoryOf, Const: DimNameAdd, DefaultAllocator: Allocator, U1>, DimNameSum, U1>> + Allocator, U1>, DimNameSum, U1>>, { #[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 Translation where T1: RealField, T2: RealField + SupersetOf, Const: DimNameAdd, DefaultAllocator: Allocator, U1>, DimNameSum, U1>> + Allocator, U1>, DimNameSum, U1>>, // + Allocator // + Allocator { #[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 id = m.generic_slice((0, 0), (DimNameSum::, U1>::name(), Const::)); // Scalar types agree. m.iter().all(|e| SupersetOf::::is_in_subset(e)) && // The block part does nothing. id.is_identity(T2::zero()) && // The normalization factor is one. m[(D, D)] == T2::one() } #[inline] fn from_superset_unchecked( m: &OMatrix, U1>, DimNameSum, U1>>, ) -> Self { let t = m.fixed_slice::(0, D); Self { vector: crate::convert_unchecked(t.into_owned()), } } } impl From> for OMatrix, U1>, DimNameSum, U1>> where Const: DimNameAdd, DefaultAllocator: Allocator, U1>, DimNameSum, U1>> + Allocator>, { #[inline] fn from(t: Translation) -> Self { t.to_homogeneous() } } impl From>> for Translation { #[inline] fn from(vector: OVector>) -> Self { Translation { vector } } } impl From<[T; D]> for Translation { #[inline] fn from(coords: [T; D]) -> Self { Translation { vector: coords.into(), } } } impl From> for Translation { #[inline] fn from(pt: Point) -> Self { Translation { vector: pt.coords } } } impl From> for [T; D] { #[inline] fn from(t: Translation) -> Self { t.vector.into() } } impl From<[Translation; 2]> for Translation where T: From<[::Element; 2]>, T::Element: Scalar, { #[inline] fn from(arr: [Translation; 2]) -> Self { Self::from(OVector::from([ arr[0].vector.clone(), arr[1].vector.clone(), ])) } } impl From<[Translation; 4]> for Translation where T: From<[::Element; 4]>, T::Element: Scalar, { #[inline] fn from(arr: [Translation; 4]) -> Self { Self::from(OVector::from([ arr[0].vector.clone(), arr[1].vector.clone(), arr[2].vector.clone(), arr[3].vector.clone(), ])) } } impl From<[Translation; 8]> for Translation where T: From<[::Element; 8]>, T::Element: Scalar, { #[inline] fn from(arr: [Translation; 8]) -> Self { Self::from(OVector::from([ arr[0].vector.clone(), arr[1].vector.clone(), arr[2].vector.clone(), arr[3].vector.clone(), arr[4].vector.clone(), arr[5].vector.clone(), arr[6].vector.clone(), arr[7].vector.clone(), ])) } } impl From<[Translation; 16]> for Translation where T: From<[::Element; 16]>, T::Element: Scalar, { #[inline] fn from(arr: [Translation; 16]) -> Self { Self::from(OVector::from([ arr[0].vector.clone(), arr[1].vector.clone(), arr[2].vector.clone(), arr[3].vector.clone(), arr[4].vector.clone(), arr[5].vector.clone(), arr[6].vector.clone(), arr[7].vector.clone(), arr[8].vector.clone(), arr[9].vector.clone(), arr[10].vector.clone(), arr[11].vector.clone(), arr[12].vector.clone(), arr[13].vector.clone(), arr[14].vector.clone(), arr[15].vector.clone(), ])) } }