impl_mesh.hh 40 KB
Newer Older
Philip Trettner's avatar
Philip Trettner committed
1
2
3
4
#pragma once

#include "../Mesh.hh"

5
6
#include "../detail/permutation.hh"

Philip Trettner's avatar
Philip Trettner committed
7
8
namespace polymesh
{
Philip Trettner's avatar
Philip Trettner committed
9
10
11
inline vertex_index Mesh::add_vertex() { return alloc_vertex(); }

inline vertex_index Mesh::alloc_vertex()
Philip Trettner's avatar
Philip Trettner committed
12
{
Philip Trettner's avatar
Philip Trettner committed
13
14
    auto idx = vertex_index(size_all_vertices());
    mVertexToOutgoingHalfedge.push_back(halfedge_index::invalid());
Philip Trettner's avatar
Philip Trettner committed
15
16

    // notify attributes
Philip Trettner's avatar
Philip Trettner committed
17
    auto vCnt = size_all_vertices();
Philip Trettner's avatar
Philip Trettner committed
18
19
20
21
22
23
    for (auto p = mVertexAttrs; p; p = p->mNextAttribute)
        p->resize(vCnt, false);

    return idx;
}

Philip Trettner's avatar
Philip Trettner committed
24
25
inline face_index Mesh::alloc_face()
{
Philip Trettner's avatar
Philip Trettner committed
26
27
    auto idx = face_index(size_all_faces());
    mFaceToHalfedge.push_back(halfedge_index::invalid());
Philip Trettner's avatar
Philip Trettner committed
28
29

    // notify attributes
Philip Trettner's avatar
Philip Trettner committed
30
    auto fCnt = size_all_faces();
Philip Trettner's avatar
Philip Trettner committed
31
32
33
34
35
36
37
38
    for (auto p = mFaceAttrs; p; p = p->mNextAttribute)
        p->resize(fCnt, false);

    return idx;
}

inline edge_index Mesh::alloc_edge()
{
Philip Trettner's avatar
Philip Trettner committed
39
40
41
42
43
44
45
46
47
48
    auto idx = edge_index(size_all_edges());

    mHalfedgeToFace.push_back(face_index::invalid());
    mHalfedgeToFace.push_back(face_index::invalid());
    mHalfedgeToVertex.push_back(vertex_index::invalid());
    mHalfedgeToVertex.push_back(vertex_index::invalid());
    mHalfedgeToNextHalfedge.push_back(halfedge_index::invalid());
    mHalfedgeToNextHalfedge.push_back(halfedge_index::invalid());
    mHalfedgeToPrevHalfedge.push_back(halfedge_index::invalid());
    mHalfedgeToPrevHalfedge.push_back(halfedge_index::invalid());
Philip Trettner's avatar
Philip Trettner committed
49
50

    // notify attributes
Philip Trettner's avatar
Philip Trettner committed
51
52
    auto hCnt = size_all_halfedges();
    auto eCnt = size_all_edges();
Philip Trettner's avatar
Philip Trettner committed
53
54
55
56
57
58
59
60
    for (auto p = mEdgeAttrs; p; p = p->mNextAttribute)
        p->resize(eCnt, false);
    for (auto p = mHalfedgeAttrs; p; p = p->mNextAttribute)
        p->resize(hCnt, false);

    return idx;
}

Philip Trettner's avatar
Philip Trettner committed
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
inline face_index Mesh::add_face(const vertex_handle *v_handles, int vcnt)
{
    mFaceInsertCache.resize(vcnt);
    for (auto i = 0; i < vcnt; ++i)
        mFaceInsertCache[i] = add_or_get_halfedge(v_handles[i].idx, v_handles[(i + 1) % vcnt].idx);
    return add_face(mFaceInsertCache.data(), vcnt);
}

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

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

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

Philip Trettner's avatar
Philip Trettner committed
89
    auto fidx = alloc_face();
Philip Trettner's avatar
Philip Trettner committed
90
91
92
93
94
95
96
97
98
99

    // 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
Philip Trettner's avatar
Philip Trettner committed
100
        assert(is_free(h0) && "half-edge already contains a face");
Philip Trettner's avatar
Philip Trettner committed
101
102
103
104
105

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

        // link face
Philip Trettner's avatar
Philip Trettner committed
106
        face_of(h0) = fidx;
Philip Trettner's avatar
Philip Trettner committed
107
108
109
110
111
112
    }

    // fix boundary states
    for (auto i = 0; i < vcnt; ++i)
    {
        auto h = half_loop[i];
Philip Trettner's avatar
Philip Trettner committed
113
114
        auto v = to_vertex_of(h);
        auto f = face_of(opposite(h));
Philip Trettner's avatar
Philip Trettner committed
115
116
117
118
119
120
121
122
123
124

        // fix vertex
        fix_boundary_state_of(v);

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

    // set up face data
Philip Trettner's avatar
Philip Trettner committed
125
    halfedge_of(fidx) = half_loop[0];
Philip Trettner's avatar
Philip Trettner committed
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

    // fix new face
    fix_boundary_state_of(fidx);

    return fidx;
}

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

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

    auto &vd_from = vertex(v_from);
    auto &vd_to = vertex(v_to);

    // allocate new
    auto he_size = (int)mHalfedges.size();
    auto h_from_to_idx = halfedge_index(he_size + 0);
    auto h_to_from_idx = halfedge_index(he_size + 1);
    auto eidx = edge_index(he_size >> 1);
    halfedge_info h_from_to;
    halfedge_info h_to_from;

    // setup data (self-connected edge)
    h_from_to.to_vertex = v_to;
    h_to_from.to_vertex = v_from;
    h_from_to.next_halfedge = h_to_from_idx;
    h_to_from.next_halfedge = h_from_to_idx;

    // link from vertex
    if (vd_from.is_isolated())
        vd_from.outgoing_halfedge = h_from_to_idx;
    else
    {
        auto from_in_idx = find_free_incident(v_from);
        assert(from_in_idx.is_valid() && "vertex is already fully connected");
        auto &from_in = halfedge(from_in_idx);
        auto from_out_idx = from_in.next_halfedge;
        auto &from_out = halfedge(from_out_idx);

        from_in.next_halfedge = h_from_to_idx;
        h_from_to.prev_halfedge = from_in_idx;

        h_to_from.next_halfedge = from_out_idx;
        from_out.prev_halfedge = h_to_from_idx;
    }

    // link to vertex
    if (vd_to.is_isolated())
        vd_to.outgoing_halfedge = h_to_from_idx;
    else
    {
        auto to_in_idx = find_free_incident(v_to);
        assert(to_in_idx.is_valid() && "vertex is already fully connected");
        auto &to_in = halfedge(to_in_idx);
        auto to_out_idx = to_in.next_halfedge;
        auto &to_out = halfedge(to_out_idx);

        to_in.next_halfedge = h_to_from_idx;
        h_to_from.prev_halfedge = to_in_idx;

        h_from_to.next_halfedge = to_out_idx;
        to_out.prev_halfedge = h_from_to_idx;
    }

    // finalize
    mHalfedges.push_back(h_from_to);
    mHalfedges.push_back(h_to_from);

    // notify attributes
Philip Trettner's avatar
Philip Trettner committed
200
    auto hCnt = (int)mHalfedges.size();
Philip Trettner's avatar
Philip Trettner committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
    auto eCnt = hCnt >> 1;
    for (auto p = mHalfedgeAttrs; p; p = p->mNextAttribute)
        p->resize(hCnt, false);
    for (auto p = mEdgeAttrs; p; p = p->mNextAttribute)
        p->resize(eCnt, false);

    return eidx;
}

inline halfedge_index Mesh::add_or_get_halfedge(vertex_index v_from, vertex_index v_to)
{
    auto e = add_or_get_edge(v_from, v_to);
    auto h0 = halfedge_of(e, 0);
    auto h1 = halfedge_of(e, 1);
    return halfedge(h0).to_vertex == v_to ? h0 : h1;
}

Philip Trettner's avatar
Philip Trettner committed
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
inline edge_index Mesh::add_or_get_edge(halfedge_index h_from, halfedge_index h_to)
{
    assert(h_from != h_to);
    auto &hd_from = halfedge(h_from);
    auto &hd_to = halfedge(h_to);

    auto v_from = hd_from.to_vertex;
    auto v_to = hd_to.to_vertex;

    auto ex_he = find_halfedge(v_from, v_to);
    if (ex_he.is_valid())
    {
        auto &existing_hed = halfedge(ex_he);
        auto &ex_opp_hed = halfedge(opposite(ex_he));
        assert(existing_hed.prev_halfedge == h_from && ex_opp_hed.prev_halfedge == h_to);
        (void)existing_hed;
        (void)ex_opp_hed;
        // TODO: Maybe try rewriting an existing halfedge that does NOT yet have the right connection.
        return edge_of(ex_he);
    }

    assert(hd_from.face.is_invalid() && hd_to.face.is_invalid() && "Cannot insert into a face");

    // allocate new
    auto he_size = (int)mHalfedges.size();
    auto h_from_to_idx = halfedge_index(he_size + 0);
    auto h_to_from_idx = halfedge_index(he_size + 1);
    auto eidx = edge_index(he_size >> 1);
    halfedge_info h_from_to;
    halfedge_info h_to_from;

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

    // Link from side
    auto h_from_next_idx = hd_from.next_halfedge;
    auto &h_from_next = halfedge(h_from_next_idx);

    hd_from.next_halfedge = h_from_to_idx;
    h_from_to.prev_halfedge = h_from;

    h_from_next.prev_halfedge = h_to_from_idx;
    h_to_from.next_halfedge = h_from_next_idx;

    // Link to side
    auto h_to_next_idx = hd_to.next_halfedge;
    auto &h_to_next = halfedge(h_to_next_idx);

    hd_to.next_halfedge = h_to_from_idx;
    h_to_from.prev_halfedge = h_to;

    h_to_next.prev_halfedge = h_from_to_idx;
    h_from_to.next_halfedge = h_to_next_idx;

    // finalize
    mHalfedges.push_back(h_from_to);
    mHalfedges.push_back(h_to_from);

    // notify attributes
    auto hCnt = (int)mHalfedges.size();
    auto eCnt = hCnt >> 1;
    for (auto p = mHalfedgeAttrs; p; p = p->mNextAttribute)
        p->resize(hCnt, false);
    for (auto p = mEdgeAttrs; p; p = p->mNextAttribute)
        p->resize(eCnt, false);

    return eidx;
}

inline halfedge_index Mesh::add_or_get_halfedge(halfedge_index h_from, halfedge_index h_to)
{
    auto e = add_or_get_edge(h_from, h_to);
    auto h0 = halfedge_of(e, 0);
    auto h1 = halfedge_of(e, 1);
Philip Trettner's avatar
Philip Trettner committed
293
    return next_halfedge_of(h_from) == h0 ? h0 : h1;
Philip Trettner's avatar
Philip Trettner committed
294
295
}

Philip Trettner's avatar
Philip Trettner committed
296
297
298
299
inline void Mesh::make_adjacent(halfedge_index he_in, halfedge_index he_out)
{
    // see http://kaba.hilvi.org/homepage/blog/halfedge/halfedge.htm ::makeAdjacent

Philip Trettner's avatar
Philip Trettner committed
300
301
    auto he_b = next_halfedge_of(he_in);
    auto he_d = prev_halfedge_of(he_out);
Philip Trettner's avatar
Philip Trettner committed
302
303
304
305
306
307
308
309
310

    // 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

Philip Trettner's avatar
Philip Trettner committed
311
    auto he_h = next_halfedge_of(he_g);
Philip Trettner's avatar
Philip Trettner committed
312
313

    // properly rewire
Philip Trettner's avatar
Philip Trettner committed
314
315
316
    connect_prev_next(he_in, he_out);
    connect_prev_next(he_g, he_b);
    connect_prev_next(he_d, he_h);
Philip Trettner's avatar
Philip Trettner committed
317
318
319
320
}

inline void Mesh::remove_face(face_index f_idx)
{
Philip Trettner's avatar
Philip Trettner committed
321
    assert(!is_removed(f_idx));
Philip Trettner's avatar
Philip Trettner committed
322

Philip Trettner's avatar
Philip Trettner committed
323
    auto he_begin = halfedge_of(f_idx);
Philip Trettner's avatar
Philip Trettner committed
324
325
326
    auto he = he_begin;
    do
    {
Philip Trettner's avatar
Philip Trettner committed
327
        assert(face_of(h) == f_idx);
Philip Trettner's avatar
Philip Trettner committed
328
329

        // set half-edge face to invalid
Philip Trettner's avatar
Philip Trettner committed
330
        face_of(he) = face_index::invalid();
Philip Trettner's avatar
Philip Trettner committed
331
332
333

        // fix outgoing vertex half-edge
        // (vertex correctly reports is_boundary)
Philip Trettner's avatar
Philip Trettner committed
334
        outgoing_halfedge_of(from_vertex_of(he)) = he;
Philip Trettner's avatar
Philip Trettner committed
335
336
337

        // fix opposite face half-edge
        auto ohe = opposite(he);
Philip Trettner's avatar
Philip Trettner committed
338
        auto of = face_of(ohe);
Philip Trettner's avatar
Philip Trettner committed
339
        if (of.is_valid())
Philip Trettner's avatar
Philip Trettner committed
340
            halfedge_of(of) = ohe;
Philip Trettner's avatar
Philip Trettner committed
341
342

        // advance
Philip Trettner's avatar
Philip Trettner committed
343
        he = next_halfedge_of(he);
Philip Trettner's avatar
Philip Trettner committed
344
345
346
347
    } while (he != he_begin);

    // mark removed
    // (at the end!)
Philip Trettner's avatar
Philip Trettner committed
348
    set_removed(f_idx);
Philip Trettner's avatar
Philip Trettner committed
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

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

inline void Mesh::remove_edge(edge_index e_idx)
{
    auto hi_in = halfedge_of(e_idx, 0);
    auto hi_out = halfedge_of(e_idx, 1);

    auto &h_in = halfedge(hi_in);
    auto &h_out = halfedge(hi_out);

    assert(h_in.is_valid());
    assert(h_out.is_valid());

    auto f0 = h_in.face;
    auto f1 = h_out.face;

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

    // rewire vertices
    auto &v_in_to = vertex(h_in.to_vertex);
    auto &v_out_to = vertex(h_out.to_vertex);

    auto hi_out_prev = h_out.prev_halfedge;
    auto hi_out_next = h_out.next_halfedge;

    auto hi_in_prev = h_in.prev_halfedge;
    auto hi_in_next = h_in.next_halfedge;

    // modify vertex if outgoing half-edge is going to be removed
    if (v_in_to.outgoing_halfedge == hi_out)
    {
        if (hi_in_next == hi_out) // v_in_to becomes isolated
            v_in_to.outgoing_halfedge = halfedge_index::invalid();
        else
            v_in_to.outgoing_halfedge = hi_in_next;
    }

    if (v_out_to.outgoing_halfedge == hi_in)
    {
        if (hi_out_next == hi_in) // v_out_to becomes isolated
            v_out_to.outgoing_halfedge = halfedge_index::invalid();
        else
            v_out_to.outgoing_halfedge = hi_out_next;
    }

    // reqire half-edges
    halfedge(hi_out_prev).next_halfedge = hi_in_next;
    halfedge(hi_in_next).prev_halfedge = hi_out_prev;

    halfedge(hi_in_prev).next_halfedge = hi_out_next;
    halfedge(hi_out_next).prev_halfedge = hi_in_prev;

    // remove half-edges
    h_in.set_removed();
    h_out.set_removed();

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

inline void Mesh::remove_vertex(vertex_index v_idx)
{
Philip Trettner's avatar
Philip Trettner committed
421
    assert(!is_removed(v_idx));
Philip Trettner's avatar
Philip Trettner committed
422
423

    // remove all outgoing edges
Philip Trettner's avatar
Philip Trettner committed
424
425
    while (!is_isolated(v_idx))
        remove_edge(edge_of(outgoing_halfedge_of(v_idx)));
Philip Trettner's avatar
Philip Trettner committed
426
427

    // mark removed
Philip Trettner's avatar
Philip Trettner committed
428
    set_removed(v_idx);
Philip Trettner's avatar
Philip Trettner committed
429
430
431
432
433
434
435
436

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

inline void Mesh::fix_boundary_state_of(vertex_index v_idx)
{
Philip Trettner's avatar
Philip Trettner committed
437
    assert(!is_isolated(v_idx));
Philip Trettner's avatar
Philip Trettner committed
438

Philip Trettner's avatar
Philip Trettner committed
439
    auto he_begin = outgoing_halfedge_of(v_idx);
Philip Trettner's avatar
Philip Trettner committed
440
441
442
443
    auto he = he_begin;
    do
    {
        // if half-edge is boundary, set it
Philip Trettner's avatar
Philip Trettner committed
444
        if (is_free(he))
Philip Trettner's avatar
Philip Trettner committed
445
        {
Philip Trettner's avatar
Philip Trettner committed
446
            outgoing_halfedge_of(v_idx) = he;
Philip Trettner's avatar
Philip Trettner committed
447
448
449
450
            return;
        }

        // advance
Philip Trettner's avatar
Philip Trettner committed
451
        he = next_halfedge_of(opposite(he));
Philip Trettner's avatar
Philip Trettner committed
452
453
454
455
456
    } while (he != he_begin);
}

inline void Mesh::fix_boundary_state_of(face_index f_idx)
{
Philip Trettner's avatar
Philip Trettner committed
457
    auto he_begin = halfedge_of(f_idx);
Philip Trettner's avatar
Philip Trettner committed
458
459
460
461
    auto he = he_begin;
    do
    {
        // if half-edge is boundary, set it
Philip Trettner's avatar
Philip Trettner committed
462
        if (is_free(opposite(he)))
Philip Trettner's avatar
Philip Trettner committed
463
        {
Philip Trettner's avatar
Philip Trettner committed
464
            halfedge_of(f_idx) = he;
Philip Trettner's avatar
Philip Trettner committed
465
466
467
468
            return;
        }

        // advance
Philip Trettner's avatar
Philip Trettner committed
469
        he = next_halfedge_of(he);
Philip Trettner's avatar
Philip Trettner committed
470
471
472
    } while (he != he_begin);
}

473
474
inline void Mesh::fix_boundary_state_of_vertices(face_index f_idx)
{
Philip Trettner's avatar
Philip Trettner committed
475
    auto he_begin = halfedge_of(f_idx);
476
477
478
479
    auto he = he_begin;
    do
    {
        // fix vertex
Philip Trettner's avatar
Philip Trettner committed
480
        fix_boundary_state_of(to_vertex_of(he));
481
482

        // advance
Philip Trettner's avatar
Philip Trettner committed
483
        he = next_halfedge_of(he);
484
485
486
    } while (he != he_begin);
}

Philip Trettner's avatar
Philip Trettner committed
487
488
489
490
491
492
493
inline halfedge_index Mesh::find_free_incident(halfedge_index in_begin, halfedge_index in_end) const
{
    assert(halfedge(in_begin).to_vertex == halfedge(in_end).to_vertex);

    auto he = in_begin;
    do
    {
Philip Trettner's avatar
Philip Trettner committed
494
        assert(to_vertex_of(he) == to_vertex(in_end));
Philip Trettner's avatar
Philip Trettner committed
495
496

        // free? found one!
Philip Trettner's avatar
Philip Trettner committed
497
        if (is_free(he))
Philip Trettner's avatar
Philip Trettner committed
498
499
500
            return he;

        // next half-edge of vertex
Philip Trettner's avatar
Philip Trettner committed
501
        he = opposite(next_halfedge_of(he));
Philip Trettner's avatar
Philip Trettner committed
502
503
504
505
506
507
508
    } while (he != in_end);

    return halfedge_index::invalid();
}

inline halfedge_index Mesh::find_free_incident(vertex_index v) const
{
Philip Trettner's avatar
Philip Trettner committed
509
    auto in_begin = opposite(outgoing_halfedge_of(v));
Philip Trettner's avatar
Philip Trettner committed
510
511
512
513
514
    return find_free_incident(in_begin, in_begin);
}

inline halfedge_index Mesh::find_halfedge(vertex_index from, vertex_index to) const
{
Philip Trettner's avatar
Philip Trettner committed
515
    auto he_begin = outgoing_halfedge_of(from);
Philip Trettner's avatar
Philip Trettner committed
516
517
518
519
520
521
522
    if (!he_begin.is_valid())
        return halfedge_index::invalid(); // isolated vertex

    auto he = he_begin;
    do
    {
        // found?
Philip Trettner's avatar
Philip Trettner committed
523
        if (to_vertex_of(he) == to)
Philip Trettner's avatar
Philip Trettner committed
524
525
526
            return he;

        // advance
Philip Trettner's avatar
Philip Trettner committed
527
        he = next_halfedge_of(opposite(he));
Philip Trettner's avatar
Philip Trettner committed
528
529
530
531
532
533
534
535

    } while (he != he_begin);

    return halfedge_index::invalid(); // not found
}

inline bool Mesh::is_boundary(vertex_index idx) const
{
Philip Trettner's avatar
Philip Trettner committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
    auto oh = outgoing_halfedge_of(idx);
    return oh.is_valid() && is_boundary(oh);
}

inline bool Mesh::is_free(halfedge_index idx) const { return face_of(idx).is_invalid(); }
inline bool Mesh::is_boundary(halfedge_index idx) const { return is_free(idx); }

inline bool Mesh::is_isolated(vertex_index idx) const { return outgoing_halfedge_of(idx).is_invalid(); }

inline bool Mesh::is_removed(vertex_index idx) const { return outgoing_halfedge_of(idx).value >= -1; }
inline bool Mesh::is_removed(face_index idx) const { return halfedge_of(idx).is_invalid(); }
inline bool Mesh::is_removed(edge_index idx) const { return to_vertex_of(halfedge_of(idx, 0)).is_invalid(); }
inline bool Mesh::is_removed(halfedge_index idx) const { return to_vertex_of(idx).is_invalid(); }

inline void Mesh::set_removed(vertex_index idx) { outgoing_halfedge_of(idx) = halfedge_index::invalid(); }
inline void Mesh::set_removed(face_index idx) { halfedge_of(idx) = halfedge_index::invalid(); }
inline void Mesh::set_removed(edge_index idx)
{
    to_vertex_of(halfedge_of(idx, 0)) = vertex_index::invalid();
    to_vertex_of(halfedge_of(idx, 1)) = vertex_index::invalid();
Philip Trettner's avatar
Philip Trettner committed
556
557
}

Philip Trettner's avatar
Philip Trettner committed
558
559
560
561
562
563
564
565
566
567
568
569
570
inline face_index &Mesh::face_of(halfedge_index idx) { return mHalfedgeToFace[(int)idx]; }
inline vertex_index &Mesh::to_vertex_of(halfedge_index idx) { return mHalfedgeToVertex[(int)idx]; }
inline halfedge_index &Mesh::next_halfedge_of(halfedge_index idx) { return mHalfedgeToNextHalfedge[(int)idx]; }
inline halfedge_index &Mesh::prev_halfedge_of(halfedge_index idx) { return mHalfedgeToPrevHalfedge[(int)idx]; }
inline halfedge_index &Mesh::halfedge_of(face_index idx) { return mFaceToHalfedge[(int)idx]; }
inline halfedge_index &Mesh::outgoing_halfedge_of(vertex_index idx) { return mVertexToOutgoingHalfedge[(int)idx]; }

inline face_index Mesh::face_of(halfedge_index idx) const { return mHalfedgeToFace[(int)idx]; }
inline vertex_index Mesh::to_vertex_of(halfedge_index idx) const { return mHalfedgeToVertex[(int)idx]; }
inline halfedge_index Mesh::next_halfedge_of(halfedge_index idx) const { return mHalfedgeToNextHalfedge[(int)idx]; }
inline halfedge_index Mesh::prev_halfedge_of(halfedge_index idx) const { return mHalfedgeToPrevHalfedge[(int)idx]; }
inline halfedge_index Mesh::halfedge_of(face_index idx) const { return mFaceToHalfedge[(int)idx]; }
inline halfedge_index Mesh::outgoing_halfedge_of(vertex_index idx) const { return mVertexToOutgoingHalfedge[(int)idx]; }
Philip Trettner's avatar
Philip Trettner committed
571

Philip Trettner's avatar
Philip Trettner committed
572
inline halfedge_index Mesh::opposite(halfedge_index he) const { return halfedge_index(he.value ^ 1); }
Philip Trettner's avatar
Philip Trettner committed
573

Philip Trettner's avatar
Philip Trettner committed
574
575
576
inline vertex_index &Mesh::from_vertex_of(halfedge_index idx) { return to_vertex_of(opposite(idx)); }
inline vertex_index Mesh::from_vertex_of(halfedge_index idx) const { return to_vertex_of(opposite(idx)); }

Philip Trettner's avatar
Philip Trettner committed
577
578
inline vertex_index Mesh::next_valid_idx_from(vertex_index idx) const
{
Philip Trettner's avatar
Philip Trettner committed
579
580
581
582
583
    auto s = size_all_vertices();
    auto i = idx;
    while (i.value < s && is_removed(i))
        i.value++;
    return i;
Philip Trettner's avatar
Philip Trettner committed
584
585
586
587
}

inline vertex_index Mesh::prev_valid_idx_from(vertex_index idx) const
{
Philip Trettner's avatar
Philip Trettner committed
588
589
590
591
    auto i = idx;
    while (i.value >= 0 && is_removed(i))
        i.value--;
    return i;
Philip Trettner's avatar
Philip Trettner committed
592
593
594
595
}

inline edge_index Mesh::next_valid_idx_from(edge_index idx) const
{
Philip Trettner's avatar
Philip Trettner committed
596
597
598
599
600
    auto s = size_all_edges();
    auto i = idx;
    while (i.value < s && is_removed(i))
        i.value++;
    return i;
Philip Trettner's avatar
Philip Trettner committed
601
602
603
604
}

inline edge_index Mesh::prev_valid_idx_from(edge_index idx) const
{
Philip Trettner's avatar
Philip Trettner committed
605
606
607
608
    auto i = idx;
    while (i.value >= 0 && is_removed(i))
        i.value--;
    return i;
Philip Trettner's avatar
Philip Trettner committed
609
610
611
612
}

inline face_index Mesh::next_valid_idx_from(face_index idx) const
{
Philip Trettner's avatar
Philip Trettner committed
613
614
615
616
617
    auto s = size_all_faces();
    auto i = idx;
    while (i.value < s && is_removed(i))
        i.value++;
    return i;
Philip Trettner's avatar
Philip Trettner committed
618
619
620
621
}

inline face_index Mesh::prev_valid_idx_from(face_index idx) const
{
Philip Trettner's avatar
Philip Trettner committed
622
623
624
625
    auto i = idx;
    while (i.value >= 0 && is_removed(i))
        i.value--;
    return i;
Philip Trettner's avatar
Philip Trettner committed
626
627
628
629
}

inline halfedge_index Mesh::next_valid_idx_from(halfedge_index idx) const
{
Philip Trettner's avatar
Philip Trettner committed
630
631
632
633
634
    auto s = size_all_halfedges();
    auto i = idx;
    while (i.value < s && is_removed(i))
        i.value++;
    return i;
Philip Trettner's avatar
Philip Trettner committed
635
636
637
638
}

inline halfedge_index Mesh::prev_valid_idx_from(halfedge_index idx) const
{
Philip Trettner's avatar
Philip Trettner committed
639
640
641
642
    auto i = idx;
    while (i.value >= 0 && is_removed(i))
        i.value--;
    return i;
Philip Trettner's avatar
Philip Trettner committed
643
644
}

Philip Trettner's avatar
Philip Trettner committed
645
646
inline void Mesh::connect_prev_next(halfedge_index prev, halfedge_index next)
{
Philip Trettner's avatar
Philip Trettner committed
647
648
    next_halfedge_of(prev) = next;
    prev_halfedge_of(next) = prev;
Philip Trettner's avatar
Philip Trettner committed
649
650
}

Philip Trettner's avatar
Philip Trettner committed
651
652
653
654
inline vertex_index Mesh::face_split(face_index f)
{
    // TODO: can be made more performant

Philip Trettner's avatar
Philip Trettner committed
655
    auto h_begin = halfedge_of(f);
Philip Trettner's avatar
Philip Trettner committed
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672

    // remove face
    remove_face(f);

    // add vertex
    vertex_index vs[3];
    vs[0] = add_vertex();

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

        add_face(vs, 3);

673
        // NOTE: add_face inserted a new halfedge
Philip Trettner's avatar
Philip Trettner committed
674
        h = next_halfedge_of(opposite(next_halfedge_of(h)));
Philip Trettner's avatar
Philip Trettner committed
675
676
677
678
679
    } while (h != h_begin);

    return vs[0];
}

Philip Trettner's avatar
Philip Trettner committed
680
inline vertex_index Mesh::edge_split(edge_index e)
Philip Trettner's avatar
Philip Trettner committed
681
{
Philip Trettner's avatar
Philip Trettner committed
682
683
684
685
686
687
688
689
690
    auto h0 = halfedge_of(e, 0);
    auto h1 = halfedge_of(e, 1);
    auto &h0_ref = halfedge(h0);
    auto &h1_ref = halfedge(h1);

    auto v0 = h0_ref.to_vertex;
    auto v1 = h1_ref.to_vertex;
    auto f0 = h0_ref.face;
    auto f1 = h1_ref.face;
Philip Trettner's avatar
Philip Trettner committed
691
692

    // add vertex
Philip Trettner's avatar
Philip Trettner committed
693
    auto v = add_vertex();
Philip Trettner's avatar
Philip Trettner committed
694
695

    // add two new edges
Philip Trettner's avatar
Philip Trettner committed
696
697
698
699
700
701
702
703
704
705
706
707
    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 &e1h0_ref = halfedge(e1h0);
    auto &e1h1_ref = halfedge(e1h1);
    auto &e2h0_ref = halfedge(e2h0);
    auto &e2h1_ref = halfedge(e2h1);
Philip Trettner's avatar
Philip Trettner committed
708

Philip Trettner's avatar
Philip Trettner committed
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
    auto h0_prev = h0_ref.prev_halfedge;
    auto h0_next = h0_ref.next_halfedge;
    auto h1_prev = h1_ref.prev_halfedge;
    auto h1_next = h1_ref.next_halfedge;

    e1h0_ref.face = f0;
    e2h0_ref.face = f0;
    e1h1_ref.face = f1;
    e2h1_ref.face = f1;

    e1h0_ref.to_vertex = v0;
    e2h0_ref.to_vertex = v;
    e1h1_ref.to_vertex = v;
    e2h1_ref.to_vertex = 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_ref = vertex(h0_ref.to_vertex);
    auto &v1_ref = vertex(h1_ref.to_vertex);
    if (v0_ref.outgoing_halfedge == h1)
        v0_ref.outgoing_halfedge = e1h1;
    if (v1_ref.outgoing_halfedge == h0)
        v1_ref.outgoing_halfedge = e2h0;

    // rewire faces
Philip Trettner's avatar
Philip Trettner committed
741
742
743
744
745
746
747
748
749
750
751
752
    if (f0.is_valid())
    {
        auto &f0_ref = face(f0);
        if (f0_ref.halfedge == h0)
            f0_ref.halfedge = e1h0;
    }
    if (f1.is_valid())
    {
        auto &f1_ref = face(f1);
        if (f1_ref.halfedge == h1)
            f1_ref.halfedge = e2h1;
    }
Philip Trettner's avatar
Philip Trettner committed
753
754
755
756
757
758
759
760

    // remove edge
    h0_ref.set_removed();
    h1_ref.set_removed();
    mRemovedHalfedges += 2;
    mCompact = false;

    return v;
Philip Trettner's avatar
Philip Trettner committed
761
762
}

Philip Trettner's avatar
Philip Trettner committed
763
inline vertex_index Mesh::halfedge_split(halfedge_index h)
Philip Trettner's avatar
Philip Trettner committed
764
{
Philip Trettner's avatar
Philip Trettner committed
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
    // add vertex and edge
    auto v = add_vertex();
    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 = halfedge(h0).to_vertex;
    auto v1 = halfedge(h1).to_vertex;

    // rewire edges
    auto &h0_ref = halfedge(h0);
    auto &h1_ref = halfedge(h1);
    auto &h2_ref = halfedge(h2);
    auto &h3_ref = halfedge(h3);

    auto h0_next = h0_ref.next_halfedge;
    auto h1_prev = h1_ref.prev_halfedge;

    auto f0 = h0_ref.face;
    auto f1 = h1_ref.face;

    h2_ref.face = f0;
    h3_ref.face = f1;

    h0_ref.to_vertex = v;
    h1_ref.to_vertex = v1; //< already there
    h2_ref.to_vertex = v0;
    h3_ref.to_vertex = v;

    connect_prev_next(h0, h2);
    connect_prev_next(h2, h0_next);
Philip Trettner's avatar
Philip Trettner committed
799

Philip Trettner's avatar
Philip Trettner committed
800
801
    connect_prev_next(h1_prev, h3);
    connect_prev_next(h3, h1);
Philip Trettner's avatar
Philip Trettner committed
802

Philip Trettner's avatar
Philip Trettner committed
803
804
805
806
807
808
809
810
811
812
813
    // rewire vertices
    auto &v0_ref = vertex(v0);
    if (v0_ref.outgoing_halfedge == h1)
        v0_ref.outgoing_halfedge = h3;

    vertex(v).outgoing_halfedge = h1_ref.is_free() ? h1 : h2; // boundary

    // rewire faces
    // -> already ok

    return v;
Philip Trettner's avatar
Philip Trettner committed
814
815
}

816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
inline face_index Mesh::face_fill(halfedge_index h)
{
    assert(is_boundary(h));

    auto f = alloc_face();
    auto &f_ref = face(f);

    f_ref.halfedge = h;

    auto h_begin = h;
    do
    {
        auto &h_ref = halfedge(h);

        // set face
        h_ref.face = f;

Philip Trettner's avatar
Philip Trettner committed
833
        // fix face boundary
834
835
836
        if (is_boundary(opposite(h)))
            f_ref.halfedge = h;

Philip Trettner's avatar
Philip Trettner committed
837
838
839
840
841
        // fix adj face boundary
        auto adj_face = halfedge(opposite(h)).face;
        if (adj_face.is_valid())
            fix_boundary_state_of(adj_face);

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
        // advance
        h = h_ref.next_halfedge;
    } while (h != h_begin);

    // fix vertex boundaries
    fix_boundary_state_of_vertices(f);

    return f;
}

inline void Mesh::halfedge_attach(halfedge_index h, vertex_index v)
{
    assert(vertex(v).is_isolated());

    auto &h_ref = halfedge(h);
    auto h_next = h_ref.next_halfedge;
    auto v_to = h_ref.to_vertex;

    auto f = h_ref.face;

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

    auto &h0_ref = halfedge(h0);
    auto &h1_ref = halfedge(h1);

    h0_ref.face = f;
    h0_ref.to_vertex = v;

    h1_ref.face = f;
    h1_ref.to_vertex = v_to;

Philip Trettner's avatar
fix    
Philip Trettner committed
875
    vertex(v).outgoing_halfedge = h1;
Philip Trettner's avatar
Philip Trettner committed
876

877
878
879
880
881
    connect_prev_next(h, h0);
    connect_prev_next(h0, h1);
    connect_prev_next(h1, h_next);
}

Philip Trettner's avatar
Philip Trettner committed
882
883
884
inline void Mesh::vertex_collapse(vertex_index v)
{
    auto &v_ref = vertex(v);
Philip Trettner's avatar
Philip Trettner committed
885

Philip Trettner's avatar
Philip Trettner committed
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
    // isolated vertices are just removed
    if (v_ref.is_isolated())
    {
        remove_vertex(v);
    }
    // boundary vertices are special
    else if (is_boundary(v))
    {
        assert(0 && "not implemented");
    }
    else // interior vertex
    {
        auto h_begin = halfedge(vertex(v).outgoing_halfedge).next_halfedge;

        remove_vertex(v);
Philip Trettner's avatar
Philip Trettner committed
901

Philip Trettner's avatar
Philip Trettner committed
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
        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 = halfedge(h).next_halfedge;
        } while (h != h_begin);

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

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

    assert(0 && "not implemented");
}
Philip Trettner's avatar
Philip Trettner committed
928

Philip Trettner's avatar
Philip Trettner committed
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
inline void Mesh::edge_rotate_next(edge_index e)
{
    assert(!handle_of(e).is_boundary() && "does not work on boundaries");
    assert(handle_of(e).vertexA().adjacent_vertices().size() > 2 && "does not work on valence <= 2 vertices");
    assert(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_ref = halfedge(h0);
    auto &h1_ref = halfedge(h1);

    auto h0_next = h0_ref.next_halfedge;
    auto h0_prev = h0_ref.prev_halfedge;
    auto h1_next = h1_ref.next_halfedge;
    auto h1_prev = h1_ref.prev_halfedge;
944

Philip Trettner's avatar
Philip Trettner committed
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
    auto &h0_next_ref = halfedge(h0_next);
    auto &h1_next_ref = halfedge(h1_next);

    auto h0_next_next = h0_next_ref.next_halfedge;
    auto h1_next_next = h1_next_ref.next_halfedge;

    // fix vertices
    auto &v0 = vertex(h0_ref.to_vertex);
    if (v0.outgoing_halfedge == h1)
        v0.outgoing_halfedge = h0_next;
    auto &v1 = vertex(h1_ref.to_vertex);
    if (v1.outgoing_halfedge == h0)
        v1.outgoing_halfedge = h1_next;

    // fix faces
    face(h0_ref.face).halfedge = h0;
    face(h1_ref.face).halfedge = h1;

    // fix half-edges
    h0_ref.to_vertex = h0_next_ref.to_vertex;
    h1_ref.to_vertex = h1_next_ref.to_vertex;
    h0_next_ref.face = h1_ref.face;
    h1_next_ref.face = h0_ref.face;

    // move to next
970
971
    connect_prev_next(h1_prev, h0_next);
    connect_prev_next(h0_prev, h1_next);
Philip Trettner's avatar
Philip Trettner committed
972

973
974
    connect_prev_next(h0_next, h1);
    connect_prev_next(h1_next, h0);
Philip Trettner's avatar
Philip Trettner committed
975

976
977
    connect_prev_next(h0, h0_next_next);
    connect_prev_next(h1, h1_next_next);
Philip Trettner's avatar
Philip Trettner committed
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998

    // fix boundary state
    fix_boundary_state_of(h0_ref.face);
    fix_boundary_state_of(h1_ref.face);
}

inline void Mesh::edge_rotate_prev(edge_index e)
{
    assert(!handle_of(e).is_boundary() && "does not work on boundaries");
    assert(handle_of(e).vertexA().adjacent_vertices().size() > 2 && "does not work on valence <= 2 vertices");
    assert(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_ref = halfedge(h0);
    auto &h1_ref = halfedge(h1);

    auto h0_next = h0_ref.next_halfedge;
    auto h0_prev = h0_ref.prev_halfedge;
    auto h1_next = h1_ref.next_halfedge;
    auto h1_prev = h1_ref.prev_halfedge;
999

Philip Trettner's avatar
Philip Trettner committed
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
    auto &h0_prev_ref = halfedge(h0_prev);
    auto &h1_prev_ref = halfedge(h1_prev);

    auto h0_prev_prev = h0_prev_ref.prev_halfedge;
    auto &h0_prev_prev_ref = halfedge(h0_prev_prev);
    auto h1_prev_prev = h1_prev_ref.prev_halfedge;
    auto &h1_prev_prev_ref = halfedge(h1_prev_prev);

    // fix vertex
    auto &v0 = vertex(h0_ref.to_vertex);
    if (v0.outgoing_halfedge == h1)
        v0.outgoing_halfedge = h0_next;
    auto &v1 = vertex(h1_ref.to_vertex);
    if (v1.outgoing_halfedge == h0)
        v1.outgoing_halfedge = h1_next;

    // fix faces
    face(h0_ref.face).halfedge = h0;
    face(h1_ref.face).halfedge = h1;

    // fix half-edge
    h1_ref.to_vertex = h0_prev_prev_ref.to_vertex;
    h0_ref.to_vertex = h1_prev_prev_ref.to_vertex;
    h0_prev_ref.face = h1_ref.face;
    h1_prev_ref.face = h0_ref.face;

    // move to next
1027
1028
    connect_prev_next(h0_prev, h1_next);
    connect_prev_next(h1_prev, h0_next);
Philip Trettner's avatar
Philip Trettner committed
1029

1030
1031
    connect_prev_next(h1, h0_prev);
    connect_prev_next(h0, h1_prev);
Philip Trettner's avatar
Philip Trettner committed
1032

1033
1034
    connect_prev_next(h0_prev_prev, h0);
    connect_prev_next(h1_prev_prev, h1);
Philip Trettner's avatar
Philip Trettner committed
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070

    // fix boundary state
    fix_boundary_state_of(h0_ref.face);
    fix_boundary_state_of(h1_ref.face);
}

inline void Mesh::halfedge_rotate_next(halfedge_index h)
{
    assert(handle_of(h).next().next().next() != h && "does not work for triangles");
    assert(!handle_of(h).edge().is_boundary() && "does not work on boundaries");
    assert(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_ref = halfedge(h0);
    auto &h1_ref = halfedge(h1);

    auto h0_next = h0_ref.next_halfedge;
    auto h1_prev = h1_ref.prev_halfedge;
    auto &h0_next_ref = halfedge(h0_next);
    auto h0_next_next = h0_next_ref.next_halfedge;

    // fix vertex
    auto &v = vertex(h0_ref.to_vertex);
    if (v.outgoing_halfedge == h1)
        v.outgoing_halfedge = h0_next;

    // fix faces
    face(h0_ref.face).halfedge = h0;
    face(h1_ref.face).halfedge = h1;

    // fix half-edges
    h0_ref.to_vertex = h0_next_ref.to_vertex;
    h0_next_ref.face = h1_ref.face;

    // move to next
1071
1072
1073
    connect_prev_next(h1_prev, h0_next);
    connect_prev_next(h0_next, h1);
    connect_prev_next(h0, h0_next_next);
Philip Trettner's avatar
Philip Trettner committed
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110

    // fix boundary state
    fix_boundary_state_of(h0_ref.face);
    fix_boundary_state_of(h1_ref.face);
}

inline void Mesh::halfedge_rotate_prev(halfedge_index h)
{
    assert(handle_of(h).prev().prev().prev() != h && "does not work for triangles");
    assert(!handle_of(h).edge().is_boundary() && "does not work on boundaries");
    assert(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_ref = halfedge(h0);
    auto &h1_ref = halfedge(h1);

    auto h0_prev = h0_ref.prev_halfedge;
    auto h1_next = h1_ref.next_halfedge;
    auto &h0_prev_ref = halfedge(h0_prev);
    auto h0_prev_prev = h0_prev_ref.prev_halfedge;
    auto &h0_prev_prev_ref = halfedge(h0_prev_prev);

    // fix vertex
    auto &v = vertex(h1_ref.to_vertex);
    if (v.outgoing_halfedge == h0)
        v.outgoing_halfedge = h1_next;

    // fix faces
    face(h0_ref.face).halfedge = h0;
    face(h1_ref.face).halfedge = h1;

    // fix half-edge
    h1_ref.to_vertex = h0_prev_prev_ref.to_vertex;
    h0_prev_ref.face = h1_ref.face;

    // move to next
1111
1112
1113
    connect_prev_next(h0_prev, h1_next);
    connect_prev_next(h1, h0_prev);
    connect_prev_next(h0_prev_prev, h0);
Philip Trettner's avatar
Philip Trettner committed
1114
1115
1116
1117
1118
1119

    // fix boundary state
    fix_boundary_state_of(h0_ref.face);
    fix_boundary_state_of(h1_ref.face);
}

1120
1121
1122
1123
1124
1125
1126
1127
1128
inline void Mesh::permute_vertices(std::vector<int> const &p)
{
    assert(detail::is_valid_permutation(p));

    // calculate transpositions
    auto ts = detail::transpositions_of(p);

    // apply them
    for (auto t : ts)
Philip Trettner's avatar
Philip Trettner committed
1129
        std::swap(mVertexToOutgoingHalfedge[t.first], mVertexToOutgoingHalfedge[t.second]);
1130
1131

    // fix half-edges
Philip Trettner's avatar
Philip Trettner committed
1132
1133
1134
    for (auto &h_to : mHalfedgeToVertex)
        if (h_to.is_valid())
            h_to.value = p[h_to.value];
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149

    // update attributes
    for (auto a = mVertexAttrs; a; a = a->mNextAttribute)
        a->apply_transpositions(ts);
}

inline void Mesh::permute_faces(std::vector<int> const &p)
{
    assert(detail::is_valid_permutation(p));

    // calculate transpositions
    auto ts = detail::transpositions_of(p);

    // apply them
    for (auto t : ts)
Philip Trettner's avatar
Philip Trettner committed
1150
        std::swap(mFaceToHalfedge[t.first], mFaceToHalfedge[t.second]);
1151
1152

    // fix half-edges
Philip Trettner's avatar
Philip Trettner committed
1153
1154
1155
    for (auto &h_f : mHalfedgeToFace)
        if (h_f.is_valid())
            h_f.value = p[h_f.value];
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185

    // update attributes
    for (auto a = mFaceAttrs; a; a = a->mNextAttribute)
        a->apply_transpositions(ts);
}

inline void Mesh::permute_edges(std::vector<int> const &p)
{
    assert(detail::is_valid_permutation(p));

    std::vector<int> hp(p.size() * 2);
    for (auto i = 0u; i < p.size(); ++i)
    {
        hp[i * 2 + 0] = p[i] * 2 + 0;
        hp[i * 2 + 1] = p[i] * 2 + 1;
    }
    assert(detail::is_valid_permutation(hp));

    // calculate transpositions
    std::vector<std::pair<int, int>> edge_ts;
    std::vector<std::pair<int, int>> halfedge_ts;

    detail::apply_permutation(p, [&](int i, int j) {
        edge_ts.emplace_back(i, j);
        halfedge_ts.emplace_back((i << 1) + 0, (j << 1) + 0);
        halfedge_ts.emplace_back((i << 1) + 1, (j << 1) + 1);
    });

    // apply them
    for (auto t : halfedge_ts)
Philip Trettner's avatar
Philip Trettner committed
1186
1187
1188
1189
1190
1191
    {
        std::swap(mHalfedgeToFace[t.first], mHalfedgeToFace[t.second]);
        std::swap(mHalfedgeToVertex[t.first], mHalfedgeToVertex[t.second]);
        std::swap(mHalfedgeToNextHalfedge[t.first], mHalfedgeToNextHalfedge[t.second]);
        std::swap(mHalfedgeToPrevHalfedge[t.first], mHalfedgeToPrevHalfedge[t.second]);
    }
1192
1193

    // fix half-edges
Philip Trettner's avatar
Philip Trettner committed
1194
1195
1196
    for (auto &v_out : mVertexToOutgoingHalfedge)
        if (v_out.value >= 0)
            v_out.value = hp[v_out.value];
1197

Philip Trettner's avatar
Philip Trettner committed
1198
1199
1200
    for (auto &f_h : mFaceToHalfedge)
        if (f_h.value >= 0)
            f_h.value = hp[f_h.value];
1201

Philip Trettner's avatar
Philip Trettner committed
1202
1203
1204
    for (auto &h_next : mHalfedgeToNextHalfedge)
        if (h_next.value >= 0)
            h_next.value = hp[h_next.value];
1205

Philip Trettner's avatar
Philip Trettner committed
1206
1207
1208
    for (auto &h_prev : mHalfedgeToPrevHalfedge)
        if (h_prev.value >= 0)
            h_prev.value = hp[h_prev.value];
1209
1210
1211
1212
1213
1214
1215
1216

    // update attributes
    for (auto a = mEdgeAttrs; a; a = a->mNextAttribute)
        a->apply_transpositions(edge_ts);
    for (auto a = mHalfedgeAttrs; a; a = a->mNextAttribute)
        a->apply_transpositions(halfedge_ts);
}

Philip Trettner's avatar
Philip Trettner committed
1217
1218
1219
1220
1221
1222
inline void Mesh::compactify()
{
    if (is_compact())
        return;

    // calculate remappings
1223
1224
1225
1226
    int v_cnt = size_all_vertices();
    int f_cnt = size_all_faces();
    int e_cnt = size_all_edges();
    int h_cnt = size_all_halfedges();
Philip Trettner's avatar
Philip Trettner committed
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
    std::vector<int> v_new_to_old;
    std::vector<int> f_new_to_old;
    std::vector<int> e_new_to_old;
    std::vector<int> h_new_to_old;
    v_new_to_old.reserve(v_cnt);
    f_new_to_old.reserve(f_cnt);
    e_new_to_old.reserve(e_cnt);
    h_new_to_old.reserve(h_cnt);
    std::vector<int> h_old_to_new(h_cnt, -1);
    std::vector<int> v_old_to_new(v_cnt, -1);
    std::vector<int> f_old_to_new(f_cnt, -1);

    for (auto i = 0; i < v_cnt; ++i)
Philip Trettner's avatar
Philip Trettner committed
1240
        if (!is_removed(vertex_index(i)))
Philip Trettner's avatar
Philip Trettner committed
1241
        {
Philip Trettner's avatar
Philip Trettner committed
1242
            v_old_to_new[i] = (int)v_new_to_old.size();
Philip Trettner's avatar
Philip Trettner committed
1243
1244
1245
1246
            v_new_to_old.push_back(i);
        }

    for (auto i = 0; i < f_cnt; ++i)
Philip Trettner's avatar
Philip Trettner committed
1247
        if (!is_removed(face_index(i)))
Philip Trettner's avatar
Philip Trettner committed
1248
        {
Philip Trettner's avatar
Philip Trettner committed
1249
            f_old_to_new[i] = (int)f_new_to_old.size();
Philip Trettner's avatar
Philip Trettner committed
1250
1251
1252
1253
            f_new_to_old.push_back(i);
        }

    for (auto i = 0; i < e_cnt; ++i)
Philip Trettner's avatar
Philip Trettner committed
1254
        if (!is_removed(edge_index(i)))
Philip Trettner's avatar
Philip Trettner committed
1255
1256
1257
            e_new_to_old.push_back(i);

    for (auto i = 0; i < h_cnt; ++i)
Philip Trettner's avatar
Philip Trettner committed
1258
        if (!is_removed(halfedge_index(i)))
Philip Trettner's avatar
Philip Trettner committed
1259
        {
Philip Trettner's avatar
Philip Trettner committed
1260
            h_old_to_new[i] = (int)h_new_to_old.size();
Philip Trettner's avatar
Philip Trettner committed
1261
1262
1263
1264
1265
1266
            h_new_to_old.push_back(i);
        }

    // apply remappings (map[new_prim_id] = old_prim_id)

    for (auto i = 0u; i < v_new_to_old.size(); ++i)
Philip Trettner's avatar
Philip Trettner committed
1267
        mVertexToOutgoingHalfedge[i] = mVertexToOutgoingHalfedge[v_new_to_old[i]];
Philip Trettner's avatar
Philip Trettner committed
1268
    for (auto i = 0u; i < f_new_to_old.size(); ++i)
Philip Trettner's avatar
Philip Trettner committed
1269
        mFaceToHalfedge[i] = mFaceToHalfedge[f_new_to_old[i]];
Philip Trettner's avatar
Philip Trettner committed
1270
1271
    for (auto i = 0u; i < h_new_to_old.size(); ++i)
    {
Philip Trettner's avatar
Philip Trettner committed
1272
1273
1274
1275
        mHalfedgeToFace[i] = mHalfedgeToFace[h_new_to_old[i]];
        mHalfedgeToVertex[i] = mHalfedgeToVertex[h_new_to_old[i]];
        mHalfedgeToNextHalfedge[i] = mHalfedgeToNextHalfedge[h_new_to_old[i]];
        mHalfedgeToPrevHalfedge[i] = mHalfedgeToPrevHalfedge[h_new_to_old[i]];
Philip Trettner's avatar
Philip Trettner committed
1276
1277
    }

Philip Trettner's avatar
Philip Trettner committed
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
    mVertexToOutgoingHalfedge.resize(v_new_to_old.size());
    mFaceToHalfedge.resize(f_new_to_old.size());
    mHalfedgeToFace.resize(h_new_to_old.size());
    mHalfedgeToVertex.resize(h_new_to_old.size());
    mHalfedgeToNextHalfedge.resize(h_new_to_old.size());
    mHalfedgeToPrevHalfedge.resize(h_new_to_old.size());

    for (auto &v_out : mVertexToOutgoingHalfedge)
        if (v_out.value >= 0)
            v_out.value = h_old_to_new[v_out.value];

    for (auto &f_h : mFaceToHalfedge)
        if (f_h.value >= 0)
            f_h.value = h_old_to_new[f_h.value];

    for (auto &h_next : mHalfedgeToNextHalfedge)
        if (h_next.value >= 0)
            h_next.value = h_old_to_new[h_next.value];
    for (auto &h_prev : mHalfedgeToPrevHalfedge)
        if (h_prev.value >= 0)
            h_prev.value = h_old_to_new[h_prev.value];
    for (auto &h_f : mHalfedgeToFace)
        if (h_f.value >= 0)
            h_f.value = f_old_to_new[h_f.value];
    for (auto &h_v : mHalfedgeToVertex)
        if (h_v.value >= 0)
            h_v.value = v_old_to_new[h_v.value];
Philip Trettner's avatar
Philip Trettner committed
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315

    for (auto a = mVertexAttrs; a; a = a->mNextAttribute)
        a->apply_remapping(v_new_to_old);
    for (auto a = mFaceAttrs; a; a = a->mNextAttribute)
        a->apply_remapping(f_new_to_old);
    for (auto a = mEdgeAttrs; a; a = a->mNextAttribute)
        a->apply_remapping(e_new_to_old);
    for (auto a = mHalfedgeAttrs; a; a = a->mNextAttribute)
        a->apply_remapping(h_new_to_old);

    // shrink to fit
Philip Trettner's avatar
Philip Trettner committed
1316
1317
1318
1319
1320
1321
    mVertexToOutgoingHalfedge.shrink_to_fit();
    mFaceToHalfedge.shrink_to_fit();
    mHalfedgeToFace.shrink_to_fit();
    mHalfedgeToVertex.shrink_to_fit();
    mHalfedgeToNextHalfedge.shrink_to_fit();
    mHalfedgeToPrevHalfedge.shrink_to_fit();
Philip Trettner's avatar
Philip Trettner committed
1322
1323

    for (auto a = mVertexAttrs; a; a = a->mNextAttribute)
1324
        a->resize(size_all_vertices(), true);
Philip Trettner's avatar
Philip Trettner committed
1325
    for (auto a = mFaceAttrs; a; a = a->mNextAttribute)
1326
        a->resize(size_all_faces(), true);
Philip Trettner's avatar
Philip Trettner committed
1327
    for (auto a = mEdgeAttrs; a; a = a->mNextAttribute)
1328
        a->resize(size_all_edges(), true);
Philip Trettner's avatar
Philip Trettner committed
1329
    for (auto a = mHalfedgeAttrs; a; a = a->mNextAttribute)
1330
        a->resize(size_all_halfedges(), true);
Philip Trettner's avatar
Philip Trettner committed
1331
1332
1333
1334
1335
1336
1337
1338
1339

    mRemovedFaces = 0;
    mRemovedHalfedges = 0;
    mRemovedVertices = 0;
    mCompact = true;
}

inline void Mesh::clear()
{
Philip Trettner's avatar
Philip Trettner committed
1340
1341
1342
1343
1344
1345
    for (auto &v_h : mVertexToOutgoingHalfedge)
        v_h = halfedge_index::invalid();
    for (auto &f_h : mFaceToHalfedge)
        f_h = halfedge_index::invalid();
    for (auto &h_v : mHalfedgeToVertex)
        h_v = vertex_index::invalid();
Philip Trettner's avatar
Philip Trettner committed
1346
1347
1348
1349
1350

    mCompact = false;
    compactify();
}

Philip Trettner's avatar
Philip Trettner committed
1351
1352
1353
inline void Mesh::copy_from(const Mesh &m)
{
    // copy topo
Philip Trettner's avatar
Philip Trettner committed
1354
1355
1356
1357
1358
1359
    mVertexToOutgoingHalfedge = m.mVertexToOutgoingHalfedge;
    mFaceToHalfedge = m.mFaceToHalfedge;
    mHalfedgeToFace = m.mHalfedgeToFace;
    mHalfedgeToVertex = m.mHalfedgeToVertex;
    mHalfedgeToNextHalfedge = m.mHalfedgeToNextHalfedge;
    mHalfedgeToPrevHalfedge = m.mHalfedgeToPrevHalfedge;
Philip Trettner's avatar
Philip Trettner committed
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384

    // copy helper data
    mRemovedFaces = m.mRemovedFaces;
    mRemovedHalfedges = m.mRemovedHalfedges;
    mRemovedVertices = m.mRemovedVertices;
    mCompact = m.mCompact;

    // resize attributes
    for (auto a = mVertexAttrs; a; a = a->mNextAttribute)
        a->resize(size_all_vertices(), true);
    for (auto a = mFaceAttrs; a; a = a->mNextAttribute)
        a->resize(size_all_faces(), true);
    for (auto a = mEdgeAttrs; a; a = a->mNextAttribute)
        a->resize(size_all_edges(), true);
    for (auto a = mHalfedgeAttrs; a; a = a->mNextAttribute)
        a->resize(size_all_halfedges(), true);
}

inline SharedMesh Mesh::copy() const
{
    auto m = create();
    m->copy_from(*this);
    return m;
}

Philip Trettner's avatar
Philip Trettner committed
1385
1386
1387
1388
1389
inline void Mesh::reserve_faces(int capacity)
{
    for (auto a = mFaceAttrs; a; a = a->mNextAttribute)
        a->resize(capacity, false);

Philip Trettner's avatar
Philip Trettner committed
1390
    mFaceToHalfedge.reserve(capacity);
Philip Trettner's avatar
Philip Trettner committed
1391
1392
1393
1394
1395
1396
1397
}

inline void Mesh::reserve_vertices(int capacity)
{
    for (auto a = mVertexAttrs; a; a = a->mNextAttribute)
        a->resize(capacity, false);

Philip Trettner's avatar
Philip Trettner committed
1398
    mVertexToOutgoingHalfedge.reserve(capacity);
Philip Trettner's avatar
Philip Trettner committed
1399
1400
1401
1402
1403
1404
1405
1406
1407
}

inline void Mesh::reserve_edges(int capacity)
{
    for (auto a = mEdgeAttrs; a; a = a->mNextAttribute)
        a->resize(capacity, false);
    for (auto a = mHalfedgeAttrs; a; a = a->mNextAttribute)
        a->resize(capacity << 1, false);

Philip Trettner's avatar
Philip Trettner committed
1408
1409
1410
1411
    mHalfedgeToFace.reserve(capacity * 2);
    mHalfedgeToVertex.reserve(capacity * 2);
    mHalfedgeToNextHalfedge.reserve(capacity * 2);
    mHalfedgeToPrevHalfedge.reserve(capacity * 2);
Philip Trettner's avatar
Philip Trettner committed
1412
1413
1414
1415
1416
1417
1418
1419
1420
}

inline void Mesh::reserve_halfedges(int capacity)
{
    for (auto a = mHalfedgeAttrs; a; a = a->mNextAttribute)
        a->resize(capacity, false);
    for (auto a = mEdgeAttrs; a; a = a->mNextAttribute)
        a->resize(capacity >> 1, false);

Philip Trettner's avatar
Philip Trettner committed
1421
1422
1423
1424
    mHalfedgeToFace.reserve(capacity);
    mHalfedgeToVertex.reserve(capacity);
    mHalfedgeToNextHalfedge.reserve(capacity);
    mHalfedgeToPrevHalfedge.reserve(capacity);
Philip Trettner's avatar
Philip Trettner committed
1425
1426
}
}