Skip to content

Commit

Permalink
Merge pull request #399 from Aceeri/colliderview-conversions
Browse files Browse the repository at this point in the history
Conversions from `ColliderView` into parry's `SharedShape`/`TypedShape`
  • Loading branch information
sebcrozet authored Aug 6, 2023
2 parents d289865 + d5140aa commit e935b5a
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/ball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/capsule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
101 changes: 101 additions & 0 deletions src/geometry/shape_views/collider_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>),
Expand Down Expand Up @@ -146,7 +147,107 @@ impl<'a> From<TypedShape<'a>> for ColliderView<'a> {
}
}

impl<'a> From<ColliderView<'a>> for TypedShape<'a> {
fn from(collider_view: ColliderView<'a>) -> TypedShape<'a> {
collider_view.as_typed_shape()
}
}

impl<'a> From<ColliderView<'a>> 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<SharedShape> {
let result = match self {
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/compound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/cone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/convex_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/convex_polyhedron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/cuboid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/cylinder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/halfspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/heightfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/polyline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/round_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/trimesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e935b5a

Please sign in to comment.