EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_PointCloudBVH.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
12#ifndef EBGEOMETRY_POINTCLOUDBVH_HPP
13#define EBGEOMETRY_POINTCLOUDBVH_HPP
14
15// Std includes
16#include <cstddef>
17#include <cstdint>
18#include <type_traits>
19#include <vector>
20
21// Our includes
23#include "EBGeometry_GPU.hpp"
24#include "EBGeometry_Math.hpp"
30#include "EBGeometry_Pool.hpp"
31#include "EBGeometry_Vec.hpp"
32
33namespace EBGeometry {
34
70template <class T, size_t K = BVH::DefaultBranchingRatio<T>(), size_t W = PointSoA::DefaultWidth<T>()>
72{
73public:
78
83
87 using Node = typename Packed::Node;
88
93
100
104 PointCloudBVH() = delete;
105
116 inline PointCloudBVH(Pool& a_pool, const std::vector<Vec3T<T>>& a_positions, std::size_t a_targetLeafSize = 16 * W);
117
123 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
124 inline Hit
125 closestPoint(const Vec3T<T>& a_query) const noexcept;
126
135 inline std::size_t
136 closestPoints(const Vec3T<T>& a_query, std::size_t a_k, Hit* a_out) const noexcept;
137
143 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
144 inline Hit
145 nearestNeighbor(uint32_t a_point) const noexcept;
146
155 inline std::size_t
156 nearestNeighbors(uint32_t a_point, std::size_t a_k, Hit* a_out) const noexcept;
157
168 [[nodiscard]] EBGEOMETRY_HOST
169 inline std::vector<Hit>
170 allNearestNeighbors(std::size_t a_k = 1) const;
171
176 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
177 inline std::size_t
178 numPoints() const noexcept
179 {
180 return m_positions.size();
181 }
182
188 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
189 inline const Vec3T<T>&
190 position(uint32_t a_index) const noexcept
191 {
192 EBGEOMETRY_EXPECT(a_index < m_positions.size());
193
194 return m_positions.at(this->base(), a_index);
195 }
196
203 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
204 inline Packed
205 getBVH() const noexcept
206 {
207 return Packed(m_bvhLayout, this->location());
208 }
209
214 using PoolHandle::base;
215
230 [[nodiscard]] EBGEOMETRY_HOST
231 inline PointCloudBVH
232 rebasedView(const Pool& a_pool) const noexcept;
233
241 [[nodiscard]] EBGEOMETRY_HOST
242 inline PointCloudBVH
243 deepCopy(Pool& a_dstPool) const;
244
245private:
250 struct BuildResult
251 {
252 std::vector<Node> nodes;
253 std::vector<AABB> boxes;
254 std::vector<PointGroup> primitives;
255 std::vector<std::uint32_t> leafOff;
256 std::vector<std::uint32_t> leafCnt;
257 std::vector<std::uint32_t> order;
258 };
259
269 inline PointCloudBVH(Pool& a_pool, const BuildResult& a_build, const std::vector<Vec3T<T>>& a_positions);
270
282 inline void
283 storeCloud(Pool& a_pool,
284 const std::vector<Vec3T<T>>& a_positions,
285 const std::vector<std::uint32_t>& a_leafOff,
286 const std::vector<std::uint32_t>& a_leafCnt,
287 const std::vector<std::uint32_t>& a_order);
288
296 template <class U>
297 [[nodiscard]] EBGEOMETRY_HOST
298 static inline std::vector<U>
299 toHost(const PODVector<U>& a_array, const void* a_base);
300
311 static inline BuildResult
312 buildTree(const std::vector<Vec3T<T>>& a_positions, std::size_t a_leafSize);
313
327 inline std::size_t
328 query(const Vec3T<T>& a_query,
329 std::size_t a_k,
330 Hit* a_out,
331 uint32_t a_exclude,
332 std::uint32_t a_seedOff,
333 std::uint32_t a_seedCnt) const noexcept;
334
338 typename Packed::Layout m_bvhLayout;
339
343 PODVector<Vec3T<T>> m_positions;
344
348 PODVector<std::uint32_t> m_leafOff;
349
353 PODVector<std::uint32_t> m_leafCnt;
354
360};
361
366static_assert(std::is_trivially_copyable_v<PointCloudBVH<float>>, "PointCloudBVH<float> must be trivially copyable");
367static_assert(std::is_trivially_copyable_v<PointCloudBVH<double>>, "PointCloudBVH<double> must be trivially copyable");
368
369} // namespace EBGeometry
370
371#include "EBGeometry_PointCloudBVHImplem.hpp"
372
373#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
#define EBGEOMETRY_EXPECT(cond)
Runtime precondition assertion for EBGeometry.
Definition EBGeometry_Macros.hpp:155
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.
Query results and helpers shared by the point-cloud search structures.
Declaration of a true SoA point-position group for SIMD nearest-neighbor evaluation.
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:357
Axis-aligned bounding box (AABB) enclosing a set of 3D points.
Definition EBGeometry_BoundingVolumes.hpp:37
A BVH over a point cloud, with a fast build and turnkey queries.
Definition EBGeometry_PointCloudBVH.hpp:72
PointCloudBVH()=delete
No default construction – a point cloud is required.
PointCloudBVH(Pool &a_pool, const std::vector< Vec3T< T > > &a_positions, std::size_t a_targetLeafSize=16 *W)
Build a BVH over a point cloud.
BVH::PackedBVH< T, PointGroup, K > Packed
The packed BVH this class holds over its point groups.
Definition EBGeometry_PointCloudBVH.hpp:82
std::size_t closestPoints(const Vec3T< T > &a_query, std::size_t a_k, Hit *a_out) const noexcept
The a_k closest cloud points to an arbitrary query point, nearest first.
Hit nearestNeighbor(uint32_t a_point) const noexcept
Nearest other point to a point already in the cloud (seed-from-own-leaf search).
Hit closestPoint(const Vec3T< T > &a_query) const noexcept
Closest cloud point to an arbitrary query point.
const Vec3T< T > & position(uint32_t a_index) const noexcept
Position of the point with the given cloud index.
Definition EBGeometry_PointCloudBVH.hpp:190
std::vector< Hit > allNearestNeighbors(std::size_t a_k=1) const
For every point, its a_k nearest other points (the k-nearest-neighbor graph).
typename Packed::Node Node
Flat node type of the held BVH.
Definition EBGeometry_PointCloudBVH.hpp:87
Packed getBVH() const noexcept
The held packed BVH over the point groups.
Definition EBGeometry_PointCloudBVH.hpp:205
PointCloudBVH deepCopy(Pool &a_dstPool) const
Duplicate this object's storage into a_dstPool.
void * base() noexcept
The base address this object's arrays are offsets from; see PoolHandle::base().
Definition EBGeometry_Pool.hpp:575
PointCloudBVH rebasedView(const Pool &a_pool) const noexcept
Produce a copy of this object that resolves against a_pool.
std::size_t nearestNeighbors(uint32_t a_point, std::size_t a_k, Hit *a_out) const noexcept
The a_k nearest other points to a point already in the cloud, nearest first.
std::size_t numPoints() const noexcept
Number of points in the cloud.
Definition EBGeometry_PointCloudBVH.hpp:178
PointCloud::Hit< T > Hit
One query result: the cloud index of a matched point and its squared distance.
Definition EBGeometry_PointCloudBVH.hpp:99
Base of every pool-resident handle: the one PoolLocation its arrays resolve against.
Definition EBGeometry_Pool.hpp:499
PoolLocation location() const noexcept
Where this handle's arrays live.
Definition EBGeometry_Pool.hpp:507
void * base() noexcept
The base address this handle's offsets resolve against; see PoolLocation::base().
Definition EBGeometry_Pool.hpp:575
Growable bump arena over a MemoryResource.
Definition EBGeometry_Pool.hpp:183
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:258
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
Compact BVH node stored in the flat node array.
Definition EBGeometry_PackedBVH.hpp:381
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 query result: the cloud index of a matched point and its squared distance.
Definition EBGeometry_PointCloud.hpp:53