From 9bf1321f8f1d2e116f44c2461a53f302c4ef4171 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Tue, 8 Dec 2020 17:31:49 +0100 Subject: Outsource the contact manifold, SAT, and some shapes. --- src/geometry/shape.rs | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) (limited to 'src/geometry/shape.rs') diff --git a/src/geometry/shape.rs b/src/geometry/shape.rs index 66840a0..80b24c5 100644 --- a/src/geometry/shape.rs +++ b/src/geometry/shape.rs @@ -1,17 +1,18 @@ use crate::dynamics::MassProperties; use crate::geometry::{Ball, Capsule, Cuboid, HeightField, Segment, Triangle, Trimesh}; use crate::math::Isometry; +use buckler::bounding_volume::AABB; +use buckler::query::{PointQuery, RayCast}; use downcast_rs::{impl_downcast, DowncastSync}; #[cfg(feature = "serde-serialize")] use erased_serde::Serialize; -use ncollide::bounding_volume::{HasBoundingVolume, AABB}; -use ncollide::query::{PointQuery, RayCast}; use num::Zero; use num_derive::FromPrimitive; #[cfg(feature = "dim3")] use { - crate::geometry::{Cone, Cylinder, PolygonalFeatureMap, RoundCylinder}, - ncollide::bounding_volume::BoundingVolume, + crate::geometry::{Cone, Cylinder, RoundCylinder}, + buckler::bounding_volume::BoundingVolume, + buckler::shape::PolygonalFeatureMap, }; #[derive(Copy, Clone, Debug, FromPrimitive)] @@ -57,7 +58,7 @@ pub enum ShapeType { } /// Trait implemented by shapes usable by Rapier. -pub trait Shape: RayCast + PointQuery + DowncastSync { +pub trait Shape: RayCast + PointQuery + DowncastSync { /// Convert this shape as a serializable entity. #[cfg(feature = "serde-serialize")] fn as_serialize(&self) -> Option<&dyn Serialize> { @@ -67,7 +68,7 @@ pub trait Shape: RayCast + PointQuery + DowncastSync { // TODO: add a compute_local_aabb method? /// Computes the AABB of this shape. - fn compute_aabb(&self, position: &Isometry) -> AABB; + fn compute_aabb(&self, position: &Isometry) -> AABB; /// Compute the mass-properties of this shape given its uniform density. fn mass_properties(&self, density: f32) -> MassProperties; @@ -144,8 +145,8 @@ impl Shape for Ball { Some(self as &dyn Serialize) } - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.bounding_volume(position) + fn compute_aabb(&self, position: &Isometry) -> AABB { + self.aabb(position) } fn mass_properties(&self, density: f32) -> MassProperties { @@ -163,7 +164,7 @@ impl Shape for Ball { // Some(self as &dyn Serialize) // } // -// fn compute_aabb(&self, position: &Isometry) -> AABB { +// fn compute_aabb(&self, position: &Isometry) -> AABB { // self.aabb(position) // } // @@ -182,8 +183,8 @@ impl Shape for Cuboid { Some(self as &dyn Serialize) } - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.bounding_volume(position) + fn compute_aabb(&self, position: &Isometry) -> AABB { + self.aabb(position) } fn mass_properties(&self, density: f32) -> MassProperties { @@ -206,7 +207,7 @@ impl Shape for Capsule { Some(self as &dyn Serialize) } - fn compute_aabb(&self, position: &Isometry) -> AABB { + fn compute_aabb(&self, position: &Isometry) -> AABB { self.aabb(position) } @@ -230,8 +231,8 @@ impl Shape for Triangle { Some(self as &dyn Serialize) } - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.bounding_volume(position) + fn compute_aabb(&self, position: &Isometry) -> AABB { + self.aabb(position) } fn mass_properties(&self, _density: f32) -> MassProperties { @@ -254,8 +255,8 @@ impl Shape for Segment { Some(self as &dyn Serialize) } - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.bounding_volume(position) + fn compute_aabb(&self, position: &Isometry) -> AABB { + self.aabb(position) } fn mass_properties(&self, _density: f32) -> MassProperties { @@ -278,7 +279,7 @@ impl Shape for Trimesh { Some(self as &dyn Serialize) } - fn compute_aabb(&self, position: &Isometry) -> AABB { + fn compute_aabb(&self, position: &Isometry) -> AABB { self.aabb(position) } @@ -297,8 +298,8 @@ impl Shape for HeightField { Some(self as &dyn Serialize) } - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.bounding_volume(position) + fn compute_aabb(&self, position: &Isometry) -> AABB { + self.aabb(position) } fn mass_properties(&self, _density: f32) -> MassProperties { @@ -317,8 +318,8 @@ impl Shape for Cylinder { Some(self as &dyn Serialize) } - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.bounding_volume(position) + fn compute_aabb(&self, position: &Isometry) -> AABB { + self.aabb(position) } fn mass_properties(&self, density: f32) -> MassProperties { @@ -342,8 +343,8 @@ impl Shape for Cone { Some(self as &dyn Serialize) } - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.bounding_volume(position) + fn compute_aabb(&self, position: &Isometry) -> AABB { + self.aabb(position) } fn mass_properties(&self, density: f32) -> MassProperties { @@ -367,7 +368,7 @@ impl Shape for RoundCylinder { Some(self as &dyn Serialize) } - fn compute_aabb(&self, position: &Isometry) -> AABB { + fn compute_aabb(&self, position: &Isometry) -> AABB { self.cylinder .compute_aabb(position) .loosened(self.border_radius) -- cgit From cc6d1b973002b4d366bc81ec6bf9e8240ad7b404 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Mon, 14 Dec 2020 15:51:43 +0100 Subject: Outsource the Shape trait, wquadtree, and shape types. --- src/geometry/shape.rs | 393 -------------------------------------------------- 1 file changed, 393 deletions(-) delete mode 100644 src/geometry/shape.rs (limited to 'src/geometry/shape.rs') diff --git a/src/geometry/shape.rs b/src/geometry/shape.rs deleted file mode 100644 index 80b24c5..0000000 --- a/src/geometry/shape.rs +++ /dev/null @@ -1,393 +0,0 @@ -use crate::dynamics::MassProperties; -use crate::geometry::{Ball, Capsule, Cuboid, HeightField, Segment, Triangle, Trimesh}; -use crate::math::Isometry; -use buckler::bounding_volume::AABB; -use buckler::query::{PointQuery, RayCast}; -use downcast_rs::{impl_downcast, DowncastSync}; -#[cfg(feature = "serde-serialize")] -use erased_serde::Serialize; -use num::Zero; -use num_derive::FromPrimitive; -#[cfg(feature = "dim3")] -use { - crate::geometry::{Cone, Cylinder, RoundCylinder}, - buckler::bounding_volume::BoundingVolume, - buckler::shape::PolygonalFeatureMap, -}; - -#[derive(Copy, Clone, Debug, FromPrimitive)] -/// Enum representing the type of a shape. -pub enum ShapeType { - /// A ball shape. - Ball = 0, - /// A convex polygon shape. - Polygon, - /// A cuboid shape. - Cuboid, - /// A capsule shape. - Capsule, - /// A segment shape. - Segment, - /// A triangle shape. - Triangle, - /// A triangle mesh shape. - Trimesh, - /// A heightfield shape. - HeightField, - #[cfg(feature = "dim3")] - /// A cylindrical shape. - Cylinder, - #[cfg(feature = "dim3")] - /// A cylindrical shape. - Cone, - // /// A custom shape type. - // Custom(u8), - // /// A cuboid with rounded corners. - // RoundedCuboid, - // /// A triangle with rounded corners. - // RoundedTriangle, - // /// A triangle-mesh with rounded corners. - // RoundedTrimesh, - // /// An heightfield with rounded corners. - // RoundedHeightField, - /// A cylinder with rounded corners. - #[cfg(feature = "dim3")] - RoundCylinder, - // /// A cone with rounded corners. - // RoundedCone, -} - -/// Trait implemented by shapes usable by Rapier. -pub trait Shape: RayCast + PointQuery + DowncastSync { - /// Convert this shape as a serializable entity. - #[cfg(feature = "serde-serialize")] - fn as_serialize(&self) -> Option<&dyn Serialize> { - None - } - - // TODO: add a compute_local_aabb method? - - /// Computes the AABB of this shape. - fn compute_aabb(&self, position: &Isometry) -> AABB; - - /// Compute the mass-properties of this shape given its uniform density. - fn mass_properties(&self, density: f32) -> MassProperties; - - /// Gets the type tag of this shape. - fn shape_type(&self) -> ShapeType; - - /// Converts this shape to a polygonal feature-map, if it is one. - #[cfg(feature = "dim3")] - fn as_polygonal_feature_map(&self) -> Option<(&dyn PolygonalFeatureMap, f32)> { - None - } - - // fn as_rounded(&self) -> Option<&Rounded>> { - // None - // } -} - -impl_downcast!(sync Shape); - -impl dyn Shape { - /// Converts this abstract shape to a ball, if it is one. - pub fn as_ball(&self) -> Option<&Ball> { - self.downcast_ref() - } - - /// Converts this abstract shape to a cuboid, if it is one. - pub fn as_cuboid(&self) -> Option<&Cuboid> { - self.downcast_ref() - } - - /// Converts this abstract shape to a capsule, if it is one. - pub fn as_capsule(&self) -> Option<&Capsule> { - self.downcast_ref() - } - - /// Converts this abstract shape to a triangle, if it is one. - pub fn as_triangle(&self) -> Option<&Triangle> { - self.downcast_ref() - } - - /// Converts this abstract shape to a triangle mesh, if it is one. - pub fn as_trimesh(&self) -> Option<&Trimesh> { - self.downcast_ref() - } - - /// Converts this abstract shape to a heightfield, if it is one. - pub fn as_heightfield(&self) -> Option<&HeightField> { - self.downcast_ref() - } - - /// Converts this abstract shape to a cylinder, if it is one. - #[cfg(feature = "dim3")] - pub fn as_cylinder(&self) -> Option<&Cylinder> { - self.downcast_ref() - } - - /// Converts this abstract shape to a cone, if it is one. - #[cfg(feature = "dim3")] - pub fn as_cone(&self) -> Option<&Cone> { - self.downcast_ref() - } - - /// Converts this abstract shape to a cone, if it is one. - #[cfg(feature = "dim3")] - pub fn as_round_cylinder(&self) -> Option<&RoundCylinder> { - self.downcast_ref() - } -} - -impl Shape for Ball { - #[cfg(feature = "serde-serialize")] - fn as_serialize(&self) -> Option<&dyn Serialize> { - Some(self as &dyn Serialize) - } - - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.aabb(position) - } - - fn mass_properties(&self, density: f32) -> MassProperties { - MassProperties::from_ball(density, self.radius) - } - - fn shape_type(&self) -> ShapeType { - ShapeType::Ball - } -} - -// impl Shape for Polygon { -// #[cfg(feature = "serde-serialize")] -// fn as_serialize(&self) -> Option<&dyn Serialize> { -// Some(self as &dyn Serialize) -// } -// -// fn compute_aabb(&self, position: &Isometry) -> AABB { -// self.aabb(position) -// } -// -// fn mass_properties(&self, _density: f32) -> MassProperties { -// unimplemented!() -// } -// -// fn shape_type(&self) -> ShapeType { -// ShapeType::Polygon -// } -// } - -impl Shape for Cuboid { - #[cfg(feature = "serde-serialize")] - fn as_serialize(&self) -> Option<&dyn Serialize> { - Some(self as &dyn Serialize) - } - - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.aabb(position) - } - - fn mass_properties(&self, density: f32) -> MassProperties { - MassProperties::from_cuboid(density, self.half_extents) - } - - fn shape_type(&self) -> ShapeType { - ShapeType::Cuboid - } - - #[cfg(feature = "dim3")] - fn as_polygonal_feature_map(&self) -> Option<(&dyn PolygonalFeatureMap, f32)> { - Some((self as &dyn PolygonalFeatureMap, 0.0)) - } -} - -impl Shape for Capsule { - #[cfg(feature = "serde-serialize")] - fn as_serialize(&self) -> Option<&dyn Serialize> { - Some(self as &dyn Serialize) - } - - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.aabb(position) - } - - fn mass_properties(&self, density: f32) -> MassProperties { - MassProperties::from_capsule(density, self.segment.a, self.segment.b, self.radius) - } - - fn shape_type(&self) -> ShapeType { - ShapeType::Capsule - } - - #[cfg(feature = "dim3")] - fn as_polygonal_feature_map(&self) -> Option<(&dyn PolygonalFeatureMap, f32)> { - Some((&self.segment as &dyn PolygonalFeatureMap, self.radius)) - } -} - -impl Shape for Triangle { - #[cfg(feature = "serde-serialize")] - fn as_serialize(&self) -> Option<&dyn Serialize> { - Some(self as &dyn Serialize) - } - - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.aabb(position) - } - - fn mass_properties(&self, _density: f32) -> MassProperties { - MassProperties::zero() - } - - fn shape_type(&self) -> ShapeType { - ShapeType::Triangle - } - - #[cfg(feature = "dim3")] - fn as_polygonal_feature_map(&self) -> Option<(&dyn PolygonalFeatureMap, f32)> { - Some((self as &dyn PolygonalFeatureMap, 0.0)) - } -} - -impl Shape for Segment { - #[cfg(feature = "serde-serialize")] - fn as_serialize(&self) -> Option<&dyn Serialize> { - Some(self as &dyn Serialize) - } - - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.aabb(position) - } - - fn mass_properties(&self, _density: f32) -> MassProperties { - MassProperties::zero() - } - - fn shape_type(&self) -> ShapeType { - ShapeType::Segment - } - - #[cfg(feature = "dim3")] - fn as_polygonal_feature_map(&self) -> Option<(&dyn PolygonalFeatureMap, f32)> { - Some((self as &dyn PolygonalFeatureMap, 0.0)) - } -} - -impl Shape for Trimesh { - #[cfg(feature = "serde-serialize")] - fn as_serialize(&self) -> Option<&dyn Serialize> { - Some(self as &dyn Serialize) - } - - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.aabb(position) - } - - fn mass_properties(&self, _density: f32) -> MassProperties { - MassProperties::zero() - } - - fn shape_type(&self) -> ShapeType { - ShapeType::Trimesh - } -} - -impl Shape for HeightField { - #[cfg(feature = "serde-serialize")] - fn as_serialize(&self) -> Option<&dyn Serialize> { - Some(self as &dyn Serialize) - } - - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.aabb(position) - } - - fn mass_properties(&self, _density: f32) -> MassProperties { - MassProperties::zero() - } - - fn shape_type(&self) -> ShapeType { - ShapeType::HeightField - } -} - -#[cfg(feature = "dim3")] -impl Shape for Cylinder { - #[cfg(feature = "serde-serialize")] - fn as_serialize(&self) -> Option<&dyn Serialize> { - Some(self as &dyn Serialize) - } - - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.aabb(position) - } - - fn mass_properties(&self, density: f32) -> MassProperties { - MassProperties::from_cylinder(density, self.half_height, self.radius) - } - - fn shape_type(&self) -> ShapeType { - ShapeType::Cylinder - } - - #[cfg(feature = "dim3")] - fn as_polygonal_feature_map(&self) -> Option<(&dyn PolygonalFeatureMap, f32)> { - Some((self as &dyn PolygonalFeatureMap, 0.0)) - } -} - -#[cfg(feature = "dim3")] -impl Shape for Cone { - #[cfg(feature = "serde-serialize")] - fn as_serialize(&self) -> Option<&dyn Serialize> { - Some(self as &dyn Serialize) - } - - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.aabb(position) - } - - fn mass_properties(&self, density: f32) -> MassProperties { - MassProperties::from_cone(density, self.half_height, self.radius) - } - - fn shape_type(&self) -> ShapeType { - ShapeType::Cone - } - - #[cfg(feature = "dim3")] - fn as_polygonal_feature_map(&self) -> Option<(&dyn PolygonalFeatureMap, f32)> { - Some((self as &dyn PolygonalFeatureMap, 0.0)) - } -} - -#[cfg(feature = "dim3")] -impl Shape for RoundCylinder { - #[cfg(feature = "serde-serialize")] - fn as_serialize(&self) -> Option<&dyn Serialize> { - Some(self as &dyn Serialize) - } - - fn compute_aabb(&self, position: &Isometry) -> AABB { - self.cylinder - .compute_aabb(position) - .loosened(self.border_radius) - } - - fn mass_properties(&self, density: f32) -> MassProperties { - // We ignore the margin here. - self.cylinder.mass_properties(density) - } - - fn shape_type(&self) -> ShapeType { - ShapeType::RoundCylinder - } - - #[cfg(feature = "dim3")] - fn as_polygonal_feature_map(&self) -> Option<(&dyn PolygonalFeatureMap, f32)> { - Some(( - &self.cylinder as &dyn PolygonalFeatureMap, - self.border_radius, - )) - } -} -- cgit