1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::math::{Isometry, Real};
use crate::partitioning::{GenericQbvh, IndexedData, Qbvh, QbvhStorage};
use crate::shape::Shape;
use crate::utils::DefaultStorage;

/// Trait implemented by shapes composed of multiple simpler shapes.
///
/// A composite shape is composed of several shapes. For example, this can
/// be a convex decomposition of a concave shape; or a triangle-mesh.
#[cfg(feature = "std")]
pub trait SimdCompositeShape {
    /// Applies a function to one sub-shape of this composite shape.
    fn map_part_at(&self, shape_id: u32, f: &mut dyn FnMut(Option<&Isometry<Real>>, &dyn Shape));

    /// Gets the acceleration structure of the composite shape.
    fn qbvh(&self) -> &Qbvh<u32>;
}

pub trait TypedSimdCompositeShape {
    type PartShape: ?Sized + Shape;
    type PartId: IndexedData;
    type QbvhStorage: QbvhStorage<Self::PartId>;

    fn map_typed_part_at(
        &self,
        shape_id: Self::PartId,
        f: impl FnMut(Option<&Isometry<Real>>, &Self::PartShape),
    );

    // TODO: we need this method because the compiler won't want
    // to cast `&Self::PartShape` to `&dyn Shape` because it complains
    // that `PairtShape` is not `Sized`.
    fn map_untyped_part_at(
        &self,
        shape_id: Self::PartId,
        f: impl FnMut(Option<&Isometry<Real>>, &dyn Shape),
    );

    fn typed_qbvh(&self) -> &GenericQbvh<Self::PartId, Self::QbvhStorage>;
}

#[cfg(feature = "std")]
impl<'a> TypedSimdCompositeShape for dyn SimdCompositeShape + 'a {
    type PartShape = dyn Shape;
    type PartId = u32;
    type QbvhStorage = DefaultStorage;

    fn map_typed_part_at(
        &self,
        shape_id: u32,
        mut f: impl FnMut(Option<&Isometry<Real>>, &Self::PartShape),
    ) {
        self.map_part_at(shape_id, &mut f)
    }

    fn map_untyped_part_at(
        &self,
        shape_id: u32,
        mut f: impl FnMut(Option<&Isometry<Real>>, &dyn Shape),
    ) {
        self.map_part_at(shape_id, &mut f)
    }

    fn typed_qbvh(&self) -> &GenericQbvh<Self::PartId, Self::QbvhStorage> {
        self.qbvh()
    }
}