-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoundingBox.cpp
64 lines (53 loc) · 1.69 KB
/
BoundingBox.cpp
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
#include "Precompiled.hpp"
#include "BoundingBox.hpp"
#include <algorithm>
namespace
{
Math::BoundingBox::BoxCorner operator ++ (Math::BoundingBox::BoxCorner& corner, int)
{
Math::BoundingBox::BoxCorner old_corner = corner;
corner = static_cast<Math::BoundingBox::BoxCorner>(corner + 1);
return old_corner;
}
}
namespace Math
{
BoundingBox::BoundingBox()
{
}
BoundingBox::BoundingBox(const Vector3& minimum, const Vector3& maximum) :
minimum_(Vector3::minimise(minimum, maximum)),
maximum_(Vector3::maximise(minimum, maximum))
{
}
bool BoundingBox::contains(const Vector3& point) const
{
return
(minimum_.x <= point.x && point.x <= maximum_.x) &&
(minimum_.y <= point.y && point.y <= maximum_.y) &&
(minimum_.z <= point.z && point.z <= maximum_.z);
}
void BoundingBox::set(const Vector3& minimum, const Vector3& maximum)
{
minimum_ = Vector3::minimise(minimum, maximum);
maximum_ = Vector3::maximise(minimum, maximum);
}
Vector3 BoundingBox::get_corner(BoxCorner corner) const
{
const float x = (corner & 1) ? maximum_.x : minimum_.x;
const float y = (corner & 2) ? maximum_.y : minimum_.y;
const float z = (corner & 4) ? maximum_.z : minimum_.z;
return Vector3(x, y, z);
}
BoundingBox::CornerArray BoundingBox::get_all_corners() const
{
CornerArray result;
BoxCorner corner = NEAR_BOTTOM_LEFT;
std::generate_n(std::begin(result), result.size(), [this, &corner]() { return get_corner(corner++); });
return result;
}
BoundingBox BoundingBox::compute_containing_box(const BoundingBox& a, const BoundingBox& b)
{
return BoundingBox(Vector3::minimise(a.minimum_corner(), b.minimum_corner()), Vector3::maximise(a.maximum_corner(), b.maximum_corner()));
}
}