mesh_to_sdf/point/
impl_array.rs

1use super::Point;
2
3impl Point for [f32; 3] {
4    #[cfg(feature = "serde")]
5    type Serde = Self;
6
7    fn new(x: f32, y: f32, z: f32) -> Self {
8        [x, y, z]
9    }
10
11    fn x(&self) -> f32 {
12        self[0]
13    }
14
15    fn y(&self) -> f32 {
16        self[1]
17    }
18
19    fn z(&self) -> f32 {
20        self[2]
21    }
22
23    fn x_mut(&mut self) -> &mut f32 {
24        &mut self[0]
25    }
26
27    fn y_mut(&mut self) -> &mut f32 {
28        &mut self[1]
29    }
30
31    fn z_mut(&mut self) -> &mut f32 {
32        &mut self[2]
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn test_point_aray() {
42        let p1 = [1.0, 2.0, 3.0];
43        let p2 = [4.0, 5.0, 6.0];
44
45        let p3: [f32; 3] = Point::new(p1.x(), p1.y(), p1.z());
46        assert_eq!(p3.x(), 1.0);
47        assert_eq!(p3.y(), 2.0);
48        assert_eq!(p3.z(), 3.0);
49
50        assert_eq!(p1.add(&p2), [5.0, 7.0, 9.0]);
51        assert_eq!(p1.sub(&p2), [-3.0, -3.0, -3.0]);
52        assert_eq!(p1.dot(&p2), 32.0);
53        assert_eq!(p1.length(), 3.7416575);
54        assert_eq!(p1.dist(&p2), 5.196152);
55        assert_eq!(p1.fmul(2.0), [2.0, 4.0, 6.0]);
56        assert_eq!(p1.comp_div(&p2), [0.25, 0.4, 0.5]);
57
58        let mut p = p1;
59        *p.x_mut() = 10.0;
60        *p.y_mut() = 20.0;
61        *p.z_mut() = 30.0;
62        assert_eq!(p, [10.0, 20.0, 30.0]);
63    }
64}