From b5038c0e42f14c0f15e661fcc1531d450217311e Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Mon, 30 Sep 2024 17:43:57 +0200 Subject: [PATCH 1/7] use meshloader to support multiple files Co-authored-by: Tin Lai --- .github/workflows/rapier-ci-build.yml | 2 +- Cargo.toml | 2 +- crates/rapier3d-meshloader/CHANGELOG.md | 22 ++++ .../Cargo.toml | 20 +++- .../LICENSE | 0 .../README.md | 0 crates/rapier3d-meshloader/src/lib.rs | 88 ++++++++++++++ crates/rapier3d-stl/CHANGELOG.md | 9 -- crates/rapier3d-stl/src/lib.rs | 110 ------------------ crates/rapier3d-urdf/CHANGELOG.md | 6 + crates/rapier3d-urdf/Cargo.toml | 14 ++- crates/rapier3d-urdf/README.md | 8 +- crates/rapier3d-urdf/src/lib.rs | 107 ++++++++--------- scripts/publish-extra-formats.sh | 4 +- 14 files changed, 207 insertions(+), 185 deletions(-) create mode 100644 crates/rapier3d-meshloader/CHANGELOG.md rename crates/{rapier3d-stl => rapier3d-meshloader}/Cargo.toml (53%) rename crates/{rapier3d-stl => rapier3d-meshloader}/LICENSE (100%) rename crates/{rapier3d-stl => rapier3d-meshloader}/README.md (100%) create mode 100644 crates/rapier3d-meshloader/src/lib.rs delete mode 100644 crates/rapier3d-stl/CHANGELOG.md delete mode 100644 crates/rapier3d-stl/src/lib.rs diff --git a/.github/workflows/rapier-ci-build.yml b/.github/workflows/rapier-ci-build.yml index e978550af..8603f1d07 100644 --- a/.github/workflows/rapier-ci-build.yml +++ b/.github/workflows/rapier-ci-build.yml @@ -23,7 +23,7 @@ jobs: steps: - uses: actions/checkout@v4 - name: Cargo doc - run: cargo doc --features parallel,simd-stable,serde-serialize,debug-render -p rapier3d -p rapier2d -p rapier3d-stl -p rapier3d-urdf + run: cargo doc --features parallel,simd-stable,serde-serialize,debug-render -p rapier3d -p rapier2d -p rapier3d-meshloader -p rapier3d-urdf build-native: runs-on: ubuntu-latest env: diff --git a/Cargo.toml b/Cargo.toml index 479779858..8d9b1b9bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ members = [ "examples3d-f64", "benchmarks3d", "crates/rapier3d-urdf", - "crates/rapier3d-stl", + "crates/rapier3d-meshloader", ] resolver = "2" diff --git a/crates/rapier3d-meshloader/CHANGELOG.md b/crates/rapier3d-meshloader/CHANGELOG.md new file mode 100644 index 000000000..0663078aa --- /dev/null +++ b/crates/rapier3d-meshloader/CHANGELOG.md @@ -0,0 +1,22 @@ +## Unreleased + +Renamed the crate from `rapier3d-stl` to `rapier3d-meshloader`, to better reflect its support for multiple formats. + +### Added + +- Add optional support for Collada and Wavefront files through new feature flags `collada` and `wavefront`. + +### Modified + +- Support for STL is now optional through feature `stl`. +- Features `stl`, `wavefront` and `collada` are enabled by default. + +## 0.3.0 + +This is the initial release of the `rapier3d-stl` crate. + +### Added + +- Add `load_from_path` for creating a shape from a stl file. +- Add `load_from_reader` for creating a shape from an object implementing `Read`. +- Add `load_from_raw_mesh` for creating a shape from an already loaded `IndexedMesh`. diff --git a/crates/rapier3d-stl/Cargo.toml b/crates/rapier3d-meshloader/Cargo.toml similarity index 53% rename from crates/rapier3d-stl/Cargo.toml rename to crates/rapier3d-meshloader/Cargo.toml index f24f10242..a470afc84 100644 --- a/crates/rapier3d-stl/Cargo.toml +++ b/crates/rapier3d-meshloader/Cargo.toml @@ -1,19 +1,31 @@ [package] -name = "rapier3d-stl" +name = "rapier3d-meshloader" version = "0.3.0" authors = ["Sébastien Crozet "] description = "STL file loader for the 3D rapier physics engine." -documentation = "https://docs.rs/rapier3d-stl" +documentation = "https://docs.rs/rapier3d-meshloader" 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] +default = ["stl", "collada", "wavefront"] +stl = ["mesh-loader/stl"] +collada = ["mesh-loader/collada"] +wavefront = ["mesh-loader/obj"] + [dependencies] thiserror = "1.0.61" -stl_io = "0.7" +mesh-loader = { version = "0.1.12", optional = true } rapier3d = { version = "0.22", path = "../rapier3d" } diff --git a/crates/rapier3d-stl/LICENSE b/crates/rapier3d-meshloader/LICENSE similarity index 100% rename from crates/rapier3d-stl/LICENSE rename to crates/rapier3d-meshloader/LICENSE diff --git a/crates/rapier3d-stl/README.md b/crates/rapier3d-meshloader/README.md similarity index 100% rename from crates/rapier3d-stl/README.md rename to crates/rapier3d-meshloader/README.md diff --git a/crates/rapier3d-meshloader/src/lib.rs b/crates/rapier3d-meshloader/src/lib.rs new file mode 100644 index 000000000..a6c944136 --- /dev/null +++ b/crates/rapier3d-meshloader/src/lib.rs @@ -0,0 +1,88 @@ +//! ## Mesh loader for the Rapier physics engine +//! +//! See documentation from [`mesh_loader`] for supported formats. +//! +//! Rapier is a set of 2D and 3D physics engines for games, animation, and robotics. The `rapier3d-meshloader` +//! crate lets you create a shape compatible with `rapier3d` and `parry3d` (the underlying collision-detection +//! library) from different file formats, see the following features list: +//! `stl`: support .stl files +//! `collada`: support .dae files +//! `wavefront`: support .obj files + +#![deny(missing_docs)] + +use mesh_loader::Mesh; +use rapier3d::geometry::{MeshConverter, SharedShape}; +use rapier3d::math::{Isometry, Point, Real}; +use rapier3d::prelude::MeshConverterError; +use std::path::Path; + +/// The result of loading a shape. +pub struct LoadedShape { + /// The shape loaded from the file and converted by the [`MeshConverter`]. + pub shape: SharedShape, + /// The shape’s pose. + pub pose: Isometry, + /// The raw mesh read from the file without any modification. + pub raw_mesh: Mesh, +} + +/// Error while loading an STL file. +#[derive(thiserror::Error, Debug)] +pub enum MeshLoaderError { + /// An error triggered by rapier’s [`MeshConverter`]. + #[error(transparent)] + MeshConverter(#[from] MeshConverterError), + /// A generic IO error. + #[error(transparent)] + Io(#[from] std::io::Error), +} + +/// Loads an file as shapes from a file. +/// +/// # Parameters +/// - `path`: the file’s path. +/// - `converter`: controls how the shapes are computed from the content. In particular, it lets +/// you specify if the computed [`SharedShape`] is a triangle mesh, its convex hull, +/// bounding box, etc. +// TODO: call a function for each mesh to load ? To be able to have a different mesh converter? +pub fn load_from_path( + path: impl AsRef, + converter: &MeshConverter, +) -> Result>, MeshLoaderError> { + let loader = mesh_loader::Loader::default(); + let mut colliders = vec![]; + let scene = loader.load(path)?; + for (raw_mesh, _) in scene.meshes.into_iter().zip(scene.materials) { + let shape = load_from_raw_mesh(&raw_mesh, converter); + colliders.push(match shape { + Ok((shape, pose)) => Ok(LoadedShape { + shape, + pose, + raw_mesh, + }), + Err(e) => Err(e), + }); + } + Ok(colliders) +} + +/// Loads an file as a shape from a preloaded raw [`mesh_loader::Mesh`]. +/// +/// # Parameters +/// - `raw_mesh`: the raw mesh. +/// - `converter`: controls how the shape is computed from the STL content. In particular, it lets +/// you specify if the computed [`SharedShape`] is a triangle mesh, its convex hull, +/// bounding box, etc. +pub fn load_from_raw_mesh( + raw_mesh: &Mesh, + converter: &MeshConverter, +) -> Result<(SharedShape, Isometry), MeshConverterError> { + let vertices: Vec<_> = raw_mesh + .vertices + .iter() + .map(|xyz| Point::new(xyz[0], xyz[1], xyz[2])) + .collect(); + let indices: Vec<_> = raw_mesh.faces.clone(); + converter.convert(vertices, indices) +} diff --git a/crates/rapier3d-stl/CHANGELOG.md b/crates/rapier3d-stl/CHANGELOG.md deleted file mode 100644 index bb3ffa31e..000000000 --- a/crates/rapier3d-stl/CHANGELOG.md +++ /dev/null @@ -1,9 +0,0 @@ -## Unreleased - -This is the initial release of the `rapier3d-stl` crate. - -### Added - -- Add `load_from_path` for creating a shape from a stl file. -- Add `load_from_reader` for creating a shape from an object implementing `Read`. -- Add `load_from_raw_mesh` for creating a shape from an already loaded `IndexedMesh`. diff --git a/crates/rapier3d-stl/src/lib.rs b/crates/rapier3d-stl/src/lib.rs deleted file mode 100644 index 7b22e7423..000000000 --- a/crates/rapier3d-stl/src/lib.rs +++ /dev/null @@ -1,110 +0,0 @@ -//! ## STL loader for the Rapier physics engine -//! -//! Rapier is a set of 2D and 3D physics engines for games, animation, and robotics. The `rapier3d-stl` -//! crate lets you create a shape compatible with `rapier3d` and `parry3d` (the underlying collision-detection -//! library) from an STL file. - -#![warn(missing_docs)] - -use rapier3d::geometry::{MeshConverter, MeshConverterError, SharedShape}; -use rapier3d::math::{Isometry, Point, Real, Vector}; -use std::fs::File; -use std::io::{BufReader, Read, Seek}; -use std::path::Path; -use stl_io::IndexedMesh; - -/// Error while loading an STL file. -#[derive(thiserror::Error, Debug)] -pub enum StlLoaderError { - /// An error triggered by rapier’s [`MeshConverter`]. - #[error(transparent)] - MeshConverter(#[from] MeshConverterError), - /// A generic IO error. - #[error(transparent)] - Io(#[from] std::io::Error), -} - -/// The result of loading a shape from an stl mesh. -pub struct StlShape { - /// The shape loaded from the file and converted by the [`MeshConverter`]. - pub shape: SharedShape, - /// The shape’s pose. - pub pose: Isometry, - /// The raw mesh read from the stl file without any modification. - pub raw_mesh: IndexedMesh, -} - -/// Loads an STL file as a shape from a file. -/// -/// # Parameters -/// - `file_path`: the STL file’s path. -/// - `converter`: controls how the shape is computed from the STL content. In particular, it lets -/// you specify if the computed [`StlShape::shape`] is a triangle mesh, its convex hull, -/// bounding box, etc. -/// - `scale`: the scaling factor applied to the geometry input to the `converter`. This scale will -/// affect at the geometric level the [`StlShape::shape`]. Note that raw mesh value stored -/// in [`StlShape::raw_mesh`] remains unscaled. -pub fn load_from_path( - file_path: impl AsRef, - converter: MeshConverter, - scale: Vector, -) -> Result { - let mut reader = BufReader::new(File::open(file_path)?); - load_from_reader(&mut reader, converter, scale) -} - -/// Loads an STL file as a shape from an arbitrary reader. -/// -/// # Parameters -/// - `reader`: the reader. -/// - `converter`: controls how the shape is computed from the STL content. In particular, it lets -/// you specify if the computed [`StlShape::shape`] is a triangle mesh, its convex hull, -/// bounding box, etc. -/// - `scale`: the scaling factor applied to the geometry input to the `converter`. This scale will -/// affect at the geometric level the [`StlShape::shape`]. Note that raw mesh value stored -/// in [`StlShape::raw_mesh`] remains unscaled. -pub fn load_from_reader( - read: &mut R, - converter: MeshConverter, - scale: Vector, -) -> Result { - let stl_mesh = stl_io::read_stl(read)?; - Ok(load_from_raw_mesh(stl_mesh, converter, scale)?) -} - -/// Loads an STL file as a shape from a preloaded raw stl mesh. -/// -/// # Parameters -/// - `raw_mesh`: the raw stl mesh. -/// - `converter`: controls how the shape is computed from the STL content. In particular, it lets -/// you specify if the computed [`StlShape::shape`] is a triangle mesh, its convex hull, -/// bounding box, etc. -/// - `scale`: the scaling factor applied to the geometry input to the `converter`. This scale will -/// affect at the geometric level the [`StlShape::shape`]. Note that raw mesh value stored -/// in [`StlShape::raw_mesh`] remains unscaled. -pub fn load_from_raw_mesh( - raw_mesh: IndexedMesh, - converter: MeshConverter, - scale: Vector, -) -> Result { - let mut vertices: Vec<_> = raw_mesh - .vertices - .iter() - .map(|xyz| Point::new(xyz[0], xyz[1], xyz[2])) - .collect(); - vertices - .iter_mut() - .for_each(|pt| pt.coords.component_mul_assign(&scale)); - let indices: Vec<_> = raw_mesh - .faces - .iter() - .map(|f| f.vertices.map(|i| i as u32)) - .collect(); - let (shape, pose) = converter.convert(vertices, indices)?; - - Ok(StlShape { - shape, - pose, - raw_mesh, - }) -} diff --git a/crates/rapier3d-urdf/CHANGELOG.md b/crates/rapier3d-urdf/CHANGELOG.md index 852e45a53..5284d9ccc 100644 --- a/crates/rapier3d-urdf/CHANGELOG.md +++ b/crates/rapier3d-urdf/CHANGELOG.md @@ -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 diff --git a/crates/rapier3d-urdf/Cargo.toml b/crates/rapier3d-urdf/Cargo.toml index ef53da981..d6f74c909 100644 --- a/crates/rapier3d-urdf/Cargo.toml +++ b/crates/rapier3d-urdf/Cargo.toml @@ -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 } diff --git a/crates/rapier3d-urdf/README.md b/crates/rapier3d-urdf/README.md index ae2e21df7..48b399c70 100644 --- a/crates/rapier3d-urdf/README.md +++ b/crates/rapier3d-urdf/README.md @@ -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: diff --git a/crates/rapier3d-urdf/src/lib.rs b/crates/rapier3d-urdf/src/lib.rs index 1f913dae6..6adad7840 100644 --- a/crates/rapier3d-urdf/src/lib.rs +++ b/crates/rapier3d-urdf/src/lib.rs @@ -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 file’s extension. All the mesh formats are + /// is supported. The format is detected mostly from the file’s 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,66 @@ 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 isn’t used if there is no external mesh feature enabled (like stl). + mesh_dir: &Path, geometry: &Geometry, origin: &Pose, -) -> Option { - let mut builder = options.collider_blueprint.clone(); +) -> Vec { 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 rapier’s 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 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::::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), + ) 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 { diff --git a/scripts/publish-extra-formats.sh b/scripts/publish-extra-formats.sh index 1d9a99097..af48e7773 100755 --- a/scripts/publish-extra-formats.sh +++ b/scripts/publish-extra-formats.sh @@ -2,8 +2,8 @@ currdir=$(pwd) -### Publish rapier3d-stl. -cd "crates/rapier3d-stl" && cargo publish $DRY_RUN || exit 1 +### Publish rapier3d-meshloader. +cd "crates/rapier3d-meshloader" && cargo publish $DRY_RUN || exit 1 cd "$currdir" || exit 2 ### Publish rapier3d-urdf. From fb8a7c7f1cce441c5e8cd3968f9aaa789c7d1a87 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Tue, 1 Oct 2024 09:31:07 +0200 Subject: [PATCH 2/7] use scale when loading a mesh from meshloader --- crates/rapier3d-meshloader/src/lib.rs | 22 +++++++++++++++++----- crates/rapier3d-urdf/src/lib.rs | 3 ++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/crates/rapier3d-meshloader/src/lib.rs b/crates/rapier3d-meshloader/src/lib.rs index a6c944136..ecac82181 100644 --- a/crates/rapier3d-meshloader/src/lib.rs +++ b/crates/rapier3d-meshloader/src/lib.rs @@ -1,19 +1,19 @@ //! ## Mesh loader for the Rapier physics engine //! -//! See documentation from [`mesh_loader`] for supported formats. -//! //! Rapier is a set of 2D and 3D physics engines for games, animation, and robotics. The `rapier3d-meshloader` //! crate lets you create a shape compatible with `rapier3d` and `parry3d` (the underlying collision-detection //! library) from different file formats, see the following features list: //! `stl`: support .stl files //! `collada`: support .dae files //! `wavefront`: support .obj files +//! +//! See documentation from [`mesh_loader`] for more details. #![deny(missing_docs)] use mesh_loader::Mesh; use rapier3d::geometry::{MeshConverter, SharedShape}; -use rapier3d::math::{Isometry, Point, Real}; +use rapier3d::math::{Isometry, Point, Real, Vector}; use rapier3d::prelude::MeshConverterError; use std::path::Path; @@ -45,16 +45,21 @@ pub enum MeshLoaderError { /// - `converter`: controls how the shapes are computed from the content. In particular, it lets /// you specify if the computed [`SharedShape`] is a triangle mesh, its convex hull, /// bounding box, etc. +/// - `scale`: the scaling factor applied to the geometry input to the `converter`. This scale will +/// affect at the geometric level the [`StlShape::shape`]. Note that raw mesh value stored +/// in [`StlShape::raw_mesh`] remains unscaled. // TODO: call a function for each mesh to load ? To be able to have a different mesh converter? pub fn load_from_path( path: impl AsRef, converter: &MeshConverter, + scale: Vector, ) -> Result>, MeshLoaderError> { let loader = mesh_loader::Loader::default(); let mut colliders = vec![]; let scene = loader.load(path)?; for (raw_mesh, _) in scene.meshes.into_iter().zip(scene.materials) { - let shape = load_from_raw_mesh(&raw_mesh, converter); + let shape = load_from_raw_mesh(&raw_mesh, converter, scale); + colliders.push(match shape { Ok((shape, pose)) => Ok(LoadedShape { shape, @@ -74,15 +79,22 @@ pub fn load_from_path( /// - `converter`: controls how the shape is computed from the STL content. In particular, it lets /// you specify if the computed [`SharedShape`] is a triangle mesh, its convex hull, /// bounding box, etc. +/// - `scale`: the scaling factor applied to the geometry input to the `converter`. This scale will +/// affect at the geometric level the [`StlShape::shape`]. Note that raw mesh value stored +/// in [`StlShape::raw_mesh`] remains unscaled. pub fn load_from_raw_mesh( raw_mesh: &Mesh, converter: &MeshConverter, + scale: Vector, ) -> Result<(SharedShape, Isometry), MeshConverterError> { - let vertices: Vec<_> = raw_mesh + let mut vertices: Vec<_> = raw_mesh .vertices .iter() .map(|xyz| Point::new(xyz[0], xyz[1], xyz[2])) .collect(); + vertices + .iter_mut() + .for_each(|pt| pt.coords.component_mul_assign(&scale)); let indices: Vec<_> = raw_mesh.faces.clone(); converter.convert(vertices, indices) } diff --git a/crates/rapier3d-urdf/src/lib.rs b/crates/rapier3d-urdf/src/lib.rs index 6adad7840..8e858c384 100644 --- a/crates/rapier3d-urdf/src/lib.rs +++ b/crates/rapier3d-urdf/src/lib.rs @@ -523,12 +523,13 @@ fn urdf_to_colliders( } Geometry::Mesh { filename, scale } => { let full_path = mesh_dir.join(filename); - let _scale = scale + let scale = scale .map(|s| Vector::new(s.x as Real, s.y as Real, s.z as Real)) .unwrap_or_else(|| Vector::::repeat(1.0)); let Ok(loaded_mesh) = rapier3d_meshloader::load_from_path( full_path, &MeshConverter::TriMeshWithFlags(options.trimesh_flags), + scale, ) else { return Vec::new(); }; From 7617ab03c7cd3614de9881c3a025d5dda78722b2 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Thu, 10 Oct 2024 10:17:15 +0200 Subject: [PATCH 3/7] fix docs --- crates/rapier3d-meshloader/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/rapier3d-meshloader/src/lib.rs b/crates/rapier3d-meshloader/src/lib.rs index ecac82181..dae049e76 100644 --- a/crates/rapier3d-meshloader/src/lib.rs +++ b/crates/rapier3d-meshloader/src/lib.rs @@ -46,8 +46,8 @@ pub enum MeshLoaderError { /// you specify if the computed [`SharedShape`] is a triangle mesh, its convex hull, /// bounding box, etc. /// - `scale`: the scaling factor applied to the geometry input to the `converter`. This scale will -/// affect at the geometric level the [`StlShape::shape`]. Note that raw mesh value stored -/// in [`StlShape::raw_mesh`] remains unscaled. +/// affect at the geometric level the [`LoadedShape::shape`]. Note that raw mesh value stored +/// in [`LoadedShape::raw_mesh`] remains unscaled. // TODO: call a function for each mesh to load ? To be able to have a different mesh converter? pub fn load_from_path( path: impl AsRef, @@ -80,8 +80,8 @@ pub fn load_from_path( /// you specify if the computed [`SharedShape`] is a triangle mesh, its convex hull, /// bounding box, etc. /// - `scale`: the scaling factor applied to the geometry input to the `converter`. This scale will -/// affect at the geometric level the [`StlShape::shape`]. Note that raw mesh value stored -/// in [`StlShape::raw_mesh`] remains unscaled. +/// affect at the geometric level the [`LoadedShape::shape`]. Note that raw mesh value stored +/// in [`LoadedShape::raw_mesh`] remains unscaled. pub fn load_from_raw_mesh( raw_mesh: &Mesh, converter: &MeshConverter, From 1b7713f5d78537f434cf6a40b8448efd68a8cbfe Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Mon, 4 Nov 2024 17:37:04 +0100 Subject: [PATCH 4/7] include readme to avoid doc duplication --- crates/rapier3d-meshloader/README.md | 10 +++++++--- crates/rapier3d-meshloader/src/lib.rs | 12 +----------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/crates/rapier3d-meshloader/README.md b/crates/rapier3d-meshloader/README.md index 98d61a0e9..003a7fae4 100644 --- a/crates/rapier3d-meshloader/README.md +++ b/crates/rapier3d-meshloader/README.md @@ -1,8 +1,12 @@ -## 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-stl` +Rapier is a set of 2D and 3D physics engines for games, animation, and robotics. The `rapier3d-meshloader` crate lets you create a shape compatible with `rapier3d` and `parry3d` (the underlying collision-detection -library) from an STL file. +library) from different file formats, see the following features list: + +- `stl`: support .stl files +- `collada`: support .dae files +- `wavefront`: support .obj files ## Resources and discussions diff --git a/crates/rapier3d-meshloader/src/lib.rs b/crates/rapier3d-meshloader/src/lib.rs index dae049e76..3be659326 100644 --- a/crates/rapier3d-meshloader/src/lib.rs +++ b/crates/rapier3d-meshloader/src/lib.rs @@ -1,14 +1,4 @@ -//! ## Mesh loader for the Rapier physics engine -//! -//! Rapier is a set of 2D and 3D physics engines for games, animation, and robotics. The `rapier3d-meshloader` -//! crate lets you create a shape compatible with `rapier3d` and `parry3d` (the underlying collision-detection -//! library) from different file formats, see the following features list: -//! `stl`: support .stl files -//! `collada`: support .dae files -//! `wavefront`: support .obj files -//! -//! See documentation from [`mesh_loader`] for more details. - +#![doc = include_str!("../README.md")] #![deny(missing_docs)] use mesh_loader::Mesh; From 8a55b7fa15df84b310d2e7af1df54ab34a62fd74 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Mon, 4 Nov 2024 17:44:29 +0100 Subject: [PATCH 5/7] pr feedbacks --- crates/rapier3d-meshloader/src/lib.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/crates/rapier3d-meshloader/src/lib.rs b/crates/rapier3d-meshloader/src/lib.rs index 3be659326..32051e575 100644 --- a/crates/rapier3d-meshloader/src/lib.rs +++ b/crates/rapier3d-meshloader/src/lib.rs @@ -28,7 +28,7 @@ pub enum MeshLoaderError { Io(#[from] std::io::Error), } -/// Loads an file as shapes from a file. +/// Loads parry shapes from a file. /// /// # Parameters /// - `path`: the file’s path. @@ -38,7 +38,6 @@ pub enum MeshLoaderError { /// - `scale`: the scaling factor applied to the geometry input to the `converter`. This scale will /// affect at the geometric level the [`LoadedShape::shape`]. Note that raw mesh value stored /// in [`LoadedShape::raw_mesh`] remains unscaled. -// TODO: call a function for each mesh to load ? To be able to have a different mesh converter? pub fn load_from_path( path: impl AsRef, converter: &MeshConverter, @@ -50,14 +49,11 @@ pub fn load_from_path( for (raw_mesh, _) in scene.meshes.into_iter().zip(scene.materials) { let shape = load_from_raw_mesh(&raw_mesh, converter, scale); - colliders.push(match shape { - Ok((shape, pose)) => Ok(LoadedShape { - shape, - pose, - raw_mesh, - }), - Err(e) => Err(e), - }); + colliders.push(shape.map(|(shape, pose)| LoadedShape { + shape, + pose, + raw_mesh, + })); } Ok(colliders) } From 0312820a69e4897908f3ace901a31b0a4deb0b87 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Mon, 4 Nov 2024 17:52:07 +0100 Subject: [PATCH 6/7] comment out meshloader publish script --- scripts/publish-extra-formats.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/publish-extra-formats.sh b/scripts/publish-extra-formats.sh index af48e7773..e727256f2 100755 --- a/scripts/publish-extra-formats.sh +++ b/scripts/publish-extra-formats.sh @@ -3,8 +3,9 @@ currdir=$(pwd) ### Publish rapier3d-meshloader. -cd "crates/rapier3d-meshloader" && cargo publish $DRY_RUN || exit 1 -cd "$currdir" || exit 2 +# FIXME: This is commented while we have not created the corresponding crate on crates.io +# cd "crates/rapier3d-meshloader" && cargo publish $DRY_RUN || exit 1 +# cd "$currdir" || exit 2 ### Publish rapier3d-urdf. cd "crates/rapier3d-urdf" && cargo publish $DRY_RUN || exit 1 From b27377b1be648111c763b9cf2f16db19471b6d1e Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Fri, 8 Nov 2024 17:22:07 +0100 Subject: [PATCH 7/7] uncomment publish script --- scripts/publish-extra-formats.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/publish-extra-formats.sh b/scripts/publish-extra-formats.sh index e727256f2..af48e7773 100755 --- a/scripts/publish-extra-formats.sh +++ b/scripts/publish-extra-formats.sh @@ -3,9 +3,8 @@ currdir=$(pwd) ### Publish rapier3d-meshloader. -# FIXME: This is commented while we have not created the corresponding crate on crates.io -# cd "crates/rapier3d-meshloader" && cargo publish $DRY_RUN || exit 1 -# cd "$currdir" || exit 2 +cd "crates/rapier3d-meshloader" && cargo publish $DRY_RUN || exit 1 +cd "$currdir" || exit 2 ### Publish rapier3d-urdf. cd "crates/rapier3d-urdf" && cargo publish $DRY_RUN || exit 1