ncollide3d/shape/
cylinder.rs

1//! Support mapping based Cylinder shape.
2
3use crate::math::{Point, Vector};
4use crate::shape::SupportMap;
5use na::{self, RealField};
6
7/// SupportMap description of a cylinder shape with its principal axis aligned with the `y` axis.
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9#[derive(PartialEq, Debug, Copy, Clone)]
10pub struct Cylinder<N> {
11    /// The half-height of the cylinder.
12    pub half_height: N,
13    /// The radius fo the cylinder.
14    pub radius: N,
15}
16
17impl<N: RealField + Copy> Cylinder<N> {
18    /// Creates a new cylinder.
19    ///
20    /// # Arguments:
21    /// * `half_height` - the half length of the cylinder along the `y` axis.
22    /// * `radius` - the length of the cylinder along all other axis.
23    pub fn new(half_height: N, radius: N) -> Cylinder<N> {
24        assert!(half_height.is_positive() && radius.is_positive());
25
26        Cylinder {
27            half_height,
28            radius,
29        }
30    }
31
32    /// The cylinder half length along the `y` axis.
33    #[inline]
34    #[deprecated(note = "use the `self.half_height` field directly.")]
35    pub fn half_height(&self) -> N {
36        self.half_height
37    }
38
39    /// The radius of the cylinder along all but the `y` axis.
40    #[inline]
41    #[deprecated(note = "use the `self.radius` field directly.")]
42    pub fn radius(&self) -> N {
43        self.radius
44    }
45}
46
47impl<N: RealField + Copy> SupportMap<N> for Cylinder<N> {
48    fn local_support_point(&self, dir: &Vector<N>) -> Point<N> {
49        let mut vres = *dir;
50
51        vres[1] = na::zero();
52
53        if vres.normalize_mut().is_zero() {
54            vres = na::zero()
55        } else {
56            vres = vres * self.radius;
57        }
58
59        vres[1] = self.half_height.copysign(dir[1]);
60
61        Point::from(vres)
62    }
63}