EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_SphereCloudBVH.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
12#ifndef EBGEOMETRY_SPHERECLOUDBVH_HPP
13#define EBGEOMETRY_SPHERECLOUDBVH_HPP
14
15// Std includes
16#include <cmath>
17#include <cstddef>
18#include <cstdint>
19#include <type_traits>
20#include <vector>
21
22// Our includes
27#include "EBGeometry_Pool.hpp"
31#include "EBGeometry_Vec.hpp"
32
33namespace EBGeometry {
34
35namespace PointCloudDetail {
36
44template <class T, class Source>
46{
48 Source m_source;
49
55 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
57 operator()(const uint32_t a_index) const noexcept
58 {
59 const T radius = m_source(a_index).m_radius;
60
61 return Vec3T<T>(radius, radius, radius);
62 }
63};
64
72template <class T, size_t W>
74{
77
80
83
85 static constexpr const char* ClassName = "SphereCloudBVH";
86
88 static constexpr const char* Singular = "sphere";
89
91 static constexpr const char* Plural = "spheres";
92
94 static constexpr const char* MaxParameter = "a_maxSpheres";
95
101 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
102 static inline const Vec3T<T>&
103 center(const Item& a_item) noexcept
104 {
105 return a_item.m_center;
106 }
107
115 static inline void
116 grow(Vec3T<T>& a_lo, Vec3T<T>& a_hi, const Item& a_item) noexcept
117 {
118 const Vec3T<T> radius = a_item.m_radius * Vec3T<T>::ones();
119
120 a_lo = min(a_lo, a_item.m_center - radius);
121 a_hi = max(a_hi, a_item.m_center + radius);
122 }
123
131 static inline void
132 requireValid(const char* a_who, const std::vector<Item>& a_items) noexcept
133 {
134 for (std::size_t i = 0; i < a_items.size(); i++) {
135 const Item& sphere = a_items[i];
136
137 EBGEOMETRY_REQUIRE(std::isfinite(sphere.m_center[0]) && std::isfinite(sphere.m_center[1]) &&
138 std::isfinite(sphere.m_center[2]),
139 "%s: sphere %zu of %zu has a non-finite coordinate",
140 a_who,
141 i,
142 a_items.size());
143 EBGEOMETRY_REQUIRE(std::isfinite(sphere.m_radius) && sphere.m_radius >= T(0),
144 "%s: sphere %zu of %zu has a non-finite or negative radius",
145 a_who,
146 i,
147 a_items.size());
148 }
149 }
150
168 template <class Source>
170 static inline void
171 requireValidWhereBuilt(const Source& a_source,
172 const uint32_t a_numItems,
173 const bool a_parallel,
174 Vec3T<T>* a_scratch,
175 const char* a_who) noexcept
176 {
177 if (a_numItems == 0) {
178 return;
179 }
180
181 if (a_parallel) {
182 const BoundingVolumes::AABBT<T> box =
183 GPU::boundingBoxOf<T>(RadiiAsPoints<T, Source>{a_source}, a_numItems, a_scratch);
184
185 const T smallest = box.getLowCorner()[0];
186 const T largest = box.getHighCorner()[0];
187
188 EBGEOMETRY_REQUIRE(std::isfinite(smallest) && std::isfinite(largest) && smallest >= T(0),
189 "%s: a sphere has a non-finite or negative radius",
190 a_who);
191
192 return;
193 }
194
195 for (uint32_t i = 0; i < a_numItems; i++) {
196 const T radius = a_source(i).m_radius;
197
198 EBGEOMETRY_REQUIRE(std::isfinite(radius) && radius >= T(0),
199 "%s: sphere %u of %u has a non-finite or negative radius",
200 a_who,
201 unsigned(i),
202 unsigned(a_numItems));
203 }
204 }
205};
206
207} // namespace PointCloudDetail
208
234template <class T, size_t K = BVH::DefaultBranchingRatio<T>(), size_t W = PointSoA::DefaultWidth<T>()>
235class SphereCloudBVH : public PointCloudDetail::CloudBVH<T, K, W, PointCloudDetail::SpherePrimitive<T, W>>
236{
237public:
242
247
252
257
262
300
311 inline SphereCloudBVH(const Layout& a_layout, const PoolLocation& a_location) noexcept;
312
317 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
318 inline Layout
319 layout() const noexcept;
320
329 inline SphereCloudBVH(Pool& a_pool, const std::vector<Sphere>& a_spheres, std::size_t a_targetLeafSize = 16 * W);
330
341 inline SphereCloudBVH(Pool& a_pool,
342 const std::vector<Vec3T<T>>& a_centers,
343 const std::vector<T>& a_radii,
344 std::size_t a_targetLeafSize = 16 * W);
345
366 template <class Source>
367 [[nodiscard]] EBGEOMETRY_HOST
368 static inline SphereCloudBVH
369 build(Pool& a_pool,
370 const Source& a_source,
371 uint32_t a_numSpheres,
372 std::size_t a_targetLeafSize = 16 * W,
373 uint32_t a_maxSpheres = 0);
374
384 [[nodiscard]] EBGEOMETRY_HOST
385 static inline SphereCloudBVH
386 build(Pool& a_pool,
387 const std::vector<Sphere>& a_spheres,
388 std::size_t a_targetLeafSize = 16 * W,
389 uint32_t a_maxSpheres = 0);
390
402 [[nodiscard]] EBGEOMETRY_HOST
403 static inline SphereCloudBVH
404 build(Pool& a_pool,
405 const std::vector<Vec3T<T>>& a_centers,
406 const std::vector<T>& a_radii,
407 std::size_t a_targetLeafSize = 16 * W,
408 uint32_t a_maxSpheres = 0);
409
421 template <class Source>
423 inline void
424 rebuild(const Source& a_source, uint32_t a_numSpheres);
425
430 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
431 inline uint32_t
432 maxSpheres() const noexcept;
433
439 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
440 inline T
441 signedDistance(const Vec3T<T>& a_point) const noexcept;
442
449 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
450 inline Hit
451 closestSphere(const Vec3T<T>& a_point) const noexcept;
452
459 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
460 inline SurfaceRef<T>
461 closestSurface(const Vec3T<T>& a_point) const noexcept;
462
469 [[nodiscard]] EBGEOMETRY_HOST
470 inline uint32_t
471 getNumSurfaces() const noexcept;
472
486 template <class Visit>
488 inline void
489 forEachContact(uint32_t a_index, T a_gap, Visit&& a_visit) const noexcept;
490
503 template <class Visit>
505 inline void
506 forEachContact(const Sphere& a_sphere,
507 T a_gap,
508 Visit&& a_visit,
509 uint32_t a_exclude = SphereCloud::InvalidIndex) const noexcept;
510
515 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
516 inline AABB
517 computeBoundingVolume() const noexcept;
518
523 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
524 inline uint32_t
525 numSpheres() const noexcept;
526
532 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
533 inline Sphere
534 sphere(uint32_t a_index) const noexcept;
535
540 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
541 inline Packed
542 getBVH() const noexcept;
543
551 [[nodiscard]] EBGEOMETRY_HOST
552 inline SphereCloudBVH
553 rebasedView(const Pool& a_pool) const noexcept;
554
560 [[nodiscard]] EBGEOMETRY_HOST
561 inline SphereCloudBVH
562 deepCopy(Pool& a_dstPool) const;
563
568 using PoolHandle::base;
569
570private:
574 using Base = PointCloudDetail::CloudBVH<T, K, W, PointCloudDetail::SpherePrimitive<T, W>>;
575
576 using Base::m_bvhLayout;
577 using Base::m_depth;
578 using Base::m_items;
579 using Base::m_leafCnt;
580 using Base::m_leafOff;
581 using Base::m_maxItems;
582 using Base::m_order;
583
587 SphereCloudBVH() noexcept = default;
588};
589
594static_assert(std::is_trivially_copyable_v<SphereCloudBVH<float>>, "SphereCloudBVH<float> must be trivially copyable");
595static_assert(std::is_trivially_copyable_v<SphereCloudBVH<double>>,
596 "SphereCloudBVH<double> must be trivially copyable");
597
598} // namespace EBGeometry
599
600#include "EBGeometry_SphereCloudBVHImplem.hpp"
601
602// Every translation unit that sees SphereCloudBVH sees its ray form too, so firstHit() on it is the same function
603// everywhere.
605
606#endif
Declarations of bounding volume types used in bounding volume hierarchies.
The linear BVH build shared by PointCloudBVH and SphereCloudBVH, over a cloud of points or spheres.
Building blocks for builds that run with GPU::parallelFor(): a bounding-box reduction,...
#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
#define EBGEOMETRY_REQUIRE(cond,...)
Always-on check of user input and one-time host setup.
Definition EBGeometry_Macros.hpp:200
The flattened, device-callable bounding volume hierarchy: PackedBVH, its node layout,...
Growable bump arena for the GPU memory foundation.
Declaration of a PointAoSoA group with a radius lane: a group of spheres carrying their ids.
The ray form (see RayForm) of SphereCloudBVH: its BVH walked along the ray (PackedBVH::traverse() wit...
The sphere, query result and sphere sources of SphereCloudBVH.
SurfaceRef: which surface of a shape decides its value at a point, and the container protocol that na...
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:371
Axis-aligned bounding box (AABB) enclosing a set of 3D points.
Definition EBGeometry_BoundingVolumes.hpp:37
Vec3T< T > & getLowCorner() noexcept
Get a modifiable reference to the low corner of the AABB.
Vec3T< T > & getHighCorner() noexcept
Get a modifiable reference to the high corner of the AABB.
The state and build of a linear BVH over a cloud of primitives (points or spheres),...
Definition EBGeometry_CloudBVH.hpp:192
CloudBVH() noexcept=default
An empty BVH, to be reserved and filled.
Base of every pool-resident handle: the one PoolLocation its arrays resolve against.
Definition EBGeometry_Pool.hpp:573
Growable bump arena over a MemoryResource.
Definition EBGeometry_Pool.hpp:210
A BVH over spheres of differing radii (polydisperse particles), with the signed distance to their uni...
Definition EBGeometry_SphereCloudBVH.hpp:236
T signedDistance(const Vec3T< T > &a_point) const noexcept
Signed distance to the union of the spheres: min_i(|p - c_i| - r_i); see the class.
Hit closestSphere(const Vec3T< T > &a_point) const noexcept
The sphere whose surface is nearest to a point, in the signed sense of signedDistance().
void forEachContact(uint32_t a_index, T a_gap, Visit &&a_visit) const noexcept
Visit every sphere in contact with sphere a_index: each sphere j != a_index whose separation |c_i - c...
SphereCloudBVH(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.
SphereCloudBVH deepCopy(Pool &a_dstPool) const
Duplicate this object's storage into a_dstPool.
SphereCloudBVH rebasedView(const Pool &a_pool) const noexcept
Produce a copy of this object that resolves against a_pool.
AABB computeBoundingVolume() const noexcept
The box of all the spheres.
uint32_t numSpheres() const noexcept
Number of spheres in the cloud.
uint32_t getNumSurfaces() const noexcept
How many surfaces the cloud numbers: one per cloud index it can hold (maxSpheres(),...
void rebuild(const Source &a_source, uint32_t a_numSpheres)
Build the BVH again, in the arrays build() reserved, from new spheres.
Packed getBVH() const noexcept
The held packed BVH over the sphere groups.
SurfaceRef< T > closestSurface(const Vec3T< T > &a_point) const noexcept
The sphere whose surface decides the cloud's value at a point, as a container names it (see hasSurfac...
void * base() noexcept
The base address this object's arrays are offsets from; see PoolHandle::base().
Definition EBGeometry_Pool.hpp:649
Sphere sphere(uint32_t a_index) const noexcept
The sphere with a cloud index.
static SphereCloudBVH build(Pool &a_pool, const Source &a_source, uint32_t a_numSpheres, std::size_t a_targetLeafSize=16 *W, uint32_t a_maxSpheres=0)
Build a BVH over spheres, where the pool's memory is.
uint32_t maxSpheres() const noexcept
The most spheres rebuild() can take.
Layout layout() const noexcept
The BVH's arrays without its location, for storing it inside another pool-resident object.
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:258
static constexpr Vec3T< T > ones() noexcept
Return a vector with x = y = z = 1.
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:1142
Placement-independent descriptor of a POD array inside a Pool.
Definition EBGeometry_PODVector.hpp:131
A source of points that are a sphere's radius in every coordinate: GPU::boundingBoxOf() over it finds...
Definition EBGeometry_SphereCloudBVH.hpp:46
Source m_source
The spheres.
Definition EBGeometry_SphereCloudBVH.hpp:48
Vec3T< T > operator()(const uint32_t a_index) const noexcept
The radius of a sphere, in every coordinate.
Definition EBGeometry_SphereCloudBVH.hpp:57
The primitive of SphereCloudBVH: a sphere, keyed at its center and packed into SphereAoSoA groups; se...
Definition EBGeometry_SphereCloudBVH.hpp:74
static void requireValid(const char *a_who, const std::vector< Item > &a_items) noexcept
Abort with a message unless the spheres of a host array can be built over: a finite center,...
Definition EBGeometry_SphereCloudBVH.hpp:132
static constexpr const char * Singular
One primitive, for the messages.
Definition EBGeometry_SphereCloudBVH.hpp:88
static const Vec3T< T > & center(const Item &a_item) noexcept
The point a sphere's Morton key is taken at.
Definition EBGeometry_SphereCloudBVH.hpp:103
static constexpr const char * ClassName
The class built over these primitives, for the messages.
Definition EBGeometry_SphereCloudBVH.hpp:85
static constexpr const char * Plural
Primitives, for the messages.
Definition EBGeometry_SphereCloudBVH.hpp:91
static void requireValidWhereBuilt(const Source &a_source, const uint32_t a_numItems, const bool a_parallel, Vec3T< T > *a_scratch, const char *a_who) noexcept
Check every radius, where the build runs: finite, and at least 0.
Definition EBGeometry_SphereCloudBVH.hpp:171
static void grow(Vec3T< T > &a_lo, Vec3T< T > &a_hi, const Item &a_item) noexcept
Grow a box to hold a sphere.
Definition EBGeometry_SphereCloudBVH.hpp:116
static constexpr const char * MaxParameter
build()'s parameter giving the room for a rebuild, for the messages.
Definition EBGeometry_SphereCloudBVH.hpp:94
Where a pool-resident descriptor resolves its offsets, and the rules for moving it.
Definition EBGeometry_Pool.hpp:99
A group of up to W spheres carrying their cloud indices: a PointAoSoA<T, W> of the centers,...
Definition EBGeometry_SphereAoSoA.hpp:37
The BVH's arrays as offsets into its pool, with no location (see PoolHandle).
Definition EBGeometry_SphereCloudBVH.hpp:269
PODVector< uint32_t > m_leafCnt
Per-sphere number of groups in its own leaf.
Definition EBGeometry_SphereCloudBVH.hpp:288
uint32_t m_depth
The tree's depth in node levels, which rebasedView() checks against a device's traversal stack.
Definition EBGeometry_SphereCloudBVH.hpp:298
PODVector< uint32_t > m_leafOff
Per-sphere offset of its own leaf's first group.
Definition EBGeometry_SphereCloudBVH.hpp:283
PODVector< Sphere > m_spheres
The spheres, by cloud index.
Definition EBGeometry_SphereCloudBVH.hpp:278
Packed::Layout m_bvh
The packed BVH over the sphere groups.
Definition EBGeometry_SphereCloudBVH.hpp:273
PODVector< uint32_t > m_order
Cloud indices in leaf (build) order.
Definition EBGeometry_SphereCloudBVH.hpp:293
A source of spheres reading one array of Sphere<T>, indexed by cloud index.
Definition EBGeometry_SphereCloud.hpp:86
One query result: the cloud index of a sphere and the signed distance to its surface.
Definition EBGeometry_SphereCloud.hpp:62
A sphere of a sphere cloud: a center and a radius.
Definition EBGeometry_SphereCloud.hpp:48
Vec3T< T > m_center
The center.
Definition EBGeometry_SphereCloud.hpp:49
T m_radius
The radius, at least 0.
Definition EBGeometry_SphereCloud.hpp:50
The surface that decides a shape's value at a point, as closestSurface() reports it.
Definition EBGeometry_SurfaceRef.hpp:39