-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoundingBox.hpp
125 lines (95 loc) · 2.49 KB
/
BoundingBox.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma once
#ifndef __BOUNDINGBOX_HPP__
#define __BOUNDINGBOX_HPP__
#include "Vector3.hpp"
#include <type_traits>
#include <iterator>
#include <numeric>
#include <functional>
#include <array>
namespace Math
{
class BoundingBox
{
public:
enum BoxCorner
{
NEAR_BOTTOM_LEFT,
NEAR_BOTTOM_RIGHT,
NEAR_TOP_LEFT,
NEAR_TOP_RIGHT,
FAR_BOTTOM_LEFT,
FAR_BOTTOM_RIGHT,
FAR_TOP_LEFT,
FAR_TOP_RIGHT
};
typedef std::array<Vector3, 8> CornerArray;
private:
Vector3 minimum_;
Vector3 maximum_;
public:
//--------------------------------------------------------------------------
// Constructors
//
BoundingBox();
BoundingBox(const Vector3& minimum, const Vector3& maximum);
BoundingBox(const BoundingBox& box) : minimum_(box.minimum_), maximum_(box.maximum_)
{
}
//--------------------------------------------------------------------------
// Assignment
//
BoundingBox& operator = (const BoundingBox& box)
{
minimum_ = box.minimum_;
maximum_ = box.maximum_;
return *this;
}
//--------------------------------------------------------------------------
// Comparison
//
bool operator == (const BoundingBox& box) const
{
return minimum_ == box.minimum_ && maximum_ == box.maximum_;
}
bool operator != (const BoundingBox& box) const
{
return minimum_ != box.minimum_ || maximum_ != box.maximum_;
}
bool contains(const Vector3& point) const;
bool is_empty() const
{
return minimum_ == maximum_;
}
//--------------------------------------------------------------------------
// Accessors
//
void set(const Vector3& minimum, const Vector3& maximum);
const Vector3& minimum_corner() const
{
return minimum_;
}
const Vector3& maximum_corner() const
{
return maximum_;
}
Vector3 get_corner(BoxCorner corner) const;
CornerArray get_all_corners() const;
//--------------------------------------------------------------------------
// Computation
//
static BoundingBox compute_containing_box(const BoundingBox& a, const BoundingBox& b);
template <class Iterator>
typename std::enable_if<std::is_same<typename std::iterator_traits<Iterator>::value_type, BoundingBox>::value, BoundingBox>::type
static compute_containing_box(Iterator begin, Iterator end)
{
if (begin != end)
{
BoundingBox bounds = *begin++;
return std::accumulate(begin, end, bounds, std::ptr_fun(&compute_containing_box));
}
return BoundingBox();
}
};
} // namespace Math
#endif // __BOUNDINGBOX_HPP__