-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoundingSphere.cpp
More file actions
62 lines (51 loc) · 1.58 KB
/
BoundingSphere.cpp
File metadata and controls
62 lines (51 loc) · 1.58 KB
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
#include "BoundingSphere.h"
#include <cmath>
BoundingSphere::BoundingSphere(const float& cx,
const float& cy,
const float& cz,
const float& rad) : _center(cx, cy, cz), _radius(rad)
{
}
BoundingSphere::~BoundingSphere(void)
{
}
void BoundingSphere::setRadius(const float& rad)
{
_radius = rad;
}
void BoundingSphere::setCenter(const float& cx, const float& cy, const float& cz)
{
_center.setX(cx);
_center.setY(cy);
_center.setZ(cz);
}
void BoundingSphere::setCenter(const QVector3D& cen)
{
_center = cen;
}
void BoundingSphere::addSphere(const BoundingSphere& other)
{
if (qFuzzyCompare(_center, other._center) && qFuzzyCompare(_radius, other._radius)) // same sphere
return;
float smallerRadius = _radius < other._radius ? _radius : other._radius;
if (_center.distanceToPoint(other._center) < smallerRadius) // one sphere inside other
{
if (_radius == smallerRadius) // this sphere is smaller
{
// make other sphere the bounding sphere
_center = other._center;
_radius = other._radius;
}
else // other sphere is already inside this one, do nothing
return;
}
else // intersecting or touching or spaced
{
QVector3D toThisEnd = (_center - other._center).normalized();
QVector3D toOtherEnd = (other._center - _center).normalized();
QVector3D thisEndPoint = _center + toThisEnd * _radius;
QVector3D otherEndPoint = other._center + toOtherEnd * other._radius;
_radius = thisEndPoint.distanceToPoint(otherEndPoint) / 2;
_center = thisEndPoint + toOtherEnd * _radius;
}
}