EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_PointCloudHashGrid.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_POINTCLOUDHASHGRID_HPP
13#define EBGEOMETRY_POINTCLOUDHASHGRID_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"
27#include "EBGeometry_Pool.hpp"
28#include "EBGeometry_Vec.hpp"
29
30namespace EBGeometry {
31
69template <class T>
71{
72public:
79
87 inline PointCloudHashGrid(Pool& a_pool, const std::vector<Vec3T<T>>& a_positions, T a_targetPerCell = T(1));
88
119 template <class Source>
120 [[nodiscard]] EBGEOMETRY_HOST
121 static inline PointCloudHashGrid
122 build(Pool& a_pool, const Source& a_source, uint32_t a_numPoints, T a_targetPerCell = T(1), uint32_t a_maxPoints = 0);
123
133 [[nodiscard]] EBGEOMETRY_HOST
134 static inline PointCloudHashGrid
135 build(Pool& a_pool, const std::vector<Vec3T<T>>& a_positions, T a_targetPerCell = T(1), uint32_t a_maxPoints = 0);
136
152 template <class Source>
154 inline void
155 rebuild(const Source& a_source, uint32_t a_numPoints, T a_targetPerCell = T(1));
156
161 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
162 inline uint32_t
163 maxPoints() const noexcept
164 {
165 return m_positions.m_capacity;
166 }
167
176 template <class Source>
177 struct CellKeys;
178
182 struct CellStarts;
183
189 template <class Source>
191
197 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
198 inline Hit
199 closestPoint(const Vec3T<T>& a_query) const noexcept;
200
209 inline std::size_t
210 closestPoints(const Vec3T<T>& a_query, std::size_t a_k, Hit* a_out) const noexcept;
211
217 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
218 inline Hit
219 nearestNeighbor(uint32_t a_point) const noexcept;
220
229 inline std::size_t
230 nearestNeighbors(uint32_t a_point, std::size_t a_k, Hit* a_out) const noexcept;
231
240 [[nodiscard]] EBGEOMETRY_HOST
241 inline std::vector<Hit>
242 allNearestNeighbors(std::size_t a_k = 1) const;
243
248 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
249 inline std::size_t
250 numPoints() const noexcept
251 {
252 return m_positions.size();
253 }
254
260 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
261 inline const Vec3T<T>&
262 position(uint32_t a_index) const noexcept
263 {
264 EBGEOMETRY_EXPECT(a_index < m_positions.size());
265
266 const void* base = this->base();
267
268 return m_positions.at(base, m_slotOf.at(base, a_index));
269 }
270
275 using PoolHandle::base;
276
284 [[nodiscard]] EBGEOMETRY_HOST
285 inline PointCloudHashGrid
286 rebasedView(const Pool& a_pool) const noexcept;
287
297 [[nodiscard]] EBGEOMETRY_HOST
298 inline PointCloudHashGrid
299 deepCopy(Pool& a_dstPool) const;
300
301private:
305 PointCloudHashGrid() noexcept = default;
306
314 inline void
315 reserveArrays(Pool& a_pool, uint32_t a_maxPoints, uint32_t a_maxCells);
316
325 template <class Source>
327 inline BoundingVolumes::AABBT<T>
328 boxOf(const Source& a_source, uint32_t a_numPoints, const char* a_who);
329
337 template <class Source>
339 inline void
340 fill(const Source& a_source, uint32_t a_numPoints);
341
354 inline void
355 setShape(const Vec3T<T>& a_lo,
356 const Vec3T<T>& a_hi,
357 std::size_t a_numPoints,
358 T a_targetPerCell,
359 std::size_t a_cellCap = Math::Limits<std::size_t>::max()) noexcept;
360
365 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
366 inline std::size_t
367 numCells() const noexcept
368 {
369 return std::size_t(m_nx) * std::size_t(m_ny) * std::size_t(m_nz);
370 }
371
379 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
380 inline int
381 cellCoord(T a_x, T a_lo, int a_n) const noexcept;
382
390 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
391 inline std::size_t
392 cellIndex(int a_ix, int a_iy, int a_iz) const noexcept;
393
399 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
400 inline std::size_t
401 cellIndexOf(const Vec3T<T>& a_p) const noexcept;
402
413 inline std::size_t
414 query(const Vec3T<T>& a_query, std::size_t a_k, Hit* a_out, uint32_t a_exclude) const noexcept;
415
419 PODVector<Vec3T<T>> m_positions;
420
424 PODVector<std::uint32_t> m_cellStart;
425
429 PODVector<std::uint32_t> m_cellPoints;
430
435
440
444 PODVector<Vec3T<T>> m_boxScratch;
445
449 bool m_parallel = false;
450
454 Vec3T<T> m_lo;
455
459 T m_h = T(1);
460
464 T m_invH = T(1);
465
469 int m_nx = 1, m_ny = 1, m_nz = 1;
470};
471
476static_assert(std::is_trivially_copyable_v<PointCloudHashGrid<float>>,
477 "PointCloudHashGrid<float> must be trivially copyable");
478static_assert(std::is_trivially_copyable_v<PointCloudHashGrid<double>>,
479 "PointCloudHashGrid<double> must be trivially copyable");
480
481} // namespace EBGeometry
482
483#include "EBGeometry_PointCloudHashGridBuildImplem.hpp"
484#include "EBGeometry_PointCloudHashGridImplem.hpp"
485
486#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:269
Scalar helpers and numeric limits that compile for both host and device.
Placement-independent POD array views for the GPU memory foundation.
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.
A uniform spatial grid specialized for point clouds, with a fast build and turnkey queries.
Definition EBGeometry_PointCloudHashGrid.hpp:71
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).
void rebuild(const Source &a_source, uint32_t a_numPoints, T a_targetPerCell=T(1))
Build the grid again, in the arrays build() reserved, from new positions.
PointCloudHashGrid deepCopy(Pool &a_dstPool) const
Duplicate this object's storage into a_dstPool.
const Vec3T< T > & position(uint32_t a_index) const noexcept
Position of the point with the given cloud index.
Definition EBGeometry_PointCloudHashGrid.hpp:262
Hit nearestNeighbor(uint32_t a_point) const noexcept
Nearest other point to a point already in the cloud.
std::size_t numPoints() const noexcept
Number of points in the cloud.
Definition EBGeometry_PointCloudHashGrid.hpp:250
static PointCloudHashGrid build(Pool &a_pool, const Source &a_source, uint32_t a_numPoints, T a_targetPerCell=T(1), uint32_t a_maxPoints=0)
Build a uniform grid over a point cloud, where the pool's memory is.
uint32_t maxPoints() const noexcept
The most points rebuild() can take.
Definition EBGeometry_PointCloudHashGrid.hpp:163
PointCloudHashGrid rebasedView(const Pool &a_pool) const noexcept
Produce a copy of this object that resolves against a_pool.
PointCloud::Hit< T > Hit
One query result: the cloud index of a matched point and its squared distance.
Definition EBGeometry_PointCloudHashGrid.hpp:78
PointCloudHashGrid(Pool &a_pool, const std::vector< Vec3T< T > > &a_positions, T a_targetPerCell=T(1))
Build a uniform grid over a point cloud: build(a_pool, a_positions, a_targetPerCell).
static PointCloudHashGrid build(Pool &a_pool, const std::vector< Vec3T< T > > &a_positions, T a_targetPerCell=T(1), uint32_t a_maxPoints=0)
Build a uniform grid over points on the host, where the pool's memory is; see the build() above.
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 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.
void * base() noexcept
The base address this object's arrays are offsets from; see PoolHandle::base().
Definition EBGeometry_Pool.hpp:639
Hit closestPoint(const Vec3T< T > &a_query) const noexcept
Closest cloud point to an arbitrary query point.
Base of every pool-resident handle: the one PoolLocation its arrays resolve against.
Definition EBGeometry_Pool.hpp:563
void * base() noexcept
The base address this handle's offsets resolve against; see PoolLocation::base().
Definition EBGeometry_Pool.hpp:639
Growable bump arena over a MemoryResource.
Definition EBGeometry_Pool.hpp:205
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:258
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
Vec2T< T > max(const Vec2T< T > &u, const Vec2T< T > &v) noexcept
Component-wise maximum of two vectors.
Placement-independent descriptor of a POD array inside a Pool.
Definition EBGeometry_PODVector.hpp:131
T & at(void *a_base, uint32_t a_i) const noexcept
Mutable element access against a_base.
Definition EBGeometry_PODVector.hpp:220
A build through GPU::parallelFor(): each slot's cloud index and position, in cell order,...
Definition EBGeometry_PointCloudHashGrid.hpp:190
A build through GPU::parallelFor(): the sort key of every point, (cell << 32) | cloud index,...
Definition EBGeometry_PointCloudHashGrid.hpp:177
One query result: the cloud index of a matched point and its squared distance.
Definition EBGeometry_PointCloud.hpp:53