Improved Alpha shapes 2 examples

-- Added a weighted point data set
-- Readability (indentation, typedefs, etc.)
This commit is contained in:
Mael Rouxel-Labbé 2017-05-02 12:35:45 +02:00
parent a9a016a5c0
commit 08da170d1b
3 changed files with 271 additions and 90 deletions

View File

@ -0,0 +1,207 @@
206
39 498 19
57 502 -90
70 496 -75
606 495 76
25 492 -51
54 492 25
61 494 3
78 494 29
85 494 -47
101 485 -38
114 487 -39
119 487 13
126 490 -88
140 488 67
148 493 -19
598 485 -88
623 492 31
34 481 -8
163 480 94
169 475 -13
184 483 35
194 478 -18
337 482 45
354 478 -29
590 477 33
621 480 -69
25 471 -22
196 467 67
211 466 51
224 474 -13
234 466 -73
244 468 -29
246 474 -4
259 465 -49
267 466 -52
277 474 44
289 469 75
298 466 50
309 472 -26
316 467 -71
333 466 -87
363 465 34
367 465 42
576 474 -76
633 465 2
17 458 -77
383 455 -64
394 456 -66
578 456 13
617 459 -69
20 452 20
366 451 -51
413 454 12
423 450 -34
432 449 18
441 447 -54
567 452 -4
612 449 95
20 439 -86
380 436 -52
391 439 -18
399 444 -59
427 439 18
562 440 77
606 440 -9
17 426 65
433 431 21
453 431 -33
456 431 16
537 433 95
545 425 -5
605 433 28
17 417 30
424 422 -63
435 423 52
459 421 -66
532 421 -40
607 415 87
10 414 -32
419 406 72
438 412 -81
460 413 87
469 406 -78
524 410 30
609 412 -47
13 396 39
415 401 76
444 403 48
482 399 35
519 395 90
603 395 95
13 385 -82
417 390 -69
439 386 14
479 393 95
512 385 21
585 392 -20
-5 379 17
415 380 87
439 384 95
478 381 13
487 379 -17
499 380 23
584 378 -55
-2 367 19
419 371 -23
431 368 -22
585 367 78
10 356 -36
576 361 44
11 345 50
582 353 -17
9 342 31
579 335 71
16 331 -86
567 333 84
20 319 11
574 320 89
23 314 32
568 306 -52
22 299 80
582 302 28
25 290 -34
571 293 -89
31 280 -56
560 284 -39
34 268 32
547 270 22
42 259 77
45 263 20
553 263 18
48 248 -9
543 248 -97
58 237 -57
68 241 35
77 241 21
539 237 18
85 225 -87
525 229 0
536 233 82
103 221 56
106 224 -49
171 222 -35
176 220 -11
188 218 -77
526 219 -22
122 209 -27
132 209 34
144 212 -32
150 205 -94
164 209 81
202 213 -52
525 208 34
214 204 -53
452 197 -41
525 197 77
206 192 7
430 189 90
444 192 0
455 193 -15
469 190 10
484 189 -81
493 188 75
535 193 12
216 184 -39
259 183 10
363 175 -65
370 178 78
377 183 23
387 181 34
415 176 61
504 177 -20
511 177 84
536 181 -74
221 173 67
230 169 -92
243 166 3
249 171 -59
269 173 41
343 172 70
350 172 -54
404 167 23
410 170 -82
420 171 79
517 174 70
545 172 -23
274 158 56
342 156 -22
522 163 -33
553 155 -43
279 149 62
332 148 77
524 149 -25
548 146 38
275 140 -9
320 138 34
530 142 -50
547 139 24
286 127 13
296 132 72
319 130 -41
525 126 -25
538 131 -47
301 124 -56
305 117 0
321 119 -80

View File

@ -8,7 +8,6 @@
#include <vector> #include <vector>
#include <list> #include <list>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K; typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::FT FT; typedef K::FT FT;
@ -19,29 +18,26 @@ typedef CGAL::Alpha_shape_vertex_base_2<K> Vb;
typedef CGAL::Alpha_shape_face_base_2<K> Fb; typedef CGAL::Alpha_shape_face_base_2<K> Fb;
typedef CGAL::Triangulation_data_structure_2<Vb,Fb> Tds; typedef CGAL::Triangulation_data_structure_2<Vb,Fb> Tds;
typedef CGAL::Delaunay_triangulation_2<K,Tds> Triangulation_2; typedef CGAL::Delaunay_triangulation_2<K,Tds> Triangulation_2;
typedef CGAL::Alpha_shape_2<Triangulation_2> Alpha_shape_2; typedef CGAL::Alpha_shape_2<Triangulation_2> Alpha_shape_2;
typedef Alpha_shape_2::Alpha_shape_edges_iterator Alpha_shape_edges_iterator; typedef Alpha_shape_2::Alpha_shape_edges_iterator Alpha_shape_edges_iterator;
template <class OutputIterator> template <class OutputIterator>
void void alpha_edges( const Alpha_shape_2& A, OutputIterator out)
alpha_edges( const Alpha_shape_2& A,
OutputIterator out)
{ {
for(Alpha_shape_edges_iterator it = A.alpha_shape_edges_begin(); Alpha_shape_edges_iterator it = A.alpha_shape_edges_begin(),
it != A.alpha_shape_edges_end(); end = A.alpha_shape_edges_end();
++it){ for( ; it!=end; ++it)
*out++ = A.segment(*it); *out++ = A.segment(*it);
} }
}
template <class OutputIterator> template <class OutputIterator>
bool bool file_input(OutputIterator out)
file_input(OutputIterator out)
{ {
std::ifstream is("./data/fin", std::ios::in); std::ifstream is("./data/fin", std::ios::in);
if(is.fail()){ if(is.fail())
{
std::cerr << "unable to open file for input" << std::endl; std::cerr << "unable to open file for input" << std::endl;
return false; return false;
} }
@ -54,15 +50,13 @@ file_input(OutputIterator out)
return true; return true;
} }
// Reads a list of points and returns a list of segments // Reads a list of points and returns a list of segments
// corresponding to the Alpha shape. // corresponding to the Alpha shape.
int main() int main()
{ {
std::list<Point> points; std::list<Point> points;
if(! file_input(std::back_inserter(points))){ if(! file_input(std::back_inserter(points)))
return -1; return -1;
}
Alpha_shape_2 A(points.begin(), points.end(), Alpha_shape_2 A(points.begin(), points.end(),
FT(10000), FT(10000),

View File

@ -7,54 +7,34 @@
#include <vector> #include <vector>
#include <list> #include <list>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K; typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::FT FT; typedef K::FT FT;
typedef K::Point_2 Point_base; typedef K::Weighted_point_2 Weighted_point;
typedef K::Weighted_point_2 Point; typedef K::Segment_2 Segment;
typedef K Gt;
typedef CGAL::Regular_triangulation_vertex_base_2<Gt> Rvb;
typedef CGAL::Alpha_shape_vertex_base_2<Gt,Rvb> Vb;
typedef CGAL::Regular_triangulation_face_base_2<Gt> Rf;
typedef CGAL::Alpha_shape_face_base_2<Gt, Rf> Fb;
typedef CGAL::Regular_triangulation_vertex_base_2<K> Rvb;
typedef CGAL::Alpha_shape_vertex_base_2<K,Rvb> Vb;
typedef CGAL::Regular_triangulation_face_base_2<K> Rf;
typedef CGAL::Alpha_shape_face_base_2<K,Rf> Fb;
typedef CGAL::Triangulation_data_structure_2<Vb,Fb> Tds; typedef CGAL::Triangulation_data_structure_2<Vb,Fb> Tds;
typedef CGAL::Regular_triangulation_2<Gt,Tds> Triangulation_2; typedef CGAL::Regular_triangulation_2<K,Tds> Triangulation_2;
typedef CGAL::Alpha_shape_2<Triangulation_2> Alpha_shape_2; typedef CGAL::Alpha_shape_2<Triangulation_2> Alpha_shape_2;
typedef Alpha_shape_2::Alpha_shape_edges_iterator Alpha_shape_edges_iterator; typedef Alpha_shape_2::Alpha_shape_edges_iterator Alpha_shape_edges_iterator;
template <class InputIterator, class OutputIterator> template <class OutputIterator>
void void alpha_edges(const Alpha_shape_2& A, OutputIterator out)
alpha_edges(InputIterator begin, InputIterator end,
const FT &Alpha,
bool mode,
OutputIterator out)
// Generate Alpha Shape
{ {
std::vector<Gt::Segment_2> V_seg; Alpha_shape_edges_iterator it = A.alpha_shape_edges_begin(),
Alpha_shape_2 A(begin,end); end = A.alpha_shape_edges_end();
for( ; it!=end; ++it)
if (mode)
{ A.set_mode(Alpha_shape_2::GENERAL); }
else
{ A.set_mode(Alpha_shape_2::REGULARIZED); };
A.set_alpha(Alpha);
for(Alpha_shape_edges_iterator it = A.alpha_shape_edges_begin();
it != A.alpha_shape_edges_end();
++it){
*out++ = A.segment(*it); *out++ = A.segment(*it);
} }
}
bool bool file_input(std::list<Weighted_point>& L)
file_input(std::list<Point>& L)
{ {
std::ifstream is("./data/fin_weighted", std::ios::in);
std::ifstream is("./data/fin", std::ios::in);
if(is.fail()) if(is.fail())
{ {
@ -62,36 +42,36 @@ file_input(std::list<Point>& L)
return false; return false;
} }
CGAL::set_ascii_mode(is);
int n; int n;
is >> n; is >> n;
std::cout << "Reading " << n << " points" << std::endl; std::cout << "Reading " << n << " points" << std::endl;
for( ; n>0; n--) for( ; n>0; n--)
{ {
Point_base p(0., 0.); Weighted_point wp;
is >> p; is >> wp;
if(is) { L.push_back(wp);
L.push_back(Point (p, FT(n*n)));
} else {
return false;
}
}
std::cout << "Points inserted" << std::endl;
return true;
} }
return true;
}
// Reads a list of points and returns a list of segments corresponding to // Reads a list of points and returns a list of segments corresponding to
// the weighted Alpha Shape. // the weighted Alpha Shape.
int main() int main()
{ {
std::list<Point> points; std::list<Weighted_point> wpoints;
file_input(points); if(!file_input(wpoints))
std::vector<Gt::Segment_2> segments; return -1;
alpha_edges(points.begin(), points.end(),
FT(10000),Alpha_shape_2::GENERAL, Alpha_shape_2 A(wpoints.begin(), wpoints.end(),
std::back_inserter(segments)); FT(10000),
std::cout << segments.size() << " alpha shape edges." << std::endl; Alpha_shape_2::GENERAL);
std::vector<Segment> segments;
alpha_edges(A, std::back_inserter(segments));
std::cout << "Alpha Shape computed" << std::endl;
std::cout << segments.size() << " alpha shape edges" << std::endl;
std::cout << "Optimal alpha: " << *A.find_optimal_alpha(1)<<std::endl;
return 0; return 0;
} }