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
103
104
105
106
107
use crate::math::{Isometry, Point, Real, Rotation, Vector};
use crate::shape::{Segment, SupportMap};
use na::Unit;
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct Capsule {
pub segment: Segment,
pub radius: Real,
}
impl Capsule {
pub fn new_x(half_height: Real, radius: Real) -> Self {
let b = Point::from(Vector::x() * half_height);
Self::new(-b, b, radius)
}
pub fn new_y(half_height: Real, radius: Real) -> Self {
let b = Point::from(Vector::y() * half_height);
Self::new(-b, b, radius)
}
#[cfg(feature = "dim3")]
pub fn new_z(half_height: Real, radius: Real) -> Self {
let b = Point::from(Vector::z() * half_height);
Self::new(-b, b, radius)
}
pub fn new(a: Point<Real>, b: Point<Real>, radius: Real) -> Self {
let segment = Segment::new(a, b);
Self { segment, radius }
}
pub fn height(&self) -> Real {
(self.segment.b - self.segment.a).norm()
}
pub fn half_height(&self) -> Real {
self.height() / 2.0
}
pub fn center(&self) -> Point<Real> {
na::center(&self.segment.a, &self.segment.b)
}
pub fn transform_by(&self, pos: &Isometry<Real>) -> Self {
Self::new(pos * self.segment.a, pos * self.segment.b, self.radius)
}
pub fn canonical_transform(&self) -> Isometry<Real> {
let tra = self.center().coords;
let rot = self.rotation_wrt_y();
Isometry::from_parts(tra.into(), rot)
}
pub fn rotation_wrt_y(&self) -> Rotation<Real> {
let mut dir = self.segment.b - self.segment.a;
if dir.y < 0.0 {
dir = -dir;
}
#[cfg(feature = "dim2")]
{
Rotation::rotation_between(&Vector::y(), &dir)
}
#[cfg(feature = "dim3")]
{
Rotation::rotation_between(&Vector::y(), &dir).unwrap_or(Rotation::identity())
}
}
pub fn transform_wrt_y(&self) -> Isometry<Real> {
let rot = self.rotation_wrt_y();
Isometry::from_parts(self.center().coords.into(), rot)
}
}
impl SupportMap for Capsule {
fn local_support_point(&self, dir: &Vector<Real>) -> Point<Real> {
let dir = Unit::try_new(*dir, 0.0).unwrap_or(Vector::y_axis());
self.local_support_point_toward(&dir)
}
fn local_support_point_toward(&self, dir: &Unit<Vector<Real>>) -> Point<Real> {
if dir.dot(&self.segment.a.coords) > dir.dot(&self.segment.b.coords) {
self.segment.a + **dir * self.radius
} else {
self.segment.b + **dir * self.radius
}
}
}