ncollide3d/shape/
convex_polyhedron.rs

1use crate::math::{Isometry, Point, Vector};
2use crate::shape::{ConvexPolygonalFeature, SupportMap};
3use na::{RealField, Unit};
4
5/// An identifier of a feature of a convex polyhedron.
6///
7/// This identifier is shape-dependent and is seach that it
8/// allows an efficient retrieval of the geometric information of the
9/// feature.
10#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
12pub enum FeatureId {
13    /// Shape-dependent identifier of a vertex.
14    Vertex(usize),
15    #[cfg(feature = "dim3")]
16    /// Shape-dependent identifier of an edge.
17    Edge(usize),
18    /// Shape-dependent identifier of a face.
19    Face(usize),
20    // XXX: remove this variant.
21    /// Unknown identifier.
22    Unknown,
23}
24
25impl FeatureId {
26    /// Revries the value of the identifier if `self` is a vertex.
27    pub fn unwrap_vertex(self) -> usize {
28        match self {
29            FeatureId::Vertex(id) => id,
30            _ => panic!("The feature id does not identify a vertex."),
31        }
32    }
33
34    /// Revries the value of the identifier if `self` is an edge.
35    #[cfg(feature = "dim3")]
36    pub fn unwrap_edge(self) -> usize {
37        match self {
38            FeatureId::Edge(id) => id,
39            _ => panic!("The feature id does not identify an edge."),
40        }
41    }
42
43    /// Retrieves the value of the identifier if `self` is a face.
44    pub fn unwrap_face(self) -> usize {
45        match self {
46            FeatureId::Face(id) => id,
47            _ => panic!("The feature id does not identify a face."),
48        }
49    }
50}
51
52/// Trait implemented by all convex polyhedron.
53pub trait ConvexPolyhedron<N: RealField + Copy>: SupportMap<N> {
54    /// Gets the specified vertex in the shape local-space.
55    fn vertex(&self, id: FeatureId) -> Point<N>;
56    /// Fill `face` with the geometric description of the specified face, in the shape's local-space.
57    fn face(&self, id: FeatureId, face: &mut ConvexPolygonalFeature<N>);
58    #[cfg(feature = "dim3")]
59    /// Get the specified edge's vertices (in the shape local-space) and the vertices' identifiers.
60    fn edge(&self, id: FeatureId) -> (Point<N>, Point<N>, FeatureId, FeatureId);
61
62    /// Returns any normal from the normal cone of the given feature.
63    fn feature_normal(&self, feature: FeatureId) -> Unit<Vector<N>>;
64
65    /// Retrieve the face (in world-space) with a normal that maximizes the scalar product with `dir`.
66    fn support_face_toward(
67        &self,
68        transform: &Isometry<N>,
69        dir: &Unit<Vector<N>>,
70        out: &mut ConvexPolygonalFeature<N>,
71    );
72
73    /// Retrieve the feature (in world-space) which normal cone contains `dir`.
74    fn support_feature_toward(
75        &self,
76        transform: &Isometry<N>,
77        dir: &Unit<Vector<N>>,
78        _angle: N,
79        out: &mut ConvexPolygonalFeature<N>,
80    );
81
82    /// Retrieve the identifier of the feature which normal cone contains `dir`.
83    fn support_feature_id_toward(&self, local_dir: &Unit<Vector<N>>) -> FeatureId;
84}