parry3d/bounding_volume/
aabb_triangle.rs

1use crate::{
2    bounding_volume::Aabb,
3    math::{Isometry, Point, Real, 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: &Isometry<Real>) -> 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.coords;
18        let b = self.b.coords;
19        let c = self.c.coords;
20
21        let mut min = Point::origin();
22        let mut max = Point::origin();
23
24        for d in 0..DIM {
25            min.coords[d] = a[d].min(b[d]).min(c[d]);
26            max.coords[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::{Isometry, Point, Real, Translation},
39        shape::Triangle,
40    };
41    use na::{RealField, UnitQuaternion};
42
43    #[test]
44    fn triangle_aabb_matches_support_map_aabb() {
45        let t = Triangle::new(
46            Point::new(0.3, -0.1, 0.2),
47            Point::new(-0.7, 1.0, 0.0),
48            Point::new(-0.7, 1.5, 0.0),
49        );
50
51        let m = Isometry::from_parts(
52            Translation::new(-0.2, 5.0, 0.2),
53            UnitQuaternion::from_euler_angles(0.0, Real::frac_pi_2(), 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}