ncollide_entities 0.5.1

DEPRECATED: use ncollide2d or ncollide3d insead.
//!
//! Shape composed from the union of primitives.
//!

use na::Translate;
use na;
use bounding_volume::{self, AABB, BoundingVolume};
use partitioning::BVT;
use math::{Point, Isometry};
use shape::ShapeHandle;

/// A compound shape with an aabb bounding volume.
///
/// A compound shape is a shape composed of the union of several simpler shape. This is
/// the main way of creating a concave shape from convex parts. Each parts can have its own
/// delta transformation to shift or rotate it with regard to the other shapes.
pub struct Compound<P: Point, M> {
    shapes:  Vec<(M, ShapeHandle<P, M>)>,
    bvt:     BVT<usize, AABB<P>>,
    bvs:     Vec<AABB<P>>
}

impl<P: Point, M: Clone> Clone for Compound<P, M> {
    fn clone(&self) -> Compound<P, M> {
        Compound {
            shapes: self.shapes.clone(),
            bvt:    self.bvt.clone(),
            bvs:    self.bvs.clone()
        }
    }
}

impl<P, M> Compound<P, M>
    where P:       Point,
          P::Vect: Translate<P>,
          M:       Isometry<P, P::Vect> {
    /// Builds a new compound shape.
    pub fn new(shapes: Vec<(M, ShapeHandle<P, M>)>) -> Compound<P, M> {
        let mut bvs    = Vec::new();
        let mut leaves = Vec::new();

        for (i, &(ref delta, ref shape)) in shapes.iter().enumerate() {
            // loosen for better persistancy
            let bv = bounding_volume::aabb(shape.as_ref(), delta).loosened(na::cast(0.04f64));

            bvs.push(bv.clone());
            leaves.push((i, bv));
        }

        let bvt = BVT::new_balanced(leaves);

        Compound {
            shapes:  shapes,
            bvt:     bvt,
            bvs:     bvs
        }
    }
}

impl<P: Point, M> Compound<P, M> {
    /// The shapes of this compound shape.
    #[inline]
    pub fn shapes(&self) -> &[(M, ShapeHandle<P, M>)] {
        &self.shapes[..]
    }

    /// The optimization structure used by this compound shape.
    #[inline]
    pub fn bvt(&self) -> &BVT<usize, AABB<P>> {
        &self.bvt
    }

    /// The shapes bounding volumes.
    #[inline]
    pub fn bounding_volumes(&self) -> &[AABB<P>] {
        &self.bvs[..]
    }

    /// The AABB of the i-th shape compositing this compound.
    #[inline]
    pub fn aabb_at(&self, i: usize) -> &AABB<P> {
        &self.bvs[i]
    }
}