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

@@ -45,7 +45,7 @@ pub fn init_world(testbed: &mut Testbed) {
let deltas = na::one(); let deltas = na::one();
let mtl_path = Path::new(""); let mtl_path = Path::new("");
let mut compound_parts = Vec::new(); let mut shapes = Vec::new();
println!("Parsing and decomposing: {}", obj_path); println!("Parsing and decomposing: {}", obj_path);
let obj = obj::parse_file(&Path::new(&obj_path), &mtl_path, ""); let obj = obj::parse_file(&Path::new(&obj_path), &mtl_path, "");
@@ -81,13 +81,9 @@ pub fn init_world(testbed: &mut Testbed) {
.into_iter() .into_iter()
.map(|idx| [idx.x, idx.y, idx.z]) .map(|idx| [idx.x, idx.y, idx.z])
.collect(); .collect();
let vhacd = VHACD::decompose(&params, &vertices, &indices, true);
for (vertices, indices) in vhacd.compute_exact_convex_hulls(&vertices, &indices) { let decomposed_shape = ColliderShape::convex_decomposition(&vertices, &indices);
if let Some(convex) = ColliderShape::convex_mesh(vertices, &indices) { shapes.push(decomposed_shape);
compound_parts.push(convex);
}
}
} }
// let compound = ColliderShape::compound(compound_parts); // let compound = ColliderShape::compound(compound_parts);
@@ -100,8 +96,8 @@ pub fn init_world(testbed: &mut Testbed) {
let body = RigidBodyBuilder::new_dynamic().translation(x, y, z).build(); let body = RigidBodyBuilder::new_dynamic().translation(x, y, z).build();
let handle = bodies.insert(body); let handle = bodies.insert(body);
for part in &compound_parts { for shape in &shapes {
let collider = ColliderBuilder::new(part.clone()).build(); let collider = ColliderBuilder::new(shape.clone()).build();
colliders.insert(collider, handle, &mut bodies); colliders.insert(collider, handle, &mut bodies);
} }
} }

View File

@@ -1,6 +1,7 @@
use crate::cdl::transformation::vhacd::VHACDParameters;
use crate::dynamics::{CoefficientCombineRule, MassProperties, RigidBodyHandle}; use crate::dynamics::{CoefficientCombineRule, MassProperties, RigidBodyHandle};
use crate::geometry::{ColliderShape, InteractionGroups}; use crate::geometry::{ColliderShape, InteractionGroups};
use crate::math::{AngVector, Isometry, Point, Real, Rotation, Vector}; use crate::math::{AngVector, Isometry, Point, Real, Rotation, Vector, DIM};
use cdl::bounding_volume::AABB; use cdl::bounding_volume::AABB;
use cdl::shape::Shape; use cdl::shape::Shape;
#[cfg(feature = "dim2")] #[cfg(feature = "dim2")]
@@ -299,6 +300,54 @@ impl ColliderBuilder {
Self::new(ColliderShape::trimesh(vertices, indices)) Self::new(ColliderShape::trimesh(vertices, indices))
} }
/// Initializes a collider builder with 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::new(ColliderShape::convex_decomposition(vertices, indices))
}
/// Initializes a collider builder with 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::new(ColliderShape::round_convex_decomposition(
vertices,
indices,
border_radius,
))
}
/// Initializes a collider builder with 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 {
Self::new(ColliderShape::convex_decomposition_with_params(
vertices, indices, params,
))
}
/// Initializes a collider builder with 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 {
Self::new(ColliderShape::round_convex_decomposition_with_params(
vertices,
indices,
params,
border_radius,
))
}
pub fn convex_hull(points: &[Point<Real>]) -> Option<Self> { pub fn convex_hull(points: &[Point<Real>]) -> Option<Self> {
ColliderShape::convex_hull(points).map(|cp| Self::new(cp)) ColliderShape::convex_hull(points).map(|cp| Self::new(cp))
} }

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::{ use cdl::shape::{
Ball, Capsule, Compound, Cuboid, HalfSpace, HeightField, RoundCuboid, RoundShape, Ball, Capsule, Compound, Cuboid, HalfSpace, HeightField, RoundCuboid, RoundShape,
RoundTriangle, Segment, Shape, ShapeType, TriMesh, Triangle, RoundTriangle, Segment, Shape, ShapeType, TriMesh, Triangle,
@@ -122,6 +123,66 @@ impl ColliderShape {
ColliderShape(Arc::new(TriMesh::new(vertices, indices))) 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> { pub fn convex_hull(points: &[Point<Real>]) -> Option<Self> {
#[cfg(feature = "dim2")] #[cfg(feature = "dim2")]
return ConvexPolygon::from_convex_hull(points).map(|ch| ColliderShape(Arc::new(ch))); return ConvexPolygon::from_convex_hull(points).map(|ch| ColliderShape(Arc::new(ch)));