1use num::Zero;
2
3use simba::scalar::{RealField, SubsetOf, SupersetOf};
4use simba::simd::{PrimitiveSimdValue, SimdRealField, SimdValue};
5
6use crate::base::{Matrix3, Matrix4, Scalar, Vector4};
7use crate::geometry::{
8 AbstractRotation, Isometry, Quaternion, Rotation, Rotation3, Similarity, SuperTCategoryOf,
9 TAffine, Transform, Translation, UnitDualQuaternion, UnitQuaternion,
10};
11
12impl<T1, T2> SubsetOf<Quaternion<T2>> for Quaternion<T1>
30where
31 T1: Scalar,
32 T2: Scalar + SupersetOf<T1>,
33{
34 #[inline]
35 fn to_superset(&self) -> Quaternion<T2> {
36 Quaternion::from(self.coords.to_superset())
37 }
38
39 #[inline]
40 fn is_in_subset(q: &Quaternion<T2>) -> bool {
41 crate::is_convertible::<_, Vector4<T1>>(&q.coords)
42 }
43
44 #[inline]
45 fn from_superset_unchecked(q: &Quaternion<T2>) -> Self {
46 Self {
47 coords: q.coords.to_subset_unchecked(),
48 }
49 }
50}
51
52impl<T1, T2> SubsetOf<UnitQuaternion<T2>> for UnitQuaternion<T1>
53where
54 T1: Scalar,
55 T2: Scalar + SupersetOf<T1>,
56{
57 #[inline]
58 fn to_superset(&self) -> UnitQuaternion<T2> {
59 UnitQuaternion::new_unchecked(self.as_ref().to_superset())
60 }
61
62 #[inline]
63 fn is_in_subset(uq: &UnitQuaternion<T2>) -> bool {
64 crate::is_convertible::<_, Quaternion<T1>>(uq.as_ref())
65 }
66
67 #[inline]
68 fn from_superset_unchecked(uq: &UnitQuaternion<T2>) -> Self {
69 Self::new_unchecked(crate::convert_ref_unchecked(uq.as_ref()))
70 }
71}
72
73impl<T1, T2> SubsetOf<Rotation<T2, 3>> for UnitQuaternion<T1>
74where
75 T1: RealField,
76 T2: RealField + SupersetOf<T1>,
77{
78 #[inline]
79 fn to_superset(&self) -> Rotation3<T2> {
80 let q: UnitQuaternion<T2> = self.to_superset();
81 q.to_rotation_matrix()
82 }
83
84 #[inline]
85 fn is_in_subset(rot: &Rotation3<T2>) -> bool {
86 crate::is_convertible::<_, Rotation3<T1>>(rot)
87 }
88
89 #[inline]
90 fn from_superset_unchecked(rot: &Rotation3<T2>) -> Self {
91 let q = UnitQuaternion::<T2>::from_rotation_matrix(rot);
92 crate::convert_unchecked(q)
93 }
94}
95
96impl<T1, T2, R> SubsetOf<Isometry<T2, R, 3>> for UnitQuaternion<T1>
97where
98 T1: RealField,
99 T2: RealField + SupersetOf<T1>,
100 R: AbstractRotation<T2, 3> + SupersetOf<Self>,
101{
102 #[inline]
103 fn to_superset(&self) -> Isometry<T2, R, 3> {
104 Isometry::from_parts(Translation::identity(), crate::convert_ref(self))
105 }
106
107 #[inline]
108 fn is_in_subset(iso: &Isometry<T2, R, 3>) -> bool {
109 iso.translation.vector.is_zero()
110 }
111
112 #[inline]
113 fn from_superset_unchecked(iso: &Isometry<T2, R, 3>) -> Self {
114 crate::convert_ref_unchecked(&iso.rotation)
115 }
116}
117
118impl<T1, T2> SubsetOf<UnitDualQuaternion<T2>> for UnitQuaternion<T1>
119where
120 T1: RealField,
121 T2: RealField + SupersetOf<T1>,
122{
123 #[inline]
124 fn to_superset(&self) -> UnitDualQuaternion<T2> {
125 let q: UnitQuaternion<T2> = crate::convert_ref(self);
126 UnitDualQuaternion::from_rotation(q)
127 }
128
129 #[inline]
130 fn is_in_subset(dq: &UnitDualQuaternion<T2>) -> bool {
131 dq.translation().vector.is_zero()
132 }
133
134 #[inline]
135 fn from_superset_unchecked(dq: &UnitDualQuaternion<T2>) -> Self {
136 crate::convert_unchecked(dq.rotation())
137 }
138}
139
140impl<T1, T2, R> SubsetOf<Similarity<T2, R, 3>> for UnitQuaternion<T1>
141where
142 T1: RealField,
143 T2: RealField + SupersetOf<T1>,
144 R: AbstractRotation<T2, 3> + SupersetOf<Self>,
145{
146 #[inline]
147 fn to_superset(&self) -> Similarity<T2, R, 3> {
148 Similarity::from_isometry(crate::convert_ref(self), T2::one())
149 }
150
151 #[inline]
152 fn is_in_subset(sim: &Similarity<T2, R, 3>) -> bool {
153 sim.isometry.translation.vector.is_zero() && sim.scaling() == T2::one()
154 }
155
156 #[inline]
157 fn from_superset_unchecked(sim: &Similarity<T2, R, 3>) -> Self {
158 crate::convert_ref_unchecked(&sim.isometry)
159 }
160}
161
162impl<T1, T2, C> SubsetOf<Transform<T2, C, 3>> for UnitQuaternion<T1>
163where
164 T1: RealField,
165 T2: RealField + SupersetOf<T1>,
166 C: SuperTCategoryOf<TAffine>,
167{
168 #[inline]
169 fn to_superset(&self) -> Transform<T2, C, 3> {
170 Transform::from_matrix_unchecked(self.clone().to_homogeneous().to_superset())
171 }
172
173 #[inline]
174 fn is_in_subset(t: &Transform<T2, C, 3>) -> bool {
175 <Self as SubsetOf<_>>::is_in_subset(t.matrix())
176 }
177
178 #[inline]
179 fn from_superset_unchecked(t: &Transform<T2, C, 3>) -> Self {
180 Self::from_superset_unchecked(t.matrix())
181 }
182}
183
184impl<T1: RealField, T2: RealField + SupersetOf<T1>> SubsetOf<Matrix4<T2>> for UnitQuaternion<T1> {
185 #[inline]
186 fn to_superset(&self) -> Matrix4<T2> {
187 self.clone().to_homogeneous().to_superset()
188 }
189
190 #[inline]
191 fn is_in_subset(m: &Matrix4<T2>) -> bool {
192 crate::is_convertible::<_, Rotation3<T1>>(m)
193 }
194
195 #[inline]
196 fn from_superset_unchecked(m: &Matrix4<T2>) -> Self {
197 let rot: Rotation3<T1> = crate::convert_ref_unchecked(m);
198 Self::from_rotation_matrix(&rot)
199 }
200}
201
202impl<T: SimdRealField> From<UnitQuaternion<T>> for Matrix4<T>
203where
204 T::Element: SimdRealField,
205{
206 #[inline]
207 fn from(q: UnitQuaternion<T>) -> Self {
208 q.to_homogeneous()
209 }
210}
211
212impl<T: SimdRealField> From<UnitQuaternion<T>> for Rotation3<T>
213where
214 T::Element: SimdRealField,
215{
216 #[inline]
217 fn from(q: UnitQuaternion<T>) -> Self {
218 q.to_rotation_matrix()
219 }
220}
221
222impl<T: SimdRealField> From<Rotation3<T>> for UnitQuaternion<T>
223where
224 T::Element: SimdRealField,
225{
226 #[inline]
227 fn from(q: Rotation3<T>) -> Self {
228 Self::from_rotation_matrix(&q)
229 }
230}
231
232impl<T: SimdRealField> From<UnitQuaternion<T>> for Matrix3<T>
233where
234 T::Element: SimdRealField,
235{
236 #[inline]
237 fn from(q: UnitQuaternion<T>) -> Self {
238 q.to_rotation_matrix().into_inner()
239 }
240}
241
242impl<T: Scalar> From<Vector4<T>> for Quaternion<T> {
243 #[inline]
244 fn from(coords: Vector4<T>) -> Self {
245 Self { coords }
246 }
247}
248
249impl<T: Scalar> From<[T; 4]> for Quaternion<T> {
250 #[inline]
251 fn from(coords: [T; 4]) -> Self {
252 Self {
253 coords: coords.into(),
254 }
255 }
256}
257
258impl<T: Scalar + PrimitiveSimdValue> From<[Quaternion<T::Element>; 2]> for Quaternion<T>
259where
260 T: From<[<T as SimdValue>::Element; 2]>,
261 T::Element: Scalar + Copy,
262{
263 #[inline]
264 fn from(arr: [Quaternion<T::Element>; 2]) -> Self {
265 Self::from(Vector4::from([arr[0].coords, arr[1].coords]))
266 }
267}
268
269impl<T: Scalar + PrimitiveSimdValue> From<[Quaternion<T::Element>; 4]> for Quaternion<T>
270where
271 T: From<[<T as SimdValue>::Element; 4]>,
272 T::Element: Scalar + Copy,
273{
274 #[inline]
275 fn from(arr: [Quaternion<T::Element>; 4]) -> Self {
276 Self::from(Vector4::from([
277 arr[0].coords,
278 arr[1].coords,
279 arr[2].coords,
280 arr[3].coords,
281 ]))
282 }
283}
284
285impl<T: Scalar + PrimitiveSimdValue> From<[Quaternion<T::Element>; 8]> for Quaternion<T>
286where
287 T: From<[<T as SimdValue>::Element; 8]>,
288 T::Element: Scalar + Copy,
289{
290 #[inline]
291 fn from(arr: [Quaternion<T::Element>; 8]) -> Self {
292 Self::from(Vector4::from([
293 arr[0].coords,
294 arr[1].coords,
295 arr[2].coords,
296 arr[3].coords,
297 arr[4].coords,
298 arr[5].coords,
299 arr[6].coords,
300 arr[7].coords,
301 ]))
302 }
303}
304
305impl<T: Scalar + PrimitiveSimdValue> From<[Quaternion<T::Element>; 16]> for Quaternion<T>
306where
307 T: From<[<T as SimdValue>::Element; 16]>,
308 T::Element: Scalar + Copy,
309{
310 #[inline]
311 fn from(arr: [Quaternion<T::Element>; 16]) -> Self {
312 Self::from(Vector4::from([
313 arr[0].coords,
314 arr[1].coords,
315 arr[2].coords,
316 arr[3].coords,
317 arr[4].coords,
318 arr[5].coords,
319 arr[6].coords,
320 arr[7].coords,
321 arr[8].coords,
322 arr[9].coords,
323 arr[10].coords,
324 arr[11].coords,
325 arr[12].coords,
326 arr[13].coords,
327 arr[14].coords,
328 arr[15].coords,
329 ]))
330 }
331}
332
333impl<T: Scalar + Copy + PrimitiveSimdValue> From<[UnitQuaternion<T::Element>; 2]>
334 for UnitQuaternion<T>
335where
336 T: From<[<T as simba::simd::SimdValue>::Element; 2]>,
337 T::Element: Scalar + Copy,
338{
339 #[inline]
340 fn from(arr: [UnitQuaternion<T::Element>; 2]) -> Self {
341 Self::new_unchecked(Quaternion::from([arr[0].into_inner(), arr[1].into_inner()]))
342 }
343}
344
345impl<T: Scalar + Copy + PrimitiveSimdValue> From<[UnitQuaternion<T::Element>; 4]>
346 for UnitQuaternion<T>
347where
348 T: From<[<T as simba::simd::SimdValue>::Element; 4]>,
349 T::Element: Scalar + Copy,
350{
351 #[inline]
352 fn from(arr: [UnitQuaternion<T::Element>; 4]) -> Self {
353 Self::new_unchecked(Quaternion::from([
354 arr[0].into_inner(),
355 arr[1].into_inner(),
356 arr[2].into_inner(),
357 arr[3].into_inner(),
358 ]))
359 }
360}
361
362impl<T: Scalar + Copy + PrimitiveSimdValue> From<[UnitQuaternion<T::Element>; 8]>
363 for UnitQuaternion<T>
364where
365 T: From<[<T as simba::simd::SimdValue>::Element; 8]>,
366 T::Element: Scalar + Copy,
367{
368 #[inline]
369 fn from(arr: [UnitQuaternion<T::Element>; 8]) -> Self {
370 Self::new_unchecked(Quaternion::from([
371 arr[0].into_inner(),
372 arr[1].into_inner(),
373 arr[2].into_inner(),
374 arr[3].into_inner(),
375 arr[4].into_inner(),
376 arr[5].into_inner(),
377 arr[6].into_inner(),
378 arr[7].into_inner(),
379 ]))
380 }
381}
382
383impl<T: Scalar + Copy + PrimitiveSimdValue> From<[UnitQuaternion<T::Element>; 16]>
384 for UnitQuaternion<T>
385where
386 T: From<[<T as simba::simd::SimdValue>::Element; 16]>,
387 T::Element: Scalar + Copy,
388{
389 #[inline]
390 fn from(arr: [UnitQuaternion<T::Element>; 16]) -> Self {
391 Self::new_unchecked(Quaternion::from([
392 arr[0].into_inner(),
393 arr[1].into_inner(),
394 arr[2].into_inner(),
395 arr[3].into_inner(),
396 arr[4].into_inner(),
397 arr[5].into_inner(),
398 arr[6].into_inner(),
399 arr[7].into_inner(),
400 arr[8].into_inner(),
401 arr[9].into_inner(),
402 arr[10].into_inner(),
403 arr[11].into_inner(),
404 arr[12].into_inner(),
405 arr[13].into_inner(),
406 arr[14].into_inner(),
407 arr[15].into_inner(),
408 ]))
409 }
410}