-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBoundingSphere.cpp
More file actions
425 lines (382 loc) · 15.6 KB
/
BoundingSphere.cpp
File metadata and controls
425 lines (382 loc) · 15.6 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
This file is part of the Geometry library.
Copyright (C) 2007-2012 Benjamin Eikel <benjamin@eikel.org>
Copyright (C) 2007-2012 Claudius Jähn <claudius@uni-paderborn.de>
Copyright (C) 2007-2012 Ralf Petring <ralf@petring.net>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "BoundingSphere.h"
#include "Sphere.h"
#include "Vec3.h"
#include <cmath>
#include <cstring> /* for std::memcmp */
#include <limits>
#include <list>
#include <utility>
namespace Geometry {
namespace BoundingSphere {
/**
* Calculate the excess of a point with respect to a sphere.
* The sphere is defined by its center and its squared radius.
*
* @see Page 328: \f$ e := ||p - c||^2 - r^2\f$
* @param center Center of the sphere
* @param radiusSquared Squared radius of the sphere
* @param point %Point to calculate the excess for
* @return Excess of the point
*/
template <typename _T>
static _T calcExcess(const _Vec3<_T> & center, _T radiusSquared, const _Vec3<_T> & point) {
return point.distanceSquared(center) - radiusSquared;
}
/**
* Calculate the maximum excess of multiple points with respect to a sphere.
* The sphere is defined by its center and its squared radius.
* The points are given as the beginning and ending of a range.
*
* @see Algorithm 2 on Page 328
* @param center Center of the sphere
* @param radiusSquared Squared radius of the sphere
* @param first Beginning of the range of points
* @param last Ending of the range of points
* @return Pair containing the maximum excess and an iterator pointing to the element with maximum excess
*/
template <typename InputIterator, typename _T>
static std::pair<_T, InputIterator> calcMaxExcess(const _Vec3<_T> & center, _T radiusSquared, InputIterator first,
InputIterator last) {
_T maxExcess = std::numeric_limits<_T>::lowest();
InputIterator result;
for (; first != last; ++first) {
const _T excess = calcExcess(center, radiusSquared, *first);
if (excess > maxExcess) {
maxExcess = excess;
result = first;
}
}
return std::make_pair(maxExcess, result);
}
/**
* Move the given point to the front of the container.
*
* @param points Container holding points
* @param point %Point that is to be moved to the front
*/
template <typename Container>
static void moveToFront(Container & points, typename Container::const_iterator point) {
points.emplace_front(*point);
points.erase(point);
}
/**
* Storage of data for one execution of the primitive operation.
* These data objects are stored in a stack.
*
* @see Section 4 on Page 329 and Page 335
*/
template <typename _T>
struct PrimitiveOperationData {
_T z;
_Vec3<_T> v;
_Vec3<_T> center;
_T radiusSquared;
};
/**
* Storage of data that is needed during the execution of the algorithm.
*/
template <typename Container, typename _T>
struct AlgorithmData {
//! Stack of miniball data calculated by mbBar()
std::vector<PrimitiveOperationData<_T>> stack;
//! End of the support set (see Page 327)
typename Container::const_iterator s;
//! Cache for the lastest valid center of the sphere
_Vec3<_T> center;
//! Cache for the latest valid squared radius of the sphere
_T radiusSquared;
};
/**
* Calculation of \f$\overline{\texttt{mb}}(B')\f$ for a new point \f$p\f$,
* with \f$B' = B \cup \{p\}\f$, when \f$\overline{\texttt{mb}}(B)\f$ has been calculated already.
*
* @see Section 3 on Page 328f.
* @param point New point \f$p\f$
* @param data Data containing the result for \f$\overline{\texttt{mb}}(B)\f$
* @return @c false if and only if the push operation should be rejected (see Equation 12 on Page 332)
*/
template <typename Container, typename _T>
static bool mbBar(const _Vec3<_T> & point, AlgorithmData<Container, _T> & data) {
if (data.stack.empty()) {
PrimitiveOperationData<_T> stackItem;
stackItem.center = point;
stackItem.radiusSquared = 0;
data.stack.emplace_back(std::move(stackItem));
} else {
const size_t m = data.stack.size();
PrimitiveOperationData<_T> current;
const PrimitiveOperationData<_T> & prev = data.stack.back();
// Page 329: Q_m := q_m - q_0
// data.stack[0].center stores q0
current.v = point - data.stack[0].center;
// Page 334: \alpha_{m,i} = (2 / z_i) * (Q_i - \bar{Q}_i)^T * Q_m i < m
_Vec3<_T> alpha;
for (uint32_t i = 1; i < m; ++i) {
alpha[i] = (2 / data.stack[i].z) * data.stack[i].v.dot(current.v);
}
// Page 335: Store vector Q_m - \bar{Q}_m
for (uint32_t i = 1; i < m; ++i) {
current.v -= data.stack[i].v * alpha[i];
}
// Page 335: Store z_m
// Lemma 1.iii: z = 2 * (Q_m - \bar{Q}_m)^T * (Q_m - \bar{Q}_m)
current.z = 2 * current.v.dot(current.v);
// Equation 12: Ignore push if z / r^2_{curr} < \epsilon
const _T epsilon = 1.0e-32;
if (current.z < epsilon * prev.radiusSquared) {
return false;
}
// Lemma 1.iv:
// c' = c + (e / z) * (Q_m - \bar{Q}_m)
// r'^2 = r^2 + (e^2 / (2 * z))
const _T excess = calcExcess(prev.center, prev.radiusSquared, point);
const _T factor = excess / current.z;
current.center = prev.center + current.v * factor;
current.radiusSquared = prev.radiusSquared + factor * excess / 2;
data.stack.emplace_back(std::move(current));
}
data.center = data.stack.back().center;
data.radiusSquared = data.stack.back().radiusSquared;
return true;
}
/**
* Move-to-front miniball computation
*
* @see Algorithm 1 on Page 327
*/
template <typename Container, typename _T>
static void mtf_mb(Container & points, typename Container::const_iterator endPoint,
AlgorithmData<Container, _T> & data) {
// Support set is empty
data.s = points.cbegin();
if (data.stack.size() == 4) {
return;
}
for (auto it = points.cbegin(); it != endPoint;) {
auto i = it++;
// Check if *i is outside of the sphere
if (calcExcess(data.center, data.radiusSquared, *i) > 0) {
if (mbBar(*i, data)) {
mtf_mb(points, i, data);
data.stack.pop_back();
// If i is the end of the support set, the support set is increased by one
if (data.s == i) {
++data.s;
}
moveToFront(points, i);
}
}
}
}
/**
* Pivot miniball computation
*
* @see Algorithm 2 on Page 328
*/
template <typename Container, typename _T>
static _Sphere<_T> pivot_mb(Container & points) {
AlgorithmData<Container, _T> data;
// Initialize the sphere with invalid values, which will generate
// an excess greater than zero for any point.
data.center = _Vec3<_T>(0, 0, 0);
data.radiusSquared = std::numeric_limits<_T>::lowest();
// t := 1
auto t = std::next(points.cbegin());
mtf_mb(points, t, data);
_T maxExcess;
_T oldRadiusSquared = std::numeric_limits<_T>::lowest();
do {
// Use t as beginning of range, to make sure k > t
auto pair = calcMaxExcess(data.center, data.radiusSquared, t, points.cend());
maxExcess = pair.first;
const auto & k = pair.second;
if (maxExcess > 0) {
t = data.s;
if (t == k) {
std::advance(t, 1);
}
oldRadiusSquared = data.radiusSquared;
mbBar(*k, data);
mtf_mb(points, data.s, data);
data.stack.pop_back();
// If k is the end of the support set, the support set is increased by one
if (data.s == k) {
++data.s;
}
moveToFront(points, k);
}
} while (maxExcess > 0 && data.radiusSquared > oldRadiusSquared);
return _Sphere<_T>(data.center, std::sqrt(data.radiusSquared));
}
static Sphere_f computeMiniballList(std::list<Vec3d> & pointList) {
// Remove duplicate values from the list
struct MemCompare {
bool operator()(const Geometry::Vec3d & a, const Geometry::Vec3d & b) const {
return std::memcmp(&a, &b, sizeof(Geometry::Vec3d)) < 0;
}
};
pointList.sort(MemCompare());
pointList.unique();
// Use double values here, because float values become instable in some cases.
const Sphere_d sphereDouble = pivot_mb<std::list<Vec3d>, double>(pointList);
return Sphere_f(Vec3f(sphereDouble.getCenter()), static_cast<float>(sphereDouble.getRadius()));
}
Sphere_f computeMiniball(const std::vector<Vec3f> & points) {
// Use a list here, because firstly the original algorithm suggests it,
// and secondly moving an element to the front is fastest for a list.
std::list<Vec3d> pointList;
for (const auto & p : points) {
pointList.emplace_back(p);
}
return computeMiniballList(pointList);
}
/**
* Project all points from a range onto a normal and identifiy the points with
* the extremal projected values.
*
* @tparam value_t Value type (e.g. float, double)
* @tparam iterator_t Iterator type for points
* @tparam nx X coordinate value of the normal
* @tparam ny Y coordinate value of the normal
* @tparam nz Z coordinate value of the normal
* @param first Iterator to begin of the point range
* @param last Iterator past the end of the point range
* @param indices Array of iterators to which two iterators pointing to the
* extremal points will be added
*/
template <typename value_t, typename iterator_t, int nx, int ny, int nz>
inline static void projectToNormal(iterator_t first, const iterator_t & last, std::vector<iterator_t> & indices) {
auto minValue = std::numeric_limits<value_t>::max();
auto minPoint = last;
auto maxValue = std::numeric_limits<value_t>::lowest();
auto maxPoint = last;
for (; first != last; ++first) {
const auto & point = *first;
// Project point onto normal
const auto projection = nx * point.getX() + ny * point.getY() + nz * point.getZ();
if (projection < minValue) {
minValue = projection;
minPoint = first;
}
if (projection > maxValue) {
maxValue = projection;
maxPoint = first;
}
}
indices.emplace_back(minPoint);
indices.emplace_back(maxPoint);
}
template <size_t numNormals>
std::list<Vec3d> findExtremalPoints(const std::vector<Vec3f> & points) {
using it_t = std::vector<Vec3f>::const_iterator;
std::vector<it_t> extremalIndices;
extremalIndices.reserve(2 * numNormals);
// See Page 28 for table of normals
// type 0 0 1
projectToNormal<float, it_t, 1, 0, 0>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 0, 1, 0>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 0, 0, 1>(points.cbegin(), points.cend(), extremalIndices);
if (numNormals > 3) {
// type 1 1 1
projectToNormal<float, it_t, 1, 1, 1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, 1, -1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, -1, 1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, -1, -1>(points.cbegin(), points.cend(), extremalIndices);
}
if (numNormals > 7) {
// type 0 1 1
projectToNormal<float, it_t, 1, 1, 0>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, -1, 0>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, 0, 1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, 0, -1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 0, 1, 1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 0, 1, -1>(points.cbegin(), points.cend(), extremalIndices);
}
if (numNormals > 13) {
// type 0 1 2
projectToNormal<float, it_t, 0, 1, 2>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 0, 2, 1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, 0, 2>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 2, 0, 1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, 2, 0>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 2, 1, 0>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 0, 1, -2>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 0, 2, -1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, 0, -2>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 2, 0, -1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, -2, 0>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 2, -1, 0>(points.cbegin(), points.cend(), extremalIndices);
// type 1 1 2
projectToNormal<float, it_t, 1, 1, 2>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 2, 1, 1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, 2, 1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, -1, 2>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, 1, -2>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, -1, -2>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 2, -1, 1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 2, 1, -1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 2, -1, -1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, -2, 1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, 2, -1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, -2, -1>(points.cbegin(), points.cend(), extremalIndices);
// type 1 2 2
projectToNormal<float, it_t, 2, 2, 1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, 2, 2>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 2, 1, 2>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 2, -2, 1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 2, 2, -1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 2, -2, -1>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, -2, 2>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, 2, -2>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 1, -2, -2>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 2, -1, 2>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 2, 1, -2>(points.cbegin(), points.cend(), extremalIndices);
projectToNormal<float, it_t, 2, -1, -2>(points.cbegin(), points.cend(), extremalIndices);
}
// Remove duplicate indices
std::sort(extremalIndices.begin(), extremalIndices.end());
extremalIndices.erase(std::unique(extremalIndices.begin(), extremalIndices.end()), extremalIndices.end());
std::list<Vec3d> extremalPoints;
for (const auto & index : extremalIndices) {
extremalPoints.emplace_back(*index);
}
return extremalPoints;
}
template <size_t s>
static Sphere_f extremalPointsOptimalSphere(const std::vector<Vec3f> & points) {
static_assert(s == 3 || s == 7 || s == 13 || s == 49, "s must be from {3, 7, 13, 49}");
const size_t n = points.size();
if (n > 2 * s) {
auto extremalPoints = findExtremalPoints<s>(points);
Sphere_f sphere = computeMiniballList(extremalPoints);
for (const auto & point : points) {
sphere.include(point);
}
return sphere;
} else {
return computeMiniball(points);
}
}
Sphere_f computeEPOS6(const std::vector<Vec3f> & points) {
return extremalPointsOptimalSphere<3>(points);
}
Sphere_f computeEPOS14(const std::vector<Vec3f> & points) {
return extremalPointsOptimalSphere<7>(points);
}
Sphere_f computeEPOS26(const std::vector<Vec3f> & points) {
return extremalPointsOptimalSphere<13>(points);
}
Sphere_f computeEPOS98(const std::vector<Vec3f> & points) {
return extremalPointsOptimalSphere<49>(points);
}
}
}