EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_MeshDistanceFunctions.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
11#ifndef EBGEOMETRY_MESHDISTANCEFUNCTIONS_HPP
12#define EBGEOMETRY_MESHDISTANCEFUNCTIONS_HPP
13
14// Std includes
15#include <cstddef>
16#include <cstdint>
17#include <memory>
18#include <type_traits>
19#include <utility>
20#include <vector>
21
22// Our includes
23#include "EBGeometry_BVH.hpp"
27#include "EBGeometry_GPU.hpp"
28#include "EBGeometry_Math.hpp"
29#include "EBGeometry_Pool.hpp"
33#include "EBGeometry_Vec.hpp"
34
35namespace EBGeometry {
36
44template <class T>
46{
48 uint32_t faceId = UINT32_MAX;
49};
50
77template <class T>
78class FlatMeshSDF : public PoolHandle
79{
80 static_assert(std::is_floating_point_v<T>, "FlatMeshSDF requires a floating-point T");
81
82public:
88
93
97 FlatMeshSDF() = delete;
98
102 struct Layout
103 {
108 };
109
119 FlatMeshSDF(const Layout& a_layout, const PoolLocation& a_location) noexcept
120 : PoolHandle(a_location), m_meshLayout(a_layout.m_mesh)
121 {}
122
127 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
128 Layout
129 layout() const noexcept
130 {
131 return Layout{m_meshLayout};
132 }
133
145 inline FlatMeshSDF(const Mesh& a_mesh, Pool& a_pool) noexcept;
146
152 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
153 inline T
154 signedDistance(const Vec3T<T>& a_point) const noexcept;
155
165 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
166 inline ClosestFace<T>
167 getClosestFace(const Vec3T<T>& a_point) const noexcept;
168
173 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
174 inline Mesh
175 getMesh() const noexcept
176 {
177 return Mesh(m_meshLayout, this->location());
178 }
179
187 [[nodiscard]] EBGEOMETRY_HOST
188 inline FlatMeshSDF
189 rebasedView(const Pool& a_pool) const noexcept;
190
198 [[nodiscard]] EBGEOMETRY_HOST
199 inline FlatMeshSDF
200 deepCopy(Pool& a_dstPool) const;
201
208 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
210 computeBoundingVolume() const noexcept;
211
212private:
216 typename Mesh::Layout m_meshLayout;
217};
218
223static_assert(std::is_trivially_copyable_v<FlatMeshSDF<float>>, "FlatMeshSDF<float> must be trivially copyable");
224static_assert(std::is_trivially_copyable_v<FlatMeshSDF<double>>, "FlatMeshSDF<double> must be trivially copyable");
225
247template <class T, size_t K>
248class MeshSDF : public PoolHandle
249{
250 static_assert(std::is_floating_point_v<T>, "MeshSDF requires a floating-point T");
251 static_assert(K >= 2, "MeshSDF requires branching factor K >= 2");
252
253public:
259
264
269
274
278 using Node = typename Root::Node;
279
283 MeshSDF() = delete;
284
291 struct Layout
292 {
296 typename Mesh::Layout m_mesh;
297
301 typename Root::Layout m_bvh;
302 };
303
313 MeshSDF(const Layout& a_layout, const PoolLocation& a_location) noexcept
314 : PoolHandle(a_location), m_meshLayout(a_layout.m_mesh), m_bvhLayout(a_layout.m_bvh)
315 {}
316
321 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
322 Layout
323 layout() const noexcept
324 {
325 return Layout{m_meshLayout, m_bvhLayout};
326 }
327
336 [[nodiscard]] EBGEOMETRY_HOST
337 static inline BVH::ConstructionOptions
339
354 inline MeshSDF(const Mesh& a_mesh, Pool& a_pool, const BVH::Construction a_construction);
355
366 inline MeshSDF(const Mesh& a_mesh,
367 Pool& a_pool,
368 const BVH::Construction a_construction,
369 const BVH::ConstructionOptions& a_options);
370
376 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
377 inline T
378 signedDistance(const Vec3T<T>& a_point) const noexcept;
379
388 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
389 inline ClosestFace<T>
390 getClosestFace(const Vec3T<T>& a_point) const noexcept;
391
407 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
408 inline Root
409 getRoot() const noexcept
410 {
411 return Root(m_bvhLayout, this->location());
412 }
413
418 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
419 inline Mesh
420 getMesh() const noexcept
421 {
422 return Mesh(m_meshLayout, this->location());
423 }
424
429 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
431 computeBoundingVolume() const noexcept
432 {
433 return this->getRoot().getBoundingVolume();
434 }
435
443 [[nodiscard]] EBGEOMETRY_HOST
444 inline MeshSDF
445 rebasedView(const Pool& a_pool) const noexcept;
446
455 [[nodiscard]] EBGEOMETRY_HOST
456 inline MeshSDF
457 deepCopy(Pool& a_dstPool) const;
458
459private:
468 [[nodiscard]] EBGEOMETRY_HOST
469 static inline Root
470 buildBVH(const Mesh& a_mesh,
471 Pool& a_pool,
472 const BVH::Construction a_construction,
473 const BVH::ConstructionOptions& a_options);
474
479 typename Mesh::Layout m_meshLayout;
480
484 typename Root::Layout m_bvhLayout;
485};
486
513template <class T, size_t K, size_t W>
514class TriMeshSDF : public PoolHandle
515{
516 static_assert(std::is_floating_point_v<T>, "TriMeshSDF<T,K,W> requires a floating-point T");
517 static_assert(K >= 2, "TriMeshSDF requires branching factor K >= 2");
518 static_assert(W > 0, "TriMeshSDF requires SoA width W > 0");
519
520public:
525 static constexpr DistanceQuality distanceQuality = DistanceQuality::Exact;
526
531
535 using Tri = typename EBGeometry::Triangle<T>;
536
541
546
550 TriMeshSDF() = delete;
551
555 struct Layout
556 {
560 typename Root::Layout m_bvh;
561 };
562
572 TriMeshSDF(const Layout& a_layout, const PoolLocation& a_location) noexcept
573 : PoolHandle(a_location), m_bvhLayout(a_layout.m_bvh)
574 {}
575
580 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
581 Layout
582 layout() const noexcept
583 {
584 return Layout{m_bvhLayout};
585 }
586
597 [[nodiscard]] EBGEOMETRY_HOST
598 static inline BVH::ConstructionOptions
599 defaultConstructionOptions(const size_t a_maxLeafGroups) noexcept;
600
625 inline TriMeshSDF(const Mesh& a_mesh,
626 Pool& a_pool,
627 const BVH::Construction a_construction,
628 const size_t a_maxLeafGroups);
629
640 inline TriMeshSDF(const std::vector<Tri>& a_triangles,
641 Pool& a_pool,
642 const BVH::Construction a_construction,
643 const size_t a_maxLeafGroups);
644
657 inline TriMeshSDF(const Mesh& a_mesh,
658 Pool& a_pool,
659 const BVH::Construction a_construction,
660 const BVH::ConstructionOptions& a_options);
661
670 inline TriMeshSDF(const std::vector<Tri>& a_triangles,
671 Pool& a_pool,
672 const BVH::Construction a_construction,
673 const BVH::ConstructionOptions& a_options);
674
680 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
681 inline T
682 signedDistance(const Vec3T<T>& a_point) const noexcept;
683
695 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
696 inline ClosestFace<T>
697 getClosestFace(const Vec3T<T>& a_point) const noexcept;
698
703 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
704 inline Root
705 getRoot() const noexcept
706 {
707 return Root(m_bvhLayout, this->location());
708 }
709
714 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
716 computeBoundingVolume() const noexcept
717 {
718 return this->getRoot().getBoundingVolume();
719 }
720
728 [[nodiscard]] EBGEOMETRY_HOST
729 inline TriMeshSDF
730 rebasedView(const Pool& a_pool) const noexcept;
731
738 [[nodiscard]] EBGEOMETRY_HOST
739 inline TriMeshSDF
740 deepCopy(Pool& a_dstPool) const;
741
742private:
748 [[nodiscard]] EBGEOMETRY_HOST
749 static inline std::vector<Tri>
750 extractTriangles(const Mesh& a_mesh);
751
760 [[nodiscard]] EBGEOMETRY_HOST
761 static inline Root
762 buildBVH(const std::vector<Tri>& a_triangles,
763 Pool& a_pool,
764 const BVH::Construction a_construction,
765 const BVH::ConstructionOptions& a_options);
766
776 [[nodiscard]] EBGEOMETRY_HOST
777 static inline Root
778 buildClusterSAH(const std::vector<Tri>& a_triangles, Pool& a_pool, const BVH::ClusterSpec& a_spec);
779
787 template <class GetTriangle>
788 [[nodiscard]] EBGEOMETRY_HOST
789 static inline std::vector<TriAoSoA>
790 groupTriangles(const GetTriangle& a_getTriangle, uint32_t a_count);
791
801 [[nodiscard]] EBGEOMETRY_HOST
802 static std::vector<TriAoSoA>
803 groupTrianglesIntoSoA(const std::vector<std::shared_ptr<const Tri>>& a_triangles,
804 uint32_t a_offset,
805 uint32_t a_count);
806
812 explicit TriMeshSDF(const Root& a_bvh) noexcept : PoolHandle(a_bvh.location()), m_bvhLayout(a_bvh.layout())
813 {}
814
819 typename Root::Layout m_bvhLayout;
820};
821
826static_assert(std::is_trivially_copyable_v<MeshSDF<float, 4>>, "MeshSDF<float, 4> must be trivially copyable");
827static_assert(std::is_trivially_copyable_v<MeshSDF<double, 4>>, "MeshSDF<double, 4> must be trivially copyable");
828static_assert(std::is_trivially_copyable_v<TriMeshSDF<float, 4, 4>>,
829 "TriMeshSDF<float, 4, 4> must be trivially copyable");
830static_assert(std::is_trivially_copyable_v<TriMeshSDF<double, 4, 4>>,
831 "TriMeshSDF<double, 4, 4> must be trivially copyable");
832
833} // namespace EBGeometry
834
835#include "EBGeometry_MeshDistanceFunctionsImplem.hpp"
836
837// Every translation unit that sees TriMeshSDF sees its ray form too, so firstHit() on it is the same
838// function everywhere.
840
841#endif
Bounding volume hierarchies: everything in EBGeometry_PackedBVH.hpp, EBGeometry_TreeBVH....
Declarations of bounding volume types used in bounding volume hierarchies.
Declaration of a mesh class which stores a DCEL mesh (with signed distance functions)
How far a function's value can be trusted as a distance: DistanceQuality, and the distanceQualityOf t...
GPU portability layer: host/device decoration macros and backend detection.
#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
Scalar helpers and numeric limits that compile for both host and device.
The ray forms (see RayForm) of the mesh SDFs. TriMeshSDF: its BVH walked along the ray (PackedBVH::tr...
Growable bump arena for the GPU memory foundation.
Declaration of a face-id-carrying wrapper around TriangleSoAT.
Declaration of SoA triangle group for SIMD signed-distance evaluation.
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.
Axis-aligned bounding box (AABB) enclosing a set of 3D points.
Definition EBGeometry_BoundingVolumes.hpp:37
Face class for navigating a DCEL mesh.
Definition EBGeometry_DCEL_Face.hpp:68
DCEL mesh class - stores a doubly-connected edge mesh.
Definition EBGeometry_DCEL_Mesh.hpp:102
Signed distance function for a DCEL mesh. Does not use BVHs.
Definition EBGeometry_MeshDistanceFunctions.hpp:79
FlatMeshSDF(const Layout &a_layout, const PoolLocation &a_location) noexcept
Rebuild a handle from a stored layout and the location of the pool that holds it.
Definition EBGeometry_MeshDistanceFunctions.hpp:119
T signedDistance(const Vec3T< T > &a_point) const noexcept
Compute the signed distance from a_point to the mesh.
Mesh getMesh() const noexcept
Get the underlying DCEL mesh descriptor.
Definition EBGeometry_MeshDistanceFunctions.hpp:175
ClosestFace< T > getClosestFace(const Vec3T< T > &a_point) const noexcept
Find the face closest to a_point, and the signed distance to it.
Layout layout() const noexcept
The object's arrays without its location, for storing it inside another pool-resident object.
Definition EBGeometry_MeshDistanceFunctions.hpp:129
FlatMeshSDF(const Mesh &a_mesh, Pool &a_pool) noexcept
Full constructor.
EBGeometry::DCEL::MeshT< T > Mesh
Alias for DCEL mesh type.
Definition EBGeometry_MeshDistanceFunctions.hpp:92
FlatMeshSDF rebasedView(const Pool &a_pool) const noexcept
Produce a copy of this object that resolves against a_pool.
static constexpr DistanceQuality distanceQuality
How far signedDistance() can be trusted as a distance: the Euclidean distance to a watertight mesh.
Definition EBGeometry_MeshDistanceFunctions.hpp:87
FlatMeshSDF deepCopy(Pool &a_dstPool) const
Duplicate the mesh's storage into a_dstPool.
FlatMeshSDF()=delete
Disallowed constructor.
EBGeometry::BoundingVolumes::AABBT< T > computeBoundingVolume() const noexcept
Compute the AABB enclosing the entire mesh.
A FlatMeshSDF must be trivially copyable: that is what lets a rebasedView() be byte-copied into a dev...
Definition EBGeometry_MeshDistanceFunctions.hpp:249
Mesh getMesh() const noexcept
Get the DCEL mesh descriptor the BVH's faces resolve against.
Definition EBGeometry_MeshDistanceFunctions.hpp:420
Layout layout() const noexcept
The object's arrays without its location, for storing it inside another pool-resident object.
Definition EBGeometry_MeshDistanceFunctions.hpp:323
typename Root::Node Node
Alias for a single linearized node.
Definition EBGeometry_MeshDistanceFunctions.hpp:278
MeshSDF(const Layout &a_layout, const PoolLocation &a_location) noexcept
Rebuild a handle from a stored layout and the location of the pool that holds it.
Definition EBGeometry_MeshDistanceFunctions.hpp:313
static BVH::ConstructionOptions defaultConstructionOptions() noexcept
The leaf-size settings the constructor without options uses.
EBGeometry::BoundingVolumes::AABBT< T > computeBoundingVolume() const noexcept
Compute the AABB enclosing the entire mesh.
Definition EBGeometry_MeshDistanceFunctions.hpp:431
MeshSDF rebasedView(const Pool &a_pool) const noexcept
Produce a copy of this object that resolves against a_pool.
typename EBGeometry::DCEL::MeshT< T > Mesh
Alias for DCEL mesh type.
Definition EBGeometry_MeshDistanceFunctions.hpp:268
MeshSDF deepCopy(Pool &a_dstPool) const
Duplicate the mesh's and the BVH's storage into a_dstPool.
MeshSDF()=delete
Default disallowed constructor.
typename EBGeometry::DCEL::FaceT< T > Face
Alias for DCEL face type.
Definition EBGeometry_MeshDistanceFunctions.hpp:263
Base of every pool-resident handle: the one PoolLocation its arrays resolve against.
Definition EBGeometry_Pool.hpp:506
PoolLocation location() const noexcept
Where this handle's arrays live.
Definition EBGeometry_Pool.hpp:514
PoolHandle() noexcept=default
An unset location; the first reservation attaches it.
Growable bump arena over a MemoryResource.
Definition EBGeometry_Pool.hpp:188
Signed distance function for a pure triangle mesh using SoA-grouped primitives in a compact (lineariz...
Definition EBGeometry_MeshDistanceFunctions.hpp:515
typename EBGeometry::BVH::PackedBVH< T, TriAoSoA, K > Root
Alias for which BVH root node.
Definition EBGeometry_MeshDistanceFunctions.hpp:545
TriMeshSDF(const std::vector< Tri > &a_triangles, Pool &a_pool, const BVH::Construction a_construction, const size_t a_maxLeafGroups)
Full constructor. Takes the input triangles and creates the BVH.
TriMeshSDF(const std::vector< Tri > &a_triangles, Pool &a_pool, const BVH::Construction a_construction, const BVH::ConstructionOptions &a_options)
Full constructor from a triangle soup, with leaf-size settings.
TriMeshSDF()=delete
Default disallowed constructor.
TriMeshSDF(const Mesh &a_mesh, Pool &a_pool, const BVH::Construction a_construction, const size_t a_maxLeafGroups)
Full constructor. Extracts flat triangles from a DCEL mesh, then builds the BVH.
typename EBGeometry::Triangle< T > Tri
Alias for the flat triangle type the BVH is built from.
Definition EBGeometry_MeshDistanceFunctions.hpp:535
EBGeometry::BoundingVolumes::AABBT< T > computeBoundingVolume() const noexcept
Compute the AABB enclosing the entire triangle mesh.
Definition EBGeometry_MeshDistanceFunctions.hpp:716
Layout layout() const noexcept
The object's arrays without its location, for storing it inside another pool-resident object.
Definition EBGeometry_MeshDistanceFunctions.hpp:582
T signedDistance(const Vec3T< T > &a_point) const noexcept
Compute the signed distance from a_point to the triangle mesh.
TriMeshSDF rebasedView(const Pool &a_pool) const noexcept
Produce a copy of this object that resolves against a_pool.
static BVH::ConstructionOptions defaultConstructionOptions(const size_t a_maxLeafGroups) noexcept
The leaf-size settings the constructors taking a_maxLeafGroups use.
TriMeshSDF(const Layout &a_layout, const PoolLocation &a_location) noexcept
Rebuild a handle from a stored layout and the location of the pool that holds it.
Definition EBGeometry_MeshDistanceFunctions.hpp:572
TriMeshSDF(const Mesh &a_mesh, Pool &a_pool, const BVH::Construction a_construction, const BVH::ConstructionOptions &a_options)
Full constructor from a DCEL mesh, with leaf-size settings.
Root getRoot() const noexcept
Get the PackedBVH storing SoA triangle groups.
Definition EBGeometry_MeshDistanceFunctions.hpp:705
ClosestFace< T > getClosestFace(const Vec3T< T > &a_point) const noexcept
Find the face closest to a_point, and the signed distance to it.
TriMeshSDF deepCopy(Pool &a_dstPool) const
Duplicate the BVH's storage into a_dstPool.
Triangle class with signed distance functionality.
Definition EBGeometry_Triangle.hpp:52
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:258
Construction
The preset BVH construction methods that the library's own BVH users accept.
Definition EBGeometry_PackedBVH.hpp:65
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
DistanceQuality
How far the value of a signed distance function can be trusted, from strongest to weakest.
Definition EBGeometry_DistanceQuality.hpp:25
@ Exact
The value's magnitude is the Euclidean distance to the surface.
Configuration for the ClusterSAH direct PackedBVH construction path.
Definition EBGeometry_PackedBVH.hpp:94
Leaf-size settings for the preset construction methods, one per family of methods.
Definition EBGeometry_PackedBVH.hpp:115
The face of a mesh closest to a query point, as the mesh SDFs' getClosestFace() returns it.
Definition EBGeometry_MeshDistanceFunctions.hpp:46
uint32_t faceId
Id of the closest face; UINT32_MAX for an empty mesh.
Definition EBGeometry_MeshDistanceFunctions.hpp:48
T signedDistance
Signed distance to the closest face; negative inside.
Definition EBGeometry_MeshDistanceFunctions.hpp:47
The mesh's arrays as offsets into its pool, with no location: what another pool-resident object store...
Definition EBGeometry_DCEL_Mesh.hpp:159
The object's arrays as offsets into its pool, with no location (see PoolHandle).
Definition EBGeometry_MeshDistanceFunctions.hpp:103
Mesh::Layout m_mesh
The mesh.
Definition EBGeometry_MeshDistanceFunctions.hpp:107
Numeric limits, with the meanings of the std::numeric_limits members of the same names.
Definition EBGeometry_Math.hpp:91
The object's arrays as offsets into its pool, with no location (see PoolHandle).
Definition EBGeometry_MeshDistanceFunctions.hpp:292
Mesh::Layout m_mesh
The mesh.
Definition EBGeometry_MeshDistanceFunctions.hpp:296
Root::Layout m_bvh
The BVH over the mesh's face ids.
Definition EBGeometry_MeshDistanceFunctions.hpp:301
Where a pool-resident descriptor resolves its offsets, and the rules for moving it.
Definition EBGeometry_Pool.hpp:99
The object's arrays as offsets into its pool, with no location (see PoolHandle).
Definition EBGeometry_MeshDistanceFunctions.hpp:556
Root::Layout m_bvh
The BVH over SoA triangle groups.
Definition EBGeometry_MeshDistanceFunctions.hpp:560
Face-id-carrying wrapper around a single TriangleSoAT<T, W>.
Definition EBGeometry_TriangleAoSoA.hpp:50