Coverage Report

Created: 2022-08-24 06:20

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