impl_low_level_api_mutable.hh 22.7 KB
Newer Older
Philip Trettner's avatar
Philip Trettner committed
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
#pragma once

#include "../Mesh.hh"

namespace polymesh
{
inline vertex_index low_level_api_mutable::add_vertex() const { return alloc_vertex(); }

inline vertex_index low_level_api_mutable::alloc_vertex() const { return m.alloc_vertex(); }
inline face_index low_level_api_mutable::alloc_face() const { return m.alloc_face(); }
inline edge_index low_level_api_mutable::alloc_edge() const { return m.alloc_edge(); }
inline void low_level_api_mutable::alloc_primitives(int vertices, int faces, int halfedges) const { m.alloc_primitives(vertices, faces, halfedges); }

inline void low_level_api_mutable::permute_faces(const std::vector<int> &p) const { m.permute_faces(p); }
inline void low_level_api_mutable::permute_edges(const std::vector<int> &p) const { m.permute_edges(p); }
inline void low_level_api_mutable::permute_vertices(const std::vector<int> &p) const { m.permute_vertices(p); }

inline face_index low_level_api_mutable::add_face(const vertex_handle *v_handles, int vcnt) const
{
    m.mFaceInsertCache.resize(vcnt);
    for (auto i = 0; i < vcnt; ++i)
        m.mFaceInsertCache[i] = add_or_get_halfedge(v_handles[i].idx, v_handles[(i + 1) % vcnt].idx);
    return add_face(m.mFaceInsertCache.data(), vcnt);
}

inline face_index low_level_api_mutable::add_face(const vertex_index *v_indices, int vcnt) const
{
    m.mFaceInsertCache.resize(vcnt);
    for (auto i = 0; i < vcnt; ++i)
        m.mFaceInsertCache[i] = add_or_get_halfedge(v_indices[i], v_indices[(i + 1) % vcnt]);
    return add_face(m.mFaceInsertCache.data(), vcnt);
}

inline face_index low_level_api_mutable::add_face(const halfedge_handle *half_loop, int vcnt) const
{
    m.mFaceInsertCache.resize(vcnt);
    for (auto i = 0; i < vcnt; ++i)
        m.mFaceInsertCache[i] = half_loop[i].idx;
    return add_face(m.mFaceInsertCache.data(), vcnt);
}

inline face_index low_level_api_mutable::add_face(const halfedge_index *half_loop, int vcnt) const
{
    assert(vcnt >= 3 && "no support for less-than-triangular faces");

    auto fidx = alloc_face();

    // ensure that half-edges are adjacent at each vertex
    for (auto i = 0; i < vcnt; ++i)
    {
        auto h0 = half_loop[i];
        auto h1 = half_loop[(i + 1) % vcnt];

        // half-edge must form a chain
        assert(to_vertex_of(h0) == from_vertex_of(h1) && "half-edges do not form a chain");
        // half-edge must be free, i.e. allow a new polygon
        assert(is_free(h0) && "half-edge already contains a face");

        // make them adjacent
        make_adjacent(h0, h1);

        // link face
        face_of(h0) = fidx;
    }

    // fix boundary states
    for (auto i = 0; i < vcnt; ++i)
    {
        auto h = half_loop[i];
        auto v = to_vertex_of(h);
        auto f = opposite_face_of(h);

        // fix vertex
        fix_boundary_state_of(v);

        // fix face
        if (f.is_valid())
            fix_boundary_state_of(f);
    }

    // set up face data
    halfedge_of(fidx) = half_loop[0];

    // fix new face
    fix_boundary_state_of(fidx);

    return fidx;
}

inline edge_index low_level_api_mutable::add_or_get_edge(vertex_index v_from, vertex_index v_to) const
{
    assert(v_from != v_to);

    // already exists?
    auto he = find_halfedge(v_from, v_to);
    if (he.is_valid())
        return edge_of(he);

    // allocate new
    auto e = alloc_edge();
    auto h_from_to = halfedge_of(e, 0);
    auto h_to_from = halfedge_of(e, 1);

    // setup data (self-connected edge)
    to_vertex_of(h_from_to) = v_to;
    to_vertex_of(h_to_from) = v_from;
    next_halfedge_of(h_from_to) = h_to_from;
    next_halfedge_of(h_to_from) = h_from_to;

    // link from vertex
    if (is_isolated(v_from))
        outgoing_halfedge_of(v_from) = h_from_to;
    else
    {
        auto from_in = find_free_incident(v_from);
        assert(from_in.is_valid() && "vertex is already fully connected");

        auto from_out = next_halfedge_of(from_in);

        connect_prev_next(from_in, h_from_to);
        connect_prev_next(h_to_from, from_out);
    }

    // link to vertex
    if (is_isolated(v_to))
        outgoing_halfedge_of(v_to) = h_to_from;
    else
    {
        auto to_in = find_free_incident(v_to);
        assert(to_in.is_valid() && "vertex is already fully connected");

        auto to_out = next_halfedge_of(to_in);

        connect_prev_next(to_in, h_to_from);
        connect_prev_next(h_from_to, to_out);
    }

    return e;
}

inline halfedge_index low_level_api_mutable::add_or_get_halfedge(vertex_index v_from, vertex_index v_to) const
{
    auto e = add_or_get_edge(v_from, v_to);
    auto h0 = halfedge_of(e, 0);
    auto h1 = halfedge_of(e, 1);
    return to_vertex_of(h0) == v_to ? h0 : h1;
}

inline edge_index low_level_api_mutable::add_or_get_edge(halfedge_index h_from, halfedge_index h_to) const
{
    assert(h_from != h_to);

    auto v_from = to_vertex_of(h_from);
    auto v_to = to_vertex_of(h_to);

    auto ex_he = find_halfedge(v_from, v_to);
    if (ex_he.is_valid())
    {
        assert(prev_halfedge_of(ex_he) == h_from && prev_halfedge_of(opposite(ex_he)) == h_to);

        // TODO: Maybe try rewriting an existing halfedge that does NOT yet have the right connection.
        return edge_of(ex_he);
    }

    assert(is_free(h_from) && is_free(h_to) && "Cannot insert into a face");

    // allocate new
    auto e = alloc_edge();
    auto h_from_to = halfedge_of(e, 0);
    auto h_to_from = halfedge_of(e, 1);

    // setup data (self-connected edge)
    to_vertex_of(h_from_to) = v_to;
    to_vertex_of(h_to_from) = v_from;

    // Link from side
    auto h_from_next = next_halfedge_of(h_from);

    connect_prev_next(h_from, h_from_to);
180
    connect_prev_next(h_to_from, h_from_next);
Philip Trettner's avatar
Philip Trettner committed
181
182
183
184
185

    // Link to side
    auto h_to_next = next_halfedge_of(h_to);

    connect_prev_next(h_to, h_to_from);
186
    connect_prev_next(h_from_to, h_to_next);
Philip Trettner's avatar
Philip Trettner committed
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

    return e;
}

inline halfedge_index low_level_api_mutable::add_or_get_halfedge(halfedge_index h_from, halfedge_index h_to) const
{
    auto e = add_or_get_edge(h_from, h_to);
    auto h0 = halfedge_of(e, 0);
    auto h1 = halfedge_of(e, 1);
    return next_halfedge_of(h_from) == h0 ? h0 : h1;
}

inline void low_level_api_mutable::make_adjacent(halfedge_index he_in, halfedge_index he_out) const
{
    // see http://kaba.hilvi.org/homepage/blog/halfedge/halfedge.htm ::makeAdjacent

    auto he_b = next_halfedge_of(he_in);
    auto he_d = prev_halfedge_of(he_out);

    // already correct
    if (he_b == he_out)
        return;

    // find free half-edge after `out` but before `in`
    auto he_g = find_free_incident(opposite(he_out), he_in);
    assert(he_g.is_valid()); // unable to make adjacent

    auto he_h = next_halfedge_of(he_g);

    // properly rewire
    connect_prev_next(he_in, he_out);
    connect_prev_next(he_g, he_b);
    connect_prev_next(he_d, he_h);
}

inline void low_level_api_mutable::remove_face(face_index f_idx) const
{
    assert(!is_removed(f_idx));

    auto he_begin = halfedge_of(f_idx);
    auto he = he_begin;
    do
    {
        assert(face_of(he) == f_idx);

        // set half-edge face to invalid
        face_of(he) = face_index::invalid();

        // fix outgoing vertex half-edge
        // (vertex correctly reports is_boundary)
        outgoing_halfedge_of(from_vertex_of(he)) = he;

        // fix opposite face half-edge
        auto ohe = opposite(he);
        auto of = face_of(ohe);
        if (of.is_valid())
            halfedge_of(of) = ohe;

        // advance
        he = next_halfedge_of(he);
    } while (he != he_begin);

    // mark removed
    // (at the end!)
    set_removed(f_idx);
}

inline void low_level_api_mutable::remove_edge(edge_index e_idx) const
{
    auto h_in = halfedge_of(e_idx, 0);
    auto h_out = halfedge_of(e_idx, 1);

    assert(!is_removed(h_in));
    assert(!is_removed(h_out));

    auto f0 = face_of(h_in);
    auto f1 = face_of(h_out);

    // remove adjacent faces
    if (f0.is_valid())
        remove_face(f0);
    if (f1.is_valid())
        remove_face(f1);

    // rewire vertices
    auto v_in_to = to_vertex_of(h_in);
    auto v_out_to = to_vertex_of(h_out);

    auto hi_out_prev = prev_halfedge_of(h_out);
    auto hi_out_next = next_halfedge_of(h_out);

    auto hi_in_prev = prev_halfedge_of(h_in);
    auto hi_in_next = next_halfedge_of(h_in);

    // modify vertex if outgoing half-edge is going to be removed
    auto &v_in_to_out = outgoing_halfedge_of(v_in_to);
    if (v_in_to_out == h_out)
    {
        if (hi_in_next == h_out) // v_in_to becomes isolated
            v_in_to_out = halfedge_index::invalid();
        else
            v_in_to_out = hi_in_next;
    }

    auto &v_out_to_out = outgoing_halfedge_of(v_out_to);
    if (v_out_to_out == h_in)
    {
        if (hi_out_next == h_in) // v_out_to becomes isolated
            v_out_to_out = halfedge_index::invalid();
        else
            v_out_to_out = hi_out_next;
    }

    // reqire half-edges
    connect_prev_next(hi_out_prev, hi_in_next);
    connect_prev_next(hi_in_prev, hi_out_next);

    // remove half-edges
    set_removed(e_idx);
}

inline void low_level_api_mutable::remove_vertex(vertex_index v_idx) const
{
    assert(!is_removed(v_idx));

    // remove all outgoing edges
    while (!is_isolated(v_idx))
        remove_edge(edge_of(outgoing_halfedge_of(v_idx)));

    // mark removed
    set_removed(v_idx);
}

inline void low_level_api_mutable::fix_boundary_state_of(vertex_index v_idx) const
{
    assert(!is_isolated(v_idx));

    auto he_begin = outgoing_halfedge_of(v_idx);
    auto he = he_begin;
    do
    {
        // if half-edge is boundary, set it
        if (is_free(he))
        {
            outgoing_halfedge_of(v_idx) = he;
            return;
        }

        // advance
        he = next_halfedge_of(opposite(he));
    } while (he != he_begin);
}

inline void low_level_api_mutable::fix_boundary_state_of(face_index f_idx) const
{
    auto he_begin = halfedge_of(f_idx);
    auto he = he_begin;
    do
    {
        // if half-edge is boundary, set it
        if (is_free(opposite(he)))
        {
            halfedge_of(f_idx) = he;
            return;
        }

        // advance
        he = next_halfedge_of(he);
    } while (he != he_begin);
}

inline void low_level_api_mutable::fix_boundary_state_of_vertices(face_index f_idx) const
{
    auto he_begin = halfedge_of(f_idx);
    auto he = he_begin;
    do
    {
        // fix vertex
        fix_boundary_state_of(to_vertex_of(he));

        // advance
        he = next_halfedge_of(he);
    } while (he != he_begin);
}
371
372
373
374

inline void low_level_api_mutable::set_removed(vertex_index idx) const
{
    assert(!is_removed(idx) && "cannot remove an already removed entry");
Philip Trettner's avatar
Philip Trettner committed
375
    outgoing_halfedge_of(idx).value = -2;
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391

    // bookkeeping
    m.mRemovedVertices++;
    m.mCompact = false;
}

inline void low_level_api_mutable::set_removed(face_index idx) const
{
    assert(!is_removed(idx) && "cannot remove an already removed entry");
    halfedge_of(idx) = halfedge_index::invalid();

    // bookkeeping
    m.mRemovedFaces++;
    m.mCompact = false;
}

Philip Trettner's avatar
Philip Trettner committed
392
393
inline void low_level_api_mutable::set_removed(edge_index idx) const
{
394
    assert(!is_removed(idx) && "cannot remove an already removed entry");
Philip Trettner's avatar
Philip Trettner committed
395
396
    to_vertex_of(halfedge_of(idx, 0)) = vertex_index::invalid();
    to_vertex_of(halfedge_of(idx, 1)) = vertex_index::invalid();
397
398
399
400
401
402
403
404
405
406
407
408
409

    // bookkeeping
    m.mRemovedHalfedges++;
    m.mRemovedHalfedges++;
    m.mCompact = false;
}

inline void low_level_api_mutable::set_removed_counts(int r_vertices, int r_faces, int r_edges)
{
    m.mRemovedVertices = r_vertices;
    m.mRemovedFaces = r_faces;
    m.mRemovedHalfedges = r_edges * 2;
    m.mCompact = r_vertices == 0 && r_faces == 0 && r_edges == 0;
Philip Trettner's avatar
Philip Trettner committed
410
411
412
413
414
415
416
417
418
419
}

inline void low_level_api_mutable::connect_prev_next(halfedge_index prev, halfedge_index next) const
{
    next_halfedge_of(prev) = next;
    prev_halfedge_of(next) = prev;
}

inline vertex_index low_level_api_mutable::face_split(face_index f) const
{
Philip Trettner's avatar
Philip Trettner committed
420
421
422
423
424
425
426
427
    auto v = add_vertex();
    face_split(f, v);
    return v;
}

inline void low_level_api_mutable::face_split(face_index f, vertex_index v) const
{
    assert(is_isolated(v));
Philip Trettner's avatar
Philip Trettner committed
428
429
430
431
432
433
434
435
436
    // TODO: can be made more performant

    auto h_begin = halfedge_of(f);

    // remove face
    remove_face(f);

    // add vertex
    vertex_index vs[3];
Philip Trettner's avatar
Philip Trettner committed
437
    vs[0] = v;
Philip Trettner's avatar
Philip Trettner committed
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454

    // add triangles
    auto h = h_begin;
    do
    {
        vs[1] = from_vertex_of(h);
        vs[2] = to_vertex_of(h);

        add_face(vs, 3);

        // NOTE: add_face inserted a new halfedge
        h = next_halfedge_of(opposite(next_halfedge_of(h)));
    } while (h != h_begin);
}

inline vertex_index low_level_api_mutable::edge_split(edge_index e) const
{
Philip Trettner's avatar
Philip Trettner committed
455
456
457
458
459
460
461
462
463
    auto v = add_vertex();
    edge_split(e, v);
    return v;
}

inline void low_level_api_mutable::edge_split(edge_index e, vertex_index v) const
{
    assert(is_isolated(v));

Philip Trettner's avatar
Philip Trettner committed
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
    auto h0 = halfedge_of(e, 0);
    auto h1 = halfedge_of(e, 1);

    auto v0 = to_vertex_of(h0);
    auto v1 = to_vertex_of(h1);
    auto f0 = face_of(h0);
    auto f1 = face_of(h1);

    // add two new edges
    auto e1 = alloc_edge();
    auto e2 = alloc_edge();
    auto e1h0 = halfedge_of(e1, 0);
    auto e1h1 = halfedge_of(e1, 1);
    auto e2h0 = halfedge_of(e2, 0);
    auto e2h1 = halfedge_of(e2, 1);

    // rewire edges
    auto h0_prev = prev_halfedge_of(h0);
    auto h0_next = next_halfedge_of(h0);
    auto h1_prev = prev_halfedge_of(h1);
    auto h1_next = next_halfedge_of(h1);

    face_of(e1h0) = f0;
    face_of(e2h0) = f0;
    face_of(e1h1) = f1;
    face_of(e2h1) = f1;

    to_vertex_of(e1h0) = v0;
    to_vertex_of(e2h0) = v;
    to_vertex_of(e1h1) = v;
    to_vertex_of(e2h1) = v1;

    connect_prev_next(h0_prev, e2h0);
    connect_prev_next(e2h0, e1h0);
    connect_prev_next(e1h0, h0_next);

    connect_prev_next(h1_prev, e1h1);
    connect_prev_next(e1h1, e2h1);
    connect_prev_next(e2h1, h1_next);

    // rewire vertices
    auto &v0_out = outgoing_halfedge_of(v0);
    auto &v1_out = outgoing_halfedge_of(v1);
    if (v0_out == h1)
        v0_out = e1h1;
    if (v1_out == h0)
        v1_out = e2h0;

512
513
    outgoing_halfedge_of(v) = is_boundary(e1h0) ? e1h0 : e2h1;

Philip Trettner's avatar
Philip Trettner committed
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
    // rewire faces
    if (f0.is_valid())
    {
        auto &f0_h = halfedge_of(f0);
        if (f0_h == h0)
            f0_h = e1h0;
    }
    if (f1.is_valid())
    {
        auto &f1_h = halfedge_of(f1);
        if (f1_h == h1)
            f1_h = e2h1;
    }

    // remove edge
    set_removed(e);
}

inline vertex_index low_level_api_mutable::halfedge_split(halfedge_index h) const
{
    auto v = add_vertex();
Philip Trettner's avatar
Philip Trettner committed
535
536
537
538
539
540
541
    halfedge_split(h, v);
    return v;
}

inline void low_level_api_mutable::halfedge_split(halfedge_index h, vertex_index v) const
{
    // add edge
Philip Trettner's avatar
Philip Trettner committed
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
    auto e = alloc_edge();

    auto h0 = h;
    auto h1 = opposite(h);
    auto h2 = halfedge_of(e, 0);
    auto h3 = halfedge_of(e, 1);

    auto v0 = to_vertex_of(h0);
    auto v1 = to_vertex_of(h1);

    // rewire edges
    auto h0_next = next_halfedge_of(h0);
    auto h1_prev = prev_halfedge_of(h1);

    auto f0 = face_of(h0);
    auto f1 = face_of(h1);

    face_of(h2) = f0;
    face_of(h3) = f1;

    to_vertex_of(h0) = v;
    to_vertex_of(h1) = v1; //< already there
    to_vertex_of(h2) = v0;
    to_vertex_of(h3) = v;

    connect_prev_next(h0, h2);
    connect_prev_next(h2, h0_next);

    connect_prev_next(h1_prev, h3);
    connect_prev_next(h3, h1);

    // rewire vertices
    auto &v0_out = outgoing_halfedge_of(v0);
    if (v0_out == h1)
        v0_out = h3;

578
    outgoing_halfedge_of(v) = is_boundary(h1) ? h1 : h2; // boundary
Philip Trettner's avatar
Philip Trettner committed
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

    // rewire faces
    // -> already ok
}

inline face_index low_level_api_mutable::face_fill(halfedge_index h) const
{
    assert(is_boundary(h));

    auto f = alloc_face();

    halfedge_of(f) = h;

    auto h_begin = h;
    do
    {
        // set face
        face_of(h) = f;

        // fix face boundary
        if (is_boundary(opposite(h)))
            halfedge_of(f) = h;

        // fix adj face boundary
        auto adj_face = opposite_face_of(h);
        if (adj_face.is_valid())
            fix_boundary_state_of(adj_face);

        // advance
        h = next_halfedge_of(h);
    } while (h != h_begin);

    // fix vertex boundaries
    fix_boundary_state_of_vertices(f);

    return f;
}

inline void low_level_api_mutable::halfedge_attach(halfedge_index h, vertex_index v) const
{
    assert(is_isolated(v));

    auto h_next = next_halfedge_of(h);
    auto v_to = to_vertex_of(h);

    auto f = face_of(h);

    auto e = alloc_edge();
    auto h0 = halfedge_of(e, 0);
    auto h1 = halfedge_of(e, 1);

    face_of(h0) = f;
    to_vertex_of(h0) = v;

    face_of(h1) = f;
    to_vertex_of(h1) = v_to;

    outgoing_halfedge_of(v) = h1;

    connect_prev_next(h, h0);
    connect_prev_next(h0, h1);
    connect_prev_next(h1, h_next);
}

inline void low_level_api_mutable::vertex_collapse(vertex_index v) const
{
    // isolated vertices are just removed
    if (is_isolated(v))
    {
        remove_vertex(v);
    }
    // boundary vertices are special
    else if (is_boundary(v))
    {
        assert(0 && "not implemented");
    }
    else // interior vertex
    {
        auto h_begin = next_halfedge_of(outgoing_halfedge_of(v));

        remove_vertex(v);

        assert(is_boundary(h_begin));

        // TODO: optimize
        std::vector<halfedge_index> hs;
        auto h = h_begin;
        do
        {
            // add half-edge ring
            hs.push_back(h);

            // advance
            h = next_halfedge_of(h);
        } while (h != h_begin);

        // add face
        add_face(hs.data(), (int)hs.size());
    }
}

inline void low_level_api_mutable::halfedge_collapse(halfedge_index h) const
{
    // TODO: collapse half-edge
    // preserve adjacent non-triangles

    assert(0 && "not implemented");
}

inline void low_level_api_mutable::edge_rotate_next(edge_index e) const
{
    assert(!is_boundary(e) && "does not work on boundaries");
    assert(m.handle_of(e).vertexA().adjacent_vertices().size() > 2 && "does not work on valence <= 2 vertices");
    assert(m.handle_of(e).vertexB().adjacent_vertices().size() > 2 && "does not work on valence <= 2 vertices");

    auto h0 = halfedge_of(e, 0);
    auto h1 = halfedge_of(e, 1);

    auto h0_next = next_halfedge_of(h0);
    auto h0_prev = prev_halfedge_of(h0);
    auto h1_next = next_halfedge_of(h1);
    auto h1_prev = prev_halfedge_of(h1);

    auto h0_next_next = next_halfedge_of(h0_next);
    auto h1_next_next = next_halfedge_of(h1_next);

    // fix vertices
    auto &v0_out = outgoing_halfedge_of(to_vertex_of(h0));
    if (v0_out == h1)
        v0_out = h0_next;
    auto &v1_out = outgoing_halfedge_of(to_vertex_of(h1));
    if (v1_out == h0)
        v1_out = h1_next;

    // fix faces
    halfedge_of(face_of(h0)) = h0;
    halfedge_of(face_of(h1)) = h1;

    // fix half-edges
    to_vertex_of(h0) = to_vertex_of(h0_next);
    to_vertex_of(h1) = to_vertex_of(h1_next);
    face_of(h0_next) = face_of(h1);
    face_of(h1_next) = face_of(h0);

    // move to next
    connect_prev_next(h1_prev, h0_next);
    connect_prev_next(h0_prev, h1_next);

    connect_prev_next(h0_next, h1);
    connect_prev_next(h1_next, h0);

    connect_prev_next(h0, h0_next_next);
    connect_prev_next(h1, h1_next_next);

    // fix boundary state
    fix_boundary_state_of(face_of(h0));
    fix_boundary_state_of(face_of(h1));
}

inline void low_level_api_mutable::edge_rotate_prev(edge_index e) const
{
    assert(!is_boundary(e) && "does not work on boundaries");
    assert(m.handle_of(e).vertexA().adjacent_vertices().size() > 2 && "does not work on valence <= 2 vertices");
    assert(m.handle_of(e).vertexB().adjacent_vertices().size() > 2 && "does not work on valence <= 2 vertices");

    auto h0 = halfedge_of(e, 0);
    auto h1 = halfedge_of(e, 1);

    auto h0_next = next_halfedge_of(h0);
    auto h0_prev = prev_halfedge_of(h0);
    auto h1_next = next_halfedge_of(h1);
    auto h1_prev = prev_halfedge_of(h1);

    auto h0_prev_prev = prev_halfedge_of(h0_prev);
    auto h1_prev_prev = prev_halfedge_of(h1_prev);

    // fix vertex
    auto &v0_out = outgoing_halfedge_of(to_vertex_of(h0));
    if (v0_out == h1)
        v0_out = h0_next;
    auto &v1_out = outgoing_halfedge_of(to_vertex_of(h1));
    if (v1_out == h0)
        v1_out = h1_next;

    // fix faces
    halfedge_of(face_of(h0)) = h0;
    halfedge_of(face_of(h1)) = h1;

    // fix half-edge
    to_vertex_of(h1) = to_vertex_of(h0_prev_prev);
    to_vertex_of(h0) = to_vertex_of(h1_prev_prev);
    face_of(h0_prev) = face_of(h1);
    face_of(h1_prev) = face_of(h0);

    // move to next
    connect_prev_next(h0_prev, h1_next);
    connect_prev_next(h1_prev, h0_next);

    connect_prev_next(h1, h0_prev);
    connect_prev_next(h0, h1_prev);

    connect_prev_next(h0_prev_prev, h0);
    connect_prev_next(h1_prev_prev, h1);

    // fix boundary state
    fix_boundary_state_of(face_of(h0));
    fix_boundary_state_of(face_of(h1));
}

inline void low_level_api_mutable::halfedge_rotate_next(halfedge_index h) const
{
    assert(m.handle_of(h).next().next().next() != h && "does not work for triangles");
    assert(!m.handle_of(h).edge().is_boundary() && "does not work on boundaries");
    assert(m.handle_of(h).vertex_to().adjacent_vertices().size() > 2 && "does not work on valence <= 2 vertices");

    auto h0 = h;
    auto h1 = opposite(h);

    auto h0_next = next_halfedge_of(h0);
    auto h1_prev = prev_halfedge_of(h1);
    auto h0_next_next = next_halfedge_of(h0_next);

    // fix vertex
    auto &v_out = outgoing_halfedge_of(to_vertex_of(h0));
    if (v_out == h1)
        v_out = h0_next;

    // fix faces
    halfedge_of(face_of(h0)) = h0;
    halfedge_of(face_of(h1)) = h1;

    // fix half-edges
    to_vertex_of(h0) = to_vertex_of(h0_next);
    face_of(h0_next) = face_of(h1);

    // move to next
    connect_prev_next(h1_prev, h0_next);
    connect_prev_next(h0_next, h1);
    connect_prev_next(h0, h0_next_next);

    // fix boundary state
    fix_boundary_state_of(face_of(h0));
    fix_boundary_state_of(face_of(h1));
}

inline void low_level_api_mutable::halfedge_rotate_prev(halfedge_index h) const
{
    assert(m.handle_of(h).prev().prev().prev() != h && "does not work for triangles");
    assert(!m.handle_of(h).edge().is_boundary() && "does not work on boundaries");
    assert(m.handle_of(h).vertex_from().adjacent_vertices().size() > 2 && "does not work on valence <= 2 vertices");

    auto h0 = h;
    auto h1 = opposite(h);

    auto h0_prev = prev_halfedge_of(h0);
    auto h1_next = next_halfedge_of(h1);
    auto h0_prev_prev = prev_halfedge_of(h0_prev);

    // fix vertex
    auto &v_out = outgoing_halfedge_of(to_vertex_of(h1));
    if (v_out == h0)
        v_out = h1_next;

    // fix faces
    halfedge_of(face_of(h0)) = h0;
    halfedge_of(face_of(h1)) = h1;

    // fix half-edge
    to_vertex_of(h1) = to_vertex_of(h0_prev_prev);
    face_of(h0_prev) = face_of(h1);

    // move to next
    connect_prev_next(h0_prev, h1_next);
    connect_prev_next(h1, h0_prev);
    connect_prev_next(h0_prev_prev, h0);

    // fix boundary state
    fix_boundary_state_of(face_of(h0));
    fix_boundary_state_of(face_of(h1));
}
}