forked from tuxalin/THST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbox.h
345 lines (292 loc) · 9.65 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//
// bbox.h
//
//
#pragma once
#include <algorithm>
#include <cmath>
#include <ostream>
#include <stdint.h>
namespace spatial {
namespace box {
enum VolumeMode {
eNormalVolume = 0, // Faster but can cause poor merges
eSphericalVolume // Slower but helps certain merge cases
};
enum RegionType {
eNW = 0, ///< North-West (Top left)
eNE, ///< North-East (Top right)
eSW, ///< South-West (Bottom left)
eSE ///< South-East (Bottom right)
};
/// Initialize a bounding box with an empty box.
struct empty_init {};
} // namespace spatial
/// Minimal bounding bbox (n-dimensional)
template <typename T, int Dimension> struct BoundingBox {
T min[Dimension];
T max[Dimension];
BoundingBox();
BoundingBox(box::empty_init);
BoundingBox(const T min[Dimension], const T max[Dimension]);
void extend(const T point[Dimension]);
void extend(const BoundingBox &bbox);
BoundingBox extended(const BoundingBox &bbox) const;
void set(const T min[Dimension], const T max[Dimension]);
void translate(const T point[Dimension]);
bool overlaps(const BoundingBox &bbox) const;
bool overlaps(const T point[Dimension], T radius) const;
bool contains(const BoundingBox &bbox) const;
bool contains(const T point[Dimension]) const;
void center(T center[Dimension]) const;
template <int VolumeMode, typename RealType> RealType volume() const;
BoundingBox quad2d(box::RegionType type) const;
private:
void checkValid() const;
template <typename RealType> RealType normalVolume() const;
// The exact volume of the bounding sphere for the given bbox.
template <typename RealType> RealType sphericalVolume() const;
/// Unit sphere constant for required number of dimensions
template <typename RealType> static RealType unitSphereVolume();
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace detail {
/// C++03 backward compatibility
/// http://en.cppreference.com/w/cpp/types/numeric_limits/lowest
template <typename T, bool is_integer> struct NumericLimitsImpl;
template <typename T> struct NumericLimitsImpl<T, false> {
static const T highest() { return std::numeric_limits<T>::max(); }
static const T lowest() { return -std::numeric_limits<T>::max(); }
};
template <typename T> struct NumericLimitsImpl<T, true> {
static const T highest() { return std::numeric_limits<T>::max(); }
static const T lowest() { return std::numeric_limits<T>::min(); }
};
template <typename T>
struct NumericLimits
: public NumericLimitsImpl<T, std::numeric_limits<T>::is_integer> {};
} // namespace detail
#define BBOX_TEMPLATE template <typename T, int Dimension>
#define BBOX_QUAL BoundingBox<T, Dimension>
BBOX_TEMPLATE
BBOX_QUAL::BoundingBox() {}
BBOX_TEMPLATE
BBOX_QUAL::BoundingBox(const T min[Dimension], const T max[Dimension]) {
set(min, max);
}
BBOX_TEMPLATE
BBOX_QUAL::BoundingBox(box::empty_init) {
for (size_t index = 0; index < (size_t)Dimension; ++index) {
min[index] = spatial::detail::NumericLimits<T>::highest();
max[index] = spatial::detail::NumericLimits<T>::lowest();
}
}
BBOX_TEMPLATE
void BBOX_QUAL::set(const T min[Dimension], const T max[Dimension]) {
// loop will get unrolled
for (int axis = 0; axis < Dimension; ++axis) {
this->min[axis] = min[axis];
this->max[axis] = max[axis];
}
checkValid();
}
BBOX_TEMPLATE
void BBOX_QUAL::translate(const T point[Dimension]) {
// loop will get unrolled
for (int axis = 0; axis < Dimension; ++axis) {
this->min[axis] += point[axis];
this->max[axis] += point[axis];
}
checkValid();
}
BBOX_TEMPLATE
void BBOX_QUAL::extend(const T point[Dimension]) {
// loop will get unrolled
for (int index = 0; index < Dimension; ++index) {
min[index] = std::min(min[index], point[index]);
max[index] = std::max(max[index], point[index]);
}
checkValid();
}
BBOX_TEMPLATE
void BBOX_QUAL::extend(const BoundingBox &bbox) {
bbox.checkValid();
// loop will get unrolled
for (int index = 0; index < Dimension; ++index) {
min[index] = std::min(min[index], bbox.min[index]);
max[index] = std::max(max[index], bbox.max[index]);
}
checkValid();
}
BBOX_TEMPLATE
typename BBOX_QUAL::BoundingBox
BBOX_QUAL::extended(const BoundingBox &obbox) const {
checkValid();
BoundingBox res;
// loop will get unrolled
for (int index = 0; index < Dimension; ++index) {
res.min[index] = std::min(min[index], obbox.min[index]);
res.max[index] = std::max(max[index], obbox.max[index]);
}
return res;
}
BBOX_TEMPLATE
bool BBOX_QUAL::overlaps(const BoundingBox &bbox) const {
// loop will get unrolled
for (int index = 0; index < Dimension; ++index) {
if (min[index] > bbox.max[index] || bbox.min[index] > max[index]) {
return false;
}
}
return true;
}
BBOX_TEMPLATE
bool BBOX_QUAL::overlaps(const T center[Dimension], T radius) const {
T point[Dimension];
for (int index = 0; index < Dimension; ++index)
point[index] = center[index];
// find the closest point near the circle
for (int index = 0; index < Dimension; ++index) {
if (point[index] > max[index])
point[index] = max[index];
if (point[index] < min[index])
point[index] = min[index];
}
// compute euclidean distance between points
double distance = 0;
for (int index = 0; index < Dimension; ++index) {
const T d = point[index] - center[index];
distance += (double)d * d;
}
return distance < (radius * radius); // avoid square root
}
BBOX_TEMPLATE
bool BBOX_QUAL::contains(const T point[Dimension]) const {
// loop will get unrolled
for (int index = 0; index < Dimension; ++index) {
if (min[index] > point[index] || point[index] > max[index]) {
return false;
}
}
return true;
}
BBOX_TEMPLATE
bool BBOX_QUAL::contains(const BoundingBox &bbox) const {
// loop will get unrolled
for (int index = 0; index < Dimension; ++index) {
if (min[index] > bbox.min[index] || bbox.max[index] > max[index]) {
return false;
}
}
return true;
}
BBOX_TEMPLATE
void BBOX_QUAL::center(T center[Dimension]) const {
for (int i = 0; i < Dimension; ++i) {
center[i] = min[i] + (max[i] - min[i]) / 2;
}
}
BBOX_TEMPLATE
template <int VolumeMode, typename RealType>
RealType BBOX_QUAL::volume() const {
if (VolumeMode == spatial::box::eSphericalVolume)
return sphericalVolume<RealType>();
return normalVolume<RealType>();
}
BBOX_TEMPLATE
BBOX_QUAL BBOX_QUAL::quad2d(box::RegionType type) const {
const T halfW = (max[0] - min[0]) / 2;
const T halfH = (max[1] - min[1]) / 2;
switch (type) {
case box::eNW: {
const T hmin[Dimension] = { min[0], min[1] + halfH };
const T hmax[Dimension] = { min[0] + halfW, max[1] };
return BoundingBox(hmin, hmax);
}
case box::eNE: {
const T hmin[Dimension] = { min[0] + halfW, min[1] + halfH };
return BoundingBox(hmin, max);
}
case box::eSW: {
const T hmax[Dimension] = { min[0] + halfW, min[1] + halfH };
return BoundingBox(min, hmax);
}
case box::eSE: {
const T hmin[Dimension] = { min[0] + halfW, min[1] };
const T hmax[Dimension] = { max[0], min[1] + halfH };
return BoundingBox(hmin, hmax);
}
default:
assert(false);
return *this;
}
}
BBOX_TEMPLATE
void BBOX_QUAL::checkValid() const {
#ifndef NDEBUG
for (int index = 0; index < Dimension; ++index) {
assert(min[index] <= max[index]);
}
#endif
}
BBOX_TEMPLATE
template <typename RealType> RealType BBOX_QUAL::normalVolume() const {
RealType volume = (RealType)1;
for (int index = 0; index < Dimension; ++index) {
volume *= max[index] - min[index];
}
assert(volume >= (RealType)0);
return volume;
}
// The exact volume of the bounding sphere for the given bbox
BBOX_TEMPLATE
template <typename RealType> RealType BBOX_QUAL::sphericalVolume() const {
RealType sumOfSquares = (RealType)0;
for (int index = 0; index < Dimension; ++index) {
RealType halfExtent = (max[index] - min[index]) * (RealType)0.5;
sumOfSquares += halfExtent * halfExtent;
}
RealType radius = (RealType)std::sqrt(sumOfSquares);
// Pow maybe slow, so test for common dims like 2,3 and just use x*x, x*x*x.
if (Dimension == 3) {
return (radius * radius * radius * unitSphereVolume<RealType>());
}
else if (Dimension == 2) {
return (radius * radius * unitSphereVolume<RealType>());
}
else {
return (RealType)(pow(radius, Dimension) * unitSphereVolume<RealType>());
}
}
BBOX_TEMPLATE
template <typename RealType> RealType BBOX_QUAL::unitSphereVolume() {
// Precomputed volumes of the unit spheres for the first few dimensions
static const float kVolumes[] = {
0.000000f, 2.000000f, 3.141593f, // Dimension 0,1,2
4.188790f, 4.934802f, 5.263789f, // Dimension 3,4,5
5.167713f, 4.724766f, 4.058712f, // Dimension 6,7,8
3.298509f, 2.550164f, 1.884104f, // Dimension 9,10,11
1.335263f, 0.910629f, 0.599265f, // Dimension 12,13,14
0.381443f, 0.235331f, 0.140981f, // Dimension 15,16,17
0.082146f, 0.046622f, 0.025807f, // Dimension 18,19,20
};
static const RealType val = (RealType)kVolumes[Dimension];
return val;
} // namespace box
template <typename T>
std::ostream &operator<<(std::ostream &stream, const BoundingBox<T, 2> &bbox) {
stream << "min: " << bbox.min[0] << " " << bbox.min[1] << " ";
stream << "max: " << bbox.max[0] << " " << bbox.max[1];
return stream;
}
template <typename T>
std::ostream &operator<<(std::ostream &stream, const BoundingBox<T, 3> &bbox) {
stream << "min: " << bbox.min[0] << " " << bbox.min[1] << " " << bbox.min[2]
<< " ";
stream << "max: " << bbox.max[0] << " " << bbox.max[1] << " " << bbox.max[2]
<< " ";
return stream;
}
#undef BBOX_TEMPLATE
#undef BBOX_QUAL
} // namespace spatial