nalgebra/geometry/
rotation_specialization.rs

1#[cfg(feature = "arbitrary")]
2use crate::base::storage::Owned;
3#[cfg(feature = "arbitrary")]
4use quickcheck::{Arbitrary, Gen};
5
6use num::Zero;
7
8#[cfg(feature = "rand-no-std")]
9use rand::{
10    distributions::{uniform::SampleUniform, Distribution, OpenClosed01, Standard, Uniform},
11    Rng,
12};
13
14use simba::scalar::RealField;
15use simba::simd::{SimdBool, SimdRealField};
16use std::ops::Neg;
17
18use crate::base::dimension::{U1, U2, U3};
19use crate::base::storage::Storage;
20use crate::base::{
21    Matrix2, Matrix3, SMatrix, SVector, Unit, UnitVector3, Vector, Vector1, Vector2, Vector3,
22};
23
24use crate::geometry::{Rotation2, Rotation3, UnitComplex, UnitQuaternion};
25
26/*
27 *
28 * 2D Rotation matrix.
29 *
30 */
31/// # Construction from a 2D rotation angle
32impl<T: SimdRealField> Rotation2<T> {
33    /// Builds a 2 dimensional rotation matrix from an angle in radian.
34    ///
35    /// # Example
36    ///
37    /// ```
38    /// # #[macro_use] extern crate approx;
39    /// # use std::f32;
40    /// # use nalgebra::{Rotation2, Point2};
41    /// let rot = Rotation2::new(f32::consts::FRAC_PI_2);
42    ///
43    /// assert_relative_eq!(rot * Point2::new(3.0, 4.0), Point2::new(-4.0, 3.0));
44    /// ```
45    pub fn new(angle: T) -> Self {
46        let (sia, coa) = angle.simd_sin_cos();
47        Self::from_matrix_unchecked(Matrix2::new(coa.clone(), -sia.clone(), sia, coa))
48    }
49
50    /// Builds a 2 dimensional rotation matrix from an angle in radian wrapped in a 1-dimensional vector.
51    ///
52    ///
53    /// This is generally used in the context of generic programming. Using
54    /// the `::new(angle)` method instead is more common.
55    #[inline]
56    pub fn from_scaled_axis<SB: Storage<T, U1>>(axisangle: Vector<T, U1, SB>) -> Self {
57        Self::new(axisangle[0].clone())
58    }
59}
60
61/// # Construction from an existing 2D matrix or rotations
62impl<T: SimdRealField> Rotation2<T> {
63    /// Builds a rotation from a basis assumed to be orthonormal.
64    ///
65    /// In order to get a valid rotation matrix, the input must be an
66    /// orthonormal basis, i.e., all vectors are normalized, and the are
67    /// all orthogonal to each other. These invariants are not checked
68    /// by this method.
69    pub fn from_basis_unchecked(basis: &[Vector2<T>; 2]) -> Self {
70        let mat = Matrix2::from_columns(&basis[..]);
71        Self::from_matrix_unchecked(mat)
72    }
73
74    /// Builds a rotation matrix by extracting the rotation part of the given transformation `m`.
75    ///
76    /// This is an iterative method. See `.from_matrix_eps` to provide mover
77    /// convergence parameters and starting solution.
78    /// This implements "A Robust Method to Extract the Rotational Part of Deformations" by Müller et al.
79    pub fn from_matrix(m: &Matrix2<T>) -> Self
80    where
81        T: RealField,
82    {
83        Self::from_matrix_eps(m, T::default_epsilon(), 0, Self::identity())
84    }
85
86    /// Builds a rotation matrix by extracting the rotation part of the given transformation `m`.
87    ///
88    /// This implements "A Robust Method to Extract the Rotational Part of Deformations" by Müller et al.
89    ///
90    /// # Parameters
91    ///
92    /// * `m`: the matrix from which the rotational part is to be extracted.
93    /// * `eps`: the angular errors tolerated between the current rotation and the optimal one.
94    /// * `max_iter`: the maximum number of iterations. Loops indefinitely until convergence if set to `0`.
95    /// * `guess`: an estimate of the solution. Convergence will be significantly faster if an initial solution close
96    ///           to the actual solution is provided. Can be set to `Rotation2::identity()` if no other
97    ///           guesses come to mind.
98    pub fn from_matrix_eps(m: &Matrix2<T>, eps: T, mut max_iter: usize, guess: Self) -> Self
99    where
100        T: RealField,
101    {
102        if max_iter == 0 {
103            max_iter = usize::MAX;
104        }
105
106        let mut rot = guess.into_inner();
107
108        for _ in 0..max_iter {
109            let axis = rot.column(0).perp(&m.column(0)) + rot.column(1).perp(&m.column(1));
110            let denom = rot.column(0).dot(&m.column(0)) + rot.column(1).dot(&m.column(1));
111
112            let angle = axis / (denom.abs() + T::default_epsilon());
113            if angle.clone().abs() > eps {
114                rot = Self::new(angle) * rot;
115            } else {
116                break;
117            }
118        }
119
120        Self::from_matrix_unchecked(rot)
121    }
122
123    /// The rotation matrix required to align `a` and `b` but with its angle.
124    ///
125    /// This is the rotation `R` such that `(R * a).angle(b) == 0 && (R * a).dot(b).is_positive()`.
126    ///
127    /// # Example
128    /// ```
129    /// # #[macro_use] extern crate approx;
130    /// # use nalgebra::{Vector2, Rotation2};
131    /// let a = Vector2::new(1.0, 2.0);
132    /// let b = Vector2::new(2.0, 1.0);
133    /// let rot = Rotation2::rotation_between(&a, &b);
134    /// assert_relative_eq!(rot * a, b);
135    /// assert_relative_eq!(rot.inverse() * b, a);
136    /// ```
137    #[inline]
138    pub fn rotation_between<SB, SC>(a: &Vector<T, U2, SB>, b: &Vector<T, U2, SC>) -> Self
139    where
140        T: RealField,
141        SB: Storage<T, U2>,
142        SC: Storage<T, U2>,
143    {
144        crate::convert(UnitComplex::rotation_between(a, b).to_rotation_matrix())
145    }
146
147    /// The smallest rotation needed to make `a` and `b` collinear and point toward the same
148    /// direction, raised to the power `s`.
149    ///
150    /// # Example
151    /// ```
152    /// # #[macro_use] extern crate approx;
153    /// # use nalgebra::{Vector2, Rotation2};
154    /// let a = Vector2::new(1.0, 2.0);
155    /// let b = Vector2::new(2.0, 1.0);
156    /// let rot2 = Rotation2::scaled_rotation_between(&a, &b, 0.2);
157    /// let rot5 = Rotation2::scaled_rotation_between(&a, &b, 0.5);
158    /// assert_relative_eq!(rot2 * rot2 * rot2 * rot2 * rot2 * a, b, epsilon = 1.0e-6);
159    /// assert_relative_eq!(rot5 * rot5 * a, b, epsilon = 1.0e-6);
160    /// ```
161    #[inline]
162    pub fn scaled_rotation_between<SB, SC>(
163        a: &Vector<T, U2, SB>,
164        b: &Vector<T, U2, SC>,
165        s: T,
166    ) -> Self
167    where
168        T: RealField,
169        SB: Storage<T, U2>,
170        SC: Storage<T, U2>,
171    {
172        crate::convert(UnitComplex::scaled_rotation_between(a, b, s).to_rotation_matrix())
173    }
174
175    /// The rotation matrix needed to make `self` and `other` coincide.
176    ///
177    /// The result is such that: `self.rotation_to(other) * self == other`.
178    ///
179    /// # Example
180    /// ```
181    /// # #[macro_use] extern crate approx;
182    /// # use nalgebra::Rotation2;
183    /// let rot1 = Rotation2::new(0.1);
184    /// let rot2 = Rotation2::new(1.7);
185    /// let rot_to = rot1.rotation_to(&rot2);
186    ///
187    /// assert_relative_eq!(rot_to * rot1, rot2);
188    /// assert_relative_eq!(rot_to.inverse() * rot2, rot1);
189    /// ```
190    #[inline]
191    #[must_use]
192    pub fn rotation_to(&self, other: &Self) -> Self {
193        other * self.inverse()
194    }
195
196    /// Ensure this rotation is an orthonormal rotation matrix. This is useful when repeated
197    /// computations might cause the matrix from progressively not being orthonormal anymore.
198    #[inline]
199    pub fn renormalize(&mut self)
200    where
201        T: RealField,
202    {
203        let mut c = UnitComplex::from(self.clone());
204        let _ = c.renormalize();
205
206        *self = Self::from_matrix_eps(self.matrix(), T::default_epsilon(), 0, c.into())
207    }
208
209    /// Raise the rotation to a given floating power, i.e., returns the rotation with the angle
210    /// of `self` multiplied by `n`.
211    ///
212    /// # Example
213    /// ```
214    /// # #[macro_use] extern crate approx;
215    /// # use nalgebra::Rotation2;
216    /// let rot = Rotation2::new(0.78);
217    /// let pow = rot.powf(2.0);
218    /// assert_relative_eq!(pow.angle(), 2.0 * 0.78);
219    /// ```
220    #[inline]
221    #[must_use]
222    pub fn powf(&self, n: T) -> Self {
223        Self::new(self.angle() * n)
224    }
225}
226
227/// # 2D angle extraction
228impl<T: SimdRealField> Rotation2<T> {
229    /// The rotation angle.
230    ///
231    /// # Example
232    /// ```
233    /// # #[macro_use] extern crate approx;
234    /// # use nalgebra::Rotation2;
235    /// let rot = Rotation2::new(1.78);
236    /// assert_relative_eq!(rot.angle(), 1.78);
237    /// ```
238    #[inline]
239    #[must_use]
240    pub fn angle(&self) -> T {
241        self.matrix()[(1, 0)]
242            .clone()
243            .simd_atan2(self.matrix()[(0, 0)].clone())
244    }
245
246    /// The rotation angle needed to make `self` and `other` coincide.
247    ///
248    /// # Example
249    /// ```
250    /// # #[macro_use] extern crate approx;
251    /// # use nalgebra::Rotation2;
252    /// let rot1 = Rotation2::new(0.1);
253    /// let rot2 = Rotation2::new(1.7);
254    /// assert_relative_eq!(rot1.angle_to(&rot2), 1.6);
255    /// ```
256    #[inline]
257    #[must_use]
258    pub fn angle_to(&self, other: &Self) -> T {
259        self.rotation_to(other).angle()
260    }
261
262    /// The rotation angle returned as a 1-dimensional vector.
263    ///
264    /// This is generally used in the context of generic programming. Using
265    /// the `.angle()` method instead is more common.
266    #[inline]
267    #[must_use]
268    pub fn scaled_axis(&self) -> SVector<T, 1> {
269        Vector1::new(self.angle())
270    }
271}
272
273#[cfg(feature = "rand-no-std")]
274impl<T: SimdRealField> Distribution<Rotation2<T>> for Standard
275where
276    T::Element: SimdRealField,
277    T: SampleUniform,
278{
279    /// Generate a uniformly distributed random rotation.
280    #[inline]
281    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Rotation2<T> {
282        let twopi = Uniform::new(T::zero(), T::simd_two_pi());
283        Rotation2::new(rng.sample(twopi))
284    }
285}
286
287#[cfg(feature = "arbitrary")]
288impl<T: SimdRealField + Arbitrary> Arbitrary for Rotation2<T>
289where
290    T::Element: SimdRealField,
291    Owned<T, U2, U2>: Send,
292{
293    #[inline]
294    fn arbitrary(g: &mut Gen) -> Self {
295        Self::new(T::arbitrary(g))
296    }
297}
298
299/*
300 *
301 * 3D Rotation matrix.
302 *
303 */
304/// # Construction from a 3D axis and/or angles
305impl<T: SimdRealField> Rotation3<T>
306where
307    T::Element: SimdRealField,
308{
309    /// Builds a 3 dimensional rotation matrix from an axis and an angle.
310    ///
311    /// # Arguments
312    ///   * `axisangle` - A vector representing the rotation. Its magnitude is the amount of rotation
313    ///     in radian. Its direction is the axis of rotation.
314    ///
315    /// # Example
316    /// ```
317    /// # #[macro_use] extern crate approx;
318    /// # use std::f32;
319    /// # use nalgebra::{Rotation3, Point3, Vector3};
320    /// let axisangle = Vector3::y() * f32::consts::FRAC_PI_2;
321    /// // Point and vector being transformed in the tests.
322    /// let pt = Point3::new(4.0, 5.0, 6.0);
323    /// let vec = Vector3::new(4.0, 5.0, 6.0);
324    /// let rot = Rotation3::new(axisangle);
325    ///
326    /// assert_relative_eq!(rot * pt, Point3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
327    /// assert_relative_eq!(rot * vec, Vector3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
328    ///
329    /// // A zero vector yields an identity.
330    /// assert_eq!(Rotation3::new(Vector3::<f32>::zeros()), Rotation3::identity());
331    /// ```
332    pub fn new<SB: Storage<T, U3>>(axisangle: Vector<T, U3, SB>) -> Self {
333        let axisangle = axisangle.into_owned();
334        let (axis, angle) = Unit::new_and_get(axisangle);
335        Self::from_axis_angle(&axis, angle)
336    }
337
338    /// Builds a 3D rotation matrix from an axis scaled by the rotation angle.
339    ///
340    /// This is the same as `Self::new(axisangle)`.
341    ///
342    /// # Example
343    /// ```
344    /// # #[macro_use] extern crate approx;
345    /// # use std::f32;
346    /// # use nalgebra::{Rotation3, Point3, Vector3};
347    /// let axisangle = Vector3::y() * f32::consts::FRAC_PI_2;
348    /// // Point and vector being transformed in the tests.
349    /// let pt = Point3::new(4.0, 5.0, 6.0);
350    /// let vec = Vector3::new(4.0, 5.0, 6.0);
351    /// let rot = Rotation3::new(axisangle);
352    ///
353    /// assert_relative_eq!(rot * pt, Point3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
354    /// assert_relative_eq!(rot * vec, Vector3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
355    ///
356    /// // A zero vector yields an identity.
357    /// assert_eq!(Rotation3::from_scaled_axis(Vector3::<f32>::zeros()), Rotation3::identity());
358    /// ```
359    pub fn from_scaled_axis<SB: Storage<T, U3>>(axisangle: Vector<T, U3, SB>) -> Self {
360        Self::new(axisangle)
361    }
362
363    /// Builds a 3D rotation matrix from an axis and a rotation angle.
364    ///
365    /// # Example
366    /// ```
367    /// # #[macro_use] extern crate approx;
368    /// # use std::f32;
369    /// # use nalgebra::{Rotation3, Point3, Vector3};
370    /// let axis = Vector3::y_axis();
371    /// let angle = f32::consts::FRAC_PI_2;
372    /// // Point and vector being transformed in the tests.
373    /// let pt = Point3::new(4.0, 5.0, 6.0);
374    /// let vec = Vector3::new(4.0, 5.0, 6.0);
375    /// let rot = Rotation3::from_axis_angle(&axis, angle);
376    ///
377    /// assert_eq!(rot.axis().unwrap(), axis);
378    /// assert_eq!(rot.angle(), angle);
379    /// assert_relative_eq!(rot * pt, Point3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
380    /// assert_relative_eq!(rot * vec, Vector3::new(6.0, 5.0, -4.0), epsilon = 1.0e-6);
381    ///
382    /// // A zero vector yields an identity.
383    /// assert_eq!(Rotation3::from_scaled_axis(Vector3::<f32>::zeros()), Rotation3::identity());
384    /// ```
385    pub fn from_axis_angle<SB>(axis: &Unit<Vector<T, U3, SB>>, angle: T) -> Self
386    where
387        SB: Storage<T, U3>,
388    {
389        angle.clone().simd_ne(T::zero()).if_else(
390            || {
391                let ux = axis.as_ref()[0].clone();
392                let uy = axis.as_ref()[1].clone();
393                let uz = axis.as_ref()[2].clone();
394                let sqx = ux.clone() * ux.clone();
395                let sqy = uy.clone() * uy.clone();
396                let sqz = uz.clone() * uz.clone();
397                let (sin, cos) = angle.simd_sin_cos();
398                let one_m_cos = T::one() - cos.clone();
399
400                Self::from_matrix_unchecked(SMatrix::<T, 3, 3>::new(
401                    sqx.clone() + (T::one() - sqx) * cos.clone(),
402                    ux.clone() * uy.clone() * one_m_cos.clone() - uz.clone() * sin.clone(),
403                    ux.clone() * uz.clone() * one_m_cos.clone() + uy.clone() * sin.clone(),
404                    ux.clone() * uy.clone() * one_m_cos.clone() + uz.clone() * sin.clone(),
405                    sqy.clone() + (T::one() - sqy) * cos.clone(),
406                    uy.clone() * uz.clone() * one_m_cos.clone() - ux.clone() * sin.clone(),
407                    ux.clone() * uz.clone() * one_m_cos.clone() - uy.clone() * sin.clone(),
408                    uy * uz * one_m_cos + ux * sin,
409                    sqz.clone() + (T::one() - sqz) * cos,
410                ))
411            },
412            Self::identity,
413        )
414    }
415
416    /// Creates a new rotation from Euler angles.
417    ///
418    /// The primitive rotations are applied in order: 1 roll − 2 pitch − 3 yaw.
419    ///
420    /// # Example
421    /// ```
422    /// # #[macro_use] extern crate approx;
423    /// # use nalgebra::Rotation3;
424    /// let rot = Rotation3::from_euler_angles(0.1, 0.2, 0.3);
425    /// let euler = rot.euler_angles();
426    /// assert_relative_eq!(euler.0, 0.1, epsilon = 1.0e-6);
427    /// assert_relative_eq!(euler.1, 0.2, epsilon = 1.0e-6);
428    /// assert_relative_eq!(euler.2, 0.3, epsilon = 1.0e-6);
429    /// ```
430    pub fn from_euler_angles(roll: T, pitch: T, yaw: T) -> Self {
431        let (sr, cr) = roll.simd_sin_cos();
432        let (sp, cp) = pitch.simd_sin_cos();
433        let (sy, cy) = yaw.simd_sin_cos();
434
435        Self::from_matrix_unchecked(SMatrix::<T, 3, 3>::new(
436            cy.clone() * cp.clone(),
437            cy.clone() * sp.clone() * sr.clone() - sy.clone() * cr.clone(),
438            cy.clone() * sp.clone() * cr.clone() + sy.clone() * sr.clone(),
439            sy.clone() * cp.clone(),
440            sy.clone() * sp.clone() * sr.clone() + cy.clone() * cr.clone(),
441            sy * sp.clone() * cr.clone() - cy * sr.clone(),
442            -sp,
443            cp.clone() * sr,
444            cp * cr,
445        ))
446    }
447}
448
449/// # Construction from a 3D eye position and target point
450impl<T: SimdRealField> Rotation3<T>
451where
452    T::Element: SimdRealField,
453{
454    /// Creates a rotation that corresponds to the local frame of an observer standing at the
455    /// origin and looking toward `dir`.
456    ///
457    /// It maps the `z` axis to the direction `dir`.
458    ///
459    /// # Arguments
460    ///   * dir - The look direction, that is, direction the matrix `z` axis will be aligned with.
461    ///   * up - The vertical direction. The only requirement of this parameter is to not be
462    ///     collinear to `dir`. Non-collinearity is not checked.
463    ///
464    /// # Example
465    /// ```
466    /// # #[macro_use] extern crate approx;
467    /// # use std::f32;
468    /// # use nalgebra::{Rotation3, Vector3};
469    /// let dir = Vector3::new(1.0, 2.0, 3.0);
470    /// let up = Vector3::y();
471    ///
472    /// let rot = Rotation3::face_towards(&dir, &up);
473    /// assert_relative_eq!(rot * Vector3::z(), dir.normalize());
474    /// ```
475    #[inline]
476    pub fn face_towards<SB, SC>(dir: &Vector<T, U3, SB>, up: &Vector<T, U3, SC>) -> Self
477    where
478        SB: Storage<T, U3>,
479        SC: Storage<T, U3>,
480    {
481        // Gram–Schmidt process
482        let zaxis = dir.normalize();
483        let xaxis = up.cross(&zaxis).normalize();
484        let yaxis = zaxis.cross(&xaxis);
485
486        Self::from_matrix_unchecked(SMatrix::<T, 3, 3>::new(
487            xaxis.x.clone(),
488            yaxis.x.clone(),
489            zaxis.x.clone(),
490            xaxis.y.clone(),
491            yaxis.y.clone(),
492            zaxis.y.clone(),
493            xaxis.z.clone(),
494            yaxis.z.clone(),
495            zaxis.z.clone(),
496        ))
497    }
498
499    /// Deprecated: Use [`Rotation3::face_towards`] instead.
500    #[deprecated(note = "renamed to `face_towards`")]
501    pub fn new_observer_frames<SB, SC>(dir: &Vector<T, U3, SB>, up: &Vector<T, U3, SC>) -> Self
502    where
503        SB: Storage<T, U3>,
504        SC: Storage<T, U3>,
505    {
506        Self::face_towards(dir, up)
507    }
508
509    /// Builds a right-handed look-at view matrix without translation.
510    ///
511    /// It maps the view direction `dir` to the **negative** `z` axis.
512    /// This conforms to the common notion of right handed look-at matrix from the computer
513    /// graphics community.
514    ///
515    /// # Arguments
516    ///   * dir - The direction toward which the camera looks.
517    ///   * up - A vector approximately aligned with required the vertical axis. The only
518    ///     requirement of this parameter is to not be collinear to `dir`.
519    ///
520    /// # Example
521    /// ```
522    /// # #[macro_use] extern crate approx;
523    /// # use std::f32;
524    /// # use nalgebra::{Rotation3, Vector3};
525    /// let dir = Vector3::new(1.0, 2.0, 3.0);
526    /// let up = Vector3::y();
527    ///
528    /// let rot = Rotation3::look_at_rh(&dir, &up);
529    /// assert_relative_eq!(rot * dir.normalize(), -Vector3::z());
530    /// ```
531    #[inline]
532    pub fn look_at_rh<SB, SC>(dir: &Vector<T, U3, SB>, up: &Vector<T, U3, SC>) -> Self
533    where
534        SB: Storage<T, U3>,
535        SC: Storage<T, U3>,
536    {
537        Self::face_towards(&dir.neg(), up).inverse()
538    }
539
540    /// Builds a left-handed look-at view matrix without translation.
541    ///
542    /// It maps the view direction `dir` to the **positive** `z` axis.
543    /// This conforms to the common notion of left handed look-at matrix from the computer
544    /// graphics community.
545    ///
546    /// # Arguments
547    ///   * dir - The direction toward which the camera looks.
548    ///   * up - A vector approximately aligned with required the vertical axis. The only
549    ///     requirement of this parameter is to not be collinear to `dir`.
550    ///
551    /// # Example
552    /// ```
553    /// # #[macro_use] extern crate approx;
554    /// # use std::f32;
555    /// # use nalgebra::{Rotation3, Vector3};
556    /// let dir = Vector3::new(1.0, 2.0, 3.0);
557    /// let up = Vector3::y();
558    ///
559    /// let rot = Rotation3::look_at_lh(&dir, &up);
560    /// assert_relative_eq!(rot * dir.normalize(), Vector3::z());
561    /// ```
562    #[inline]
563    pub fn look_at_lh<SB, SC>(dir: &Vector<T, U3, SB>, up: &Vector<T, U3, SC>) -> Self
564    where
565        SB: Storage<T, U3>,
566        SC: Storage<T, U3>,
567    {
568        Self::face_towards(dir, up).inverse()
569    }
570}
571
572/// # Construction from an existing 3D matrix or rotations
573impl<T: SimdRealField> Rotation3<T>
574where
575    T::Element: SimdRealField,
576{
577    /// The rotation matrix required to align `a` and `b` but with its angle.
578    ///
579    /// This is the rotation `R` such that `(R * a).angle(b) == 0 && (R * a).dot(b).is_positive()`.
580    ///
581    /// # Example
582    /// ```
583    /// # #[macro_use] extern crate approx;
584    /// # use nalgebra::{Vector3, Rotation3};
585    /// let a = Vector3::new(1.0, 2.0, 3.0);
586    /// let b = Vector3::new(3.0, 1.0, 2.0);
587    /// let rot = Rotation3::rotation_between(&a, &b).unwrap();
588    /// assert_relative_eq!(rot * a, b, epsilon = 1.0e-6);
589    /// assert_relative_eq!(rot.inverse() * b, a, epsilon = 1.0e-6);
590    /// ```
591    #[inline]
592    pub fn rotation_between<SB, SC>(a: &Vector<T, U3, SB>, b: &Vector<T, U3, SC>) -> Option<Self>
593    where
594        T: RealField,
595        SB: Storage<T, U3>,
596        SC: Storage<T, U3>,
597    {
598        Self::scaled_rotation_between(a, b, T::one())
599    }
600
601    /// The smallest rotation needed to make `a` and `b` collinear and point toward the same
602    /// direction, raised to the power `s`.
603    ///
604    /// # Example
605    /// ```
606    /// # #[macro_use] extern crate approx;
607    /// # use nalgebra::{Vector3, Rotation3};
608    /// let a = Vector3::new(1.0, 2.0, 3.0);
609    /// let b = Vector3::new(3.0, 1.0, 2.0);
610    /// let rot2 = Rotation3::scaled_rotation_between(&a, &b, 0.2).unwrap();
611    /// let rot5 = Rotation3::scaled_rotation_between(&a, &b, 0.5).unwrap();
612    /// assert_relative_eq!(rot2 * rot2 * rot2 * rot2 * rot2 * a, b, epsilon = 1.0e-6);
613    /// assert_relative_eq!(rot5 * rot5 * a, b, epsilon = 1.0e-6);
614    /// ```
615    #[inline]
616    pub fn scaled_rotation_between<SB, SC>(
617        a: &Vector<T, U3, SB>,
618        b: &Vector<T, U3, SC>,
619        n: T,
620    ) -> Option<Self>
621    where
622        T: RealField,
623        SB: Storage<T, U3>,
624        SC: Storage<T, U3>,
625    {
626        // TODO: code duplication with Rotation.
627        if let (Some(na), Some(nb)) = (a.try_normalize(T::zero()), b.try_normalize(T::zero())) {
628            let c = na.cross(&nb);
629
630            if let Some(axis) = Unit::try_new(c, T::default_epsilon()) {
631                return Some(Self::from_axis_angle(&axis, na.dot(&nb).acos() * n));
632            }
633
634            // Zero or PI.
635            if na.dot(&nb) < T::zero() {
636                // PI
637                //
638                // The rotation axis is undefined but the angle not zero. This is not a
639                // simple rotation.
640                return None;
641            }
642        }
643
644        Some(Self::identity())
645    }
646
647    /// The rotation matrix needed to make `self` and `other` coincide.
648    ///
649    /// The result is such that: `self.rotation_to(other) * self == other`.
650    ///
651    /// # Example
652    /// ```
653    /// # #[macro_use] extern crate approx;
654    /// # use nalgebra::{Rotation3, Vector3};
655    /// let rot1 = Rotation3::from_axis_angle(&Vector3::y_axis(), 1.0);
656    /// let rot2 = Rotation3::from_axis_angle(&Vector3::x_axis(), 0.1);
657    /// let rot_to = rot1.rotation_to(&rot2);
658    /// assert_relative_eq!(rot_to * rot1, rot2, epsilon = 1.0e-6);
659    /// ```
660    #[inline]
661    #[must_use]
662    pub fn rotation_to(&self, other: &Self) -> Self {
663        other * self.inverse()
664    }
665
666    /// Raise the rotation to a given floating power, i.e., returns the rotation with the same
667    /// axis as `self` and an angle equal to `self.angle()` multiplied by `n`.
668    ///
669    /// # Example
670    /// ```
671    /// # #[macro_use] extern crate approx;
672    /// # use nalgebra::{Rotation3, Vector3, Unit};
673    /// let axis = Unit::new_normalize(Vector3::new(1.0, 2.0, 3.0));
674    /// let angle = 1.2;
675    /// let rot = Rotation3::from_axis_angle(&axis, angle);
676    /// let pow = rot.powf(2.0);
677    /// assert_relative_eq!(pow.axis().unwrap(), axis, epsilon = 1.0e-6);
678    /// assert_eq!(pow.angle(), 2.4);
679    /// ```
680    #[inline]
681    #[must_use]
682    pub fn powf(&self, n: T) -> Self
683    where
684        T: RealField,
685    {
686        if let Some(axis) = self.axis() {
687            Self::from_axis_angle(&axis, self.angle() * n)
688        } else if self.matrix()[(0, 0)] < T::zero() {
689            let minus_id = SMatrix::<T, 3, 3>::from_diagonal_element(-T::one());
690            Self::from_matrix_unchecked(minus_id)
691        } else {
692            Self::identity()
693        }
694    }
695
696    /// Builds a rotation from a basis assumed to be orthonormal.
697    ///
698    /// In order to get a valid rotation matrix, the input must be an
699    /// orthonormal basis, i.e., all vectors are normalized, and the are
700    /// all orthogonal to each other. These invariants are not checked
701    /// by this method.
702    pub fn from_basis_unchecked(basis: &[Vector3<T>; 3]) -> Self {
703        let mat = Matrix3::from_columns(&basis[..]);
704        Self::from_matrix_unchecked(mat)
705    }
706
707    /// Builds a rotation matrix by extracting the rotation part of the given transformation `m`.
708    ///
709    /// This is an iterative method. See `.from_matrix_eps` to provide mover
710    /// convergence parameters and starting solution.
711    /// This implements "A Robust Method to Extract the Rotational Part of Deformations" by Müller et al.
712    pub fn from_matrix(m: &Matrix3<T>) -> Self
713    where
714        T: RealField,
715    {
716        Self::from_matrix_eps(m, T::default_epsilon(), 0, Self::identity())
717    }
718
719    /// Builds a rotation matrix by extracting the rotation part of the given transformation `m`.
720    ///
721    /// This implements "A Robust Method to Extract the Rotational Part of Deformations" by Müller et al.
722    ///
723    /// # Parameters
724    ///
725    /// * `m`: the matrix from which the rotational part is to be extracted.
726    /// * `eps`: the angular errors tolerated between the current rotation and the optimal one.
727    /// * `max_iter`: the maximum number of iterations. Loops indefinitely until convergence if set to `0`.
728    /// * `guess`: a guess of the solution. Convergence will be significantly faster if an initial solution close
729    ///           to the actual solution is provided. Can be set to `Rotation3::identity()` if no other
730    ///           guesses come to mind.
731    pub fn from_matrix_eps(m: &Matrix3<T>, eps: T, mut max_iter: usize, guess: Self) -> Self
732    where
733        T: RealField,
734    {
735        if max_iter == 0 {
736            max_iter = usize::MAX;
737        }
738
739        // Using sqrt(eps) ensures we perturb with something larger than eps; clamp to eps to handle the case of eps > 1.0
740        let eps_disturbance = eps.clone().sqrt().max(eps.clone() * eps.clone());
741        let mut perturbation_axes = Vector3::x_axis();
742        let mut rot = guess.into_inner();
743
744        for _ in 0..max_iter {
745            let axis = rot.column(0).cross(&m.column(0))
746                + rot.column(1).cross(&m.column(1))
747                + rot.column(2).cross(&m.column(2));
748            let denom = rot.column(0).dot(&m.column(0))
749                + rot.column(1).dot(&m.column(1))
750                + rot.column(2).dot(&m.column(2));
751
752            let axisangle = axis / (denom.abs() + T::default_epsilon());
753
754            if let Some((axis, angle)) = Unit::try_new_and_get(axisangle, eps.clone()) {
755                rot = Rotation3::from_axis_angle(&axis, angle) * rot;
756            } else {
757                // Check if stuck in a maximum w.r.t. the norm (m - rot).norm()
758                let mut perturbed = rot.clone();
759                let norm_squared = (m - &rot).norm_squared();
760                let mut new_norm_squared: T;
761
762                // Perturb until the new norm is significantly different
763                loop {
764                    perturbed *=
765                        Rotation3::from_axis_angle(&perturbation_axes, eps_disturbance.clone());
766                    new_norm_squared = (m - &perturbed).norm_squared();
767                    if abs_diff_ne!(
768                        norm_squared,
769                        new_norm_squared,
770                        epsilon = T::default_epsilon()
771                    ) {
772                        break;
773                    }
774                }
775
776                // If new norm is larger, it's a minimum
777                if norm_squared < new_norm_squared {
778                    break;
779                }
780
781                // If not, continue from perturbed rotation, but use a different axes for the next perturbation
782                perturbation_axes = UnitVector3::new_unchecked(perturbation_axes.yzx());
783                rot = perturbed;
784            }
785        }
786
787        Self::from_matrix_unchecked(rot)
788    }
789
790    /// Ensure this rotation is an orthonormal rotation matrix. This is useful when repeated
791    /// computations might cause the matrix from progressively not being orthonormal anymore.
792    #[inline]
793    pub fn renormalize(&mut self)
794    where
795        T: RealField,
796    {
797        let mut c = UnitQuaternion::from(self.clone());
798        let _ = c.renormalize();
799
800        *self = Self::from_matrix_eps(self.matrix(), T::default_epsilon(), 0, c.into())
801    }
802}
803
804/// # 3D axis and angle extraction
805impl<T: SimdRealField> Rotation3<T> {
806    /// The rotation angle in [0; pi].
807    ///
808    /// # Example
809    /// ```
810    /// # #[macro_use] extern crate approx;
811    /// # use nalgebra::{Unit, Rotation3, Vector3};
812    /// let axis = Unit::new_normalize(Vector3::new(1.0, 2.0, 3.0));
813    /// let rot = Rotation3::from_axis_angle(&axis, 1.78);
814    /// assert_relative_eq!(rot.angle(), 1.78);
815    /// ```
816    #[inline]
817    #[must_use]
818    pub fn angle(&self) -> T {
819        ((self.matrix()[(0, 0)].clone()
820            + self.matrix()[(1, 1)].clone()
821            + self.matrix()[(2, 2)].clone()
822            - T::one())
823            / crate::convert(2.0))
824        .simd_acos()
825    }
826
827    /// The rotation axis. Returns `None` if the rotation angle is zero or PI.
828    ///
829    /// # Example
830    /// ```
831    /// # #[macro_use] extern crate approx;
832    /// # use nalgebra::{Rotation3, Vector3, Unit};
833    /// let axis = Unit::new_normalize(Vector3::new(1.0, 2.0, 3.0));
834    /// let angle = 1.2;
835    /// let rot = Rotation3::from_axis_angle(&axis, angle);
836    /// assert_relative_eq!(rot.axis().unwrap(), axis);
837    ///
838    /// // Case with a zero angle.
839    /// let rot = Rotation3::from_axis_angle(&axis, 0.0);
840    /// assert!(rot.axis().is_none());
841    /// ```
842    #[inline]
843    #[must_use]
844    pub fn axis(&self) -> Option<Unit<Vector3<T>>>
845    where
846        T: RealField,
847    {
848        let rotmat = self.matrix();
849        let axis = SVector::<T, 3>::new(
850            rotmat[(2, 1)].clone() - rotmat[(1, 2)].clone(),
851            rotmat[(0, 2)].clone() - rotmat[(2, 0)].clone(),
852            rotmat[(1, 0)].clone() - rotmat[(0, 1)].clone(),
853        );
854
855        Unit::try_new(axis, T::default_epsilon())
856    }
857
858    /// The rotation axis multiplied by the rotation angle.
859    ///
860    /// # Example
861    /// ```
862    /// # #[macro_use] extern crate approx;
863    /// # use nalgebra::{Rotation3, Vector3, Unit};
864    /// let axisangle = Vector3::new(0.1, 0.2, 0.3);
865    /// let rot = Rotation3::new(axisangle);
866    /// assert_relative_eq!(rot.scaled_axis(), axisangle, epsilon = 1.0e-6);
867    /// ```
868    #[inline]
869    #[must_use]
870    pub fn scaled_axis(&self) -> Vector3<T>
871    where
872        T: RealField,
873    {
874        if let Some(axis) = self.axis() {
875            axis.into_inner() * self.angle()
876        } else {
877            Vector::zero()
878        }
879    }
880
881    /// The rotation axis and angle in (0, pi] of this rotation matrix.
882    ///
883    /// Returns `None` if the angle is zero.
884    ///
885    /// # Example
886    /// ```
887    /// # #[macro_use] extern crate approx;
888    /// # use nalgebra::{Rotation3, Vector3, Unit};
889    /// let axis = Unit::new_normalize(Vector3::new(1.0, 2.0, 3.0));
890    /// let angle = 1.2;
891    /// let rot = Rotation3::from_axis_angle(&axis, angle);
892    /// let axis_angle = rot.axis_angle().unwrap();
893    /// assert_relative_eq!(axis_angle.0, axis);
894    /// assert_relative_eq!(axis_angle.1, angle);
895    ///
896    /// // Case with a zero angle.
897    /// let rot = Rotation3::from_axis_angle(&axis, 0.0);
898    /// assert!(rot.axis_angle().is_none());
899    /// ```
900    #[inline]
901    #[must_use]
902    pub fn axis_angle(&self) -> Option<(Unit<Vector3<T>>, T)>
903    where
904        T: RealField,
905    {
906        self.axis().map(|axis| (axis, self.angle()))
907    }
908
909    /// The rotation angle needed to make `self` and `other` coincide.
910    ///
911    /// # Example
912    /// ```
913    /// # #[macro_use] extern crate approx;
914    /// # use nalgebra::{Rotation3, Vector3};
915    /// let rot1 = Rotation3::from_axis_angle(&Vector3::y_axis(), 1.0);
916    /// let rot2 = Rotation3::from_axis_angle(&Vector3::x_axis(), 0.1);
917    /// assert_relative_eq!(rot1.angle_to(&rot2), 1.0045657, epsilon = 1.0e-6);
918    /// ```
919    #[inline]
920    #[must_use]
921    pub fn angle_to(&self, other: &Self) -> T
922    where
923        T::Element: SimdRealField,
924    {
925        self.rotation_to(other).angle()
926    }
927
928    /// Creates Euler angles from a rotation.
929    ///
930    /// The angles are produced in the form (roll, pitch, yaw).
931    #[deprecated(note = "This is renamed to use `.euler_angles()`.")]
932    pub fn to_euler_angles(self) -> (T, T, T)
933    where
934        T: RealField,
935    {
936        self.euler_angles()
937    }
938
939    /// Euler angles corresponding to this rotation from a rotation.
940    ///
941    /// The angles are produced in the form (roll, pitch, yaw).
942    ///
943    /// # Example
944    /// ```
945    /// # #[macro_use] extern crate approx;
946    /// # use nalgebra::Rotation3;
947    /// let rot = Rotation3::from_euler_angles(0.1, 0.2, 0.3);
948    /// let euler = rot.euler_angles();
949    /// assert_relative_eq!(euler.0, 0.1, epsilon = 1.0e-6);
950    /// assert_relative_eq!(euler.1, 0.2, epsilon = 1.0e-6);
951    /// assert_relative_eq!(euler.2, 0.3, epsilon = 1.0e-6);
952    /// ```
953    #[must_use]
954    pub fn euler_angles(&self) -> (T, T, T)
955    where
956        T: RealField,
957    {
958        // Implementation informed by "Computing Euler angles from a rotation matrix", by Gregory G. Slabaugh
959        //  https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.371.6578
960        //  where roll, pitch, yaw angles are referred to as ψ, θ, ϕ,
961        if self[(2, 0)].clone().abs() < T::one() {
962            let pitch = -self[(2, 0)].clone().asin();
963            let theta_cos = pitch.clone().cos();
964            let roll = (self[(2, 1)].clone() / theta_cos.clone())
965                .atan2(self[(2, 2)].clone() / theta_cos.clone());
966            let yaw =
967                (self[(1, 0)].clone() / theta_cos.clone()).atan2(self[(0, 0)].clone() / theta_cos);
968            (roll, pitch, yaw)
969        } else if self[(2, 0)].clone() <= -T::one() {
970            (
971                self[(0, 1)].clone().atan2(self[(0, 2)].clone()),
972                T::frac_pi_2(),
973                T::zero(),
974            )
975        } else {
976            (
977                -self[(0, 1)].clone().atan2(-self[(0, 2)].clone()),
978                -T::frac_pi_2(),
979                T::zero(),
980            )
981        }
982    }
983
984    /// Represent this rotation as Euler angles.
985    ///
986    /// Returns the angles produced in the order provided by seq parameter, along with the
987    /// observability flag. The Euler axes passed to seq must form an orthonormal basis. If the
988    /// rotation is gimbal locked, then the observability flag is false.
989    ///
990    /// # Panics
991    ///
992    /// Panics if the Euler axes in `seq` are not orthonormal.
993    ///
994    /// # Example 1:
995    /// ```
996    /// use std::f64::consts::PI;
997    /// use approx::assert_relative_eq;
998    /// use nalgebra::{Matrix3, Rotation3, Unit, Vector3};
999    ///
1000    /// // 3-1-2
1001    /// let n = [
1002    ///     Unit::new_unchecked(Vector3::new(0.0, 0.0, 1.0)),
1003    ///     Unit::new_unchecked(Vector3::new(1.0, 0.0, 0.0)),
1004    ///     Unit::new_unchecked(Vector3::new(0.0, 1.0, 0.0)),
1005    /// ];
1006    ///
1007    /// let r1 = Rotation3::from_axis_angle(&n[2], 20.0 * PI / 180.0);
1008    /// let r2 = Rotation3::from_axis_angle(&n[1], 30.0 * PI / 180.0);
1009    /// let r3 = Rotation3::from_axis_angle(&n[0], 45.0 * PI / 180.0);
1010    ///
1011    /// let d = r3 * r2 * r1;
1012    ///
1013    /// let (angles, observable) = d.euler_angles_ordered(n, false);
1014    /// assert!(observable);
1015    /// assert_relative_eq!(angles[0] * 180.0 / PI, 45.0, epsilon = 1e-12);
1016    /// assert_relative_eq!(angles[1] * 180.0 / PI, 30.0, epsilon = 1e-12);
1017    /// assert_relative_eq!(angles[2] * 180.0 / PI, 20.0, epsilon = 1e-12);
1018    /// ```
1019    ///
1020    /// # Example 2:
1021    /// ```
1022    /// use std::f64::consts::PI;
1023    /// use approx::assert_relative_eq;
1024    /// use nalgebra::{Matrix3, Rotation3, Unit, Vector3};
1025    ///
1026    /// let sqrt_2 = 2.0_f64.sqrt();
1027    /// let n = [
1028    ///     Unit::new_unchecked(Vector3::new(1.0 / sqrt_2, 1.0 / sqrt_2, 0.0)),
1029    ///     Unit::new_unchecked(Vector3::new(1.0 / sqrt_2, -1.0 / sqrt_2, 0.0)),
1030    ///     Unit::new_unchecked(Vector3::new(0.0, 0.0, 1.0)),
1031    /// ];
1032    ///
1033    /// let r1 = Rotation3::from_axis_angle(&n[2], 20.0 * PI / 180.0);
1034    /// let r2 = Rotation3::from_axis_angle(&n[1], 30.0 * PI / 180.0);
1035    /// let r3 = Rotation3::from_axis_angle(&n[0], 45.0 * PI / 180.0);
1036    ///
1037    /// let d = r3 * r2 * r1;
1038    ///
1039    /// let (angles, observable) = d.euler_angles_ordered(n, false);
1040    /// assert!(observable);
1041    /// assert_relative_eq!(angles[0] * 180.0 / PI, 45.0, epsilon = 1e-12);
1042    /// assert_relative_eq!(angles[1] * 180.0 / PI, 30.0, epsilon = 1e-12);
1043    /// assert_relative_eq!(angles[2] * 180.0 / PI, 20.0, epsilon = 1e-12);
1044    /// ```
1045    ///
1046    /// Algorithm based on:
1047    /// Malcolm D. Shuster, F. Landis Markley, “General formula for extraction the Euler
1048    /// angles”, Journal of guidance, control, and dynamics, vol. 29.1, pp. 215-221. 2006,
1049    /// and modified to be able to produce extrinsic rotations.
1050    #[must_use]
1051    pub fn euler_angles_ordered(
1052        &self,
1053        mut seq: [Unit<Vector3<T>>; 3],
1054        extrinsic: bool,
1055    ) -> ([T; 3], bool)
1056    where
1057        T: RealField + Copy,
1058    {
1059        let mut angles = [T::zero(); 3];
1060        let eps = T::from_subset(&1e-7);
1061        let two = T::from_subset(&2.0);
1062
1063        if extrinsic {
1064            seq.reverse();
1065        }
1066
1067        let [n1, n2, n3] = &seq;
1068        assert_relative_eq!(n1.dot(n2), T::zero(), epsilon = eps);
1069        assert_relative_eq!(n3.dot(n1), T::zero(), epsilon = eps);
1070
1071        let n1_c_n2 = n1.cross(n2);
1072        let s1 = n1_c_n2.dot(n3);
1073        let c1 = n1.dot(n3);
1074        let lambda = s1.atan2(c1);
1075
1076        let mut c = Matrix3::zeros();
1077        c.column_mut(0).copy_from(n2);
1078        c.column_mut(1).copy_from(&n1_c_n2);
1079        c.column_mut(2).copy_from(n1);
1080        c.transpose_mut();
1081
1082        let r1l = Matrix3::new(
1083            T::one(),
1084            T::zero(),
1085            T::zero(),
1086            T::zero(),
1087            c1,
1088            s1,
1089            T::zero(),
1090            -s1,
1091            c1,
1092        );
1093        let o_t = c * self.matrix() * (c.transpose() * r1l);
1094        angles[1] = o_t.m33.acos();
1095
1096        let safe1 = angles[1].abs() >= eps;
1097        let safe2 = (angles[1] - T::pi()).abs() >= eps;
1098        let observable = safe1 && safe2;
1099        angles[1] += lambda;
1100
1101        if observable {
1102            angles[0] = o_t.m13.atan2(-o_t.m23);
1103            angles[2] = o_t.m31.atan2(o_t.m32);
1104        } else {
1105            // gimbal lock detected
1106            if extrinsic {
1107                // angle1 is initialized to zero
1108                if !safe1 {
1109                    angles[2] = (o_t.m12 - o_t.m21).atan2(o_t.m11 + o_t.m22);
1110                } else {
1111                    angles[2] = -(o_t.m12 + o_t.m21).atan2(o_t.m11 - o_t.m22);
1112                };
1113            } else {
1114                // angle3 is initialized to zero
1115                if !safe1 {
1116                    angles[0] = (o_t.m12 - o_t.m21).atan2(o_t.m11 + o_t.m22);
1117                } else {
1118                    angles[0] = (o_t.m12 + o_t.m21).atan2(o_t.m11 - o_t.m22);
1119                };
1120            };
1121        };
1122
1123        let adjust = if seq[0] == seq[2] {
1124            // lambda = 0, so ensure angle2 -> [0, pi]
1125            angles[1] < T::zero() || angles[1] > T::pi()
1126        } else {
1127            // lambda = + or - pi/2, so ensure angle2 -> [-pi/2, pi/2]
1128            angles[1] < -T::frac_pi_2() || angles[1] > T::frac_pi_2()
1129        };
1130
1131        // dont adjust gimbal locked rotation
1132        if adjust && observable {
1133            angles[0] += T::pi();
1134            angles[1] = two * lambda - angles[1];
1135            angles[2] -= T::pi();
1136        }
1137
1138        // ensure all angles are within [-pi, pi]
1139        for angle in angles.as_mut_slice().iter_mut() {
1140            if *angle < -T::pi() {
1141                *angle += T::two_pi();
1142            } else if *angle > T::pi() {
1143                *angle -= T::two_pi();
1144            }
1145        }
1146
1147        if extrinsic {
1148            angles.reverse();
1149        }
1150
1151        (angles, observable)
1152    }
1153}
1154
1155#[cfg(feature = "rand-no-std")]
1156impl<T: SimdRealField> Distribution<Rotation3<T>> for Standard
1157where
1158    T::Element: SimdRealField,
1159    OpenClosed01: Distribution<T>,
1160    T: SampleUniform,
1161{
1162    /// Generate a uniformly distributed random rotation.
1163    #[inline]
1164    fn sample<'a, R: Rng + ?Sized>(&self, rng: &mut R) -> Rotation3<T> {
1165        // James Arvo.
1166        // Fast random rotation matrices.
1167        // In D. Kirk, editor, Graphics Gems III, pages 117-120. Academic, New York, 1992.
1168
1169        // Compute a random rotation around Z
1170        let twopi = Uniform::new(T::zero(), T::simd_two_pi());
1171        let theta = rng.sample(&twopi);
1172        let (ts, tc) = theta.simd_sin_cos();
1173        let a = SMatrix::<T, 3, 3>::new(
1174            tc.clone(),
1175            ts.clone(),
1176            T::zero(),
1177            -ts,
1178            tc,
1179            T::zero(),
1180            T::zero(),
1181            T::zero(),
1182            T::one(),
1183        );
1184
1185        // Compute a random rotation *of* Z
1186        let phi = rng.sample(&twopi);
1187        let z = rng.sample(OpenClosed01);
1188        let (ps, pc) = phi.simd_sin_cos();
1189        let sqrt_z = z.clone().simd_sqrt();
1190        let v = Vector3::new(pc * sqrt_z.clone(), ps * sqrt_z, (T::one() - z).simd_sqrt());
1191        let mut b = v.clone() * v.transpose();
1192        b += b.clone();
1193        b -= SMatrix::<T, 3, 3>::identity();
1194
1195        Rotation3::from_matrix_unchecked(b * a)
1196    }
1197}
1198
1199#[cfg(feature = "arbitrary")]
1200impl<T: SimdRealField + Arbitrary> Arbitrary for Rotation3<T>
1201where
1202    T::Element: SimdRealField,
1203    Owned<T, U3, U3>: Send,
1204    Owned<T, U3>: Send,
1205{
1206    #[inline]
1207    fn arbitrary(g: &mut Gen) -> Self {
1208        Self::new(SVector::arbitrary(g))
1209    }
1210}