Add collider constructors for shapes obtained from convex decomposition.

This commit is contained in:
Crozet Sébastien
2021-01-21 16:29:05 +01:00
parent 98d3980db7
commit 800b35b103
3 changed files with 117 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
use crate::math::{Isometry, Point, Real, Vector};
use crate::cdl::transformation::vhacd::{VHACDParameters, VHACD};
use crate::math::{Isometry, Point, Real, Vector, DIM};
use cdl::shape::{
Ball, Capsule, Compound, Cuboid, HalfSpace, HeightField, RoundCuboid, RoundShape,
RoundTriangle, Segment, Shape, ShapeType, TriMesh, Triangle,
@@ -122,6 +123,66 @@ impl ColliderShape {
ColliderShape(Arc::new(TriMesh::new(vertices, indices)))
}
/// Initializes a compound shape obtained from the decomposition of the given trimesh (in 3D) or
/// polyline (in 2D) into convex parts.
pub fn convex_decomposition(vertices: &[Point<Real>], indices: &[[u32; DIM]]) -> Self {
Self::convex_decomposition_with_params(vertices, indices, &VHACDParameters::default())
}
/// Initializes a compound shape obtained from the decomposition of the given trimesh (in 3D) or
/// polyline (in 2D) into convex parts dilated with round corners.
pub fn round_convex_decomposition(
vertices: &[Point<Real>],
indices: &[[u32; DIM]],
border_radius: Real,
) -> Self {
Self::round_convex_decomposition_with_params(
vertices,
indices,
&VHACDParameters::default(),
border_radius,
)
}
/// Initializes a compound shape obtained from the decomposition of the given trimesh (in 3D) or
/// polyline (in 2D) into convex parts.
pub fn convex_decomposition_with_params(
vertices: &[Point<Real>],
indices: &[[u32; DIM]],
params: &VHACDParameters,
) -> Self {
let mut parts = vec![];
let decomp = VHACD::decompose(params, &vertices, &indices, true);
for (vertices, indices) in decomp.compute_exact_convex_hulls(&vertices, &indices) {
if let Some(convex) = Self::convex_mesh(vertices, &indices) {
parts.push((Isometry::identity(), convex));
}
}
Self::compound(parts)
}
/// Initializes a compound shape obtained from the decomposition of the given trimesh (in 3D) or
/// polyline (in 2D) into convex parts dilated with round corners.
pub fn round_convex_decomposition_with_params(
vertices: &[Point<Real>],
indices: &[[u32; DIM]],
params: &VHACDParameters,
border_radius: Real,
) -> Self {
let mut parts = vec![];
let decomp = VHACD::decompose(params, &vertices, &indices, true);
for (vertices, indices) in decomp.compute_exact_convex_hulls(&vertices, &indices) {
if let Some(convex) = Self::round_convex_mesh(vertices, &indices, border_radius) {
parts.push((Isometry::identity(), convex));
}
}
Self::compound(parts)
}
pub fn convex_hull(points: &[Point<Real>]) -> Option<Self> {
#[cfg(feature = "dim2")]
return ConvexPolygon::from_convex_hull(points).map(|ch| ColliderShape(Arc::new(ch)));