forked from EricAlex/structrock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Miniball.hpp
515 lines (450 loc) · 14.5 KB
/
Miniball.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copright (C) 1999-2013, Bernd Gaertner
// $Rev: 3581 $
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Contact:
// --------
// Bernd Gaertner
// Institute of Theoretical Computer Science
// ETH Zuerich
// CAB G31.1
// CH-8092 Zuerich, Switzerland
// http://www.inf.ethz.ch/personal/gaertner
#include <cassert>
#include <algorithm>
#include <list>
#include <ctime>
#include <limits>
namespace Miniball {
// Global Functions
// ================
template <typename NT>
inline NT mb_sqr (NT r) {return r*r;}
// Functors
// ========
// functor to map a point iterator to the corresponding coordinate iterator;
// generic version for points whose coordinate containers have begin()
template < typename Pit_, typename Cit_ >
struct CoordAccessor {
typedef Pit_ Pit;
typedef Cit_ Cit;
inline Cit operator() (Pit it) const { return (*it).begin(); }
};
// partial specialization for points whose coordinate containers are arrays
template < typename Pit_, typename Cit_ >
struct CoordAccessor<Pit_, Cit_*> {
typedef Pit_ Pit;
typedef Cit_* Cit;
inline Cit operator() (Pit it) const { return *it; }
};
// Class Declaration
// =================
template <typename CoordAccessor>
class Miniball {
private:
// types
// The iterator type to go through the input points
typedef typename CoordAccessor::Pit Pit;
// The iterator type to go through the coordinates of a single point.
typedef typename CoordAccessor::Cit Cit;
// The coordinate type
typedef typename std::iterator_traits<Cit>::value_type NT;
// The iterator to go through the support points
typedef typename std::list<Pit>::iterator Sit;
// data members...
const int d; // dimension
Pit points_begin;
Pit points_end;
CoordAccessor coord_accessor;
double time;
const NT nt0; // NT(0)
//...for the algorithms
std::list<Pit> L;
Sit support_end;
int fsize; // number of forced points
int ssize; // number of support points
// ...for the ball updates
NT* current_c;
NT current_sqr_r;
NT** c;
NT* sqr_r;
// helper arrays
NT* q0;
NT* z;
NT* f;
NT** v;
NT** a;
public:
// The iterator type to go through the support points
typedef typename std::list<Pit>::const_iterator SupportPointIterator;
// PRE: [begin, end) is a nonempty range
// POST: computes the smallest enclosing ball of the points in the range
// [begin, end); the functor a maps a point iterator to an iterator
// through the d coordinates of the point
Miniball (int d_, Pit begin, Pit end, CoordAccessor ca = CoordAccessor());
// POST: returns a pointer to the first element of an array that holds
// the d coordinates of the center of the computed ball
const NT* center () const;
// POST: returns the squared radius of the computed ball
NT squared_radius () const;
// POST: returns the number of support points of the computed ball;
// the support points form a minimal set with the same smallest
// enclosing ball as the input set; in particular, the support
// points are on the boundary of the computed ball, and their
// number is at most d+1
int nr_support_points () const;
// POST: returns an iterator to the first support point
SupportPointIterator support_points_begin () const;
// POST: returns a past-the-end iterator for the range of support points
SupportPointIterator support_points_end () const;
// POST: returns the maximum excess of any input point w.r.t. the computed
// ball, divided by the squared radius of the computed ball. The
// excess of a point is the difference between its squared distance
// from the center and the squared radius; Ideally, the return value
// is 0. subopt is set to the absolute value of the most negative
// coefficient in the affine combination of the support points that
// yields the center. Ideally, this is a convex combination, and there
// is no negative coefficient in which case subopt is set to 0.
NT relative_error (NT& subopt) const;
// POST: return true if the relative error is at most tol, and the
// suboptimality is 0; the default tolerance is 10 times the
// coordinate type's machine epsilon
bool is_valid (NT tol = NT(10) * std::numeric_limits<NT>::epsilon()) const;
// POST: returns the time in seconds taken by the constructor call for
// computing the smallest enclosing ball
double get_time() const;
// POST: deletes dynamically allocated arrays
~Miniball();
private:
void mtf_mb (Sit n);
void mtf_move_to_front (Sit j);
void pivot_mb (Pit n);
void pivot_move_to_front (Pit j);
NT excess (Pit pit) const;
void pop ();
bool push (Pit pit);
NT suboptimality () const;
void create_arrays();
void delete_arrays();
};
// Class Definition
// ================
template <typename CoordAccessor>
Miniball<CoordAccessor>::Miniball (int d_, Pit begin, Pit end,
CoordAccessor ca)
: d (d_),
points_begin (begin),
points_end (end),
coord_accessor (ca),
time (clock()),
nt0 (NT(0)),
L(),
support_end (L.begin()),
fsize(0),
ssize(0),
current_c (NULL),
current_sqr_r (NT(-1)),
c (NULL),
sqr_r (NULL),
q0 (NULL),
z (NULL),
f (NULL),
v (NULL),
a (NULL)
{
assert (points_begin != points_end);
create_arrays();
// set initial center
for (int j=0; j<d; ++j) c[0][j] = nt0;
current_c = c[0];
// compute miniball
pivot_mb (points_end);
// update time
time = (clock() - time) / CLOCKS_PER_SEC;
}
template <typename CoordAccessor>
Miniball<CoordAccessor>::~Miniball()
{
delete_arrays();
}
template <typename CoordAccessor>
void Miniball<CoordAccessor>::create_arrays()
{
c = new NT*[d+1];
v = new NT*[d+1];
a = new NT*[d+1];
for (int i=0; i<d+1; ++i) {
c[i] = new NT[d];
v[i] = new NT[d];
a[i] = new NT[d];
}
sqr_r = new NT[d+1];
q0 = new NT[d];
z = new NT[d+1];
f = new NT[d+1];
}
template <typename CoordAccessor>
void Miniball<CoordAccessor>::delete_arrays()
{
delete[] f;
delete[] z;
delete[] q0;
delete[] sqr_r;
for (int i=0; i<d+1; ++i) {
delete[] a[i];
delete[] v[i];
delete[] c[i];
}
delete[] a;
delete[] v;
delete[] c;
}
template <typename CoordAccessor>
const typename Miniball<CoordAccessor>::NT*
Miniball<CoordAccessor>::center () const
{
return current_c;
}
template <typename CoordAccessor>
typename Miniball<CoordAccessor>::NT
Miniball<CoordAccessor>::squared_radius () const
{
return current_sqr_r;
}
template <typename CoordAccessor>
int Miniball<CoordAccessor>::nr_support_points () const
{
assert (ssize < d+2);
return ssize;
}
template <typename CoordAccessor>
typename Miniball<CoordAccessor>::SupportPointIterator
Miniball<CoordAccessor>::support_points_begin () const
{
return L.begin();
}
template <typename CoordAccessor>
typename Miniball<CoordAccessor>::SupportPointIterator
Miniball<CoordAccessor>::support_points_end () const
{
return support_end;
}
template <typename CoordAccessor>
typename Miniball<CoordAccessor>::NT
Miniball<CoordAccessor>::relative_error (NT& subopt) const
{
NT e, max_e = nt0;
// compute maximum absolute excess of support points
for (SupportPointIterator it = support_points_begin();
it != support_points_end(); ++it) {
e = excess (*it);
if (e < nt0) e = -e;
if (e > max_e) {
max_e = e;
}
}
// compute maximum excess of any point
for (Pit i = points_begin; i != points_end; ++i)
if ((e = excess (i)) > max_e)
max_e = e;
subopt = suboptimality();
assert (current_sqr_r > nt0 || max_e == nt0);
return (current_sqr_r == nt0 ? nt0 : max_e / current_sqr_r);
}
template <typename CoordAccessor>
bool Miniball<CoordAccessor>::is_valid (NT tol) const
{
NT suboptimality;
return ( (relative_error (suboptimality) <= tol) && (suboptimality == 0) );
}
template <typename CoordAccessor>
double Miniball<CoordAccessor>::get_time() const
{
return time;
}
template <typename CoordAccessor>
void Miniball<CoordAccessor>::mtf_mb (Sit n)
{
// Algorithm 1: mtf_mb (L_{n-1}, B), where L_{n-1} = [L.begin, n)
// B: the set of forced points, defining the current ball
// S: the superset of support points computed by the algorithm
// --------------------------------------------------------------
// from B. Gaertner, Fast and Robust Smallest Enclosing Balls, ESA 1999,
// http://www.inf.ethz.ch/personal/gaertner/texts/own_work/esa99_final.pdf
// PRE: B = S
assert (fsize == ssize);
support_end = L.begin();
if ((fsize) == d+1) return;
// incremental construction
for (Sit i = L.begin(); i != n;)
{
// INV: (support_end - L.begin() == |S|-|B|)
assert (std::distance (L.begin(), support_end) == ssize - fsize);
Sit j = i++;
if (excess(*j) > nt0)
if (push(*j)) { // B := B + p_i
mtf_mb (j); // mtf_mb (L_{i-1}, B + p_i)
pop(); // B := B - p_i
mtf_move_to_front(j);
}
}
// POST: the range [L.begin(), support_end) stores the set S\B
}
template <typename CoordAccessor>
void Miniball<CoordAccessor>::mtf_move_to_front (Sit j)
{
if (support_end == j)
support_end++;
L.splice (L.begin(), L, j);
}
template <typename CoordAccessor>
void Miniball<CoordAccessor>::pivot_mb (Pit n)
{
// Algorithm 2: pivot_mb (L_{n-1}), where L_{n-1} = [L.begin, n)
// --------------------------------------------------------------
// from B. Gaertner, Fast and Robust Smallest Enclosing Balls, ESA 1999,
// http://www.inf.ethz.ch/personal/gaertner/texts/own_work/esa99_final.pdf
NT old_sqr_r;
const NT* c;
Pit pivot, k;
NT e, max_e, sqr_r;
Cit p;
do {
old_sqr_r = current_sqr_r;
sqr_r = current_sqr_r;
pivot = points_begin;
max_e = nt0;
for (k = points_begin; k != n; ++k) {
p = coord_accessor(k);
e = -sqr_r;
c = current_c;
for (int j=0; j<d; ++j)
e += mb_sqr<NT>(*p++-*c++);
if (e > max_e) {
max_e = e;
pivot = k;
}
}
if (max_e > nt0) {
// check if the pivot is already contained in the support set
if (std::find(L.begin(), support_end, pivot) == support_end) {
assert (fsize == 0);
if (push (pivot)) {
mtf_mb(support_end);
pop();
pivot_move_to_front(pivot);
}
}
}
} while (old_sqr_r < current_sqr_r);
}
template <typename CoordAccessor>
void Miniball<CoordAccessor>::pivot_move_to_front (Pit j)
{
L.push_front(j);
if (std::distance(L.begin(), support_end) == d+2)
support_end--;
}
template <typename CoordAccessor>
inline typename Miniball<CoordAccessor>::NT
Miniball<CoordAccessor>::excess (Pit pit) const
{
Cit p = coord_accessor(pit);
NT e = -current_sqr_r;
NT* c = current_c;
for (int k=0; k<d; ++k){
e += mb_sqr<NT>(*p++-*c++);
}
return e;
}
template <typename CoordAccessor>
void Miniball<CoordAccessor>::pop ()
{
--fsize;
}
template <typename CoordAccessor>
bool Miniball<CoordAccessor>::push (Pit pit)
{
int i, j;
NT eps = mb_sqr<NT>(std::numeric_limits<NT>::epsilon());
Cit cit = coord_accessor(pit);
Cit p = cit;
if (fsize==0) {
for (i=0; i<d; ++i)
q0[i] = *p++;
for (i=0; i<d; ++i)
c[0][i] = q0[i];
sqr_r[0] = nt0;
}
else {
// set v_fsize to Q_fsize
for (i=0; i<d; ++i)
//v[fsize][i] = p[i]-q0[i];
v[fsize][i] = *p++-q0[i];
// compute the a_{fsize,i}, i< fsize
for (i=1; i<fsize; ++i) {
a[fsize][i] = nt0;
for (j=0; j<d; ++j)
a[fsize][i] += v[i][j] * v[fsize][j];
a[fsize][i]*=(2/z[i]);
}
// update v_fsize to Q_fsize-\bar{Q}_fsize
for (i=1; i<fsize; ++i) {
for (j=0; j<d; ++j)
v[fsize][j] -= a[fsize][i]*v[i][j];
}
// compute z_fsize
z[fsize]=nt0;
for (j=0; j<d; ++j)
z[fsize] += mb_sqr<NT>(v[fsize][j]);
z[fsize]*=2;
// reject push if z_fsize too small
if (z[fsize]<eps*current_sqr_r) {
return false;
}
// update c, sqr_r
p=cit;
NT e = -sqr_r[fsize-1];
for (i=0; i<d; ++i)
e += mb_sqr<NT>(*p++-c[fsize-1][i]);
f[fsize]=e/z[fsize];
for (i=0; i<d; ++i)
c[fsize][i] = c[fsize-1][i]+f[fsize]*v[fsize][i];
sqr_r[fsize] = sqr_r[fsize-1] + e*f[fsize]/2;
}
current_c = c[fsize];
current_sqr_r = sqr_r[fsize];
ssize = ++fsize;
return true;
}
template <typename CoordAccessor>
typename Miniball<CoordAccessor>::NT
Miniball<CoordAccessor>::suboptimality () const
{
NT* l = new NT[d+1];
NT min_l = nt0;
l[0] = NT(1);
for (int i=ssize-1; i>0; --i) {
l[i] = f[i];
for (int k=ssize-1; k>i; --k)
l[i]-=a[k][i]*l[k];
if (l[i] < min_l) min_l = l[i];
l[0] -= l[i];
}
if (l[0] < min_l) min_l = l[0];
delete[] l;
if (min_l < nt0)
return -min_l;
return nt0;
}
} // end Namespace Miniball