Debian Codegraph @

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

raw
1 // Generated from quat.rs.tera template. Edit the template, not the generated file.
2
3 use crate::{
4 euler::{EulerFromQuaternion, EulerRot, EulerToQuaternion},
5 f32::math,
6 wasm32::*,
7 DQuat, Mat3, Mat3A, Mat4, Vec2, Vec3, Vec3A, Vec4,
8 };
9
10 use core::arch::wasm32::*;
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) v128);
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(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::from_xyzw(a[0], a[1], a[2], a[3])
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: v128 = v128_from_f32x4([-1.0, -1.0, -1.0, 1.0]);
413 Self(f32x4_mul(self.0, 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: v128 = v128_from_f32x4([-0.0; 4]);
596 let start = self.0;
597 let end = end.0;
598 let dot = dot4_into_v128(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 = v128_and(dot, NEG_ZERO);
602 let interpolated = f32x4_add(
603 f32x4_mul(f32x4_sub(v128_xor(end, bias), start), f32x4_splat(s)),
604 start,
605 );
606 Quat(interpolated).normalize()
607 }
608
609 /// Performs a spherical linear interpolation between `self` and `end`
610 /// based on the value `s`.
611 ///
612 /// When `s` is `0.0`, the result will be equal to `self`. When `s`
613 /// is `1.0`, the result will be equal to `end`.
614 ///
615 /// # Panics
616 ///
617 /// Will panic if `self` or `end` are not normalized when `glam_assert` is enabled.
618 #[inline]
619 #[must_use]
620 pub fn slerp(self, mut end: Self, s: f32) -> Self {
621 // http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/
622 glam_assert!(self.is_normalized());
623 glam_assert!(end.is_normalized());
624
625 const DOT_THRESHOLD: f32 = 0.9995;
626
627 // Note that a rotation can be represented by two quaternions: `q` and
628 // `-q`. The slerp path between `q` and `end` will be different from the
629 // path between `-q` and `end`. One path will take the long way around and
630 // one will take the short way. In order to correct for this, the `dot`
631 // product between `self` and `end` should be positive. If the `dot`
632 // product is negative, slerp between `self` and `-end`.
633 let mut dot = self.dot(end);
634 if dot < 0.0 {
635 end = -end;
636 dot = -dot;
637 }
638
639 if dot > DOT_THRESHOLD {
640 // assumes lerp returns a normalized quaternion
641 self.lerp(end, s)
642 } else {
643 let theta = math::acos_approx(dot);
644
645 // TODO: v128_sin is broken
646 // let x = 1.0 - s;
647 // let y = s;
648 // let z = 1.0;
649 // let w = 0.0;
650 // let tmp = f32x4_mul(f32x4_splat(theta), f32x4(x, y, z, w));
651 // let tmp = v128_sin(tmp);
652 let x = math::sin(theta * (1.0 - s));
653 let y = math::sin(theta * s);
654 let z = math::sin(theta);
655 let w = 0.0;
656 let tmp = f32x4(x, y, z, w);
657
658 let scale1 = i32x4_shuffle::<0, 0, 4, 4>(tmp, tmp);
659 let scale2 = i32x4_shuffle::<1, 1, 5, 5>(tmp, tmp);
660 let theta_sin = i32x4_shuffle::<2, 2, 6, 6>(tmp, tmp);
661
662 Self(f32x4_div(
663 f32x4_add(f32x4_mul(self.0, scale1), f32x4_mul(end.0, scale2)),
664 theta_sin,
665 ))
666 }
667 }
668
669 /// Multiplies a quaternion and a 3D vector, returning the rotated vector.
670 ///
671 /// # Panics
672 ///
673 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
674 #[inline]
675 #[must_use]
676 pub fn mul_vec3(self, rhs: Vec3) -> Vec3 {
677 glam_assert!(self.is_normalized());
678
679 self.mul_vec3a(rhs.into()).into()
680 }
681
682 /// Multiplies two quaternions. If they each represent a rotation, the result will
683 /// represent the combined rotation.
684 ///
685 /// Note that due to floating point rounding the result may not be perfectly normalized.
686 ///
687 /// # Panics
688 ///
689 /// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled.
690 #[inline]
691 #[must_use]
692 pub fn mul_quat(self, rhs: Self) -> Self {
693 glam_assert!(self.is_normalized());
694 glam_assert!(rhs.is_normalized());
695
696 let lhs = self.0;
697 let rhs = rhs.0;
698
699 const CONTROL_WZYX: v128 = v128_from_f32x4([1.0, -1.0, 1.0, -1.0]);
700 const CONTROL_ZWXY: v128 = v128_from_f32x4([1.0, 1.0, -1.0, -1.0]);
701 const CONTROL_YXWZ: v128 = v128_from_f32x4([-1.0, 1.0, 1.0, -1.0]);
702
703 let r_xxxx = i32x4_shuffle::<0, 0, 4, 4>(lhs, lhs);
704 let r_yyyy = i32x4_shuffle::<1, 1, 5, 5>(lhs, lhs);
705 let r_zzzz = i32x4_shuffle::<2, 2, 6, 6>(lhs, lhs);
706 let r_wwww = i32x4_shuffle::<3, 3, 7, 7>(lhs, lhs);
707
708 let lxrw_lyrw_lzrw_lwrw = f32x4_mul(r_wwww, rhs);
709 let l_wzyx = i32x4_shuffle::<3, 2, 5, 4>(rhs, rhs);
710
711 let lwrx_lzrx_lyrx_lxrx = f32x4_mul(r_xxxx, l_wzyx);
712 let l_zwxy = i32x4_shuffle::<1, 0, 7, 6>(l_wzyx, l_wzyx);
713
714 let lwrx_nlzrx_lyrx_nlxrx = f32x4_mul(lwrx_lzrx_lyrx_lxrx, CONTROL_WZYX);
715
716 let lzry_lwry_lxry_lyry = f32x4_mul(r_yyyy, l_zwxy);
717 let l_yxwz = i32x4_shuffle::<3, 2, 5, 4>(l_zwxy, l_zwxy);
718
719 let lzry_lwry_nlxry_nlyry = f32x4_mul(lzry_lwry_lxry_lyry, CONTROL_ZWXY);
720
721 let lyrz_lxrz_lwrz_lzrz = f32x4_mul(r_zzzz, l_yxwz);
722 let result0 = f32x4_add(lxrw_lyrw_lzrw_lwrw, lwrx_nlzrx_lyrx_nlxrx);
723
724 let nlyrz_lxrz_lwrz_wlzrz = f32x4_mul(lyrz_lxrz_lwrz_lzrz, CONTROL_YXWZ);
725 let result1 = f32x4_add(lzry_lwry_nlxry_nlyry, nlyrz_lxrz_lwrz_wlzrz);
726
727 Self(f32x4_add(result0, result1))
728 }
729
730 /// Creates a quaternion from a 3x3 rotation matrix inside a 3D affine transform.
731 #[inline]
732 #[must_use]
733 pub fn from_affine3(a: &crate::Affine3A) -> Self {
734 #[allow(clippy::useless_conversion)]
735 Self::from_rotation_axes(
736 a.matrix3.x_axis.into(),
737 a.matrix3.y_axis.into(),
738 a.matrix3.z_axis.into(),
739 )
740 }
741
742 /// Multiplies a quaternion and a 3D vector, returning the rotated vector.
743 #[inline]
744 #[must_use]
745 pub fn mul_vec3a(self, rhs: Vec3A) -> Vec3A {
746 const TWO: v128 = v128_from_f32x4([2.0; 4]);
747 let w = i32x4_shuffle::<3, 3, 7, 7>(self.0, self.0);
748 let b = self.0;
749 let b2 = dot3_into_v128(b, b);
750 Vec3A(f32x4_add(
751 f32x4_add(
752 f32x4_mul(rhs.0, f32x4_sub(f32x4_mul(w, w), b2)),
753 f32x4_mul(b, f32x4_mul(dot3_into_v128(rhs.0, b), TWO)),
754 ),
755 f32x4_mul(Vec3A(b).cross(rhs).into(), f32x4_mul(w, TWO)),
756 ))
757 }
758
759 #[inline]
760 #[must_use]
761 pub fn as_dquat(self) -> DQuat {
762 DQuat::from_xyzw(self.x as f64, self.y as f64, self.z as f64, self.w as f64)
763 }
764 }
765
766 #[cfg(not(target_arch = "spirv"))]
767 impl fmt::Debug for Quat {
768 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
769 fmt.debug_tuple(stringify!(Quat))
770 .field(&self.x)
771 .field(&self.y)
772 .field(&self.z)
773 .field(&self.w)
774 .finish()
775 }
776 }
777
778 #[cfg(not(target_arch = "spirv"))]
779 impl fmt::Display for Quat {
780 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
781 if let Some(p) = f.precision() {
782 write!(
783 f,
784 "[{:.*}, {:.*}, {:.*}, {:.*}]",
785 p, self.x, p, self.y, p, self.z, p, self.w
786 )
787 } else {
788 write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
789 }
790 }
791 }
792
793 impl Add<Quat> for Quat {
794 type Output = Self;
795 /// Adds two quaternions.
796 ///
797 /// The sum is not guaranteed to be normalized.
798 ///
799 /// Note that addition is not the same as combining the rotations represented by the
800 /// two quaternions! That corresponds to multiplication.
801 #[inline]
802 fn add(self, rhs: Self) -> Self {
803 Self::from_vec4(Vec4::from(self) + Vec4::from(rhs))
804 }
805 }
806
807 impl Sub<Quat> for Quat {
808 type Output = Self;
809 /// Subtracts the `rhs` quaternion from `self`.
810 ///
811 /// The difference is not guaranteed to be normalized.
812 #[inline]
813 fn sub(self, rhs: Self) -> Self {
814 Self::from_vec4(Vec4::from(self) - Vec4::from(rhs))
815 }
816 }
817
818 impl Mul<f32> for Quat {
819 type Output = Self;
820 /// Multiplies a quaternion by a scalar value.
821 ///
822 /// The product is not guaranteed to be normalized.
823 #[inline]
824 fn mul(self, rhs: f32) -> Self {
825 Self::from_vec4(Vec4::from(self) * rhs)
826 }
827 }
828
829 impl Div<f32> for Quat {
830 type Output = Self;
831 /// Divides a quaternion by a scalar value.
832 /// The quotient is not guaranteed to be normalized.
833 #[inline]
834 fn div(self, rhs: f32) -> Self {
835 Self::from_vec4(Vec4::from(self) / rhs)
836 }
837 }
838
839 impl Mul<Quat> for Quat {
840 type Output = Self;
841 /// Multiplies two quaternions. If they each represent a rotation, the result will
842 /// represent the combined rotation.
843 ///
844 /// Note that due to floating point rounding the result may not be perfectly
845 /// normalized.
846 ///
847 /// # Panics
848 ///
849 /// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled.
850 #[inline]
851 fn mul(self, rhs: Self) -> Self {
852 self.mul_quat(rhs)
853 }
854 }
855
856 impl MulAssign<Quat> for Quat {
857 /// Multiplies two quaternions. If they each represent a rotation, the result will
858 /// represent the combined rotation.
859 ///
860 /// Note that due to floating point rounding the result may not be perfectly
861 /// normalized.
862 ///
863 /// # Panics
864 ///
865 /// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled.
866 #[inline]
867 fn mul_assign(&mut self, rhs: Self) {
868 *self = self.mul_quat(rhs);
869 }
870 }
871
872 impl Mul<Vec3> for Quat {
873 type Output = Vec3;
874 /// Multiplies a quaternion and a 3D vector, returning the rotated vector.
875 ///
876 /// # Panics
877 ///
878 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
879 #[inline]
880 fn mul(self, rhs: Vec3) -> Self::Output {
881 self.mul_vec3(rhs)
882 }
883 }
884
885 impl Neg for Quat {
886 type Output = Self;
887 #[inline]
888 fn neg(self) -> Self {
889 self * -1.0
890 }
891 }
892
893 impl Default for Quat {
894 #[inline]
895 fn default() -> Self {
896 Self::IDENTITY
897 }
898 }
899
900 impl PartialEq for Quat {
901 #[inline]
902 fn eq(&self, rhs: &Self) -> bool {
903 Vec4::from(*self).eq(&Vec4::from(*rhs))
904 }
905 }
906
907 #[cfg(not(target_arch = "spirv"))]
908 impl AsRef<[f32; 4]> for Quat {
909 #[inline]
910 fn as_ref(&self) -> &[f32; 4] {
911 unsafe { &*(self as *const Self as *const [f32; 4]) }
912 }
913 }
914
915 impl Sum<Self> for Quat {
916 fn sum<I>(iter: I) -> Self
917 where
918 I: Iterator<Item = Self>,
919 {
920 iter.fold(Self::ZERO, Self::add)
921 }
922 }
923
924 impl<'a> Sum<&'a Self> for Quat {
925 fn sum<I>(iter: I) -> Self
926 where
927 I: Iterator<Item = &'a Self>,
928 {
929 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
930 }
931 }
932
933 impl Product for Quat {
934 fn product<I>(iter: I) -> Self
935 where
936 I: Iterator<Item = Self>,
937 {
938 iter.fold(Self::IDENTITY, Self::mul)
939 }
940 }
941
942 impl<'a> Product<&'a Self> for Quat {
943 fn product<I>(iter: I) -> Self
944 where
945 I: Iterator<Item = &'a Self>,
946 {
947 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
948 }
949 }
950
951 impl Mul<Vec3A> for Quat {
952 type Output = Vec3A;
953 #[inline]
954 fn mul(self, rhs: Vec3A) -> Self::Output {
955 self.mul_vec3a(rhs)
956 }
957 }
958
959 impl From<Quat> for Vec4 {
960 #[inline]
961 fn from(q: Quat) -> Self {
962 Self(q.0)
963 }
964 }
965
966 impl From<Quat> for (f32, f32, f32, f32) {
967 #[inline]
968 fn from(q: Quat) -> Self {
969 Vec4::from(q).into()
970 }
971 }
972
973 impl From<Quat> for [f32; 4] {
974 #[inline]
975 fn from(q: Quat) -> Self {
976 Vec4::from(q).into()
977 }
978 }
979
980 impl From<Quat> for v128 {
981 #[inline]
982 fn from(q: Quat) -> Self {
983 q.0
984 }
985 }
986
987 impl Deref for Quat {
988 type Target = crate::deref::Vec4<f32>;
989 #[inline]
990 fn deref(&self) -> &Self::Target {
991 unsafe { &*(self as *const Self).cast() }
992 }
993 }
994
995 impl DerefMut for Quat {
996 #[inline]
997 fn deref_mut(&mut self) -> &mut Self::Target {
998 unsafe { &mut *(self as *mut Self).cast() }
999 }
1000 }