parry2d/transformation/to_polyline/
voxels_to_polyline.rs

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