Debian Codegraph @

debian/missing-sources/glam-0.22.0/src/f64/dquat.rs

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