use num::{One, Zero}; use simba::scalar::{RealField, SubsetOf, SupersetOf}; use simba::simd::PrimitiveSimdValue; use crate::base::allocator::Allocator; use crate::base::dimension::{DimName, DimNameAdd, DimNameSum, U1}; use crate::base::{DefaultAllocator, MatrixN, Scalar, VectorN}; use crate::geometry::{ AbstractRotation, Isometry, Similarity, SuperTCategoryOf, TAffine, Transform, Translation, }; /* * This file provides the following conversions: * ============================================= * * Translation -> Translation * Translation -> Isometry * 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 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, D: DimNameAdd, DefaultAllocator: Allocator + Allocator + Allocator, DimNameSum> + Allocator, DimNameSum>, { #[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>> for Translation where N1: RealField, N2: RealField + SupersetOf, D: DimNameAdd, DefaultAllocator: Allocator + Allocator + Allocator, DimNameSum> + Allocator, DimNameSum>, { #[inline] fn to_superset(&self) -> MatrixN> { self.to_homogeneous().to_superset() } #[inline] fn is_in_subset(m: &MatrixN>) -> bool { let id = m.fixed_slice::, 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::dim(), D::dim())] == N2::one() } #[inline] fn from_superset_unchecked(m: &MatrixN>) -> Self { let t = m.fixed_slice::(0, D::dim()); Self { vector: crate::convert_unchecked(t.into_owned()), } } } impl From> for MatrixN> where D: DimNameAdd, DefaultAllocator: Allocator + Allocator, DimNameSum>, { #[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 + Copy, DefaultAllocator: Allocator + Allocator, >::Buffer: Copy, { #[inline] fn from(arr: [Translation; 2]) -> Self { Self::from(VectorN::from([arr[0].vector, arr[1].vector])) } } impl From<[Translation; 4]> for Translation where N: From<[::Element; 4]>, N::Element: Scalar + Copy, DefaultAllocator: Allocator + Allocator, >::Buffer: Copy, { #[inline] fn from(arr: [Translation; 4]) -> Self { Self::from(VectorN::from([ arr[0].vector, arr[1].vector, arr[2].vector, arr[3].vector, ])) } } impl From<[Translation; 8]> for Translation where N: From<[::Element; 8]>, N::Element: Scalar + Copy, DefaultAllocator: Allocator + Allocator, >::Buffer: Copy, { #[inline] fn from(arr: [Translation; 8]) -> Self { Self::from(VectorN::from([ arr[0].vector, arr[1].vector, arr[2].vector, arr[3].vector, arr[4].vector, arr[5].vector, arr[6].vector, arr[7].vector, ])) } } impl From<[Translation; 16]> for Translation where N: From<[::Element; 16]>, N::Element: Scalar + Copy, DefaultAllocator: Allocator + Allocator, >::Buffer: Copy, { #[inline] fn from(arr: [Translation; 16]) -> Self { Self::from(VectorN::from([ arr[0].vector, arr[1].vector, arr[2].vector, arr[3].vector, arr[4].vector, arr[5].vector, arr[6].vector, arr[7].vector, arr[8].vector, arr[9].vector, arr[10].vector, arr[11].vector, arr[12].vector, arr[13].vector, arr[14].vector, arr[15].vector, ])) } }