| TGX 1.1.3 A tiny 2D/3D graphics library optimized for 32 bits microcontrollers.

Loading...
Searching...
No Matches
Box3.h
Go to the documentation of this file.
1
5//
6// Copyright 2020 Arvind Singh
7//
8// This library is free software; you can redistribute it and/or
9// modify it under the terms of the GNU Lesser General Public
10// License as published by the Free Software Foundation; either
11//version 2.1 of the License, or (at your option) any later version.
12//
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU
16// Lesser General Public License for more details.
17//
18// You should have received a copy of the GNU Lesser General Public
19// License along with this library; If not, see <http://www.gnu.org/licenses/>.
20
21
22#ifndef _TGX_BOX3_H_
23#define _TGX_BOX3_H_
24
25// only C++, no plain C
26#ifdef __cplusplus
27
28#include "Misc.h"
29#include "Vec3.h"
30
31#include <type_traits>
32#include <stdint.h>
33
34 namespace tgx
35{
36
37
38 // Forward declaration
39
40 template<typename T> struct Box3;
41
42
43 // Specializations
44
45 typedef Box3<int> iBox3;
46
47 typedef Box3<float> fBox3;
48
49 typedef Box3<double> dBox3;
50
51
66 template<typename T> struct Box3
67 {
68
69 // mtools extension (if available).
70 #if (MTOOLS_TGX_EXTENSIONS)
71 #include <mtools/extensions/tgx/tgx_ext_Box3.inl>
72 #endif
73
74 // box dimension: [minX, maxX] x [minY, maxY] x [minZ, maxZ]
75
76 T minX;
77 T maxX;
78 T minY;
79 T maxY;
80 T minZ;
81 T maxZ;
82
83
87 constexpr Box3()
88 {
89 }
90
91
95 constexpr Box3(const T minx, const T maxx, const T miny, const T maxy, const T minz, const T maxz) : minX(minx), maxX(maxx), minY(miny), maxY(maxy), minZ(minz), maxZ(maxz)
96 {
97 }
98
99
103 constexpr Box3(const Vec3<T>& v) : minX(v.x), maxX(v.x), minY(v.y), maxY(v.y), minZ(v.z), maxZ(v.z)
104 {
105 }
106
107
111 Box3(const Box3<T>& B) = default;
112
113
117 Box3<T>& operator=(const Box3<T>& B) = default;
118
119
123 template<typename U>
124 explicit operator Box3<U>() const { return Box3<U>((U)minX, (U)maxX, (U)minY, (U)maxY, (U)minZ, (U)maxZ); }
125
126
130 operator Box3<typename DefaultFPType<T>::fptype>() const { return Box3<typename DefaultFPType<T>::fptype>((typename DefaultFPType<T>::fptype)minX, (typename DefaultFPType<T>::fptype)maxX, (typename DefaultFPType<T>::fptype)minY, (typename DefaultFPType<T>::fptype)maxY, (typename DefaultFPType<T>::fptype)minZ, (typename DefaultFPType<T>::fptype)maxZ); }
131
132
136 constexpr inline bool isEmpty() const { return ((maxX < minX) || (maxY < minY) || (maxZ < minZ)); }
137
138
142 void empty()
143 {
144 minX = (T)1;
145 maxX = (T)0;
146 minY = (T)1;
147 maxY = (T)0;
148 minZ = (T)1;
149 maxZ = (T)0;
150 }
151
152
160 inline T lx() const
161 {
162 if (std::is_integral<T>::value) // compiler optimizes this away.
163 {
164 return (maxX - minX + 1); // for integer, return the number of points
165 }
166 else
167 {
168 return (maxX - minX); // for floating point type, return maxX - minX.
169 }
170 }
171
172
180 inline T ly() const
181 {
182 if (std::is_integral<T>::value) // compiler optimizes this away.
183 {
184 return (maxY - minY + 1); // for integer, return the number of points
185 }
186 else
187 {
188 return (maxY - minY); // for floating point type, return maxX - minX.
189 }
190 }
191
192
200 inline T lz() const
201 {
202 if (std::is_integral<T>::value) // compiler optimizes this away.
203 {
204 return (maxZ - minZ + 1); // for integer, return the number of points
205 }
206 else
207 {
208 return (maxZ - minZ); // for floating point type, return maxX - minX.
209 }
210 }
211
212
220 inline bool equals(const Box3<T>& B) const
221 {
222 if (isEmpty()) { return B.isEmpty(); }
223 return ((minX == B.minX) && (maxX == B.maxX) && (minY == B.minY) && (maxY == B.maxY) && (minZ == B.minZ) && (maxZ == B.maxZ));
224 }
225
226
234 inline bool operator==(const Box3<T>& B) const
235 {
236 return equals(B);
237 }
238
239
243 inline bool contains(const Vec3<T>& v) const
244 {
245 return(((minX <= v.x) && (v.x <= maxX)) && ((minY <= v.y) && (v.y <= maxY)) && ((minZ <= v.z) && (v.z <= maxZ)));
246 }
247
248
256 inline bool contains(const Box3<T>& B) const
257 {
258 if (isEmpty()) return false;
259 if (B.isEmpty()) return true;
260 return ((minX <= B.minX) && (maxX >= B.maxX) && (minY <= B.minY) && (maxY >= B.maxY) && (minZ <= B.minZ) && (maxZ >= B.maxZ));
261 }
262
263
273 inline bool operator>=(const Box3<T>& B) const
274 {
275 return contains(B);
276 }
277
278
288 inline bool operator<=(const Box3<T>& B) const
289 {
290 return B.contains(*this);
291 }
292
293
301 inline bool operator>(const Box3<T>& B) const
302 {
303 return ((contains(B)) && (!equals(B)));
304 }
305
306
314 inline bool operator<(const Box3<T>& B) const
315 {
316 return ((B.contains(*this)) && (!B.equals(*this)));
317 }
318
319
323 inline Box3<T> operator&(const Box3<T>& B) const
324 {
325 Box3<T> R;
326 if (isEmpty())
327 {
328 R = *this;
329 }
330 else if (B.isEmpty())
331 {
332 R = B;
333 }
334 else
335 {
336 R.minX = max(minX, B.minX);
337 R.maxX = min(maxX, B.maxX);
338 R.minY = max(minY, B.minY);
339 R.maxY = min(maxY, B.maxY);
340 R.minZ = max(minZ, B.minZ);
341 R.maxZ = min(maxZ, B.maxZ);
342 }
343 return R;
344 }
345
346
350 inline void operator&=(const Box3<T>& B)
351 {
352 (*this) = ((*this) & B);
353 }
354
355
359 inline Box3<T> operator|(const Box3<T>& B) const
360 {
361 Box3<T> R;
362 if (isEmpty())
363 {
364 R = B;
365 }
366 else if (B.isEmpty())
367 {
368 R = (*this);
369 }
370 else
371 {
372 R.minX = min(minX, B.minX);
373 R.maxX = max(maxX, B.maxX);
374 R.minY = min(minY, B.minY);
375 R.maxY = max(maxY, B.maxY);
376 R.minZ = min(minZ, B.minZ);
377 R.maxZ = max(maxZ, B.maxZ);
378 }
379 return R;
380 }
381
382
386 inline void operator|=(const Box3<T>& B)
387 {
388 *this = (*this) | B;
389 }
390
391
395 inline Box3<T> operator|(const Vec3<T>& v) const
396 {
397 Box3<T> R;
398 if (isEmpty())
399 {
400 R.minX = R.maxX = v.x;
401 R.minY = R.maxY = v.y;
402 R.minZ = R.maxZ = v.z;
403 }
404 else
405 {
406 R.minX = min(minX, v.x);
407 R.maxX = max(maxX, v.x);
408 R.minY = min(minY, v.y);
409 R.maxY = max(maxY, v.y);
410 R.minZ = min(minZ, v.z);
411 R.maxZ = max(maxZ, v.z);
412 }
413 return R;
414 }
415
416
420 inline void operator|=(const Vec3<T>& v)
421 {
422 (*this) = (*this) | v;
423 }
424
425
429 inline void operator+=(const Vec3<T> & V)
430 {
431 minX += V.x;
432 maxX += V.x;
433 minY += V.y;
434 maxY += V.y;
435 minZ += V.z;
436 maxZ += V.z;
437 }
438
439
443 inline Box3<T> operator+(const Vec3<T> & V) const
444 {
445 return Box3<T>(minX + V.x, maxX + V.x, minY + V.y, maxY + V.y, minZ + V.z, maxZ + V.z);
446 }
447
448
452 inline void operator-=(const Vec3<T> & V)
453 {
454 minX -= V.x;
455 maxX -= V.x;
456 minY -= V.y;
457 maxY -= V.y;
458 minZ -= V.z;
459 maxZ -= V.z;
460 }
461
462
466 inline Box3<T> operator-(const Vec3<T> & V) const
467 {
468 return Box3<T>(minX - V.x, maxX - V.x, minY - V.y, maxY - V.y, minZ - V.z, maxZ - V.z);
469 }
470
471
475 Vec3<T> center() const
476 {
477 return Vec3<T>((minX + maxX) / 2, (minY + maxY) / 2, (minZ + maxZ) / 2);
478 }
479
480
486 void zoomOut()
487 {
488 const T u = lx() / 10;
489 minX -= u;
490 maxX += u;
491 const T v = ly() / 10;
492 minY -= v;
493 maxY += v;
494 const T w = lz() / 10;
495 minZ -= w;
496 maxZ += w;
497 }
498
499
505 void zoomIn()
506 {
507 const T u = lx() / 8;
508 minX += u;
509 maxX -= u;
510 const T v = ly() / 8;
511 minY += v;
512 maxY -= v;
513 const T w = lz() / 8;
514 minZ += w;
515 maxZ -= w;
516 }
517
518
524 void left()
525 {
526 const T u = lx() / 10;
527 minX -= u;
528 maxX -= u;
529 }
530
531
537 void right()
538 {
539 const T u = lx() / 10;
540 minX += u;
541 maxX += u;
542 }
543
544
550 void up()
551 {
552 const T v = ly() / 10;
553 minY -= v;
554 maxY -= v;
555 }
556
557
563 void down()
564 {
565 const T v = ly() / 10;
566 minY += v;
567 maxY += v;
568 }
569
570
576 void front()
577 {
578 const T v = lz() / 10;
579 minZ -= v;
580 maxZ -= v;
581 }
582
583
589 void back()
590 {
591 const T v = lz() / 10;
592 minZ += v;
593 maxZ += v;
594 }
595
596
597 };
598
599
600
601
602}
603
604#endif
605
606#endif
607
Misc.h
Utility/miscellaneous functions used throughout the library.
tgx::max
TGX_INLINE T max(T a, T b)
Don't know why but much faster than fmaxf() for floats.
Definition: Misc.h:153
tgx::min
TGX_INLINE T min(T a, T b)
Don't know why but faster than fminf() for floats.
Definition: Misc.h:149
Vec3.h
3D vector.
tgx::Box3
Generic 3D Box [specializations iBox3, fBox3, dBox3].
Definition: Box3.h:67
tgx::Box3::operator|
Box3< T > operator|(const Vec3< T > &v) const
Return the smallest box containing this box and point v.
Definition: Box3.h:395
tgx::Box3::left
void left()
Move the box to the left by 1/10th of its width.
Definition: Box3.h:524
tgx::Box3::operator&=
void operator&=(const Box3< T > &B)
Intersect this box with box B.
Definition: Box3.h:350
tgx::Box3::minX
T minX
min horizontal (X) value (inclusive)
Definition: Box3.h:76
tgx::Box3::maxY
T maxY
max vertical (Y) value (inclusive)
Definition: Box3.h:79
tgx::Box3::ly
T ly() const
Return the box height.
Definition: Box3.h:180
tgx::Box3::zoomOut
void zoomOut()
Zoom outside the box (ie increase its size by 1/10th).
Definition: Box3.h:486
tgx::Box3::operator>=
bool operator>=(const Box3< T > &B) const
Return true if B is included in this box.
Definition: Box3.h:273
tgx::Box3::Box3
constexpr Box3(const T minx, const T maxx, const T miny, const T maxy, const T minz, const T maxz)
Constructor with explicit dimensions.
Definition: Box3.h:95
tgx::Box3::center
Vec3< T > center() const
Return the position of the box center as a 3 dimensional vector.
Definition: Box3.h:475
tgx::Box3::operator<=
bool operator<=(const Box3< T > &B) const
Return true if this box is included in B.
Definition: Box3.h:288
tgx::Box3::equals
bool equals(const Box3< T > &B) const
Return true if the boxes are equal.
Definition: Box3.h:220
tgx::Box3::zoomIn
void zoomIn()
Zoom inside the box (ie decrease its size by 1/8th).
Definition: Box3.h:505
tgx::Box3::Box3
constexpr Box3(const Vec3< T > &v)
Construct a box representing a single point.
Definition: Box3.h:103
tgx::Box3::operator+=
void operator+=(const Vec3< T > &V)
Translate this box by a given vector.
Definition: Box3.h:429
tgx::Box3::minY
T minY
min vertical (Y) value (inclusive)
Definition: Box3.h:78
tgx::Box3::operator|=
void operator|=(const Vec3< T > &v)
Enlarge this box in order to contain point v.
Definition: Box3.h:420
tgx::Box3::maxZ
T maxZ
max depth (Z) value (inclusive)
Definition: Box3.h:81
tgx::Box3::Box3
constexpr Box3()
default constructor: the box content is undefined.
Definition: Box3.h:87
tgx::Box3::contains
bool contains(const Box3< T > &B) const
Return true if B is included in this box.
Definition: Box3.h:256
tgx::Box3::Box3
Box3(const Box3< T > &B)=default
default copy constructor.
tgx::Box3::operator>
bool operator>(const Box3< T > &B) const
Return true if B is strictly included in this box (i.e.
Definition: Box3.h:301
tgx::Box3::isEmpty
constexpr bool isEmpty() const
Return true if the box is empty.
Definition: Box3.h:136
tgx::Box3::operator+
Box3< T > operator+(const Vec3< T > &V) const
Return this box translated by v.
Definition: Box3.h:443
tgx::Box3::contains
bool contains(const Vec3< T > &v) const
Return true if the box contains the point v.
Definition: Box3.h:243
tgx::Box3::right
void right()
Move the box to the right by 1/10th of its width.
Definition: Box3.h:537
tgx::Box3::minZ
T minZ
min depth (Z) value (inclusive)
Definition: Box3.h:80
tgx::Box3::front
void front()
Move the box front by 1/10th of its height.
Definition: Box3.h:576
tgx::Box3::operator<
bool operator<(const Box3< T > &B) const
Return true if this box is strictly included inside B (i.e.
Definition: Box3.h:314
tgx::Box3::operator==
bool operator==(const Box3< T > &B) const
Return true if the boxes are equal.
Definition: Box3.h:234
tgx::Box3::operator|
Box3< T > operator|(const Box3< T > &B) const
Return the smallest box containing both this box and B.
Definition: Box3.h:359
tgx::Box3::lx
T lx() const
Return the box width.
Definition: Box3.h:160
tgx::Box3::empty
void empty()
Make the box empty.
Definition: Box3.h:142
tgx::Box3::lz
T lz() const
Return the box depth.
Definition: Box3.h:200
tgx::Box3::operator-=
void operator-=(const Vec3< T > &V)
Translate the box by a given vector (substracted)
Definition: Box3.h:452
tgx::Box3::maxX
T maxX
max horizontal (X) value (inclusive)
Definition: Box3.h:77
tgx::Box3::operator-
Box3< T > operator-(const Vec3< T > &V) const
Return this box translated by v (substracted).
Definition: Box3.h:466
tgx::Box3::operator&
Box3< T > operator&(const Box3< T > &B) const
Return the intersection of this box and B.
Definition: Box3.h:323
tgx::Box3::back
void back()
Move the box back by 1/10th of its height.
Definition: Box3.h:589
tgx::Box3::operator|=
void operator|=(const Box3< T > &B)
Enlarge this box in order to contain B.
Definition: Box3.h:386
tgx::Box3::up
void up()
Move the box up by 1/10th of its height.
Definition: Box3.h:550
tgx::Box3::down
void down()
Move the box down by 1/10th of its height.
Definition: Box3.h:563
tgx::Box3::operator=
Box3< T > & operator=(const Box3< T > &B)=default
default assignment operator.
tgx::Vec2::x
T x
'x' coordinate (first dimension)
Definition: Vec2.h:72
tgx::Vec2::y
T y
'y' coordinate (second dimension)
Definition: Vec2.h:73
tgx::Vec3
Generic 3D vector [specializations iVec3, fVec3, dVec3].
Definition: Vec3.h:70
tgx::Vec3::z
T z
'z' coordinate (third dimension)
Definition: Vec3.h:83