generated from libigl/libigl-example-project
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathharm_simple.cpp
751 lines (604 loc) · 24 KB
/
harm_simple.cpp
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_3.h>
#include <CGAL/Triangulation_3.h>
#include <CGAL/Triangulation_cell_base_with_info_3.h>
#include <CGAL/Triangulation_vertex_base_with_info_3.h>
#include <CGAL/Triangulation_utils_3.h>
#include <CGAL/Real_timer.h>
#include <CGAL/IO/read_off_points.h>
#include <CGAL/IO/read_ply_points.h>
#include "CLI11.hpp"
#include <iostream>
#include <fstream>
#include <vector>
//#include <set>
//#include <unordered_set>
//#include <bitset>
#include <queue>
//#include <map>
//#include <chrono>
//#include <random>
#include <algorithm>
//#include <boost/math/tools/minima.hpp>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::FT FT;
typedef CGAL::Triangulation_vertex_base_with_info_3<int, K> Vb;
typedef CGAL::Triangulation_cell_base_with_info_3<int, K> Cb;
typedef CGAL::Triangulation_data_structure_3<Vb, Cb> Tds;
typedef CGAL::Delaunay_triangulation_3<K,Tds> DT;
typedef CGAL::Triangulation_3<K,Tds> Triangulation;
typedef Triangulation::Finite_vertices_iterator Finite_vertices_iterator;
typedef Triangulation::Finite_edges_iterator Finite_edges_iterator;
typedef Triangulation::Finite_facets_iterator Finite_facets_iterator;
typedef Triangulation::Finite_cells_iterator Finite_cells_iterator;
typedef Triangulation::Point Point;
typedef Triangulation::Facet Facet;
typedef Triangulation::Edge Edge;
typedef Triangulation::Vertex_handle VH;
typedef Triangulation::Cell_handle CH;
typedef Triangulation::Cell_circulator CC;
typedef Triangulation::Facet_circulator FC;
//typedef K::Vector_3 Vector;
struct flip_info {
CH c; // the cell defining the edge or face to be flipped
int i,j; // cell indices defining edge or face (j=-1)
int ci1, ci2, ci3; // cell indices (info) at time of creation
FT hi1,hi2,hi3; // the 2 or 3 harmonic indices for the cells after flipping
// so we don't have to recompute them
FT val; // value for priority queue
};
struct flip_comp {
bool operator()(flip_info a,flip_info b) const {
return a.val < b.val;
}
};
std::vector<FT> hi;
int n, cci = 0;
Triangulation T;
//std::vector<bool> bdy;
//
//Polyhedron bdy_poly;
//Tree bdy_tree;
//bool fix_bdy_points;
//
//static int histcount = 0;
void write_cgal(std::string filename)
{
std::cout << "Writing tets to " << filename << std::endl;
std::ofstream of(filename);
of << T;
of.close();
}
void write_tetgen(std::string filename)
{
// tetget/tetview really expects the points to be sorted by index...
std::vector<VH> V(n);
Finite_vertices_iterator vit = T.finite_vertices_begin();
for(; vit != T.finite_vertices_end(); ++vit)
V[vit->info()]=vit;
std::ofstream node(filename+".node");
node << n << " 3 0 0" << std::endl;
for (int i = 0; i < n; i++)
{
node << (i+1);
Point p = V[i]->point();
for (int j = 0; j < 3; j++)
node << " " << p[j];
node << std::endl;
}
node.close();
std::ofstream ele(filename+".ele");
ele << T.number_of_finite_cells() << " 4 0" << std::endl;
Finite_cells_iterator cit = T.finite_cells_begin();
int cid = 0;
for (; cit != T.finite_cells_end(); ++cit)
{
ele << (++cid);
for (int j = 0; j < 4; j++)
ele << " " << (cit->vertex(j)->info()+1);
ele << std::endl;
}
ele.close();
}
void stats()
{
std::vector<double> da,vol,csr,isr,ar;
double hti = 0.0;
Finite_cells_iterator cit = T.finite_cells_begin();
int nc = 0;
for (; cit != T.finite_cells_end(); ++cit)
{
std::array<CGAL::Vector_3<K>,4> normal;
double a = 0.0, a2 = 0.0;
for (int i = 0; i < 4; i++)
{
normal[i] = CGAL::unit_normal(T.point(cit,i),
T.point(cit,(i+1)%4),
T.point(cit,(i+2)%4));
if (i&1) normal[i] *= -1;
double fa2 = CGAL::squared_area(T.point(cit,i),
T.point(cit,(i+1)%4),
T.point(cit,(i+2)%4));
a += sqrt(fa2);
a2 += fa2;
}
double v = T.tetrahedron(cit).volume();
if (v < 0) std::cerr << "Tet with negative volume: " << cit->vertex(0)->info() << std::endl;
vol.push_back(v);
hti += a2/v;
double r = 3.0*v/a;
isr.push_back(r);
double R = sqrt(CGAL::squared_radius(T.point(cit,0),
T.point(cit,1),
T.point(cit,2),
T.point(cit,3)));
csr.push_back(R);
ar.push_back(R/r);
for (int i = 0; i < 3; i++)
for (int j = i+1; j < 4; j++)
da.push_back(acos(-(normal[i]*normal[j]))*180.0/M_PI);
nc++;
}
std::sort(da.begin(), da.end());
std::cout << "Number of vertices: " << T.number_of_vertices() << std::endl;
std::cout << "Number of finite cells: " << nc << std::endl;
std::cout << "Trace of Laplacian: " << (hti) << std::endl;
std::cout << "Mean Harmonic index: " << (hti / double(nc)) << std::endl;
std::cout << std::endl << "Dihedral angles" << std::endl;
std::cout << "Min: " << da.front() << std::endl;
std::cout << "5th percentile: " << da[da.size()/20] << std::endl;
std::cout << "Median: " << da[da.size()/2] << std::endl;
std::cout << "95th percentile: " << da[19*da.size()/20] << std::endl;
std::cout << "Max: " << da.back() << std::endl;
std::sort(ar.begin(),ar.end());
std::cout << std::endl << "Aspect ratio (circumradius / inradius)" << std::endl;
std::cout << "5th percentile: " << ar[ar.size()/20] << std::endl;
std::cout << "Median: " << ar[ar.size()/2] << std::endl;
std::cout << "95th percentile: " << ar[19*ar.size()/20] << std::endl;
/*
double mir = 0.0;
for (auto ir : isr) mir += ir;
std::cerr << std::endl << "Mean inradius: " << (mir/double(isr.size())) << std::endl;
double mcr = 0.0;
for (auto cr : csr) mcr += cr;
std::cerr << std::endl << "Mean circumradius: " << (mcr/double(csr.size())) << std::endl;
*/
}
// the following two functions are taken from CGAL source because they were private
void set_adjacency(CH c0, int i0,
CH c1, int i1)
{
c0->set_neighbor(i0,c1);
c1->set_neighbor(i1,c0);
}
void change_orientation(CH c)
{
VH tmp_v = c->vertex(0);
c->set_vertex(0, c->vertex(1));
c->set_vertex(1, tmp_v);
CH tmp_c = c->neighbor(0);
c->set_neighbor(0, c->neighbor(1));
c->set_neighbor(1, tmp_c);
}
FT eta(CH ch)
{
FT sas(0);
for (int i = 0; i < 4; i++)
sas += T.triangle(ch,i).squared_area();
return sas / T.tetrahedron(ch).volume();
}
bool check_face(CH c, int i, flip_info &fi)
{
CH n = c->neighbor(i);
// get the 2 vertex handles opposite the face, stop if any is infinite
VH vhl = c->vertex(i);
if (T.is_infinite(vhl)) return false;
int in = n->index(c);
VH vhr = n->vertex(in);
if (T.is_infinite(vhr)) return false;
// get the other three vertices and their handles
int i1 = (i+1)&3;
int i2 = (i+2)&3;
int i3 = (i+3)&3;
VH vhf1 = c->vertex(i1);
VH vhf2 = c->vertex(i2);
VH vhf3 = c->vertex(i3);
// the three new tets are
// vhl, vhf1, vhf2, vhr
// vhl, vhf2, vhf3, vhr
// vhl, vhf3, vhf1, vhr
// compute their volumes, if any is neagtive stop
FT vol1 = CGAL::volume(vhl->point(),
vhf1->point(),
vhf2->point(),
vhr->point());
if (vol1 <= 0.0) return false;
FT vol2 = CGAL::volume(vhl->point(),
vhf2->point(),
vhf3->point(),
vhr->point());
if (vol2 <= 0.0) return false;
FT vol3 = CGAL::volume(vhl->point(),
vhf3->point(),
vhf1->point(),
vhr->point());
if (vol3 <= 0.0) return false;
// compute the squared face areas
FT sasl1 = CGAL::squared_area(vhl->point(),vhf1->point(),vhf2->point());
FT sasl2 = CGAL::squared_area(vhl->point(),vhf2->point(),vhf3->point());
FT sasl3 = CGAL::squared_area(vhl->point(),vhf3->point(),vhf1->point());
FT sasr1 = CGAL::squared_area(vhr->point(),vhf1->point(),vhf2->point());
FT sasr2 = CGAL::squared_area(vhr->point(),vhf2->point(),vhf3->point());
FT sasr3 = CGAL::squared_area(vhr->point(),vhf3->point(),vhf1->point());
FT sasi1 = CGAL::squared_area(vhl->point(),vhf1->point(),vhr->point());
FT sasi2 = CGAL::squared_area(vhl->point(),vhf2->point(),vhr->point());
FT sasi3 = CGAL::squared_area(vhl->point(),vhf3->point(),vhr->point());
fi.hi1 = (sasl1+sasr1+sasi1+sasi2)/vol1;
fi.hi2 = (sasl2+sasr2+sasi2+sasi3)/vol2;
fi.hi3 = (sasl3+sasr3+sasi3+sasi1)/vol3;
fi.val = hi[c->info()]+hi[n->info()] - (fi.hi1+fi.hi2+fi.hi3);
if (fi.val <= FT(0)) return false;
fi.c = c;
fi.i = i;
fi.j = -1;
fi.ci1 = c->info();
fi.ci2 = n->info();
fi.ci3 = -1;
return true;
}
bool perform_face_flip(flip_info fi, Edge &e)
{
CH c = fi.c;
int i = fi.i;
if (c==NULL || c->info() != fi.ci1) return false;
CH n = c->neighbor(i);
if (n==NULL || n->info() != fi.ci2) return false;
// get the 2 vertex handles opposite the face
VH vhl = fi.c->vertex(i);
int in = n->index(c);
VH vhr = n->vertex(in);
// get the other three vertices
int i1 = (i+1)&3;
int i2 = (i+2)&3;
int i3 = (i+3)&3;
int in1 = n->index(c->vertex(i1));
int in2 = n->index(c->vertex(i2));
int in3 = n->index(c->vertex(i3));
T.tds().set_adjacency(c, i, n->neighbor(in3), n->neighbor(in3)->index(n));
c->set_vertex( i3, n->vertex(in) );
T.tds().set_adjacency(n, in, c->neighbor(i1), c->neighbor(i1)->index(c));
n->set_vertex( in1, c->vertex(i) );
CH cnew = T.tds().create_cell(c->vertex(i), c->vertex(i1),
n->vertex(in), n->vertex(in3));
T.tds().set_adjacency(cnew, 0, n->neighbor(in2), n->neighbor(in2)->index(n));
T.tds().set_adjacency(cnew, 1, n, in2);
T.tds().set_adjacency(cnew, 2, c->neighbor(i2), c->neighbor(i2)->index(c));
T.tds().set_adjacency(cnew, 3, c, i2);
T.tds().set_adjacency(c, i1, n, in3);
if ((i&1) != 0)
change_orientation(cnew);
c->vertex(i1)->set_cell(cnew);
c->vertex(i2)->set_cell(c);
n->vertex(in3)->set_cell(n);
// cleanup
c->info() = cci++;
hi.push_back(fi.hi1);
n->info() = cci++;
hi.push_back(fi.hi2);
cnew->info() = cci++;
hi.push_back(fi.hi3);
e.first = c;
e.second = i;
e.third = i3;
return true;
}
bool check_edge(CH c, int i, int j, flip_info &fi)
{
// get vertex handles of vertices around edge
int next = CGAL::Triangulation_utils_3::next_around_edge(i,j);
CH c1 = c->neighbor( next );
VH vhf1 = c->vertex( next ); // will become vertex of c1
if (T.is_infinite(vhf1)) return false;
int i1 = c1->index( c->vertex(i) );
int j1 = c1->index( c->vertex(j) );
int next1 = CGAL::Triangulation_utils_3::next_around_edge(i1,j1);
CH c2 = c1->neighbor( next1 );
VH vhf2 = c1->vertex( next1 ); // will become vertex of c3
if (T.is_infinite(vhf2)) return false;
int i2 = c2->index( c->vertex(i) );
int j2 = c2->index( c->vertex(j) );
int next2 = CGAL::Triangulation_utils_3::next_around_edge(i2,j2);
if (c != c2->neighbor( next2 ) ) return false; // edge degree != 3
VH vhf3 = c2->vertex( next2 );
if (T.is_infinite(vhf3)) return false;
VH vhl = c->vertex(i);
VH vhr = c->vertex(j);
// the two new tets are
// vhl, vhf1, vhf2, vhf3
// vhf1, vhf2, vhf3, vhr
// compute their volumes, if any is neagtive stop
FT voll = CGAL::volume(vhl->point(),
vhf1->point(),
vhf2->point(),
vhf3->point());
if (voll <= 0.0) return false;
FT volr = CGAL::volume(vhf1->point(),
vhf2->point(),
vhf3->point(),
vhr->point());
if (volr <= 0.0) return false;
// compute the squared face areas
FT sasl1 = CGAL::squared_area(vhl->point(),vhf1->point(),vhf2->point());
FT sasl2 = CGAL::squared_area(vhl->point(),vhf2->point(),vhf3->point());
FT sasl3 = CGAL::squared_area(vhl->point(),vhf3->point(),vhf1->point());
FT sasr1 = CGAL::squared_area(vhr->point(),vhf1->point(),vhf2->point());
FT sasr2 = CGAL::squared_area(vhr->point(),vhf2->point(),vhf3->point());
FT sasr3 = CGAL::squared_area(vhr->point(),vhf3->point(),vhf1->point());
FT sasi = CGAL::squared_area(vhf1->point(),vhf2->point(),vhf3->point());
fi.hi1 = (sasl1+sasl2+sasl3+sasi)/voll;
fi.hi2 = (sasr1+sasr2+sasr3+sasi)/volr;
fi.hi3 = FT(0);
if (hi[c1->info()]+hi[c2->info()]+hi[c->info()] <= (fi.hi1+fi.hi2))
return false;
fi.val = hi[c1->info()]+hi[c2->info()]+hi[c->info()] - (fi.hi1+fi.hi2);
fi.c = c;
fi.i = i;
fi.j = j;
fi.ci1 = c->info();
fi.ci2 = c1->info();
fi.ci3 = c2->info();
return true;
}
bool perform_edge_flip(flip_info fi, Facet &f)
{
CH c = fi.c;
int i = fi.i, j = fi.j;
if (c==NULL || c->info() != fi.ci1) return false;
// get vertex handles of vertices around edge
int next = CGAL::Triangulation_utils_3::next_around_edge(i,j);
CH c1 = c->neighbor( next );
if (c1==NULL || c1->info() != fi.ci2) return false;
VH v1 = c->vertex( next ); // will become vertex of c1
int i1 = c1->index( c->vertex(i) );
int j1 = c1->index( c->vertex(j) );
int next1 = CGAL::Triangulation_utils_3::next_around_edge(i1,j1);
CH c2 = c1->neighbor( next1 );
if (c2==NULL || c2->info() != fi.ci3) return false;
VH v2 = c1->vertex( next1 ); // will become vertex of c2
int i2 = c2->index( c->vertex(i) );
int j2 = c2->index( c->vertex(j) );
int next2 = CGAL::Triangulation_utils_3::next_around_edge(i2,j2);
VH v3 = c2->vertex( next2 );
c->vertex(i)->set_cell(c1);
c->vertex(j)->set_cell(c2);
c1->set_vertex( j1, v1 );
v1->set_cell(c1);
c2->set_vertex( i2, v2 );
v2->set_cell(c2);
set_adjacency(c1, next1,c2->neighbor(j2), c2->neighbor(j2)->index(c2));
set_adjacency(c2,c2->index(v1),c1->neighbor(i1),c1->neighbor(i1)->index(c1));
set_adjacency(c1, i1, c2, j2);
set_adjacency(c1, 6-i1-j1-next1, c->neighbor(j), c->neighbor(j)->index(c));
set_adjacency(c2, next2, c->neighbor(i), c->neighbor(i)->index(c));
v3->set_cell( c2 );
// cleanup
T.tds().delete_cell( c ); // perhaps better not to do this? or is it necessary?
c->info() = -1;
c1->info() = cci++;
hi.push_back(fi.hi1);
c2->info() = cci++;
hi.push_back(fi.hi2);
f.first = c1;
f.second = i1;
return true;
}
void flip2harmonic(bool dofaces)
{
CGAL::Real_timer rt;
double total_time = 0.0;
hi.resize(0);
// (re)compute harmonic indices, set up original cell ids
rt.start();
Finite_cells_iterator cit = T.finite_cells_begin();
for (cci = 0; cit != T.finite_cells_end(); ++cit, ++cci)
{
cit->info() = cci;
hi.push_back(eta(cit));
}
rt.stop();
std::cerr << "Computed " << cci << " harmonic indices in " << rt.time() << " seconds" << std::endl;
total_time += rt.time();
std::priority_queue<flip_info, std::vector<flip_info>, flip_comp> fpq;
int nf = 0, ne = 0;
// check all finite edges, insert the ones that should be flipped into queue
rt.reset(); rt.start();
Finite_edges_iterator eit = T.finite_edges_begin();
for (; eit != T.finite_edges_end(); ++eit)
{
flip_info fi;
if (check_edge(eit->first,eit->second,eit->third,fi))
{
fpq.push(fi);
ne++;
}
}
rt.stop();
std::cerr << "Found " << ne << " initial edge flips in " << rt.time() << " seconds" << std::endl;
total_time += rt.time();
// check all finite faces, insert the ones that should be flipped into queue
if (dofaces)
{
rt.reset(); rt.start();
Finite_facets_iterator fit = T.finite_facets_begin();
for (; fit != T.finite_facets_end(); ++fit)
{
flip_info fi;
if (check_face(fit->first,fit->second,fi))
{
fpq.push(fi);
nf++;
}
}
rt.stop();
std::cerr << "Found " << nf << " initial face flips in " << rt.time() << " seconds" << std::endl;
total_time += rt.time();
}
rt.reset(); rt.start();
nf = 0; ne = 0;
while (!fpq.empty())
{
flip_info fi = fpq.top();
fpq.pop();
// if j entry in face_info is -1 this is a face flip
if (fi.j < 0)
{
Edge e;
if (perform_face_flip(fi,e))
{
// go through three tets incident on new edge
// for each of them check faces on convex hull
// and the one edge opposite to the new diagonal
CH c = e.first;
int i = e.second;
int j = e.third;
int next = CGAL::Triangulation_utils_3::next_around_edge(i,j);
int k = 6-(i+j+next);
flip_info fifi, fifj, fie;
if (check_face(c,i,fifi))
fpq.push(fifi);
if (check_face(c,j,fifj))
fpq.push(fifj);
if (check_edge(c,next,k,fie))
fpq.push(fie);
CH c1 = c->neighbor( next );
VH v1 = c->vertex( next );
int i1 = c1->index( c->vertex(i) );
int j1 = c1->index( c->vertex(j) );
int next1 = CGAL::Triangulation_utils_3::next_around_edge(i1,j1);
int k1 = 6-(i1+j1+next1);
flip_info fifi1, fifj1, fie1;
if (check_face(c,i,fifi1))
fpq.push(fifi1);
if (check_face(c,j,fifj1))
fpq.push(fifj1);
if (check_edge(c,next,k,fie1))
fpq.push(fie1);
CH c2 = c1->neighbor( next1 );
VH v2 = c1->vertex( next1 );
int i2 = c2->index( c->vertex(i) );
int j2 = c2->index( c->vertex(j) );
int next2 = CGAL::Triangulation_utils_3::next_around_edge(i2,j2);
int k2 = 6-(i1+j2+next2);
flip_info fifi2, fifj2, fie2;
if (check_face(c,i,fifi2))
fpq.push(fifi2);
if (check_face(c,j,fifj2))
fpq.push(fifj2);
if (check_edge(c,next,k,fie2))
fpq.push(fie2);
nf++;
}
}
else // it is an edge flip
{
Facet f;
if (perform_edge_flip(fi,f))
{
// check the edges on the convex hull
// except the ones on the new triangle
flip_info fiel[3],fier[3];//1,fiel2,fiel3;
CH n = f.first->neighbor(f.second);
int in = n->index(f.first);
for (int k = 0; k < 3; k++)
{
if (check_edge(f.first,f.second,(f.second+1+k)&3,fiel[k]))
fpq.push(fiel[k]);
if (check_edge(n,in,(in+1+k)&3,fier[k]))
fpq.push(fier[k]);
}
// check faces on convex hull
if (dofaces)
{
flip_info fifl[3],fifr[3];//,fifl2,fifl3;
for (int k = 0; k < 3; k++)
{
if (check_face(f.first,(f.second+1+k)&3,fifl[k]))
fpq.push(fifl[k]);
if (check_face(n,(in+1+k)&3,fifr[k]))
fpq.push(fifr[k]);
}
}
ne++;
}
}
}
rt.stop();
std::cerr << "Performed " << ne << " edge and " << nf << " face flips in " << rt.time() << " seconds" << std::endl;
total_time += rt.time();
std::cerr << "Total time for harmonizing Delaunay triangulation: " << total_time << " seconds" << std::endl;
}
int main(int argc, char *argv[])
{
CLI::App app{"Harmonic triangulation"};
std::string infilename = "default";
CLI::Option *offopt = app.add_option("-o,--off", infilename, "Point set input (off)");
CLI::Option *plyopt = app.add_option("-p,--ply", infilename, "Point set input (ply)");
std::string delfilename = "default";
CLI::Option *delopt = app.add_option("-d,--delaunay", delfilename, "write Delaunay tet mesh");
std::string cgalfilename = "default";
CLI::Option *cgalopt = app.add_option("-c,--cgaloutput", cgalfilename, "write the harmonized mesh in cgal format");
std::string tetviewfilename = "default";
CLI::Option *tetviewopt = app.add_option("-t,--tetviewoutput", tetviewfilename, "write the harmonized mesh in tetview format");
bool faceflips;
app.add_flag("-f,--faceflips", faceflips, "also check for face flips");
CLI11_PARSE(app, argc, argv);
if ( !(*offopt) && !(*plyopt))
{
std::cerr << "Input file required" << std::endl;
exit(0);
}
std::ifstream in(infilename);
if (!in)
{
std::cerr << "Error: cannot open file " << infilename << std::endl;
exit(0);
}
std::vector<Point> P;
if (*offopt)
{
if (!CGAL::read_off_points(in,std::back_inserter(P)))
{
std::cerr << "Error: cannot read off file " << infilename << std::endl;
exit(0);
}
}
if (*plyopt)
{
if (!CGAL::read_ply_points(in,std::back_inserter(P)))
{
std::cerr << "Error: cannot read ply file " << infilename << std::endl;
exit(0);
}
}
n = P.size();
std::cout << "Read " << n << " points from file " << infilename << std::endl;
std::vector<std::pair<Point,int> > Pi(n);
for (int i = 0; i < n; i++)
Pi[i] = std::make_pair(P[i],i);
CGAL::Real_timer rt;
std::cout << "Performing Delaunay triangulation on " << n << " points" << std::endl;
rt.start();
T = DT (Pi.begin(), Pi.end());
rt.stop();
std::cout << "Done in " << rt.time() << " seconds " << std::endl;
Finite_vertices_iterator vit = T.finite_vertices_begin();
for (int i = 0; vit != T.finite_vertices_end(); ++vit, ++i)
vit->info() = i;
if (*delopt) write_cgal(delfilename);
stats();
flip2harmonic(faceflips);
stats();
if (*cgalopt) write_cgal(cgalfilename);
if (*tetviewopt) write_tetgen(tetviewfilename);
return 0;
}