-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoundingSphere.hpp
122 lines (96 loc) · 2.46 KB
/
BoundingSphere.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
#pragma once
#ifndef __MATHS_BOUNDINGSPHERE_HPP__
#define __MATHS_BOUNDINGSPHERE_HPP__
#include "Vector3.hpp"
#include <type_traits>
#include <iterator>
#include <algorithm>
#include <functional>
#include <numeric>
namespace Math
{
class BoundingSphere
{
private:
Vector3 center_;
float radius_;
public:
//--------------------------------------------------------------------------
// Constructors
//
BoundingSphere() : radius_(0.0f)
{
}
BoundingSphere(const Vector3& center, float radius) : center_(center), radius_(std::abs(radius))
{
}
BoundingSphere(const BoundingSphere& sphere) : center_(sphere.center_), radius_(sphere.radius_)
{
}
//--------------------------------------------------------------------------
// Assignment
//
BoundingSphere& operator = (const BoundingSphere& sphere)
{
center_ = sphere.center_;
radius_ = sphere.radius_;
return *this;
}
//--------------------------------------------------------------------------
// Comparison
//
bool operator == (const BoundingSphere& sphere) const
{
return center_ == sphere.center_ && radius_ == sphere.radius_;
}
bool operator != (const BoundingSphere& sphere) const
{
return center_ != sphere.center_ || radius_ != sphere.radius_;
}
bool contains(const Vector3& point) const
{
return Vector3(center_ - point).length_squared() <= radius_ * radius_;
}
bool is_empty()
{
return radius_ <= FLT_EPSILON;
}
//--------------------------------------------------------------------------
// Accessors
//
void set(const Vector3& center, float radius)
{
center_ = center;
radius_ = std::abs(radius);
}
void center(const Vector3& center)
{
center_ = center;
}
const Vector3& center() const
{
return center_;
}
void radius(float radius)
{
radius_ = std::abs(radius);
}
float radius() const
{
return radius_;
}
static BoundingSphere compute_containing_sphere(const BoundingSphere& a, const BoundingSphere& b);
template <class Iterator>
typename std::enable_if<std::is_same<typename std::iterator_traits<Iterator>::value_type, BoundingSphere>::value, BoundingSphere>::type
static compute_containing_sphere(Iterator begin, Iterator end)
{
if (begin != end)
{
BoundingSphere bounds = *begin++;
return std::accumulate(begin, end, bounds, std::ptr_fun(&compute_containing_sphere));
}
return BoundingSphere();
}
};
} // namespace Math
#endif // __MATHS_BOUNDINGSPHERE_H__