nalgebra/geometry/
translation_conversion.rs

1use num::{One, Zero};
2
3use simba::scalar::{RealField, SubsetOf, SupersetOf};
4use simba::simd::PrimitiveSimdValue;
5
6use crate::base::allocator::Allocator;
7use crate::base::dimension::{DimNameAdd, DimNameSum, U1};
8use crate::base::{Const, DefaultAllocator, DimName, OMatrix, OVector, SVector, Scalar};
9
10use crate::geometry::{
11    AbstractRotation, Isometry, Similarity, SuperTCategoryOf, TAffine, Transform, Translation,
12    Translation3, UnitDualQuaternion, UnitQuaternion,
13};
14use crate::{ArrayStorage, Point};
15
16/*
17 * This file provides the following conversions:
18 * =============================================
19 *
20 * Translation -> Translation
21 * Translation -> Isometry
22 * Translation3 -> UnitDualQuaternion
23 * Translation -> Similarity
24 * Translation -> Transform
25 * Translation -> Matrix (homogeneous)
26 */
27
28impl<T1, T2, const D: usize> SubsetOf<Translation<T2, D>> for Translation<T1, D>
29where
30    T1: Scalar,
31    T2: Scalar + SupersetOf<T1>,
32{
33    #[inline]
34    fn to_superset(&self) -> Translation<T2, D> {
35        Translation::from(self.vector.to_superset())
36    }
37
38    #[inline]
39    fn is_in_subset(rot: &Translation<T2, D>) -> bool {
40        crate::is_convertible::<_, SVector<T1, D>>(&rot.vector)
41    }
42
43    #[inline]
44    fn from_superset_unchecked(rot: &Translation<T2, D>) -> Self {
45        Translation {
46            vector: rot.vector.to_subset_unchecked(),
47        }
48    }
49}
50
51impl<T1, T2, R, const D: usize> SubsetOf<Isometry<T2, R, D>> for Translation<T1, D>
52where
53    T1: RealField,
54    T2: RealField + SupersetOf<T1>,
55    R: AbstractRotation<T2, D>,
56{
57    #[inline]
58    fn to_superset(&self) -> Isometry<T2, R, D> {
59        Isometry::from_parts(self.to_superset(), R::identity())
60    }
61
62    #[inline]
63    fn is_in_subset(iso: &Isometry<T2, R, D>) -> bool {
64        iso.rotation == R::identity()
65    }
66
67    #[inline]
68    fn from_superset_unchecked(iso: &Isometry<T2, R, D>) -> Self {
69        Self::from_superset_unchecked(&iso.translation)
70    }
71}
72
73impl<T1, T2> SubsetOf<UnitDualQuaternion<T2>> for Translation3<T1>
74where
75    T1: RealField,
76    T2: RealField + SupersetOf<T1>,
77{
78    #[inline]
79    fn to_superset(&self) -> UnitDualQuaternion<T2> {
80        let dq = UnitDualQuaternion::<T1>::from_parts(self.clone(), UnitQuaternion::identity());
81        dq.to_superset()
82    }
83
84    #[inline]
85    fn is_in_subset(dq: &UnitDualQuaternion<T2>) -> bool {
86        crate::is_convertible::<_, Translation<T1, 3>>(&dq.translation())
87            && dq.rotation() == UnitQuaternion::identity()
88    }
89
90    #[inline]
91    fn from_superset_unchecked(dq: &UnitDualQuaternion<T2>) -> Self {
92        let dq: UnitDualQuaternion<T1> = crate::convert_ref_unchecked(dq);
93        dq.translation()
94    }
95}
96
97impl<T1, T2, R, const D: usize> SubsetOf<Similarity<T2, R, D>> for Translation<T1, D>
98where
99    T1: RealField,
100    T2: RealField + SupersetOf<T1>,
101    R: AbstractRotation<T2, D>,
102{
103    #[inline]
104    fn to_superset(&self) -> Similarity<T2, R, D> {
105        Similarity::from_parts(self.to_superset(), R::identity(), T2::one())
106    }
107
108    #[inline]
109    fn is_in_subset(sim: &Similarity<T2, R, D>) -> bool {
110        sim.isometry.rotation == R::identity() && sim.scaling() == T2::one()
111    }
112
113    #[inline]
114    fn from_superset_unchecked(sim: &Similarity<T2, R, D>) -> Self {
115        Self::from_superset_unchecked(&sim.isometry.translation)
116    }
117}
118
119impl<T1, T2, C, const D: usize> SubsetOf<Transform<T2, C, D>> for Translation<T1, D>
120where
121    T1: RealField,
122    T2: RealField + SupersetOf<T1>,
123    C: SuperTCategoryOf<TAffine>,
124    Const<D>: DimNameAdd<U1>,
125    DefaultAllocator: Allocator<DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,
126{
127    #[inline]
128    fn to_superset(&self) -> Transform<T2, C, D> {
129        Transform::from_matrix_unchecked(self.to_homogeneous().to_superset())
130    }
131
132    #[inline]
133    fn is_in_subset(t: &Transform<T2, C, D>) -> bool {
134        <Self as SubsetOf<_>>::is_in_subset(t.matrix())
135    }
136
137    #[inline]
138    fn from_superset_unchecked(t: &Transform<T2, C, D>) -> Self {
139        Self::from_superset_unchecked(t.matrix())
140    }
141}
142
143impl<T1, T2, const D: usize>
144    SubsetOf<OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>> for Translation<T1, D>
145where
146    T1: RealField,
147    T2: RealField + SupersetOf<T1>,
148    Const<D>: DimNameAdd<U1>,
149    DefaultAllocator: Allocator<Const<D>, Buffer<T1> = ArrayStorage<T1, D, 1>>
150        + Allocator<DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,
151{
152    #[inline]
153    fn to_superset(&self) -> OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>> {
154        self.to_homogeneous().to_superset()
155    }
156
157    #[inline]
158    fn is_in_subset(m: &OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>) -> bool {
159        let id = m.generic_view((0, 0), (DimNameSum::<Const<D>, U1>::name(), Const::<D>));
160
161        // Scalar types agree.
162        m.iter().all(|e| SupersetOf::<T1>::is_in_subset(e)) &&
163        // The block part does nothing.
164        id.is_identity(T2::zero()) &&
165        // The normalization factor is one.
166        m[(D, D)] == T2::one()
167    }
168
169    #[inline]
170    fn from_superset_unchecked(
171        m: &OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,
172    ) -> Self {
173        let t: OVector<T2, Const<D>> = m.fixed_view::<D, 1>(0, D).into_owned();
174        Self {
175            vector: crate::convert_unchecked(t),
176        }
177    }
178}
179
180impl<T: Scalar + Zero + One, const D: usize> From<Translation<T, D>>
181    for OMatrix<T, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>
182where
183    Const<D>: DimNameAdd<U1>,
184    DefaultAllocator:
185        Allocator<DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>> + Allocator<Const<D>>,
186{
187    #[inline]
188    fn from(t: Translation<T, D>) -> Self {
189        t.to_homogeneous()
190    }
191}
192
193impl<T: Scalar, const D: usize> From<OVector<T, Const<D>>> for Translation<T, D> {
194    #[inline]
195    fn from(vector: OVector<T, Const<D>>) -> Self {
196        Translation { vector }
197    }
198}
199
200impl<T: Scalar, const D: usize> From<[T; D]> for Translation<T, D> {
201    #[inline]
202    fn from(coords: [T; D]) -> Self {
203        Translation {
204            vector: coords.into(),
205        }
206    }
207}
208
209impl<T: Scalar, const D: usize> From<Point<T, D>> for Translation<T, D> {
210    #[inline]
211    fn from(pt: Point<T, D>) -> Self {
212        Translation { vector: pt.coords }
213    }
214}
215
216impl<T: Scalar, const D: usize> From<Translation<T, D>> for [T; D] {
217    #[inline]
218    fn from(t: Translation<T, D>) -> Self {
219        t.vector.into()
220    }
221}
222
223impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Translation<T::Element, D>; 2]>
224    for Translation<T, D>
225where
226    T: From<[<T as simba::simd::SimdValue>::Element; 2]>,
227    T::Element: Scalar,
228{
229    #[inline]
230    fn from(arr: [Translation<T::Element, D>; 2]) -> Self {
231        Self::from(OVector::from([
232            arr[0].vector.clone(),
233            arr[1].vector.clone(),
234        ]))
235    }
236}
237
238impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Translation<T::Element, D>; 4]>
239    for Translation<T, D>
240where
241    T: From<[<T as simba::simd::SimdValue>::Element; 4]>,
242    T::Element: Scalar,
243{
244    #[inline]
245    fn from(arr: [Translation<T::Element, D>; 4]) -> Self {
246        Self::from(OVector::from([
247            arr[0].vector.clone(),
248            arr[1].vector.clone(),
249            arr[2].vector.clone(),
250            arr[3].vector.clone(),
251        ]))
252    }
253}
254
255impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Translation<T::Element, D>; 8]>
256    for Translation<T, D>
257where
258    T: From<[<T as simba::simd::SimdValue>::Element; 8]>,
259    T::Element: Scalar,
260{
261    #[inline]
262    fn from(arr: [Translation<T::Element, D>; 8]) -> Self {
263        Self::from(OVector::from([
264            arr[0].vector.clone(),
265            arr[1].vector.clone(),
266            arr[2].vector.clone(),
267            arr[3].vector.clone(),
268            arr[4].vector.clone(),
269            arr[5].vector.clone(),
270            arr[6].vector.clone(),
271            arr[7].vector.clone(),
272        ]))
273    }
274}
275
276impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Translation<T::Element, D>; 16]>
277    for Translation<T, D>
278where
279    T: From<[<T as simba::simd::SimdValue>::Element; 16]>,
280    T::Element: Scalar,
281{
282    #[inline]
283    fn from(arr: [Translation<T::Element, D>; 16]) -> Self {
284        Self::from(OVector::from([
285            arr[0].vector.clone(),
286            arr[1].vector.clone(),
287            arr[2].vector.clone(),
288            arr[3].vector.clone(),
289            arr[4].vector.clone(),
290            arr[5].vector.clone(),
291            arr[6].vector.clone(),
292            arr[7].vector.clone(),
293            arr[8].vector.clone(),
294            arr[9].vector.clone(),
295            arr[10].vector.clone(),
296            arr[11].vector.clone(),
297            arr[12].vector.clone(),
298            arr[13].vector.clone(),
299            arr[14].vector.clone(),
300            arr[15].vector.clone(),
301        ]))
302    }
303}