marg_orientation/
euler_angles.rs

1use crate::impl_standard_traits;
2use core::fmt::{Debug, Formatter};
3use uniform_array_derive::UniformArray;
4
5#[derive(UniformArray)]
6#[cfg_attr(test, ensure_uniform_type::ensure_uniform_type)]
7#[repr(C)]
8pub struct EulerAngles<T> {
9    /// The roll angle, in radians.
10    pub roll_phi: T,
11    /// The pitch angle, in radians.
12    pub pitch_theta: T,
13    /// The yaw angle, in radians.
14    pub yaw_psi: T,
15}
16
17impl<T> EulerAngles<T> {
18    /// Initializes a new [`EulerAngles`] instance.
19    #[inline(always)]
20    pub const fn new(roll_phi: T, pitch_theta: T, yaw_psi: T) -> Self {
21        Self {
22            roll_phi,
23            pitch_theta,
24            yaw_psi,
25        }
26    }
27}
28
29impl<T> Default for EulerAngles<T>
30where
31    T: Default,
32{
33    #[inline]
34    fn default() -> Self {
35        Self::new(Default::default(), Default::default(), Default::default())
36    }
37}
38
39impl<T> Clone for EulerAngles<T>
40where
41    T: Clone,
42{
43    fn clone(&self) -> Self {
44        Self {
45            roll_phi: self.roll_phi.clone(),
46            pitch_theta: self.pitch_theta.clone(),
47            yaw_psi: self.yaw_psi.clone(),
48        }
49    }
50}
51
52impl<T> Debug for EulerAngles<T>
53where
54    T: Debug,
55{
56    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
57        f.debug_tuple("EulerAngles")
58            .field(&self.roll_phi)
59            .field(&self.pitch_theta)
60            .field(&self.yaw_psi)
61            .finish()
62    }
63}
64
65impl_standard_traits!(EulerAngles, T);
66
67#[cfg(test)]
68mod test {
69    use super::*;
70
71    #[test]
72    fn test_len() {
73        let reading = EulerAngles::<f32>::default();
74        assert_eq!(reading.len(), 3);
75    }
76
77    #[test]
78    fn test_index() {
79        let reading = EulerAngles::<f32> {
80            roll_phi: 1.0,
81            pitch_theta: 2.0,
82            yaw_psi: 3.0,
83        };
84
85        assert_eq!(reading[0], 1.0);
86        assert_eq!(reading[1], 2.0);
87        assert_eq!(reading[2], 3.0);
88    }
89}