use crate::math::{Point, Real, Vector};
use crate::shape::SupportMap;
use na;
use num::Zero;
#[cfg(feature = "std")]
use either::Either;
#[cfg(not(feature = "std"))]
use na::RealField; #[cfg(feature = "rkyv")]
use rkyv::{bytecheck, CheckBytes};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize, CheckBytes),
archive(as = "Self")
)]
#[derive(PartialEq, Debug, Copy, Clone)]
#[repr(C)]
pub struct Cylinder {
pub half_height: Real,
pub radius: Real,
}
impl Cylinder {
pub fn new(half_height: Real, radius: Real) -> Cylinder {
assert!(half_height.is_sign_positive() && radius.is_sign_positive());
Cylinder {
half_height,
radius,
}
}
#[cfg(feature = "std")]
#[inline]
pub fn scaled(
self,
scale: &Vector<Real>,
nsubdivs: u32,
) -> Option<Either<Self, super::ConvexPolyhedron>> {
if scale.x != scale.z {
let (mut vtx, idx) = self.to_trimesh(nsubdivs);
vtx.iter_mut()
.for_each(|pt| pt.coords = pt.coords.component_mul(scale));
Some(Either::Right(super::ConvexPolyhedron::from_convex_mesh(
vtx, &idx,
)?))
} else {
Some(Either::Left(Self::new(
self.half_height * scale.y,
self.radius * scale.x,
)))
}
}
}
impl SupportMap for Cylinder {
fn local_support_point(&self, dir: &Vector<Real>) -> Point<Real> {
let mut vres = *dir;
vres[1] = 0.0;
if vres.normalize_mut().is_zero() {
vres = na::zero()
} else {
vres *= self.radius;
}
vres[1] = self.half_height.copysign(dir[1]);
Point::from(vres)
}
}