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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
//! Support mapping based Cone shape.
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; // for .copysign()
#[cfg(feature = "rkyv")]
use rkyv::{bytecheck, CheckBytes};
/// Cone shape with its principal axis aligned with the `y` axis.
#[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")
)]
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
#[derive(PartialEq, Debug, Copy, Clone)]
#[repr(C)]
pub struct Cone {
/// The half-height of the cone.
pub half_height: Real,
/// The base radius of the cone.
pub radius: Real,
}
impl Cone {
/// Creates a new cone.
///
/// # Arguments:
/// * `half_height` - the half length of the cone along the `y` axis.
/// * `radius` - the length of the cone along all other axis.
pub fn new(half_height: Real, radius: Real) -> Cone {
Cone {
half_height,
radius,
}
}
/// Computes a scaled version of this cone.
///
/// If the scaling factor is non-uniform, then it can’t be represented as
/// cone. Instead, a convex polyhedral approximation (with `nsubdivs`
/// subdivisions) is returned. Returns `None` if that approximation had degenerate
/// normals (for example if the scaling factor along one axis is zero).
#[cfg(feature = "std")]
#[inline]
pub fn scaled(
self,
scale: &Vector<Real>,
nsubdivs: u32,
) -> Option<Either<Self, super::ConvexPolyhedron>> {
// NOTE: if the y scale is negative, the result cone points downwards,
// which can’t be represented with this Cone (without a transform).
if scale.x != scale.z || scale.y < 0.0 {
// The scaled shape isn’t a cone.
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 Cone {
#[inline]
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();
vres[1] = self.half_height.copysign(dir[1]);
} else {
vres *= self.radius;
vres[1] = -self.half_height;
if dir.dot(&vres) < dir[1] * self.half_height {
vres = na::zero();
vres[1] = self.half_height
}
}
Point::from(vres)
}
}