EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_TriangleDistance.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
15#ifndef EBGEOMETRY_TRIANGLEDISTANCE_HPP
16#define EBGEOMETRY_TRIANGLEDISTANCE_HPP
17
18// Std includes
19#include <type_traits>
20
21// Our includes
22#include "EBGeometry_GPU.hpp"
23#include "EBGeometry_SIMD.hpp"
24
25namespace EBGeometry {
26
30namespace TriangleDistance {
31
37template <class V>
38struct Vec3V
39{
43 V x;
44
48 V y;
49
53 V z;
54};
55
57template <class V>
59inline Vec3V<V>
60operator-(const Vec3V<V>& a_u, const Vec3V<V>& a_v) noexcept
61{
62 return Vec3V<V>{a_u.x - a_v.x, a_u.y - a_v.y, a_u.z - a_v.z};
63}
64
65template <class V>
67inline V
68dot(const Vec3V<V>& a_u, const Vec3V<V>& a_v) noexcept
69{
70 return a_u.x * a_v.x + a_u.y * a_v.y + a_u.z * a_v.z;
71}
72
73template <class V>
75inline Vec3V<V>
76cross(const Vec3V<V>& a_u, const Vec3V<V>& a_v) noexcept
77{
78 return Vec3V<V>{a_u.y * a_v.z - a_u.z * a_v.y, a_u.z * a_v.x - a_u.x * a_v.z, a_u.x * a_v.y - a_u.y * a_v.x};
79}
80
81template <class V>
83inline Vec3V<V>
84scaled(const Vec3V<V>& a_u, const V a_s) noexcept
85{
86 return Vec3V<V>{a_s * a_u.x, a_s * a_u.y, a_s * a_u.z};
87}
88
89template <class V>
91inline V
92constant(const int a_value) noexcept
93{
94 using T = typename SIMD::Scalar<V>::type;
95
96 return V(T(a_value));
97}
98
99// +1 where a_x > 0, else -1 (zero and NaN included).
100template <class V>
102inline V
103sign(const V a_x) noexcept
104{
105 using SIMD::select;
106
107 return select(a_x > constant<V>(0), constant<V>(1), constant<V>(-1));
108}
109
110// Whether a projection parameter lies strictly inside its edge, 0 < t < 1.
111template <class V>
113inline auto
114onEdge(const V a_t) noexcept
115{
116 using SIMD::both;
117
118 return both(a_t > constant<V>(0), a_t < constant<V>(1));
119}
121
148template <class V, class Source>
149[[nodiscard]] EBGEOMETRY_HOST_DEVICE
151inline V
152signedDistance(const Source& a_triangle, const Vec3V<V>& a_point) noexcept
153{
154 using SIMD::abs;
155 using SIMD::both;
156 using SIMD::select;
157 using SIMD::sqrt;
158
159 const Vec3V<V> v0 = a_triangle.vertex(0);
160 const Vec3V<V> v1 = a_triangle.vertex(1);
161 const Vec3V<V> v2 = a_triangle.vertex(2);
162 const Vec3V<V> n = a_triangle.normal();
163
164 const Vec3V<V> v21 = v1 - v0;
165 const Vec3V<V> v32 = v2 - v1;
166 const Vec3V<V> v13 = v0 - v2;
167
168 const Vec3V<V> p1 = a_point - v0;
169 const Vec3V<V> p2 = a_point - v1;
170 const Vec3V<V> p3 = a_point - v2;
171
172 // With outward normals the three signs are all -1 for a point projecting inside (sum -3), with
173 // inward normals all +1 (sum +3).
174 const V s0 = sign(dot(cross(v21, n), p1));
175 const V s1 = sign(dot(cross(v32, n), p2));
176 const V s2 = sign(dot(cross(v13, n), p3));
177
178 const auto inside = abs(s0 + s1 + s2) >= constant<V>(2);
179 const V faceD = dot(n, p1);
180
181 if constexpr (std::is_floating_point_v<V>) {
182 // One lane can branch where a SIMD lane cannot. A point projecting inside the face needs no
183 // vertex or edge, and only the closest candidate needs its sign: on a device this skips most of
184 // the double-precision work, which bounds the kernel there.
185 if (inside) {
186 return faceD;
187 }
188
189 // The closest candidate so far: its squared distance, the vector from it to the point, and
190 // which pseudonormal signs it (0-2 vertices, 3-5 edges). An edge's closest point is computed only
191 // where the point projects onto the edge (0 < t < 1).
192 V bestD2 = dot(p1, p1);
193 Vec3V<V> bestVec = p1;
194 int best = 0;
195
196 const V p2d2 = dot(p2, p2);
197
198 if (p2d2 < bestD2) {
199 bestD2 = p2d2;
200 bestVec = p2;
201 best = 1;
202 }
203
204 const V p3d2 = dot(p3, p3);
205
206 if (p3d2 < bestD2) {
207 bestD2 = p3d2;
208 bestVec = p3;
209 best = 2;
210 }
211
212 const V t1 = dot(p1, v21) / dot(v21, v21);
213
214 if (onEdge(t1)) {
215 const Vec3V<V> y1 = p1 - scaled(v21, t1);
216 const V y1d2 = dot(y1, y1);
217
218 if (y1d2 < bestD2) {
219 bestD2 = y1d2;
220 bestVec = y1;
221 best = 3;
222 }
223 }
224
225 const V t2 = dot(p2, v32) / dot(v32, v32);
226
227 if (onEdge(t2)) {
228 const Vec3V<V> y2 = p2 - scaled(v32, t2);
229 const V y2d2 = dot(y2, y2);
230
231 if (y2d2 < bestD2) {
232 bestD2 = y2d2;
233 bestVec = y2;
234 best = 4;
235 }
236 }
237
238 const V t3 = dot(p3, v13) / dot(v13, v13);
239
240 if (onEdge(t3)) {
241 const Vec3V<V> y3 = p3 - scaled(v13, t3);
242 const V y3d2 = dot(y3, y3);
243
244 if (y3d2 < bestD2) {
245 bestD2 = y3d2;
246 bestVec = y3;
247 best = 5;
248 }
249 }
250
251 const auto k = static_cast<size_t>(best);
252 const Vec3V<V> pseudonormal = k < 3 ? a_triangle.vertexNormal(k) : a_triangle.edgeNormal(k - 3);
253
254 return sign(dot(pseudonormal, bestVec)) * sqrt(bestD2);
255 }
256
257 const V t1 = dot(p1, v21) / dot(v21, v21);
258 const V t2 = dot(p2, v32) / dot(v32, v32);
259 const V t3 = dot(p3, v13) / dot(v13, v13);
260
261 const Vec3V<V> y1 = p1 - scaled(v21, t1);
262 const Vec3V<V> y2 = p2 - scaled(v32, t2);
263 const Vec3V<V> y3 = p3 - scaled(v13, t3);
264
265 // Vertices, then edges; a candidate replaces the best only when strictly closer, and an edge counts
266 // only where the point projects inside it (0 < t < 1).
267 V bestD2 = dot(p1, p1);
268 V bestSign = sign(dot(a_triangle.vertexNormal(0), p1));
269
270 const V p2d2 = dot(p2, p2);
271 const auto c2 = p2d2 < bestD2;
272 bestSign = select(c2, sign(dot(a_triangle.vertexNormal(1), p2)), bestSign);
273 bestD2 = select(c2, p2d2, bestD2);
274
275 const V p3d2 = dot(p3, p3);
276 const auto c3 = p3d2 < bestD2;
277 bestSign = select(c3, sign(dot(a_triangle.vertexNormal(2), p3)), bestSign);
278 bestD2 = select(c3, p3d2, bestD2);
279
280 const V y1d2 = dot(y1, y1);
281 const auto e1 = both(onEdge(t1), y1d2 < bestD2);
282 bestSign = select(e1, sign(dot(a_triangle.edgeNormal(0), y1)), bestSign);
283 bestD2 = select(e1, y1d2, bestD2);
284
285 const V y2d2 = dot(y2, y2);
286 const auto e2 = both(onEdge(t2), y2d2 < bestD2);
287 bestSign = select(e2, sign(dot(a_triangle.edgeNormal(1), y2)), bestSign);
288 bestD2 = select(e2, y2d2, bestD2);
289
290 const V y3d2 = dot(y3, y3);
291 const auto e3 = both(onEdge(t3), y3d2 < bestD2);
292 bestSign = select(e3, sign(dot(a_triangle.edgeNormal(2), y3)), bestSign);
293 bestD2 = select(e3, y3d2, bestD2);
294
295 return select(inside, faceD, bestSign * sqrt(bestD2));
296}
297
313template <class V, class Source>
314[[nodiscard]] EBGEOMETRY_HOST_DEVICE
316inline V
317unsignedDistance2(const Source& a_triangle, const Vec3V<V>& a_point) noexcept
318{
319 using SIMD::abs;
320 using SIMD::both;
321 using SIMD::select;
322
323 const Vec3V<V> v0 = a_triangle.vertex(0);
324 const Vec3V<V> v1 = a_triangle.vertex(1);
325 const Vec3V<V> v2 = a_triangle.vertex(2);
326 const Vec3V<V> n = a_triangle.normal();
327
328 const Vec3V<V> v21 = v1 - v0;
329 const Vec3V<V> v32 = v2 - v1;
330 const Vec3V<V> v13 = v0 - v2;
331
332 const Vec3V<V> p1 = a_point - v0;
333 const Vec3V<V> p2 = a_point - v1;
334 const Vec3V<V> p3 = a_point - v2;
335
336 const V s0 = sign(dot(cross(v21, n), p1));
337 const V s1 = sign(dot(cross(v32, n), p2));
338 const V s2 = sign(dot(cross(v13, n), p3));
339
340 const auto inside = abs(s0 + s1 + s2) >= constant<V>(2);
341 const V faceD = dot(n, p1);
342
343 if constexpr (std::is_floating_point_v<V>) {
344 if (inside) {
345 return faceD * faceD;
346 }
347
348 V bestD2 = dot(p1, p1);
349
350 const V p2d2 = dot(p2, p2);
351 const V p3d2 = dot(p3, p3);
352
353 bestD2 = p2d2 < bestD2 ? p2d2 : bestD2;
354 bestD2 = p3d2 < bestD2 ? p3d2 : bestD2;
355
356 const V t1 = dot(p1, v21) / dot(v21, v21);
357
358 if (onEdge(t1)) {
359 const Vec3V<V> y1 = p1 - scaled(v21, t1);
360 const V y1d2 = dot(y1, y1);
361
362 bestD2 = y1d2 < bestD2 ? y1d2 : bestD2;
363 }
364
365 const V t2 = dot(p2, v32) / dot(v32, v32);
366
367 if (onEdge(t2)) {
368 const Vec3V<V> y2 = p2 - scaled(v32, t2);
369 const V y2d2 = dot(y2, y2);
370
371 bestD2 = y2d2 < bestD2 ? y2d2 : bestD2;
372 }
373
374 const V t3 = dot(p3, v13) / dot(v13, v13);
375
376 if (onEdge(t3)) {
377 const Vec3V<V> y3 = p3 - scaled(v13, t3);
378 const V y3d2 = dot(y3, y3);
379
380 bestD2 = y3d2 < bestD2 ? y3d2 : bestD2;
381 }
382
383 return bestD2;
384 }
385
386 const V t1 = dot(p1, v21) / dot(v21, v21);
387 const V t2 = dot(p2, v32) / dot(v32, v32);
388 const V t3 = dot(p3, v13) / dot(v13, v13);
389
390 const Vec3V<V> y1 = p1 - scaled(v21, t1);
391 const Vec3V<V> y2 = p2 - scaled(v32, t2);
392 const Vec3V<V> y3 = p3 - scaled(v13, t3);
393
394 V bestD2 = dot(p1, p1);
395
396 const V p2d2 = dot(p2, p2);
397 bestD2 = select(p2d2 < bestD2, p2d2, bestD2);
398
399 const V p3d2 = dot(p3, p3);
400 bestD2 = select(p3d2 < bestD2, p3d2, bestD2);
401
402 const V y1d2 = dot(y1, y1);
403 bestD2 = select(both(onEdge(t1), y1d2 < bestD2), y1d2, bestD2);
404
405 const V y2d2 = dot(y2, y2);
406 bestD2 = select(both(onEdge(t2), y2d2 < bestD2), y2d2, bestD2);
407
408 const V y3d2 = dot(y3, y3);
409 bestD2 = select(both(onEdge(t3), y3d2 < bestD2), y3d2, bestD2);
410
411 return select(inside, faceD * faceD, bestD2);
412}
413
431template <class T, class Source>
432[[nodiscard]] EBGEOMETRY_HOST_DEVICE
433inline T
434closestPoint(const Source& a_triangle, const Vec3V<T>& a_point, Vec3V<T>& a_closestPoint, Vec3V<T>& a_normal) noexcept
435{
436 static_assert(std::is_floating_point_v<T>, "TriangleDistance::closestPoint is a scalar kernel");
437
438 const Vec3V<T> v0 = a_triangle.vertex(0);
439 const Vec3V<T> v1 = a_triangle.vertex(1);
440 const Vec3V<T> v2 = a_triangle.vertex(2);
441 const Vec3V<T> n = a_triangle.normal();
442
443 const Vec3V<T> v21 = v1 - v0;
444 const Vec3V<T> v32 = v2 - v1;
445 const Vec3V<T> v13 = v0 - v2;
446
447 const Vec3V<T> p1 = a_point - v0;
448 const Vec3V<T> p2 = a_point - v1;
449 const Vec3V<T> p3 = a_point - v2;
450
451 const T s0 = sign(dot(cross(v21, n), p1));
452 const T s1 = sign(dot(cross(v32, n), p2));
453 const T s2 = sign(dot(cross(v13, n), p3));
454
455 const bool inside = std::abs(s0 + s1 + s2) >= T(2);
456
457 // The closest candidate so far: its squared distance, the vector from it to the point, and which feature
458 // it is (0-2 vertices, 3-5 edges).
459 T bestD2 = dot(p1, p1);
460 Vec3V<T> bestVec = p1;
461 int best = 0;
462
463 const T p2d2 = dot(p2, p2);
464
465 if (p2d2 < bestD2) {
466 bestD2 = p2d2;
467 bestVec = p2;
468 best = 1;
469 }
470
471 const T p3d2 = dot(p3, p3);
472
473 if (p3d2 < bestD2) {
474 bestD2 = p3d2;
475 bestVec = p3;
476 best = 2;
477 }
478
479 const T t1 = dot(p1, v21) / dot(v21, v21);
480
481 if (onEdge(t1)) {
482 const Vec3V<T> y1 = p1 - scaled(v21, t1);
483 const T y1d2 = dot(y1, y1);
484
485 if (y1d2 < bestD2) {
486 bestD2 = y1d2;
487 bestVec = y1;
488 best = 3;
489 }
490 }
491
492 const T t2 = dot(p2, v32) / dot(v32, v32);
493
494 if (onEdge(t2)) {
495 const Vec3V<T> y2 = p2 - scaled(v32, t2);
496 const T y2d2 = dot(y2, y2);
497
498 if (y2d2 < bestD2) {
499 bestD2 = y2d2;
500 bestVec = y2;
501 best = 4;
502 }
503 }
504
505 const T t3 = dot(p3, v13) / dot(v13, v13);
506
507 if (onEdge(t3)) {
508 const Vec3V<T> y3 = p3 - scaled(v13, t3);
509 const T y3d2 = dot(y3, y3);
510
511 if (y3d2 < bestD2) {
512 bestD2 = y3d2;
513 bestVec = y3;
514 best = 5;
515 }
516 }
517
518 const auto k = static_cast<size_t>(best);
519
520 // A point projecting inside the face is nearest the face, unless it projects onto the face's boundary,
521 // where an edge or a vertex is as near: that feature is then the one the point lies on, and its
522 // pseudonormal (not the face's) is the normal there. The distance is the face's either way.
523 if (inside) {
524 const T faceD = dot(n, p1);
525
526 if (bestD2 > faceD * faceD) {
527 a_closestPoint = a_point - scaled(n, faceD);
528 a_normal = n;
529 }
530 else {
531 a_closestPoint = a_point - bestVec;
532 a_normal = k < 3 ? a_triangle.vertexNormal(k) : a_triangle.edgeNormal(k - 3);
533 }
534
535 return faceD;
536 }
537
538 // bestVec runs from the closest point to the query point, whatever the feature.
539 a_closestPoint = a_point - bestVec;
540 a_normal = k < 3 ? a_triangle.vertexNormal(k) : a_triangle.edgeNormal(k - 3);
541
542 return sign(dot(a_normal, bestVec)) * std::sqrt(bestD2);
543}
544
545} // namespace TriangleDistance
546
547} // namespace EBGeometry
548
549#endif
GPU portability layer: host/device decoration macros and backend detection.
#define EBGEOMETRY_HOST_ALWAYS_INLINE
Always inline a function in a host pass (GCC and Clang); nothing in a device pass.
Definition EBGeometry_GPU.hpp:123
#define EBGEOMETRY_HOST_DEVICE
Mark a function as callable from both host and device code (no-op on host compilers).
Definition EBGeometry_GPU.hpp:80
Thin wrappers over SSE4.1, AVX and AVX-512 registers, so that one kernel template serves every SIMD w...
EBGEOMETRY_HOST_ALWAYS_INLINE Tagged< T > select(const bool a_second, const Tagged< T > &a_first, const Tagged< T > &a_other) noexcept
One of two tagged values, chosen field by field, so that the compiler selects without a branch: a who...
Definition EBGeometry_CSGOps.hpp:273
bool both(const bool a_lhs, const bool a_rhs) noexcept
Whether both conditions hold (scalar form).
Definition EBGeometry_SIMD.hpp:62
T select(const bool a_mask, const T a_ifTrue, const T a_ifFalse) noexcept
Pick a_ifTrue where a_mask holds, else a_ifFalse (scalar form).
Definition EBGeometry_SIMD.hpp:49
float sqrt(const float a_x) noexcept
Square root (scalar form).
Definition EBGeometry_SIMD.hpp:74
float abs(const float a_x) noexcept
Absolute value (scalar form).
Definition EBGeometry_SIMD.hpp:98
EBGEOMETRY_HOST_ALWAYS_INLINE V unsignedDistance2(const Source &a_triangle, const Vec3V< V > &a_point) noexcept
The squared distance from a point to a triangle, without its sign: the candidates of signedDistance()...
Definition EBGeometry_TriangleDistance.hpp:317
T closestPoint(const Source &a_triangle, const Vec3V< T > &a_point, Vec3V< T > &a_closestPoint, Vec3V< T > &a_normal) noexcept
The closest point on a triangle to a_point, the pseudonormal there, and the signed distance.
Definition EBGeometry_TriangleDistance.hpp:434
EBGEOMETRY_HOST_ALWAYS_INLINE V signedDistance(const Source &a_triangle, const Vec3V< V > &a_point) noexcept
Signed distance from a_point to a triangle, or to one triangle per lane.
Definition EBGeometry_TriangleDistance.hpp:152
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
constexpr Vec3T< T > cross(const Vec3T< T > &u, const Vec3T< T > &v) noexcept
Cross product of two 3D vectors.
T dot(const Vec2T< T > &u, const Vec2T< T > &v) noexcept
Dot product of two 2D vectors.
V type
The scalar type.
Definition EBGeometry_SIMD.hpp:177
A 3D vector of V, where V is a scalar or a SIMD::Pack: x, y and z each hold one lane per triangle.
Definition EBGeometry_TriangleDistance.hpp:39
V y
y components.
Definition EBGeometry_TriangleDistance.hpp:48
V z
z components.
Definition EBGeometry_TriangleDistance.hpp:53
V x
x components.
Definition EBGeometry_TriangleDistance.hpp:43