-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
make_cylinder_mesh.cc
434 lines (399 loc) · 19.7 KB
/
make_cylinder_mesh.cc
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include "drake/geometry/proximity/make_cylinder_mesh.h"
#include <cmath>
#include "drake/common/default_scalars.h"
#include "drake/geometry/proximity/meshing_utilities.h"
#include "drake/geometry/proximity/proximity_utilities.h"
namespace drake {
namespace geometry {
namespace internal {
namespace {
/* Generates tetrahedral elements of a long cylinder conforming to its medial
axis. It assumes the mesh vertices on the surface of the cylinder was already
generated on the bottom and the top circular rims of the cylinder together
with the centers of the two circles. After adding two medial vertices inside
the cylinder, it connects mesh vertices to form tetrahedral elements.
@param[in] bottom_center Index into mesh_vertices of the center of the
bottom circular rim of the cylinder.
@param[in] bottom A sequence of indices into mesh_vertices of the
points on the bottom circular rim of the cylinder
in the counterclockwise order around +Z axis of
rotation of the cylinder. The first index can be
anywhere on the circular rim.
@param[in] top_center Index into mesh_vertices of the center of the top
circular rim of the cylinder.
@param[in] top A sequence of indices into mesh_vertices of the
points on the top circular rim of the cylinder in
the same order as `bottom`.
@param[in,out] mesh_vertices As input, it contain vertices on the surface of
the cylinder. The two medial vertices of the
cylinder will be added to mesh_vertices as
output.
@return The generated tetrahedral elements. There is no guarantee how the
elements are ordered.
*/
template <typename T>
std::vector<VolumeElement> CalcLongCylinderVolumeMeshWithMa(
const Cylinder& cylinder, const int num_vertices_per_circle,
const int bottom_center, const std::vector<int>& bottom,
const int top_center, const std::vector<int>& top,
std::vector<Vector3<T>>* mesh_vertices) {
const double top_z = cylinder.length() / 2.;
const double tolerance =
DistanceToPointRelativeTolerance(std::min(top_z, cylinder.radius()));
DRAKE_DEMAND(top_z - cylinder.radius() > tolerance);
DRAKE_DEMAND(num_vertices_per_circle == static_cast<int>(bottom.size()));
DRAKE_DEMAND(num_vertices_per_circle == static_cast<int>(top.size()));
// These two mesh vertices will be at the two end points of the central
// medial line segment.
int medial[2];
const double offset_distance = cylinder.radius();
const double offset_top_z = top_z - offset_distance;
const double offset_bottom_z = -offset_top_z;
medial[0] = static_cast<int>(mesh_vertices->size());
mesh_vertices->emplace_back(0, 0, offset_bottom_z);
medial[1] = static_cast<int>(mesh_vertices->size());
mesh_vertices->emplace_back(0, 0, offset_top_z);
std::vector<VolumeElement> mesh_elements;
// n = num_vertices_per_circle.
// bottom cone: n tetrahedra.
// top cone: n tetrahedra.
// remaining volume: n (topological) triangular prisms => 3n tetrahedra.
mesh_elements.reserve(5 * num_vertices_per_circle);
// +Z (axis of rotation)
// ↑
// | ●t2
// | /
// |/
// t3●--------●tc------●t1
// /| t = top
// / | tc = top_center
// ●t0| m = medial vertices
// |
// ●m1
// |
// |
// +--------------------+Y
// /|
// / |
// / ●m0
// / |
// / | ●b2 b = bottom
// +X | / bc = bottom_center
// |/
// b3●--------●bc------●b1
// /
// /
// ●b0
// Add tetrahedra as we go around the circle.
// i = (n-1) 0 1 2 ... (n-2)
// j = 0 1 2 3 ... (n-1)
int i = num_vertices_per_circle - 1;
for (int j = 0; j < num_vertices_per_circle; ++j) {
// The bottom cone (covering b0,b1,b2,b3,m0 in the picture above) is
// discretized by a series of tetrahedra around the axis of rotation (at
// bc,m0 in the picture above).
mesh_elements.emplace_back(bottom_center, bottom[i], bottom[j], medial[0]);
// The top cone (covering t0,t1,t2,t3,m0 in the picture above) is
// discretized by a series of tetrahedra around the axis of rotation (at
// tc,m1 in the picture above).
mesh_elements.emplace_back(top_center, top[j], top[i], medial[1]);
// The remaining volume is discretized by a series of topological
// triangular prisms (e.g., topological prism m0,b0,b1,m1,t0,t1 in the
// picture above) around the axis of rotation (at m0,m1 in the picture
// above).
Append(SplitTriangularPrismToTetrahedra(medial[0], bottom[i], bottom[j],
medial[1], top[i], top[j]),
&mesh_elements);
i = j;
}
return mesh_elements;
}
/* Generates tetrahedral elements of a medium cylinder conforming to its medial
axis. It assumes the mesh vertices on the surface of the cylinder were already
generated on the bottom and the top circular rims of the cylinder together
with the centers of the two circles. After adding the medial vertex at the
center of the cylinder, it connects mesh vertices to form tetrahedral elements.
@param[in] bottom_center Index into mesh_vertices of the center of the
bottom circular rim of the cylinder.
@param[in] bottom A sequence of indices into mesh_vertices of the
points on the bottom circular rim of the cylinder
in the counterclockwise order around +Z axis of
rotation of the cylinder. The first index can be
anywhere on the circular rim.
@param[in] top_center Index into mesh_vertices of the center of the top
circular rim of the cylinder.
@param[in] top A sequence of indices into mesh_vertices of the
points on the top circular rim of the cylinder in
the same order as `bottom`.
@param[in,out] mesh_vertices As input, it contains vertices on the surface of
the cylinder. The medial vertex of the
cylinder will be added to mesh_vertices as
output.
@return The generated tetrahedral elements. There is no guarantee how the
elements are ordered.
*/
template <typename T>
std::vector<VolumeElement> CalcMediumCylinderVolumeMeshWithMa(
const Cylinder& cylinder, const int num_vertices_per_circle,
const int bottom_center, const std::vector<int>& bottom,
const int top_center, const std::vector<int>& top,
std::vector<Vector3<T>>* mesh_vertices) {
const double top_z = cylinder.length() / 2.;
const double tolerance =
DistanceToPointRelativeTolerance(std::min(top_z, cylinder.radius()));
DRAKE_DEMAND(std::abs(top_z - cylinder.radius()) <= tolerance);
DRAKE_DEMAND(num_vertices_per_circle == static_cast<int>(bottom.size()));
DRAKE_DEMAND(num_vertices_per_circle == static_cast<int>(top.size()));
// The central medial vertex is at the center of the medium cylinder.
int medial = static_cast<int>(mesh_vertices->size());
mesh_vertices->emplace_back(0, 0, 0);
std::vector<VolumeElement> mesh_elements;
// n = num_vertices_per_circle.
// bottom cone: n tetrahedra.
// top cone: n tetrahedra.
// remaining volume: ring of n pyramids => 2n tetrahedra.
mesh_elements.reserve(4 * num_vertices_per_circle);
//
// +Z (axis of rotation)
// ↑
// | ●t2
// | /
// |/
// t3●--------●tc------●t1
// /| t = top
// / | tc = top_center
// ●t0| m = medial vertex at origin.
// |
// ●m---------------+Y
// /|
// / | ●b2 b = bottom
// / | / bc = bottom_center
// +X |/
// b3●--------●bc------●b1
// /
// /
// ●b0
// Add tetrahedra as we go around the circle.
// i = (n-1) 0 1 2 ... (n-2)
// j = 0 1 2 3 ... (n-1)
int i = num_vertices_per_circle - 1;
for (int j = 0; j < num_vertices_per_circle; ++j) {
// The bottom cone (covering b0,b1,b2,b3,m in the picture above) is
// discretized by a series of tetrahedra (e.g., tetrahedron bc,b0,b1,m in
// the picture) around the axis of rotation (at bc,m in the picture).
mesh_elements.emplace_back(bottom_center, bottom[i], bottom[j], medial);
// The top cone (covering t0,t1,t2,t3,m in the picture above) is
// discretized by a series of tetrahedra (e.g., tetrahedron tc,t1,t0,m in
// the picture) around the axis of rotation (at tc,m in the picture).
mesh_elements.emplace_back(top_center, top[j], top[i], medial);
// The remaining volume is discretized by a series of rectangular
// pyramids (e.g., pyramid t0,t1,b1,b0,m in the picture above) around the
// axis of rotation. These pyramids share the apex at the medial vertex
// (m in the picture).
Append(
SplitPyramidToTetrahedra(top[i], top[j], bottom[j], bottom[i], medial),
&mesh_elements);
i = j;
}
return mesh_elements;
}
/* Generates tetrahedral elements of a short cylinder conforming to its medial
axis. It assumes the mesh vertices on the surface of the cylinder was already
generated on the bottom and the top circular rims of the cylinder together
with the centers of the two circles. After adding vertices on the medial
circular disk of the cylinder, it connects mesh vertices to form
tetrahedral elements.
@param[in] bottom_center Index into mesh_vertices of the center of the
bottom circular rim of the cylinder.
@param[in] bottom A sequence of indices into mesh_vertices of the
points on the bottom circular rim of the cylinder
in the counterclockwise order around +Z axis of
rotation of the cylinder. The first index can be
anywhere on the circular rim.
@param[in] top_center Index into mesh_vertices of the center of the top
circular rim of the cylinder.
@param[in] top A sequence of indices into mesh_vertices of the
points on the top circular rim of the cylinder in
the same order as `bottom`.
@param[in,out] mesh_vertices As input, it contains vertices on the surface of
the cylinder. The vertices on the medial
circular disk of the cylinder will be added to
mesh_vertices as output.
@return The generated tetrahedral elements. There is no guarantee how the
elements are ordered.
*/
template <typename T>
std::vector<VolumeElement> CalcShortCylinderVolumeMeshWithMa(
const Cylinder& cylinder, const int num_vertices_per_circle,
const int bottom_center, const std::vector<int>& bottom,
const int top_center, const std::vector<int>& top,
std::vector<Vector3<T>>* mesh_vertices) {
const double top_z = cylinder.length() / 2.;
const double tolerance =
DistanceToPointRelativeTolerance(std::min(top_z, cylinder.radius()));
DRAKE_DEMAND(cylinder.radius() - top_z > tolerance);
DRAKE_DEMAND(num_vertices_per_circle == static_cast<int>(bottom.size()));
DRAKE_DEMAND(num_vertices_per_circle == static_cast<int>(top.size()));
// For convenience in generating the mesh, we add one vertex at the center
// of the cylinder, which is also the center of the medial circular disk.
// This vertex helps us make the mesh symmetric around the rotation axis of
// the cylinder. It is possible to skip this center point, and we
// will decrease the number of vertices and tetrahedra slightly, but the
// mesh will not be symmetric around the rotation axis, and the code will
// become more complicated.
int center = static_cast<int>(mesh_vertices->size());
mesh_vertices->emplace_back(0, 0, 0);
// Vertices along the medial circle.
std::vector<int> medial(num_vertices_per_circle);
const double medial_radius = cylinder.radius() - cylinder.length() / 2.;
const double scale_cylinder_radius_to_medial_circle =
medial_radius / cylinder.radius();
for (int i = 0; i < num_vertices_per_circle; ++i) {
const double x =
ExtractDoubleOrThrow(mesh_vertices->at(bottom[i]).x()) *
scale_cylinder_radius_to_medial_circle;
const double y =
ExtractDoubleOrThrow(mesh_vertices->at(bottom[i]).y()) *
scale_cylinder_radius_to_medial_circle;
medial[i] = static_cast<int>(mesh_vertices->size());
mesh_vertices->emplace_back(x, y, 0);
}
std::vector<VolumeElement> mesh_elements;
// n = num_vertices_per_circle.
// bottom frustum: n triangular prisms => 3n tetrahedra.
// top frustum: n triangular prisms => 3n tetrahedra.
// remaining volume: ring of n triangular prisms => 3n tetrahedra.
mesh_elements.reserve(9 * num_vertices_per_circle);
// +Z (axis of rotation)
// ↑
// | ●t2
// | ↗
// | ↗ t = top
// | ↗ tc = top_center
// t3● ← ← ← ← ●tc→ → → → ●t1 m = vertices on medial circle
// ↙ |
// ↙ | +Z
// ↙ | ●m2 ↑
// t0● | ↗ + → +Y
// m3● ← ●c → ●m1 ↙
// ↙ | ●b2 +X c = `center` is at the origin.
// ●m0 | ↗
// | ↗ b = bottom
// | ↗ bc = bottom_center
// b3● ← ← ← ← ●bc→ → → → ●b1
// ↙
// ↙
// ↙
// b0●
//
// Add tetrahedra as we go around the circle.
// i = (n-1) 0 1 2 ... (n-2)
// j = 0 1 2 3 ... (n-1)
int i = num_vertices_per_circle - 1;
for (int j = 0; j < num_vertices_per_circle; ++j) {
// The bottom circular frustum (covering b0,b1,b2,b3,m0,m1,m2,m3 in the
// picture above) is discretized by a series of topological triangular
// prisms around the axis of rotation (at c,bc in the picture).
Append(SplitTriangularPrismToTetrahedra(bottom_center, bottom[i], bottom[j],
center, medial[i], medial[j]),
&mesh_elements);
// The top circular frustum (covering t0,t1,t2,t3,m0,m1,m2,m3 in the
// picture above) is discretized by a series of topological triangular
// prisms around the axis of rotation (at c,tc in the picture).
Append(SplitTriangularPrismToTetrahedra(center, medial[i], medial[j],
top_center, top[i], top[j]),
&mesh_elements);
// The remaining volume of the cylinder is discretized by a series of
// topological triangular prisms with their triangular faces (b_i,m_i,t_i,
// i=0,1,2,3 in the picture above) circling around the axis of rotation.
Append(SplitTriangularPrismToTetrahedra(bottom[i], medial[i], top[i],
bottom[j], medial[j], top[j]),
&mesh_elements);
i = j;
}
return mesh_elements;
}
} // namespace
template <typename T>
VolumeMesh<T> MakeCylinderVolumeMeshWithMa(const Cylinder& cylinder,
const double resolution_hint) {
DRAKE_DEMAND(resolution_hint > 0.0);
const double top_z = cylinder.length() / 2.;
const double bottom_z = -top_z;
enum class CylinderClass { kLong, kMedium, kShort };
// This is just a heuristic to make the numerical decision below.
const double smaller_measure = std::min(top_z, cylinder.radius());
const double tolerance = DistanceToPointRelativeTolerance(smaller_measure);
CylinderClass cylinder_class = CylinderClass::kMedium;
if (top_z - cylinder.radius() > tolerance)
cylinder_class = CylinderClass::kLong;
else if (cylinder.radius() - top_z > tolerance)
cylinder_class = CylinderClass::kShort;
// At the minimum 3 vertices per circular rims of the cylinder, the mesh
// will cover a triangular prism inside the cylinder. The larger value
// of num_vertices_per_circle will make the mesh approximate the cylinder
// better.
const int num_vertices_per_circle = std::max(
3,
static_cast<int>(ceil(2. * M_PI * cylinder.radius() / resolution_hint)));
std::vector<Vector3<T>> mesh_vertices;
switch (cylinder_class) {
case CylinderClass::kLong:
mesh_vertices.reserve(2 * num_vertices_per_circle + 4);
break;
case CylinderClass::kMedium:
mesh_vertices.reserve(2 * num_vertices_per_circle + 3);
break;
case CylinderClass::kShort:
mesh_vertices.reserve(3 * num_vertices_per_circle + 3);
break;
}
// For convenience in generating the mesh, we always add one vertex at the
// center of each of the bottom and the top circular caps of the cylinder.
// These two vertices help us make the mesh symmetric around the rotation
// axis of the cylinder. It is possible to skip these two points, and we
// will decrease the number of vertices and tetrahedra slightly, but the
// mesh will not be symmetric around the rotation axis, and the code will
// become more complicated.
int bottom_center = static_cast<int>(mesh_vertices.size());
mesh_vertices.emplace_back(0, 0, bottom_z);
int top_center = static_cast<int>(mesh_vertices.size());
mesh_vertices.emplace_back(0, 0, top_z);
// Vertices on the two circular rims of the cylinder.
std::vector<int> bottom(num_vertices_per_circle);
std::vector<int> top(num_vertices_per_circle);
const double angle_step = 2. * M_PI / num_vertices_per_circle;
for (int i = 0; i < num_vertices_per_circle; ++i) {
const double x = cylinder.radius() * cos(angle_step * i);
const double y = cylinder.radius() * sin(angle_step * i);
bottom[i] = static_cast<int>(mesh_vertices.size());
mesh_vertices.emplace_back(x, y, bottom_z);
top[i] = static_cast<int>(mesh_vertices.size());
mesh_vertices.emplace_back(x, y, top_z);
}
std::vector<VolumeElement> mesh_elements;
switch (cylinder_class) {
case CylinderClass::kLong:
mesh_elements = CalcLongCylinderVolumeMeshWithMa<T>(
cylinder, num_vertices_per_circle, bottom_center, bottom, top_center,
top, &mesh_vertices);
break;
case CylinderClass::kMedium:
mesh_elements = CalcMediumCylinderVolumeMeshWithMa<T>(
cylinder, num_vertices_per_circle, bottom_center, bottom, top_center,
top, &mesh_vertices);
break;
case CylinderClass::kShort:
mesh_elements = CalcShortCylinderVolumeMeshWithMa<T>(
cylinder, num_vertices_per_circle, bottom_center, bottom, top_center,
top, &mesh_vertices);
break;
}
return {std::move(mesh_elements), std::move(mesh_vertices)};
}
DRAKE_DEFINE_FUNCTION_TEMPLATE_INSTANTIATIONS_ON_DEFAULT_NONSYMBOLIC_SCALARS((
&MakeCylinderVolumeMeshWithMa<T>
))
} // namespace internal
} // namespace geometry
} // namespace drake