Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
BoundingSphere.inl
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#ifndef DVD_CORE_MATH_BOUNDINGVOLUMES_BOUNDINGSPHERE_INL_
33#define DVD_CORE_MATH_BOUNDINGVOLUMES_BOUNDINGSPHERE_INL_
34
35namespace Divide {
36
37inline void BoundingSphere::fromBoundingBox(const BoundingBox& bBox) noexcept {
38 _center.set(bBox.getCenter());
39 _radius = (bBox.getMax() - _center).length();
40}
41
42inline void BoundingSphere::fromBoundingSphere(const BoundingSphere& bSphere) noexcept {
43 _center.set(bSphere.getCenter());
44 _radius = bSphere.getRadius();
45}
46
47// https://code.google.com/p/qe3e/source/browse/trunk/src/BoundingSphere.h?r=28
48inline void BoundingSphere::add(const BoundingSphere& bSphere) noexcept {
49 const F32 dist = (bSphere._center - _center).length();
50
51 if (_radius >= dist + bSphere._radius) {
52 return;
53 }
54
55 if (bSphere._radius >= dist + _radius) {
56 _center = bSphere._center;
57 _radius = bSphere._radius;
58 }
59
60 if (dist > EPSILON_F32) {
61 const F32 nRadius = (_radius + dist + bSphere._radius) * 0.5f;
62 const F32 ratio = (nRadius - _radius) / dist;
63 _center += (bSphere._center - _center) * ratio;
64
65 _radius = nRadius;
66 }
67}
68
69inline void BoundingSphere::addRadius(const BoundingSphere& bSphere) noexcept {
70 const F32 dist = (bSphere._center - _center).length() + bSphere._radius;
71 if (_radius < dist) {
72 _radius = dist;
73 }
74}
75
76inline void BoundingSphere::add(const vec3<F32>& point) noexcept {
77 const vec3<F32> diff(point - _center);
78 const F32 dist = diff.length();
79 if (_radius < dist) {
80 const F32 nRadius = (dist - _radius) * 0.5f;
81 _center += diff * (nRadius / dist);
82 _radius += nRadius;
83 }
84}
85
86inline void BoundingSphere::addRadius(const vec3<F32>& point) noexcept {
87 const F32 dist = (point - _center).length();
88 if (_radius < dist) {
89 _radius = dist;
90 }
91}
92
93inline void BoundingSphere::createFromPoints(const vector<vec3<F32>>& points) noexcept {
94 _radius = 0;
95 const F32 numPoints = to_F32(points.size());
96
97 for (const vec3<F32>& p : points) {
98 _center += p / numPoints;
99 }
100
101 for (const vec3<F32>& p : points) {
102 const F32 distance = (p - _center).length();
103
104 if (distance > _radius) {
105 _radius = distance;
106 }
107 }
108}
109
110inline void BoundingSphere::createFromPoints(const std::array<vec3<F32>, 8>& points) noexcept {
111 _radius = 0;
112
113 for (const vec3<F32>& p : points) {
114 _center += p / 8;
115 }
116
117 for (const vec3<F32>& p : points) {
118 const F32 distance = (p - _center).length();
119
120 if (distance > _radius) {
121 _radius = distance;
122 }
123 }
124}
125
126inline void BoundingSphere::reset() noexcept {
127 _center.reset();
128 _radius = 0.0f;
129}
130
131inline void BoundingSphere::setRadius(const F32 radius) noexcept {
132 _radius = radius;
133}
134
135inline void BoundingSphere::setCenter(const vec3<F32>& center) noexcept {
136 _center = center;
137}
138
139inline const vec3<F32>& BoundingSphere::getCenter() const noexcept { return _center; }
140
141inline F32 BoundingSphere::getRadius() const noexcept { return _radius; }
142
143inline F32 BoundingSphere::getDiameter() const noexcept { return _radius * 2; }
144
145inline F32 BoundingSphere::getDistanceFromPoint(const vec3<F32>& point) const noexcept
146{
147 return getCenter().distance(point) - getRadius();
148}
149inline F32 BoundingSphere::getDistanceSQFromPoint(const vec3<F32>& point) const noexcept
150{
151 // If this is negative, than the sphere contains the point, so we clamp min distance
152 return std::max(getCenter().distanceSquared(point) - SQUARED(getRadius()), 0.f);
153}
154inline vec4<F32> BoundingSphere::asVec4() const noexcept
155{
156 return vec4<F32>(getCenter(), getRadius());
157}
158} // namespace Divide
159
160#endif //DVD_CORE_MATH_BOUNDINGVOLUMES_BOUNDINGSPHERE_INL_
void fromBoundingBox(const BoundingBox &bBox) noexcept
void addRadius(const BoundingSphere &bSphere) noexcept
void createFromPoints(const vector< vec3< F32 > > &points) noexcept
const vec3< F32 > & getCenter() const noexcept
F32 getDistanceFromPoint(const vec3< F32 > &point) const noexcept
void fromBoundingSphere(const BoundingSphere &bSphere) noexcept
void setRadius(F32 radius) noexcept
F32 getRadius() const noexcept
vec4< F32 > asVec4() const noexcept
void add(const BoundingSphere &bSphere) noexcept
F32 getDiameter() const noexcept
void setCenter(const vec3< F32 > &center) noexcept
F32 getDistanceSQFromPoint(const vec3< F32 > &point) const noexcept
T length() const noexcept
return the vector's length
Definition: MathVectors.h:747
void reset() noexcept
set all the components back to 0
Definition: MathVectors.h:742
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr T SQUARED(T input) noexcept
Definition: MathHelper.inl:156
constexpr F32 to_F32(const T value)
constexpr F32 EPSILON_F32
eastl::vector< Type > vector
Definition: Vector.h:42