PointSampler library (C++)
Loading...
Searching...
No Matches
relaxation.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 <cstddef>
6#include <optional>
7
10
11namespace ps
12{
13
62template <typename T, size_t N>
64 size_t k_neighbors = 8,
65 T step_size = T(0.1),
66 size_t iterations = 10)
67{
68 for (size_t iter = 0; iter < iterations; ++iter)
69 {
71 KDTree<T, N> index(N, adaptor);
72 index.buildIndex();
73
74 std::vector<Point<T, N>> new_points = points;
75
76 for (size_t i = 0; i < points.size(); ++i)
77 {
78 const auto &p = points[i];
79
80 std::vector<size_t> ret_indexes(k_neighbors + 1);
81 std::vector<T> out_dists_sqr(k_neighbors + 1);
82
83 nanoflann::KNNResultSet<T> result_set(k_neighbors + 1);
84 result_set.init(ret_indexes.data(), out_dists_sqr.data());
85
86 std::array<T, N> query;
87 for (size_t d = 0; d < N; ++d)
88 query[d] = p[d];
89
90 index.findNeighbors(result_set, query.data(), nanoflann::SearchParameters());
91
93 // skip self at j=0
94 for (size_t j = 1; j < result_set.size(); ++j)
95 {
96 const auto &q = points[ret_indexes[j]];
97 auto delta = p - q;
98 // avoid div by 0
100
102 }
103
105 }
106
107 points = std::move(new_points);
108 }
109}
110
111} // 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
T length_squared(const Point< T, N > &a)
Definition point.hpp:206
Point< T, N > normalized(const Point< T, N > &a)
Definition point.hpp:216
nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Simple_Adaptor< T, PointCloudAdaptor< T, N > >, PointCloudAdaptor< T, N >, N > KDTree
Definition nanoflann_adaptator.hpp:35
void relaxation_ktree(std::vector< Point< T, N > > &points, size_t k_neighbors=8, T step_size=T(0.1), size_t iterations=10)
Relax a point set using a k-nearest neighbor repulsion algorithm with a KD-tree.
Definition relaxation.hpp:63
Definition nanoflann_adaptator.hpp:13
A fixed-size N-dimensional point/vector class.
Definition point.hpp:39