use simba::scalar::{RealField, SubsetOf, SupersetOf}; use crate::base::allocator::Allocator; use crate::base::dimension::{DimMin, DimName, DimNameAdd, DimNameSum, U1}; use crate::base::{DefaultAllocator, MatrixN}; use crate::geometry::{ AbstractRotation, Isometry, Similarity, SuperTCategoryOf, TAffine, Transform, Translation, }; /* * This file provides the following conversions: * ============================================= * * Similarity -> Similarity * Similarity -> Transform * Similarity -> Matrix (homogeneous) */ impl SubsetOf> for Similarity where N1: RealField + SubsetOf, N2: RealField + SupersetOf, R1: AbstractRotation + SubsetOf, R2: AbstractRotation, DefaultAllocator: Allocator + Allocator, { #[inline] fn to_superset(&self) -> Similarity { Similarity::from_isometry(self.isometry.to_superset(), self.scaling().to_superset()) } #[inline] fn is_in_subset(sim: &Similarity) -> bool { crate::is_convertible::<_, Isometry>(&sim.isometry) && crate::is_convertible::<_, N1>(&sim.scaling()) } #[inline] fn from_superset_unchecked(sim: &Similarity) -> Self { Similarity::from_isometry( sim.isometry.to_subset_unchecked(), sim.scaling().to_subset_unchecked(), ) } } impl SubsetOf> for Similarity where N1: RealField, N2: RealField + SupersetOf, C: SuperTCategoryOf, R: AbstractRotation + SubsetOf>> + SubsetOf>>, D: DimNameAdd + DimMin, // needed by .determinant() DefaultAllocator: Allocator + Allocator + Allocator, DimNameSum> + Allocator, DimNameSum> + Allocator<(usize, usize), D> + Allocator, DimNameSum> + Allocator + Allocator, { #[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 Similarity where N1: RealField, N2: RealField + SupersetOf, R: AbstractRotation + SubsetOf>> + SubsetOf>>, D: DimNameAdd + DimMin, // needed by .determinant() DefaultAllocator: Allocator + Allocator + Allocator, DimNameSum> + Allocator, DimNameSum> + Allocator<(usize, usize), D> + Allocator, DimNameSum> + Allocator + Allocator, { #[inline] fn to_superset(&self) -> MatrixN> { self.to_homogeneous().to_superset() } #[inline] fn is_in_subset(m: &MatrixN>) -> bool { let mut rot = m.fixed_slice::(0, 0).clone_owned(); if rot .fixed_columns_mut::(0) .try_normalize_mut(N2::zero()) .is_some() && rot .fixed_columns_mut::(1) .try_normalize_mut(N2::zero()) .is_some() && rot .fixed_columns_mut::(2) .try_normalize_mut(N2::zero()) .is_some() { // FIXME: could we avoid explicit the computation of the determinant? // (its sign is needed to see if the scaling factor is negative). if rot.determinant() < N2::zero() { rot.fixed_columns_mut::(0).neg_mut(); rot.fixed_columns_mut::(1).neg_mut(); rot.fixed_columns_mut::(2).neg_mut(); } let bottom = m.fixed_slice::(D::dim(), 0); // Scalar types agree. m.iter().all(|e| SupersetOf::::is_in_subset(e)) && // The normalized block part is a rotation. // rot.is_special_orthogonal(N2::default_epsilon().sqrt()) && // The bottom row is (0, 0, ..., 1) bottom.iter().all(|e| e.is_zero()) && m[(D::dim(), D::dim())] == N2::one() } else { false } } #[inline] fn from_superset_unchecked(m: &MatrixN>) -> Self { let mut mm = m.clone_owned(); let na = mm.fixed_slice_mut::(0, 0).normalize_mut(); let nb = mm.fixed_slice_mut::(0, 1).normalize_mut(); let nc = mm.fixed_slice_mut::(0, 2).normalize_mut(); let mut scale = (na + nb + nc) / crate::convert(3.0); // We take the mean, for robustness. // FIXME: could we avoid the explicit computation of the determinant? // (its sign is needed to see if the scaling factor is negative). if mm.fixed_slice::(0, 0).determinant() < N2::zero() { mm.fixed_slice_mut::(0, 0).neg_mut(); mm.fixed_slice_mut::(0, 1).neg_mut(); mm.fixed_slice_mut::(0, 2).neg_mut(); scale = -scale; } let t = m.fixed_slice::(0, D::dim()).into_owned(); let t = Translation { vector: crate::convert_unchecked(t), }; Self::from_parts( t, crate::convert_unchecked(mm), crate::convert_unchecked(scale), ) } } impl From> for MatrixN> where D: DimNameAdd, R: SubsetOf>>, DefaultAllocator: Allocator + Allocator, DimNameSum>, { #[inline] fn from(sim: Similarity) -> Self { sim.to_homogeneous() } }