PointSampler library (C++)
Loading...
Searching...
No Matches
range.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 <fstream>
6#include <sstream>
7#include <string>
8#include <vector>
9
11
12namespace ps
13{
14
38template <typename T, size_t N>
39std::vector<Point<T, N>> filter_points_in_range(
40 const std::vector<Point<T, N>> &points,
41 const std::array<std::pair<T, T>, N> &axis_ranges)
42{
43 std::vector<Point<T, N>> filtered;
44 filtered.reserve(points.size());
45
46 for (const auto &p : points)
47 {
48 bool inside = true;
49 for (size_t i = 0; i < N; ++i)
50 {
51 const auto &[min_val, max_val] = axis_ranges[i];
52 if (p[i] < min_val || p[i] > max_val)
53 {
54 inside = false;
55 break;
56 }
57 }
58 if (inside)
59 filtered.push_back(p);
60 }
61
62 return filtered;
63}
64
92template <typename T, std::size_t N, typename Func>
93std::vector<Point<T, N>> filter_points_function(const std::vector<Point<T, N>> &points,
94 Func fn)
95{
96 std::vector<Point<T, N>> filtered;
97 filtered.reserve(points.size());
98
99 for (const auto &p : points)
100 {
101 if (fn(p) != T(0)) // keep point if function value is not zero
102 filtered.push_back(p);
103 }
104
105 return filtered;
106}
107
134template <typename T, size_t N>
136 const std::array<std::pair<T, T>, N> &target_ranges)
137{
138 if (points.empty())
139 return;
140
141 std::array<T, N> min_vals, max_vals;
142
143 // Initialize min/max
144 for (size_t d = 0; d < N; ++d)
145 {
146 min_vals[d] = points[0][d];
147 max_vals[d] = points[0][d];
148 }
149
150 // Compute bounding box
151 for (const auto &p : points)
152 {
153 for (size_t d = 0; d < N; ++d)
154 {
155 min_vals[d] = std::min(min_vals[d], p[d]);
156 max_vals[d] = std::max(max_vals[d], p[d]);
157 }
158 }
159
160 // Apply linear mapping to each point
161 for (auto &p : points)
162 {
163 for (size_t d = 0; d < N; ++d)
164 {
165 const T in_min = min_vals[d];
166 const T in_max = max_vals[d];
167 const T out_min = target_ranges[d].first;
168 const T out_max = target_ranges[d].second;
169
170 if (std::abs(in_max - in_min) < T(1e-12))
171 {
172 // Degenerate axis: center the target range
173 p[d] = (out_min + out_max) / T(2);
174 }
175 else
176 {
177 const T t = (p[d] - in_min) / (in_max - in_min);
178 p[d] = out_min + t * (out_max - out_min);
179 }
180 }
181 }
182}
183
206template <typename T, size_t N>
208 const std::array<std::pair<T, T>, N> &ranges)
209{
210 for (auto &pt : points)
211 for (size_t d = 0; d < N; ++d)
212 pt[d] = ranges[d].first + pt[d] * (ranges[d].second - ranges[d].first);
213}
214
215} // 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
void refit_points_to_range(std::vector< Point< T, N > > &points, const std::array< std::pair< T, T >, N > &target_ranges)
Linearly remap a set of points to fit within the specified axis-aligned ranges.
Definition range.hpp:135
std::vector< Point< T, N > > filter_points_function(const std::vector< Point< T, N > > &points, Func fn)
Filters points using a user-provided function.
Definition range.hpp:93
void rescale_points(std::vector< Point< T, N > > &points, const std::array< std::pair< T, T >, N > &ranges)
Rescales normalized points (in [0, 1]) to specified axis-aligned ranges.
Definition range.hpp:207
std::vector< Point< T, N > > filter_points_in_range(const std::vector< Point< T, N > > &points, const std::array< std::pair< T, T >, N > &axis_ranges)
Filters points that lie within the specified axis-aligned bounding box.
Definition range.hpp:39
A fixed-size N-dimensional point/vector class.
Definition point.hpp:39