-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgjk_distance.hpp
562 lines (512 loc) · 20.4 KB
/
gjk_distance.hpp
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//
// Created by wei on 23-1-12.
//
#pragma once
namespace fcl {
namespace cvx_collide {
template <typename T>
bool GJK<T>::findMinimumDistancePointsWithSeparatedVertexInit(
const MinkowskiDiff<T>& shape, GJKSimplex<T>& simplex,
std::pair<Vector3<T>, Vector3<T>>& p0p1_in_frame0) const {
// Contains the witness point
assert(simplex.rank == 1);
if (simplex.rank != 1) {
return false;
}
// Iteration variable except simplex
Vector3<T> current_min_distance_point;
Vector3<T> next_direction;
MinkowskiDiffVertex<T> new_vertex;
T bookkeeping_min_distance;
// Init the current_min_distance_point
auto init_status =
computeMinDistanceAndUpdateSimplex(simplex, current_min_distance_point);
if (init_status != MinDistanceUpdateStatus::OK) return false;
// Init the distance and direction
bookkeeping_min_distance = current_min_distance_point.norm();
if (bookkeeping_min_distance <= tolerance_) {
return extractSeparationPointNoSubSimplex(shape, simplex, p0p1_in_frame0);
}
// The direction is the negative of min_distance_point
// As it is not norm-zero, we can normalize it
next_direction = -current_min_distance_point / bookkeeping_min_distance;
// Start the loop
std::size_t iteration = 0;
const T tolerance_squared = tolerance_ * tolerance_;
while (iteration < max_iterations_) {
// Update the index
iteration += 1;
// Compute the new vertex
new_vertex = shape.supportVertex(next_direction);
// Terminate checking on new vertex
{
// 1: Should not be very close to the original simplex
const T delta_on_direction =
next_direction.dot(new_vertex.vertex - current_min_distance_point);
if (delta_on_direction < tolerance_) {
return extractSeparationPointNoSubSimplex(shape, simplex,
p0p1_in_frame0);
}
// 2: Should not be the same as old ones
for (auto j = 0; j < simplex.n_vertices(); j++) {
const Vector3<T>& vertex_j = simplex.vertices[j].vertex;
if ((vertex_j - new_vertex.vertex).squaredNorm() < tolerance_squared) {
return extractSeparationPointNoSubSimplex(shape, simplex,
p0p1_in_frame0);
}
}
}
// Update the simplex
assert(simplex.rank >= 1 && simplex.rank <= 3);
simplex.AddVertex(new_vertex);
// Compute the min-distance point and update the simplex to match that
auto update_status =
computeMinDistanceAndUpdateSimplex(simplex, current_min_distance_point);
assert(simplex.rank >= 1 && simplex.rank <= 3);
// Check the termination
if (update_status == MinDistanceUpdateStatus::NoImprovement) {
return extractSeparationPointNoSubSimplex(shape, simplex, p0p1_in_frame0);
} else if (update_status == MinDistanceUpdateStatus::OK) {
// Check if the update converges by numerical
const T new_min_distance = current_min_distance_point.norm();
const T distance_improvement =
bookkeeping_min_distance - new_min_distance;
if (distance_improvement < tolerance_) {
return extractSeparationPointNoSubSimplex(shape, simplex,
p0p1_in_frame0);
} else if (new_min_distance < tolerance_) {
return extractSeparationPointNoSubSimplex(shape, simplex,
p0p1_in_frame0);
}
// Continue on the next iteration
bookkeeping_min_distance = new_min_distance;
next_direction = -current_min_distance_point / bookkeeping_min_distance;
} else {
return false;
}
}
// Reach the iteration limit, claim invalid
return false;
}
template <typename T>
typename GJK<T>::MinDistanceUpdateStatus
GJK<T>::computeMinDistanceAndUpdateSimplex(
GJKSimplex<T>& simplex, Vector3<T>& min_distance_output) const {
if (simplex.rank == 1) {
min_distance_output = simplex.vertices[0].vertex;
return MinDistanceUpdateStatus::OK;
} else if (simplex.rank == 2) {
computeMinDistanceAndUpdateSimplex2(simplex, min_distance_output);
return MinDistanceUpdateStatus::OK;
} else if (simplex.rank == 3) {
computeMinDistanceAndUpdateSimplex3(simplex, min_distance_output);
return MinDistanceUpdateStatus::OK;
} else if (simplex.rank == 4) {
return computeMinDistanceAndUpdateSimplex4(simplex, min_distance_output);
} else {
return MinDistanceUpdateStatus::Failed;
}
}
template <typename T>
void GJK<T>::computeMinDistanceAndUpdateSimplex2(
GJKSimplex<T>& simplex, Vector3<T>& min_distance_output) const {
assert(simplex.rank == 2);
// The last one is the new vertex
const Vector3<T>& s1_new = simplex.vertices[1].vertex;
const Vector3<T>& s2 = simplex.vertices[0].vertex;
// Project to s1
const Vector3<T> s1_to_s2 = s2 - s1_new;
const T squared_length = s1_to_s2.squaredNorm();
const T t = -s1_new.dot(s1_to_s2);
if (t <= 0 || squared_length <= tolerance_ * tolerance_) {
// Update to point simplex of s1
min_distance_output = s1_new;
simplex.vertices[0] = simplex.vertices[1];
simplex.rank = 1;
} else if (t >= squared_length) {
// Update to point simplex of s2
min_distance_output = s2;
simplex.rank = 1;
} else {
// To segment, the simplex is the same as before
const T s2_weight = (t / squared_length);
min_distance_output = s2_weight * s2 + (T(1.0) - s2_weight) * s1_new;
}
}
template <typename T>
void GJK<T>::computeMinDistanceAndUpdateSimplex3(
GJKSimplex<T>& simplex, Vector3<T>& min_distance_output) const {
assert(simplex.rank == 3);
// The last one is the new vertex
const Vector3<T>& s1_new = simplex.vertices[2].vertex;
const Vector3<T>& s2 = simplex.vertices[1].vertex;
const Vector3<T>& s3 = simplex.vertices[0].vertex;
// The case of s1
const Vector3<T> s1_to_s2 = s2 - s1_new;
const Vector3<T> s1_to_s3 = s3 - s1_new;
const bool s1_separate_s2_o = s1_new.dot(s1_to_s2) >= 0;
const bool s1_separate_s3_o = s1_new.dot(s1_to_s3) >= 0;
if (s1_separate_s2_o && s1_separate_s3_o) {
// Update to point simplex of s1
min_distance_output = s1_new;
simplex.vertices[0] = simplex.vertices[2];
simplex.rank = 1;
return;
}
// The line separation condition needs normal
const Vector3<T> s1s2s3_plane_normal = s1_to_s2.cross(s1_to_s3);
const T area_squared = s1s2s3_plane_normal.squaredNorm();
const bool is_zero_area = area_squared <= T(0.0);
// We can determine it is segment s1s2
// s1 here is actually o_to_s1 = s1
const Vector3<T> s1s2_normal_in_s123 = s1s2s3_plane_normal.cross(s1_to_s2);
// Old impl:
// const bool s1s2_separate_s3_o = is_sign_matched(
// s1_new.dot(s1s2_normal_in_s123), s1_to_s3.dot(s1s2_normal_in_s123));
const bool s1s2_separate_s3_o = s1_new.dot(s1s2_normal_in_s123) > 0;
if ((!s1_separate_s2_o) && s1s2_separate_s3_o) {
// Keep vertex[2, 1]
simplex.vertices[0] = simplex.vertices[1];
simplex.vertices[1] = simplex.vertices[2];
simplex.rank = 2;
computeMinDistanceAndUpdateSimplex2(simplex, min_distance_output);
return;
}
// We can determine it is segment s1s3
const Vector3<T> s1s3_normal_in_s123 = s1s2s3_plane_normal.cross(s1_to_s3);
// Old impl
// const bool s1s3_separate_s2_o = is_sign_matched(
// s1_new.dot(s1s3_normal_in_s123), s1_to_s2.dot(s1s3_normal_in_s123));
const bool s1s3_separate_s2_o = s1_new.dot(s1s3_normal_in_s123) < 0;
if ((!s1_separate_s3_o) && s1s3_separate_s2_o) {
// Keep vertex[2, 0]
simplex.vertices[1] = simplex.vertices[2];
simplex.rank = 2;
computeMinDistanceAndUpdateSimplex2(simplex, min_distance_output);
return;
}
// Here we need to iterate over all edge
T min_distance_square = -1;
Vector3<T> min_distance_point_in_edges, min_distance_point_cache;
GJKSimplex<T> min_distance_simplex, simplex_cache;
if (is_zero_area || s1s2_separate_s3_o) {
// Keep vertex[2, 1]
simplex_cache = simplex;
simplex_cache.vertices[0] = simplex_cache.vertices[1];
simplex_cache.vertices[1] = simplex_cache.vertices[2];
simplex_cache.rank = 2;
computeMinDistanceAndUpdateSimplex2(simplex_cache,
min_distance_point_cache);
const T min_distance_s12 = min_distance_point_cache.squaredNorm();
if (min_distance_square < 0 || min_distance_s12 < min_distance_square) {
min_distance_square = min_distance_s12;
min_distance_simplex = simplex_cache;
min_distance_point_in_edges = min_distance_point_cache;
}
}
if (is_zero_area || s1s3_separate_s2_o) {
// Keep vertex[2, 0]
simplex_cache = simplex;
simplex_cache.vertices[1] = simplex_cache.vertices[2];
simplex_cache.rank = 2;
computeMinDistanceAndUpdateSimplex2(simplex_cache,
min_distance_point_cache);
const T min_distance_s13 = min_distance_point_cache.squaredNorm();
if (min_distance_square < 0 || min_distance_s13 < min_distance_square) {
min_distance_square = min_distance_s13;
min_distance_simplex = simplex_cache;
min_distance_point_in_edges = min_distance_point_cache;
}
}
// Last case, s23
const Vector3<T> s2s3_normal_in_s123 = s1s2s3_plane_normal.cross(s3 - s2);
// Old impl
// const bool s2s3_separate_s1_o = is_sign_matched(
// - s2.dot(s2s3_normal_in_s123), s1_to_s2.dot(s2s3_normal_in_s123));
const bool s2s3_separate_s1_o = s2.dot(s2s3_normal_in_s123) > 0;
if (is_zero_area || s2s3_separate_s1_o) {
// Keep vertex[1, 0]
simplex_cache = simplex;
simplex_cache.rank = 2;
computeMinDistanceAndUpdateSimplex2(simplex_cache,
min_distance_point_cache);
const T min_distance_s23 = min_distance_point_cache.squaredNorm();
if (min_distance_square < 0 || min_distance_s23 < min_distance_square) {
min_distance_square = min_distance_s23;
min_distance_simplex = simplex_cache;
min_distance_point_in_edges = min_distance_point_cache;
}
}
// Now, do triangle s123 if necessary
// If min_distance_square is updated, then there must be one edge s_ij that
// separates s_k and o. In this situation, o can not be projected to the
// internal of s_ijk (and we do NOT need to check that).
// On the contrary, if min_distance_square is not updated, then o must be
// projected into the internal of s_ijk (s_123).
if (min_distance_square < 0) {
assert(!is_zero_area);
const Vector3<T>& n = s1s2s3_plane_normal;
const T d = s1_new.dot(n);
const Vector3<T> o_to_project = n * (d / area_squared);
min_distance_output = o_to_project;
} else {
// Just use the smallest distance one
// Important: we cannot return NoImprovement as this method is used as a
// subroutine for simplex4.
// If it is not a subroutine, then edge s23 achieves min-distance
// implies no improvement.
simplex = min_distance_simplex;
min_distance_output = min_distance_point_in_edges;
}
}
template <typename T>
typename GJK<T>::MinDistanceUpdateStatus
GJK<T>::computeMinDistanceAndUpdateSimplex4(
GJKSimplex<T>& simplex, Vector3<T>& min_distance_output) const {
assert(simplex.rank == 4);
// The last one is the new vertex
// const Vector3<T>& s1_new = simplex.vertices[3].vertex;
// const Vector3<T>& s2 = simplex.vertices[2].vertex;
// const Vector3<T>& s3 = simplex.vertices[1].vertex;
// const Vector3<T>& s4 = simplex.vertices[0].vertex;
// As we know it is seperated, the min-distance must be one of its triangle
// In particular s123, s124, s134
// The cache output
T min_distance_square = -1;
Vector3<T> min_distance_point_in_faces, min_distance_point_cache;
GJKSimplex<T> min_distance_simplex, simplex_cache;
// s123
{
simplex_cache = simplex;
// Keep vertex[3, 2, 1]
simplex_cache.vertices[0] = simplex_cache.vertices[1];
simplex_cache.vertices[1] = simplex_cache.vertices[2];
simplex_cache.vertices[2] = simplex_cache.vertices[3];
simplex_cache.rank = 3;
// We need to run weight checking as in sub-simplex, the min-distance
// triangle may NOT contain s1
computeMinDistanceAndUpdateSimplex3(simplex_cache,
min_distance_point_cache);
const T min_distance_square_s123 = min_distance_point_cache.squaredNorm();
if (min_distance_square < 0 ||
min_distance_square_s123 < min_distance_square) {
min_distance_square = min_distance_square_s123;
min_distance_point_in_faces = min_distance_point_cache;
min_distance_simplex = simplex_cache;
}
}
// s124
{
simplex_cache = simplex;
// Keep vertex[3, 2, 0]
simplex_cache.vertices[1] = simplex_cache.vertices[2];
simplex_cache.vertices[2] = simplex_cache.vertices[3];
simplex_cache.rank = 3;
computeMinDistanceAndUpdateSimplex3(simplex_cache,
min_distance_point_cache);
const T min_distance_square_s124 = min_distance_point_cache.squaredNorm();
if (min_distance_square < 0 ||
min_distance_square_s124 < min_distance_square) {
min_distance_square = min_distance_square_s124;
min_distance_point_in_faces = min_distance_point_cache;
min_distance_simplex = simplex_cache;
}
}
// s134
{
simplex_cache = simplex;
// Keep vertex[3, 1, 0]
simplex_cache.vertices[2] = simplex_cache.vertices[3];
simplex_cache.rank = 3;
computeMinDistanceAndUpdateSimplex3(simplex_cache,
min_distance_point_cache);
const T min_distance_square_s134 = min_distance_point_cache.squaredNorm();
if (min_distance_square < 0 ||
min_distance_square_s134 < min_distance_square) {
min_distance_square = min_distance_square_s134;
min_distance_point_in_faces = min_distance_point_cache;
min_distance_simplex = simplex_cache;
}
}
// Improvement
if (min_distance_square < 0) {
return MinDistanceUpdateStatus::NoImprovement;
} else {
simplex = min_distance_simplex;
min_distance_output = min_distance_point_in_faces;
return MinDistanceUpdateStatus::OK;
}
}
template <typename T>
bool GJK<T>::extractSeparationPointNoSubSimplex(
const MinkowskiDiff<T>& shape, const GJKSimplex<T>& simplex,
std::pair<Vector3<T>, Vector3<T>>& output) const {
// The ordering (old/new) of vertices does NOT matter in this method
if ((simplex.rank == 4) || (!simplex.is_valid())) {
// Must be valid simplex, and the size cannot be 4
return false;
}
// Simple case
if (simplex.rank == 1) {
const auto& v = simplex.vertices[0];
output.first = shape.support0(v.direction);
output.second = shape.support1(-v.direction);
return true;
} else if (simplex.rank == 2) {
const auto& v1 = simplex.vertices[0];
const auto& v2 = simplex.vertices[1];
const Vector3<T>& s1_new = v1.vertex;
const Vector3<T>& s2 = v2.vertex;
const Vector3<T> s1_to_s2 = s2 - s1_new;
const T squared_length = s1_to_s2.squaredNorm();
// This should not happen if we reach here, but let's handle it
if (squared_length <= 0.0) {
const auto& v = simplex.vertices[0];
output.first = shape.support0(v.direction);
output.second = shape.support1(-v.direction);
return true;
}
// Now we can do division
const T t = -s1_new.dot(s1_to_s2);
const T s2_weight = t / squared_length;
// Should be in [0, 1]
if (s2_weight > 1 + barycentric_weight_tolerance) {
// Should be s2, write output but return false
output.first = shape.support0(v2.direction);
output.second = shape.support1(-v2.direction);
return false;
} else if (s2_weight < -barycentric_weight_tolerance) {
// Should be s1
output.first = shape.support0(v1.direction);
output.second = shape.support1(-v1.direction);
return false;
} else {
// Assign the output
const T s1_weight = T(1.0) - s2_weight;
output.first = shape.support0(v1.direction) * s1_weight +
shape.support0(v2.direction) * s2_weight;
output.second = shape.support1(-v1.direction) * s1_weight +
shape.support1(-v2.direction) * s2_weight;
return true;
}
} else {
assert(simplex.rank == 3);
const auto& v1 = simplex.vertices[0];
const auto& v2 = simplex.vertices[1];
const auto& v3 = simplex.vertices[2];
const Vector3<T>& s1 = v1.vertex;
const Vector3<T>& s2 = v2.vertex;
const Vector3<T>& s3 = v3.vertex;
// Compute the weight
const Vector3<T> s1_to_s2 = s2 - s1;
const Vector3<T> s1_to_s3 = s3 - s1;
const Vector3<T> s1s2s3_plane_normal = s1_to_s2.cross(s1_to_s3);
const T area_squared = s1s2s3_plane_normal.squaredNorm();
if (area_squared <= 0.0) return false; // Should not happen if we are here
const Vector3<T>& n = s1s2s3_plane_normal;
const T d = s1.dot(n);
const Vector3<T> o_to_project = n * (d / area_squared);
const T area = std::sqrt(area_squared);
const T s2_weight = (s1_to_s3.cross(s1 - o_to_project)).norm() / area;
const T s3_weight = (s1_to_s2.cross(s1 - o_to_project)).norm() / area;
const T s1_weight = T(1.0) - s2_weight - s3_weight;
// All should be in [0, 1]
if ((s1_weight < -barycentric_weight_tolerance) ||
(s2_weight < -barycentric_weight_tolerance) ||
(s3_weight < -barycentric_weight_tolerance)) {
return false;
}
// Assign the output
output.first = shape.support0(v1.direction) * s1_weight +
shape.support0(v2.direction) * s2_weight +
shape.support0(v3.direction) * s3_weight;
output.second = shape.support1(-v1.direction) * s1_weight +
shape.support1(-v2.direction) * s2_weight +
shape.support1(-v3.direction) * s3_weight;
return true;
}
}
template <typename T>
bool GJK<T>::extractSeparationPointTrySubSimplex(
const MinkowskiDiff<T>& shape, const GJKSimplex<T>& simplex,
std::pair<Vector3<T>, Vector3<T>>& output) const {
if (simplex.rank >= 4) {
return false;
} else if (simplex.rank <= 2) {
extractSeparationPointNoSubSimplex(shape, simplex, output);
// Always OK here, despite the subtle when rank==2
return true;
}
// Must be 3-simplex
assert(simplex.rank == 3);
T min_distance_squared = -1;
GJKSimplex<T> simplex_cache;
std::pair<Vector3<T>, Vector3<T>> min_distance_output, output_cache;
// Triangle 123
{
simplex_cache = simplex;
auto ok =
extractSeparationPointNoSubSimplex(shape, simplex_cache, output_cache);
if (ok) {
// Must be on the triangle
const auto this_distance_square =
(output_cache.first - output_cache.second).squaredNorm();
if (min_distance_squared < 0 ||
(this_distance_square < min_distance_squared)) {
min_distance_squared = this_distance_square;
min_distance_output = output_cache;
}
}
}
// Segment 12
{
simplex_cache = simplex;
simplex_cache.rank = 2;
extractSeparationPointNoSubSimplex(shape, simplex_cache, output_cache);
// Must be on valid
const auto this_distance_square =
(output_cache.first - output_cache.second).squaredNorm();
if (min_distance_squared < 0 ||
(this_distance_square < min_distance_squared)) {
min_distance_squared = this_distance_square;
min_distance_output = output_cache;
}
}
// Segment 13
{
simplex_cache = simplex;
simplex_cache.vertices[1] = simplex_cache.vertices[2];
simplex_cache.rank = 2;
extractSeparationPointNoSubSimplex(shape, simplex_cache, output_cache);
// Must be on valid
const auto this_distance_square =
(output_cache.first - output_cache.second).squaredNorm();
if (min_distance_squared < 0 ||
(this_distance_square < min_distance_squared)) {
min_distance_squared = this_distance_square;
min_distance_output = output_cache;
}
}
// Segment 23
{
simplex_cache = simplex;
simplex_cache.vertices[0] = simplex_cache.vertices[1];
simplex_cache.vertices[1] = simplex_cache.vertices[2];
simplex_cache.rank = 2;
extractSeparationPointNoSubSimplex(shape, simplex_cache, output_cache);
// Must be on valid
const auto this_distance_square =
(output_cache.first - output_cache.second).squaredNorm();
if (min_distance_squared < 0 ||
(this_distance_square < min_distance_squared)) {
min_distance_squared = this_distance_square;
min_distance_output = output_cache;
}
}
// Assign the output
assert(min_distance_squared >= 0);
output = min_distance_output;
return true;
}
} // namespace cvx_collide
} // namespace fcl