avian3d/collision/contact_types/
feature_id.rs

1use bevy::prelude::*;
2
3/// A feature ID indicating the type of a geometric feature: a vertex, an edge, or (in 3D) a face.
4///
5/// This type packs the feature type into the same value as the feature index,
6/// which indicates the specific vertex/edge/face that this ID belongs to.
7#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Reflect)]
8#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
9#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
10#[reflect(Debug, Hash, PartialEq)]
11pub struct PackedFeatureId(pub u32);
12
13impl PackedFeatureId {
14    /// Packed feature id identifying an unknown feature.
15    pub const UNKNOWN: Self = Self(0);
16
17    const CODE_MASK: u32 = 0x3fff_ffff;
18    const HEADER_MASK: u32 = !Self::CODE_MASK;
19    const HEADER_VERTEX: u32 = 0b01 << 30;
20    #[cfg(feature = "3d")]
21    const HEADER_EDGE: u32 = 0b10 << 30;
22    const HEADER_FACE: u32 = 0b11 << 30;
23
24    /// Converts a vertex feature id into a packed feature id.
25    pub fn vertex(code: u32) -> Self {
26        assert_eq!(code & Self::HEADER_MASK, 0);
27        Self(Self::HEADER_VERTEX | code)
28    }
29
30    /// Converts a edge feature id into a packed feature id.
31    #[cfg(feature = "3d")]
32    pub fn edge(code: u32) -> Self {
33        assert_eq!(code & Self::HEADER_MASK, 0);
34        Self(Self::HEADER_EDGE | code)
35    }
36
37    /// Converts a face feature id into a packed feature id.
38    pub fn face(code: u32) -> Self {
39        assert_eq!(code & Self::HEADER_MASK, 0);
40        Self(Self::HEADER_FACE | code)
41    }
42
43    /// Is the identified feature a face?
44    pub fn is_face(self) -> bool {
45        self.0 & Self::HEADER_MASK == Self::HEADER_FACE
46    }
47
48    /// Is the identified feature a vertex?
49    pub fn is_vertex(self) -> bool {
50        self.0 & Self::HEADER_MASK == Self::HEADER_VERTEX
51    }
52
53    /// Is the identified feature an edge?
54    #[cfg(feature = "3d")]
55    pub fn is_edge(self) -> bool {
56        self.0 & Self::HEADER_MASK == Self::HEADER_EDGE
57    }
58
59    /// Is the identified feature unknown?
60    pub fn is_unknown(self) -> bool {
61        self == Self::UNKNOWN
62    }
63}
64
65impl From<u32> for PackedFeatureId {
66    fn from(code: u32) -> Self {
67        Self(code)
68    }
69}
70
71#[cfg(any(feature = "parry-f32", feature = "parry-f64"))]
72impl From<crate::parry::shape::PackedFeatureId> for PackedFeatureId {
73    fn from(id: crate::parry::shape::PackedFeatureId) -> Self {
74        Self(id.0)
75    }
76}