11#include <boost/multiprecision/cpp_bin_float.hpp>
35inline std::array<T, 4> det4(
const std::array<T, 12>& s)
37 std::span<const T, 3> s0(s.begin(), 3);
38 std::span<const T, 3> s1(s.begin() + 3, 3);
39 std::span<const T, 3> s2(s.begin() + 6, 3);
40 std::span<const T, 3> s3(s.begin() + 9, 3);
43 T c0 = s2[1] * s3[2] - s2[2] * s3[1];
44 T c1 = s2[0] * s3[2] - s2[2] * s3[0];
45 T c2 = s2[0] * s3[1] - s2[1] * s3[0];
46 w[2] = -s0[0] * c0 + s0[1] * c1 - s0[2] * c2;
47 w[3] = s1[0] * c0 - s1[1] * c1 + s1[2] * c2;
49 c0 = s0[1] * s1[2] - s0[2] * s1[1];
50 c1 = s0[0] * s1[2] - s0[2] * s1[0];
51 c2 = s0[0] * s1[1] - s0[1] * s1[0];
52 w[0] = -s2[0] * c0 + s2[1] * c1 - s2[2] * c2;
53 w[1] = s3[0] * c0 - s3[1] * c1 + s3[2] * c2;
62template <
typename Vec>
63inline Vec::value_type dot3(
const Vec& a,
const Vec& b)
65 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
76template <
typename T, std::
size_t simplex_size>
77void nearest_simplex(
const std::array<T, 12>& s, std::array<T, 4>& coordinates)
80 SPDLOG_DEBUG(
"GJK: nearest_simplex({})", simplex_size);
82 if constexpr (simplex_size == 2)
86 std::span<const T, 3> s0(s.data(), 3);
87 std::span<const T, 3> s1(s.data() + 3, 3);
89 T lm = dot3(s0, s0) - dot3(s0, s1);
92 SPDLOG_DEBUG(
"GJK: line point A");
98 T mu = dot3(s1, s1) - dot3(s1, s0);
101 SPDLOG_DEBUG(
"GJK: line point B");
102 coordinates[0] = 0.0;
103 coordinates[1] = 1.0;
107 SPDLOG_DEBUG(
"GJK line: AB");
108 T f1 = 1.0 / (lm + mu);
109 coordinates[0] = mu * f1;
110 coordinates[1] = lm * f1;
113 else if constexpr (simplex_size == 3)
117 std::span<const T, 3> a(s.data(), 3);
118 std::span<const T, 3> b(s.data() + 3, 3);
119 std::span<const T, 3> c(s.data() + 6, 3);
126 if (d1 < 0.0 and d2 < 0.0)
128 SPDLOG_DEBUG(
"GJK: Point A");
129 coordinates[0] = 1.0;
130 coordinates[1] = 0.0;
131 coordinates[2] = 0.0;
139 if (d3 < 0.0 and d4 < 0.0)
141 SPDLOG_DEBUG(
"GJK: Point B");
142 coordinates[0] = 0.0;
143 coordinates[1] = 1.0;
144 coordinates[2] = 0.0;
151 if (d5 < 0.0 and d6 < 0.0)
153 SPDLOG_DEBUG(
"GJK: Point C");
154 coordinates[0] = 0.0;
155 coordinates[1] = 0.0;
156 coordinates[2] = 1.0;
160 T vc = d4 * d1 - d1 * d3 + d3 * d2;
161 if (vc < 0.0 and d1 > 0.0 and d3 > 0.0)
163 SPDLOG_DEBUG(
"GJK: edge AB");
164 T f1 = 1.0 / (d1 + d3);
169 coordinates[2] = 0.0;
172 T vb = d1 * d5 - d5 * d2 + d2 * d6;
173 if (vb < 0.0 and d2 > 0.0 and d5 > 0.0)
175 SPDLOG_DEBUG(
"GJK: edge AC");
176 T f1 = 1.0 / (d2 + d5);
180 coordinates[1] = 0.0;
184 T va = d3 * d6 - d6 * d4 + d4 * d5;
185 if (va < 0.0 and d4 > 0.0 and d6 > 0.0)
187 SPDLOG_DEBUG(
"GJK: edge BC");
188 T f1 = 1.0 / (d4 + d6);
191 coordinates[0] = 0.0;
197 SPDLOG_DEBUG(
"GJK: triangle ABC");
198 T f1 = 1.0 / (va + vb + vc);
199 coordinates[0] = va * f1;
200 coordinates[1] = vb * f1;
201 coordinates[2] = vc * f1;
204 else if constexpr (simplex_size == 4)
208 std::ranges::fill(coordinates, 0.0);
211 for (
int i = 0; i < 4; ++i)
214 std::span<const T, 3> si(s.begin() + i * 3, 3);
215 T sii = dot3(si, si);
217 for (
int j = 0; j < 4; ++j)
219 std::span<const T, 3> sj(s.begin() + j * 3, 3);
221 d[i][j] = (sii - dot3(si, sj));
222 SPDLOG_DEBUG(
"d[{}][{}] = {}", i, j,
static_cast<double>(d[i][j]));
229 coordinates[i] = 1.0;
234 SPDLOG_DEBUG(
"Check for edges");
238 int edges[6][2] = {{2, 3}, {1, 3}, {1, 2}, {0, 3}, {0, 2}, {0, 1}};
239 for (
int i = 0; i < 6; ++i)
243 int j0 = edges[i][0];
244 int j1 = edges[i][1];
245 int j2 = edges[5 - i][0];
246 int j3 = edges[5 - i][1];
247 v[i][0] = d[j1][j2] * d[j0][j1] - d[j0][j1] * d[j1][j0]
248 + d[j1][j0] * d[j0][j2];
249 v[i][1] = d[j1][j3] * d[j0][j1] - d[j0][j1] * d[j1][j0]
250 + d[j1][j0] * d[j0][j3];
252 SPDLOG_DEBUG(
"v[{}] = {},{}", i, (
double)v[i][0], (
double)v[i][1]);
253 if (v[i][0] <= 0.0 and v[i][1] <= 0.0 and d[j0][j1] >= 0.0
254 and d[j1][j0] >= 0.0)
257 T f1 = 1.0 / (d[j0][j1] + d[j1][j0]);
258 coordinates[j0] = f1 * d[j1][j0];
259 coordinates[j1] = f1 * d[j0][j1];
265 std::array<T, 4> w = det4(s);
266 T wsum = w[0] + w[1] + w[2] + w[3];
276 if (w[0] < 0.0 and v[2][0] > 0.0 and v[4][0] > 0.0 and v[5][0] > 0.0)
278 T f1 = 1.0 / (v[2][0] + v[4][0] + v[5][0]);
279 coordinates[0] = v[2][0] * f1;
280 coordinates[1] = v[4][0] * f1;
281 coordinates[2] = v[5][0] * f1;
282 coordinates[3] = 0.0;
286 if (w[1] < 0.0 and v[1][0] > 0.0 and v[3][0] > 0.0 and v[5][1] > 0.0)
288 T f1 = 1.0 / (v[1][0] + v[3][0] + v[5][1]);
289 coordinates[0] = v[1][0] * f1;
290 coordinates[1] = v[3][0] * f1;
291 coordinates[2] = 0.0;
292 coordinates[3] = v[5][1] * f1;
296 if (w[2] < 0.0 and v[0][0] > 0.0 and v[3][1] > 0 and v[4][1] > 0.0)
298 T f1 = 1.0 / (v[0][0] + v[3][1] + v[4][1]);
299 coordinates[0] = v[0][0] * f1;
300 coordinates[1] = 0.0;
301 coordinates[2] = v[3][1] * f1;
302 coordinates[3] = v[4][1] * f1;
306 if (w[3] < 0.0 and v[0][1] > 0.0 and v[1][1] > 0.0 and v[2][1] > 0.0)
308 T f1 = 1.0 / (v[0][1] + v[1][1] + v[2][1]);
309 coordinates[0] = 0.0;
310 coordinates[1] = v[0][1] * f1;
311 coordinates[2] = v[1][1] * f1;
312 coordinates[3] = v[2][1] * f1;
317 coordinates[0] = w[3] / wsum;
318 coordinates[1] = w[2] / wsum;
319 coordinates[2] = w[1] / wsum;
320 coordinates[3] = w[0] / wsum;
326 static_assert(simplex_size >= 2 && simplex_size <= 4,
327 "Number of rows defining simplex not supported.");
336inline int support(std::span<const T> bd,
const std::array<T, 3>& v)
339 T qmax = bd[0] * v[0] + bd[1] * v[1] + bd[2] * v[2];
340 for (std::size_t m = 1; m < bd.size() / 3; ++m)
342 T q = bd[3 * m] * v[0] + bd[3 * m + 1] * v[1] + bd[3 * m + 2] * v[2];
367template <std::floating_point T,
368 typename U = boost::multiprecision::cpp_bin_float_double_extended>
370 std::span<const T> q0)
372 assert(p0.size() % 3 == 0);
373 assert(q0.size() % 3 == 0);
375 constexpr int maxk = 15;
376 const U eps = 1000 * std::numeric_limits<U>::epsilon();
379 std::array<U, 3> x_k = {
static_cast<U
>(p0[0]) -
static_cast<U
>(q0[0]),
380 static_cast<U
>(p0[1]) -
static_cast<U
>(q0[1]),
381 static_cast<U
>(p0[2]) -
static_cast<U
>(q0[2])};
383 std::array<U, 12> s = {0};
387 std::array<U, 4> lmn = {0};
389 std::size_t simplex_size = 1;
392 for (k = 0; k < maxk; ++k)
397 const U x_norm2 = impl_gjk::dot3(x_k, x_k);
398 std::array<U, 3> x_k_normalized = x_k;
399 if (x_norm2 > eps * eps)
405 U inv_norm = U(1.0) / sqrt(x_norm2);
406 x_k_normalized[0] *= inv_norm;
407 x_k_normalized[1] *= inv_norm;
408 x_k_normalized[2] *= inv_norm;
411 std::array<T, 3> dir_p = {
static_cast<T
>(-x_k_normalized[0]),
412 static_cast<T
>(-x_k_normalized[1]),
413 static_cast<T
>(-x_k_normalized[2])};
414 std::array<T, 3> dir_q
415 = {
static_cast<T
>(x_k_normalized[0]),
static_cast<T
>(x_k_normalized[1]),
416 static_cast<T
>(x_k_normalized[2])};
417 int ip = impl_gjk::support(p0, dir_p);
418 int iq = impl_gjk::support(q0, dir_q);
422 = {
static_cast<U
>(p0[ip * 3]) -
static_cast<U
>(q0[iq * 3]),
423 static_cast<U
>(p0[ip * 3 + 1]) -
static_cast<U
>(q0[iq * 3 + 1]),
424 static_cast<U
>(p0[ip * 3 + 2]) -
static_cast<U
>(q0[iq * 3 + 2])};
428 for (m = 0; m < simplex_size; ++m)
430 auto it = std::next(s.begin(), 3 * m);
431 if (std::equal(it, std::next(it, 3), s_k.begin(), s_k.end()))
435 if (m != simplex_size)
439 const U xs_diff = x_norm2 - impl_gjk::dot3(x_k, s_k);
440 if (xs_diff < (eps * x_norm2) or xs_diff < eps)
443 SPDLOG_DEBUG(
"GJK: xs_diff={}/{}",
static_cast<double>(xs_diff),
444 static_cast<double>(eps));
447 std::ranges::copy(s_k, s.begin() + 3 * simplex_size);
451 switch (simplex_size)
454 impl_gjk::nearest_simplex<U, 2>(s, lmn);
457 impl_gjk::nearest_simplex<U, 3>(s, lmn);
460 impl_gjk::nearest_simplex<U, 4>(s, lmn);
463 throw std::runtime_error(
"Invalid simplex size");
468 x_k = {0.0, 0.0, 0.0};
469 for (std::size_t i = 0; i < simplex_size; ++i)
471 std::span<const U> sc(std::next(s.begin(), 3 * i), 3);
474 x_k[0] += lmn[i] * sc[0];
475 x_k[1] += lmn[i] * sc[1];
476 x_k[2] += lmn[i] * sc[2];
478 std::ranges::copy(sc, std::next(s.begin(), 3 * j));
485 const U x_next_norm2 = impl_gjk::dot3(x_k, x_k);
486 if (x_norm2 <= x_next_norm2)
490 if (x_next_norm2 < eps * eps)
495 throw std::runtime_error(
"GJK error - max iteration limit reached");
496 return {
static_cast<T
>(x_k[0]),
static_cast<T
>(x_k[1]),
497 static_cast<T
>(x_k[2])};
517template <std::floating_point T,
518 typename U = boost::multiprecision::cpp_bin_float_double_extended>
521 std::span<const T> q,
int num_threads)
523 if (num_threads <= 0)
524 throw std::runtime_error(
"num_threads must be >= 1.");
526 std::size_t total_size = bodies.size();
528 = std::max<std::size_t>(1, std::min(num_threads, (
int)total_size));
530 std::vector<T> results(total_size * 3);
532 [](std::vector<T>& results,
const std::vector<std::span<const T>>& bodies,
533 std::size_t c0, std::size_t c1, std::span<const T> q_ref)
535 for (std::size_t i = c0; i < c1; ++i)
539 results[3 * i + 0] = dist[0];
540 results[3 * i + 1] = dist[1];
541 results[3 * i + 2] = dist[2];
545 std::vector<std::jthread> threads;
546 for (
int i = 1; i < num_threads; ++i)
549 threads.emplace_back(compute_chunk, std::ref(results), std::ref(bodies), c0,
553 compute_chunk(std::ref(results), std::cref(bodies), c0, c1, q);
constexpr std::array< std::int64_t, 2 > local_range(int index, std::int64_t N, int size)
Partition a global range [0, N - 1] across callers into non-overlapping sub-partitions of almost equa...
Definition local_range.h:26
Geometry data structures and algorithms.
Definition BoundingBoxTree.h:24
std::vector< T > compute_distances_gjk(const std::vector< std::span< const T > > &bodies, std::span< const T > q, int num_threads)
Compute the distance between a sequence of convex bodies p0, ..., pN and q, each defined by a set of ...
Definition gjk.h:520
std::array< T, 3 > compute_distance_gjk(std::span< const T > p0, std::span< const T > q0)
Compute the distance between two convex bodies p0 and q0, each defined by a set of points.
Definition gjk.h:369