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, MatrixN, Scalar, VectorN}; use crate::geometry::{ AbstractRotation, Isometry, Similarity, SuperTCategoryOf, TAffine, Transform, Translation, Translation3, UnitDualQuaternion, UnitQuaternion, }; /* * This file provides the following conversions: * ============================================= * * Translation -> Translation * Translation -> Isometry * Translation3 -> UnitDualQuaternion * Translation -> Similarity * Translation -> Transform * Translation -> Matrix (homogeneous) */ impl SubsetOf> for Translation where N1: Scalar, N2: Scalar + SupersetOf, // DefaultAllocator: Allocator + Allocator, { #[inline] fn to_superset(&self) -> Translation { Translation::from(self.vector.to_superset()) } #[inline] fn is_in_subset(rot: &Translation) -> bool { crate::is_convertible::<_, VectorN>(&rot.vector) } #[inline] fn from_superset_unchecked(rot: &Translation) -> Self { Translation { vector: rot.vector.to_subset_unchecked(), } } } impl SubsetOf> for Translation where N1: RealField, N2: RealField + SupersetOf, R: AbstractRotation, // DefaultAllocator: Allocator + Allocator, { #[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 N1: RealField, N2: RealField + SupersetOf, { #[inline] fn to_superset(&self) -> UnitDualQuaternion { let dq = UnitDualQuaternion::::from_parts(self.clone(), 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 N1: RealField, N2: RealField + SupersetOf, R: AbstractRotation, // DefaultAllocator: Allocator + Allocator, { #[inline] fn to_superset(&self) -> Similarity { Similarity::from_parts(self.to_superset(), R::identity(), N2::one()) } #[inline] fn is_in_subset(sim: &Similarity) -> bool { sim.isometry.rotation == R::identity() && sim.scaling() == N2::one() } #[inline] fn from_superset_unchecked(sim: &Similarity) -> Self { Self::from_superset_unchecked(&sim.isometry.translation) } } impl SubsetOf> for Translation where N1: RealField, N2: 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>>> for Translation where N1: RealField, N2: RealField + SupersetOf, Const: DimNameAdd, DefaultAllocator: Allocator, U1>, DimNameSum, U1>> + Allocator, U1>, DimNameSum, U1>>, // + Allocator // + Allocator { #[inline] fn to_superset(&self) -> MatrixN, U1>> { self.to_homogeneous().to_superset() } #[inline] fn is_in_subset(m: &MatrixN, U1>>) -> bool { let id = m.fixed_slice::, U1>, D>(0, 0); // Scalar types agree. m.iter().all(|e| SupersetOf::::is_in_subset(e)) && // The block part does nothing. id.is_identity(N2::zero()) && // The normalization factor is one. m[(D, D)] == N2::one() } #[inline] fn from_superset_unchecked(m: &MatrixN, U1>>) -> Self { let t = m.fixed_slice::(0, D); Self { vector: crate::convert_unchecked(t.into_owned()), } } } impl From> for MatrixN, U1>> where Const: DimNameAdd, DefaultAllocator: Allocator, U1>, DimNameSum, U1>> + Allocator>, { #[inline] fn from(t: Translation) -> Self { t.to_homogeneous() } } impl From>> for Translation // where // DefaultAllocator: Allocator, { #[inline] fn from(vector: VectorN>) -> Self { Translation { vector } } } impl From<[Translation; 2]> for Translation where N: From<[::Element; 2]>, N::Element: Scalar, // DefaultAllocator: Allocator + Allocator, { #[inline] fn from(arr: [Translation; 2]) -> Self { Self::from(VectorN::from([ arr[0].vector.clone(), arr[1].vector.clone(), ])) } } impl From<[Translation; 4]> for Translation where N: From<[::Element; 4]>, N::Element: Scalar, // DefaultAllocator: Allocator + Allocator, { #[inline] fn from(arr: [Translation; 4]) -> Self { Self::from(VectorN::from([ arr[0].vector.clone(), arr[1].vector.clone(), arr[2].vector.clone(), arr[3].vector.clone(), ])) } } impl From<[Translation; 8]> for Translation where N: From<[::Element; 8]>, N::Element: Scalar, // DefaultAllocator: Allocator + Allocator, { #[inline] fn from(arr: [Translation; 8]) -> Self { Self::from(VectorN::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 N: From<[::Element; 16]>, N::Element: Scalar, // DefaultAllocator: Allocator + Allocator, { #[inline] fn from(arr: [Translation; 16]) -> Self { Self::from(VectorN::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(), ])) } }