parry3d/transformation/to_outline/
capsule_to_outline.rs

1use crate::math::{Real, Vector};
2use crate::shape::{Capsule, Cylinder};
3use crate::transformation::utils;
4use alloc::vec::Vec;
5
6impl Capsule {
7    /// Outlines this capsule’s shape using polylines.
8    pub fn to_outline(&self, nsubdiv: u32) -> (Vec<Vector>, Vec<[u32; 2]>) {
9        let (vtx, idx) = canonical_capsule_outline(self.radius, self.half_height(), nsubdiv);
10        (utils::transformed(vtx, self.canonical_transform()), idx)
11    }
12}
13
14/// Generates a capsule.
15pub(crate) fn canonical_capsule_outline(
16    caps_radius: Real,
17    cylinder_half_height: Real,
18    nsubdiv: u32,
19) -> (Vec<Vector>, Vec<[u32; 2]>) {
20    let (mut vtx, mut idx) = Cylinder::new(cylinder_half_height, caps_radius).to_outline(nsubdiv);
21    let shift = vtx.len() as u32;
22
23    // Generate the hemispheres.
24    super::ball_to_outline::push_unit_hemisphere_outline(nsubdiv / 2, &mut vtx, &mut idx);
25    super::ball_to_outline::push_unit_hemisphere_outline(nsubdiv / 2, &mut vtx, &mut idx);
26
27    let ncap_pts = (nsubdiv / 2 + 1) * 2;
28    vtx[shift as usize..(shift + ncap_pts) as usize]
29        .iter_mut()
30        .for_each(|pt| {
31            *pt *= caps_radius * 2.0;
32            pt.y += cylinder_half_height
33        });
34
35    vtx[(shift + ncap_pts) as usize..]
36        .iter_mut()
37        .for_each(|pt| {
38            *pt *= caps_radius * 2.0;
39            pt.y = -pt.y - cylinder_half_height
40        });
41
42    (vtx, idx)
43}