EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_CloudBVH.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
11#ifndef EBGEOMETRY_CLOUDBVH_HPP
12#define EBGEOMETRY_CLOUDBVH_HPP
13
14// Std includes
15#include <cstddef>
16#include <cstdint>
17#include <type_traits>
18#include <vector>
19
20// Our includes
22#include "EBGeometry_GPU.hpp"
23#include "EBGeometry_Math.hpp"
29#include "EBGeometry_Pool.hpp"
30#include "EBGeometry_Vec.hpp"
31
32namespace EBGeometry {
33
34namespace PointCloudDetail {
35
41{
43 uint64_t m_code;
44
46 uint32_t m_index;
47
53 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
54 inline bool
55 operator<(const MortonKey& a_other) const noexcept
56 {
57 return m_code < a_other.m_code || (m_code == a_other.m_code && m_index < a_other.m_index);
58 }
59};
60
66{
68 uint32_t m_lo;
69
71 uint32_t m_hi;
72
74 uint32_t m_parent;
75
77 uint32_t m_slot;
78
80 uint32_t m_firstChild;
81
83 uint32_t m_subtree;
84
86 uint32_t m_depthFirst;
87};
88
99template <class T, size_t W>
101{
103 using Item = Vec3T<T>;
104
107
110
112 static constexpr const char* ClassName = "PointCloudBVH";
113
115 static constexpr const char* Singular = "point";
116
118 static constexpr const char* Plural = "points";
119
121 static constexpr const char* MaxParameter = "a_maxPoints";
122
128 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
129 static inline const Vec3T<T>&
130 center(const Item& a_item) noexcept
131 {
132 return a_item;
133 }
134
142 static inline void
143 grow(Vec3T<T>& a_lo, Vec3T<T>& a_hi, const Item& a_item) noexcept
144 {
145 a_lo = min(a_lo, a_item);
146 a_hi = max(a_hi, a_item);
147 }
148
155 static inline void
156 requireValid(const char* a_who, const std::vector<Item>& a_items) noexcept
157 {
158 requireValidCloud(a_who, a_items);
159 }
160
165 template <class Source>
167 static inline void
168 requireValidWhereBuilt(const Source&, uint32_t, bool, Vec3T<T>*, const char*) noexcept
169 {}
170};
171
178template <class Prim, class Source>
180{
182 Source m_source;
183
189 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
190 auto
191 operator()(const uint32_t a_index) const noexcept
192 {
193 return Prim::center(m_source(a_index));
194 }
195};
196
208template <class T, size_t K, size_t W, class Prim>
209class CloudBVH : public PoolHandle
210{
211public:
215 using Item = typename Prim::Item;
216
220 using Group = typename Prim::Group;
221
226
230 using Node = typename Packed::Node;
231
236
237protected:
241 CloudBVH() noexcept = default;
242
271
275 struct Sizes
276 {
278 std::uint32_t m_nodes;
279
281 std::uint32_t m_interior;
282
284 std::uint32_t m_groups;
285 };
286
297 template <class Source>
299 inline void
301 const Source& a_source,
302 uint32_t a_numItems,
303 std::size_t a_targetLeafSize,
304 uint32_t a_maxItems,
305 const char* a_who);
306
317 inline void
319 const std::vector<Item>& a_items,
320 std::size_t a_targetLeafSize,
321 uint32_t a_maxItems,
322 const char* a_who);
323
331 template <class Source>
333 inline void
334 reinitialize(const Source& a_source, uint32_t a_numItems, const char* a_who);
335
341 inline void
342 rebase(const Pool& a_pool) noexcept;
343
351 inline void
352 copyInto(CloudBVH& a_copy, Pool& a_dstPool) const;
353
361 [[nodiscard]] EBGEOMETRY_HOST
362 static inline Sizes
363 sizesFor(uint32_t a_numItems, uint32_t a_leafSize, const char* a_who) noexcept;
364
373 [[nodiscard]] EBGEOMETRY_HOST
374 static inline Scratch
375 reserveScratch(Pool& a_pool, uint32_t a_numItems, uint32_t a_leafSize, const char* a_who);
376
384 inline void
385 reserveArrays(Pool& a_pool, const Sizes& a_sizes, uint32_t a_numItems);
386
398 template <class Source>
400 inline Sizes
401 shape(const Scratch& a_scratch, void* a_base, const Source& a_source, uint32_t a_numItems, const char* a_who);
402
412 template <class Source>
414 inline void
415 write(const Scratch& a_scratch, void* a_base, const Sizes& a_sizes, const Source& a_source, uint32_t a_numItems);
416
423 template <class F>
425 inline void
426 run(std::size_t a_count, const F& a_f) const;
427
437 inline std::uint32_t
438 scan(std::uint32_t* a_values, std::uint32_t a_count, std::uint32_t* a_scratch) const;
439
447 template <class U>
448 [[nodiscard]] EBGEOMETRY_HOST
449 static inline std::vector<U>
450 toHost(const PODVector<U>& a_array, const void* a_base);
451
456
461
466
471
476
481
485 std::uint32_t m_maxItems = 0;
486
490 std::uint32_t m_leafSize = 0;
491
495 std::uint32_t m_depth = 0;
496
500 bool m_parallel = false;
501};
502
503} // namespace PointCloudDetail
504
505} // namespace EBGeometry
506
507#include "EBGeometry_CloudBVHImplem.hpp"
508
509#endif
Declarations of bounding volume types used in bounding volume hierarchies.
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.
Placement-independent POD array views for the GPU memory foundation.
The flattened, device-callable bounding volume hierarchy: PackedBVH, its node layout,...
Declaration of a PointSoAT group that carries point ids.
Internal helpers shared by PointCloudBVH and PointCloudHashGrid, and by their builds.
Query results and helpers shared by the point-cloud search structures.
Growable bump arena for the GPU memory foundation.
Declaration of 2D and 3D point/vector classes with templated precision, and free functions on them.
Forward declaration of the linearised BVH, defined below.
Definition EBGeometry_PackedBVH.hpp:365
Axis-aligned bounding box (AABB) enclosing a set of 3D points.
Definition EBGeometry_BoundingVolumes.hpp:37
The state and build of a linear BVH over a cloud of primitives (points or spheres),...
Definition EBGeometry_CloudBVH.hpp:210
PODVector< std::uint32_t > m_order
Cloud indices in leaf (build) order; the spatially coherent batch-query order.
Definition EBGeometry_CloudBVH.hpp:475
void write(const Scratch &a_scratch, void *a_base, const Sizes &a_sizes, const Source &a_source, uint32_t a_numItems)
Build step 6 (see PointCloudBVH): write the arrays reserved for the tree shape() gave.
std::uint32_t m_maxItems
The most primitives a rebuild can take, 0 for none.
Definition EBGeometry_CloudBVH.hpp:485
Sizes shape(const Scratch &a_scratch, void *a_base, const Source &a_source, uint32_t a_numItems, const char *a_who)
Build steps 1 to 5 (see PointCloudBVH): the box, the sorted keys, the tree's levels,...
static Sizes sizesFor(uint32_t a_numItems, uint32_t a_leafSize, const char *a_who) noexcept
Bounds on the array sizes of a build over a_numItems primitives.
std::uint32_t scan(std::uint32_t *a_values, std::uint32_t a_count, std::uint32_t *a_scratch) const
An exclusive scan, where the build runs; see GPU::exclusiveScan().
void initialize(Pool &a_pool, const std::vector< Item > &a_items, std::size_t a_targetLeafSize, uint32_t a_maxItems, const char *a_who)
Build over the primitives of a host array, where the pool's memory is: for a pool in device memory,...
void copyInto(CloudBVH &a_copy, Pool &a_dstPool) const
Duplicate this object's storage into a_dstPool, for a_copy, a copy of this object: the body of PointC...
void run(std::size_t a_count, const F &a_f) const
Run a build step: through GPU::parallelFor() for a build there, and in a host loop otherwise.
typename Packed::Node Node
Flat node type of the held BVH.
Definition EBGeometry_CloudBVH.hpp:230
PODVector< Item > m_items
The primitives, indexed by cloud index.
Definition EBGeometry_CloudBVH.hpp:460
std::uint32_t m_depth
The tree's depth in node levels (the root is level 1), 0 for an empty cloud.
Definition EBGeometry_CloudBVH.hpp:495
PODVector< std::uint32_t > m_leafOff
Per-primitive own-leaf group offset (for seeding).
Definition EBGeometry_CloudBVH.hpp:465
std::uint32_t m_leafSize
The largest leaf, max(a_targetLeafSize, K): a node of more primitives is cut.
Definition EBGeometry_CloudBVH.hpp:490
bool m_parallel
Whether the build runs through GPU::parallelFor() (the pool's memory is device-accessible).
Definition EBGeometry_CloudBVH.hpp:500
Scratch m_scratch
The scratch arrays a rebuild uses; empty for a BVH without room for one.
Definition EBGeometry_CloudBVH.hpp:480
typename Prim::Item Item
What a source of the primitives returns.
Definition EBGeometry_CloudBVH.hpp:215
void rebase(const Pool &a_pool) noexcept
Resolve this object against a_pool: the body of PointCloudBVH::rebasedView(), on a copy.
void reinitialize(const Source &a_source, uint32_t a_numItems, const char *a_who)
Build again in the arrays initialize() reserved: the body of PointCloudBVH::rebuild().
void reserveArrays(Pool &a_pool, const Sizes &a_sizes, uint32_t a_numItems)
Reserve the BVH's and the cloud's arrays from a_pool.
typename Prim::Group Group
The SoA leaf primitive: a group of up to W primitives carrying their cloud indices.
Definition EBGeometry_CloudBVH.hpp:220
static std::vector< U > toHost(const PODVector< U > &a_array, const void *a_base)
Copy one pool-resident array back into a host std::vector.
static Scratch reserveScratch(Pool &a_pool, uint32_t a_numItems, uint32_t a_leafSize, const char *a_who)
Reserve a build's scratch from a_pool, for up to a_numItems primitives.
PODVector< std::uint32_t > m_leafCnt
Per-primitive own-leaf group count (for seeding).
Definition EBGeometry_CloudBVH.hpp:470
CloudBVH() noexcept=default
An empty BVH, to be reserved and filled.
void initialize(Pool &a_pool, const Source &a_source, uint32_t a_numItems, std::size_t a_targetLeafSize, uint32_t a_maxItems, const char *a_who)
Build over the primitives of a source, where the pool's memory is: the body of PointCloudBVH::build()...
Packed::Layout m_bvhLayout
The packed BVH over the groups, as a layout: it shares this object's location.
Definition EBGeometry_CloudBVH.hpp:455
Base of every pool-resident handle: the one PoolLocation its arrays resolve against.
Definition EBGeometry_Pool.hpp:506
Growable bump arena over a MemoryResource.
Definition EBGeometry_Pool.hpp:188
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:258
void requireValidCloud(const char *a_who, const std::vector< Vec3T< T > > &a_positions) noexcept
Abort with a message unless a point cloud can be indexed.
Definition EBGeometry_PointCloudDetail.hpp:50
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
Vec2T< T > min(const Vec2T< T > &u, const Vec2T< T > &v) noexcept
Component-wise minimum of two vectors.
Vec2T< T > max(const Vec2T< T > &u, const Vec2T< T > &v) noexcept
Component-wise maximum of two vectors.
The BVH's arrays as offsets into its pool, and its root box: a PackedBVH minus its location.
Definition EBGeometry_PackedBVH.hpp:1135
Compact BVH node stored in the flat node array.
Definition EBGeometry_PackedBVH.hpp:389
Placement-independent descriptor of a POD array inside a Pool.
Definition EBGeometry_PODVector.hpp:119
A PointSoAT<T, W> group that also carries each point's cloud index.
Definition EBGeometry_PointAoSoA.hpp:44
One node of a cloud BVH build, in breadth-first order.
Definition EBGeometry_CloudBVH.hpp:66
uint32_t m_subtree
Nodes in the node's subtree, itself included.
Definition EBGeometry_CloudBVH.hpp:83
uint32_t m_parent
The parent's breadth-first index (0 for the root).
Definition EBGeometry_CloudBVH.hpp:74
uint32_t m_depthFirst
The node's index in the depth-first node array.
Definition EBGeometry_CloudBVH.hpp:86
uint32_t m_hi
One past the node's last primitive, in sorted order.
Definition EBGeometry_CloudBVH.hpp:71
uint32_t m_slot
The node's position among its parent's children.
Definition EBGeometry_CloudBVH.hpp:77
uint32_t m_lo
First of the node's primitives, in sorted order.
Definition EBGeometry_CloudBVH.hpp:68
uint32_t m_firstChild
The first child's breadth-first index (interior nodes).
Definition EBGeometry_CloudBVH.hpp:80
A source of primitive centers, from a source of primitives.
Definition EBGeometry_CloudBVH.hpp:180
Source m_source
The primitives.
Definition EBGeometry_CloudBVH.hpp:182
auto operator()(const uint32_t a_index) const noexcept
The center of a primitive.
Definition EBGeometry_CloudBVH.hpp:191
The scratch arrays of a build: in the BVH's pool for a BVH with room for a rebuild,...
Definition EBGeometry_CloudBVH.hpp:248
PODVector< Vec3T< T > > m_box
GPU::boundingBoxOf()'s scratch.
Definition EBGeometry_CloudBVH.hpp:253
PODVector< MortonKey > m_keys
The primitives' sort keys, a power of two of them (the padding sorts last).
Definition EBGeometry_CloudBVH.hpp:250
PODVector< std::uint32_t > m_groups
By depth-first index, each leaf's first group.
Definition EBGeometry_CloudBVH.hpp:266
PODVector< std::uint32_t > m_scan
GPU::exclusiveScan()'s scratch.
Definition EBGeometry_CloudBVH.hpp:269
PODVector< BuildNode > m_nodes
The build's nodes, in breadth-first order.
Definition EBGeometry_CloudBVH.hpp:256
PODVector< std::uint32_t > m_rows
A level's interior flags, scanned into ranks; then, by depth-first index, each interior node's row.
Definition EBGeometry_CloudBVH.hpp:263
PODVector< AABB > m_boxes
Each build node's box, indexed like m_nodes.
Definition EBGeometry_CloudBVH.hpp:259
The largest array sizes any cloud of a given size can need; see PointCloudBVH's build steps.
Definition EBGeometry_CloudBVH.hpp:276
std::uint32_t m_groups
Groups.
Definition EBGeometry_CloudBVH.hpp:284
std::uint32_t m_interior
Interior nodes, one child-box row each.
Definition EBGeometry_CloudBVH.hpp:281
std::uint32_t m_nodes
Nodes.
Definition EBGeometry_CloudBVH.hpp:278
A primitive's sort key in a cloud BVH build: its center's Morton code, ties broken by its cloud index...
Definition EBGeometry_CloudBVH.hpp:41
uint32_t m_index
The primitive's cloud index.
Definition EBGeometry_CloudBVH.hpp:46
uint64_t m_code
The Morton code of the primitive center's cell.
Definition EBGeometry_CloudBVH.hpp:43
bool operator<(const MortonKey &a_other) const noexcept
Whether this key sorts before a_other: by code, then by cloud index.
Definition EBGeometry_CloudBVH.hpp:55
The primitive of PointCloudBVH: a point, packed into PointAoSoA groups.
Definition EBGeometry_CloudBVH.hpp:101
static constexpr const char * MaxParameter
build()'s parameter giving the room for a rebuild, for the messages.
Definition EBGeometry_CloudBVH.hpp:121
static constexpr const char * ClassName
The class built over these primitives, for the messages.
Definition EBGeometry_CloudBVH.hpp:112
static void grow(Vec3T< T > &a_lo, Vec3T< T > &a_hi, const Item &a_item) noexcept
Grow a box to hold a primitive.
Definition EBGeometry_CloudBVH.hpp:143
static const Vec3T< T > & center(const Item &a_item) noexcept
The point a primitive's Morton key is taken at.
Definition EBGeometry_CloudBVH.hpp:130
static constexpr const char * Singular
One primitive, for the messages.
Definition EBGeometry_CloudBVH.hpp:115
static void requireValid(const char *a_who, const std::vector< Item > &a_items) noexcept
Abort with a message unless the primitives of a host array can be built over.
Definition EBGeometry_CloudBVH.hpp:156
static void requireValidWhereBuilt(const Source &, uint32_t, bool, Vec3T< T > *, const char *) noexcept
Check, where the build runs, what the box of the centers does not: nothing for a point.
Definition EBGeometry_CloudBVH.hpp:168
static constexpr const char * Plural
Primitives, for the messages.
Definition EBGeometry_CloudBVH.hpp:118
A source of point positions for the builds that take one (PointCloudHashGrid::build()): an array of V...
Definition EBGeometry_PointCloud.hpp:227