-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler_operations.h
1806 lines (1604 loc) · 61.4 KB
/
Euler_operations.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
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2014 GeometryFactory (France). All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL$
// $Id$
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s) : Philipp Moeller
#ifndef CGAL_EULER_OPERATIONS_H
#define CGAL_EULER_OPERATIONS_H
#include <stdexcept>
#include <algorithm>
#include <vector>
#include <boost/graph/graph_traits.hpp>
#include <CGAL/boost/graph/properties.h>
#include <CGAL/assertions.h>
#include <CGAL/boost/graph/helpers.h>
#include <CGAL/boost/graph/internal/helpers.h>
#include <CGAL/boost/graph/iterator.h>
#include <CGAL/boost/graph/named_params_helper.h>
#include <boost/container/small_vector.hpp>
namespace CGAL {
/// \cond SKIP_IN_MANUAL
namespace EulerImpl {
template<typename Graph>
typename boost::graph_traits<Graph>::halfedge_descriptor
join_face(typename boost::graph_traits<Graph>::halfedge_descriptor h,
Graph& g)
{
typedef typename boost::graph_traits<Graph> Traits;
typedef typename Traits::halfedge_descriptor halfedge_descriptor;
typedef typename Traits::face_descriptor face_descriptor;
halfedge_descriptor hop = opposite(h,g);
halfedge_descriptor hprev = prev(h, g), gprev = prev(hop, g);
face_descriptor f = face(h, g), f2 = face(hop, g);
internal::remove_tip(hprev, g);
internal::remove_tip(gprev, g);
if(! is_border(hop,g)){
remove_face(f2, g);
}
bool fnull = is_border(h,g);
halfedge_descriptor hprev2 = hprev;
while(hprev2 != gprev) {
hprev2 = next(hprev2, g);
set_face(hprev2, f, g);
}
if (! fnull)
set_halfedge(f, hprev, g);
set_halfedge(target(hprev,g), hprev, g);
set_halfedge(target(gprev,g), gprev, g);
// internal::set_constant_vertex_is_border(g, target(h, g));
// internal::set_constant_vertex_is_border(g, target(opposite(h, g), g));
remove_edge(edge(h, g), g);
return hprev;
}
} // namespace EulerImpl
/// \endcond
namespace Euler {
/// \ingroup PkgBGLEulerOperations
/// @{
/**
* joins the two vertices incident to `h`, (that is `source(h, g)` and
* `target(h, g)`) and removes `source(h,g)`. Returns the predecessor
* of `h` around the vertex, i.e., `prev(opposite(h,g))`. The
* invariant `join_vertex(split_vertex(h,g),g)` returns `h`. The
* time complexity is linear in the degree of the vertex removed.
*
* \image html join_vertex.svg
*
* \tparam Graph must be a model of `MutableFaceGraph`
*
* \param g the graph
* \param h the halfedge which incident vertices are joint
*
* \returns `prev(opposite(h,g))`
*
* \pre The size of the faces incident to `h` and `opposite(h,g)` is at least 4.
*
* \post `source(h, g)` is invalidated
* \post `h` is invalidated
*
* \sa `split_vertex()`
*/
template<typename Graph>
typename boost::graph_traits<Graph>::halfedge_descriptor
join_vertex(typename boost::graph_traits<Graph>::halfedge_descriptor h,
Graph& g)
{
typedef typename boost::graph_traits<Graph> Traits;
typedef typename Traits::halfedge_descriptor halfedge_descriptor;
typedef typename Traits::vertex_descriptor vertex_descriptor;
typedef Halfedge_around_target_iterator<Graph> halfedge_around_vertex_iterator;
halfedge_descriptor hop = opposite(h, g)
, hprev = prev(hop, g)
, gprev = prev(h, g)
, hnext = next(hop, g)
, gnext = next(h, g);
vertex_descriptor v_to_remove = target(hop, g)
, v = target(h, g);
// this assertion fires needlessly
// CGAL_precondition(std::distance(
// halfedges_around_face(e, g).first,
// halfedges_around_face(e, g).second) >= 4);
CGAL_assertion( halfedge(v_to_remove, v, g).first == h );
halfedge_around_vertex_iterator ieb, iee;
for(boost::tie(ieb, iee) = halfedges_around_target(hop, g); ieb != iee; ++ieb) {
CGAL_assertion( target(*ieb,g) == v_to_remove);
set_target(*ieb ,v , g);
}
set_next(hprev, hnext, g);
set_next(gprev, gnext, g);
set_halfedge(v, gprev, g);
// internal::set_constant_vertex_is_border(g, v);
if(! is_border(gprev,g)){
set_halfedge(face(gprev,g),gprev,g);
}
if(! is_border(hprev,g)){
set_halfedge(face(hprev,g),hprev,g);
}
remove_edge(edge(h, g), g);
remove_vertex(v_to_remove, g);
return hprev;
}
/**
* splits the target vertex `v` of `h1` and `h2`, and connects the new vertex
* and `v` with a new edge. Let `hnew` be `opposite(next(h1, g), g)` after the
* split. The split regroups the halfedges around the two vertices. The
* edge sequence `hnew`, `opposite(next(h2, g), g)`, ..., `h1`
* remains around the old vertex, while the halfedge sequence
* `opposite(hnew, g)`, `opposite(next(h1, g), g)` (before the
* split), ..., `h2` is regrouped around the new vertex. The split
* returns `hnew`, i.e., the new edge incident to vertex `v`. The
* time is proportional to the distance from `h1` to `h2` around the
* vertex.
*
* \image html split_vertex.svg
*
* \tparam Graph must be a model of `MutableFaceGraph`
*
* \param g the graph
* \param h1 halfedge descriptor
* \param h2 halfedge descriptor
*
* \returns `hnew`
*
* \pre `target(h1, g) == target(h2, g)`, that is `h1` and `h2` are incident to the same vertex
* \pre `h1 != h2`, that is no antennas
*
* \sa `join_vertex()`
*
*/
template<typename Graph>
typename boost::graph_traits<Graph>::halfedge_descriptor
split_vertex(typename boost::graph_traits<Graph>::halfedge_descriptor h1,
typename boost::graph_traits<Graph>::halfedge_descriptor h2,
Graph& g)
{
CGAL_assertion(h1 != h2);
CGAL_assertion(target(h1, g) == target(h2, g));
typename boost::graph_traits<Graph>::halfedge_descriptor
hnew = halfedge(add_edge(g), g),
hnewopp = opposite(hnew, g);
typename boost::graph_traits<Graph>::vertex_descriptor
vnew = add_vertex(g);
internal::insert_halfedge(hnew, h2, g);
internal::insert_halfedge(hnewopp, h1, g);
set_target(hnew, target(h1, g), g);
typename boost::graph_traits<Graph>::halfedge_descriptor
end = hnewopp;
do
{
set_target(hnewopp, vnew, g);
hnewopp = opposite(next(hnewopp, g), g);
} while (hnewopp != end);
internal::set_vertex_halfedge(hnew, g);
// internal::set_constant_vertex_is_border(g, target(hnew, g));
internal::set_vertex_halfedge(hnewopp, g);
// internal::set_constant_vertex_is_border(g, target(hnewopp, g));
return hnew;
}
/**
* splits the halfedge `h` into two halfedges inserting a new vertex that is a copy of `vertex(opposite(h,g),g)`.
* Is equivalent to `opposite(split_vertex( prev(h,g), opposite(h,g),g), g)`.
* \returns the new halfedge `hnew` pointing to the inserted vertex. The new halfedge is followed by the old halfedge, i.e., `next(hnew,g) == h`.
*/
template<typename Graph>
typename boost::graph_traits<Graph>::halfedge_descriptor
split_edge(typename boost::graph_traits<Graph>::halfedge_descriptor h, Graph& g)
{ return opposite(split_vertex(prev(h,g), opposite(h,g),g), g); }
/**
* joins the two faces incident to `h` and `opposite(h,g)`.
* The faces may be holes.
*
* If `Graph` is a model of `MutableFaceGraph`
* the face incident to `opposite(h,g)` is removed.
*
* `join_face()` and `split_face()` are inverse operations, that is
* `join_face(split_face(h,g),g)` returns `h`.
*
* \image html join_face.svg
*
* \tparam Graph must be a model of `MutableFaceGraph`.
* \param g the graph
* \param h the halfedge incident to one of the faces to be joined.
*
* \returns `prev(h,g)`
*
* \pre `out_degree(source(h,g)), g)) >= 3`
* \pre `out_degree(target(h,g)) >= 3`
*
* \sa `split_face()`
*/
template<typename Graph>
typename boost::graph_traits<Graph>::halfedge_descriptor
join_face(typename boost::graph_traits<Graph>::halfedge_descriptor h,
Graph& g)
{
return EulerImpl::join_face(h,g);
}
/**
* splits the face incident to `h1` and `h2`. Creates the opposite
* halfedges `h3` and `h4`, such that `next(h1,g) == h3` and `next(h2,g) == h4`.
* Performs the inverse operation to `join_face()`.
*
* If `Graph` is a model of `MutableFaceGraph` and if the update of faces is not disabled
* a new face incident to `h4` is added.
*
* \image html split_face.svg
*
* \tparam Graph must be a model of `MutableFaceGraph`
*
* \param g the graph
* \param h1
* \param h2
*
* \returns `h3`
*
* \pre `h1` and `h2` are incident to the same face
* \pre `h1 != h2`
* \pre `next(h1,g) != h2` and `next(h2,g) != h1` (no loop)
*/
template<typename Graph>
typename boost::graph_traits<Graph>::halfedge_descriptor
split_face(typename boost::graph_traits<Graph>::halfedge_descriptor h1,
typename boost::graph_traits<Graph>::halfedge_descriptor h2,
Graph& g)
{
typedef typename boost::graph_traits<Graph> Traits;
typedef typename Traits::halfedge_descriptor halfedge_descriptor;
typedef typename Traits::face_descriptor face_descriptor;
halfedge_descriptor hnew = halfedge(add_edge(g), g);
face_descriptor fnew = add_face(g);
internal::insert_tip( hnew, h2, g);
internal::insert_tip( opposite(hnew, g), h1, g);
set_face( hnew, face(h1,g), g);
internal::set_face_in_face_loop(opposite(hnew,g), fnew, g);
set_halfedge(face(hnew,g), hnew, g);
set_halfedge(face(opposite(hnew,g),g), opposite(hnew,g), g);
return hnew;
}
/**
* glues the cycle of halfedges of `h1` and `h2` together.
* The vertices in the cycle of `h2` get removed.
* If `h1` or `h2` are not border halfedges their faces get removed.
* The vertices on the face cycle of `h1` get removed.
* The invariant `join_loop(h1, split_loop(h1,h2,h3,g), g)` returns `h1` and keeps
* the graph unchanged.
*
* \image html join_loop.svg
*
* \tparam Graph must be a `MutableFaceGraph`
*
* \returns `h1`.
*
* \pre The faces incident to `h` and `g` are different and have equal number of edges.
*/
template<typename Graph>
typename boost::graph_traits<Graph>::halfedge_descriptor
join_loop(typename boost::graph_traits<Graph>::halfedge_descriptor h1,
typename boost::graph_traits<Graph>::halfedge_descriptor h2,
Graph& g)
{
typedef typename boost::graph_traits<Graph> Traits;
typedef typename Traits::halfedge_descriptor halfedge_descriptor;
CGAL_precondition( is_border(h1,g) || face(h1, g) != face(h2, g));
if (! is_border(h1,g))
remove_face(face(h1, g), g);
if (! is_border(h2,g))
remove_face(face(h2,g), g);
halfedge_descriptor hi = h1;
halfedge_descriptor gi = h2;
CGAL_assertion_code( std::size_t termination_count = 0;)
do {
CGAL_assertion( ++termination_count != 0);
halfedge_descriptor hii = hi;
halfedge_descriptor gii = gi;
hi = next(hi, g);
// gi = find_prev(gi); // Replaced by search around vertex.
set_face( hii, face( opposite(gii, g), g), g);
set_halfedge(face(hii, g), hii, g);
remove_vertex(target(opposite(gii, g), g), g);
if ( next(opposite(next(opposite(gii,g), g), g), g) == gii) {
gi = opposite(next(opposite(gii,g),g), g);
} else {
set_next(hii, next(opposite(gii,g), g), g);
gii = opposite(next(opposite(gii, g), g), g);
set_target( gii, target(hii, g), g);
while ( next(opposite(next(gii, g), g), g) != gi) {
CGAL_assertion( ++termination_count != 0);
gii = opposite(next(gii,g), g);
set_target( gii, target(hii, g), g);
}
gi = opposite(next(gii,g), g);
set_next(gii, hi, g);
}
} while ( hi != h1);
CGAL_assertion( gi == h2);
do {
halfedge_descriptor gii = gi;
gi = next(gi, g);
remove_edge(edge(gii,g), g);
} while ( gi != h2);
return h1;
}
/**
* cuts the graph along the cycle `(h1,h2,h3)` changing the genus
* (halfedge `h3` runs on the backside of the three dimensional figure below).
* Three new vertices, three new pairs of halfedges,
* and two new triangular faces are created.
*
* `h1`, `h2`, and `h3` will be incident to the first new face.
*
* Note that `split_loop()` does not deal with properties of new vertices, halfedges, and faces.
*
* \image html split_loop.svg
*
* \tparam Graph must be a `MutableFaceGraph`
*
* \returns the halfedge incident to the second new face.
*
* \pre `h1`, `h2`, and `h3` denote distinct, consecutive halfedges of the graph
* and form a cycle: i.e., `target(h1) == target(opposite(h2,g),g)`, … ,
* `target(h3,g) == target(opposite(h1,g),g)`.
* \pre The six faces incident to `h1`, `h2`, and `h3` are all distinct.
*/
template<typename Graph>
typename boost::graph_traits<Graph>::halfedge_descriptor
split_loop(typename boost::graph_traits<Graph>::halfedge_descriptor h1,
typename boost::graph_traits<Graph>::halfedge_descriptor h2,
typename boost::graph_traits<Graph>::halfedge_descriptor h3,
Graph& g)
{
typedef typename boost::graph_traits<Graph> Traits;
typedef typename Traits::halfedge_descriptor halfedge_descriptor;
typedef typename Traits::face_descriptor face_descriptor;
halfedge_descriptor h = h1, i = h2, j = h3;
CGAL_precondition( h != i);
CGAL_precondition( h != j);
CGAL_precondition( i != j);
CGAL_precondition( target(h,g) == target(opposite(i,g),g));
CGAL_precondition( target(i,g) == target(opposite(j,g),g));
CGAL_precondition( target(j,g) == target(opposite(h,g),g));
// Create a copy of the triangle.
halfedge_descriptor hnew = internal::copy(h,g);
halfedge_descriptor inew = internal::copy(i,g);
halfedge_descriptor jnew = internal::copy(j,g);
internal::close_tip( hnew, add_vertex(g), g);
internal::close_tip( inew, add_vertex(g), g);
internal::close_tip( jnew, add_vertex(g), g);
internal::insert_tip( opposite(inew, g), hnew, g);
internal::insert_tip( opposite(jnew, g), inew, g);
internal::insert_tip( opposite(hnew, g), jnew, g);
// Make the new incidences with the old stucture.
CGAL_assertion_code( std::size_t termination_count = 0;)
if ( next(h,g) != i) {
halfedge_descriptor nh = next(h, g);
set_next(h, i, g);
set_next(hnew, nh, g);
nh = opposite(nh, g);
while ( next(nh, g) != i) {
CGAL_assertion( ++termination_count != 0);
set_target( nh, target(hnew,g), g);
nh = opposite(next(nh, g), g);
}
set_target( nh, target(hnew,g), g);
set_next(nh, inew, g);
}
if ( next(i, g) != j) {
halfedge_descriptor nh = next(i, g);
set_next(i, j, g);
set_next(inew, nh, g);
nh = opposite(nh,g);
while ( next(nh,g) != j) {
CGAL_assertion( ++termination_count != 0);
set_target( nh, target(inew, g), g);
nh = opposite(next(nh, g), g);
}
set_target( nh, target(inew, g), g);
set_next(nh, jnew, g);
}
if ( next(j,g) != h) {
halfedge_descriptor nh = next(j, g);
set_next(j, h, g);
set_next(jnew, nh, g);
nh = opposite(nh, g);
while ( next(nh,g) != h) {
CGAL_assertion( ++termination_count != 0);
set_target( nh, target(jnew, g), g);
nh = opposite(next(nh, g), g);
}
set_target(nh, target(jnew, g), g);
set_next(nh, hnew, g);
}
// Fill the holes with two new faces.
face_descriptor f = add_face(g);
set_face( h, f, g);
set_face( i, f, g);
set_face( j, f, g);
set_halfedge(face(h,g), h, g);
f = add_face(g);
set_face( opposite(hnew, g), f, g);
set_face( opposite(inew, g), f, g);
set_face( opposite(jnew, g), f, g);
set_halfedge(face(opposite(hnew,g),g), opposite(hnew,g), g);
// Take care of maybe changed halfedge pointers.
set_halfedge(face(hnew, g), hnew, g);
set_halfedge(face(inew, g), inew, g);
set_halfedge(face(jnew, g), jnew, g);
set_halfedge(target(hnew, g), hnew, g);
set_halfedge(target(inew, g), inew, g);
set_halfedge(target(jnew, g), jnew, g);
return opposite(hnew, g);
}
/**
* removes the incident face of `h` and changes all halfedges incident to the face into border halfedges
* or removes them from the graph if they were already border halfedges.
*
* If this creates isolated vertices they get removed as well.
*
* \image html remove_face.svg
* \image html remove_face_and_vertex.svg
*
* \tparam Graph must be a model of `MutableFaceGraph`
*
* \pre `h` is not a border halfedge
*
* \sa `make_hole()` for a more specialized variant.
*/
template< typename Graph >
void remove_face(typename boost::graph_traits<Graph>::halfedge_descriptor h,
Graph& g)
{
typedef typename boost::graph_traits<Graph> Traits;
typedef typename Traits::halfedge_descriptor halfedge_descriptor;
typedef typename Traits::face_descriptor face_descriptor;
CGAL_precondition(! is_border(h,g));
face_descriptor f = face(h, g);
halfedge_descriptor end = h;
do {
internal::set_border(h,g);
halfedge_descriptor nh = next(h, g);
bool h_border = is_border(opposite(h, g),g);
bool nh_bborder = is_border(opposite(nh, g),g);
if(h_border && nh_bborder && next(opposite(nh, g), g) == opposite(h, g)) {
remove_vertex(target(h, g), g);
if(h != end)
remove_edge(edge(h, g), g);
} else {
if(nh_bborder) {
internal::set_vertex_halfedge(opposite(next(opposite(nh, g), g), g), g);
internal::remove_tip(h, g);
//internal::set_constant_vertex_is_border(g, target(h, g));
}
if(h_border) {
internal::set_vertex_halfedge(opposite(next(h, g), g), g);
internal::remove_tip(prev(opposite(h, g), g), g);
//internal::set_constant_vertex_is_border(g, target(prev(opposite(h, g), g), g));
if(h != end)
remove_edge(edge(h, g), g);
}
}
h = nh;
} while(h != end);
remove_face(f, g);
if(is_border(opposite(h, g),g))
remove_edge(edge(h, g), g);
}
/**
* adds and returns the edge `e` connecting `s` and `t`
* halfedge(e, g) has s as source and t as target
*/
template<typename Graph>
typename boost::graph_traits<Graph>::edge_descriptor
add_edge(typename boost::graph_traits<Graph>::vertex_descriptor s,
typename boost::graph_traits<Graph>::vertex_descriptor t,
Graph& g)
{
typename boost::graph_traits<Graph>::edge_descriptor e = add_edge(g);
set_target(halfedge(e, g), t, g);
set_target(opposite(halfedge(e, g), g), s, g);
return e;
}
/**
* checks whether a new face defined by a range of vertices (identified by their descriptors,
* `boost::graph_traits<Graph>::%vertex_descriptor`) can be added.
*/
template <typename VertexRange,typename PMesh>
bool can_add_face(const VertexRange& vrange, const PMesh& sm)
{
typedef typename boost::graph_traits<PMesh>::vertex_descriptor vertex_descriptor;
typedef typename boost::graph_traits<PMesh>::halfedge_descriptor halfedge_descriptor;
std::vector<typename boost::graph_traits<PMesh>::vertex_descriptor> face(vrange.begin(), vrange.end());
std::size_t N = face.size();
std::vector<vertex_descriptor> f2(face);
std::sort(f2.begin(), f2.end());
typename std::vector<vertex_descriptor>::iterator it = std::unique(f2.begin(),f2.end());
if((N > 0) && (it != f2.end())){
return false;
}
if(N < 3){
return false;
}
face.push_back(face.front());
for(std::size_t i=0; i < N; ++i){
halfedge_descriptor hd;
bool found;
boost::tie(hd,found) = halfedge(face[i],face[i+1],sm);
if(found && (! is_border(hd,sm))){
return false;
}
}
for(std::size_t i=0; i < N; ++i){
if(halfedge(face[i],sm) == boost::graph_traits<PMesh>::null_halfedge()){
continue;
}
if(! is_border(face[i],sm)){
return false;
}
}
//Test if all halfedges of the new face
//are possibly consecutive border halfedges in the HDS.
//Possibly because it may be not directly encoded in the HDS
//(using next() function ). This situation can occur when one or
//more facets share only a vertex: For example, the new facet we try to add
//would make the vertex indices[i] a manifold but this should be forbidden
//if a facet only incident to that vertex has already been inserted.
//We check this for each vertex of the sequence.
for(std::size_t i = 0; i < N; ++i) {
std::size_t prev_index= (i-1+N)%N;
std::size_t next_index= (i+1)%N;
vertex_descriptor previous_vertex = face[ prev_index ];
vertex_descriptor next_vertex = face[ next_index ];
halfedge_descriptor halfedge_around_vertex = halfedge(face[i],sm);
if ( halfedge_around_vertex == boost::graph_traits<PMesh>::null_halfedge() ||
halfedge(previous_vertex,sm) == boost::graph_traits<PMesh>::null_halfedge()||
halfedge(next_vertex,sm) == boost::graph_traits<PMesh>::null_halfedge()
) continue;
halfedge_descriptor start=halfedge_around_vertex;
//halfedges pointing to/running out from vertex indices[i]
//and that need to be possibly consecutive
halfedge_descriptor prev_hd= boost::graph_traits<PMesh>::null_halfedge(),next_hd= boost::graph_traits<PMesh>::null_halfedge();
halfedge_around_vertex = opposite(next(halfedge_around_vertex,sm),sm);
//look for a halfedge incident to vertex indices[i]
//and which opposite is incident to previous_vertex
do{
if(target(opposite(halfedge_around_vertex,sm),sm)==previous_vertex){
prev_hd=halfedge_around_vertex;
CGAL_precondition(is_border(prev_hd,sm));
break;
}
halfedge_around_vertex = opposite(next(halfedge_around_vertex,sm),sm);
}
while (halfedge_around_vertex!=start);
if (prev_hd != boost::graph_traits<PMesh>::null_halfedge()){
halfedge_around_vertex = opposite(next(halfedge_around_vertex,sm),sm);
//prev_hd and next are already consecutive in the HDS
if (target(opposite(halfedge_around_vertex,sm),sm)==next_vertex) continue;
//look for a border halfedge which opposite is
//incident to next_vertex: set next halfedge
do
{
if (target(opposite(halfedge_around_vertex,sm),sm)==next_vertex){
next_hd = opposite(halfedge_around_vertex,sm);
break;
}
halfedge_around_vertex = opposite(next(halfedge_around_vertex,sm),sm);
}
while(halfedge_around_vertex != prev_hd);
if (next_hd==boost::graph_traits<PMesh>::null_halfedge()) continue;
//check if no constraint prevents
//prev_hd and next_hd to be adjacent:
do{
halfedge_around_vertex = opposite(next(halfedge_around_vertex, sm),sm);
if ( is_border(opposite(halfedge_around_vertex,sm),sm) ) break;
}
while (halfedge_around_vertex != prev_hd);
if (halfedge_around_vertex == prev_hd) return false;
start = halfedge_around_vertex;
}
}
return true;
}
/**
* adds a new face defined by a range of vertices (identified by their descriptors,
* `boost::graph_traits<Graph>::%vertex_descriptor`).
* For each pair of consecutive vertices, the corresponding halfedge
* is added in `g` if new, and its connectivity is updated otherwise.
* The face can be added only at the boundary of `g`, or as a new connected component.
*
* @pre `vr` contains at least 3 vertices
* @returns the added face descriptor, or `boost::graph_traits<Graph>::%null_face()` if the face could not be added.
*/
template< typename Graph, typename VertexRange >
typename boost::graph_traits<Graph>::face_descriptor
add_face(const VertexRange& vr, Graph& g)
{
typedef typename boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor;
typedef typename boost::graph_traits<Graph>::halfedge_descriptor halfedge_descriptor;
typedef typename boost::graph_traits<Graph>::face_descriptor face_descriptor;
typedef typename boost::graph_traits<Graph>::edge_descriptor edge_descriptor;
std::vector<vertex_descriptor> vertices(vr.begin(), vr.end()); // quick and dirty copy
unsigned int n = (unsigned int)vertices.size();
//check that every vertex is unique
std::sort(vertices.begin(), vertices.end());
if(std::adjacent_find(vertices.begin(), vertices.end()) != vertices.end()){
return boost::graph_traits<Graph>::null_face();
}
std::copy(vr.begin(), vr.end(), vertices.begin());
// don't allow degenerated faces
if(n <= 2){
return boost::graph_traits<Graph>::null_face();
}
std::vector<halfedge_descriptor> halfedges(n);
std::vector<bool> is_new(n);
for (unsigned int i = 0, ii = 1; i<n; ++i, ++ii, ii %= n)
{
if ( ! internal::is_isolated(vertices[i], g)
&& ! is_border(vertices[i], g))
return boost::graph_traits<Graph>::null_face();
std::pair<halfedge_descriptor, bool> he
= halfedge(vertices[i], vertices[ii], g);
halfedges[i] = he.first;//collect if exists
is_new[i] = !(he.second/*true if exists*/);
if (!is_new[i] && !is_border(halfedges[i], g))
return boost::graph_traits<Graph>::null_face();
}
halfedge_descriptor inner_next, inner_prev,
outer_next, outer_prev,
border_next, border_prev,
patch_start, patch_end;
// cache for set_next and vertex' set_halfedge
typedef std::pair<halfedge_descriptor, halfedge_descriptor> NextCacheEntry;
typedef boost::container::small_vector<NextCacheEntry,9> NextCache;
NextCache next_cache;
next_cache.reserve(3 * n);
// re-link patches if necessary
for (unsigned int i = 0, ii = 1; i<n; ++i, ++ii, ii %= n)
{
if (!is_new[i] && !is_new[ii])
{
inner_prev = halfedges[i];
inner_next = halfedges[ii];
if (next(inner_prev, g) != inner_next)
{
// here comes the ugly part... we have to relink a whole patch
// search a free gap
// free gap will be between border_prev and border_next
outer_prev = opposite(inner_next, g);
outer_next = opposite(inner_prev, g);
border_prev = outer_prev;
do{
border_prev = opposite(next(border_prev, g), g);
}while (!is_border(border_prev, g) || border_prev == inner_prev);
border_next = next(border_prev, g);
CGAL_assertion(is_border(border_prev, g));
CGAL_assertion(is_border(border_next, g));
if (border_next == inner_next)
return boost::graph_traits<Graph>::null_face();
// other halfedges' indices
patch_start = next(inner_prev, g);
patch_end = prev(inner_next, g);
// relink
next_cache.push_back(NextCacheEntry(border_prev, patch_start));
next_cache.push_back(NextCacheEntry(patch_end, border_next));
next_cache.push_back(NextCacheEntry(inner_prev, inner_next));
}
}
}
// create missing edges
for (unsigned int i = 0, ii = 1; i<n; ++i, ++ii, ii %= n)
{
if (is_new[i])
{
edge_descriptor ne = add_edge(vertices[i], vertices[ii], g);
halfedges[i] = halfedge(ne, g);
CGAL_assertion(halfedges[i] != boost::graph_traits<Graph>::null_halfedge());
set_face(opposite(halfedges[i], g), boost::graph_traits<Graph>::null_face(), g); // as it may be recycled we have to reset it
CGAL_assertion(source(halfedges[i], g) == vertices[i]);
}
}
// create the face
face_descriptor f = add_face(g);
set_halfedge(f, halfedges[n - 1], g);
// setup halfedges
for (unsigned int i = 0, ii = 1; i<n; ++i, ++ii, ii %= n)
{
vertex_descriptor v = vertices[ii];
inner_prev = halfedges[i];
inner_next = halfedges[ii];
unsigned int id = 0;
if (is_new[i]) id |= 1;
if (is_new[ii]) id |= 2;
if (id)
{
outer_prev = opposite(inner_next, g);
outer_next = opposite(inner_prev, g);
// set outer links
switch (id)
{
case 1: // prev is new, next is old
border_prev = prev(inner_next, g);
next_cache.push_back(NextCacheEntry(border_prev, outer_next));
set_halfedge(v, border_prev, g);
break;
case 2: // next is new, prev is old
border_next = next(inner_prev, g);
next_cache.push_back(NextCacheEntry(outer_prev, border_next));
set_halfedge(v, outer_prev, g);
break;
case 3: // both are new
{
// try to pick a border halfedge with v as target
halfedge_descriptor hv = halfedge(v, g);
if (hv != boost::graph_traits<Graph>::null_halfedge() && !is_border(hv, g))
{
for(halfedge_descriptor h_around_v : halfedges_around_target(hv, g))
if (is_border(h_around_v, g))
{
hv = h_around_v;
break;
}
if (!is_border(hv, g))
hv = boost::graph_traits<Graph>::null_halfedge();
}
if (hv == boost::graph_traits<Graph>::null_halfedge())
{
set_halfedge(v, outer_prev, g);
next_cache.push_back(NextCacheEntry(outer_prev, outer_next));
}
else
{
border_prev = hv;
border_next = next(border_prev, g);
next_cache.push_back(NextCacheEntry(border_prev, outer_next));
next_cache.push_back(NextCacheEntry(outer_prev, border_next));
}
break;
}
}
// set inner link
next_cache.push_back(NextCacheEntry(inner_prev, inner_next));
}
// set face index
set_face(halfedges[i], f, g);
}
// process next halfedge cache
typename NextCache::const_iterator ncIt(next_cache.begin()), ncEnd(next_cache.end());
for (; ncIt != ncEnd; ++ncIt)
set_next(ncIt->first, ncIt->second, g);
// adjust vertices' halfedge index
for (unsigned int i = 0; i<n; ++i)
internal::adjust_incoming_halfedge(vertices[i], g);
return f;
}
// TODO: add a visitor for new edge/vertex/face created
// TODO: doc (VertexRange is random access for now, making a copy to a vector as an noticeable impact on the runtime)
// TODO: handle and return false in case of non valid input?
// An interesting property of this function is that in case the mesh contains non-manifold boundary vertices,
// the connected components of faces incident to such a vertex will not be linked together around the
// vertex (boundary edges are connected by turning around the vertex in the interior of the mesh).
// This produce a deterministic behavior for non-manifold vertices.
template <class PolygonMesh, class RangeofVertexRange>
void add_faces(const RangeofVertexRange& faces_to_add, PolygonMesh& pm)
{
typedef typename boost::graph_traits<PolygonMesh> GT;
typedef typename GT::halfedge_descriptor halfedge_descriptor;
typedef typename GT::edge_descriptor edge_descriptor;
typedef typename GT::vertex_descriptor vertex_descriptor;
typedef typename GT::face_descriptor face_descriptor;
typedef typename RangeofVertexRange::const_iterator VTR_const_it;
typedef typename std::iterator_traits<VTR_const_it>::value_type Vertex_range;
typedef boost::container::small_vector<halfedge_descriptor,8> Halfedges;
typedef typename CGAL::GetInitializedVertexIndexMap<PolygonMesh>::type Vid_map;
Vid_map vid = CGAL::get_initialized_vertex_index_map(pm);
// TODO: add also this lambda as an Euler function?
auto add_new_edge = [&pm](vertex_descriptor v1, vertex_descriptor v2)
{
halfedge_descriptor v1v2 = halfedge(add_edge(pm), pm), v2v1=opposite(v1v2, pm);
if (halfedge(v1,pm)==GT::null_halfedge()) set_halfedge(v1, v2v1, pm);
if (halfedge(v2,pm)==GT::null_halfedge()) set_halfedge(v2, v1v2, pm);
set_target(v1v2, v2, pm);
set_target(v2v1, v1, pm);
set_next(v1v2,v2v1, pm);
set_next(v2v1,v1v2, pm);
return v1v2;
};
// used to collect existing border halfedges that will no longer be on the border.
// Some update is needed in case of non-manifold vertex at the source/target of those
// edges are present.
std::vector<halfedge_descriptor> former_border_hedges;
std::vector<Halfedges> outgoing_hedges(num_vertices(pm));
for (const Vertex_range& vr : faces_to_add)
{
std::size_t nbh=vr.size();
for (std::size_t i=0; i<nbh; ++i)
{
vertex_descriptor v1=vr[i], v2=vr[(i+1)%nbh];
std::pair<edge_descriptor, bool> edge_and_bool = edge(v1, v2, pm);
if (v2<v1){
// needed in case an existing border edge won't be found
// because the outgoing edge from the smallest vertex is on the patch boundary
if (edge_and_bool.second && is_border(halfedge(edge_and_bool.first, pm), pm))
{
outgoing_hedges[get(vid,v2)].push_back(opposite(halfedge(edge_and_bool.first, pm), pm));
former_border_hedges.push_back(halfedge(edge_and_bool.first, pm));
}
continue;
}
if (edge_and_bool.second)
{
halfedge_descriptor h = halfedge(edge_and_bool.first, pm);
outgoing_hedges[get(vid,v1)].push_back(h);
if (is_border(h, pm))
former_border_hedges.push_back(h);
}
else
outgoing_hedges[get(vid,v1)].push_back(add_new_edge(v1,v2));
CGAL_assertion( source(outgoing_hedges[get(vid,v1)].back(), pm)==v1 );
CGAL_assertion( target(outgoing_hedges[get(vid,v1)].back(), pm)==v2 );
}
}
// disconnect hand-fans (umbrellas being not affected) at non-manifold vertices
// in case the location on the boundary of the mesh where they are attached is closed.
// Note that we link the boundary of the hand fans together, making them
// independant boundary cycles (even if the non-manifold vertex is not duplicated)
if ( !former_border_hedges.empty() )
{
std::sort(former_border_hedges.begin(), former_border_hedges.end()); // TODO: is it better to use a dynamic pmap?
for (halfedge_descriptor h : former_border_hedges)
{
// update link around target vertex
halfedge_descriptor nh = next(h, pm);
if ( !std::binary_search(former_border_hedges.begin(), former_border_hedges.end(), nh) )
{
do
{
// look for a new prev for h
halfedge_descriptor candidate = opposite(next(opposite(nh, pm), pm), pm);
while (!is_border(candidate, pm))
candidate = opposite(next(candidate, pm), pm);
halfedge_descriptor for_next_iteration = next(candidate, pm);
set_next(candidate, nh, pm);
nh = for_next_iteration;
if (candidate==h) break; // stop condition for a vertex that will stay on the boundary after the operation
if ( std::binary_search(former_border_hedges.begin(), former_border_hedges.end(), nh) )
{
// linking halfedges that will no longer be on the boundary
set_next(h, nh, pm);
break;
}
}
while(true);
}
// update link around source vertex
halfedge_descriptor ph = prev(h, pm);
if ( !std::binary_search(former_border_hedges.begin(), former_border_hedges.end(), ph) )
{
do
{
// look for a new next for h
halfedge_descriptor candidate = opposite(prev(opposite(ph, pm), pm), pm);
while (!is_border(candidate, pm))
candidate = opposite(prev(candidate, pm), pm);