PointSampler library (C++)
Loading...
Searching...
No Matches
percolation_clustering.hpp
Go to the documentation of this file.
1/* Copyright (c) 2025 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4#pragma once
5#include <queue>
6
9
10namespace ps
11{
12
37template <typename T, size_t N>
38std::vector<int> percolation_clustering(const std::vector<Point<T, N>> &points,
40{
41 if (points.empty())
42 return {};
43
44 // KD-tree for neighbor search
46 KDTree<T, N> index(N, adaptor);
47 index.buildIndex();
48
49 std::vector<int> labels(points.size(), -1);
50 int current_cluster = 0;
51
52 for (size_t i = 0; i < points.size(); ++i)
53 {
54 if (labels[i] != -1)
55 continue; // already assigned
56
57 // Start BFS/DFS for new cluster
58 std::queue<size_t> q;
59 q.push(i);
61
62 while (!q.empty())
63 {
64 size_t p_idx = q.front();
65 q.pop();
66
67 // Radius search around p_idx
68 const Point<T, N> &p = points[p_idx];
69 std::array<T, N> query;
70 for (size_t d = 0; d < N; ++d)
71 query[d] = p[d];
72
73 std::vector<nanoflann::ResultItem<unsigned int, T>> ret_matches;
74 nanoflann::SearchParameters params;
75 index.radiusSearch(query.data(),
78 params);
79
80 for (auto &m : ret_matches)
81 {
82 size_t neighbor = m.first;
83 if (labels[neighbor] == -1)
84 {
86 q.push(neighbor);
87 }
88 }
89 }
90
92 }
93
94 return labels;
95}
96
97} // namespace ps
Definition dbscan_clustering.hpp:11
std::vector< Point< T, N > > random(size_t count, const std::array< std::pair< T, T >, N > &axis_ranges, std::optional< unsigned int > seed=std::nullopt)
Generates a specified number of uniformly distributed random points in N-dimensional space.
Definition random.hpp:66
std::vector< int > percolation_clustering(const std::vector< Point< T, N > > &points, T connection_radius)
Analyze percolation clusters from a set of points using a radius-based neighbor graph.
Definition percolation_clustering.hpp:38
nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Simple_Adaptor< T, PointCloudAdaptor< T, N > >, PointCloudAdaptor< T, N >, N > KDTree
Definition nanoflann_adaptator.hpp:35
Definition nanoflann_adaptator.hpp:13
A fixed-size N-dimensional point/vector class.
Definition point.hpp:39