-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBox.hpp
100 lines (79 loc) · 2.07 KB
/
Box.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
#pragma once
#include <limits>
#include "Vector.hpp"
namespace Pvl {
template <typename Vec>
class BoundingBox {
Vec lower_;
Vec upper_;
public:
using Vector = Vec;
using Float = typename Vec::Float;
BoundingBox()
: lower_(std::numeric_limits<Float>::max())
, upper_(std::numeric_limits<Float>::lowest()) {}
BoundingBox(const Vec& lower, const Vec& upper)
: lower_(lower)
, upper_(upper) {}
Vec& lower() {
return lower_;
}
const Vec& lower() const {
return lower_;
}
Vec& upper() {
return upper_;
}
const Vec& upper() const {
return upper_;
}
Vec size() const {
return upper_ - lower_;
}
Vec center() const {
return Float(0.5) * (upper_ + lower_);
}
bool contains(const Vec& p) const {
for (int i = 0; i < Vec::size(); ++i) {
if (p[i] < lower_[i] || p[i] > upper_[i]) {
return false;
}
}
return true;
}
void extend(const Vec& p) {
lower_ = min(lower_, p);
upper_ = max(upper_, p);
}
void extend(const BoundingBox& b) {
extend(b.lower());
extend(b.upper());
}
};
using Box2f = BoundingBox<Vec2f>;
using Box3f = BoundingBox<Vec3f>;
/// \brief Splits the box along given coordinate.
///
/// The splitting plane must pass through the box.
template <typename Box, typename T>
std::pair<Box, Box> splitBox(const Box& box, const int dim, const T x) {
/*ASSERT(isValid());*/
PVL_ASSERT(dim < Box::Vector::size());
PVL_ASSERT(x >= box.lower()[dim] && x <= box.upper()[dim]);
Box b1 = box;
Box b2 = box;
b1.upper()[dim] = x;
b2.lower()[dim] = x;
return std::make_pair(b1, b2);
}
template <typename Box>
bool overlaps(const Box& box1, const Box& box2) {
constexpr int Dim = Box::Vector::size();
for (int i = 0; i < Dim; ++i) {
if (box1.lower()[i] > box2.upper()[i] || box2.lower()[i] > box1.upper()[i]) {
return false;
}
}
return true;
}
} // namespace Pvl