use crate::math::{Pose, Real, Rotation, Vector};
use crate::shape::{Segment, SupportMap};
#[cfg(feature = "alloc")]
use either::Either;
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
#[cfg_attr(feature = "encase", derive(encase::ShaderType))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)
)]
#[repr(C)]
pub struct Capsule {
pub segment: Segment,
pub radius: Real,
}
impl Capsule {
pub fn new_x(half_height: Real, radius: Real) -> Self {
let b = Vector::X * half_height;
Self::new(-b, b, radius)
}
pub fn new_y(half_height: Real, radius: Real) -> Self {
let b = Vector::Y * half_height;
Self::new(-b, b, radius)
}
#[cfg(feature = "dim3")]
pub fn new_z(half_height: Real, radius: Real) -> Self {
let b = Vector::Z * half_height;
Self::new(-b, b, radius)
}
pub fn new(a: Vector, b: Vector, radius: Real) -> Self {
let segment = Segment::new(a, b);
Self { segment, radius }
}
pub fn height(&self) -> Real {
(self.segment.b - self.segment.a).length()
}
pub fn half_height(&self) -> Real {
self.height() / 2.0
}
pub fn center(&self) -> Vector {
self.segment.a.midpoint(self.segment.b)
}
pub fn transform_by(&self, pos: &Pose) -> Self {
Self::new(pos * self.segment.a, pos * self.segment.b, self.radius)
}
pub fn canonical_transform(&self) -> Pose {
let tra = self.center();
let rot = self.rotation_wrt_y();
Pose::from_parts(tra, rot)
}
pub fn rotation_wrt_y(&self) -> Rotation {
let mut dir = self.segment.b - self.segment.a;
if dir.y < 0.0 {
dir = -dir;
}
let dir = dir.normalize_or(Vector::Y);
Rotation::from_rotation_arc(Vector::Y, dir)
}
pub fn transform_wrt_y(&self) -> Pose {
let rot = self.rotation_wrt_y();
Pose::from_parts(self.center(), rot)
}
#[cfg(all(feature = "dim2", feature = "alloc"))]
pub fn scaled(
self,
scale: Vector,
nsubdivs: u32,
) -> Option<Either<Self, super::ConvexPolygon>> {
if scale.x != scale.y {
let mut vtx = self.to_polyline(nsubdivs);
vtx.iter_mut().for_each(|pt| *pt *= scale);
Some(Either::Right(super::ConvexPolygon::from_convex_polyline(
vtx,
)?))
} else {
let uniform_scale = scale.x;
Some(Either::Left(Self::new(
self.segment.a * uniform_scale,
self.segment.b * uniform_scale,
self.radius * uniform_scale.abs(),
)))
}
}
#[cfg(all(feature = "dim3", feature = "alloc"))]
pub fn scaled(
self,
scale: Vector,
nsubdivs: u32,
) -> Option<Either<Self, super::ConvexPolyhedron>> {
if scale.x != scale.y || scale.x != scale.z || scale.y != scale.z {
let (mut vtx, idx) = self.to_trimesh(nsubdivs, nsubdivs);
vtx.iter_mut().for_each(|pt| *pt *= scale);
Some(Either::Right(super::ConvexPolyhedron::from_convex_mesh(
vtx, &idx,
)?))
} else {
let uniform_scale = scale.x;
Some(Either::Left(Self::new(
self.segment.a * uniform_scale,
self.segment.b * uniform_scale,
self.radius * uniform_scale.abs(),
)))
}
}
}
impl SupportMap for Capsule {
fn local_support_point(&self, dir: Vector) -> Vector {
let dir = dir.normalize_or(Vector::Y);
self.local_support_point_toward(dir)
}
fn local_support_point_toward(&self, dir: Vector) -> Vector {
if dir.dot(self.segment.a) > dir.dot(self.segment.b) {
self.segment.a + dir * self.radius
} else {
self.segment.b + dir * self.radius
}
}
}