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: GPL-3.0-or-later
4
12#ifndef EBGEOMETRY_POINTCLOUDHASHGRID_HPP
13#define EBGEOMETRY_POINTCLOUDHASHGRID_HPP
14
15// Std includes
16#include <cstddef>
17#include <cstdint>
18#include <limits>
19#include <vector>
20
21// Our includes
22#include "EBGeometry_Vec.hpp"
23
24namespace EBGeometry {
25
53template <class T, class Meta = std::size_t>
55{
56public:
66 struct Hit
67 {
68 std::size_t index = 0;
69 T distanceSquared = std::numeric_limits<T>::max();
70 };
71
76
86 inline PointCloudHashGrid(const std::vector<Vec3T<T>>& a_positions,
87 const std::vector<Meta>& a_metadata,
88 T a_targetPerCell = T(1));
89
95 [[nodiscard]] inline Hit
96 closestPoint(const Vec3T<T>& a_query) const noexcept;
97
105 inline std::size_t
106 closestPoints(const Vec3T<T>& a_query, std::size_t a_k, Hit* a_out) const noexcept;
107
113 [[nodiscard]] inline Hit
114 nearestNeighbor(std::size_t a_point) const noexcept;
115
123 inline std::size_t
124 nearestNeighbors(std::size_t a_point, std::size_t a_k, Hit* a_out) const noexcept;
125
134 [[nodiscard]] inline std::vector<Hit>
135 allNearestNeighbors(std::size_t a_k = 1) const;
136
143 [[nodiscard]] inline Hit
145
155 inline std::size_t
156 closestPointsBruteForce(const Vec3T<T>& a_query, std::size_t a_k, Hit* a_out) const;
157
164 [[nodiscard]] inline Hit
165 nearestNeighborBruteForce(std::size_t a_point) const noexcept;
166
176 inline std::size_t
177 nearestNeighborsBruteForce(std::size_t a_point, std::size_t a_k, Hit* a_out) const;
178
183 [[nodiscard]] inline std::size_t
185 {
186 return m_positions.size();
187 }
188
194 [[nodiscard]] inline const Vec3T<T>&
195 position(std::size_t a_index) const noexcept
196 {
197 return m_positions[a_index];
198 }
199
205 [[nodiscard]] inline const Meta&
206 metadata(std::size_t a_index) const noexcept
207 {
208 return m_metadata[a_index];
209 }
210
211private:
219 [[nodiscard]] inline int
220 cellCoord(T a_x, T a_lo, int a_n) const noexcept;
221
229 [[nodiscard]] inline std::size_t
230 cellIndex(int a_ix, int a_iy, int a_iz) const noexcept;
231
237 [[nodiscard]] inline std::size_t
238 cellIndexOf(const Vec3T<T>& a_p) const noexcept;
239
248 inline void
249 query(
250 const Vec3T<T>& a_query, std::size_t a_k, Hit* a_out, std::size_t& a_found, std::size_t a_exclude) const noexcept;
251
258 [[nodiscard]] inline Hit
259 bruteForceOne(const Vec3T<T>& a_query, std::size_t a_exclude) const noexcept;
260
269 inline std::size_t
270 bruteForceK(const Vec3T<T>& a_query, std::size_t a_k, Hit* a_out, std::size_t a_exclude) const;
271
275 static constexpr std::size_t s_none = std::numeric_limits<std::size_t>::max();
276
280 std::vector<Vec3T<T>> m_positions;
281
285 std::vector<Meta> m_metadata;
286
290 Vec3T<T> m_lo;
291
295 T m_h = T(1);
296
300 T m_invH = T(1);
301
305 int m_nx = 1, m_ny = 1, m_nz = 1;
306
310 std::vector<std::uint32_t> m_cellStart;
311
315 std::vector<std::uint32_t> m_cellPoints;
316};
317
318} // namespace EBGeometry
319
320#include "EBGeometry_PointCloudHashGridImplem.hpp"
321
322#endif
Declaration of 2D and 3D point/vector classes with templated precision. Used with DCEL tools.
A uniform spatial grid specialized for point clouds, with a fast build and turnkey queries.
Definition EBGeometry_PointCloudHashGrid.hpp:55
PointCloudHashGrid()=delete
No default construction – a point cloud is required.
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 nearestNeighborBruteForce(std::size_t a_point) const noexcept
Brute-force nearest other point to a cloud point (O(N) reference for nearestNeighbor()).
Hit closestPoint(const Vec3T< T > &a_query) const noexcept
Closest cloud point to an arbitrary query point.
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()).
Hit nearestNeighbor(std::size_t a_point) const noexcept
Nearest other point to a point already in the cloud.
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::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.
const Meta & metadata(std::size_t a_index) const noexcept
User metadata of the point with the given cloud index.
Definition EBGeometry_PointCloudHashGrid.hpp:206
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_PointCloudHashGrid.hpp:184
const Vec3T< T > & position(std::size_t a_index) const noexcept
Position of the point with the given cloud index.
Definition EBGeometry_PointCloudHashGrid.hpp:195
PointCloudHashGrid(const std::vector< Vec3T< T > > &a_positions, const std::vector< Meta > &a_metadata, T a_targetPerCell=T(1))
Build a uniform grid over a point cloud.
Hit closestPointBruteForce(const Vec3T< T > &a_query) const noexcept
Brute-force closest point to an arbitrary query point (O(N) reference for closestPoint()).
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:225
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_AnalyticDistanceFunctions.hpp:31
One query result: the cloud index of a matched point and its squared distance.
Definition EBGeometry_PointCloudHashGrid.hpp:67
T distanceSquared
Squared distance from the query to it.
Definition EBGeometry_PointCloudHashGrid.hpp:69
std::size_t index
Cloud index of the matched point.
Definition EBGeometry_PointCloudHashGrid.hpp:68