use num::{One, Zero}; use alga::general::{Real, SubsetOf, SupersetOf}; use alga::linear::Rotation; use crate::base::allocator::Allocator; use crate::base::dimension::{DimName, DimNameAdd, DimNameSum, U1}; use crate::base::{DefaultAllocator, MatrixN, Scalar, VectorN}; use crate::geometry::{Isometry, Point, 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] unsafe fn from_superset_unchecked(rot: &Translation) -> Self { Translation { vector: rot.vector.to_subset_unchecked(), } } } impl SubsetOf> for Translation where N1: Real, N2: Real + SupersetOf, R: Rotation>, 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] unsafe fn from_superset_unchecked(iso: &Isometry) -> Self { Self::from_superset_unchecked(&iso.translation) } } impl SubsetOf> for Translation where N1: Real, N2: Real + SupersetOf, R: Rotation>, 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] unsafe fn from_superset_unchecked(sim: &Similarity) -> Self { Self::from_superset_unchecked(&sim.isometry.translation) } } impl SubsetOf> for Translation where N1: Real, N2: Real + 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] unsafe fn from_superset_unchecked(t: &Transform) -> Self { Self::from_superset_unchecked(t.matrix()) } } impl SubsetOf>> for Translation where N1: Real, N2: Real + 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] unsafe 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 } } }