Debian Codegraph @

debian/missing-sources/glam-0.28.0/src/f32/coresimd/quat.rs

raw
1 // Generated from quat.rs.tera template. Edit the template, not the generated file.
2
3 use crate::{
4 coresimd::*,
5 euler::{EulerFromQuaternion, EulerRot, EulerToQuaternion},
6 f32::math,
7 DQuat, Mat3, Mat3A, Mat4, Vec2, Vec3, Vec3A, Vec4,
8 };
9
10 use core::simd::*;
11
12 #[cfg(not(target_arch = "spirv"))]
13 use core::fmt;
14 use core::iter::{Product, Sum};
15 use core::ops::{Add, Deref, DerefMut, Div, Mul, MulAssign, Neg, Sub};
16
17 /// Creates a quaternion from `x`, `y`, `z` and `w` values.
18 ///
19 /// This should generally not be called manually unless you know what you are doing. Use
20 /// one of the other constructors instead such as `identity` or `from_axis_angle`.
21 #[inline]
22 #[must_use]
23 pub const fn quat(x: f32, y: f32, z: f32, w: f32) -> Quat {
24 Quat::from_xyzw(x, y, z, w)
25 }
26
27 /// A quaternion representing an orientation.
28 ///
29 /// This quaternion is intended to be of unit length but may denormalize due to
30 /// floating point "error creep" which can occur when successive quaternion
31 /// operations are applied.
32 ///
33 /// SIMD vector types are used for storage on supported platforms.
34 ///
35 /// This type is 16 byte aligned.
36 #[derive(Clone, Copy)]
37 #[repr(transparent)]
38 pub struct Quat(pub(crate) f32x4);
39
40 impl Quat {
41 /// All zeros.
42 const ZERO: Self = Self::from_array([0.0; 4]);
43
44 /// The identity quaternion. Corresponds to no rotation.
45 pub const IDENTITY: Self = Self::from_xyzw(0.0, 0.0, 0.0, 1.0);
46
47 /// All NANs.
48 pub const NAN: Self = Self::from_array([f32::NAN; 4]);
49
50 /// Creates a new rotation quaternion.
51 ///
52 /// This should generally not be called manually unless you know what you are doing.
53 /// Use one of the other constructors instead such as `identity` or `from_axis_angle`.
54 ///
55 /// `from_xyzw` is mostly used by unit tests and `serde` deserialization.
56 ///
57 /// # Preconditions
58 ///
59 /// This function does not check if the input is normalized, it is up to the user to
60 /// provide normalized input or to normalized the resulting quaternion.
61 #[inline(always)]
62 #[must_use]
63 pub const fn from_xyzw(x: f32, y: f32, z: f32, w: f32) -> Self {
64 Self(f32x4::from_array([x, y, z, w]))
65 }
66
67 /// Creates a rotation quaternion from an array.
68 ///
69 /// # Preconditions
70 ///
71 /// This function does not check if the input is normalized, it is up to the user to
72 /// provide normalized input or to normalized the resulting quaternion.
73 #[inline]
74 #[must_use]
75 pub const fn from_array(a: [f32; 4]) -> Self {
76 Self(f32x4::from_array(a))
77 }
78
79 /// Creates a new rotation quaternion from a 4D vector.
80 ///
81 /// # Preconditions
82 ///
83 /// This function does not check if the input is normalized, it is up to the user to
84 /// provide normalized input or to normalized the resulting quaternion.
85 #[inline]
86 #[must_use]
87 pub const fn from_vec4(v: Vec4) -> Self {
88 Self(v.0)
89 }
90
91 /// Creates a rotation quaternion from a slice.
92 ///
93 /// # Preconditions
94 ///
95 /// This function does not check if the input is normalized, it is up to the user to
96 /// provide normalized input or to normalized the resulting quaternion.
97 ///
98 /// # Panics
99 ///
100 /// Panics if `slice` length is less than 4.
101 #[inline]
102 #[must_use]
103 pub fn from_slice(slice: &[f32]) -> Self {
104 Self::from_xyzw(slice[0], slice[1], slice[2], slice[3])
105 }
106
107 /// Writes the quaternion to an unaligned slice.
108 ///
109 /// # Panics
110 ///
111 /// Panics if `slice` length is less than 4.
112 #[inline]
113 pub fn write_to_slice(self, slice: &mut [f32]) {
114 slice[0] = self.x;
115 slice[1] = self.y;
116 slice[2] = self.z;
117 slice[3] = self.w;
118 }
119
120 /// Create a quaternion for a normalized rotation `axis` and `angle` (in radians).
121 ///
122 /// The axis must be a unit vector.
123 ///
124 /// # Panics
125 ///
126 /// Will panic if `axis` is not normalized when `glam_assert` is enabled.
127 #[inline]
128 #[must_use]
129 pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self {
130 glam_assert!(axis.is_normalized());
131 let (s, c) = math::sin_cos(angle * 0.5);
132 let v = axis * s;
133 Self::from_xyzw(v.x, v.y, v.z, c)
134 }
135
136 /// Create a quaternion that rotates `v.length()` radians around `v.normalize()`.
137 ///
138 /// `from_scaled_axis(Vec3::ZERO)` results in the identity quaternion.
139 #[inline]
140 #[must_use]
141 pub fn from_scaled_axis(v: Vec3) -> Self {
142 let length = v.length();
143 if length == 0.0 {
144 Self::IDENTITY
145 } else {
146 Self::from_axis_angle(v / length, length)
147 }
148 }
149
150 /// Creates a quaternion from the `angle` (in radians) around the x axis.
151 #[inline]
152 #[must_use]
153 pub fn from_rotation_x(angle: f32) -> Self {
154 let (s, c) = math::sin_cos(angle * 0.5);
155 Self::from_xyzw(s, 0.0, 0.0, c)
156 }
157
158 /// Creates a quaternion from the `angle` (in radians) around the y axis.
159 #[inline]
160 #[must_use]
161 pub fn from_rotation_y(angle: f32) -> Self {
162 let (s, c) = math::sin_cos(angle * 0.5);
163 Self::from_xyzw(0.0, s, 0.0, c)
164 }
165
166 /// Creates a quaternion from the `angle` (in radians) around the z axis.
167 #[inline]
168 #[must_use]
169 pub fn from_rotation_z(angle: f32) -> Self {
170 let (s, c) = math::sin_cos(angle * 0.5);
171 Self::from_xyzw(0.0, 0.0, s, c)
172 }
173
174 /// Creates a quaternion from the given Euler rotation sequence and the angles (in radians).
175 #[inline]
176 #[must_use]
177 pub fn from_euler(euler: EulerRot, a: f32, b: f32, c: f32) -> Self {
178 euler.new_quat(a, b, c)
179 }
180
181 /// From the columns of a 3x3 rotation matrix.
182 #[inline]
183 #[must_use]
184 pub(crate) fn from_rotation_axes(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3) -> Self {
185 // Based on https://github.com/microsoft/DirectXMath `XM$quaternionRotationMatrix`
186 let (m00, m01, m02) = x_axis.into();
187 let (m10, m11, m12) = y_axis.into();
188 let (m20, m21, m22) = z_axis.into();
189 if m22 <= 0.0 {
190 // x^2 + y^2 >= z^2 + w^2
191 let dif10 = m11 - m00;
192 let omm22 = 1.0 - m22;
193 if dif10 <= 0.0 {
194 // x^2 >= y^2
195 let four_xsq = omm22 - dif10;
196 let inv4x = 0.5 / math::sqrt(four_xsq);
197 Self::from_xyzw(
198 four_xsq * inv4x,
199 (m01 + m10) * inv4x,
200 (m02 + m20) * inv4x,
201 (m12 - m21) * inv4x,
202 )
203 } else {
204 // y^2 >= x^2
205 let four_ysq = omm22 + dif10;
206 let inv4y = 0.5 / math::sqrt(four_ysq);
207 Self::from_xyzw(
208 (m01 + m10) * inv4y,
209 four_ysq * inv4y,
210 (m12 + m21) * inv4y,
211 (m20 - m02) * inv4y,
212 )
213 }
214 } else {
215 // z^2 + w^2 >= x^2 + y^2
216 let sum10 = m11 + m00;
217 let opm22 = 1.0 + m22;
218 if sum10 <= 0.0 {
219 // z^2 >= w^2
220 let four_zsq = opm22 - sum10;
221 let inv4z = 0.5 / math::sqrt(four_zsq);
222 Self::from_xyzw(
223 (m02 + m20) * inv4z,
224 (m12 + m21) * inv4z,
225 four_zsq * inv4z,
226 (m01 - m10) * inv4z,
227 )
228 } else {
229 // w^2 >= z^2
230 let four_wsq = opm22 + sum10;
231 let inv4w = 0.5 / math::sqrt(four_wsq);
232 Self::from_xyzw(
233 (m12 - m21) * inv4w,
234 (m20 - m02) * inv4w,
235 (m01 - m10) * inv4w,
236 four_wsq * inv4w,
237 )
238 }
239 }
240 }
241
242 /// Creates a quaternion from a 3x3 rotation matrix.
243 #[inline]
244 #[must_use]
245 pub fn from_mat3(mat: &Mat3) -> Self {
246 Self::from_rotation_axes(mat.x_axis, mat.y_axis, mat.z_axis)
247 }
248
249 /// Creates a quaternion from a 3x3 SIMD aligned rotation matrix.
250 #[inline]
251 #[must_use]
252 pub fn from_mat3a(mat: &Mat3A) -> Self {
253 Self::from_rotation_axes(mat.x_axis.into(), mat.y_axis.into(), mat.z_axis.into())
254 }
255
256 /// Creates a quaternion from a 3x3 rotation matrix inside a homogeneous 4x4 matrix.
257 #[inline]
258 #[must_use]
259 pub fn from_mat4(mat: &Mat4) -> Self {
260 Self::from_rotation_axes(
261 mat.x_axis.truncate(),
262 mat.y_axis.truncate(),
263 mat.z_axis.truncate(),
264 )
265 }
266
267 /// Gets the minimal rotation for transforming `from` to `to`. The rotation is in the
268 /// plane spanned by the two vectors. Will rotate at most 180 degrees.
269 ///
270 /// The inputs must be unit vectors.
271 ///
272 /// `from_rotation_arc(from, to) * from ≈ to`.
273 ///
274 /// For near-singular cases (from≈to and from≈-to) the current implementation
275 /// is only accurate to about 0.001 (for `f32`).
276 ///
277 /// # Panics
278 ///
279 /// Will panic if `from` or `to` are not normalized when `glam_assert` is enabled.
280 #[must_use]
281 pub fn from_rotation_arc(from: Vec3, to: Vec3) -> Self {
282 glam_assert!(from.is_normalized());
283 glam_assert!(to.is_normalized());
284
285 const ONE_MINUS_EPS: f32 = 1.0 - 2.0 * f32::EPSILON;
286 let dot = from.dot(to);
287 if dot > ONE_MINUS_EPS {
288 // 0° singularity: from ≈ to
289 Self::IDENTITY
290 } else if dot < -ONE_MINUS_EPS {
291 // 180° singularity: from ≈ -to
292 use core::f32::consts::PI; // half a turn = 𝛕/2 = 180°
293 Self::from_axis_angle(from.any_orthonormal_vector(), PI)
294 } else {
295 let c = from.cross(to);
296 Self::from_xyzw(c.x, c.y, c.z, 1.0 + dot).normalize()
297 }
298 }
299
300 /// Gets the minimal rotation for transforming `from` to either `to` or `-to`. This means
301 /// that the resulting quaternion will rotate `from` so that it is colinear with `to`.
302 ///
303 /// The rotation is in the plane spanned by the two vectors. Will rotate at most 90
304 /// degrees.
305 ///
306 /// The inputs must be unit vectors.
307 ///
308 /// `to.dot(from_rotation_arc_colinear(from, to) * from).abs() ≈ 1`.
309 ///
310 /// # Panics
311 ///
312 /// Will panic if `from` or `to` are not normalized when `glam_assert` is enabled.
313 #[inline]
314 #[must_use]
315 pub fn from_rotation_arc_colinear(from: Vec3, to: Vec3) -> Self {
316 if from.dot(to) < 0.0 {
317 Self::from_rotation_arc(from, -to)
318 } else {
319 Self::from_rotation_arc(from, to)
320 }
321 }
322
323 /// Gets the minimal rotation for transforming `from` to `to`. The resulting rotation is
324 /// around the z axis. Will rotate at most 180 degrees.
325 ///
326 /// The inputs must be unit vectors.
327 ///
328 /// `from_rotation_arc_2d(from, to) * from ≈ to`.
329 ///
330 /// For near-singular cases (from≈to and from≈-to) the current implementation
331 /// is only accurate to about 0.001 (for `f32`).
332 ///
333 /// # Panics
334 ///
335 /// Will panic if `from` or `to` are not normalized when `glam_assert` is enabled.
336 #[must_use]
337 pub fn from_rotation_arc_2d(from: Vec2, to: Vec2) -> Self {
338 glam_assert!(from.is_normalized());
339 glam_assert!(to.is_normalized());
340
341 const ONE_MINUS_EPSILON: f32 = 1.0 - 2.0 * f32::EPSILON;
342 let dot = from.dot(to);
343 if dot > ONE_MINUS_EPSILON {
344 // 0° singularity: from ≈ to
345 Self::IDENTITY
346 } else if dot < -ONE_MINUS_EPSILON {
347 // 180° singularity: from ≈ -to
348 const COS_FRAC_PI_2: f32 = 0.0;
349 const SIN_FRAC_PI_2: f32 = 1.0;
350 // rotation around z by PI radians
351 Self::from_xyzw(0.0, 0.0, SIN_FRAC_PI_2, COS_FRAC_PI_2)
352 } else {
353 // vector3 cross where z=0
354 let z = from.x * to.y - to.x * from.y;
355 let w = 1.0 + dot;
356 // calculate length with x=0 and y=0 to normalize
357 let len_rcp = 1.0 / math::sqrt(z * z + w * w);
358 Self::from_xyzw(0.0, 0.0, z * len_rcp, w * len_rcp)
359 }
360 }
361
362 /// Returns the rotation axis (normalized) and angle (in radians) of `self`.
363 #[inline]
364 #[must_use]
365 pub fn to_axis_angle(self) -> (Vec3, f32) {
366 const EPSILON: f32 = 1.0e-8;
367 let v = Vec3::new(self.x, self.y, self.z);
368 let length = v.length();
369 if length >= EPSILON {
370 let angle = 2.0 * math::atan2(length, self.w);
371 let axis = v / length;
372 (axis, angle)
373 } else {
374 (Vec3::X, 0.0)
375 }
376 }
377
378 /// Returns the rotation axis scaled by the rotation in radians.
379 #[inline]
380 #[must_use]
381 pub fn to_scaled_axis(self) -> Vec3 {
382 let (axis, angle) = self.to_axis_angle();
383 axis * angle
384 }
385
386 /// Returns the rotation angles for the given euler rotation sequence.
387 #[inline]
388 #[must_use]
389 pub fn to_euler(self, euler: EulerRot) -> (f32, f32, f32) {
390 euler.convert_quat(self)
391 }
392
393 /// `[x, y, z, w]`
394 #[inline]
395 #[must_use]
396 pub fn to_array(&self) -> [f32; 4] {
397 [self.x, self.y, self.z, self.w]
398 }
399
400 /// Returns the vector part of the quaternion.
401 #[inline]
402 #[must_use]
403 pub fn xyz(self) -> Vec3 {
404 Vec3::new(self.x, self.y, self.z)
405 }
406
407 /// Returns the quaternion conjugate of `self`. For a unit quaternion the
408 /// conjugate is also the inverse.
409 #[inline]
410 #[must_use]
411 pub fn conjugate(self) -> Self {
412 const SIGN: f32x4 = f32x4::from_array([-1.0, -1.0, -1.0, 1.0]);
413 Self(self.0.mul(SIGN))
414 }
415
416 /// Returns the inverse of a normalized quaternion.
417 ///
418 /// Typically quaternion inverse returns the conjugate of a normalized quaternion.
419 /// Because `self` is assumed to already be unit length this method *does not* normalize
420 /// before returning the conjugate.
421 ///
422 /// # Panics
423 ///
424 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
425 #[inline]
426 #[must_use]
427 pub fn inverse(self) -> Self {
428 glam_assert!(self.is_normalized());
429 self.conjugate()
430 }
431
432 /// Computes the dot product of `self` and `rhs`. The dot product is
433 /// equal to the cosine of the angle between two quaternion rotations.
434 #[inline]
435 #[must_use]
436 pub fn dot(self, rhs: Self) -> f32 {
437 Vec4::from(self).dot(Vec4::from(rhs))
438 }
439
440 /// Computes the length of `self`.
441 #[doc(alias = "magnitude")]
442 #[inline]
443 #[must_use]
444 pub fn length(self) -> f32 {
445 Vec4::from(self).length()
446 }
447
448 /// Computes the squared length of `self`.
449 ///
450 /// This is generally faster than `length()` as it avoids a square
451 /// root operation.
452 #[doc(alias = "magnitude2")]
453 #[inline]
454 #[must_use]
455 pub fn length_squared(self) -> f32 {
456 Vec4::from(self).length_squared()
457 }
458
459 /// Computes `1.0 / length()`.
460 ///
461 /// For valid results, `self` must _not_ be of length zero.
462 #[inline]
463 #[must_use]
464 pub fn length_recip(self) -> f32 {
465 Vec4::from(self).length_recip()
466 }
467
468 /// Returns `self` normalized to length 1.0.
469 ///
470 /// For valid results, `self` must _not_ be of length zero.
471 ///
472 /// Panics
473 ///
474 /// Will panic if `self` is zero length when `glam_assert` is enabled.
475 #[inline]
476 #[must_use]
477 pub fn normalize(self) -> Self {
478 Self::from_vec4(Vec4::from(self).normalize())
479 }
480
481 /// Returns `true` if, and only if, all elements are finite.
482 /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
483 #[inline]
484 #[must_use]
485 pub fn is_finite(self) -> bool {
486 Vec4::from(self).is_finite()
487 }
488
489 #[inline]
490 #[must_use]
491 pub fn is_nan(self) -> bool {
492 Vec4::from(self).is_nan()
493 }
494
495 /// Returns whether `self` of length `1.0` or not.
496 ///
497 /// Uses a precision threshold of `1e-6`.
498 #[inline]
499 #[must_use]
500 pub fn is_normalized(self) -> bool {
501 Vec4::from(self).is_normalized()
502 }
503
504 #[inline]
505 #[must_use]
506 pub fn is_near_identity(self) -> bool {
507 // Based on https://github.com/nfrechette/rtm `rtm::quat_near_identity`
508 let threshold_angle = 0.002_847_144_6;
509 // Because of floating point precision, we cannot represent very small rotations.
510 // The closest f32 to 1.0 that is not 1.0 itself yields:
511 // 0.99999994.acos() * 2.0 = 0.000690533954 rad
512 //
513 // An error threshold of 1.e-6 is used by default.
514 // (1.0 - 1.e-6).acos() * 2.0 = 0.00284714461 rad
515 // (1.0 - 1.e-7).acos() * 2.0 = 0.00097656250 rad
516 //
517 // We don't really care about the angle value itself, only if it's close to 0.
518 // This will happen whenever quat.w is close to 1.0.
519 // If the quat.w is close to -1.0, the angle will be near 2*PI which is close to
520 // a negative 0 rotation. By forcing quat.w to be positive, we'll end up with
521 // the shortest path.
522 let positive_w_angle = math::acos_approx(math::abs(self.w)) * 2.0;
523 positive_w_angle < threshold_angle
524 }
525
526 /// Returns the angle (in radians) for the minimal rotation
527 /// for transforming this quaternion into another.
528 ///
529 /// Both quaternions must be normalized.
530 ///
531 /// # Panics
532 ///
533 /// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled.
534 #[inline]
535 #[must_use]
536 pub fn angle_between(self, rhs: Self) -> f32 {
537 glam_assert!(self.is_normalized() && rhs.is_normalized());
538 math::acos_approx(math::abs(self.dot(rhs))) * 2.0
539 }
540
541 /// Rotates towards `rhs` up to `max_angle` (in radians).
542 ///
543 /// When `max_angle` is `0.0`, the result will be equal to `self`. When `max_angle` is equal to
544 /// `self.angle_between(rhs)`, the result will be equal to `rhs`. If `max_angle` is negative,
545 /// rotates towards the exact opposite of `rhs`. Will not go past the target.
546 ///
547 /// Both quaternions must be normalized.
548 ///
549 /// # Panics
550 ///
551 /// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled.
552 #[inline]
553 #[must_use]
554 pub fn rotate_towards(&self, rhs: Self, max_angle: f32) -> Self {
555 glam_assert!(self.is_normalized() && rhs.is_normalized());
556 let angle = self.angle_between(rhs);
557 if angle <= 1e-4 {
558 return *self;
559 }
560 let s = (max_angle / angle).clamp(-1.0, 1.0);
561 self.slerp(rhs, s)
562 }
563
564 /// Returns true if the absolute difference of all elements between `self` and `rhs`
565 /// is less than or equal to `max_abs_diff`.
566 ///
567 /// This can be used to compare if two quaternions contain similar elements. It works
568 /// best when comparing with a known value. The `max_abs_diff` that should be used used
569 /// depends on the values being compared against.
570 ///
571 /// For more see
572 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
573 #[inline]
574 #[must_use]
575 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
576 Vec4::from(self).abs_diff_eq(Vec4::from(rhs), max_abs_diff)
577 }
578
579 /// Performs a linear interpolation between `self` and `rhs` based on
580 /// the value `s`.
581 ///
582 /// When `s` is `0.0`, the result will be equal to `self`. When `s`
583 /// is `1.0`, the result will be equal to `rhs`.
584 ///
585 /// # Panics
586 ///
587 /// Will panic if `self` or `end` are not normalized when `glam_assert` is enabled.
588 #[doc(alias = "mix")]
589 #[inline]
590 #[must_use]
591 pub fn lerp(self, end: Self, s: f32) -> Self {
592 glam_assert!(self.is_normalized());
593 glam_assert!(end.is_normalized());
594
595 const NEG_ZERO: f32x4 = f32x4::from_array([-0.0; 4]);
596 let start = self.0;
597 let end = end.0;
598 let dot = dot4_into_f32x4(start, end);
599 // Calculate the bias, if the dot product is positive or zero, there is no bias
600 // but if it is negative, we want to flip the 'end' rotation XYZW components
601 let bias = f32x4_bitand(dot, NEG_ZERO);
602 let interpolated = start + ((f32x4_bitxor(end, bias) - start) * f32x4::splat(s));
603 Quat(interpolated).normalize()
604 }
605
606 /// Performs a spherical linear interpolation between `self` and `end`
607 /// based on the value `s`.
608 ///
609 /// When `s` is `0.0`, the result will be equal to `self`. When `s`
610 /// is `1.0`, the result will be equal to `end`.
611 ///
612 /// # Panics
613 ///
614 /// Will panic if `self` or `end` are not normalized when `glam_assert` is enabled.
615 #[inline]
616 #[must_use]
617 pub fn slerp(self, mut end: Self, s: f32) -> Self {
618 // http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/
619 glam_assert!(self.is_normalized());
620 glam_assert!(end.is_normalized());
621
622 const DOT_THRESHOLD: f32 = 0.9995;
623
624 // Note that a rotation can be represented by two quaternions: `q` and
625 // `-q`. The slerp path between `q` and `end` will be different from the
626 // path between `-q` and `end`. One path will take the long way around and
627 // one will take the short way. In order to correct for this, the `dot`
628 // product between `self` and `end` should be positive. If the `dot`
629 // product is negative, slerp between `self` and `-end`.
630 let mut dot = self.dot(end);
631 if dot < 0.0 {
632 end = -end;
633 dot = -dot;
634 }
635
636 if dot > DOT_THRESHOLD {
637 // assumes lerp returns a normalized quaternion
638 self.lerp(end, s)
639 } else {
640 let theta = math::acos_approx(dot);
641
642 let x = math::sin(theta * (1.0 - s));
643 let y = math::sin(theta * s);
644 let z = math::sin(theta);
645 let w = 0.0;
646 let tmp = f32x4::from_array([x, y, z, w]);
647
648 let scale1 = simd_swizzle!(tmp, [0, 0, 0, 0]);
649 let scale2 = simd_swizzle!(tmp, [1, 1, 1, 1]);
650 let theta_sin = simd_swizzle!(tmp, [2, 2, 2, 2]);
651
652 Self(self.0.mul(scale1).add(end.0.mul(scale2)).div(theta_sin))
653 }
654 }
655
656 /// Multiplies a quaternion and a 3D vector, returning the rotated vector.
657 ///
658 /// # Panics
659 ///
660 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
661 #[inline]
662 #[must_use]
663 pub fn mul_vec3(self, rhs: Vec3) -> Vec3 {
664 glam_assert!(self.is_normalized());
665
666 self.mul_vec3a(rhs.into()).into()
667 }
668
669 /// Multiplies two quaternions. If they each represent a rotation, the result will
670 /// represent the combined rotation.
671 ///
672 /// Note that due to floating point rounding the result may not be perfectly normalized.
673 ///
674 /// # Panics
675 ///
676 /// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled.
677 #[inline]
678 #[must_use]
679 pub fn mul_quat(self, rhs: Self) -> Self {
680 glam_assert!(self.is_normalized());
681 glam_assert!(rhs.is_normalized());
682
683 let lhs = self.0;
684 let rhs = rhs.0;
685
686 const CONTROL_WZYX: f32x4 = f32x4::from_array([1.0, -1.0, 1.0, -1.0]);
687 const CONTROL_ZWXY: f32x4 = f32x4::from_array([1.0, 1.0, -1.0, -1.0]);
688 const CONTROL_YXWZ: f32x4 = f32x4::from_array([-1.0, 1.0, 1.0, -1.0]);
689
690 let r_xxxx = simd_swizzle!(lhs, [0, 0, 0, 0]);
691 let r_yyyy = simd_swizzle!(lhs, [1, 1, 1, 1]);
692 let r_zzzz = simd_swizzle!(lhs, [2, 2, 2, 2]);
693 let r_wwww = simd_swizzle!(lhs, [3, 3, 3, 3]);
694
695 let lxrw_lyrw_lzrw_lwrw = r_wwww * rhs;
696 let l_wzyx = simd_swizzle!(rhs, [3, 2, 1, 0]);
697
698 let lwrx_lzrx_lyrx_lxrx = r_xxxx * l_wzyx;
699 let l_zwxy = simd_swizzle!(l_wzyx, [1, 0, 3, 2]);
700
701 let lwrx_nlzrx_lyrx_nlxrx = lwrx_lzrx_lyrx_lxrx * CONTROL_WZYX;
702
703 let lzry_lwry_lxry_lyry = r_yyyy * l_zwxy;
704 let l_yxwz = simd_swizzle!(l_zwxy, [3, 2, 1, 0]);
705
706 let lzry_lwry_nlxry_nlyry = lzry_lwry_lxry_lyry * CONTROL_ZWXY;
707
708 let lyrz_lxrz_lwrz_lzrz = r_zzzz * l_yxwz;
709 let result0 = lxrw_lyrw_lzrw_lwrw + lwrx_nlzrx_lyrx_nlxrx;
710
711 let nlyrz_lxrz_lwrz_wlzrz = lyrz_lxrz_lwrz_lzrz * CONTROL_YXWZ;
712 let result1 = lzry_lwry_nlxry_nlyry + nlyrz_lxrz_lwrz_wlzrz;
713 Self(result0 + result1)
714 }
715
716 /// Creates a quaternion from a 3x3 rotation matrix inside a 3D affine transform.
717 #[inline]
718 #[must_use]
719 pub fn from_affine3(a: &crate::Affine3A) -> Self {
720 #[allow(clippy::useless_conversion)]
721 Self::from_rotation_axes(
722 a.matrix3.x_axis.into(),
723 a.matrix3.y_axis.into(),
724 a.matrix3.z_axis.into(),
725 )
726 }
727
728 /// Multiplies a quaternion and a 3D vector, returning the rotated vector.
729 #[inline]
730 #[must_use]
731 pub fn mul_vec3a(self, rhs: Vec3A) -> Vec3A {
732 const TWO: f32x4 = f32x4::from_array([2.0; 4]);
733 let w = simd_swizzle!(self.0, [3, 3, 3, 3]);
734 let b = self.0;
735 let b2 = dot3_into_f32x4(b, b);
736 Vec3A(
737 rhs.0
738 .mul(w.mul(w).sub(b2))
739 .add(b.mul(dot3_into_f32x4(rhs.0, b).mul(TWO)))
740 .add(Vec3A(b).cross(rhs).0.mul(w.mul(TWO))),
741 )
742 }
743
744 #[inline]
745 #[must_use]
746 pub fn as_dquat(self) -> DQuat {
747 DQuat::from_xyzw(self.x as f64, self.y as f64, self.z as f64, self.w as f64)
748 }
749 }
750
751 #[cfg(not(target_arch = "spirv"))]
752 impl fmt::Debug for Quat {
753 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
754 fmt.debug_tuple(stringify!(Quat))
755 .field(&self.x)
756 .field(&self.y)
757 .field(&self.z)
758 .field(&self.w)
759 .finish()
760 }
761 }
762
763 #[cfg(not(target_arch = "spirv"))]
764 impl fmt::Display for Quat {
765 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
766 if let Some(p) = f.precision() {
767 write!(
768 f,
769 "[{:.*}, {:.*}, {:.*}, {:.*}]",
770 p, self.x, p, self.y, p, self.z, p, self.w
771 )
772 } else {
773 write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
774 }
775 }
776 }
777
778 impl Add<Quat> for Quat {
779 type Output = Self;
780 /// Adds two quaternions.
781 ///
782 /// The sum is not guaranteed to be normalized.
783 ///
784 /// Note that addition is not the same as combining the rotations represented by the
785 /// two quaternions! That corresponds to multiplication.
786 #[inline]
787 fn add(self, rhs: Self) -> Self {
788 Self::from_vec4(Vec4::from(self) + Vec4::from(rhs))
789 }
790 }
791
792 impl Sub<Quat> for Quat {
793 type Output = Self;
794 /// Subtracts the `rhs` quaternion from `self`.
795 ///
796 /// The difference is not guaranteed to be normalized.
797 #[inline]
798 fn sub(self, rhs: Self) -> Self {
799 Self::from_vec4(Vec4::from(self) - Vec4::from(rhs))
800 }
801 }
802
803 impl Mul<f32> for Quat {
804 type Output = Self;
805 /// Multiplies a quaternion by a scalar value.
806 ///
807 /// The product is not guaranteed to be normalized.
808 #[inline]
809 fn mul(self, rhs: f32) -> Self {
810 Self::from_vec4(Vec4::from(self) * rhs)
811 }
812 }
813
814 impl Div<f32> for Quat {
815 type Output = Self;
816 /// Divides a quaternion by a scalar value.
817 /// The quotient is not guaranteed to be normalized.
818 #[inline]
819 fn div(self, rhs: f32) -> Self {
820 Self::from_vec4(Vec4::from(self) / rhs)
821 }
822 }
823
824 impl Mul<Quat> for Quat {
825 type Output = Self;
826 /// Multiplies two quaternions. If they each represent a rotation, the result will
827 /// represent the combined rotation.
828 ///
829 /// Note that due to floating point rounding the result may not be perfectly
830 /// normalized.
831 ///
832 /// # Panics
833 ///
834 /// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled.
835 #[inline]
836 fn mul(self, rhs: Self) -> Self {
837 self.mul_quat(rhs)
838 }
839 }
840
841 impl MulAssign<Quat> for Quat {
842 /// Multiplies two quaternions. If they each represent a rotation, the result will
843 /// represent the combined rotation.
844 ///
845 /// Note that due to floating point rounding the result may not be perfectly
846 /// normalized.
847 ///
848 /// # Panics
849 ///
850 /// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled.
851 #[inline]
852 fn mul_assign(&mut self, rhs: Self) {
853 *self = self.mul_quat(rhs);
854 }
855 }
856
857 impl Mul<Vec3> for Quat {
858 type Output = Vec3;
859 /// Multiplies a quaternion and a 3D vector, returning the rotated vector.
860 ///
861 /// # Panics
862 ///
863 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
864 #[inline]
865 fn mul(self, rhs: Vec3) -> Self::Output {
866 self.mul_vec3(rhs)
867 }
868 }
869
870 impl Neg for Quat {
871 type Output = Self;
872 #[inline]
873 fn neg(self) -> Self {
874 self * -1.0
875 }
876 }
877
878 impl Default for Quat {
879 #[inline]
880 fn default() -> Self {
881 Self::IDENTITY
882 }
883 }
884
885 impl PartialEq for Quat {
886 #[inline]
887 fn eq(&self, rhs: &Self) -> bool {
888 Vec4::from(*self).eq(&Vec4::from(*rhs))
889 }
890 }
891
892 #[cfg(not(target_arch = "spirv"))]
893 impl AsRef<[f32; 4]> for Quat {
894 #[inline]
895 fn as_ref(&self) -> &[f32; 4] {
896 unsafe { &*(self as *const Self as *const [f32; 4]) }
897 }
898 }
899
900 impl Sum<Self> for Quat {
901 fn sum<I>(iter: I) -> Self
902 where
903 I: Iterator<Item = Self>,
904 {
905 iter.fold(Self::ZERO, Self::add)
906 }
907 }
908
909 impl<'a> Sum<&'a Self> for Quat {
910 fn sum<I>(iter: I) -> Self
911 where
912 I: Iterator<Item = &'a Self>,
913 {
914 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
915 }
916 }
917
918 impl Product for Quat {
919 fn product<I>(iter: I) -> Self
920 where
921 I: Iterator<Item = Self>,
922 {
923 iter.fold(Self::IDENTITY, Self::mul)
924 }
925 }
926
927 impl<'a> Product<&'a Self> for Quat {
928 fn product<I>(iter: I) -> Self
929 where
930 I: Iterator<Item = &'a Self>,
931 {
932 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
933 }
934 }
935
936 impl Mul<Vec3A> for Quat {
937 type Output = Vec3A;
938 #[inline]
939 fn mul(self, rhs: Vec3A) -> Self::Output {
940 self.mul_vec3a(rhs)
941 }
942 }
943
944 impl From<Quat> for Vec4 {
945 #[inline]
946 fn from(q: Quat) -> Self {
947 Self(q.0)
948 }
949 }
950
951 impl From<Quat> for (f32, f32, f32, f32) {
952 #[inline]
953 fn from(q: Quat) -> Self {
954 Vec4::from(q).into()
955 }
956 }
957
958 impl From<Quat> for [f32; 4] {
959 #[inline]
960 fn from(q: Quat) -> Self {
961 Vec4::from(q).into()
962 }
963 }
964
965 impl From<Quat> for f32x4 {
966 #[inline]
967 fn from(q: Quat) -> Self {
968 q.0
969 }
970 }
971
972 impl Deref for Quat {
973 type Target = crate::deref::Vec4<f32>;
974 #[inline]
975 fn deref(&self) -> &Self::Target {
976 unsafe { &*(self as *const Self).cast() }
977 }
978 }
979
980 impl DerefMut for Quat {
981 #[inline]
982 fn deref_mut(&mut self) -> &mut Self::Target {
983 unsafe { &mut *(self as *mut Self).cast() }
984 }
985 }