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
152
153
154
155
156
157
158
159
160
161
162
use na::{Point2, Real, Vector3};
use query::{ray_internal, Ray, RayCast, RayIntersection};
use shape::TriMesh;
use bounding_volume::AABB;
use partitioning::BVTCostFn;
use math::Isometry;
impl<N: Real> RayCast<N> for TriMesh<N> {
#[inline]
fn toi_with_ray(&self, m: &Isometry<N>, ray: &Ray<N>, _: bool) -> Option<N> {
let ls_ray = ray.inverse_transform_by(m);
let mut cost_fn = TriMeshRayToiCostFn {
mesh: self,
ray: &ls_ray,
};
self.bvt()
.best_first_search(&mut cost_fn)
.map(|(_, res)| res)
}
#[inline]
fn toi_and_normal_with_ray(
&self,
m: &Isometry<N>,
ray: &Ray<N>,
_: bool,
) -> Option<RayIntersection<N>> {
let ls_ray = ray.inverse_transform_by(m);
let mut cost_fn = TriMeshRayToiAndNormalCostFn {
mesh: self,
ray: &ls_ray,
};
self.bvt()
.best_first_search(&mut cost_fn)
.map(|(_, mut res)| {
res.normal = m * res.normal;
res
})
}
fn toi_and_normal_and_uv_with_ray(
&self,
m: &Isometry<N>,
ray: &Ray<N>,
solid: bool,
) -> Option<RayIntersection<N>> {
if self.uvs().is_none() {
return self.toi_and_normal_with_ray(m, ray, solid);
}
let ls_ray = ray.inverse_transform_by(m);
let mut cost_fn = TriMeshRayToiAndNormalAndUVsCostFn {
mesh: self,
ray: &ls_ray,
};
let cast = self.bvt().best_first_search(&mut cost_fn);
match cast {
None => None,
Some((best, inter)) => {
let toi = inter.0.toi;
let n = inter.0.normal;
let uv = inter.1;
let idx = &self.indices()[*best];
let uvs = self.uvs().as_ref().unwrap();
let uv1 = uvs[idx[0]];
let uv2 = uvs[idx[1]];
let uv3 = uvs[idx[2]];
let uvx = uv1.x * uv.x + uv2.x * uv.y + uv3.x * uv.z;
let uvy = uv1.y * uv.x + uv2.y * uv.y + uv3.y * uv.z;
Some(RayIntersection::new_with_uvs(
toi,
m * n,
Some(Point2::new(uvx, uvy)),
))
}
}
}
}
struct TriMeshRayToiCostFn<'a, N: 'a + Real> {
mesh: &'a TriMesh<N>,
ray: &'a Ray<N>,
}
impl<'a, N: Real> BVTCostFn<N, usize, AABB<N>> for TriMeshRayToiCostFn<'a, N> {
type UserData = N;
#[inline]
fn compute_bv_cost(&mut self, aabb: &AABB<N>) -> Option<N> {
aabb.toi_with_ray(&Isometry::identity(), self.ray, true)
}
#[inline]
fn compute_b_cost(&mut self, b: &usize) -> Option<(N, N)> {
self.mesh
.triangle_at(*b)
.toi_with_ray(&Isometry::identity(), self.ray, true)
.map(|toi| (toi, toi))
}
}
struct TriMeshRayToiAndNormalCostFn<'a, N: 'a + Real> {
mesh: &'a TriMesh<N>,
ray: &'a Ray<N>,
}
impl<'a, N: Real> BVTCostFn<N, usize, AABB<N>> for TriMeshRayToiAndNormalCostFn<'a, N> {
type UserData = RayIntersection<N>;
#[inline]
fn compute_bv_cost(&mut self, aabb: &AABB<N>) -> Option<N> {
aabb.toi_with_ray(&Isometry::identity(), self.ray, true)
}
#[inline]
fn compute_b_cost(&mut self, b: &usize) -> Option<(N, RayIntersection<N>)> {
self.mesh
.triangle_at(*b)
.toi_and_normal_with_ray(&Isometry::identity(), self.ray, true)
.map(|inter| (inter.toi, inter))
}
}
struct TriMeshRayToiAndNormalAndUVsCostFn<'a, N: 'a + Real> {
mesh: &'a TriMesh<N>,
ray: &'a Ray<N>,
}
impl<'a, N: Real> BVTCostFn<N, usize, AABB<N>> for TriMeshRayToiAndNormalAndUVsCostFn<'a, N> {
type UserData = (RayIntersection<N>, Vector3<N>);
#[inline]
fn compute_bv_cost(&mut self, aabb: &AABB<N>) -> Option<N> {
aabb.toi_with_ray(&Isometry::identity(), self.ray, true)
}
#[inline]
fn compute_b_cost(&mut self, b: &usize) -> Option<(N, (RayIntersection<N>, Vector3<N>))> {
let vs = &self.mesh.vertices()[..];
let idx = &self.mesh.indices()[*b];
let a = &vs[idx[0]];
let b = &vs[idx[1]];
let c = &vs[idx[2]];
ray_internal::triangle_ray_intersection(a, b, c, self.ray).map(|inter| (inter.0.toi, inter))
}
}