three_d/renderer/geometry/
bounding_box.rs

1use crate::renderer::*;
2
3///
4/// A bounding box geometry used for visualising an [AxisAlignedBoundingBox].
5///
6pub struct BoundingBox {
7    mesh: InstancedMesh,
8}
9
10impl BoundingBox {
11    ///
12    /// Creates a bounding box geometry from an axis aligned bounding box.
13    ///
14    pub fn new(context: &Context, aabb: AxisAlignedBoundingBox) -> Self {
15        let size = aabb.size();
16        let thickness = 0.02 * size.x.max(size.y).max(size.z);
17
18        Self::new_with_thickness(context, aabb, thickness)
19    }
20
21    ///
22    /// Creates a bounding box object from an axis aligned bounding box with a specified line
23    /// thickness.
24    ///
25    pub fn new_with_thickness(
26        context: &Context,
27        aabb: AxisAlignedBoundingBox,
28        thickness: f32,
29    ) -> Self {
30        let max = aabb.max();
31        let min = aabb.min();
32        let size = aabb.size();
33        let translations = vec![
34            min,
35            vec3(min.x, max.y, max.z),
36            vec3(min.x, min.y, max.z),
37            vec3(min.x, max.y, min.z),
38            min,
39            vec3(max.x, min.y, max.z),
40            vec3(min.x, min.y, max.z),
41            vec3(max.x, min.y, min.z),
42            min,
43            vec3(max.x, max.y, min.z),
44            vec3(min.x, max.y, min.z),
45            vec3(max.x, min.y, min.z),
46        ];
47
48        let mesh = InstancedMesh::new(
49            context,
50            &Instances {
51                transformations: (0..12)
52                    .map(|i| {
53                        Mat4::from_translation(translations[i])
54                            * match i {
55                                0..=3 => Mat4::from_nonuniform_scale(size.x, thickness, thickness),
56                                4..=7 => {
57                                    Mat4::from_angle_z(degrees(90.0))
58                                        * Mat4::from_nonuniform_scale(size.y, thickness, thickness)
59                                }
60                                8..=11 => {
61                                    Mat4::from_angle_y(degrees(-90.0))
62                                        * Mat4::from_nonuniform_scale(size.z, thickness, thickness)
63                                }
64                                _ => unreachable!(),
65                            }
66                    })
67                    .collect(),
68                ..Default::default()
69            },
70            &CpuMesh::cylinder(16),
71        );
72        Self { mesh }
73    }
74}
75
76impl<'a> IntoIterator for &'a BoundingBox {
77    type Item = &'a dyn Geometry;
78    type IntoIter = std::iter::Once<&'a dyn Geometry>;
79
80    fn into_iter(self) -> Self::IntoIter {
81        std::iter::once(self)
82    }
83}
84
85use std::ops::Deref;
86impl Deref for BoundingBox {
87    type Target = InstancedMesh;
88    fn deref(&self) -> &Self::Target {
89        &self.mesh
90    }
91}
92
93impl std::ops::DerefMut for BoundingBox {
94    fn deref_mut(&mut self) -> &mut Self::Target {
95        &mut self.mesh
96    }
97}
98
99impl Geometry for BoundingBox {
100    impl_geometry_body!(deref);
101
102    fn animate(&mut self, time: f32) {
103        self.mesh.animate(time)
104    }
105}