parry3d/transformation/to_trimesh/
cylinder_to_trimesh.rs

1use crate::math::{Real, RealField, Vector};
2use crate::shape::Cylinder;
3use crate::transformation::utils;
4use alloc::vec::Vec;
5
6impl Cylinder {
7    /// Discretize the boundary of this cylinder as a triangle-mesh.
8    pub fn to_trimesh(&self, nsubdiv: u32) -> (Vec<Vector>, Vec<[u32; 3]>) {
9        let diameter = self.radius * 2.0;
10        let height = self.half_height * 2.0;
11        let scale = Vector::new(diameter, height, diameter);
12        let (vtx, idx) = unit_cylinder(nsubdiv);
13        (utils::scaled(vtx, scale), idx)
14    }
15}
16
17/// Generates a cylinder with unit height and diameter.
18fn unit_cylinder(nsubdiv: u32) -> (Vec<Vector>, Vec<[u32; 3]>) {
19    let two_pi = Real::two_pi();
20    let invsubdiv = 1.0 / (nsubdiv as Real);
21    let dtheta = two_pi * invsubdiv;
22    let mut coords = Vec::new();
23    let mut indices = Vec::new();
24
25    utils::push_circle(0.5, nsubdiv, dtheta, -0.5, &mut coords);
26
27    utils::push_circle(0.5, nsubdiv, dtheta, 0.5, &mut coords);
28
29    utils::push_ring_indices(0, nsubdiv, nsubdiv, &mut indices);
30    utils::push_filled_circle_indices(0, nsubdiv, &mut indices);
31    utils::push_filled_circle_indices(nsubdiv, nsubdiv, &mut indices);
32
33    let len = indices.len();
34    let bottom_start_id = len - (nsubdiv as usize - 2);
35    utils::reverse_clockwising(&mut indices[bottom_start_id..]);
36
37    (coords, indices)
38}