1use crate::point::Point;
11use crate::shape::Shape;
12use nalgebra::SVector;
13
14#[derive(Clone, Copy, Debug)]
23pub struct Capsule<const D: usize> {
24 pub half_height: f64,
26 pub radius: f64,
28 pub axis: usize,
30}
31
32impl<const D: usize> Capsule<D> {
33 pub fn new(half_height: f64, radius: f64, axis: usize) -> Self {
38 debug_assert!(axis < D, "axis {axis} out of range for D={D}");
39 Self {
40 half_height,
41 radius,
42 axis,
43 }
44 }
45
46 pub fn y_aligned(half_height: f64, radius: f64) -> Self {
48 assert!(D >= 2, "Y-axis requires D >= 2");
49 Self::new(half_height, radius, 1)
50 }
51
52 pub fn total_length(&self) -> f64 {
54 2.0 * self.half_height + 2.0 * self.radius
55 }
56}
57
58impl<const D: usize> Shape<D> for Capsule<D> {
59 fn support(&self, direction: &SVector<f64, D>) -> SVector<f64, D> {
64 let norm = direction.norm();
65 if norm < 1e-15 {
66 let mut result = SVector::zeros();
68 result[self.axis] = self.half_height;
69 return result;
70 }
71
72 let mut center = SVector::zeros();
74 center[self.axis] = if direction[self.axis] >= 0.0 {
75 self.half_height
76 } else {
77 -self.half_height
78 };
79
80 center + direction * (self.radius / norm)
82 }
83
84 fn bounding_sphere(&self) -> (Point<D>, f64) {
85 (Point::origin(), self.half_height + self.radius)
86 }
87
88 fn as_any(&self) -> &dyn std::any::Any { self }
89}
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94
95 #[test]
96 fn support_along_axis() {
97 let cap = Capsule::<3>::y_aligned(2.0, 0.5);
98 let dir = SVector::from([0.0, 1.0, 0.0]);
99 let sp = cap.support(&dir);
100 assert!((sp[1] - 2.5).abs() < 1e-12, "support Y+ = {}", sp[1]);
102 }
103
104 #[test]
105 fn support_negative_axis() {
106 let cap = Capsule::<3>::y_aligned(2.0, 0.5);
107 let dir = SVector::from([0.0, -1.0, 0.0]);
108 let sp = cap.support(&dir);
109 assert!((sp[1] - (-2.5)).abs() < 1e-12, "support Y- = {}", sp[1]);
110 }
111
112 #[test]
113 fn support_perpendicular() {
114 let cap = Capsule::<3>::y_aligned(2.0, 0.5);
115 let dir = SVector::from([1.0, 0.0, 0.0]);
116 let sp = cap.support(&dir);
117 assert!((sp[0] - 0.5).abs() < 1e-12, "support X = {}", sp[0]);
119 }
120
121 #[test]
122 fn support_diagonal() {
123 let cap = Capsule::<3>::y_aligned(2.0, 0.5);
124 let dir = SVector::from([1.0, 1.0, 0.0]);
125 let sp = cap.support(&dir);
126 let expected_y = 2.0 + 0.5 / 2.0f64.sqrt();
128 let expected_x = 0.5 / 2.0f64.sqrt();
129 assert!((sp[1] - expected_y).abs() < 1e-10, "diag Y = {}", sp[1]);
130 assert!((sp[0] - expected_x).abs() < 1e-10, "diag X = {}", sp[0]);
131 }
132
133 #[test]
134 fn bounding_sphere_contains_capsule() {
135 let cap = Capsule::<3>::y_aligned(3.0, 1.0);
136 let (center, radius) = cap.bounding_sphere();
137 assert!((center.coord(0)).abs() < 1e-12);
138 assert!((radius - 4.0).abs() < 1e-12);
139 }
140
141 #[test]
142 fn capsule_x_aligned() {
143 let cap = Capsule::<3>::new(1.5, 0.3, 0);
144 let dir = SVector::from([1.0, 0.0, 0.0]);
145 let sp = cap.support(&dir);
146 assert!((sp[0] - 1.8).abs() < 1e-12, "X-capsule support = {}", sp[0]);
147 }
148
149 #[test]
150 fn capsule_4d() {
151 let cap = Capsule::<4>::new(2.0, 1.0, 3); let dir = SVector::from([0.0, 0.0, 0.0, 1.0]);
153 let sp = cap.support(&dir);
154 assert!((sp[3] - 3.0).abs() < 1e-12, "4D capsule W+ = {}", sp[3]);
155 }
156
157 #[test]
158 fn capsule_2d() {
159 let cap = Capsule::<2>::new(1.0, 0.5, 0); let dir = SVector::from([0.0, 1.0]);
161 let sp = cap.support(&dir);
162 assert!((sp[1] - 0.5).abs() < 1e-12, "2D capsule Y = {}", sp[1]);
164 }
165
166 #[test]
167 fn degenerate_direction() {
168 let cap = Capsule::<3>::y_aligned(2.0, 0.5);
169 let dir = SVector::from([0.0, 0.0, 0.0]);
170 let sp = cap.support(&dir);
171 assert!((sp[1] - 2.0).abs() < 1e-12);
173 }
174
175 #[test]
176 fn total_length() {
177 let cap = Capsule::<3>::y_aligned(2.0, 0.5);
178 assert!((cap.total_length() - 5.0).abs() < 1e-12);
179 }
180}