1use num::Zero;
2
3use simba::scalar::{RealField, SubsetOf, SupersetOf};
4use simba::simd::{PrimitiveSimdValue, SimdValue};
5
6use crate::base::allocator::Allocator;
7use crate::base::dimension::{DimMin, DimNameAdd, DimNameSum, U1};
8use crate::base::{Const, DefaultAllocator, Matrix2, Matrix3, Matrix4, OMatrix, SMatrix, Scalar};
9
10use crate::geometry::{
11 AbstractRotation, Isometry, Rotation, Rotation2, Rotation3, Similarity, SuperTCategoryOf,
12 TAffine, Transform, Translation, UnitComplex, UnitDualQuaternion, UnitQuaternion,
13};
14
15impl<T1, T2, const D: usize> SubsetOf<Rotation<T2, D>> for Rotation<T1, D>
31where
32 T1: RealField,
33 T2: RealField + SupersetOf<T1>,
34{
35 #[inline]
36 fn to_superset(&self) -> Rotation<T2, D> {
37 Rotation::from_matrix_unchecked(self.matrix().to_superset())
38 }
39
40 #[inline]
41 fn is_in_subset(rot: &Rotation<T2, D>) -> bool {
42 crate::is_convertible::<_, SMatrix<T1, D, D>>(rot.matrix())
43 }
44
45 #[inline]
46 fn from_superset_unchecked(rot: &Rotation<T2, D>) -> Self {
47 Rotation::from_matrix_unchecked(rot.matrix().to_subset_unchecked())
48 }
49}
50
51impl<T1, T2> SubsetOf<UnitQuaternion<T2>> for Rotation3<T1>
52where
53 T1: RealField,
54 T2: RealField + SupersetOf<T1>,
55{
56 #[inline]
57 fn to_superset(&self) -> UnitQuaternion<T2> {
58 let q = UnitQuaternion::<T1>::from_rotation_matrix(self);
59 q.to_superset()
60 }
61
62 #[inline]
63 fn is_in_subset(q: &UnitQuaternion<T2>) -> bool {
64 crate::is_convertible::<_, UnitQuaternion<T1>>(q)
65 }
66
67 #[inline]
68 fn from_superset_unchecked(q: &UnitQuaternion<T2>) -> Self {
69 let q: UnitQuaternion<T1> = crate::convert_ref_unchecked(q);
70 q.to_rotation_matrix()
71 }
72}
73
74impl<T1, T2> SubsetOf<UnitDualQuaternion<T2>> for Rotation3<T1>
75where
76 T1: RealField,
77 T2: RealField + SupersetOf<T1>,
78{
79 #[inline]
80 fn to_superset(&self) -> UnitDualQuaternion<T2> {
81 let q = UnitQuaternion::<T1>::from_rotation_matrix(self);
82 let dq = UnitDualQuaternion::from_rotation(q);
83 dq.to_superset()
84 }
85
86 #[inline]
87 fn is_in_subset(dq: &UnitDualQuaternion<T2>) -> bool {
88 crate::is_convertible::<_, UnitQuaternion<T1>>(&dq.rotation())
89 && dq.translation().vector.is_zero()
90 }
91
92 #[inline]
93 fn from_superset_unchecked(dq: &UnitDualQuaternion<T2>) -> Self {
94 let dq: UnitDualQuaternion<T1> = crate::convert_ref_unchecked(dq);
95 dq.rotation().to_rotation_matrix()
96 }
97}
98
99impl<T1, T2> SubsetOf<UnitComplex<T2>> for Rotation2<T1>
100where
101 T1: RealField,
102 T2: RealField + SupersetOf<T1>,
103{
104 #[inline]
105 fn to_superset(&self) -> UnitComplex<T2> {
106 let q = UnitComplex::<T1>::from_rotation_matrix(self);
107 q.to_superset()
108 }
109
110 #[inline]
111 fn is_in_subset(q: &UnitComplex<T2>) -> bool {
112 crate::is_convertible::<_, UnitComplex<T1>>(q)
113 }
114
115 #[inline]
116 fn from_superset_unchecked(q: &UnitComplex<T2>) -> Self {
117 let q: UnitComplex<T1> = crate::convert_ref_unchecked(q);
118 q.to_rotation_matrix()
119 }
120}
121
122impl<T1, T2, R, const D: usize> SubsetOf<Isometry<T2, R, D>> for Rotation<T1, D>
123where
124 T1: RealField,
125 T2: RealField + SupersetOf<T1>,
126 R: AbstractRotation<T2, D> + SupersetOf<Self>,
127{
128 #[inline]
129 fn to_superset(&self) -> Isometry<T2, R, D> {
130 Isometry::from_parts(Translation::identity(), crate::convert_ref(self))
131 }
132
133 #[inline]
134 fn is_in_subset(iso: &Isometry<T2, R, D>) -> bool {
135 iso.translation.vector.is_zero()
136 }
137
138 #[inline]
139 fn from_superset_unchecked(iso: &Isometry<T2, R, D>) -> Self {
140 crate::convert_ref_unchecked(&iso.rotation)
141 }
142}
143
144impl<T1, T2, R, const D: usize> SubsetOf<Similarity<T2, R, D>> for Rotation<T1, D>
145where
146 T1: RealField,
147 T2: RealField + SupersetOf<T1>,
148 R: AbstractRotation<T2, D> + SupersetOf<Self>,
149{
150 #[inline]
151 fn to_superset(&self) -> Similarity<T2, R, D> {
152 Similarity::from_parts(Translation::identity(), crate::convert_ref(self), T2::one())
153 }
154
155 #[inline]
156 fn is_in_subset(sim: &Similarity<T2, R, D>) -> bool {
157 sim.isometry.translation.vector.is_zero() && sim.scaling() == T2::one()
158 }
159
160 #[inline]
161 fn from_superset_unchecked(sim: &Similarity<T2, R, D>) -> Self {
162 crate::convert_ref_unchecked(&sim.isometry.rotation)
163 }
164}
165
166impl<T1, T2, C, const D: usize> SubsetOf<Transform<T2, C, D>> for Rotation<T1, D>
167where
168 T1: RealField,
169 T2: RealField + SupersetOf<T1>,
170 C: SuperTCategoryOf<TAffine>,
171 Const<D>: DimNameAdd<U1> + DimMin<Const<D>, Output = Const<D>>, DefaultAllocator: Allocator<T1, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>
173 + Allocator<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,
174 {
178 #[inline]
180 fn to_superset(&self) -> Transform<T2, C, D> {
181 Transform::from_matrix_unchecked(self.to_homogeneous().to_superset())
182 }
183
184 #[inline]
185 fn is_in_subset(t: &Transform<T2, C, D>) -> bool {
186 <Self as SubsetOf<_>>::is_in_subset(t.matrix())
187 }
188
189 #[inline]
190 fn from_superset_unchecked(t: &Transform<T2, C, D>) -> Self {
191 Self::from_superset_unchecked(t.matrix())
192 }
193}
194
195impl<T1, T2, const D: usize>
196 SubsetOf<OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>> for Rotation<T1, D>
197where
198 T1: RealField,
199 T2: RealField + SupersetOf<T1>,
200 Const<D>: DimNameAdd<U1> + DimMin<Const<D>, Output = Const<D>>, DefaultAllocator: Allocator<T1, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>
202 + Allocator<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>, {
206 #[inline]
208 fn to_superset(&self) -> OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>> {
209 self.to_homogeneous().to_superset()
210 }
211
212 #[inline]
213 fn is_in_subset(m: &OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>) -> bool {
214 let rot = m.fixed_slice::<D, D>(0, 0);
215 let bottom = m.fixed_slice::<1, D>(D, 0);
216
217 m.iter().all(|e| SupersetOf::<T1>::is_in_subset(e)) &&
219 rot.is_special_orthogonal(T2::default_epsilon() * crate::convert(100.0)) &&
221 bottom.iter().all(|e| e.is_zero()) && m[(D, D)] == T2::one()
223 }
224
225 #[inline]
226 fn from_superset_unchecked(
227 m: &OMatrix<T2, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,
228 ) -> Self {
229 let r = m.fixed_slice::<D, D>(0, 0);
230 Self::from_matrix_unchecked(crate::convert_unchecked(r.into_owned()))
231 }
232}
233
234impl<T: RealField> From<Rotation2<T>> for Matrix3<T> {
235 #[inline]
236 fn from(q: Rotation2<T>) -> Self {
237 q.to_homogeneous()
238 }
239}
240
241impl<T: RealField> From<Rotation2<T>> for Matrix2<T> {
242 #[inline]
243 fn from(q: Rotation2<T>) -> Self {
244 q.into_inner()
245 }
246}
247
248impl<T: RealField> From<Rotation3<T>> for Matrix4<T> {
249 #[inline]
250 fn from(q: Rotation3<T>) -> Self {
251 q.to_homogeneous()
252 }
253}
254
255impl<T: RealField> From<Rotation3<T>> for Matrix3<T> {
256 #[inline]
257 fn from(q: Rotation3<T>) -> Self {
258 q.into_inner()
259 }
260}
261
262impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Rotation<T::Element, D>; 2]>
263 for Rotation<T, D>
264where
265 T: From<[<T as SimdValue>::Element; 2]>,
266 T::Element: Scalar + Copy,
267{
268 #[inline]
269 fn from(arr: [Rotation<T::Element, D>; 2]) -> Self {
270 Self::from_matrix_unchecked(OMatrix::from([arr[0].into_inner(), arr[1].into_inner()]))
271 }
272}
273
274impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Rotation<T::Element, D>; 4]>
275 for Rotation<T, D>
276where
277 T: From<[<T as SimdValue>::Element; 4]>,
278 T::Element: Scalar + Copy,
279{
280 #[inline]
281 fn from(arr: [Rotation<T::Element, D>; 4]) -> Self {
282 Self::from_matrix_unchecked(OMatrix::from([
283 arr[0].into_inner(),
284 arr[1].into_inner(),
285 arr[2].into_inner(),
286 arr[3].into_inner(),
287 ]))
288 }
289}
290
291impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Rotation<T::Element, D>; 8]>
292 for Rotation<T, D>
293where
294 T: From<[<T as SimdValue>::Element; 8]>,
295 T::Element: Scalar + Copy,
296{
297 #[inline]
298 fn from(arr: [Rotation<T::Element, D>; 8]) -> Self {
299 Self::from_matrix_unchecked(OMatrix::from([
300 arr[0].into_inner(),
301 arr[1].into_inner(),
302 arr[2].into_inner(),
303 arr[3].into_inner(),
304 arr[4].into_inner(),
305 arr[5].into_inner(),
306 arr[6].into_inner(),
307 arr[7].into_inner(),
308 ]))
309 }
310}
311
312impl<T: Scalar + PrimitiveSimdValue, const D: usize> From<[Rotation<T::Element, D>; 16]>
313 for Rotation<T, D>
314where
315 T: From<[<T as SimdValue>::Element; 16]>,
316 T::Element: Scalar + Copy,
317{
318 #[inline]
319 fn from(arr: [Rotation<T::Element, D>; 16]) -> Self {
320 Self::from_matrix_unchecked(OMatrix::from([
321 arr[0].into_inner(),
322 arr[1].into_inner(),
323 arr[2].into_inner(),
324 arr[3].into_inner(),
325 arr[4].into_inner(),
326 arr[5].into_inner(),
327 arr[6].into_inner(),
328 arr[7].into_inner(),
329 arr[8].into_inner(),
330 arr[9].into_inner(),
331 arr[10].into_inner(),
332 arr[11].into_inner(),
333 arr[12].into_inner(),
334 arr[13].into_inner(),
335 arr[14].into_inner(),
336 arr[15].into_inner(),
337 ]))
338 }
339}