Skip to main content

mln_style/types/
position.rs

1use crate::interpolate::Interpolate;
2use crate::math::deg2radf;
3
4/// A spherical position (radial, azimuthal, polar) with its cached cartesian
5/// form.
6#[derive(Clone, Copy, Debug, Default, mln_wire::Wire)]
7pub struct Position {
8    radial: f32,
9    azimuthal: f32,
10    polar: f32,
11    x: f32,
12    y: f32,
13    z: f32,
14}
15
16impl Position {
17    #[must_use]
18    pub fn new(position: [f32; 3]) -> Self {
19        let mut p = Position { radial: position[0], azimuthal: position[1], polar: position[2], ..Default::default() };
20        p.calculate_cartesian();
21        p
22    }
23
24    #[must_use]
25    pub fn cartesian(&self) -> [f32; 3] {
26        [self.x, self.y, self.z]
27    }
28
29    #[must_use]
30    pub fn spherical(&self) -> [f32; 3] {
31        [self.radial, self.azimuthal, self.polar]
32    }
33
34    /// Utility function to be used only during interpolation; this leaves
35    /// spherical coordinates undefined.
36    pub(crate) fn set_cartesian(&mut self, position: [f32; 3]) {
37        self.x = position[0];
38        self.y = position[1];
39        self.z = position[2];
40    }
41
42    fn calculate_cartesian(&mut self) {
43        // We abstract "north"/"up" (compass-wise) to be 0° when really this is
44        // 90° (π/2): we correct for that here
45        let a = deg2radf(self.azimuthal + 90.0);
46        let p = deg2radf(self.polar);
47
48        self.x = self.radial * a.cos() * p.sin();
49        self.y = self.radial * a.sin() * p.sin();
50        self.z = self.radial * p.cos();
51    }
52}
53
54impl PartialEq for Position {
55    fn eq(&self, other: &Self) -> bool {
56        self.radial == other.radial && self.azimuthal == other.azimuthal && self.polar == other.polar
57        // TODO this doesn't address wrapping, which would be better addressed
58        // by comparing cartesian coordinates but being calculated floats are ont to be trusted.
59    }
60}
61
62/// Interpolates the cartesian coordinates.
63impl Interpolate for Position {
64    fn interpolate(&self, to: &Self, t: f64) -> Self {
65        let mut pos = Position::default();
66        pos.set_cartesian(self.cartesian().interpolate(&to.cartesian(), t));
67        pos
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use approx::assert_abs_diff_eq;
74
75    use super::*;
76
77    /// The cartesian form of radial, azimuthal and polar coordinates, with
78    /// the azimuth measured from north and angles a turn apart alike.
79    #[test]
80    fn cartesian_from_spherical() {
81        for (spherical, cartesian) in [
82            ([2.0, 10.0, 270.0], [0.347_296_4, -1.969_615_5, 0.0]),
83            ([1.0, 80.0, 270.0], [0.984_807_7, -0.173_648_2, 0.0]),
84            ([1.15, 210.0, 30.0], [0.287_5, -0.497_964_6, 0.995_929_2]),
85            ([2.0, 30.0, 10.0], [-0.173_648_2, 0.300_767_3, 1.969_615_5]),
86            ([2.0, 30.0, 370.0], [-0.173_648_2, 0.300_767_3, 1.969_615_5]),
87        ] {
88            let position = Position::new(spherical);
89            assert_eq!(position.spherical(), spherical);
90            assert_abs_diff_eq!(&position.cartesian()[..], &cartesian[..], epsilon = 1e-5);
91        }
92    }
93}