autd3_core/common/
angle.rs

1/// \[°\]
2#[allow(non_camel_case_types)]
3pub struct deg;
4
5/// \[rad\]
6#[allow(non_camel_case_types)]
7pub struct rad;
8
9use derive_more::Debug;
10
11/// Angle
12#[repr(C)]
13#[derive(Clone, Copy, PartialEq, Debug)]
14#[debug("{}rad", radian)]
15pub struct Angle {
16    radian: f32,
17}
18
19impl Angle {
20    /// An angle of zero
21    pub const ZERO: Self = Self { radian: 0.0 };
22
23    /// An angle of π
24    pub const PI: Self = Self {
25        radian: std::f32::consts::PI,
26    };
27
28    /// Returns the angle in radian
29    #[must_use]
30    pub const fn radian(self) -> f32 {
31        self.radian
32    }
33
34    /// Returns the angle in degree
35    #[must_use]
36    pub const fn degree(self) -> f32 {
37        self.radian.to_degrees()
38    }
39}
40
41impl std::ops::Mul<deg> for f32 {
42    type Output = Angle;
43
44    fn mul(self, _rhs: deg) -> Self::Output {
45        Self::Output {
46            radian: self.to_radians(),
47        }
48    }
49}
50
51impl std::ops::Mul<rad> for f32 {
52    type Output = Angle;
53
54    fn mul(self, _rhs: rad) -> Self::Output {
55        Self::Output { radian: self }
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn dbg() {
65        assert_eq!(format!("{:?}", 1.0 * rad), "1rad");
66    }
67}