parry3d/transformation/to_outline/
round_cuboid_to_outline.rs

1use crate::bounding_volume::Aabb;
2use crate::math::{Point, Real, Vector};
3use crate::shape::RoundCuboid;
4use crate::transformation::utils;
5use alloc::{vec, vec::Vec};
6
7impl RoundCuboid {
8    /// Outlines this round cuboid’s surface with polylines.
9    pub fn to_outline(&self, nsubdivs: u32) -> (Vec<Point<Real>>, Vec<[u32; 2]>) {
10        let aabb = Aabb::from_half_extents(Point::origin(), self.inner_shape.half_extents);
11        let aabb_vtx = aabb.vertices();
12        let fidx = Aabb::FACES_VERTEX_IDS;
13        let x = Vector::x() * self.border_radius;
14        let y = Vector::y() * self.border_radius;
15        let z = Vector::z() * self.border_radius;
16
17        #[rustfmt::skip]
18        let mut vtx = vec![
19            aabb_vtx[fidx[0].0] + x, aabb_vtx[fidx[0].1] + x, aabb_vtx[fidx[0].2] + x, aabb_vtx[fidx[0].3] + x,
20            aabb_vtx[fidx[1].0] - x, aabb_vtx[fidx[1].1] - x, aabb_vtx[fidx[1].2] - x, aabb_vtx[fidx[1].3] - x,
21            aabb_vtx[fidx[2].0] + y, aabb_vtx[fidx[2].1] + y, aabb_vtx[fidx[2].2] + y, aabb_vtx[fidx[2].3] + y,
22            aabb_vtx[fidx[3].0] - y, aabb_vtx[fidx[3].1] - y, aabb_vtx[fidx[3].2] - y, aabb_vtx[fidx[3].3] - y,
23            aabb_vtx[fidx[4].0] + z, aabb_vtx[fidx[4].1] + z, aabb_vtx[fidx[4].2] + z, aabb_vtx[fidx[4].3] + z,
24            aabb_vtx[fidx[5].0] - z, aabb_vtx[fidx[5].1] - z, aabb_vtx[fidx[5].2] - z, aabb_vtx[fidx[5].3] - z,
25        ];
26
27        let mut idx = vec![];
28        for i in 0..6 {
29            idx.push([i * 4, i * 4 + 1]);
30            idx.push([i * 4 + 1, i * 4 + 2]);
31            idx.push([i * 4 + 2, i * 4 + 3]);
32            idx.push([i * 4 + 3, i * 4]);
33        }
34
35        let arcs = [
36            [4, 13, 20],
37            [0, 12, 21],
38            [1, 8, 22],
39            [5, 9, 23],
40            [7, 14, 16],
41            [3, 15, 17],
42            [2, 11, 18],
43            [6, 10, 19],
44        ];
45
46        for (center, aidx) in arcs.iter().enumerate() {
47            for ia in 0..3 {
48                let ib = (ia + 1) % 3;
49                utils::push_arc_and_idx(
50                    aabb_vtx[center],
51                    aidx[ia],
52                    aidx[ib],
53                    nsubdivs,
54                    &mut vtx,
55                    &mut idx,
56                );
57            }
58        }
59
60        (vtx, idx)
61    }
62}