PointSampler library (C++)
Loading...
Searching...
No Matches
point.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 <algorithm>
6#include <array>
7#include <cmath>
8#include <initializer_list>
9#include <numeric>
10#include <stdexcept>
11#include <vector>
12
13namespace ps
14{
15
38template <typename T, size_t N> struct Point
39{
40 std::array<T, N> coords;
41
42 // Default constructor
43 Point() = default;
44
45 // Constructor from std::vector<T>
46 explicit Point(const std::vector<T> &v)
47 {
48 if (v.size() != N)
49 throw std::invalid_argument("Point: vector size mismatch");
50 for (size_t i = 0; i < N; ++i)
51 coords[i] = v[i];
52 }
53
54 // Constructor from initializer list
55 Point(std::initializer_list<T> init)
56 {
57 if (init.size() != N)
58 throw std::invalid_argument("Point: initializer list size mismatch");
59 std::copy(init.begin(), init.end(), coords.begin());
60 }
61
62 Point(const std::array<T, N> &coords_) { coords = coords_; }
63
64 // Accessors
65 T &operator[](size_t i) { return coords[i]; }
66 const T &operator[](size_t i) const { return coords[i]; }
67
68 // Optional 2D/3D accessors
69 T &x()
70 {
71 static_assert(N > 0, "No x");
72 return coords[0];
73 }
74 T &y()
75 {
76 static_assert(N > 1, "No y");
77 return coords[1];
78 }
79 T &z()
80 {
81 static_assert(N > 2, "No z");
82 return coords[2];
83 }
84 T &w()
85 {
86 static_assert(N > 3, "No w");
87 return coords[3];
88 }
89
90 const T &x() const
91 {
92 static_assert(N > 0, "No x");
93 return coords[0];
94 }
95 const T &y() const
96 {
97 static_assert(N > 1, "No y");
98 return coords[1];
99 }
100 const T &z() const
101 {
102 static_assert(N > 2, "No z");
103 return coords[2];
104 }
105 const T &w() const
106 {
107 static_assert(N > 3, "No w");
108 return coords[3];
109 }
110};
111
112// ------------------------------
113// Arithmetic Operators
114// ------------------------------
115template <typename T, size_t N>
117{
119 for (size_t i = 0; i < N; ++i)
120 result[i] = a[i] + b[i];
121 return result;
122}
123
124template <typename T, size_t N>
126{
128 for (size_t i = 0; i < N; ++i)
129 result[i] = a[i] - b[i];
130 return result;
131}
132
133template <typename T, size_t N>
135{
137 for (size_t i = 0; i < N; ++i)
138 result[i] = a[i] * b[i];
139 return result;
140}
141
142template <typename T, size_t N>
144{
146 for (size_t i = 0; i < N; ++i)
147 result[i] = a[i] / b[i];
148 return result;
149}
150
151// Scalar ops
152template <typename T, size_t N> Point<T, N> operator+(const Point<T, N> &p, T scalar)
153{
155 for (size_t i = 0; i < N; ++i)
156 result[i] = p[i] + scalar;
157 return result;
158}
159
160template <typename T, size_t N> Point<T, N> operator-(const Point<T, N> &p, T scalar)
161{
163 for (size_t i = 0; i < N; ++i)
164 result[i] = p[i] - scalar;
165 return result;
166}
167
168template <typename T, size_t N> Point<T, N> operator*(const Point<T, N> &p, T scalar)
169{
171 for (size_t i = 0; i < N; ++i)
172 result[i] = p[i] * scalar;
173 return result;
174}
175
176template <typename T, size_t N> Point<T, N> operator/(const Point<T, N> &p, T scalar)
177{
179 for (size_t i = 0; i < N; ++i)
180 result[i] = p[i] / scalar;
181 return result;
182}
183
184// Reverse scalar ops
185template <typename T, size_t N> Point<T, N> operator*(T scalar, const Point<T, N> &p)
186{
187 return p * scalar;
188}
189
190template <typename T, size_t N> Point<T, N> operator+(T scalar, const Point<T, N> &p)
191{
192 return p + scalar;
193}
194
195// ------------------------------
196// Geometric Functions
197// ------------------------------
198template <typename T, size_t N> T dot(const Point<T, N> &a, const Point<T, N> &b)
199{
200 T result = T(0);
201 for (size_t i = 0; i < N; ++i)
202 result += a[i] * b[i];
203 return result;
204}
205
206template <typename T, size_t N> T length_squared(const Point<T, N> &a)
207{
208 return dot(a, a);
209}
210
211template <typename T, size_t N> T length(const Point<T, N> &a)
212{
213 return std::sqrt(length_squared(a));
214}
215
216template <typename T, size_t N> Point<T, N> normalized(const Point<T, N> &a)
217{
218 T len = length(a);
219 if (len == T(0))
220 return Point<T, N>();
221 else
222 return a / len;
223}
224
225template <typename T, size_t N>
227{
228 return length_squared(a - b);
229}
230
231template <typename T, size_t N> T distance(const Point<T, N> &a, const Point<T, N> &b)
232{
233 return length(a - b);
234}
235
236template <typename T, size_t N>
238{
239 return a + (b - a) * t;
240}
241
242template <typename T, size_t N>
244{
246 for (size_t i = 0; i < N; ++i)
247 result[i] = std::min(std::max(p[i], min_val), max_val);
248 return result;
249}
250
251} // 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(const Point< T, N > &a)
Definition point.hpp:211
Point< T, N > operator/(const Point< T, N > &a, const Point< T, N > &b)
Definition point.hpp:143
Point< T, N > operator*(const Point< T, N > &a, const Point< T, N > &b)
Definition point.hpp:134
Point< T, N > lerp(const Point< T, N > &a, const Point< T, N > &b, T t)
Definition point.hpp:237
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
Point< T, N > operator-(const Point< T, N > &a, const Point< T, N > &b)
Definition point.hpp:125
T distance(const Point< T, N > &a, const Point< T, N > &b)
Definition point.hpp:231
T dot(const Point< T, N > &a, const Point< T, N > &b)
Definition point.hpp:198
Point< T, N > clamp(const Point< T, N > &p, T min_val, T max_val)
Definition point.hpp:243
T distance_squared(const Point< T, N > &a, const Point< T, N > &b)
Definition point.hpp:226
Point< T, N > operator+(const Point< T, N > &a, const Point< T, N > &b)
Definition point.hpp:116
A fixed-size N-dimensional point/vector class.
Definition point.hpp:39
T & operator[](size_t i)
Definition point.hpp:65
T & z()
Definition point.hpp:79
const T & w() const
Definition point.hpp:105
Point(const std::array< T, N > &coords_)
Definition point.hpp:62
T & w()
Definition point.hpp:84
Point()=default
std::array< T, N > coords
Definition point.hpp:40
Point(const std::vector< T > &v)
Definition point.hpp:46
const T & operator[](size_t i) const
Definition point.hpp:66
T & x()
Definition point.hpp:69
const T & x() const
Definition point.hpp:90
const T & z() const
Definition point.hpp:100
const T & y() const
Definition point.hpp:95
Point(std::initializer_list< T > init)
Definition point.hpp:55
T & y()
Definition point.hpp:74