-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbox.h
203 lines (179 loc) · 6.38 KB
/
bbox.h
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#pragma once
#include "vec.h"
#include <cfloat>
// NOTE on usage of BBoxes
// all BBoxes are initialized so that
// convex(BBox(), bb) == bb
// for any bb
// **************************************************************************
// * BBox2 stores 2-dimensional axis aligned bounding boxes
// **************************************************************************
template<class N>
class BBox2 {
public: // data
Vec2<N> minp, maxp;
public: // constructors
BBox2(const Vec2<N> &minpp, const Vec2<N> &maxpp) :
minp(minpp), maxp(maxpp) {}
inline BBox2(const BBox2<N> &bb) : minp(bb.minp), maxp(bb.maxp) {}
inline BBox2(); // specialized implementations for float/double only
};
template<> inline
BBox2<float>::BBox2() : minp(FLT_MAX, FLT_MAX), maxp(-FLT_MAX, -FLT_MAX) {}
template<> inline
BBox2<double>::BBox2() : minp(DBL_MAX, DBL_MAX), maxp(-DBL_MAX, -DBL_MAX) {}
template<class N>
inline bool isEmpty(const BBox2<N> &bb) {
return bb.maxp[0] < bb.minp[0] ||
bb.maxp[1] < bb.minp[1];
}
template<class N>
inline bool isIn(const Vec2<N> &p, const BBox2<N> &bb) {
return bb.minp[0] <= p[0] && p[0] <= bb.maxp[0] && bb.minp[1] <= p[1] && p[1] <= bb.maxp[1];
}
template<class N>
inline bool hasIsct(const BBox2<N> &lhs, const BBox2<N> &rhs) {
return lhs.minp[0] <= rhs.maxp[0] && lhs.maxp[0] >= rhs.minp[0] && lhs.minp[1] <= rhs.maxp[1] && lhs.maxp[1] >= rhs.minp[1];
}
template<class N>
inline BBox2<N> convex(const BBox2<N> &lhs, const BBox2<N> &rhs) {
return BBox2<N>(min(lhs.minp, rhs.minp), max(lhs.maxp, rhs.maxp));
}
template<class N>
inline BBox2<N> isct(const BBox2<N> &lhs, const BBox2<N> &rhs) {
return BBox2<N>(max(lhs.minp, rhs.minp), min(lhs.maxp, rhs.maxp));
}
template<class N>
inline Vec2<N> dim(const BBox2<N> &bb) {
return bb.maxp - bb.minp;
}
template<class N>
inline N perimeter(const BBox2<N> &bb) {
Vec2<N> d = dim(bb);
return 2 * (d[0] + d[1]);
}
template<class N>
inline std::ostream& operator<<(std::ostream &out, const BBox2<N> &bb) {
return out << "[min" << bb.minp << ";max" << bb.maxp << ']';
}
// **************************************************************************
// * BBox3 stores 3-dimensional axis aligned bounding boxes
// **************************************************************************
template<class N>
class BBox3 {
public: // data
Vec3<N> minp, maxp;
public: // constructors
BBox3(const Vec3<N> &minpp, const Vec3<N> &maxpp) :
minp(minpp), maxp(maxpp) {}
inline BBox3(const BBox3<N> &bb) : minp(bb.minp), maxp(bb.maxp) {}
inline BBox3(); // specialized implementations for float/double only
};
template<> inline
BBox3<float>::BBox3() : minp(FLT_MAX, FLT_MAX, FLT_MAX), maxp(-FLT_MAX, -FLT_MAX, -FLT_MAX) {}
template<> inline
BBox3<double>::BBox3() : minp(DBL_MAX, DBL_MAX, DBL_MAX), maxp(-DBL_MAX, -DBL_MAX, -DBL_MAX) {}
template<class N>
inline bool isEmpty(const BBox3<N> &bb) {
return bb.maxp[0] < bb.minp[0] ||
bb.maxp[1] < bb.minp[1] ||
bb.maxp[2] < bb.minp[2];
}
template<class N>
inline bool isIn(const Vec3<N> &p, const BBox3<N> &bb) {
return bb.minp[0] <= p[0] && p[0] <= bb.maxp[0] && bb.minp[1] <= p[1] && p[1] <= bb.maxp[1] && bb.minp[2] <= p[2] && p[2] <= bb.maxp[2];
}
template<class N>
inline bool hasIsct(const BBox3<N> &lhs, const BBox3<N> &rhs) {
return lhs.minp[0] <= rhs.maxp[0] && lhs.maxp[0] >= rhs.minp[0] &&
lhs.minp[1] <= rhs.maxp[1] && lhs.maxp[1] >= rhs.minp[1] &&
lhs.minp[2] <= rhs.maxp[2] && lhs.maxp[2] >= rhs.minp[2];
}
template<class N>
inline BBox3<N> convex(const BBox3<N> &lhs, const BBox3<N> &rhs) {
return BBox3<N>(min(lhs.minp, rhs.minp), max(lhs.maxp, rhs.maxp));
}
template<class N>
inline BBox3<N> isct(const BBox3<N> &lhs, const BBox3<N> &rhs) {
return BBox3<N>(max(lhs.minp, rhs.minp), min(lhs.maxp, rhs.maxp));
}
template<class N>
inline Vec3<N> dim(const BBox3<N> &bb) {
return bb.maxp - bb.minp;
}
template<class N>
inline N surfaceArea(const BBox3<N> &bb) {
Vec3<N> d = dim(bb);
return 2 * (d[1] * d[2] + d[0] * d[2] + d[0] * d[1]);
}
template<class N>
inline std::ostream& operator<<(std::ostream &out, const BBox3<N> &bb) {
return out << "[min" << bb.minp << ";max" << bb.maxp << ']';
}
// **************************************************************************
// * BBox4 stores 4-dimensional axis aligned bounding boxes
// **************************************************************************
template<class N>
class BBox4 {
public: // data
Vec4<N> minp, maxp;
public: // constructors
BBox4(const Vec4<N> &minpp, const Vec4<N> &maxpp) :
minp(minpp), maxp(maxpp) {}
inline BBox4(const BBox4<N> &bb) : minp(bb.minp), maxp(bb.maxp) {}
inline BBox4(); // specialized implementations for float/double only
};
template<> inline
BBox4<float>::BBox4() :
minp(FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX),
maxp(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX)
{}
template<> inline
BBox4<double>::BBox4() :
minp(DBL_MAX, DBL_MAX, DBL_MAX, DBL_MAX),
maxp(-DBL_MAX, -DBL_MAX, -DBL_MAX, -DBL_MAX)
{}
template<class N>
inline bool isEmpty(const BBox4<N> &bb) {
return bb.maxp[0] < bb.minp[0] ||
bb.maxp[1] < bb.minp[1] ||
bb.maxp[2] < bb.minp[2] ||
bb.maxp[3] < bb.minp[3];
}
template<class N>
inline bool isIn(const Vec4<N> &p, const BBox4<N> &bb) {
return bb.minp[0] <= p[0] && p[0] <= bb.maxp[0] &&
bb.minp[1] <= p[1] && p[1] <= bb.maxp[1] &&
bb.minp[2] <= p[2] && p[2] <= bb.maxp[2] &&
bb.minp[3] <= p[3] && p[3] <= bb.maxp[3];
}
template<class N>
inline bool hasIsct(const BBox4<N> &lhs, const BBox4<N> &rhs) {
return lhs.minp[0] <= rhs.maxp[0] && lhs.maxp[0] >= rhs.minp[0] &&
lhs.minp[1] <= rhs.maxp[1] && lhs.maxp[1] >= rhs.minp[1] &&
lhs.minp[2] <= rhs.maxp[2] && lhs.maxp[2] >= rhs.minp[2] &&
lhs.minp[3] <= rhs.maxp[3] && lhs.maxp[3] >= rhs.minp[3];
}
template<class N>
inline BBox4<N> convex(const BBox4<N> &lhs, const BBox4<N> &rhs) {
return BBox4<N>(min(lhs.minp, rhs.minp), max(lhs.maxp, rhs.maxp));
}
template<class N>
inline BBox4<N> isct(const BBox4<N> &lhs, const BBox4<N> &rhs) {
return BBox4<N>(max(lhs.minp, rhs.minp), min(lhs.maxp, rhs.maxp));
}
template<class N>
inline Vec4<N> dim(const BBox4<N> &bb) {
return bb.maxp - bb.minp;
}
template<class N>
inline std::ostream& operator<<(std::ostream &out, const BBox4<N> &bb) {
return out << "[min" << bb.minp << ";max" << bb.maxp << ']';
}
// define some common useful versions of the boxes
typedef BBox2<float> BBox2f;
typedef BBox3<float> BBox3f;
typedef BBox4<float> BBox4f;
typedef BBox2<double> BBox2d;
typedef BBox3<double> BBox3d;
typedef BBox4<double> BBox4d;