-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEuler_operations.h
More file actions
1481 lines (1312 loc) · 49 KB
/
Euler_operations.h
File metadata and controls
1481 lines (1312 loc) · 49 KB
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); you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 3 of the License,
// or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL$
// $Id$
// SPDX-License-Identifier: LGPL-3.0+
//
//
// Author(s) : Philipp Moeller
#ifndef CGAL_EULER_OPERATIONS_H
#define CGAL_EULER_OPERATIONS_H
#include <stdexcept>
#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>
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;
}
/**
* 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();
// don't allow degenerated faces
CGAL_assertion(n > 2);
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 std::vector<NextCacheEntry> 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
if (halfedge(v, g) == boost::graph_traits<Graph>::null_halfedge())
{
set_halfedge(v, outer_prev, g);
next_cache.push_back(NextCacheEntry(outer_prev, outer_next));
}
else
{
border_prev = halfedge(v, g);
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;
}
/**
* removes the incident face of `h` and changes all halfedges incident to the face into border halfedges. See `remove_face(g,h)` for a more generalized variant.
*
* \pre None of the incident edges of the face is a border edge.
*/
template< typename Graph>
void make_hole(typename boost::graph_traits<Graph>::halfedge_descriptor h,
Graph& g)
{
typedef typename boost::graph_traits<Graph> Traits;
typedef typename Traits::face_descriptor face_descriptor;
typedef Halfedge_around_face_iterator<Graph> halfedge_around_face_iterator;
CGAL_precondition(! is_border(h,g));
face_descriptor fd = face(h, g);
halfedge_around_face_iterator hafib, hafie;
for(boost::tie(hafib, hafie) = halfedges_around_face(h, g);
hafib != hafie;
++hafib){
CGAL_assertion(! is_border(opposite(*hafib,g),g));
internal::set_border(*hafib, g);
}
remove_face(fd,g);
}
/** fills the hole incident to `h`.
* \pre `h` must be a border halfedge
*/
template< typename Graph>
void fill_hole(typename boost::graph_traits<Graph>::halfedge_descriptor h,
Graph& g)
{
typedef typename boost::graph_traits<Graph> Traits;
typedef typename Traits::face_descriptor face_descriptor;
typedef typename Traits::halfedge_descriptor halfedge_descriptor;
face_descriptor f = add_face(g);
CGAL_FOREACH(halfedge_descriptor hd, halfedges_around_face(h,g)){
set_face(hd, f,g);
}
set_halfedge(f,h,g);
}
/**
* creates a barycentric triangulation of the face incident to `h`. Creates a new
* vertex and connects it to each vertex incident to `h` and splits `face(h, g)`
* into triangular faces.
* `h` remains incident to
* the original face. The time complexity is linear in the size of the face.
*
* \image html add_center_vertex.svg
*
* \returns the halfedge `next(h, g)` after the
* operation, i.e., a halfedge pointing to the new vertex.
*
* Note that `add_center_vertex()` does not deal with properties of new vertices,
* halfedges, and faces.
* \pre `h` is not a border halfedge.
*
* \param g the graph
* \param h halfedge descriptor
* \tparam Graph must be a model of `MutableFaceGraph`
* \sa `remove_center_vertex()`
*
*/
template<typename Graph>
typename boost::graph_traits<Graph>::halfedge_descriptor
add_center_vertex(typename boost::graph_traits<Graph>::halfedge_descriptor h,
Graph& g)
{
typedef typename boost::graph_traits<Graph> Traits;
typedef typename Traits::vertex_descriptor vertex_descriptor;
typedef typename Traits::halfedge_descriptor halfedge_descriptor;
typedef typename Traits::face_descriptor face_descriptor;
halfedge_descriptor hnew = halfedge(add_edge(g),g);
vertex_descriptor vnew = add_vertex(g);
internal::close_tip(hnew, vnew, g);
internal::insert_tip(opposite(hnew, g), h, g);
set_face(hnew, face(h, g), g);
set_halfedge(face(h,g), h, g);
halfedge_descriptor h2 = next(opposite(hnew, g), g);
while ( next(h2, g) != hnew) {
halfedge_descriptor gnew = halfedge(add_edge(g),g);
internal::insert_tip( gnew, hnew, g);
internal::insert_tip( opposite(gnew,g), h2, g);
face_descriptor fnew = add_face(g);
set_face( h2, fnew, g);
set_face( gnew, fnew, g);
set_face( next(gnew,g), fnew, g);
set_halfedge(face(h2, g), h2, g);
h2 = next(opposite(gnew, g), g);
}
set_face(next(hnew,g), face(hnew,g), g);
internal::set_vertex_halfedge(hnew, g);
return hnew;
}
/**
* removes the vertex `target(h, g)` and all incident halfedges thereby merging all
* incident faces. The resulting face may not be triangulated.
* This function is the inverse operation of `add_center_vertex()`.
* The invariant `h == remove_center_vertex(add_center_vertex(h,g),g)`
* holds, if `h` is not a border halfedge.
*
* \image html remove_center_vertex.svg
*
* \tparam Graph must be a model of `MutableFaceGraph`
*
* \param g the graph
* \param h halfedge descriptor
*
* \returns `prev(h, g)`
*
* \pre None of the incident faces of `target(h,g)` is a
* hole. There are at least two distinct faces incident to the faces
* that are incident to `target(h,g)`. (This prevents the
* operation from collapsing a volume into two faces glued together
* with opposite orientations, such as would happen with any vertex of
* a tetrahedron.)
*
* \sa `add_center_vertex()`
*
*/
template<typename Graph>
typename boost::graph_traits<Graph>::halfedge_descriptor
remove_center_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;
// h points to the vertex that gets removed
halfedge_descriptor h2 = opposite(next(h, g), g);
halfedge_descriptor hret = prev(h, g);
while (h2 != h) {
halfedge_descriptor gprev = prev(h2, g);
internal::set_vertex_halfedge(gprev, g);
internal::remove_tip(gprev, g);
remove_face(face(h2, g), g);
halfedge_descriptor gnext = opposite(next(h2, g), g);
remove_edge(edge(h2,g), g);
h2 = gnext;
}
internal::set_vertex_halfedge(hret, g);
internal::remove_tip(hret, g);
remove_vertex(target(h, g), g);
remove_edge(edge(h, g), g);
internal::set_face_in_face_loop(hret, face(hret, g), g);
set_halfedge(face(hret, g), hret, g);
return hret;
}
/**
* appends a new face to the border halfedge `h2` by connecting
* the tip of `h2` with the tip of `h1` with two new halfedges and a new vertex
* and creating a new face that is incident to `h2`.
* Note that `add_vertex_and_face_to_border()` does not deal with properties of new
* vertices, halfedges, and faces.
*
* \image html add_vertex_and_face_to_border.svg
*
* \tparam Graph must be a model of `MutableFaceGraph`
*
* \returns the halfedge of the new edge that is incident to the new face
* and the new vertex.
*
* \pre `h1` and `h2` are border halfedges
* \pre `h1 != h2`,
* \pre `h1` and `h2` are on the same border.
*/
template<typename Graph>
typename boost::graph_traits<Graph>::halfedge_descriptor
add_vertex_and_face_to_border(typename boost::graph_traits<Graph>::halfedge_descriptor h1,
typename boost::graph_traits<Graph>::halfedge_descriptor h2,
Graph& g)
{
typename boost::graph_traits<Graph>::vertex_descriptor v = add_vertex(g);
typename boost::graph_traits<Graph>::face_descriptor f = add_face(g);
typename boost::graph_traits<Graph>::edge_descriptor e1 = add_edge(g);
typename boost::graph_traits<Graph>::edge_descriptor e2 = add_edge(g);
typename boost::graph_traits<Graph>::halfedge_descriptor he1= halfedge(e1, g);
typename boost::graph_traits<Graph>::halfedge_descriptor he2= halfedge(e2, g);
typename boost::graph_traits<Graph>::halfedge_descriptor ohe1= opposite(he1, g);
typename boost::graph_traits<Graph>::halfedge_descriptor ohe2= opposite(he2, g);
set_next(he1, next(h1,g),g);
set_next(h1,ohe1,g);
set_target(he1,target(h1,g),g);
set_target(ohe1,v,g);
set_next(he2,he1,g);
set_next(ohe1,ohe2,g);
set_target(he2,v,g);
set_halfedge(v,ohe1,g);
set_next(ohe2,next(h2,g),g);
set_target(ohe2,target(h2,g),g);
set_next(h2,he2,g);
internal::set_border(he1,g);
internal::set_border(he2,g);
CGAL::Halfedge_around_face_iterator<Graph> hafib,hafie;
for(boost::tie(hafib, hafie) = halfedges_around_face(ohe1, g);
hafib != hafie;
++hafib){
set_face(*hafib, f, g);
}
set_halfedge(f, ohe1, g);
return ohe2;
}
/**
* appends a new face incident to the border halfedge `h1` and `h2` by connecting the vertex `target(h2,g)`
* and the vertex `target(h1,g)` with a new halfedge, and filling this separated part of the hole
* with a new face, such that the new face is incident to `h2`.
*
* \image html add_face_to_border.svg
*
* \tparam Graph must be a model of `MutableFaceGraph`
*
* \returns the halfedge of the new edge that is incident to the new face.
*
* \pre `h1` and `h2` are border halfedges,
* \pre `h1 != h2`,
* \pre `next(h1,g) != h2`,
* \pre `h1` and `h2` are on the same border.
*/
template<typename Graph>
typename boost::graph_traits<Graph>::halfedge_descriptor
add_face_to_border(typename boost::graph_traits<Graph>::halfedge_descriptor h1,
typename boost::graph_traits<Graph>::halfedge_descriptor h2,
Graph& g)
{
CGAL_precondition(is_border(h1,g) == true);
CGAL_precondition(is_border(h2,g) == true);
CGAL_precondition(h1 != h2);
CGAL_precondition(next(h1, g) != h2);
typename boost::graph_traits<Graph>::face_descriptor f = add_face(g);
typename boost::graph_traits<Graph>::edge_descriptor e = add_edge(g);
typename boost::graph_traits<Graph>::halfedge_descriptor
newh= halfedge(e, g)
, newhop = opposite(newh, g);
set_next(newhop, next(h2, g), g);
set_next(h2, newh, g);
set_next(newh, next(h1, g), g);
set_next(h1, newhop, g);
set_target(newh, target(h1, g), g);
set_target(newhop, target(h2, g), g);