nalgebra/geometry/
rotation_construction.rs

1use num::{One, Zero};
2
3use simba::scalar::{ClosedAddAssign, ClosedMulAssign, SupersetOf};
4
5use crate::base::{SMatrix, Scalar};
6
7use crate::geometry::Rotation;
8
9impl<T, const D: usize> Default for Rotation<T, D>
10where
11    T: Scalar + Zero + One,
12{
13    fn default() -> Self {
14        Self::identity()
15    }
16}
17
18/// # Identity
19impl<T, const D: usize> Rotation<T, D>
20where
21    T: Scalar + Zero + One,
22{
23    /// Creates a new square identity rotation of the given `dimension`.
24    ///
25    /// # Example
26    /// ```
27    /// # use nalgebra::{Rotation2, Rotation3};
28    /// # use nalgebra::Vector3;
29    /// let rot1 = Rotation2::identity();
30    /// let rot2 = Rotation2::new(std::f32::consts::FRAC_PI_2);
31    ///
32    /// assert_eq!(rot1 * rot2, rot2);
33    /// assert_eq!(rot2 * rot1, rot2);
34    ///
35    /// let rot1 = Rotation3::identity();
36    /// let rot2 = Rotation3::from_axis_angle(&Vector3::z_axis(), std::f32::consts::FRAC_PI_2);
37    ///
38    /// assert_eq!(rot1 * rot2, rot2);
39    /// assert_eq!(rot2 * rot1, rot2);
40    /// ```
41    #[inline]
42    pub fn identity() -> Rotation<T, D> {
43        Self::from_matrix_unchecked(SMatrix::<T, D, D>::identity())
44    }
45}
46
47impl<T: Scalar, const D: usize> Rotation<T, D> {
48    /// Cast the components of `self` to another type.
49    ///
50    /// # Example
51    /// ```
52    /// # use nalgebra::Rotation2;
53    /// let rot = Rotation2::<f64>::identity();
54    /// let rot2 = rot.cast::<f32>();
55    /// assert_eq!(rot2, Rotation2::<f32>::identity());
56    /// ```
57    pub fn cast<To: Scalar>(self) -> Rotation<To, D>
58    where
59        Rotation<To, D>: SupersetOf<Self>,
60    {
61        crate::convert(self)
62    }
63}
64
65impl<T, const D: usize> One for Rotation<T, D>
66where
67    T: Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
68{
69    #[inline]
70    fn one() -> Self {
71        Self::identity()
72    }
73}