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