PointSampler library (C++)
Loading...
Searching...
No Matches
random_rejection_filter.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
8
9#include <iostream>
10
11namespace ps
12{
13
36template <typename T, std::size_t N>
37std::vector<Point<T, N>> random_rejection_filter(const std::vector<Point<T, N>> &points,
38 std::size_t target_count)
39{
40 if (target_count >= points.size())
41 {
42 return points; // nothing to remove
43 }
44
45 std::vector<std::size_t> indices(points.size());
46 std::iota(indices.begin(), indices.end(), 0);
47
48 std::random_device rd;
49 std::mt19937 gen(rd());
50 std::shuffle(indices.begin(), indices.end(), gen);
51
52 // Take the first `target_count` shuffled indices
53 std::vector<Point<T, N>> result;
54 result.reserve(target_count);
55 for (std::size_t i = 0; i < target_count; ++i)
56 {
57 result.push_back(points[indices[i]]);
58 }
59
60 return result;
61}
62
83template <typename T, std::size_t N>
84std::vector<Point<T, N>> random_rejection_filter(const std::vector<Point<T, N>> &points,
85 float keep_fraction)
86{
87 assert(keep_fraction >= 0.0 && keep_fraction <= 1.0);
88 std::size_t target_count = static_cast<std::size_t>(keep_fraction * points.size());
90}
91
92} // 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< Point< T, N > > random_rejection_filter(const std::vector< Point< T, N > > &points, std::size_t target_count)
Randomly retains a fixed number of points from the input set.
Definition random_rejection_filter.hpp:37
A fixed-size N-dimensional point/vector class.
Definition point.hpp:39