parry3d/transformation/to_trimesh/
capsule_to_trimesh.rs

1use crate::math::Real;
2use crate::shape::Capsule;
3use crate::transformation::utils;
4use alloc::vec::Vec;
5use na::{self, Point3};
6
7impl Capsule {
8    /// Discretize the boundary of this capsule as a triangle-mesh.
9    pub fn to_trimesh(
10        &self,
11        ntheta_subdiv: u32,
12        nphi_subdiv: u32,
13    ) -> (Vec<Point3<Real>>, Vec<[u32; 3]>) {
14        let diameter = self.radius * 2.0;
15        let height = self.half_height() * 2.0;
16        let (vtx, idx) = canonical_capsule(diameter, height, ntheta_subdiv, nphi_subdiv);
17        (utils::transformed(vtx, self.canonical_transform()), idx)
18    }
19}
20
21/// Generates a capsule.
22pub(crate) fn canonical_capsule(
23    caps_diameter: Real,
24    cylinder_height: Real,
25    ntheta_subdiv: u32,
26    nphi_subdiv: u32,
27) -> (Vec<Point3<Real>>, Vec<[u32; 3]>) {
28    let (coords, indices) = super::ball_to_trimesh::unit_hemisphere(ntheta_subdiv, nphi_subdiv / 2);
29    let mut bottom_coords = coords.clone();
30    let mut bottom_indices = indices.clone();
31    utils::reverse_clockwising(&mut bottom_indices[..]);
32
33    let mut top_coords = coords;
34    let mut top_indices = indices;
35
36    let half_height = cylinder_height * 0.5;
37
38    // shift the top
39    for coord in top_coords.iter_mut() {
40        coord.x *= caps_diameter;
41        coord.y = coord.y * caps_diameter + half_height;
42        coord.z *= caps_diameter;
43    }
44
45    // flip + shift the bottom
46    for coord in bottom_coords.iter_mut() {
47        coord.x *= caps_diameter;
48        coord.y = -(coord.y * caps_diameter) - half_height;
49        coord.z *= caps_diameter;
50    }
51
52    // shift the top index buffer
53    let base_top_coords = bottom_coords.len() as u32;
54
55    for idx in top_indices.iter_mut() {
56        idx[0] += base_top_coords;
57        idx[1] += base_top_coords;
58        idx[2] += base_top_coords;
59    }
60
61    // merge all buffers
62    bottom_coords.extend(top_coords);
63    bottom_indices.extend(top_indices);
64
65    // attach the two caps
66    utils::push_ring_indices(0, base_top_coords, ntheta_subdiv, &mut bottom_indices);
67
68    (bottom_coords, bottom_indices)
69}