-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathPoint_container.h
469 lines (382 loc) · 11.9 KB
/
Point_container.h
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
// Copyright (c) 2002,2011 Utrecht University (The Netherlands).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// $URL$
// $Id$
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s) : Hans Tangelder (<[email protected]>)
// custom point container
#ifndef CGAL_POINT_CONTAINER_H
#define CGAL_POINT_CONTAINER_H
#include <CGAL/license/Spatial_searching.h>
#include <list>
#include <vector>
#include <functional>
#include <algorithm>
#include <CGAL/Kd_tree_rectangle.h>
#include <CGAL/Spatial_searching/internal/Get_dimension_tag.h>
#include <boost/optional.hpp>
namespace CGAL {
template <class Traits>
class Point_container {
private:
typedef typename Traits::Point_d Point_d;
typedef std::vector<const Point_d*> Point_vector;
public:
typedef typename Traits::FT FT;
typedef typename Point_vector::iterator iterator;
typedef typename Point_vector::const_iterator const_iterator;
typedef typename internal::Get_dimension_tag<Traits>::Dimension D;
private:
Traits traits;
// the iterator range of the Point_container
boost::optional<iterator> m_b ;
boost::optional<iterator> m_e ;
int built_coord; // a coordinate for which the pointer list is built
Kd_tree_rectangle<FT,D> bbox; // bounding box, i.e. rectangle of node
Kd_tree_rectangle<FT,D> tbox; // tight bounding box,
// i.e. minimal enclosing bounding
// box of points
public:
inline const Kd_tree_rectangle<FT,D>&
bounding_box() const
{
return bbox;
}
inline const Kd_tree_rectangle<FT,D>&
tight_bounding_box() const
{
return tbox;
}
inline int
dimension() const
{
return bbox.dimension();
}
inline int
built_coordinate() const
{
return built_coord;
}
// coordinate of the maximal span
inline int
max_span_coord() const
{
return bbox.max_span_coord();
}
// coordinate of the maximal tight span
inline int
max_tight_span_coord() const
{
return tbox.max_span_coord();
}
inline FT
max_span_lower() const
{
return bbox.min_coord(max_span_coord());
}
inline FT
max_tight_span_lower() const
{
return tbox.min_coord(max_tight_span_coord());
}
inline FT
max_span_upper() const
{
return bbox.max_coord(max_span_coord());
}
inline FT
max_tight_span_upper() const
{
return tbox.max_coord(max_tight_span_coord());
}
inline FT
max_spread() const
{
return max_span_upper() - max_span_lower();
}
inline FT
max_tight_spread() const
{
return max_tight_span_upper() - max_tight_span_lower();
}
int
max_tight_span_coord_balanced(FT Aspect_ratio) const
{
int cut_dim(-1);
FT max_spread_points(FT(-1));
FT max_length = max_spread(); // length of longest side of box
int dim = dimension();
for (int d=0; d<dim; d++) {
FT length=bbox.max_coord(d)-bbox.min_coord(d);
if (FT(2)*max_length/length <= Aspect_ratio) {
FT spread=tbox.max_coord(d)-tbox.min_coord(d);
if (spread > max_spread_points) {
max_spread_points = spread;
cut_dim = d;
}
}
}
// CGAL_assertion(cut_dim >= 0);
return cut_dim;
}
FT
max_span_upper_without_dim(int d) const
{
FT max_span(FT(0));
int dim=dimension();
for (int i=0; i<dim; i++) {
FT span = bbox.max_coord(i)-bbox.min_coord(i);
if (d != i && span > max_span) max_span=span;
}
return max_span;
}
FT
balanced_fair(int d, FT Aspect_ratio)
{
FT small_piece = max_span_upper_without_dim(d) / Aspect_ratio;
FT low_cut = bbox.min_coord(d) + small_piece; // lowest legal cut;
FT high_cut = bbox.max_coord(d) - small_piece; //highest legal cut;
// CGAL_assertion (high_cut >= low_cut);
FT split_value = median(d);
if (split_value < low_cut) split_value = low_cut;
if (split_value > high_cut) split_value = high_cut;
return split_value;
}
FT
balanced_sliding_fair(int d, FT Aspect_ratio)
{
FT small_piece = max_span_upper_without_dim(d) / Aspect_ratio;
FT low_cut = bbox.min_coord(d) + small_piece; // lowest legal cut;
FT high_cut = bbox.max_coord(d) - small_piece; //highest legal cut;
// CGAL_assertion (high_cut >= low_cut);
FT split_value = median(d);
FT max_span_lower = tbox.min_coord(d);
FT max_span_upper = tbox.max_coord(d);
if (split_value < low_cut) split_value= max_span_lower;
if (split_value > high_cut) split_value = max_span_upper;
return split_value;
}
// points
inline std::size_t
size() const
{
return *m_e - *m_b;
}
inline const_iterator
begin() const {
return *m_b;
}
inline const_iterator
end() const
{
return *m_e;
}
inline iterator
begin()
{
return *m_b;
}
inline iterator
end()
{
return *m_e;
}
inline bool
empty() const
{
return !m_b || !m_e || (*m_b == *m_e ) ;
}
// building the container from a sequence of Point_d*
Point_container(const int d, iterator begin, iterator end,const Traits& traits_) :
traits(traits_),m_b(begin), m_e(end), bbox(d, begin, end,traits.construct_cartesian_const_iterator_d_object()), tbox(bbox)
{
built_coord = max_span_coord();
}
void
set_range(iterator begin, iterator end)
{
m_b = begin;
m_e = end;
}
// building an empty container
Point_container(const int d,const Traits& traits_) :
traits(traits_),bbox(d), tbox(d)
{}
template <class Traits2>
struct Cmp {
typedef typename Traits2::FT FT;
typedef typename Traits2::Point_d Point_d;
typedef std::vector<const Point_d*> Point_vector;
int split_coord;
FT value;
const typename Traits2::Construct_cartesian_const_iterator_d& construct_it;
Cmp(int s, FT c,const typename Traits2::Construct_cartesian_const_iterator_d& cst_it)
: split_coord(s), value(c), construct_it(cst_it)
{}
bool
operator()(const Point_d* pt) const
{
typename Traits2::Cartesian_const_iterator_d ptit;
ptit = construct_it(*pt);
return *(ptit+split_coord) < value;
}
};
template <class Traits2>
struct Between {
typedef typename Traits2::FT FT;
typedef typename Traits2::Point_d Point_d;
typedef std::vector<const Point_d*> Point_vector;
int split_coord;
FT low, high;
const typename Traits2::Construct_cartesian_const_iterator_d& construct_it;
Between(int s, FT l, FT h,const typename Traits2::Construct_cartesian_const_iterator_d& cst_it)
: split_coord(s), low(l), high(h), construct_it(cst_it)
{}
bool
operator()(const Point_d* pt) const
{
typename Traits2::Cartesian_const_iterator_d ptit;
ptit = construct_it(*pt);
if(! ( *(ptit+split_coord) <= high ) ){
// std::cerr << "Point " << *pt << " exceeds " << high << " in dimension " << split_coord << std::endl;
return false;
}
if(! ( *(ptit+split_coord) >= low ) ){
//std::cerr << "Point " << *pt << " below " << low << " in dimension " << split_coord << std::endl;
return false;
}
return true;
}
};
void recompute_tight_bounding_box()
{
tbox.template update_from_point_pointers<typename Traits::Construct_cartesian_const_iterator_d>(begin(), end(),traits.construct_cartesian_const_iterator_d_object());
}
bool
is_valid() const
{
if(empty()) return true;
bool b = true;
for (int i = 0; i < dimension(); i++){
CGAL_assertion( b = (b && (bbox.min_coord(i) <= tbox.min_coord(i))));
CGAL_assertion( b = (b && (bbox.max_coord(i) >= tbox.max_coord(i))));
typename Traits::Construct_cartesian_const_iterator_d construct_it=traits.construct_cartesian_const_iterator_d_object();
Between<Traits> between(i,tbox.min_coord(i), tbox.max_coord(i), construct_it);
for(const_iterator it = begin(); it != end(); it++){
b = (b && between(*it));
}
}
return b;
}
// note that splitting is restricted to the built coordinate
template <class Separator>
void split(Point_container<Traits>& c, Separator& sep,
bool sliding=false)
{
CGAL_assertion(dimension()==c.dimension());
CGAL_assertion(is_valid());
c.bbox=bbox;
const int split_coord = sep.cutting_dimension();
FT cutting_value = sep.cutting_value();
built_coord=split_coord;
c.built_coord=split_coord;
typename Traits::Construct_cartesian_const_iterator_d construct_it=traits.construct_cartesian_const_iterator_d_object();
Cmp<Traits> cmp(split_coord, cutting_value,construct_it);
iterator it = std::partition(begin(), end(), cmp);
// now [begin,it) are lower and [it,end) are upper
if (sliding) { // avoid empty lists
if (it == begin()) {
iterator minelt = std::min_element(begin(),end(),comp_coord_val<Traits,int>(split_coord,construct_it));
if(minelt != it){
std::iter_swap(minelt,it);
}
cutting_value = *(construct_it(**it)+split_coord);
sep.set_cutting_value(cutting_value);
it++;
}
if (it == end()) {
iterator maxelt = std::max_element(begin(),end(),comp_coord_val<Traits,int>(split_coord,construct_it));
it--;
if(maxelt != it){
std::iter_swap(maxelt,it);
}
cutting_value = *(construct_it(**it)+split_coord);
sep.set_cutting_value(cutting_value);
}
}
c.set_range(begin(), it);
set_range(it, end());
// adjusting boxes
bbox.set_lower_bound(split_coord, cutting_value);
tbox. template update_from_point_pointers<typename Traits::Construct_cartesian_const_iterator_d>(begin(),end(),construct_it);
c.bbox.set_upper_bound(split_coord, cutting_value);
c.tbox. template update_from_point_pointers<typename Traits::Construct_cartesian_const_iterator_d>(c.begin(),c.end(),construct_it);
CGAL_assertion(is_valid());
CGAL_assertion(c.is_valid());
}
template <class Traits2, class Value>
struct comp_coord_val {
private:
Value coord;
const typename Traits2::Construct_cartesian_const_iterator_d& construct_it;
typedef typename Traits2::Point_d Point_d;
public:
comp_coord_val (const Value& coordinate,const typename Traits2::Construct_cartesian_const_iterator_d& cst_it)
: coord(coordinate), construct_it(cst_it)
{}
bool
operator()(const Point_d *a, const Point_d *b) const
{
typename Traits2::Cartesian_const_iterator_d ait = construct_it(*a),
bit = construct_it(*b);
return *(ait+coord) < *(bit+coord);
}
};
FT
median(const int split_coord)
{
typename Traits::Construct_cartesian_const_iterator_d construct_it=traits.construct_cartesian_const_iterator_d_object();
iterator mid = begin() + (end() - begin())/2;
std::nth_element(begin(), mid, end(),comp_coord_val<Traits,int>(split_coord,construct_it));
typename Traits::Cartesian_const_iterator_d mpit = construct_it((*(*mid)));
FT val1 = *(mpit+split_coord);
// Avoid using the low coord value as it results in an empty split
if (val1 == tbox.min_coord(split_coord)) {
iterator it = std::min_element(mid, end(), [=](const Point_d* a, const Point_d* b) -> bool {
FT a_c = *(construct_it(*a) + split_coord);
FT b_c = *(construct_it(*b) + split_coord);
if (a_c == val1)
return false;
if (b_c == val1)
return true;
return a_c < b_c;
});
return *(construct_it(**it) + split_coord);
}
mid++;
if (mid == end())
return val1;
mpit = construct_it((*(*mid)));
FT val2 = *(mpit+split_coord);
return (val1+val2)/FT(2);
}
private:
explicit Point_container()
{} // disable default constructor
};
template <class Point>
std::ostream&
operator<< (std::ostream& s, Point_container<Point>& c)
{
s << "Points container of size " << c.size() << "\n cell:";
s << c.bounding_box();
s << "\n minimal box enclosing points:"; s << c.tight_bounding_box();
return s;
}
} // namespace CGAL
#endif // CGAL_POINT_CONTAINER_H