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 <limits>
19#include <vector>
20
21// Our includes
22#include "EBGeometry_BVH.hpp"
26#include "EBGeometry_Vec.hpp"
27
28namespace EBGeometry {
29
56template <class T,
57 class Meta = std::size_t,
58 size_t K = BVH::DefaultBranchingRatio<T>(),
59 size_t W = PointSoA::DefaultWidth<T>()>
61 : public BVH::PackedBVH<T, PointAoSoA<T, std::size_t, W>, K, BVH::ValueStorage<PointAoSoA<T, std::size_t, W>>>
62{
63public:
68
73
77 using Node = typename Base::Node;
78
83
93 struct Hit
94 {
95 std::size_t index = 0;
96 T distanceSquared = std::numeric_limits<T>::max();
97 };
98
102 PointCloudBVH() = delete;
103
110 inline PointCloudBVH(const std::vector<Vec3T<T>>& a_positions,
111 const std::vector<Meta>& a_metadata,
112 std::size_t a_targetLeafSize = 16 * W);
113
119 [[nodiscard]] inline Hit
120 closestPoint(const Vec3T<T>& a_query) const noexcept;
121
129 inline std::size_t
130 closestPoints(const Vec3T<T>& a_query, std::size_t a_k, Hit* a_out) const noexcept;
131
137 [[nodiscard]] inline Hit
138 nearestNeighbor(std::size_t a_point) const noexcept;
139
147 inline std::size_t
148 nearestNeighbors(std::size_t a_point, std::size_t a_k, Hit* a_out) const noexcept;
149
160 [[nodiscard]] inline std::vector<Hit>
161 allNearestNeighbors(std::size_t a_k = 1) const;
162
170 [[nodiscard]] inline Hit
172
183 inline std::size_t
184 closestPointsBruteForce(const Vec3T<T>& a_query, std::size_t a_k, Hit* a_out) const;
185
193 [[nodiscard]] inline Hit
194 nearestNeighborBruteForce(std::size_t a_point) const noexcept;
195
206 inline std::size_t
207 nearestNeighborsBruteForce(std::size_t a_point, std::size_t a_k, Hit* a_out) const;
208
213 [[nodiscard]] inline std::size_t
215 {
216 return m_positions.size();
217 }
218
224 [[nodiscard]] inline const Vec3T<T>&
225 position(std::size_t a_index) const noexcept
226 {
227 return m_positions[a_index];
228 }
229
235 [[nodiscard]] inline const Meta&
236 metadata(std::size_t a_index) const noexcept
237 {
238 return m_metadata[a_index];
239 }
240
241private:
245 struct BuildResult
246 {
247 std::vector<Node> nodes;
248 std::vector<PointGroup> primitives;
249 std::vector<std::uint32_t> leafOff;
250 std::vector<std::uint32_t> leafCnt;
251 std::vector<std::uint32_t> order;
252 };
253
262 inline PointCloudBVH(BuildResult&& a_build,
263 const std::vector<Vec3T<T>>& a_positions,
264 const std::vector<Meta>& a_metadata);
265
275 static inline BuildResult
276 buildTree(const std::vector<Vec3T<T>>& a_positions, std::size_t a_leafSize);
277
289 inline void
290 query(const Vec3T<T>& a_query,
291 std::size_t a_k,
292 Hit* a_out,
293 std::size_t& a_found,
294 std::size_t a_exclude,
295 std::uint32_t a_seedOff,
296 std::uint32_t a_seedCnt) const noexcept;
297
305 [[nodiscard]] inline Hit
306 bruteForceOne(const Vec3T<T>& a_query, std::size_t a_exclude) const noexcept;
307
317 inline std::size_t
318 bruteForceK(const Vec3T<T>& a_query, std::size_t a_k, Hit* a_out, std::size_t a_exclude) const;
319
323 static constexpr std::size_t s_none = std::numeric_limits<std::size_t>::max();
324
328 std::vector<Vec3T<T>> m_positions;
329
333 std::vector<Meta> m_metadata;
334
338 std::vector<std::uint32_t> m_leafOff;
339
343 std::vector<std::uint32_t> m_leafCnt;
344
349 std::vector<std::uint32_t> m_order;
350};
351
352} // namespace EBGeometry
353
354#include "EBGeometry_PointCloudBVHImplem.hpp"
355
356#endif
Declaration of bounding volume hierarchy (BVH) classes.
Declarations of bounding volume types used in bounding volume hierarchies.
Declaration of a metadata-carrying wrapper around PointSoAT.
Declaration of a true SoA point-position group for SIMD nearest-neighbor evaluation.
Declaration of 2D and 3D point/vector classes with templated precision. Used with DCEL tools.
Forward declaration of the linearised BVH. Needed so that TreeBVH::pack() and TreeBVH::packWith() can...
Definition EBGeometry_BVH.hpp:1238
Axis-aligned bounding box (AABB) enclosing a set of 3D points.
Definition EBGeometry_BoundingVolumes.hpp:247
A PackedBVH specialized for point clouds, with a fast build and turnkey queries.
Definition EBGeometry_PointCloudBVH.hpp:62
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.
std::size_t closestPointsBruteForce(const Vec3T< T > &a_query, std::size_t a_k, Hit *a_out) const
Brute-force a_k closest points to an arbitrary query point (O(N) reference for closestPoints()).
PointCloudBVH(const std::vector< Vec3T< T > > &a_positions, const std::vector< Meta > &a_metadata, std::size_t a_targetLeafSize=16 *W)
Build a BVH over a point cloud.
const Vec3T< T > & position(std::size_t a_index) const noexcept
Position of the point with the given cloud index.
Definition EBGeometry_PointCloudBVH.hpp:225
Hit closestPoint(const Vec3T< T > &a_query) const noexcept
Closest cloud point to an arbitrary query point.
PointCloudBVH()=delete
No default construction – a point cloud is required.
Hit closestPointBruteForce(const Vec3T< T > &a_query) const noexcept
Brute-force closest point to an arbitrary query point (O(N) reference for closestPoint()).
std::size_t nearestNeighbors(std::size_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:214
std::size_t nearestNeighborsBruteForce(std::size_t a_point, std::size_t a_k, Hit *a_out) const
Brute-force a_k nearest other points to a cloud point (O(N) reference for nearestNeighbors()).
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).
Hit nearestNeighbor(std::size_t a_point) const noexcept
Nearest other point to a point already in the cloud (seed-from-own-leaf search).
typename Base::Node Node
Flat node type inherited from Base.
Definition EBGeometry_PointCloudBVH.hpp:77
const Meta & metadata(std::size_t a_index) const noexcept
User metadata of the point with the given cloud index.
Definition EBGeometry_PointCloudBVH.hpp:236
Hit nearestNeighborBruteForce(std::size_t a_point) const noexcept
Brute-force nearest other point to a cloud point (O(N) reference for nearestNeighbor()).
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:225
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_AnalyticDistanceFunctions.hpp:31
Compact BVH node stored in the flat node array.
Definition EBGeometry_BVH.hpp:1262
One query result: the cloud index of a matched point and its squared distance.
Definition EBGeometry_PointCloudBVH.hpp:94
std::size_t index
Cloud index of the matched point.
Definition EBGeometry_PointCloudBVH.hpp:95
T distanceSquared
Squared distance from the query to it.
Definition EBGeometry_PointCloudBVH.hpp:96