parry3d/bounding_volume/
aabb_triangle.rs

1use crate::{
2    bounding_volume::Aabb,
3    math::{Pose, Vector, DIM},
4    shape::Triangle,
5};
6
7impl Triangle {
8    /// Computes the world-space [`Aabb`] of this triangle, transformed by `pos`.
9    #[inline]
10    pub fn aabb(&self, pos: &Pose) -> Aabb {
11        self.transformed(pos).local_aabb()
12    }
13
14    /// Computes the local-space [`Aabb`] of this triangle.
15    #[inline]
16    pub fn local_aabb(&self) -> Aabb {
17        let a = self.a;
18        let b = self.b;
19        let c = self.c;
20
21        let mut min = Vector::ZERO;
22        let mut max = Vector::ZERO;
23
24        for d in 0..DIM {
25            min[d] = a[d].min(b[d]).min(c[d]);
26            max[d] = a[d].max(b[d]).max(c[d]);
27        }
28
29        Aabb::new(min, max)
30    }
31}
32
33#[cfg(test)]
34#[cfg(feature = "dim3")]
35mod test {
36    use crate::{
37        bounding_volume::details::support_map_aabb,
38        math::{Pose, Real, Rotation, Vector},
39        shape::Triangle,
40    };
41    use core::f64::consts::FRAC_PI_2;
42
43    #[test]
44    fn triangle_aabb_matches_support_map_aabb() {
45        let t = Triangle::new(
46            Vector::new(0.3, -0.1, 0.2),
47            Vector::new(-0.7, 1.0, 0.0),
48            Vector::new(-0.7, 1.5, 0.0),
49        );
50
51        let m = Pose::from_parts(
52            Vector::new(-0.2, 5.0, 0.2),
53            Rotation::from_euler(glamx::EulerRot::XYZ, 0.0, FRAC_PI_2 as Real, 0.0),
54        );
55
56        assert_eq!(t.aabb(&m), support_map_aabb(&m, &t));
57
58        // TODO: also test local Aabb once support maps have a local Aabb
59        // function too
60    }
61}