Newer
Older
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* \file Calculation of a minimum bounding sphere for a vector of points
* \author Karsten Rink
* \date 2014-07-11
* \brief Implementation of the BoundingSphere class.
*
* \copyright
* Copyright (c) 2013, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
#include "BoundingSphere.h"
// ThirdParty/logog
#include "logog/include/logog.hpp"
#include "MathTools.h"
namespace GeoLib {
BoundingSphere::BoundingSphere()
: _center(0,0,0), _radius(-1)
{
}
BoundingSphere::BoundingSphere(const BoundingSphere &sphere)
: _center(sphere.getCenter()), _radius(sphere.getRadius())
{
}
BoundingSphere::BoundingSphere(const GeoLib::Point &p)
: _center(p), _radius(std::numeric_limits<double>::epsilon())
{
}
BoundingSphere::BoundingSphere(const GeoLib::Point &p, double radius)
: _center(p), _radius(radius)
{
}
BoundingSphere::BoundingSphere(const GeoLib::Point &p, const GeoLib::Point &q)
{
const MathLib::Vector3 a(p, q);
const MathLib::Vector3 o(0.5*a);
_radius = o.getLength() + std::numeric_limits<double>::epsilon();
_center = MathLib::Vector3(p) + o;
}
BoundingSphere::BoundingSphere(const GeoLib::Point &p, const GeoLib::Point &q, const GeoLib::Point &r)
{
const MathLib::Vector3 a(p,r);
const MathLib::Vector3 b(p,q);
const MathLib::Vector3 cross_ab(crossProduct(a,b));
const double denom = 2.0 * scalarProduct(cross_ab,cross_ab);
const MathLib::Vector3 o = (scalarProduct(b,b) * crossProduct(cross_ab, a)
+ scalarProduct(a,a) * crossProduct(b, cross_ab))
* (1.0 / denom);
_radius = o.getLength() + std::numeric_limits<double>::epsilon();
_center = MathLib::Vector3(p) + o;
}
BoundingSphere::BoundingSphere(const GeoLib::Point &p, const GeoLib::Point &q, const GeoLib::Point &r, const GeoLib::Point &s)
{
const MathLib::Vector3 a(p, q);
const MathLib::Vector3 b(p, r);
const MathLib::Vector3 c(p, s);
// det of matrix [a^T, b^T, c^T]^T
const double denom = 2.0 * (a[0] * (b[1] * c[2] - c[1] * b[2])
- b[0] * (a[1] * c[2] - c[1] * a[2])
+ c[0] * (a[1] * b[2] - b[1] * a[2]));
const MathLib::Vector3 o = (scalarProduct(c,c) * crossProduct(a,b)
+ scalarProduct(b,b) * crossProduct(c,a)
+ scalarProduct(a,a) * crossProduct(b,c))
* (1.0 / denom);
_radius = o.getLength() + std::numeric_limits<double>::epsilon();
_center = MathLib::Vector3(p) + o;
}
BoundingSphere::BoundingSphere(const std::vector<GeoLib::Point*> &points)
: _center(0,0,0), _radius(-1)
{
std::vector<GeoLib::Point*> sphere_points;
sphere_points.reserve(points.size());
std::copy(points.cbegin(), points.cend(), std::back_inserter(sphere_points));
const BoundingSphere bounding_sphere = recurseCalculation(sphere_points, sphere_points.size(), 0);
this->_center = bounding_sphere.getCenter();
this->_radius = bounding_sphere.getRadius();
}
BoundingSphere BoundingSphere::recurseCalculation(std::vector<GeoLib::Point*> &sphere_points, std::size_t idx, std::size_t boundary_points)
{
BoundingSphere sphere;
switch(boundary_points)
{
case 0:
sphere = BoundingSphere();
break;
case 1:
sphere = BoundingSphere(*sphere_points[0]);
break;
case 2:
sphere = BoundingSphere(*sphere_points[0], *sphere_points[1]);
break;
case 3:
sphere = BoundingSphere(*sphere_points[0], *sphere_points[1], *sphere_points[2]);
break;
case 4:
{
sphere = BoundingSphere(*sphere_points[0], *sphere_points[1], *sphere_points[2], *sphere_points[3]);
return sphere;
}
}
for(std::size_t i=0; i<idx; ++i)
{
if(sphere.sqrPointDist(*sphere_points[i]) > 0)
{
for(std::size_t j=i; j>0; --j)
{
GeoLib::Point* tmp = sphere_points[j];
sphere_points[j] = sphere_points[j-1];
sphere_points[j - 1] = tmp;
}
sphere = recurseCalculation(sphere_points, i, boundary_points+1);
}
}
return sphere;
}
double BoundingSphere::sqrPointDist(const GeoLib::Point pnt) const
{
return MathLib::sqrDist(_center.getCoords(), pnt.getCoords())-(_radius*_radius);
}
std::vector<GeoLib::Point*>* BoundingSphere::getSpherePoints(std::size_t n_points) const
{
std::vector<GeoLib::Point*> *pnts = new std::vector<GeoLib::Point*>;
pnts->reserve(n_points);
srand ( static_cast<unsigned>(time(NULL)) );
for (std::size_t k(0); k<n_points; ++k)
{
MathLib::Vector3 vec (0,0,0);
double sum (0);
for (unsigned i=0; i<3; ++i)
{
vec[i] = (double)rand()-(RAND_MAX/2.0);
sum+=(vec[i]*vec[i]);
}
double fac (this->_radius/sqrt(sum));
pnts->push_back(new GeoLib::Point(_center[0]+vec[0]*fac, _center[1]+vec[1]*fac, _center[2]+vec[2]*fac));
}
return pnts;
}
}