12#include <boost/multiprecision/cpp_bin_float.hpp>
17#include <dolfinx/common/local_range.h>
22#include <spdlog/spdlog.h>
40inline std::array<T, 4> det4(
const std::array<T, 12>& s)
42 std::span<const T, 3> s0(s.begin(), 3);
43 std::span<const T, 3> s1(s.begin() + 3, 3);
44 std::span<const T, 3> s2(s.begin() + 6, 3);
45 std::span<const T, 3> s3(s.begin() + 9, 3);
48 T c0 = s2[1] * s3[2] - s2[2] * s3[1];
49 T c1 = s2[0] * s3[2] - s2[2] * s3[0];
50 T c2 = s2[0] * s3[1] - s2[1] * s3[0];
51 w[2] = -s0[0] * c0 + s0[1] * c1 - s0[2] * c2;
52 w[3] = s1[0] * c0 - s1[1] * c1 + s1[2] * c2;
54 c0 = s0[1] * s1[2] - s0[2] * s1[1];
55 c1 = s0[0] * s1[2] - s0[2] * s1[0];
56 c2 = s0[0] * s1[1] - s0[1] * s1[0];
57 w[0] = -s2[0] * c0 + s2[1] * c1 - s2[2] * c2;
58 w[1] = s3[0] * c0 - s3[1] * c1 + s3[2] * c2;
67template <
typename Vec>
68inline Vec::value_type dot3(
const Vec& a,
const Vec& b)
70 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
81template <
typename T, std::
size_t simplex_size>
82void nearest_simplex(
const std::array<T, 12>& s, std::array<T, 4>& coordinates)
85 SPDLOG_DEBUG(
"GJK: nearest_simplex({})", simplex_size);
87 if constexpr (simplex_size == 2)
91 std::span<const T, 3> s0(s.data(), 3);
92 std::span<const T, 3> s1(s.data() + 3, 3);
94 T lm = dot3(s0, s0) - dot3(s0, s1);
97 SPDLOG_DEBUG(
"GJK: line point A");
100 coordinates[1] = 0.0;
103 T mu = dot3(s1, s1) - dot3(s1, s0);
106 SPDLOG_DEBUG(
"GJK: line point B");
107 coordinates[0] = 0.0;
108 coordinates[1] = 1.0;
112 SPDLOG_DEBUG(
"GJK line: AB");
113 T f1 = 1.0 / (lm + mu);
114 coordinates[0] = mu * f1;
115 coordinates[1] = lm * f1;
118 else if constexpr (simplex_size == 3)
122 std::span<const T, 3> a(s.data(), 3);
123 std::span<const T, 3> b(s.data() + 3, 3);
124 std::span<const T, 3> c(s.data() + 6, 3);
131 if (d1 < 0.0 and d2 < 0.0)
133 SPDLOG_DEBUG(
"GJK: Point A");
134 coordinates[0] = 1.0;
135 coordinates[1] = 0.0;
136 coordinates[2] = 0.0;
144 if (d3 < 0.0 and d4 < 0.0)
146 SPDLOG_DEBUG(
"GJK: Point B");
147 coordinates[0] = 0.0;
148 coordinates[1] = 1.0;
149 coordinates[2] = 0.0;
156 if (d5 < 0.0 and d6 < 0.0)
158 SPDLOG_DEBUG(
"GJK: Point C");
159 coordinates[0] = 0.0;
160 coordinates[1] = 0.0;
161 coordinates[2] = 1.0;
165 T vc = d4 * d1 - d1 * d3 + d3 * d2;
166 if (vc < 0.0 and d1 > 0.0 and d3 > 0.0)
168 SPDLOG_DEBUG(
"GJK: edge AB");
169 T f1 = 1.0 / (d1 + d3);
174 coordinates[2] = 0.0;
177 T vb = d1 * d5 - d5 * d2 + d2 * d6;
178 if (vb < 0.0 and d2 > 0.0 and d5 > 0.0)
180 SPDLOG_DEBUG(
"GJK: edge AC");
181 T f1 = 1.0 / (d2 + d5);
185 coordinates[1] = 0.0;
189 T va = d3 * d6 - d6 * d4 + d4 * d5;
190 if (va < 0.0 and d4 > 0.0 and d6 > 0.0)
192 SPDLOG_DEBUG(
"GJK: edge BC");
193 T f1 = 1.0 / (d4 + d6);
196 coordinates[0] = 0.0;
202 SPDLOG_DEBUG(
"GJK: triangle ABC");
203 T f1 = 1.0 / (va + vb + vc);
204 coordinates[0] = va * f1;
205 coordinates[1] = vb * f1;
206 coordinates[2] = vc * f1;
209 else if constexpr (simplex_size == 4)
213 std::ranges::fill(coordinates, 0.0);
216 for (
int i = 0; i < 4; ++i)
219 std::span<const T, 3> si(s.begin() + i * 3, 3);
220 T sii = dot3(si, si);
222 for (
int j = 0; j < 4; ++j)
224 std::span<const T, 3> sj(s.begin() + j * 3, 3);
227 d[i][j] = (sii - dot3(si, sj));
228 SPDLOG_DEBUG(
"d[{}][{}] = {}", i, j,
static_cast<double>(d[i][j]));
236 coordinates[i] = 1.0;
241 SPDLOG_DEBUG(
"Check for edges");
245 int edges[6][2] = {{2, 3}, {1, 3}, {1, 2}, {0, 3}, {0, 2}, {0, 1}};
246 for (
int i = 0; i < 6; ++i)
250 int j0 = edges[i][0];
251 int j1 = edges[i][1];
252 int j2 = edges[5 - i][0];
253 int j3 = edges[5 - i][1];
254 v[i][0] = d[j1][j2] * d[j0][j1] - d[j0][j1] * d[j1][j0]
255 + d[j1][j0] * d[j0][j2];
256 v[i][1] = d[j1][j3] * d[j0][j1] - d[j0][j1] * d[j1][j0]
257 + d[j1][j0] * d[j0][j3];
259 SPDLOG_DEBUG(
"v[{}] = {},{}", i, (
double)v[i][0], (
double)v[i][1]);
260 if (v[i][0] <= 0.0 and v[i][1] <= 0.0 and d[j0][j1] >= 0.0
261 and d[j1][j0] >= 0.0)
264 T f1 = 1.0 / (d[j0][j1] + d[j1][j0]);
265 coordinates[j0] = f1 * d[j1][j0];
266 coordinates[j1] = f1 * d[j0][j1];
272 std::array<T, 4> w = det4(s);
273 T wsum = w[0] + w[1] + w[2] + w[3];
283 if (w[0] < 0.0 and v[2][0] > 0.0 and v[4][0] > 0.0 and v[5][0] > 0.0)
285 T f1 = 1.0 / (v[2][0] + v[4][0] + v[5][0]);
286 coordinates[0] = v[2][0] * f1;
287 coordinates[1] = v[4][0] * f1;
288 coordinates[2] = v[5][0] * f1;
289 coordinates[3] = 0.0;
293 if (w[1] < 0.0 and v[1][0] > 0.0 and v[3][0] > 0.0 and v[5][1] > 0.0)
295 T f1 = 1.0 / (v[1][0] + v[3][0] + v[5][1]);
296 coordinates[0] = v[1][0] * f1;
297 coordinates[1] = v[3][0] * f1;
298 coordinates[2] = 0.0;
299 coordinates[3] = v[5][1] * f1;
303 if (w[2] < 0.0 and v[0][0] > 0.0 and v[3][1] > 0 and v[4][1] > 0.0)
305 T f1 = 1.0 / (v[0][0] + v[3][1] + v[4][1]);
306 coordinates[0] = v[0][0] * f1;
307 coordinates[1] = 0.0;
308 coordinates[2] = v[3][1] * f1;
309 coordinates[3] = v[4][1] * f1;
313 if (w[3] < 0.0 and v[0][1] > 0.0 and v[1][1] > 0.0 and v[2][1] > 0.0)
315 T f1 = 1.0 / (v[0][1] + v[1][1] + v[2][1]);
316 coordinates[0] = 0.0;
317 coordinates[1] = v[0][1] * f1;
318 coordinates[2] = v[1][1] * f1;
319 coordinates[3] = v[2][1] * f1;
324 coordinates[0] = w[3] / wsum;
325 coordinates[1] = w[2] / wsum;
326 coordinates[2] = w[1] / wsum;
327 coordinates[3] = w[0] / wsum;
333 static_assert(simplex_size >= 2 && simplex_size <= 4,
334 "Number of rows defining simplex not supported.");
343inline int support(std::span<const T> bd,
const std::array<T, 3>& v)
346 T qmax = bd[0] * v[0] + bd[1] * v[1] + bd[2] * v[2];
347 for (std::size_t m = 1; m < bd.size() / 3; ++m)
349 T q = bd[3 * m] * v[0] + bd[3 * m + 1] * v[1] + bd[3 * m + 2] * v[2];
374template <std::floating_point T,
375 typename U = boost::multiprecision::cpp_bin_float_double_extended>
377 std::span<const T> q0)
379 assert(p0.size() % 3 == 0);
380 assert(q0.size() % 3 == 0);
382 constexpr int maxk = 15;
383 const U eps = 1000 * std::numeric_limits<U>::epsilon();
386 std::array<U, 3> x_k = {
static_cast<U
>(p0[0]) -
static_cast<U
>(q0[0]),
387 static_cast<U
>(p0[1]) -
static_cast<U
>(q0[1]),
388 static_cast<U
>(p0[2]) -
static_cast<U
>(q0[2])};
390 std::array<U, 12> s = {0};
394 std::array<U, 4> lmn = {0};
396 std::size_t simplex_size = 1;
399 for (k = 0; k < maxk; ++k)
404 const U x_norm2 = impl_gjk::dot3(x_k, x_k);
405 std::array<U, 3> x_k_normalized = x_k;
406 if (x_norm2 > eps * eps)
412 U inv_norm = U(1.0) / sqrt(x_norm2);
413 x_k_normalized[0] *= inv_norm;
414 x_k_normalized[1] *= inv_norm;
415 x_k_normalized[2] *= inv_norm;
418 std::array<T, 3> dir_p = {
static_cast<T
>(-x_k_normalized[0]),
419 static_cast<T
>(-x_k_normalized[1]),
420 static_cast<T
>(-x_k_normalized[2])};
421 std::array<T, 3> dir_q
422 = {
static_cast<T
>(x_k_normalized[0]),
static_cast<T
>(x_k_normalized[1]),
423 static_cast<T
>(x_k_normalized[2])};
424 int ip = impl_gjk::support(p0, dir_p);
425 int iq = impl_gjk::support(q0, dir_q);
429 = {
static_cast<U
>(p0[ip * 3]) -
static_cast<U
>(q0[iq * 3]),
430 static_cast<U
>(p0[ip * 3 + 1]) -
static_cast<U
>(q0[iq * 3 + 1]),
431 static_cast<U
>(p0[ip * 3 + 2]) -
static_cast<U
>(q0[iq * 3 + 2])};
435 for (m = 0; m < simplex_size; ++m)
437 auto it = std::next(s.begin(), 3 * m);
438 if (std::equal(it, std::next(it, 3), s_k.begin(), s_k.end()))
442 if (m != simplex_size)
446 const U xs_diff = x_norm2 - impl_gjk::dot3(x_k, s_k);
447 if (xs_diff < (eps * x_norm2) or xs_diff < eps)
450 SPDLOG_DEBUG(
"GJK: xs_diff={}/{}",
static_cast<double>(xs_diff),
451 static_cast<double>(eps));
454 assert(simplex_size < 4);
455 std::ranges::copy(s_k, s.begin() + 3 * simplex_size);
459 switch (simplex_size)
462 impl_gjk::nearest_simplex<U, 2>(s, lmn);
465 impl_gjk::nearest_simplex<U, 3>(s, lmn);
468 impl_gjk::nearest_simplex<U, 4>(s, lmn);
471 throw std::runtime_error(
"Invalid simplex size");
476 x_k = {0.0, 0.0, 0.0};
477 for (std::size_t i = 0; i < simplex_size; ++i)
479 std::span<const U> sc(std::next(s.begin(), 3 * i), 3);
482 x_k[0] += lmn[i] * sc[0];
483 x_k[1] += lmn[i] * sc[1];
484 x_k[2] += lmn[i] * sc[2];
486 std::ranges::copy(sc, std::next(s.begin(), 3 * j));
495 if (simplex_size == 4)
499 const U x_next_norm2 = impl_gjk::dot3(x_k, x_k);
500 if (x_norm2 <= x_next_norm2)
504 if (x_next_norm2 < eps * eps)
509 throw std::runtime_error(
"GJK error - max iteration limit reached");
510 return {
static_cast<T
>(x_k[0]),
static_cast<T
>(x_k[1]),
511 static_cast<T
>(x_k[2])};
531template <std::floating_point T,
532 typename U = boost::multiprecision::cpp_bin_float_double_extended>
535 std::span<const T> q,
int num_threads)
537 if (num_threads <= 0)
538 throw std::invalid_argument(
"num_threads must be >= 1.");
540 std::size_t total_size = bodies.size();
542 = std::max<std::size_t>(1, std::min(num_threads, (
int)total_size));
544 std::vector<T> results(total_size * 3);
546 = [](std::vector<T>& chunk_results,
547 const std::vector<std::span<const T>>& chunk_bodies, std::size_t c0,
548 std::size_t c1, std::span<const T> q_ref)
550 for (std::size_t i = c0; i < c1; ++i)
553 std::array<T, 3> dist
555 chunk_results[3 * i + 0] = dist[0];
556 chunk_results[3 * i + 1] = dist[1];
557 chunk_results[3 * i + 2] = dist[2];
561 std::vector<std::jthread> threads;
562 for (
int i = 1; i < num_threads; ++i)
565 threads.emplace_back(compute_chunk, std::ref(results), std::ref(bodies), c0,
569 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:26
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:534
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:376