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::{ SuperTCategoryOf, TAffine, Transform, Scale }; use crate::{ClosedAdd, Point}; /* * This file provides the following conversions: * ============================================= * * Scale -> Scale * Scale -> Transform * Scale -> Matrix (homogeneous) */ impl SubsetOf> for Scale where T1: Scalar, T2: Scalar + SupersetOf, { #[inline] fn to_superset(&self) -> Scale { Scale::from(self.vector.to_superset()) } #[inline] fn is_in_subset(rot: &Scale) -> bool { crate::is_convertible::<_, SVector>(&rot.vector) } #[inline] fn from_superset_unchecked(rot: &Scale) -> Self { Scale { vector: rot.vector.to_subset_unchecked(), } } } impl SubsetOf> for Scale where T1: RealField, T2: RealField + SupersetOf, C: SuperTCategoryOf, Const: DimNameAdd, DefaultAllocator: Allocator, U1>, DimNameSum, U1>> + Allocator, U1>, 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 Scale where T1: RealField, T2: RealField + SupersetOf, Const: DimNameAdd, DefaultAllocator: Allocator, U1>, DimNameSum, U1>> + Allocator, U1>, 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, U1>, U1> + Allocator>, { #[inline] fn from(t: Scale) -> Self { t.to_homogeneous() } } impl From>> for Scale { #[inline] fn from(vector: OVector>) -> Self { Scale { vector } } } impl From<[T; D]> for Scale { #[inline] fn from(coords: [T; D]) -> Self { Scale { vector: coords.into(), } } } impl From> for Scale { #[inline] fn from(pt: Point) -> Self { Scale { vector: pt.coords } } } impl From> for [T; D] { #[inline] fn from(t: Scale) -> Self { t.vector.into() } } impl From<[Scale; 2]> for Scale where T: From<[::Element; 2]>, T::Element: Scalar, { #[inline] fn from(arr: [Scale; 2]) -> Self { Self::from(OVector::from([ arr[0].vector.clone(), arr[1].vector.clone(), ])) } } impl From<[Scale; 4]> for Scale where T: From<[::Element; 4]>, T::Element: Scalar, { #[inline] fn from(arr: [Scale; 4]) -> Self { Self::from(OVector::from([ arr[0].vector.clone(), arr[1].vector.clone(), arr[2].vector.clone(), arr[3].vector.clone(), ])) } } impl From<[Scale; 8]> for Scale where T: From<[::Element; 8]>, T::Element: Scalar, { #[inline] fn from(arr: [Scale; 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<[Scale; 16]> for Scale where T: From<[::Element; 16]>, T::Element: Scalar, { #[inline] fn from(arr: [Scale; 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(), ])) } }