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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use crate::bounding_volume::AABB;
use crate::math::{Isometry, Point, Real};
use crate::partitioning::QBVH;
use crate::shape::composite_shape::SimdCompositeShape;
#[cfg(feature = "dim3")]
use crate::shape::{Cuboid, HeightField};
use crate::shape::{FeatureId, Shape, Triangle, TypedSimdCompositeShape};
#[derive(Clone)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct TriMesh {
quadtree: QBVH<u32>,
vertices: Vec<Point<Real>>,
indices: Vec<[u32; 3]>,
}
impl TriMesh {
pub fn new(vertices: Vec<Point<Real>>, indices: Vec<[u32; 3]>) -> Self {
assert!(
indices.len() > 0,
"A triangle mesh must contain at least one triangle."
);
let data = indices.iter().enumerate().map(|(i, idx)| {
let aabb = Triangle::new(
vertices[idx[0] as usize],
vertices[idx[1] as usize],
vertices[idx[2] as usize],
)
.local_aabb();
(i as u32, aabb)
});
let mut quadtree = QBVH::new();
quadtree.clear_and_rebuild(data, 0.0);
Self {
quadtree,
vertices,
indices,
}
}
pub fn aabb(&self, pos: &Isometry<Real>) -> AABB {
self.quadtree.root_aabb().transform_by(pos)
}
pub fn local_aabb(&self) -> &AABB {
self.quadtree.root_aabb()
}
pub fn quadtree(&self) -> &QBVH<u32> {
&self.quadtree
}
pub fn num_triangles(&self) -> usize {
self.indices.len()
}
pub fn is_backface(&self, feature: FeatureId) -> bool {
if let FeatureId::Face(i) = feature {
i >= self.indices.len() as u32
} else {
false
}
}
pub fn triangles(&self) -> impl Iterator<Item = Triangle> + '_ {
self.indices.iter().map(move |ids| {
Triangle::new(
self.vertices[ids[0] as usize],
self.vertices[ids[1] as usize],
self.vertices[ids[2] as usize],
)
})
}
pub fn triangle(&self, i: u32) -> Triangle {
let idx = self.indices[i as usize];
Triangle::new(
self.vertices[idx[0] as usize],
self.vertices[idx[1] as usize],
self.vertices[idx[2] as usize],
)
}
pub fn vertices(&self) -> &[Point<Real>] {
&self.vertices[..]
}
pub fn indices(&self) -> &[[u32; 3]] {
&self.indices
}
pub fn flat_indices(&self) -> &[u32] {
unsafe {
let len = self.indices.len() * 3;
let data = self.indices.as_ptr() as *const u32;
std::slice::from_raw_parts(data, len)
}
}
}
#[cfg(feature = "dim3")]
impl From<HeightField> for TriMesh {
fn from(heightfield: HeightField) -> Self {
let (vtx, idx) = heightfield.to_trimesh();
TriMesh::new(vtx, idx)
}
}
#[cfg(feature = "dim3")]
impl From<Cuboid> for TriMesh {
fn from(cuboid: Cuboid) -> Self {
let (vtx, idx) = cuboid.to_trimesh();
TriMesh::new(vtx, idx)
}
}
impl SimdCompositeShape for TriMesh {
fn map_part_at(&self, i: u32, f: &mut dyn FnMut(Option<&Isometry<Real>>, &dyn Shape)) {
let tri = self.triangle(i);
f(None, &tri)
}
fn quadtree(&self) -> &QBVH<u32> {
&self.quadtree
}
}
impl TypedSimdCompositeShape for TriMesh {
type PartShape = Triangle;
type PartId = u32;
#[inline(always)]
fn map_typed_part_at(
&self,
i: u32,
mut f: impl FnMut(Option<&Isometry<Real>>, &Self::PartShape),
) {
let tri = self.triangle(i);
f(None, &tri)
}
#[inline(always)]
fn map_untyped_part_at(&self, i: u32, mut f: impl FnMut(Option<&Isometry<Real>>, &dyn Shape)) {
let tri = self.triangle(i);
f(None, &tri)
}
fn typed_quadtree(&self) -> &QBVH<u32> {
&self.quadtree
}
}