diff --git a/CHANGELOG.md b/CHANGELOG.md index 71df4c34..1204da4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased ### Added +- `ColliderView::as_typed_shape` and `::to_shared_shape` to convert a `ColliderView` to a parry’s + `TypedShape` or `SharedShape`. The `From` trait has also been implemented accordingly. +- Implement `Copy` for `ColliderView` and all the other non-mut shape views. - Add `RapierContext::rigid_body_colliders` to retrieve all collider entities attached to this rigid-body. ## 0.22.0 (10 July 2023) diff --git a/src/geometry/shape_views/ball.rs b/src/geometry/shape_views/ball.rs index 3f4878fb..7964cb28 100644 --- a/src/geometry/shape_views/ball.rs +++ b/src/geometry/shape_views/ball.rs @@ -2,6 +2,7 @@ use crate::math::Real; use rapier::parry::shape::Ball; /// Read-only access to the properties of a ball. +#[derive(Copy, Clone)] pub struct BallView<'a> { /// The raw shape from Rapier. pub raw: &'a Ball, diff --git a/src/geometry/shape_views/capsule.rs b/src/geometry/shape_views/capsule.rs index 2391d0d4..8db6f210 100644 --- a/src/geometry/shape_views/capsule.rs +++ b/src/geometry/shape_views/capsule.rs @@ -3,6 +3,7 @@ use crate::math::{Real, Rot, Vect}; use rapier::parry::shape::Capsule; /// Read-only access to the properties of a capsule. +#[derive(Copy, Clone)] pub struct CapsuleView<'a> { /// The raw shape from Rapier. pub raw: &'a Capsule, diff --git a/src/geometry/shape_views/collider_view.rs b/src/geometry/shape_views/collider_view.rs index cb5dbb5d..2cf2107b 100644 --- a/src/geometry/shape_views/collider_view.rs +++ b/src/geometry/shape_views/collider_view.rs @@ -7,6 +7,7 @@ use rapier::parry::either::Either; use rapier::parry::shape::TypedShape; /// Read-only access to the properties of a collider. +#[derive(Copy, Clone)] pub enum ColliderView<'a> { /// A ball shape. Ball(BallView<'a>), @@ -146,7 +147,107 @@ impl<'a> From> for ColliderView<'a> { } } +impl<'a> From> for TypedShape<'a> { + fn from(collider_view: ColliderView<'a>) -> TypedShape<'a> { + collider_view.as_typed_shape() + } +} + +impl<'a> From> for SharedShape { + fn from(collider_view: ColliderView<'a>) -> SharedShape { + collider_view.to_shared_shape() + } +} + impl<'a> ColliderView<'a> { + /// Convert to [`parry::TypedShape`]. + pub fn as_typed_shape(self) -> TypedShape<'a> { + match self { + ColliderView::Ball(BallView { raw: s }) => TypedShape::Ball(s), + ColliderView::Cuboid(CuboidView { raw: s }) => TypedShape::Cuboid(s), + ColliderView::Capsule(CapsuleView { raw: s }) => TypedShape::Capsule(s), + ColliderView::Segment(SegmentView { raw: s }) => TypedShape::Segment(s), + ColliderView::Triangle(TriangleView { raw: s }) => TypedShape::Triangle(s), + ColliderView::TriMesh(TriMeshView { raw: s }) => TypedShape::TriMesh(s), + ColliderView::Polyline(PolylineView { raw: s }) => TypedShape::Polyline(s), + ColliderView::HalfSpace(HalfSpaceView { raw: s }) => TypedShape::HalfSpace(s), + ColliderView::HeightField(HeightFieldView { raw: s }) => TypedShape::HeightField(s), + ColliderView::Compound(CompoundView { raw: s }) => TypedShape::Compound(s), + #[cfg(feature = "dim2")] + ColliderView::ConvexPolygon(ConvexPolygonView { raw: s }) => { + TypedShape::ConvexPolygon(s) + } + #[cfg(feature = "dim3")] + ColliderView::ConvexPolyhedron(ConvexPolyhedronView { raw: s }) => { + TypedShape::ConvexPolyhedron(s) + } + #[cfg(feature = "dim3")] + ColliderView::Cylinder(CylinderView { raw: s }) => TypedShape::Cylinder(s), + #[cfg(feature = "dim3")] + ColliderView::Cone(ConeView { raw: s }) => TypedShape::Cone(s), + ColliderView::RoundCuboid(RoundCuboidView { raw: s }) => TypedShape::RoundCuboid(s), + ColliderView::RoundTriangle(RoundTriangleView { raw: s }) => { + TypedShape::RoundTriangle(s) + } + // RoundedTriMesh, + // RoundedHeightField, + #[cfg(feature = "dim2")] + ColliderView::RoundConvexPolygon(RoundConvexPolygonView { raw: s }) => { + TypedShape::RoundConvexPolygon(s) + } + #[cfg(feature = "dim3")] + ColliderView::RoundCylinder(RoundCylinderView { raw: s }) => { + TypedShape::RoundCylinder(s) + } + #[cfg(feature = "dim3")] + ColliderView::RoundCone(RoundConeView { raw: s }) => TypedShape::RoundCone(s), + #[cfg(feature = "dim3")] + ColliderView::RoundConvexPolyhedron(RoundConvexPolyhedronView { raw: s }) => { + TypedShape::RoundConvexPolyhedron(s) + } + } + } + + /// Convert to [`parry::SharedShape`]. + pub fn to_shared_shape(self) -> SharedShape { + match self { + ColliderView::Ball(BallView { raw }) => SharedShape::new(*raw), + ColliderView::Cuboid(CuboidView { raw }) => SharedShape::new(*raw), + ColliderView::Capsule(CapsuleView { raw }) => SharedShape::new(*raw), + ColliderView::Segment(SegmentView { raw }) => SharedShape::new(*raw), + ColliderView::Triangle(TriangleView { raw }) => SharedShape::new(*raw), + ColliderView::TriMesh(TriMeshView { raw }) => SharedShape::new(raw.clone()), + ColliderView::Polyline(PolylineView { raw }) => SharedShape::new(raw.clone()), + ColliderView::HalfSpace(HalfSpaceView { raw }) => SharedShape::new(*raw), + ColliderView::HeightField(HeightFieldView { raw }) => SharedShape::new(raw.clone()), + ColliderView::Compound(CompoundView { raw }) => SharedShape::new(raw.clone()), + #[cfg(feature = "dim2")] + ColliderView::ConvexPolygon(ConvexPolygonView { raw }) => SharedShape::new(raw.clone()), + #[cfg(feature = "dim3")] + ColliderView::ConvexPolyhedron(ConvexPolyhedronView { raw }) => { + SharedShape::new(raw.clone()) + } + #[cfg(feature = "dim3")] + ColliderView::Cylinder(CylinderView { raw }) => SharedShape::new(*raw), + #[cfg(feature = "dim3")] + ColliderView::Cone(ConeView { raw }) => SharedShape::new(*raw), + ColliderView::RoundCuboid(RoundCuboidView { raw }) => SharedShape::new(*raw), + ColliderView::RoundTriangle(RoundTriangleView { raw }) => SharedShape::new(*raw), + #[cfg(feature = "dim2")] + ColliderView::RoundConvexPolygon(RoundConvexPolygonView { raw }) => { + SharedShape::new(raw.clone()) + } + #[cfg(feature = "dim3")] + ColliderView::RoundCylinder(RoundCylinderView { raw }) => SharedShape::new(*raw), + #[cfg(feature = "dim3")] + ColliderView::RoundCone(RoundConeView { raw }) => SharedShape::new(*raw), + #[cfg(feature = "dim3")] + ColliderView::RoundConvexPolyhedron(RoundConvexPolyhedronView { raw }) => { + SharedShape::new(raw.clone()) + } + } + } + /// Compute the scaled version of `self.raw`. pub fn raw_scale_by(&self, scale: Vect, num_subdivisions: u32) -> Option { let result = match self { diff --git a/src/geometry/shape_views/compound.rs b/src/geometry/shape_views/compound.rs index 96469333..860628eb 100644 --- a/src/geometry/shape_views/compound.rs +++ b/src/geometry/shape_views/compound.rs @@ -3,6 +3,7 @@ use crate::math::{Rot, Vect}; use rapier::parry::shape::Compound; /// Read-only access to the properties of a compound shape. +#[derive(Copy, Clone)] pub struct CompoundView<'a> { /// The raw shape from Rapier. pub raw: &'a Compound, diff --git a/src/geometry/shape_views/cone.rs b/src/geometry/shape_views/cone.rs index 3ffab2ac..3c3b1965 100644 --- a/src/geometry/shape_views/cone.rs +++ b/src/geometry/shape_views/cone.rs @@ -2,6 +2,7 @@ use crate::math::Real; use rapier::parry::shape::Cone; /// Read-only access to the properties of a cone. +#[derive(Copy, Clone)] pub struct ConeView<'a> { /// The raw shape from Rapier. pub raw: &'a Cone, diff --git a/src/geometry/shape_views/convex_polygon.rs b/src/geometry/shape_views/convex_polygon.rs index 547cad52..5cccc800 100644 --- a/src/geometry/shape_views/convex_polygon.rs +++ b/src/geometry/shape_views/convex_polygon.rs @@ -2,6 +2,7 @@ use crate::math::Vect; use rapier::parry::shape::ConvexPolygon; /// Read-only access to the properties of a convex polygon. +#[derive(Copy, Clone)] pub struct ConvexPolygonView<'a> { /// The raw shape from Rapier. pub raw: &'a ConvexPolygon, diff --git a/src/geometry/shape_views/convex_polyhedron.rs b/src/geometry/shape_views/convex_polyhedron.rs index 5b977dea..2ea6a751 100644 --- a/src/geometry/shape_views/convex_polyhedron.rs +++ b/src/geometry/shape_views/convex_polyhedron.rs @@ -2,6 +2,7 @@ use crate::math::Vect; use rapier::parry::shape::ConvexPolyhedron; /// Read-only access to the properties of a convex polyhedron. +#[derive(Copy, Clone)] pub struct ConvexPolyhedronView<'a> { /// The raw shape from Rapier. pub raw: &'a ConvexPolyhedron, diff --git a/src/geometry/shape_views/cuboid.rs b/src/geometry/shape_views/cuboid.rs index 3bf8c149..443afee6 100644 --- a/src/geometry/shape_views/cuboid.rs +++ b/src/geometry/shape_views/cuboid.rs @@ -2,6 +2,7 @@ use crate::math::Vect; use rapier::geometry::Cuboid; /// Read-only access to the properties of a cuboid. +#[derive(Copy, Clone)] pub struct CuboidView<'a> { /// The raw shape from Rapier. pub raw: &'a Cuboid, diff --git a/src/geometry/shape_views/cylinder.rs b/src/geometry/shape_views/cylinder.rs index ab00d260..f7e59ea6 100644 --- a/src/geometry/shape_views/cylinder.rs +++ b/src/geometry/shape_views/cylinder.rs @@ -2,6 +2,7 @@ use crate::math::Real; use rapier::parry::shape::Cylinder; /// Read-only access to the properties of a cylinder. +#[derive(Copy, Clone)] pub struct CylinderView<'a> { /// The raw shape from Rapier. pub raw: &'a Cylinder, diff --git a/src/geometry/shape_views/halfspace.rs b/src/geometry/shape_views/halfspace.rs index 287f3f00..59974b05 100644 --- a/src/geometry/shape_views/halfspace.rs +++ b/src/geometry/shape_views/halfspace.rs @@ -2,6 +2,7 @@ use crate::math::Vect; use rapier::parry::shape::HalfSpace; /// Read-only access to the properties of a half-space. +#[derive(Copy, Clone)] pub struct HalfSpaceView<'a> { /// The raw shape from Rapier. pub raw: &'a HalfSpace, diff --git a/src/geometry/shape_views/heightfield.rs b/src/geometry/shape_views/heightfield.rs index 022c084f..8efa4d05 100644 --- a/src/geometry/shape_views/heightfield.rs +++ b/src/geometry/shape_views/heightfield.rs @@ -3,6 +3,7 @@ use rapier::parry::shape::HeightField; pub use rapier::parry::shape::HeightFieldCellStatus; /// Read-only access to the properties of a heightfield. +#[derive(Copy, Clone)] pub struct HeightFieldView<'a> { /// The raw shape from Rapier. pub raw: &'a HeightField, diff --git a/src/geometry/shape_views/polyline.rs b/src/geometry/shape_views/polyline.rs index 57489dd3..eb811d77 100644 --- a/src/geometry/shape_views/polyline.rs +++ b/src/geometry/shape_views/polyline.rs @@ -2,6 +2,7 @@ use crate::math::Vect; use rapier::parry::shape::Polyline; /// Read-only access to the properties of a polyline. +#[derive(Copy, Clone)] pub struct PolylineView<'a> { /// The raw shape from Rapier. pub raw: &'a Polyline, diff --git a/src/geometry/shape_views/round_shape.rs b/src/geometry/shape_views/round_shape.rs index 6b569cb9..936a1170 100644 --- a/src/geometry/shape_views/round_shape.rs +++ b/src/geometry/shape_views/round_shape.rs @@ -19,6 +19,7 @@ use { macro_rules! round_shape_view( ($RoundShape: ident, $RoundShapeView: ident, $ShapeView: ident, $RoundShapeViewMut: ident, $ShapeViewMut: ident) => { /// Read-only access to the properties of a round shape. + #[derive(Copy, Clone)] pub struct $RoundShapeView<'a> { /// The raw shape from Rapier. pub raw: &'a $RoundShape, diff --git a/src/geometry/shape_views/segment.rs b/src/geometry/shape_views/segment.rs index 0a4fc855..e3acf3b7 100644 --- a/src/geometry/shape_views/segment.rs +++ b/src/geometry/shape_views/segment.rs @@ -2,6 +2,7 @@ use crate::math::{Real, Vect}; use rapier::parry::shape::Segment; /// Read-only access to the properties of a segment. +#[derive(Copy, Clone)] pub struct SegmentView<'a> { /// The raw shape from Rapier. pub raw: &'a Segment, diff --git a/src/geometry/shape_views/triangle.rs b/src/geometry/shape_views/triangle.rs index 4b0a2f78..d676689b 100644 --- a/src/geometry/shape_views/triangle.rs +++ b/src/geometry/shape_views/triangle.rs @@ -2,6 +2,7 @@ use crate::math::{Real, Vect}; use rapier::parry::shape::Triangle; /// Read-only access to the properties of a triangle. +#[derive(Copy, Clone)] pub struct TriangleView<'a> { /// The raw shape from Rapier. pub raw: &'a Triangle, diff --git a/src/geometry/shape_views/trimesh.rs b/src/geometry/shape_views/trimesh.rs index a935ca80..9a94146e 100644 --- a/src/geometry/shape_views/trimesh.rs +++ b/src/geometry/shape_views/trimesh.rs @@ -2,6 +2,7 @@ use crate::math::Vect; use rapier::parry::shape::{TopologyError, TriMesh, TriMeshFlags}; /// Read-only access to the properties of a triangle mesh. +#[derive(Copy, Clone)] pub struct TriMeshView<'a> { /// The raw shape from Rapier. pub raw: &'a TriMesh,