EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_TriangleSoA.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
11#ifndef EBGEOMETRY_TRIANGLESOA_HPP
12#define EBGEOMETRY_TRIANGLESOA_HPP
13
14// Std includes
15#include <cstddef>
16#include <cstdint>
17#include <type_traits>
18
19// Our includes
20#include "EBGeometry_Array.hpp"
21#include "EBGeometry_SIMD.hpp"
24#include "EBGeometry_Vec.hpp"
25
26namespace EBGeometry {
27
31namespace TriangleSoA {
32
50template <typename T>
51[[nodiscard]] constexpr size_t
52HostWidth() noexcept
53{
54 static_assert(std::is_floating_point_v<T>, "EBGeometry::TriangleSoA::HostWidth requires a floating-point T");
55#if defined(__AVX512F__)
56 if constexpr (std::is_same_v<T, double>) {
57 return 8;
58 }
59 else {
60 return 16;
61 }
62#elif defined(__AVX__)
63 if constexpr (std::is_same_v<T, double>) {
64 return 4;
65 }
66 else {
67 return 8;
68 }
69#else
70 return 4;
71#endif
72}
73
105template <typename T>
106[[nodiscard]] constexpr size_t
107DefaultWidth() noexcept
108{
109 static_assert(std::is_floating_point_v<T>, "EBGeometry::TriangleSoA::DefaultWidth requires a floating-point T");
110
111#if defined(EBGEOMETRY_HOST_TUNED_DEFAULTS)
112 return HostWidth<T>();
113#else
114 return 4;
115#endif
116}
117
118} // namespace TriangleSoA
119
140template <class T, size_t W>
142{
143 static_assert(W > 0, "W must be positive");
144 static_assert(std::is_floating_point_v<T>, "TriangleSoAT requires a floating-point type T");
145
146public:
155 void
156 pack(const Triangle<T>* tris, uint32_t count) noexcept;
157
167 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
168 T
169 signedDistance(const Vec3T<T>& a_point) const noexcept;
170
184 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
186 signedDistances(const Vec3T<T>& a_point) const noexcept;
187
198 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
199 uint32_t
200 nearestLane(const Vec3T<T>& a_point, T& a_d2) const noexcept;
201
209 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
210 T
211 signedDistanceAt(uint32_t a_lane, const Vec3T<T>& a_point) const noexcept;
212
222 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
223 T
224 closestPointAt(uint32_t a_lane, const Vec3T<T>& a_point, Vec3T<T>& a_closestPoint, Vec3T<T>& a_normal) const noexcept;
225
238 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
239 T
241 const Vec3T<T>& a_origin, const Vec3T<T>& a_direction, T a_tMin, T a_tMax, uint32_t& a_lane) const noexcept;
242
255 void
257 const Vec3T<T>& a_origin, const Vec3T<T>& a_direction, T a_tMin, T a_tMax, T* a_crossings) const noexcept;
258
264 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
266 getNormal(uint32_t a_lane) const noexcept;
267
275 template <class BV>
276 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
277 BV
278 computeBoundingVolume() const noexcept;
279
286 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
287 inline Vec3T<T>
288 getVertex(size_t a_lane, size_t a_vertex) const noexcept
289 {
290 return Vec3T<T>(m_vx[a_vertex][a_lane], m_vy[a_vertex][a_lane], m_vz[a_vertex][a_lane]);
291 }
292
293protected:
303 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
304 T
305 signedDistanceLane(uint32_t a_lane, const Vec3T<T>& a_point) const noexcept;
306
313 template <class V>
315 {
320
324 size_t m_offset;
325
331 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
332 V
333 fetch(const T* a_lanes) const noexcept
334 {
335 if constexpr (std::is_same_v<V, T>) {
336 return a_lanes[m_offset];
337 }
338 else {
339 return V::load(a_lanes + m_offset);
340 }
341 }
342
348 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
350 vertex(const size_t a_k) const noexcept
351 {
352 return {fetch(m_group->m_vx[a_k]), fetch(m_group->m_vy[a_k]), fetch(m_group->m_vz[a_k])};
353 }
354
359 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
361 normal() const noexcept
362 {
364 }
365
371 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
373 vertexNormal(const size_t a_k) const noexcept
374 {
375 return {fetch(m_group->m_vnx[a_k]), fetch(m_group->m_vny[a_k]), fetch(m_group->m_vnz[a_k])};
376 }
377
383 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
385 edgeNormal(const size_t a_k) const noexcept
386 {
387 return {fetch(m_group->m_enx[a_k]), fetch(m_group->m_eny[a_k]), fetch(m_group->m_enz[a_k])};
388 }
389 };
390
399 template <size_t N>
401 void
402 signedDistancesSIMD(const Vec3T<T>& a_point, T* a_distances) const noexcept;
403
410 template <size_t N>
412 void
413 unsignedDistances2SIMD(const Vec3T<T>& a_point, T* a_d2) const noexcept;
414
426 template <size_t N>
428 void
430 const Vec3T<T>& a_origin, const Vec3T<T>& a_direction, T a_tMin, T a_tMax, T* a_crossings) const noexcept;
431
435 alignas(W * sizeof(T)) T m_vx[3][W];
436
440 T m_vy[3][W];
441
445 T m_vz[3][W];
446
450 alignas(W * sizeof(T)) T m_nx[W];
451
455 T m_ny[W];
456
460 T m_nz[W];
461
465 alignas(W * sizeof(T)) T m_vnx[3][W];
466
470 T m_vny[3][W];
471
475 T m_vnz[3][W];
476
480 alignas(W * sizeof(T)) T m_enx[3][W];
481
485 T m_eny[3][W];
486
490 T m_enz[3][W];
491
498 uint32_t m_validCount = 0;
499};
500
501static_assert(std::is_trivially_copyable_v<TriangleSoAT<float, 4>>, "TriangleSoAT<float> must be trivially copyable");
502static_assert(std::is_trivially_copyable_v<TriangleSoAT<double, 4>>, "TriangleSoAT<double> must be trivially copyable");
503
504} // namespace EBGeometry
505
506#include "EBGeometry_TriangleSoAImplem.hpp"
507
508#endif
A fixed-size array that compiles for both host and device.
#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
#define EBGEOMETRY_HOST
Mark a function as callable from host code only (no-op on host compilers).
Definition EBGeometry_GPU.hpp:83
Thin wrappers over SSE4.1, AVX and AVX-512 registers, so that one kernel template serves every SIMD w...
The triangle distance kernels, written once for a scalar and for every SIMD::Pack.
Declaration of a triangle class with signed distance functionality.
Declaration of 2D and 3D point/vector classes with templated precision, and free functions on them.
Triangle class with signed distance functionality.
Definition EBGeometry_Triangle.hpp:52
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:258
constexpr size_t HostWidth() noexcept
The SoA width W (triangles per TriangleSoAT group) that fills one SIMD register for type T under the ...
Definition EBGeometry_TriangleSoA.hpp:52
constexpr size_t DefaultWidth() noexcept
The default SoA width W (triangles per TriangleSoAT group): 4, or HostWidth<T>() in a build that defi...
Definition EBGeometry_TriangleSoA.hpp:107
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
Fixed-size array with host- and device-callable members.
Definition EBGeometry_Array.hpp:33
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
The triangle accessor TriangleDistance::signedDistance() reads lanes [m_offset, m_offset + lanes of V...
Definition EBGeometry_TriangleSoA.hpp:315
TriangleDistance::Vec3V< V > vertexNormal(const size_t a_k) const noexcept
Vertex pseudonormal.
Definition EBGeometry_TriangleSoA.hpp:373
TriangleDistance::Vec3V< V > edgeNormal(const size_t a_k) const noexcept
Edge pseudonormal.
Definition EBGeometry_TriangleSoA.hpp:385
TriangleDistance::Vec3V< V > vertex(const size_t a_k) const noexcept
Vertex position.
Definition EBGeometry_TriangleSoA.hpp:350
TriangleDistance::Vec3V< V > normal() const noexcept
Face normal.
Definition EBGeometry_TriangleSoA.hpp:361
V fetch(const T *a_lanes) const noexcept
One value per lane, from the array starting at a_lanes.
Definition EBGeometry_TriangleSoA.hpp:333
const TriangleSoAT * m_group
The group.
Definition EBGeometry_TriangleSoA.hpp:319
size_t m_offset
First lane.
Definition EBGeometry_TriangleSoA.hpp:324
SoA (Structure of Arrays) layout for W triangles, enabling SIMD evaluation.
Definition EBGeometry_TriangleSoA.hpp:142
void pack(const Triangle< T > *tris, uint32_t count) noexcept
Pack count triangles from tris[0..count-1] into this SoA group.
T signedDistanceAt(uint32_t a_lane, const Vec3T< T > &a_point) const noexcept
One lane's signed distance: on the host, bit for bit the value signedDistances() gives that lane; on ...
T m_vz[3][W]
z-coordinates of vertex positions. m_vz[i][j] = z-coord of vertex i for triangle j.
Definition EBGeometry_TriangleSoA.hpp:445
void rayCrossings(const Vec3T< T > &a_origin, const Vec3T< T > &a_direction, T a_tMin, T a_tMax, T *a_crossings) const noexcept
Where a ray crosses each triangle in this group.
T m_enz[3][W]
z-components of edge normals. m_enz[i][j] = z of edge normal i for triangle j.
Definition EBGeometry_TriangleSoA.hpp:490
T m_nx[W]
x-components of face normals. m_nx[j] = x-component of the face normal for triangle j.
Definition EBGeometry_TriangleSoA.hpp:450
void signedDistancesSIMD(const Vec3T< T > &a_point, T *a_distances) const noexcept
Signed distances to the lanes below m_validCount, N at a time, with SIMD::Pack<T, N>.
uint32_t m_validCount
Number of valid (non-padded) triangles in this group (1..W).
Definition EBGeometry_TriangleSoA.hpp:498
T m_vnz[3][W]
z-components of vertex normals. m_vnz[i][j] = z of vertex normal i for triangle j.
Definition EBGeometry_TriangleSoA.hpp:475
T rayCrossing(const Vec3T< T > &a_origin, const Vec3T< T > &a_direction, T a_tMin, T a_tMax, uint32_t &a_lane) const noexcept
Where a ray first crosses a triangle in this group.
T closestPointAt(uint32_t a_lane, const Vec3T< T > &a_point, Vec3T< T > &a_closestPoint, Vec3T< T > &a_normal) const noexcept
The closest point on one lane's triangle, and the pseudonormal there.
void unsignedDistances2SIMD(const Vec3T< T > &a_point, T *a_d2) const noexcept
Every lane's squared unsigned distance with SIMD packs of N lanes, as signedDistancesSIMD().
T signedDistanceLane(uint32_t a_lane, const Vec3T< T > &a_point) const noexcept
Signed distance from a_point to the single triangle stored in lane a_lane.
T m_ny[W]
y-components of face normals.
Definition EBGeometry_TriangleSoA.hpp:455
T m_enx[3][W]
x-components of edge normals. m_enx[i][j] = x of edge normal i for triangle j.
Definition EBGeometry_TriangleSoA.hpp:480
T m_vy[3][W]
y-coordinates of vertex positions. m_vy[i][j] = y-coord of vertex i for triangle j.
Definition EBGeometry_TriangleSoA.hpp:440
T m_nz[W]
z-components of face normals.
Definition EBGeometry_TriangleSoA.hpp:460
void rayCrossingsSIMD(const Vec3T< T > &a_origin, const Vec3T< T > &a_direction, T a_tMin, T a_tMax, T *a_crossings) const noexcept
Ray crossings of the lanes below m_validCount, N at a time, with SIMD::Pack<T, N>.
T m_vx[3][W]
x-coordinates of vertex positions. m_vx[i][j] = x-coord of vertex i for triangle j.
Definition EBGeometry_TriangleSoA.hpp:435
BV computeBoundingVolume() const noexcept
Compute the bounding volume enclosing all valid triangles in this group.
T m_eny[3][W]
y-components of edge normals. m_eny[i][j] = y of edge normal i for triangle j.
Definition EBGeometry_TriangleSoA.hpp:485
T m_vny[3][W]
y-components of vertex normals. m_vny[i][j] = y of vertex normal i for triangle j.
Definition EBGeometry_TriangleSoA.hpp:470
Vec3T< T > getVertex(size_t a_lane, size_t a_vertex) const noexcept
Vertex a_vertex of the triangle in lane a_lane.
Definition EBGeometry_TriangleSoA.hpp:288
Vec3T< T > getNormal(uint32_t a_lane) const noexcept
The unit normal of one lane's triangle.
T m_vnx[3][W]
x-components of vertex normals. m_vnx[i][j] = x of vertex normal i for triangle j.
Definition EBGeometry_TriangleSoA.hpp:465
Array< T, W > signedDistances(const Vec3T< T > &a_point) const noexcept
Signed distances from a_point to every one of the W lane triangles.
T signedDistance(const Vec3T< T > &a_point) const noexcept
Evaluate signed distance from a_point to the closest triangle in this group.
uint32_t nearestLane(const Vec3T< T > &a_point, T &a_d2) const noexcept
The lane nearest a point and its squared distance, without a sign.