Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use meshloader to support multiple file formats loading #744

Merged
merged 7 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rapier-ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ members = [
"examples3d-f64",
"benchmarks3d",
"crates/rapier3d-urdf",
"crates/rapier3d-stl",
"crates/rapier3d-meshloader",
]
resolver = "2"

Expand Down
22 changes: 22 additions & 0 deletions crates/rapier3d-meshloader/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
[package]
name = "rapier3d-stl"
name = "rapier3d-meshloader"
version = "0.3.0"
authors = ["Sébastien Crozet <[email protected]>"]
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" }
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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

Expand Down
86 changes: 86 additions & 0 deletions crates/rapier3d-meshloader/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]

use mesh_loader::Mesh;
use rapier3d::geometry::{MeshConverter, SharedShape};
use rapier3d::math::{Isometry, Point, Real, Vector};
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<Real>,
/// 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 parry 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.
/// - `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.
pub fn load_from_path(
path: impl AsRef<Path>,
converter: &MeshConverter,
scale: Vector<Real>,
) -> Result<Vec<Result<LoadedShape, MeshConverterError>>, 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, scale);

colliders.push(shape.map(|(shape, pose)| LoadedShape {
shape,
pose,
raw_mesh,
}));
}
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.
/// - `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.
pub fn load_from_raw_mesh(
raw_mesh: &Mesh,
converter: &MeshConverter,
scale: Vector<Real>,
) -> Result<(SharedShape, Isometry<Real>), MeshConverterError> {
let mut vertices: Vec<_> = raw_mesh
.vertices
.iter()
.map(|xyz| Point::new(xyz[0], xyz[1], xyz[2]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work?

Suggested change
.map(|xyz| Point::new(xyz[0], xyz[1], xyz[2]))
.map(Point::from)

Copy link
Contributor Author

@Vrixyz Vrixyz Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your suggestion doesn´t work with the following error:

error[E0277]: the trait bound OPoint<_, Const<3>>: From<&[f32; 3]> is not satisfied

I can fix it with cloned(), or could make an upstream pr to support that conversion?

.collect();
vertices
.iter_mut()
.for_each(|pt| pt.coords.component_mul_assign(&scale));
let indices: Vec<_> = raw_mesh.faces.clone();
converter.convert(vertices, indices)
}
9 changes: 0 additions & 9 deletions crates/rapier3d-stl/CHANGELOG.md

This file was deleted.

110 changes: 0 additions & 110 deletions crates/rapier3d-stl/src/lib.rs

This file was deleted.

6 changes: 6 additions & 0 deletions crates/rapier3d-urdf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 11 additions & 3 deletions crates/rapier3d-urdf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 }
8 changes: 5 additions & 3 deletions crates/rapier3d-urdf/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
## 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
`rapier3d` physics engine.

## 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

Are listed below some known limitations you might want to be aware of before picking this library. Contributions to
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:
Expand Down
Loading
Loading