Use meshloader to support multiple file formats loading (#744)

Co-authored-by: Tin Lai <tin@tinyiu.com>
This commit is contained in:
Thierry Berger
2024-11-12 09:02:55 +01:00
committed by GitHub
parent 0d791eb794
commit 71f65fe55a
14 changed files with 214 additions and 189 deletions

View File

@@ -1,5 +1,11 @@
## Unreleased
### Added
- Add optional support for Collada and Wavefront files through new feature flags `collada` and `wavefront`.
## 0.3.0
This is the initial release of the `rapier3d-urdf` crate.
### Added

View File

@@ -7,13 +7,21 @@ documentation = "https://docs.rs/rapier3d-urdf"
homepage = "https://rapier.rs"
repository = "https://github.com/dimforge/rapier"
readme = "README.md"
categories = ["science", "game-development", "mathematics", "simulation", "wasm"]
categories = [
"science",
"game-development",
"mathematics",
"simulation",
"wasm",
]
keywords = ["physics", "joints", "multibody", "robotics", "urdf"]
license = "Apache-2.0"
edition = "2021"
[features]
stl = ["dep:rapier3d-stl"]
stl = ["dep:rapier3d-meshloader", "rapier3d-meshloader/stl"]
collada = ["dep:rapier3d-meshloader", "rapier3d-meshloader/collada"]
wavefront = ["dep:rapier3d-meshloader", "rapier3d-meshloader/wavefront"]
[dependencies]
log = "0.4"
@@ -23,4 +31,4 @@ bitflags = "2"
xurdf = "0.2"
rapier3d = { version = "0.22", path = "../rapier3d" }
rapier3d-stl = { version = "0.3.0", path = "../rapier3d-stl", optional = true }
rapier3d-meshloader = { version = "0.3.0", path = "../rapier3d-meshloader", default-features = false, optional = true }

View File

@@ -1,4 +1,4 @@
## STL loader for the Rapier physics engine
## Mesh loader for the Rapier physics engine
Rapier is a set of 2D and 3D physics engines for games, animation, and robotics. The `rapier3d-urdf`
crate lets you convert an URDF file into a set of rigid-bodies, colliders, and joints, for usage with the
@@ -6,7 +6,9 @@ crate lets you convert an URDF file into a set of rigid-bodies, colliders, and j
## Optional cargo features
- `stl`: enables loading STL meshes referenced by the URDF file.
- `stl`: enables loading `.STL` meshes referenced by the URDF file.
- `collada`: enables loading `.dae` meshes referenced by the URDF file.
- `wavefront`: enables loading `.obj` meshes referenced by the URDF file.
## Limitations
@@ -14,7 +16,7 @@ Are listed below some known limitations you might want to be aware of before pic
improve
these elements are very welcome!
- Mesh file types other than `stl` are not supported yet. Contributions are welcome. You my check the `rapier3d-stl`
- Supported mesh formats are `stl`, `collada` and `wavefront`. Contributions are welcome. You my check the `rapier3d-meshloader`
repository for an example of mesh loader.
- When inserting joints as multibody joints, they will be reset to their neutral position (all coordinates = 0).
- The following fields are currently ignored:

View File

@@ -1,4 +1,4 @@
//! ## STL loader for the Rapier physics engine
//! ## URDF loader for the Rapier physics engine
//!
//! Rapier is a set of 2D and 3D physics engines for games, animation, and robotics. The `rapier3d-urdf`
//! crate lets you convert an URDF file into a set of rigid-bodies, colliders, and joints, for usage with the
@@ -7,6 +7,8 @@
//! ## Optional cargo features
//!
//! - `stl`: enables loading STL meshes referenced by the URDF file.
//! - `collada`: enables loading Collada (`.dae`) meshes referenced by the URDF file.
//! - `wavefront`: enables loading Wavefront (`.obj`) meshes referenced by the URDF file.
//!
//! ## Limitations
//!
@@ -14,7 +16,7 @@
//! improve
//! these elements are very welcome!
//!
//! - Mesh file types other than `stl` are not supported yet. Contributions are welcome. You my check the `rapier3d-stl`
//! - Mesh file types are limited. Contributions are welcome. You may check the `rapier3d-meshloader`
//! repository for an example of mesh loader.
//! - When inserting joints as multibody joints, they will be reset to their neutral position (all coordinates = 0).
//! - The following fields are currently ignored:
@@ -35,6 +37,7 @@ use rapier3d::{
geometry::{Collider, ColliderBuilder, ColliderHandle, ColliderSet, SharedShape, TriMeshFlags},
math::{Isometry, Point, Real, Vector},
na,
prelude::MeshConverter,
};
use std::collections::HashMap;
use std::path::Path;
@@ -289,7 +292,7 @@ impl UrdfRobot {
/// (`UrdfRobot`). Both structures are arranged the same way, with matching indices for each part.
///
/// If the URDF file references external meshes, they will be loaded automatically if the format
/// is supported. The format is detected from the files extension. All the mesh formats are
/// is supported. The format is detected mostly from the files extension. All the mesh formats are
/// disabled by default and can be enabled through cargo features (e.g. the `stl` feature of
/// this crate enabled loading referenced meshes in stl format).
///
@@ -310,13 +313,13 @@ impl UrdfRobot {
name_to_link_id.insert(&link.name, id);
let mut colliders = vec![];
if options.create_colliders_from_collision_shapes {
colliders.extend(link.collisions.iter().filter_map(|co| {
urdf_to_collider(&options, mesh_dir, &co.geometry, &co.origin)
colliders.extend(link.collisions.iter().flat_map(|co| {
urdf_to_colliders(&options, mesh_dir, &co.geometry, &co.origin)
}))
}
if options.create_colliders_from_visual_shapes {
colliders.extend(link.visuals.iter().filter_map(|vis| {
urdf_to_collider(&options, mesh_dir, &vis.geometry, &vis.origin)
colliders.extend(link.visuals.iter().flat_map(|vis| {
urdf_to_colliders(&options, mesh_dir, &vis.geometry, &vis.origin)
}))
}
let mut body = urdf_to_rigid_body(&options, &link.inertial);
@@ -488,66 +491,67 @@ fn urdf_to_rigid_body(options: &UrdfLoaderOptions, inertial: &Inertial) -> Rigid
builder.build()
}
fn urdf_to_collider(
fn urdf_to_colliders(
options: &UrdfLoaderOptions,
_mesh_dir: &Path, // NOTO: this isnt used if there is no external mesh feature enabled (like stl).
mesh_dir: &Path,
geometry: &Geometry,
origin: &Pose,
) -> Option<Collider> {
let mut builder = options.collider_blueprint.clone();
) -> Vec<Collider> {
let mut shape_transform = Isometry::identity();
let shape = match &geometry {
Geometry::Box { size } => SharedShape::cuboid(
size[0] as Real / 2.0,
size[1] as Real / 2.0,
size[2] as Real / 2.0,
),
let mut colliders = Vec::new();
match &geometry {
Geometry::Box { size } => {
colliders.push(SharedShape::cuboid(
size[0] as Real / 2.0,
size[1] as Real / 2.0,
size[2] as Real / 2.0,
));
}
Geometry::Cylinder { radius, length } => {
// This rotation will make the cylinder Z-up as per the URDF spec,
// instead of rapiers default Y-up.
shape_transform = Isometry::rotation(Vector::x() * Real::frac_pi_2());
SharedShape::cylinder(*length as Real / 2.0, *radius as Real)
colliders.push(SharedShape::cylinder(
*length as Real / 2.0,
*radius as Real,
));
}
Geometry::Sphere { radius } => {
colliders.push(SharedShape::ball(*radius as Real));
}
Geometry::Sphere { radius } => SharedShape::ball(*radius as Real),
Geometry::Mesh { filename, scale } => {
let path: &Path = filename.as_ref();
let _scale = scale
let full_path = mesh_dir.join(filename);
let scale = scale
.map(|s| Vector::new(s.x as Real, s.y as Real, s.z as Real))
.unwrap_or_else(|| Vector::<Real>::repeat(1.0));
match path.extension().and_then(|ext| ext.to_str()) {
#[cfg(feature = "stl")]
Some("stl") | Some("STL") => {
use rapier3d::geometry::MeshConverter;
let full_path = _mesh_dir.join(filename);
match rapier3d_stl::load_from_path(
full_path,
MeshConverter::TriMeshWithFlags(options.trimesh_flags),
_scale,
) {
Ok(stl_shape) => {
shape_transform = stl_shape.pose;
stl_shape.shape
}
Err(e) => {
log::error!("failed to load STL file {filename}: {e}");
return None;
}
}
}
_ => {
log::error!("failed to load file with unknown type {filename}");
return None;
}
}
let Ok(loaded_mesh) = rapier3d_meshloader::load_from_path(
full_path,
&MeshConverter::TriMeshWithFlags(options.trimesh_flags),
scale,
) else {
return Vec::new();
};
colliders.append(
&mut loaded_mesh
.into_iter()
.filter_map(|x| x.map(|s| s.shape).ok())
.collect(),
);
}
};
}
builder.shape = shape;
Some(
builder
.position(urdf_to_isometry(origin) * shape_transform)
.build(),
)
colliders
.drain(..)
.map(move |shape| {
let mut builder = options.collider_blueprint.clone();
builder.shape = shape;
builder
.position(urdf_to_isometry(origin) * shape_transform)
.build()
})
.collect()
}
fn urdf_to_isometry(pose: &Pose) -> Isometry<Real> {