parry3d/transformation/to_trimesh/
cone_to_trimesh.rs

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