ncollide3d/bounding_volume/
aabb_utils.rs

1use std::iter::IntoIterator;
2
3use crate::bounding_volume::AABB;
4use crate::math::{Isometry, Point, Vector, DIM};
5use crate::shape::SupportMap;
6use na::{self, RealField};
7
8/// Computes the AABB of an support mapped shape.
9pub fn support_map_aabb<N, G>(m: &Isometry<N>, i: &G) -> AABB<N>
10where
11    N: RealField + Copy,
12    G: SupportMap<N>,
13{
14    let mut min = na::zero::<Vector<N>>();
15    let mut max = na::zero::<Vector<N>>();
16    let mut basis = na::zero::<Vector<N>>();
17
18    for d in 0..DIM {
19        // FIXME: this could be further improved iterating on `m`'s columns, and passing
20        // Id as the transformation matrix.
21        basis[d] = na::one();
22        max[d] = i.support_point(m, &basis)[d];
23
24        basis[d] = -na::one::<N>();
25        min[d] = i.support_point(m, &basis)[d];
26
27        basis[d] = na::zero();
28    }
29
30    AABB::new(Point::from(min), Point::from(max))
31}
32
33/// Computes the AABB of an support mapped shape.
34pub fn local_support_map_aabb<N, G>(i: &G) -> AABB<N>
35where
36    N: RealField + Copy,
37    G: SupportMap<N>,
38{
39    let mut min = na::zero::<Vector<N>>();
40    let mut max = na::zero::<Vector<N>>();
41    let mut basis = na::zero::<Vector<N>>();
42
43    for d in 0..DIM {
44        // FIXME: this could be further improved iterating on `m`'s columns, and passing
45        // Id as the transformation matrix.
46        basis[d] = na::one();
47        max[d] = i.local_support_point(&basis)[d];
48
49        basis[d] = -na::one::<N>();
50        min[d] = i.local_support_point(&basis)[d];
51
52        basis[d] = na::zero();
53    }
54
55    AABB::new(Point::from(min), Point::from(max))
56}
57
58/// Computes the AABB of a set of points transformed by `m`.
59pub fn point_cloud_aabb<'a, N: RealField + Copy, I>(m: &Isometry<N>, pts: I) -> AABB<N>
60where
61    I: IntoIterator<Item = &'a Point<N>>,
62{
63    let mut it = pts.into_iter();
64
65    let p0 = it.next().expect(
66        "Point cloud AABB construction: the input iterator should yield at least one point.",
67    );
68    let wp0 = m.transform_point(&p0);
69    let mut min: Point<N> = wp0;
70    let mut max: Point<N> = wp0;
71
72    for pt in it {
73        let wpt = m * pt;
74        min = min.inf(&wpt);
75        max = max.sup(&wpt);
76    }
77
78    AABB::new(min, max)
79}
80
81/// Computes the AABB of a set of points.
82pub fn local_point_cloud_aabb<'a, N: RealField + Copy, I>(pts: I) -> AABB<N>
83where
84    I: IntoIterator<Item = &'a Point<N>>,
85{
86    let mut it = pts.into_iter();
87
88    let p0 = it.next().expect(
89        "Point cloud AABB construction: the input iterator should yield at least one point.",
90    );
91    let mut min: Point<N> = *p0;
92    let mut max: Point<N> = *p0;
93
94    for pt in it {
95        min = min.inf(&pt);
96        max = max.sup(&pt);
97    }
98
99    AABB::new(min, max)
100}