parry3d/transformation/to_outline/
voxels_to_outline.rs

1use crate::bounding_volume::Aabb;
2use crate::math::Vector;
3use crate::shape::{VoxelType, Voxels};
4use alloc::{vec, vec::Vec};
5
6impl Voxels {
7    /// Outlines this voxels shape as a set of polylines.
8    ///
9    /// The outline is such that only convex edges are output in the polyline.
10    pub fn to_outline(&self) -> (Vec<Vector>, Vec<[u32; 2]>) {
11        let mut points = vec![];
12        self.iter_outline(|a, b| {
13            points.push(a);
14            points.push(b);
15        });
16        let indices = (0..points.len() as u32 / 2)
17            .map(|i| [i * 2, i * 2 + 1])
18            .collect();
19        (points, indices)
20    }
21
22    /// Outlines this voxels shape using segments.
23    ///
24    /// The outline is such that only convex edges are output in the polyline.
25    pub fn iter_outline(&self, mut f: impl FnMut(Vector, Vector)) {
26        // TODO: move this as a new method: Voxels::to_outline?
27        let radius = self.voxel_size() / 2.0;
28        let aabb = Aabb::from_half_extents(Vector::ZERO, radius);
29        let vtx = aabb.vertices();
30
31        for vox in self.voxels() {
32            match vox.state.voxel_type() {
33                VoxelType::Vertex => {
34                    let mask = vox.state.feature_mask();
35
36                    for edge in Aabb::EDGES_VERTEX_IDS {
37                        if mask & (1 << edge.0) != 0 || mask & (1 << edge.1) != 0 {
38                            f(vox.center + vtx[edge.0], vox.center + vtx[edge.1]);
39                        }
40                    }
41                }
42                VoxelType::Edge => {
43                    let vtx = aabb.vertices();
44                    let mask = vox.state.feature_mask();
45
46                    for (i, edge) in Aabb::EDGES_VERTEX_IDS.iter().enumerate() {
47                        if mask & (1 << i) != 0 {
48                            f(vox.center + vtx[edge.0], vox.center + vtx[edge.1]);
49                        }
50                    }
51                }
52                _ => {}
53            }
54        }
55    }
56}