Skip to main content

collider_shape/
shape.rs

1//! This module defines a trait for geometric shapes and a wrapper type for dynamic dispatch.
2
3use nalgebra::Vector3;
4use std::fmt::Debug;
5
6/// A type alias for an object implementing the Shape trait.
7/// This is separated from ShapeWrapper to allow for implementing the Debug trait.
8pub type InnerShapeWrapper = dyn Shape + Send + Sync;
9
10impl Debug for &InnerShapeWrapper {
11    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12        f.debug_struct("InnerShapeWrapper")
13            .field("shape_type", &self.get_shape_type())
14            .field("radius", &self.get_radius())
15            .field("half_extents", &self.get_half_extents())
16            .field("half_length", &self.get_half_length())
17            .finish()
18    }
19}
20
21impl PartialEq for &InnerShapeWrapper {
22    fn eq(&self, other: &Self) -> bool {
23        self.get_shape_type() == other.get_shape_type()
24            && self.get_radius() == other.get_radius()
25            && self.get_half_extents() == other.get_half_extents()
26            && self.get_half_length() == other.get_half_length()
27    }
28}
29
30/// A wrapper type for the Shape trait to allow dynamic dispatch.
31pub type ShapeWrapper = Box<InnerShapeWrapper>;
32
33impl PartialEq for ShapeWrapper {
34    fn eq(&self, other: &Self) -> bool {
35        self.get_shape_type() == other.get_shape_type()
36            && self.get_radius() == other.get_radius()
37            && self.get_half_extents() == other.get_half_extents()
38            && self.get_half_length() == other.get_half_length()
39    }
40}
41
42/// Shape trait for defining geometric shapes.
43pub trait Shape {
44    /// Returns whether the shape is convex or not.
45    fn is_convex(&self) -> bool;
46
47    /// Clones the shape and returns a boxed version of it.
48    fn clone_box(&self) -> ShapeWrapper;
49
50    /// Returns the shape type.
51    fn get_shape_type(&self) -> ShapeType;
52
53    /// Returns the radius of the shape.
54    fn get_radius(&self) -> Option<f32> {
55        None
56    }
57
58    /// Returns the half extents of the shape.
59    fn get_half_extents(&self) -> Option<Vector3<f32>> {
60        None
61    }
62
63    /// Returns the half length of the shape.
64    fn get_half_length(&self) -> Option<f32> {
65        None
66    }
67
68    /// Returns the mesh path of the shape.
69    fn get_mesh_path(&self) -> Option<String> {
70        None
71    }
72}
73
74#[cfg_attr(feature = "python", pyo3::prelude::pyclass)]
75#[derive(Clone, Copy, Debug, PartialEq, Eq)]
76pub enum ShapeType {
77    Capsule,
78    Cone,
79    Cuboid,
80    Cylinder,
81    Sphere,
82    Mesh,
83}