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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use crate::math::{Isometry, Point, Real, Vector};
use crate::shape::FeatureId;
#[derive(Debug, Clone, Copy)]
pub struct Ray {
pub origin: Point<Real>,
pub dir: Vector<Real>,
}
impl Ray {
pub fn new(origin: Point<Real>, dir: Vector<Real>) -> Ray {
Ray { origin, dir }
}
#[inline]
pub fn transform_by(&self, m: &Isometry<Real>) -> Self {
Self::new(m * self.origin, m * self.dir)
}
#[inline]
pub fn inverse_transform_by(&self, m: &Isometry<Real>) -> Self {
Self::new(
m.inverse_transform_point(&self.origin),
m.inverse_transform_vector(&self.dir),
)
}
#[inline]
pub fn translate_by(&self, v: Vector<Real>) -> Self {
Self::new(self.origin + v, self.dir)
}
#[inline]
pub fn point_at(&self, t: Real) -> Point<Real> {
self.origin + self.dir * t
}
}
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RayIntersection {
pub toi: Real,
pub normal: Vector<Real>,
pub feature: FeatureId,
}
impl RayIntersection {
#[inline]
#[cfg(feature = "dim3")]
pub fn new(toi: Real, normal: Vector<Real>, feature: FeatureId) -> RayIntersection {
RayIntersection {
toi,
normal,
feature,
}
}
#[inline]
#[cfg(feature = "dim2")]
pub fn new(toi: Real, normal: Vector<Real>, feature: FeatureId) -> RayIntersection {
RayIntersection {
toi,
normal,
feature,
}
}
#[inline]
pub fn transform_by(&self, transform: &Isometry<Real>) -> Self {
RayIntersection {
toi: self.toi,
normal: transform * self.normal,
feature: self.feature,
}
}
}
pub trait RayCast {
fn cast_local_ray(&self, ray: &Ray, max_toi: Real, solid: bool) -> Option<Real> {
self.cast_local_ray_and_get_normal(ray, max_toi, solid)
.map(|inter| inter.toi)
}
fn cast_local_ray_and_get_normal(
&self,
ray: &Ray,
max_toi: Real,
solid: bool,
) -> Option<RayIntersection>;
#[inline]
fn intersects_local_ray(&self, ray: &Ray, max_toi: Real) -> bool {
self.cast_local_ray(ray, max_toi, true).is_some()
}
fn cast_ray(&self, m: &Isometry<Real>, ray: &Ray, max_toi: Real, solid: bool) -> Option<Real> {
let ls_ray = ray.inverse_transform_by(m);
self.cast_local_ray(&ls_ray, max_toi, solid)
}
fn cast_ray_and_get_normal(
&self,
m: &Isometry<Real>,
ray: &Ray,
max_toi: Real,
solid: bool,
) -> Option<RayIntersection> {
let ls_ray = ray.inverse_transform_by(m);
self.cast_local_ray_and_get_normal(&ls_ray, max_toi, solid)
.map(|inter| inter.transform_by(m))
}
#[inline]
fn intersects_ray(&self, m: &Isometry<Real>, ray: &Ray, max_toi: Real) -> bool {
let ls_ray = ray.inverse_transform_by(m);
self.intersects_local_ray(&ls_ray, max_toi)
}
}