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