1//
2// SPDX-License-Identifier: BSD-3-Clause
3// Copyright Contributors to the OpenEXR Project.
4//
5
6//
7// Axis-aligned bounding box
8//
9
10#ifndef INCLUDED_IMATHBOX_H
11#define INCLUDED_IMATHBOX_H
12
13#include "ImathExport.h"
14#include "ImathNamespace.h"
15
16#include "ImathVec.h"
17
18IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
19
20///
21/// The `Box<V>` template represents an axis-aligned bounding box defined by
22/// minimum and maximum values of type `V`. The `min` and `max` members are
23/// public.
24///
25/// The type `V` is typically an Imath vector (i.e. `V2i`, `V3f`, etc) and must
26/// implement an index `operator[]` that returns a type (typically as scalar)
27/// that supports assignment, comparison, and arithmetic operators.
28///
29/// `V` must also provide a constructor that takes a float and/or double for
30/// use in initializing the box.
31///
32/// `V` must also provide a function `V::dimensions()` which returns the
33/// number of dimensions in the class (since its assumed its a vector) --
34/// preferably, this returns a constant expression, typically 2 or 3.
35///
36
37template <class V> class IMATH_EXPORT_TEMPLATE_TYPE Box
38{
39 public:
40
41 /// @{
42 /// @name Direct access to bounds
43
44 /// The minimum value of the box.
45 V min;
46
47 /// The maximum value of the box.
48 V max;
49
50 /// @}
51
52 /// @{
53 /// @name Constructors
54
55 /// Construct an empty bounding box. This initializes the mimimum to
56 /// std::numeric_limits<V::baseType>::max() and the maximum to
57 /// std::numeric_limits<V::baseType>::lowest().
58 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box() IMATH_NOEXCEPT;
59
60 /// Construct a bounding box that contains a single point.
61 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box (const V& point) IMATH_NOEXCEPT;
62
63 /// Construct a bounding box with the given minimum and maximum values.
64 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box (const V& minV, const V& maxV) IMATH_NOEXCEPT;
65
66 /// @}
67
68 /// @{
69 /// @name Comparison
70
71 /// Equality
72 IMATH_HOSTDEVICE constexpr bool operator== (const Box<V>& src) const IMATH_NOEXCEPT;
73
74 /// Inequality
75 IMATH_HOSTDEVICE constexpr bool operator!= (const Box<V>& src) const IMATH_NOEXCEPT;
76
77 /// @}
78
79 /// @{
80 /// @name Manipulation
81
82 /// Set the box to be empty. A box is empty if the mimimum is greater
83 /// than the maximum. makeEmpty() sets the mimimum to `V::baseTypeMax()`
84 /// and the maximum to `V::baseTypeLowest()`.
85 IMATH_HOSTDEVICE void makeEmpty() IMATH_NOEXCEPT;
86
87 /// Extend the box to include the given point.
88 IMATH_HOSTDEVICE void extendBy (const V& point) IMATH_NOEXCEPT;
89
90 /// Extend the box to include the given box.
91 IMATH_HOSTDEVICE void extendBy (const Box<V>& box) IMATH_NOEXCEPT;
92
93 /// Make the box include the entire range of `V`.
94 IMATH_HOSTDEVICE void makeInfinite() IMATH_NOEXCEPT;
95
96 /// @}
97
98 /// @{
99 /// @name Query
100
101 /// Return the size of the box. The size is of type `V`, defined
102 /// as `(max-min)`. An empty box has a size of `V(0)`, i.e. 0 in
103 /// each dimension.
104 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 V size() const IMATH_NOEXCEPT;
105
106 /// Return the center of the box. The center is defined as
107 /// `(max+min)/2`. The center of an empty box is undefined.
108 IMATH_HOSTDEVICE constexpr V center() const IMATH_NOEXCEPT;
109
110 /// Return true if the given point is inside the box, false otherwise.
111 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const V& point) const IMATH_NOEXCEPT;
112
113 /// Return true if the given box is inside the box, false otherwise.
114 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const Box<V>& box) const IMATH_NOEXCEPT;
115
116 /// Return the major axis of the box. The major axis is the dimension with
117 /// the greatest difference between maximum and minimum.
118 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 unsigned int majorAxis() const IMATH_NOEXCEPT;
119
120 /// Return true if the box is empty, false otherwise. An empty box's
121 /// minimum is greater than its maximum.
122 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isEmpty() const IMATH_NOEXCEPT;
123
124 /// Return true if the box is larger than a single point, false otherwise.
125 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool hasVolume() const IMATH_NOEXCEPT;
126
127 /// Return true if the box contains all points, false otherwise.
128 /// An infinite box has a mimimum of`V::baseTypeLowest()`
129 /// and a maximum of `V::baseTypeMax()`.
130 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isInfinite() const IMATH_NOEXCEPT;
131
132 /// @}
133};
134
135//--------------------
136// Convenient typedefs
137//--------------------
138
139/// 2D box of base type `short`.
140typedef Box<V2s> Box2s;
141
142/// 2D box of base type `int`.
143typedef Box<V2i> Box2i;
144
145/// 2D box of base type `int64_t`.
146typedef Box<V2i64> Box2i64;
147
148/// 2D box of base type `float`.
149typedef Box<V2f> Box2f;
150
151/// 2D box of base type `double`.
152typedef Box<V2d> Box2d;
153
154/// 3D box of base type `short`.
155typedef Box<V3s> Box3s;
156
157/// 3D box of base type `int`.
158typedef Box<V3i> Box3i;
159
160/// 3D box of base type `int64_t`.
161typedef Box<V3i64> Box3i64;
162
163/// 3D box of base type `float`.
164typedef Box<V3f> Box3f;
165
166/// 3D box of base type `double`.
167typedef Box<V3d> Box3d;
168
169template <class V>
170IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<V>::Box() IMATH_NOEXCEPT
171{
172 makeEmpty();
173}
174
175template <class V>
176IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<V>::Box (const V& point) IMATH_NOEXCEPT
177{
178 min = point;
179 max = point;
180}
181
182template <class V>
183IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<V>::Box (const V& minV, const V& maxV) IMATH_NOEXCEPT
184{
185 min = minV;
186 max = maxV;
187}
188
189template <class V>
190IMATH_HOSTDEVICE constexpr inline bool
191Box<V>::operator== (const Box<V>& src) const IMATH_NOEXCEPT
192{
193 return (min == src.min && max == src.max);
194}
195
196template <class V>
197IMATH_HOSTDEVICE constexpr inline bool
198Box<V>::operator!= (const Box<V>& src) const IMATH_NOEXCEPT
199{
200 return (min != src.min || max != src.max);
201}
202
203template <class V>
204IMATH_HOSTDEVICE inline void
205Box<V>::makeEmpty() IMATH_NOEXCEPT
206{
207 min = V (V::baseTypeMax());
208 max = V (V::baseTypeLowest());
209}
210
211template <class V>
212IMATH_HOSTDEVICE inline void
213Box<V>::makeInfinite() IMATH_NOEXCEPT
214{
215 min = V (V::baseTypeLowest());
216 max = V (V::baseTypeMax());
217}
218
219template <class V>
220IMATH_HOSTDEVICE inline void
221Box<V>::extendBy (const V& point) IMATH_NOEXCEPT
222{
223 for (unsigned int i = 0; i < min.dimensions(); i++)
224 {
225 if (point[i] < min[i])
226 min[i] = point[i];
227
228 if (point[i] > max[i])
229 max[i] = point[i];
230 }
231}
232
233template <class V>
234IMATH_HOSTDEVICE inline void
235Box<V>::extendBy (const Box<V>& box) IMATH_NOEXCEPT
236{
237 for (unsigned int i = 0; i < min.dimensions(); i++)
238 {
239 if (box.min[i] < min[i])
240 min[i] = box.min[i];
241
242 if (box.max[i] > max[i])
243 max[i] = box.max[i];
244 }
245}
246
247template <class V>
248IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
249Box<V>::intersects (const V& point) const IMATH_NOEXCEPT
250{
251 for (unsigned int i = 0; i < min.dimensions(); i++)
252 {
253 if (point[i] < min[i] || point[i] > max[i])
254 return false;
255 }
256
257 return true;
258}
259
260template <class V>
261IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
262Box<V>::intersects (const Box<V>& box) const IMATH_NOEXCEPT
263{
264 for (unsigned int i = 0; i < min.dimensions(); i++)
265 {
266 if (box.max[i] < min[i] || box.min[i] > max[i])
267 return false;
268 }
269
270 return true;
271}
272
273template <class V>
274IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline V
275Box<V>::size() const IMATH_NOEXCEPT
276{
277 if (isEmpty())
278 return V (0);
279
280 return max - min;
281}
282
283template <class V>
284IMATH_HOSTDEVICE constexpr inline V
285Box<V>::center() const IMATH_NOEXCEPT
286{
287 return (max + min) / 2;
288}
289
290template <class V>
291IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
292Box<V>::isEmpty() const IMATH_NOEXCEPT
293{
294 for (unsigned int i = 0; i < min.dimensions(); i++)
295 {
296 if (max[i] < min[i])
297 return true;
298 }
299
300 return false;
301}
302
303template <class V>
304IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
305Box<V>::isInfinite() const IMATH_NOEXCEPT
306{
307 for (unsigned int i = 0; i < min.dimensions(); i++)
308 {
309 if (min[i] != V::baseTypeLowest() || max[i] != V::baseTypeMax())
310 return false;
311 }
312
313 return true;
314}
315
316template <class V>
317IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
318Box<V>::hasVolume() const IMATH_NOEXCEPT
319{
320 for (unsigned int i = 0; i < min.dimensions(); i++)
321 {
322 if (max[i] <= min[i])
323 return false;
324 }
325
326 return true;
327}
328
329template <class V>
330IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline unsigned int
331Box<V>::majorAxis() const IMATH_NOEXCEPT
332{
333 unsigned int major = 0;
334 V s = size();
335
336 for (unsigned int i = 1; i < min.dimensions(); i++)
337 {
338 if (s[i] > s[major])
339 major = i;
340 }
341
342 return major;
343}
344
345//-------------------------------------------------------------------
346//
347// Partial class specializations for Imath::Vec2<T> and Imath::Vec3<T>
348//
349//-------------------------------------------------------------------
350
351template <typename V> class Box;
352
353///
354/// The Box<Vec2<T>> template represents a 2D bounding box defined by
355/// minimum and maximum values of type Vec2<T>. The min and max members are
356/// public.
357///
358
359template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Box<Vec2<T>>
360{
361 public:
362
363 /// @{
364 /// @name Direct access to bounds
365
366 /// The minimum value of the box.
367 Vec2<T> min;
368
369 /// The maximum value of the box.
370 Vec2<T> max;
371
372 /// @}
373
374 /// @{
375 /// @name Constructors and Assignment
376
377 /// Empty by default
378 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box() IMATH_NOEXCEPT;
379
380 /// Construct a bounding box that contains a single point.
381 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box (const Vec2<T>& point) IMATH_NOEXCEPT;
382
383 /// Construct a bounding box with the given minimum and maximum points
384 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box (const Vec2<T>& minT, const Vec2<T>& maxT) IMATH_NOEXCEPT;
385
386 /// @}
387
388 /// @{
389 /// @name Comparison
390
391 /// Equality
392 IMATH_HOSTDEVICE constexpr bool operator== (const Box<Vec2<T>>& src) const IMATH_NOEXCEPT;
393
394 /// Inequality
395 IMATH_HOSTDEVICE constexpr bool operator!= (const Box<Vec2<T>>& src) const IMATH_NOEXCEPT;
396
397 /// @}
398
399 /// @{
400 /// @name Manipulation
401
402 /// Set the Box to be empty. A Box is empty if the mimimum is
403 /// greater than the maximum. makeEmpty() sets the mimimum to
404 /// std::numeric_limits<T>::max() and the maximum to
405 /// std::numeric_limits<T>::lowest().
406 IMATH_HOSTDEVICE void makeEmpty() IMATH_NOEXCEPT;
407
408 /// Extend the Box to include the given point.
409 IMATH_HOSTDEVICE void extendBy (const Vec2<T>& point) IMATH_NOEXCEPT;
410
411 /// Extend the Box to include the given box.
412 IMATH_HOSTDEVICE void extendBy (const Box<Vec2<T>>& box) IMATH_NOEXCEPT;
413
414 /// Make the box include the entire range of T.
415 IMATH_HOSTDEVICE void makeInfinite() IMATH_NOEXCEPT;
416
417 /// @}
418
419 /// @{
420 /// @name Query
421
422 /// Return the size of the box. The size is of type `V`, defined as
423 /// `(max-min)`. An empty box has a size of `V(0)`, i.e. 0 in each dimension.
424 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec2<T> size() const IMATH_NOEXCEPT;
425
426 /// Return the center of the box. The center is defined as
427 /// `(max+min)/2`. The center of an empty box is undefined.
428 IMATH_HOSTDEVICE constexpr Vec2<T> center() const IMATH_NOEXCEPT;
429
430 /// Return true if the given point is inside the box, false otherwise.
431 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const Vec2<T>& point) const IMATH_NOEXCEPT;
432
433 /// Return true if the given box is inside the box, false otherwise.
434 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const Box<Vec2<T>>& box) const IMATH_NOEXCEPT;
435
436 /// Return the major axis of the box. The major axis is the dimension with
437 /// the greatest difference between maximum and minimum.
438 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 unsigned int majorAxis() const IMATH_NOEXCEPT;
439
440 /// Return true if the box is empty, false otherwise. An empty box's
441 /// minimum is greater than its maximum.
442 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isEmpty() const IMATH_NOEXCEPT;
443
444 /// Return true if the box is larger than a single point, false otherwise.
445 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool hasVolume() const IMATH_NOEXCEPT;
446
447 /// Return true if the box contains all points, false otherwise.
448 /// An infinite box has a mimimum of `V::baseTypeMin()`
449 /// and a maximum of `V::baseTypeMax()`.
450 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isInfinite() const IMATH_NOEXCEPT;
451
452 /// @}
453};
454
455//----------------
456// Implementation
457//----------------
458
459template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<Vec2<T>>::Box() IMATH_NOEXCEPT
460{
461 makeEmpty();
462}
463
464template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<Vec2<T>>::Box (const Vec2<T>& point) IMATH_NOEXCEPT
465{
466 min = point;
467 max = point;
468}
469
470template <class T>
471IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<Vec2<T>>::Box (const Vec2<T>& minT, const Vec2<T>& maxT) IMATH_NOEXCEPT
472{
473 min = minT;
474 max = maxT;
475}
476
477template <class T>
478IMATH_HOSTDEVICE constexpr inline bool
479Box<Vec2<T>>::operator== (const Box<Vec2<T>>& src) const IMATH_NOEXCEPT
480{
481 return (min == src.min && max == src.max);
482}
483
484template <class T>
485IMATH_HOSTDEVICE constexpr inline bool
486Box<Vec2<T>>::operator!= (const Box<Vec2<T>>& src) const IMATH_NOEXCEPT
487{
488 return (min != src.min || max != src.max);
489}
490
491template <class T>
492IMATH_HOSTDEVICE inline void
493Box<Vec2<T>>::makeEmpty() IMATH_NOEXCEPT
494{
495 min = Vec2<T> (Vec2<T>::baseTypeMax());
496 max = Vec2<T> (Vec2<T>::baseTypeLowest());
497}
498
499template <class T>
500IMATH_HOSTDEVICE inline void
501Box<Vec2<T>>::makeInfinite() IMATH_NOEXCEPT
502{
503 min = Vec2<T> (Vec2<T>::baseTypeLowest());
504 max = Vec2<T> (Vec2<T>::baseTypeMax());
505}
506
507template <class T>
508IMATH_HOSTDEVICE inline void
509Box<Vec2<T>>::extendBy (const Vec2<T>& point) IMATH_NOEXCEPT
510{
511 if (point[0] < min[0])
512 min[0] = point[0];
513
514 if (point[0] > max[0])
515 max[0] = point[0];
516
517 if (point[1] < min[1])
518 min[1] = point[1];
519
520 if (point[1] > max[1])
521 max[1] = point[1];
522}
523
524template <class T>
525IMATH_HOSTDEVICE inline void
526Box<Vec2<T>>::extendBy (const Box<Vec2<T>>& box) IMATH_NOEXCEPT
527{
528 if (box.min[0] < min[0])
529 min[0] = box.min[0];
530
531 if (box.max[0] > max[0])
532 max[0] = box.max[0];
533
534 if (box.min[1] < min[1])
535 min[1] = box.min[1];
536
537 if (box.max[1] > max[1])
538 max[1] = box.max[1];
539}
540
541template <class T>
542IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
543Box<Vec2<T>>::intersects (const Vec2<T>& point) const IMATH_NOEXCEPT
544{
545 if (point[0] < min[0] || point[0] > max[0] || point[1] < min[1] || point[1] > max[1])
546 return false;
547
548 return true;
549}
550
551template <class T>
552IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
553Box<Vec2<T>>::intersects (const Box<Vec2<T>>& box) const IMATH_NOEXCEPT
554{
555 if (box.max[0] < min[0] || box.min[0] > max[0] || box.max[1] < min[1] || box.min[1] > max[1])
556 return false;
557
558 return true;
559}
560
561template <class T>
562IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Vec2<T>
563Box<Vec2<T>>::size() const IMATH_NOEXCEPT
564{
565 if (isEmpty())
566 return Vec2<T> (0);
567
568 return max - min;
569}
570
571template <class T>
572IMATH_HOSTDEVICE constexpr inline Vec2<T>
573Box<Vec2<T>>::center() const IMATH_NOEXCEPT
574{
575 return (max + min) / 2;
576}
577
578template <class T>
579IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
580Box<Vec2<T>>::isEmpty() const IMATH_NOEXCEPT
581{
582 if (max[0] < min[0] || max[1] < min[1])
583 return true;
584
585 return false;
586}
587
588template <class T>
589IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
590Box<Vec2<T>>::isInfinite() const IMATH_NOEXCEPT
591{
592 if (min[0] != std::numeric_limits<T>::lowest() ||
593 max[0] != std::numeric_limits<T>::max() ||
594 min[1] != std::numeric_limits<T>::lowest() ||
595 max[1] != std::numeric_limits<T>::max())
596 return false;
597
598 return true;
599}
600
601template <class T>
602IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
603Box<Vec2<T>>::hasVolume() const IMATH_NOEXCEPT
604{
605 if (max[0] <= min[0] || max[1] <= min[1])
606 return false;
607
608 return true;
609}
610
611template <class T>
612IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline unsigned int
613Box<Vec2<T>>::majorAxis() const IMATH_NOEXCEPT
614{
615 unsigned int major = 0;
616 Vec2<T> s = size();
617
618 if (s[1] > s[major])
619 major = 1;
620
621 return major;
622}
623
624///
625/// The Box<Vec3> template represents a 3D bounding box defined by
626/// minimum and maximum values of type Vec3.
627///
628template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Box<Vec3<T>>
629{
630 public:
631
632 /// @{
633 /// @name Direct access to bounds
634
635 /// The minimum value of the box.
636 Vec3<T> min;
637
638 /// The maximum value of the box.
639 Vec3<T> max;
640
641 /// @}
642
643 /// @{
644 /// @name Constructors
645
646 /// Empty by default
647 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box() IMATH_NOEXCEPT;
648
649 /// Construct a bounding box that contains a single point.
650 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box (const Vec3<T>& point) IMATH_NOEXCEPT;
651
652 /// Construct a bounding box with the given minimum and maximum points
653 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box (const Vec3<T>& minT, const Vec3<T>& maxT) IMATH_NOEXCEPT;
654
655 /// @}
656
657 /// Equality
658 IMATH_HOSTDEVICE constexpr bool operator== (const Box<Vec3<T>>& src) const IMATH_NOEXCEPT;
659
660 /// Inequality
661 IMATH_HOSTDEVICE constexpr bool operator!= (const Box<Vec3<T>>& src) const IMATH_NOEXCEPT;
662
663 /// Set the Box to be empty. A Box is empty if the mimimum is
664 /// greater than the maximum. makeEmpty() sets the mimimum to
665 /// std::numeric_limits<T>::max() and the maximum to
666 /// std::numeric_limits<T>::lowest().
667 IMATH_HOSTDEVICE void makeEmpty() IMATH_NOEXCEPT;
668
669 /// Extend the Box to include the given point.
670 IMATH_HOSTDEVICE void extendBy (const Vec3<T>& point) IMATH_NOEXCEPT;
671 /// Extend the Box to include the given box.
672
673 IMATH_HOSTDEVICE void extendBy (const Box<Vec3<T>>& box) IMATH_NOEXCEPT;
674
675 /// Make the box include the entire range of T.
676 IMATH_HOSTDEVICE void makeInfinite() IMATH_NOEXCEPT;
677
678 /// Return the size of the box. The size is of type `V`, defined as
679 /// (max-min). An empty box has a size of V(0), i.e. 0 in each dimension.
680 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3<T> size() const IMATH_NOEXCEPT;
681
682 /// Return the center of the box. The center is defined as
683 /// (max+min)/2. The center of an empty box is undefined.
684 IMATH_HOSTDEVICE constexpr Vec3<T> center() const IMATH_NOEXCEPT;
685
686 /// Return true if the given point is inside the box, false otherwise.
687 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const Vec3<T>& point) const IMATH_NOEXCEPT;
688
689 /// Return true if the given box is inside the box, false otherwise.
690 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const Box<Vec3<T>>& box) const IMATH_NOEXCEPT;
691
692 /// Return the major axis of the box. The major axis is the dimension with
693 /// the greatest difference between maximum and minimum.
694 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 unsigned int majorAxis() const IMATH_NOEXCEPT;
695
696 /// Return true if the box is empty, false otherwise. An empty box's
697 /// minimum is greater than its maximum.
698 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isEmpty() const IMATH_NOEXCEPT;
699
700 /// Return true if the box is larger than a single point, false otherwise.
701 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool hasVolume() const IMATH_NOEXCEPT;
702
703 /// Return true if the box contains all points, false otherwise.
704 /// An infinite box has a mimimum of`V::baseTypeMin()`
705 /// and a maximum of `V::baseTypeMax()`.
706 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isInfinite() const IMATH_NOEXCEPT;
707};
708
709//----------------
710// Implementation
711//----------------
712
713template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<Vec3<T>>::Box() IMATH_NOEXCEPT
714{
715 makeEmpty();
716}
717
718template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<Vec3<T>>::Box (const Vec3<T>& point) IMATH_NOEXCEPT
719{
720 min = point;
721 max = point;
722}
723
724template <class T>
725IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<Vec3<T>>::Box (const Vec3<T>& minT, const Vec3<T>& maxT) IMATH_NOEXCEPT
726{
727 min = minT;
728 max = maxT;
729}
730
731template <class T>
732IMATH_HOSTDEVICE constexpr inline bool
733Box<Vec3<T>>::operator== (const Box<Vec3<T>>& src) const IMATH_NOEXCEPT
734{
735 return (min == src.min && max == src.max);
736}
737
738template <class T>
739IMATH_HOSTDEVICE constexpr inline bool
740Box<Vec3<T>>::operator!= (const Box<Vec3<T>>& src) const IMATH_NOEXCEPT
741{
742 return (min != src.min || max != src.max);
743}
744
745template <class T>
746IMATH_HOSTDEVICE inline void
747Box<Vec3<T>>::makeEmpty() IMATH_NOEXCEPT
748{
749 min = Vec3<T> (Vec3<T>::baseTypeMax());
750 max = Vec3<T> (Vec3<T>::baseTypeLowest());
751}
752
753template <class T>
754IMATH_HOSTDEVICE inline void
755Box<Vec3<T>>::makeInfinite() IMATH_NOEXCEPT
756{
757 min = Vec3<T> (Vec3<T>::baseTypeLowest());
758 max = Vec3<T> (Vec3<T>::baseTypeMax());
759}
760
761template <class T>
762IMATH_HOSTDEVICE inline void
763Box<Vec3<T>>::extendBy (const Vec3<T>& point) IMATH_NOEXCEPT
764{
765 if (point[0] < min[0])
766 min[0] = point[0];
767
768 if (point[0] > max[0])
769 max[0] = point[0];
770
771 if (point[1] < min[1])
772 min[1] = point[1];
773
774 if (point[1] > max[1])
775 max[1] = point[1];
776
777 if (point[2] < min[2])
778 min[2] = point[2];
779
780 if (point[2] > max[2])
781 max[2] = point[2];
782}
783
784template <class T>
785IMATH_HOSTDEVICE inline void
786Box<Vec3<T>>::extendBy (const Box<Vec3<T>>& box) IMATH_NOEXCEPT
787{
788 if (box.min[0] < min[0])
789 min[0] = box.min[0];
790
791 if (box.max[0] > max[0])
792 max[0] = box.max[0];
793
794 if (box.min[1] < min[1])
795 min[1] = box.min[1];
796
797 if (box.max[1] > max[1])
798 max[1] = box.max[1];
799
800 if (box.min[2] < min[2])
801 min[2] = box.min[2];
802
803 if (box.max[2] > max[2])
804 max[2] = box.max[2];
805}
806
807template <class T>
808IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
809Box<Vec3<T>>::intersects (const Vec3<T>& point) const IMATH_NOEXCEPT
810{
811 if (point[0] < min[0] || point[0] > max[0] || point[1] < min[1] || point[1] > max[1] ||
812 point[2] < min[2] || point[2] > max[2])
813 return false;
814
815 return true;
816}
817
818template <class T>
819IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
820Box<Vec3<T>>::intersects (const Box<Vec3<T>>& box) const IMATH_NOEXCEPT
821{
822 if (box.max[0] < min[0] || box.min[0] > max[0] || box.max[1] < min[1] || box.min[1] > max[1] ||
823 box.max[2] < min[2] || box.min[2] > max[2])
824 return false;
825
826 return true;
827}
828
829template <class T>
830IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Vec3<T>
831Box<Vec3<T>>::size() const IMATH_NOEXCEPT
832{
833 if (isEmpty())
834 return Vec3<T> (0);
835
836 return max - min;
837}
838
839template <class T>
840IMATH_HOSTDEVICE constexpr inline Vec3<T>
841Box<Vec3<T>>::center() const IMATH_NOEXCEPT
842{
843 return (max + min) / 2;
844}
845
846template <class T>
847IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
848Box<Vec3<T>>::isEmpty() const IMATH_NOEXCEPT
849{
850 if (max[0] < min[0] || max[1] < min[1] || max[2] < min[2])
851 return true;
852
853 return false;
854}
855
856template <class T>
857IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
858Box<Vec3<T>>::isInfinite() const IMATH_NOEXCEPT
859{
860 if (min[0] != std::numeric_limits<T>::lowest() ||
861 max[0] != std::numeric_limits<T>::max() ||
862 min[1] != std::numeric_limits<T>::lowest() ||
863 max[1] != std::numeric_limits<T>::max() ||
864 min[2] != std::numeric_limits<T>::lowest() ||
865 max[2] != std::numeric_limits<T>::max())
866 return false;
867
868 return true;
869}
870
871template <class T>
872IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
873Box<Vec3<T>>::hasVolume() const IMATH_NOEXCEPT
874{
875 if (max[0] <= min[0] || max[1] <= min[1] || max[2] <= min[2])
876 return false;
877
878 return true;
879}
880
881template <class T>
882IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline unsigned int
883Box<Vec3<T>>::majorAxis() const IMATH_NOEXCEPT
884{
885 unsigned int major = 0;
886 Vec3<T> s = size();
887
888 if (s[1] > s[major])
889 major = 1;
890
891 if (s[2] > s[major])
892 major = 2;
893
894 return major;
895}
896
897IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
898
899#endif // INCLUDED_IMATHBOX_H
900

source code of include/Imath/ImathBox.h