parry3d/transformation/to_trimesh/
cuboid_to_trimesh.rs

1use crate::bounding_volume::Aabb;
2use crate::math::{Point, Real};
3use crate::shape::Cuboid;
4use crate::transformation::utils;
5use alloc::vec::Vec;
6
7impl Aabb {
8    /// Discretize the boundary of this Aabb as a triangle-mesh.
9    pub fn to_trimesh(&self) -> (Vec<Point<Real>>, Vec<[u32; 3]>) {
10        let center = self.center();
11        let half_extents = self.half_extents();
12        let mut cube_mesh = Cuboid::new(half_extents).to_trimesh();
13        cube_mesh.0.iter_mut().for_each(|p| *p += center.coords);
14        cube_mesh
15    }
16}
17
18impl Cuboid {
19    /// Discretize the boundary of this cuboid as a triangle-mesh.
20    pub fn to_trimesh(&self) -> (Vec<Point<Real>>, Vec<[u32; 3]>) {
21        let (vtx, idx) = unit_cuboid();
22        (utils::scaled(vtx, self.half_extents * 2.0), idx)
23    }
24}
25
26/// Generates a cuboid shape with a split index buffer.
27///
28/// The cuboid is centered at the origin, and has its half extents set to 0.5.
29fn unit_cuboid() -> (Vec<Point<Real>>, Vec<[u32; 3]>) {
30    #[cfg(feature = "dim3")]
31    {
32        let mut coords = Vec::with_capacity(8);
33        let mut faces = Vec::with_capacity(12);
34
35        coords.push(Point::new(-0.5, -0.5, 0.5));
36        coords.push(Point::new(-0.5, -0.5, -0.5));
37        coords.push(Point::new(0.5, -0.5, -0.5));
38        coords.push(Point::new(0.5, -0.5, 0.5));
39        coords.push(Point::new(-0.5, 0.5, 0.5));
40        coords.push(Point::new(-0.5, 0.5, -0.5));
41        coords.push(Point::new(0.5, 0.5, -0.5));
42        coords.push(Point::new(0.5, 0.5, 0.5));
43
44        faces.push([4, 5, 0]);
45        faces.push([5, 1, 0]);
46
47        faces.push([5, 6, 1]);
48        faces.push([6, 2, 1]);
49
50        faces.push([6, 7, 3]);
51        faces.push([2, 6, 3]);
52
53        faces.push([7, 4, 0]);
54        faces.push([3, 7, 0]);
55
56        faces.push([0, 1, 2]);
57        faces.push([3, 0, 2]);
58
59        faces.push([7, 6, 5]);
60        faces.push([4, 7, 5]);
61
62        (coords, faces)
63    }
64    #[cfg(feature = "dim2")]
65    {
66        let mut coords = Vec::with_capacity(8);
67        let mut faces = Vec::with_capacity(12);
68
69        coords.push(Point::new(-0.5, -0.5));
70        coords.push(Point::new(0.5, -0.5));
71        coords.push(Point::new(-0.5, 0.5));
72        coords.push(Point::new(0.5, 0.5));
73
74        faces.push([0, 1, 2]);
75        faces.push([2, 1, 3]);
76
77        (coords, faces)
78    }
79}