parry3d/transformation/to_trimesh/
cone_to_trimesh.rs

1use crate::math::{Real, RealField, Vector};
2use crate::shape::Cone;
3use crate::transformation::utils;
4use alloc::vec::Vec;
5
6impl Cone {
7    /// Discretize the boundary of this cone 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_cone(nsubdiv);
13        (utils::scaled(vtx, scale), idx)
14    }
15}
16
17/// Generates a cone with unit height and diameter.
18fn unit_cone(nsubdiv: u32) -> (Vec<Vector>, Vec<[u32; 3]>) {
19    let two_pi = Real::two_pi();
20    let dtheta = two_pi / (nsubdiv as Real);
21    let mut coords = Vec::new();
22    let mut indices = Vec::new();
23
24    utils::push_circle(0.5, nsubdiv, dtheta, -0.5, &mut coords);
25
26    coords.push(Vector::new(0.0, 0.5, 0.0));
27
28    utils::push_degenerate_top_ring_indices(0, coords.len() as u32 - 1, nsubdiv, &mut indices);
29    utils::push_filled_circle_indices(0, nsubdiv, &mut indices);
30
31    (coords, indices)
32}