rapier3d/geometry/
mesh_converter.rs1use parry::bounding_volume;
2use parry::math::{Isometry, Point, Real};
3use parry::shape::{Cuboid, SharedShape, TriMeshBuilderError, TriMeshFlags};
4
5#[cfg(feature = "dim3")]
6use parry::transformation::vhacd::VHACDParameters;
7
8#[derive(thiserror::Error, Debug)]
16pub enum MeshConverterError {
17 #[error("convex-hull computation failed")]
19 ConvexHullFailed,
20 #[error("TriMesh building failed")]
22 TriMeshBuilderError(TriMeshBuilderError),
23}
24
25#[derive(Clone, Copy, Debug, PartialEq, Default)]
27pub enum MeshConverter {
28 #[default]
30 TriMesh,
31 TriMeshWithFlags(TriMeshFlags),
33 Obb,
38 Aabb,
42 ConvexHull,
46 #[cfg(feature = "dim3")]
48 ConvexDecomposition,
49 #[cfg(feature = "dim3")]
52 ConvexDecompositionWithParams(VHACDParameters),
53}
54
55impl MeshConverter {
56 #[profiling::function]
59 pub fn convert(
60 &self,
61 vertices: Vec<Point<Real>>,
62 indices: Vec<[u32; 3]>,
63 ) -> Result<(SharedShape, Isometry<Real>), MeshConverterError> {
64 let mut transform = Isometry::identity();
65 let shape = match self {
66 MeshConverter::TriMesh => SharedShape::trimesh(vertices, indices)
67 .map_err(MeshConverterError::TriMeshBuilderError)?,
68 MeshConverter::TriMeshWithFlags(flags) => {
69 SharedShape::trimesh_with_flags(vertices, indices, *flags)
70 .map_err(MeshConverterError::TriMeshBuilderError)?
71 }
72 MeshConverter::Obb => {
73 let (pose, cuboid) = parry::utils::obb(&vertices);
74 transform = pose;
75 SharedShape::new(cuboid)
76 }
77 MeshConverter::Aabb => {
78 let aabb =
79 bounding_volume::details::local_point_cloud_aabb(vertices.iter().copied());
80 let cuboid = Cuboid::new(aabb.half_extents());
81 transform = Isometry::from(aabb.center().coords);
82 SharedShape::new(cuboid)
83 }
84 MeshConverter::ConvexHull => {
85 SharedShape::convex_hull(&vertices).ok_or(MeshConverterError::ConvexHullFailed)?
86 }
87 #[cfg(feature = "dim3")]
88 MeshConverter::ConvexDecomposition => {
89 SharedShape::convex_decomposition(&vertices, &indices)
90 }
91 #[cfg(feature = "dim3")]
92 MeshConverter::ConvexDecompositionWithParams(params) => {
93 SharedShape::convex_decomposition_with_params(&vertices, &indices, params)
94 }
95 };
96 Ok((shape, transform))
97 }
98}