FastLED 3.9.15
Loading...
Searching...
No Matches
geometry.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/int.h"
4#include "fl/math.h"
6#include "fl/move.h"
7
9
14
15namespace fl {
16
17template <typename T> struct vec3 {
18 // value_type
19 using value_type = T;
20 T x = 0;
21 T y = 0;
22 T z = 0;
23 constexpr vec3() = default;
24 constexpr vec3(T x, T y, T z) : x(x), y(y), z(z) {}
25
26 template <typename U>
27 explicit constexpr vec3(U xyz) : x(xyz), y(xyz), z(xyz) {}
28
29 constexpr vec3(const vec3 &p) = default;
30 constexpr vec3(vec3 &&p) noexcept = default;
31 vec3 &operator=(vec3 &&p) noexcept = default;
32
33 vec3 &operator*=(const float &f) {
34 x *= f;
35 y *= f;
36 z *= f;
37 return *this;
38 }
39 vec3 &operator/=(const float &f) {
40 x /= f;
41 y /= f;
42 z /= f;
43 return *this;
44 }
45 vec3 &operator*=(const double &f) {
46 x *= f;
47 y *= f;
48 z *= f;
49 return *this;
50 }
51 vec3 &operator/=(const double &f) {
52 x /= f;
53 y /= f;
54 z /= f;
55 return *this;
56 }
57
58 vec3 &operator/=(const u16 &d) {
59 x /= d;
60 y /= d;
61 z /= d;
62 return *this;
63 }
64
65 vec3 &operator/=(const int &d) {
66 x /= d;
67 y /= d;
68 z /= d;
69 return *this;
70 }
71
72 vec3 &operator/=(const vec3 &p) {
73 x /= p.x;
74 y /= p.y;
75 z /= p.z;
76 return *this;
77 }
78
79 vec3 &operator+=(const vec3 &p) {
80 x += p.x;
81 y += p.y;
82 z += p.z;
83 return *this;
84 }
85
86 vec3 &operator-=(const vec3 &p) {
87 x -= p.x;
88 y -= p.y;
89 z -= p.z;
90 return *this;
91 }
92
93 vec3 &operator=(const vec3 &p) {
94 x = p.x;
95 y = p.y;
96 z = p.z;
97 return *this;
98 }
99
100 vec3 operator-(const vec3 &p) const {
101 return vec3(x - p.x, y - p.y, z - p.z);
102 }
103
104 vec3 operator+(const vec3 &p) const {
105 return vec3(x + p.x, y + p.y, z + p.z);
106 }
107
108 vec3 operator*(const vec3 &p) const {
109 return vec3(x * p.x, y * p.y, z * p.z);
110 }
111
112 vec3 operator/(const vec3 &p) const {
113 return vec3(x / p.x, y / p.y, z / p.z);
114 }
115
116 template <typename NumberT> vec3 operator+(const NumberT &p) const {
117 return vec3(x + p, y + p, z + p);
118 }
119
120 template <typename U> vec3 operator+(const vec3<U> &p) const {
121 return vec3(x + p.x, y + p.y, z + p.z);
122 }
123
124 template <typename NumberT> vec3 operator-(const NumberT &p) const {
125 return vec3(x - p, y - p, z - p);
126 }
127
128 template <typename NumberT> vec3 operator*(const NumberT &p) const {
129 return vec3(x * p, y * p, z * p);
130 }
131
132 template <typename NumberT> vec3 operator/(const NumberT &p) const {
133 T a = x / p;
134 T b = y / p;
135 T c = z / p;
136 return vec3<T>(a, b, c);
137 }
138
139 bool operator==(const vec3 &p) const {
140 return (x == p.x && y == p.y && z == p.z);
141 }
142
143 bool operator!=(const vec3 &p) const {
144 return (x != p.x || y != p.y || z != p.z);
145 }
146
147 template <typename U> bool operator==(const vec3<U> &p) const {
148 return (x == p.x && y == p.y && z == p.z);
149 }
150
151 template <typename U> bool operator!=(const vec3<U> &p) const {
152 return (x != p.x || y != p.y || z != p.z);
153 }
154
155 vec3 getMax(const vec3 &p) const {
156 return vec3(MAX(x, p.x), MAX(y, p.y), MAX(z, p.z));
157 }
158
159 vec3 getMin(const vec3 &p) const {
160 return vec3(MIN(x, p.x), MIN(y, p.y), MIN(z, p.z));
161 }
162
163 template <typename U> vec3<U> cast() const {
164 return vec3<U>(static_cast<U>(x), static_cast<U>(y), static_cast<U>(z));
165 }
166
167 template <typename U> vec3 getMax(const vec3<U> &p) const {
168 return vec3<U>(MAX(x, p.x), MAX(y, p.y), MAX(z, p.z));
169 }
170
171 template <typename U> vec3 getMin(const vec3<U> &p) const {
172 return vec3<U>(MIN(x, p.x), MIN(y, p.y), MIN(z, p.z));
173 }
174
175 T distance(const vec3 &p) const {
176 T dx = x - p.x;
177 T dy = y - p.y;
178 T dz = z - p.z;
179 return sqrt(dx * dx + dy * dy + dz * dz);
180 }
181
182 bool is_zero() const { return (x == 0 && y == 0 && z == 0); }
183};
184
185using vec3f = vec3<float>; // Full precision but slow.
186
187template <typename T> struct vec2 {
188 // value_type
189 using value_type = T;
192 constexpr vec2() = default;
193 constexpr vec2(T x, T y) : x(x), y(y) {}
194
195 template <typename U> explicit constexpr vec2(U xy) : x(xy), y(xy) {}
196
197 constexpr vec2(const vec2 &p) = default;
198 constexpr vec2(vec2 &&p) noexcept = default;
199 vec2 &operator=(vec2 &&p) noexcept = default;
200
201 vec2 &operator*=(const float &f) {
202 x *= f;
203 y *= f;
204 return *this;
205 }
206 vec2 &operator/=(const float &f) {
207 // *this = point_xy_math::div(*this, f);
208 x /= f;
209 y /= f;
210 return *this;
211 }
212 vec2 &operator*=(const double &f) {
213 // *this = point_xy_math::mul(*this, f);
214 x *= f;
215 y *= f;
216 return *this;
217 }
218 vec2 &operator/=(const double &f) {
219 // *this = point_xy_math::div(*this, f);
220 x /= f;
221 y /= f;
222 return *this;
223 }
224
225 vec2 &operator/=(const u16 &d) {
226 // *this = point_xy_math::div(*this, d);
227 x /= d;
228 y /= d;
229 return *this;
230 }
231
232 vec2 &operator/=(const int &d) {
233 // *this = point_xy_math::div(*this, d);
234 x /= d;
235 y /= d;
236 return *this;
237 }
238
239 vec2 &operator/=(const vec2 &p) {
240 // *this = point_xy_math::div(*this, p);
241 x /= p.x;
242 y /= p.y;
243 return *this;
244 }
245
246 vec2 &operator+=(const vec2 &p) {
247 //*this = point_xy_math::add(*this, p);
248 x += p.x;
249 y += p.y;
250 return *this;
251 }
252
253 vec2 &operator-=(const vec2 &p) {
254 // *this = point_xy_math::sub(*this, p);
255 x -= p.x;
256 y -= p.y;
257 return *this;
258 }
259
260 vec2 &operator=(const vec2 &p) {
261 x = p.x;
262 y = p.y;
263 return *this;
264 }
265
266 vec2 operator-(const vec2 &p) const { return vec2(x - p.x, y - p.y); }
267
268 vec2 operator+(const vec2 &p) const { return vec2(x + p.x, y + p.y); }
269
270 vec2 operator*(const vec2 &p) const { return vec2(x * p.x, y * p.y); }
271
272 vec2 operator/(const vec2 &p) const { return vec2(x / p.x, y / p.y); }
273
274 template <typename NumberT> vec2 operator+(const NumberT &p) const {
275 return vec2(x + p, y + p);
276 }
277
278 template <typename U> vec2 operator+(const vec2<U> &p) const {
279 return vec2(x + p.x, y + p.x);
280 }
281
282 template <typename NumberT> vec2 operator-(const NumberT &p) const {
283 return vec2(x - p, y - p);
284 }
285
286 template <typename NumberT> vec2 operator*(const NumberT &p) const {
287 return vec2(x * p, y * p);
288 }
289
290 template <typename NumberT> vec2 operator/(const NumberT &p) const {
291 T a = x / p;
292 T b = y / p;
293 return vec2<T>(a, b);
294 }
295
296 bool operator==(const vec2 &p) const { return (x == p.x && y == p.y); }
297
298 bool operator!=(const vec2 &p) const { return (x != p.x || y != p.y); }
299
300 template <typename U> bool operator==(const vec2<U> &p) const {
301 return (x == p.x && y == p.y);
302 }
303
304 template <typename U> bool operator!=(const vec2<U> &p) const {
305 return (x != p.x || y != p.y);
306 }
307
308 vec2 getMax(const vec2 &p) const { return vec2(MAX(x, p.x), MAX(y, p.y)); }
309
310 vec2 getMin(const vec2 &p) const { return vec2(MIN(x, p.x), MIN(y, p.y)); }
311
312 template <typename U> vec2<U> cast() const {
313 return vec2<U>(static_cast<U>(x), static_cast<U>(y));
314 }
315
316 template <typename U> vec2 getMax(const vec2<U> &p) const {
317 return vec2<U>(MAX(x, p.x), MAX(y, p.y));
318 }
319
320 template <typename U> vec2 getMin(const vec2<U> &p) const {
321 return vec2<U>(MIN(x, p.x), MIN(y, p.y));
322 }
323
324 T distance(const vec2 &p) const {
325 T dx = x - p.x;
326 T dy = y - p.y;
327 return sqrt(dx * dx + dy * dy);
328 }
329
330 bool is_zero() const { return (x == 0 && y == 0); }
331};
332
333using vec2f = vec2<float>; // Full precision but slow.
334using vec2u8 = vec2<fl::u8>; // 8-bit unsigned integer vector.
335using vec2i16 = vec2<i16>; // 16-bit signed integer vector.
336
337// Legacy support for vec3
338using pair_xyz_float = vec3<float>; // Legacy name for vec3f
339
340// Legacy support for vec2
341
342using pair_xy_float = vec2<float>; // Legacy name for vec2f
343
344// pair_xy<T> is the legacy name for vec2<T>
345template <typename T> struct pair_xy : public vec2<T> {
346 using value_type = T;
347 using vec2<T>::vec2;
348 pair_xy() = default;
349 pair_xy(const vec2<T> &p) : vec2<T>(p) {}
350};
351
352template <typename T> struct line_xy {
355
356 line_xy() = default;
358 : start(start), end(end) {}
359
360 line_xy(T start_x, T start_y, T end_x, T end_y)
361 : start(start_x, start_y), end(end_x, end_y) {}
362
363 line_xy(const line_xy &other) = default;
364 line_xy &operator=(const line_xy &other) = default;
365 line_xy(line_xy &&other) noexcept = default;
366 line_xy &operator=(line_xy &&other) noexcept = default;
367
368 bool empty() const { return (start == end); }
369
370 float distance_to(const vec2<T> &p,
371 vec2<T> *out_projected = nullptr) const {
372 return distance_to_line_with_point(p, start, end, out_projected);
373 }
374
375 private:
376 // Computes the closest distance from `p` to the line through `a` and `b`,
377 // and writes the projected point.
379 vec2<T> *out_projected) {
380 vec2<T> maybe;
381 vec2<T> &out_proj = out_projected ? *out_projected : maybe;
382 float dx = b.x - a.x;
383 float dy = b.y - a.y;
384 float len_sq = dx * dx + dy * dy;
385
388 const bool is_zero = (len_sq == 0.0f);
390
391 if (is_zero) {
392 // a == b, the segment is a point
393 out_proj = a;
394 dx = p.x - a.x;
395 dy = p.y - a.y;
396 return sqrt(dx * dx + dy * dy);
397 }
398
399 // Project point p onto the line segment, computing parameter t
400 float t = ((p.x - a.x) * dx + (p.y - a.y) * dy) / len_sq;
401
402 // Clamp t to [0,1] to stay within the segment
403 if (t < 0.0f)
404 t = 0.0f;
405 else if (t > 1.0f)
406 t = 1.0f;
407
408 // Find the closest point
409 out_proj.x = a.x + t * dx;
410 out_proj.y = a.y + t * dy;
411
412 dx = p.x - out_proj.x;
413 dy = p.y - out_proj.y;
414 return sqrt(dx * dx + dy * dy);
415 }
416};
417
418template <typename T> struct rect {
421
422 rect() = default;
423 rect(const vec2<T> &min, const vec2<T> &max) : mMin(min), mMax(max) {}
424
425 rect(T min_x, T min_y, T max_x, T max_y)
426 : mMin(min_x, min_y), mMax(max_x, max_y) {}
427
428 rect(const rect &other) = default;
429 rect &operator=(const rect &other) = default;
430 rect(rect &&other) noexcept = default;
431 rect &operator=(rect &&other) noexcept = default;
432
433 u16 width() const { return mMax.x - mMin.x; }
434
435 u16 height() const { return mMax.y - mMin.y; }
436
437 bool empty() const { return (mMin.x == mMax.x && mMin.y == mMax.y); }
438
439 void expand(const vec2<T> &p) { expand(p.x, p.y); }
440
441 void expand(const rect &r) {
442 expand(r.mMin);
443 expand(r.mMax);
444 }
445
446 void expand(T x, T y) {
447 mMin.x = MIN(mMin.x, x);
448 mMin.y = MIN(mMin.y, y);
449 mMax.x = MAX(mMax.x, x);
450 mMax.y = MAX(mMax.y, y);
451 }
452
453 bool contains(const vec2<T> &p) const {
454 return (p.x >= mMin.x && p.x < mMax.x && p.y >= mMin.y && p.y < mMax.y);
455 }
456
457 bool contains(const T &x, const T &y) const {
458 return contains(vec2<T>(x, y));
459 }
460
461 bool operator==(const rect &r) const {
462 return (mMin == r.mMin && mMax == r.mMax);
463 }
464
465 bool operator!=(const rect &r) const { return !(*this == r); }
466
467 template <typename U> bool operator==(const rect<U> &r) const {
468 return (mMin == r.mMin && mMax == r.mMax);
469 }
470
471 template <typename U> bool operator!=(const rect<U> &r) const {
472 return !(*this == r);
473 }
474};
475
476} // namespace fl
477
int y
Definition simple.h:93
int x
Definition simple.h:92
unsigned int xy(unsigned int x, unsigned int y)
#define FL_DISABLE_WARNING(warning)
#define FL_DISABLE_WARNING_IMPLICIT_INT_CONVERSION
#define FL_DISABLE_WARNING_PUSH
#define FL_DISABLE_WARNING_SIGN_CONVERSION
#define FL_DISABLE_WARNING_POP
#define FL_DISABLE_WARNING_FLOAT_CONVERSION
static uint32_t t
Definition Luminova.h:54
#define MIN(a, b)
Definition math_macros.h:41
#define MAX(a, b)
Definition math_macros.h:37
vec2< float > pair_xy_float
Definition geometry.h:342
vec3< float > vec3f
Definition geometry.h:185
vec2< float > vec2f
Definition geometry.h:333
vec2< i16 > vec2i16
Definition geometry.h:335
vec2< fl::u8 > vec2u8
Definition geometry.h:334
bool equal(Iterator1 first1, Iterator1 last1, Iterator2 first2)
Definition algorithm.h:95
vec3< float > pair_xyz_float
Definition geometry.h:338
IMPORTANT!
Definition crgb.h:20
line_xy()=default
static float distance_to_line_with_point(vec2< T > p, vec2< T > a, vec2< T > b, vec2< T > *out_projected)
Definition geometry.h:378
vec2< T > end
Definition geometry.h:354
bool empty() const
Definition geometry.h:368
vec2< T > start
Definition geometry.h:353
line_xy(const line_xy &other)=default
line_xy & operator=(line_xy &&other) noexcept=default
line_xy(line_xy &&other) noexcept=default
float distance_to(const vec2< T > &p, vec2< T > *out_projected=nullptr) const
Definition geometry.h:370
line_xy(const vec2< T > &start, const vec2< T > &end)
Definition geometry.h:357
line_xy & operator=(const line_xy &other)=default
line_xy(T start_x, T start_y, T end_x, T end_y)
Definition geometry.h:360
pair_xy(const vec2< T > &p)
Definition geometry.h:349
pair_xy()=default
vec2< T > mMax
Definition geometry.h:420
u16 height() const
Definition geometry.h:435
bool operator==(const rect &r) const
Definition geometry.h:461
rect(const vec2< T > &min, const vec2< T > &max)
Definition geometry.h:423
u16 width() const
Definition geometry.h:433
rect()=default
bool operator!=(const rect< U > &r) const
Definition geometry.h:471
rect & operator=(rect &&other) noexcept=default
rect(const rect &other)=default
void expand(const rect &r)
Definition geometry.h:441
rect(T min_x, T min_y, T max_x, T max_y)
Definition geometry.h:425
bool contains(const vec2< T > &p) const
Definition geometry.h:453
void expand(const vec2< T > &p)
Definition geometry.h:439
rect(rect &&other) noexcept=default
bool empty() const
Definition geometry.h:437
bool operator==(const rect< U > &r) const
Definition geometry.h:467
rect & operator=(const rect &other)=default
vec2< T > mMin
Definition geometry.h:419
bool operator!=(const rect &r) const
Definition geometry.h:465
void expand(T x, T y)
Definition geometry.h:446
bool contains(const T &x, const T &y) const
Definition geometry.h:457
bool operator==(const vec2< U > &p) const
Definition geometry.h:300
constexpr vec2(U xy)
Definition geometry.h:195
vec2 operator/(const vec2 &p) const
Definition geometry.h:272
vec2 operator/(const NumberT &p) const
Definition geometry.h:290
constexpr vec2(T x, T y)
Definition geometry.h:193
constexpr vec2(const vec2 &p)=default
vec2 & operator/=(const u16 &d)
Definition geometry.h:225
vec2 & operator/=(const double &f)
Definition geometry.h:218
vec2 operator-(const NumberT &p) const
Definition geometry.h:282
vec2 & operator/=(const float &f)
Definition geometry.h:206
vec2 getMin(const vec2 &p) const
Definition geometry.h:310
T distance(const vec2 &p) const
Definition geometry.h:324
vec2 operator*(const vec2 &p) const
Definition geometry.h:270
vec2 operator+(const NumberT &p) const
Definition geometry.h:274
vec2 & operator=(const vec2 &p)
Definition geometry.h:260
vec2 getMax(const vec2< U > &p) const
Definition geometry.h:316
vec2 getMax(const vec2 &p) const
Definition geometry.h:308
vec2 & operator/=(const vec2 &p)
Definition geometry.h:239
constexpr vec2(vec2 &&p) noexcept=default
value_type y
Definition geometry.h:191
value_type x
Definition geometry.h:190
vec2 operator-(const vec2 &p) const
Definition geometry.h:266
bool is_zero() const
Definition geometry.h:330
vec2 operator+(const vec2 &p) const
Definition geometry.h:268
bool operator==(const vec2 &p) const
Definition geometry.h:296
bool operator!=(const vec2 &p) const
Definition geometry.h:298
vec2 & operator*=(const double &f)
Definition geometry.h:212
vec2 & operator+=(const vec2 &p)
Definition geometry.h:246
constexpr vec2()=default
vec2 operator*(const NumberT &p) const
Definition geometry.h:286
vec2 & operator=(vec2 &&p) noexcept=default
vec2 getMin(const vec2< U > &p) const
Definition geometry.h:320
vec2 & operator-=(const vec2 &p)
Definition geometry.h:253
vec2 & operator*=(const float &f)
Definition geometry.h:201
vec2< U > cast() const
Definition geometry.h:312
vec2 operator+(const vec2< U > &p) const
Definition geometry.h:278
vec2 & operator/=(const int &d)
Definition geometry.h:232
T value_type
Definition geometry.h:189
bool operator!=(const vec2< U > &p) const
Definition geometry.h:304
constexpr vec3(const vec3 &p)=default
vec3 operator-(const vec3 &p) const
Definition geometry.h:100
vec3 getMin(const vec3 &p) const
Definition geometry.h:159
T value_type
Definition geometry.h:19
vec3 operator+(const vec3< U > &p) const
Definition geometry.h:120
vec3 & operator/=(const vec3 &p)
Definition geometry.h:72
vec3 operator-(const NumberT &p) const
Definition geometry.h:124
vec3 operator*(const vec3 &p) const
Definition geometry.h:108
constexpr vec3(vec3 &&p) noexcept=default
bool is_zero() const
Definition geometry.h:182
constexpr vec3(T x, T y, T z)
Definition geometry.h:24
vec3 getMax(const vec3 &p) const
Definition geometry.h:155
vec3 & operator=(const vec3 &p)
Definition geometry.h:93
bool operator==(const vec3< U > &p) const
Definition geometry.h:147
bool operator!=(const vec3 &p) const
Definition geometry.h:143
vec3 operator/(const vec3 &p) const
Definition geometry.h:112
vec3 & operator/=(const double &f)
Definition geometry.h:51
bool operator==(const vec3 &p) const
Definition geometry.h:139
constexpr vec3()=default
vec3 operator/(const NumberT &p) const
Definition geometry.h:132
vec3 operator*(const NumberT &p) const
Definition geometry.h:128
vec3 & operator/=(const u16 &d)
Definition geometry.h:58
vec3 & operator*=(const double &f)
Definition geometry.h:45
vec3 & operator/=(const int &d)
Definition geometry.h:65
vec3 operator+(const vec3 &p) const
Definition geometry.h:104
T distance(const vec3 &p) const
Definition geometry.h:175
vec3 & operator*=(const float &f)
Definition geometry.h:33
vec3 & operator/=(const float &f)
Definition geometry.h:39
vec3 & operator-=(const vec3 &p)
Definition geometry.h:86
vec3 operator+(const NumberT &p) const
Definition geometry.h:116
vec3< U > cast() const
Definition geometry.h:163
vec3 & operator+=(const vec3 &p)
Definition geometry.h:79
vec3 getMin(const vec3< U > &p) const
Definition geometry.h:171
bool operator!=(const vec3< U > &p) const
Definition geometry.h:151
vec3 & operator=(vec3 &&p) noexcept=default
constexpr vec3(U xyz)
Definition geometry.h:27
vec3 getMax(const vec3< U > &p) const
Definition geometry.h:167