impl_mesh.hh 36.9 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
9
10
11
12
13
14
namespace polymesh
{
inline vertex_index Mesh::add_vertex()
{
    auto idx = vertex_index((int)mVertices.size());
    mVertices.push_back(vertex_info());

    // notify attributes
Philip Trettner's avatar
Philip Trettner committed
15
    auto vCnt = (int)mVertices.size();
Philip Trettner's avatar
Philip Trettner committed
16
17
18
19
20
21
    for (auto p = mVertexAttrs; p; p = p->mNextAttribute)
        p->resize(vCnt, false);

    return idx;
}

Philip Trettner's avatar
Philip Trettner committed
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
inline face_index Mesh::alloc_face()
{
    auto idx = face_index((int)mFaces.size());
    mFaces.push_back(face_info());

    // notify attributes
    auto fCnt = (int)mFaces.size();
    for (auto p = mFaceAttrs; p; p = p->mNextAttribute)
        p->resize(fCnt, false);

    return idx;
}

inline edge_index Mesh::alloc_edge()
{
    auto idx = edge_index((int)mHalfedges.size() >> 1);
    mHalfedges.push_back(halfedge_info());
    mHalfedges.push_back(halfedge_info());

    // notify attributes
    auto hCnt = (int)mHalfedges.size();
    auto eCnt = (int)mHalfedges.size() >> 1;
    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
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
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");

    auto fidx = face_index((int)mFaces.size());

    // 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(halfedge(h0).is_free() && "half-edge already contains a face");

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

        // link face
        halfedge(h0).face = fidx;
    }

    // fix boundary states
    for (auto i = 0; i < vcnt; ++i)
    {
        auto h = half_loop[i];
        auto v = halfedge(h).to_vertex;
        auto f = halfedge(opposite(h)).face;

        // fix vertex
        fix_boundary_state_of(v);

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

    // set up face data
    face_info f;
    f.halfedge = half_loop[0];
    mFaces.push_back(f);

    // fix new face
    fix_boundary_state_of(fidx);

    // notify attributes
Philip Trettner's avatar
Philip Trettner committed
124
    auto fCnt = (int)mFaces.size();
Philip Trettner's avatar
Philip Trettner committed
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
    for (auto p = mFaceAttrs; p; p = p->mNextAttribute)
        p->resize(fCnt, false);

    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
198
    auto hCnt = (int)mHalfedges.size();
Philip Trettner's avatar
Philip Trettner committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    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
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
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);
    return halfedge(h_from).next_halfedge == h0 ? h0 : h1;
}

Philip Trettner's avatar
Philip Trettner committed
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
inline void Mesh::make_adjacent(halfedge_index he_in, halfedge_index he_out)
{
    // see http://kaba.hilvi.org/homepage/blog/halfedge/halfedge.htm ::makeAdjacent
    auto &in = halfedge(he_in);
    auto &out = halfedge(he_out);

    auto he_b = in.next_halfedge;
    auto he_d = out.prev_halfedge;

    // 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 &b = halfedge(he_b);
    auto &d = halfedge(he_d);
    auto &g = halfedge(he_g);

    auto he_h = g.next_halfedge;
    auto &h = halfedge(he_h);

    // properly rewire
    in.next_halfedge = he_out;
    out.prev_halfedge = he_in;

    g.next_halfedge = he_b;
    b.prev_halfedge = he_g;

    d.next_halfedge = he_h;
    h.prev_halfedge = he_d;
}

inline void Mesh::remove_face(face_index f_idx)
{
    auto &f = face(f_idx);
    assert(f.halfedge.is_valid());

    auto he_begin = f.halfedge;
    auto he = he_begin;
    do
    {
        auto &h = halfedge(he);
        assert(h.face == f_idx);

        // set half-edge face to invalid
        h.face = face_index::invalid();

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

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

        // advance
        he = h.next_halfedge;
    } while (he != he_begin);

    // mark removed
    // (at the end!)
    f.set_removed();

    // 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)
{
    auto &v = vertex(v_idx);
    assert(v.is_valid());

    // remove all outgoing edges
    while (!v.is_isolated())
        remove_edge(edge_of(v.outgoing_halfedge));

    // mark removed
    v.set_removed();

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

inline void Mesh::fix_boundary_state_of(vertex_index v_idx)
{
    auto &v = vertex(v_idx);
    assert(!v.is_isolated());

    auto he_begin = v.outgoing_halfedge;
    auto he = he_begin;
    do
    {
        // if half-edge is boundary, set it
        if (halfedge(he).is_free())
        {
            v.outgoing_halfedge = he;
            return;
        }

        // advance
        he = halfedge(opposite(he)).next_halfedge;
    } while (he != he_begin);
}

inline void Mesh::fix_boundary_state_of(face_index f_idx)
{
    auto &f = face(f_idx);

    auto he_begin = f.halfedge;
    auto he = he_begin;
    do
    {
        // if half-edge is boundary, set it
        if (halfedge(opposite(he)).is_free())
        {
            f.halfedge = he;
            return;
        }

        // advance
        he = halfedge(he).next_halfedge;
    } while (he != he_begin);
}

489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
inline void Mesh::fix_boundary_state_of_vertices(face_index f_idx)
{
    auto &f = face(f_idx);

    auto he_begin = f.halfedge;
    auto he = he_begin;
    do
    {
        auto &h_ref = halfedge(he);

        // fix vertex
        fix_boundary_state_of(h_ref.to_vertex);

        // advance
        he = h_ref.next_halfedge;
    } while (he != he_begin);
}

Philip Trettner's avatar
Philip Trettner committed
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
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
    {
        auto const &h = halfedge(he);
        assert(h.to_vertex == halfedge(in_end).to_vertex);

        // free? found one!
        if (h.is_free())
            return he;

        // next half-edge of vertex
        he = opposite(h.next_halfedge);
    } while (he != in_end);

    return halfedge_index::invalid();
}

inline halfedge_index Mesh::find_free_incident(vertex_index v) const
{
    auto in_begin = opposite(vertex(v).outgoing_halfedge);
    return find_free_incident(in_begin, in_begin);
}

inline halfedge_index Mesh::find_halfedge(vertex_index from, vertex_index to) const
{
    auto he_begin = vertex(from).outgoing_halfedge;
    if (!he_begin.is_valid())
        return halfedge_index::invalid(); // isolated vertex

    auto he = he_begin;
    do
    {
        auto const &h = halfedge(he);

        // found?
        if (h.to_vertex == to)
            return he;

        // advance
        he = halfedge(opposite(he)).next_halfedge;

    } while (he != he_begin);

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

inline bool Mesh::is_boundary(vertex_index idx) const
{
    auto const &v = vertex(idx);
    return v.outgoing_halfedge.is_valid() && is_boundary(v.outgoing_halfedge);
}

Philip Trettner's avatar
Philip Trettner committed
563
inline bool Mesh::is_boundary(halfedge_index idx) const { return halfedge(idx).is_free(); }
Philip Trettner's avatar
Philip Trettner committed
564

Philip Trettner's avatar
Philip Trettner committed
565
inline halfedge_index Mesh::opposite(halfedge_index he) const { return halfedge_index(he.value ^ 1); }
Philip Trettner's avatar
Philip Trettner committed
566
567
568
569
570
571

inline vertex_index Mesh::next_valid_idx_from(vertex_index idx) const
{
    for (auto i = idx.value; i < (int)mVertices.size(); ++i)
        if (mVertices[i].is_valid())
            return vertex_index(i);
572
    return vertex_index(size_all_vertices()); // end index
Philip Trettner's avatar
Philip Trettner committed
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
}

inline vertex_index Mesh::prev_valid_idx_from(vertex_index idx) const
{
    for (auto i = idx.value; i >= 0; --i)
        if (mVertices[i].is_valid())
            return vertex_index(i);
    return {}; // invalid
}

inline edge_index Mesh::next_valid_idx_from(edge_index idx) const
{
    for (auto i = idx.value << 1; i < (int)mHalfedges.size(); i += 2)
        if (mHalfedges[i].is_valid())
            return edge_index(i >> 1);
588
    return edge_index(size_all_edges()); // end index
Philip Trettner's avatar
Philip Trettner committed
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
}

inline edge_index Mesh::prev_valid_idx_from(edge_index idx) const
{
    for (auto i = idx.value << 1; i >= 0; i -= 2)
        if (mHalfedges[i].is_valid())
            return edge_index(i >> 1);
    return {}; // invalid
}

inline face_index Mesh::next_valid_idx_from(face_index idx) const
{
    for (auto i = idx.value; i < (int)mFaces.size(); ++i)
        if (mFaces[i].is_valid())
            return face_index(i);
604
    return face_index(size_all_faces()); // end index
Philip Trettner's avatar
Philip Trettner committed
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
}

inline face_index Mesh::prev_valid_idx_from(face_index idx) const
{
    for (auto i = idx.value; i >= 0; --i)
        if (mFaces[i].is_valid())
            return face_index(i);
    return {}; // invalid
}

inline halfedge_index Mesh::next_valid_idx_from(halfedge_index idx) const
{
    for (auto i = idx.value; i < (int)mHalfedges.size(); ++i)
        if (mHalfedges[i].is_valid())
            return halfedge_index(i);
620
    return halfedge_index(size_all_halfedges()); // end index
Philip Trettner's avatar
Philip Trettner committed
621
622
623
624
625
626
627
628
629
630
}

inline halfedge_index Mesh::prev_valid_idx_from(halfedge_index idx) const
{
    for (auto i = idx.value; i >= 0; --i)
        if (mHalfedges[i].is_valid())
            return halfedge_index(i);
    return {}; // invalid
}

Philip Trettner's avatar
Philip Trettner committed
631
632
inline void Mesh::connect_prev_next(halfedge_index prev, halfedge_index next)
{
Philip Trettner's avatar
Philip Trettner committed
633
634
    auto &prev_ref = halfedge(prev);
    auto &next_ref = halfedge(next);
Philip Trettner's avatar
Philip Trettner committed
635
636
637
638
639

    prev_ref.next_halfedge = next;
    next_ref.prev_halfedge = prev;
}

Philip Trettner's avatar
Philip Trettner committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
inline vertex_index Mesh::face_split(face_index f)
{
    // TODO: can be made more performant

    auto h_begin = face(f).halfedge;

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

662
663
        // NOTE: add_face inserted a new halfedge
        h = halfedge(opposite(halfedge(h).next_halfedge)).next_halfedge;
Philip Trettner's avatar
Philip Trettner committed
664
665
666
667
668
    } while (h != h_begin);

    return vs[0];
}

Philip Trettner's avatar
Philip Trettner committed
669
inline vertex_index Mesh::edge_split(edge_index e)
Philip Trettner's avatar
Philip Trettner committed
670
{
Philip Trettner's avatar
Philip Trettner committed
671
672
673
674
675
676
677
678
679
    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
680
681

    // add vertex
Philip Trettner's avatar
Philip Trettner committed
682
    auto v = add_vertex();
Philip Trettner's avatar
Philip Trettner committed
683
684

    // add two new edges
Philip Trettner's avatar
Philip Trettner committed
685
686
687
688
689
690
691
692
693
694
695
696
    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
697

Philip Trettner's avatar
Philip Trettner committed
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
    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
730
731
732
733
734
735
736
737
738
739
740
741
    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
742
743
744
745
746
747
748
749

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

    return v;
Philip Trettner's avatar
Philip Trettner committed
750
751
}

Philip Trettner's avatar
Philip Trettner committed
752
inline vertex_index Mesh::halfedge_split(halfedge_index h)
Philip Trettner's avatar
Philip Trettner committed
753
{
Philip Trettner's avatar
Philip Trettner committed
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
    // 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
788

Philip Trettner's avatar
Philip Trettner committed
789
790
    connect_prev_next(h1_prev, h3);
    connect_prev_next(h3, h1);
Philip Trettner's avatar
Philip Trettner committed
791

Philip Trettner's avatar
Philip Trettner committed
792
793
794
795
796
797
798
799
800
801
802
    // 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
803
804
}

805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
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
822
        // fix face boundary
823
824
825
        if (is_boundary(opposite(h)))
            f_ref.halfedge = h;

Philip Trettner's avatar
Philip Trettner committed
826
827
828
829
830
        // fix adj face boundary
        auto adj_face = halfedge(opposite(h)).face;
        if (adj_face.is_valid())
            fix_boundary_state_of(adj_face);

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
        // 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
864
    vertex(v).outgoing_halfedge = h1;
Philip Trettner's avatar
Philip Trettner committed
865

866
867
868
869
870
    connect_prev_next(h, h0);
    connect_prev_next(h0, h1);
    connect_prev_next(h1, h_next);
}

Philip Trettner's avatar
Philip Trettner committed
871
872
873
inline void Mesh::vertex_collapse(vertex_index v)
{
    auto &v_ref = vertex(v);
Philip Trettner's avatar
Philip Trettner committed
874

Philip Trettner's avatar
Philip Trettner committed
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
    // 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
890

Philip Trettner's avatar
Philip Trettner committed
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
        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
917

Philip Trettner's avatar
Philip Trettner committed
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
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;
933

Philip Trettner's avatar
Philip Trettner committed
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
    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
959
960
    connect_prev_next(h1_prev, h0_next);
    connect_prev_next(h0_prev, h1_next);
Philip Trettner's avatar
Philip Trettner committed
961

962
963
    connect_prev_next(h0_next, h1);
    connect_prev_next(h1_next, h0);
Philip Trettner's avatar
Philip Trettner committed
964

965
966
    connect_prev_next(h0, h0_next_next);
    connect_prev_next(h1, h1_next_next);
Philip Trettner's avatar
Philip Trettner committed
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987

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

Philip Trettner's avatar
Philip Trettner committed
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
    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
1016
1017
    connect_prev_next(h0_prev, h1_next);
    connect_prev_next(h1_prev, h0_next);
Philip Trettner's avatar
Philip Trettner committed
1018

1019
1020
    connect_prev_next(h1, h0_prev);
    connect_prev_next(h0, h1_prev);
Philip Trettner's avatar
Philip Trettner committed
1021

1022
1023
    connect_prev_next(h0_prev_prev, h0);
    connect_prev_next(h1_prev_prev, h1);
Philip Trettner's avatar
Philip Trettner committed
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
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

    // 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
1060
1061
1062
    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
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
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

    // 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
1100
1101
1102
    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
1103
1104
1105
1106
1107
1108

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

1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
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
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
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)
        std::swap(mVertices[t.first], mVertices[t.second]);

    // fix half-edges
    for (auto &h : mHalfedges)
        if (h.to_vertex.is_valid())
            h.to_vertex.value = p[h.to_vertex.value];

    // 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)
        std::swap(mFaces[t.first], mFaces[t.second]);

    // fix half-edges
    for (auto &h : mHalfedges)
        if (h.face.is_valid())
            h.face.value = p[h.face.value];

    // 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)
        std::swap(mHalfedges[t.first], mHalfedges[t.second]);

    // fix half-edges
    for (auto &v : mVertices)
        if (v.outgoing_halfedge.value >= 0)
            v.outgoing_halfedge.value = hp[v.outgoing_halfedge.value];

    for (auto &f : mFaces)
        if (f.halfedge.value >= 0)
            f.halfedge.value = hp[f.halfedge.value];

    for (auto &h : mHalfedges)
    {
        if (h.next_halfedge.value >= 0)
            h.next_halfedge.value = hp[h.next_halfedge.value];

        if (h.prev_halfedge.value >= 0)
            h.prev_halfedge.value = hp[h.prev_halfedge.value];
    }

    // 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
1202
1203
1204
1205
1206
1207
inline void Mesh::compactify()
{
    if (is_compact())
        return;

    // calculate remappings
1208
1209
1210
1211
    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
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
    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)
        if (mVertices[i].is_valid())
        {
Philip Trettner's avatar
Philip Trettner committed
1227
            v_old_to_new[i] = (int)v_new_to_old.size();
Philip Trettner's avatar
Philip Trettner committed
1228
1229
1230
1231
1232
1233
            v_new_to_old.push_back(i);
        }

    for (auto i = 0; i < f_cnt; ++i)
        if (mFaces[i].is_valid())
        {
Philip Trettner's avatar
Philip Trettner committed
1234
            f_old_to_new[i] = (int)f_new_to_old.size();
Philip Trettner's avatar
Philip Trettner committed
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
            f_new_to_old.push_back(i);
        }

    for (auto i = 0; i < e_cnt; ++i)
        if (mHalfedges[i << 1].is_valid())
            e_new_to_old.push_back(i);

    for (auto i = 0; i < h_cnt; ++i)
        if (mHalfedges[i].is_valid())
        {
Philip Trettner's avatar
Philip Trettner committed
1245
            h_old_to_new[i] = (int)h_new_to_old.size();
Philip Trettner's avatar
Philip Trettner committed
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
            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)
        mVertices[i] = mVertices[v_new_to_old[i]];
    for (auto i = 0u; i < f_new_to_old.size(); ++i)
        mFaces[i] = mFaces[f_new_to_old[i]];
    for (auto i = 0u; i < h_new_to_old.size(); ++i)
        mHalfedges[i] = mHalfedges[h_new_to_old[i]];

    mVertices.resize(v_new_to_old.size());
    mFaces.resize(f_new_to_old.size());
    mHalfedges.resize(h_new_to_old.size());

    for (auto &v : mVertices)
    {
        if (v.outgoing_halfedge.value >= 0)
            v.outgoing_halfedge.value = h_old_to_new[v.outgoing_halfedge.value];
    }

    for (auto &f : mFaces)
    {
        if (f.halfedge.value >= 0)
            f.halfedge.value = h_old_to_new[f.halfedge.value];
    }

    for (auto &h : mHalfedges)
    {
        if (h.next_halfedge.value >= 0)
            h.next_halfedge.value = h_old_to_new[h.next_halfedge.value];
        if (h.prev_halfedge.value >= 0)
            h.prev_halfedge.value = h_old_to_new[h.prev_halfedge.value];
        if (h.face.value >= 0)
            h.face.value = f_old_to_new[h.face.value];
        if (h.to_vertex.value >= 0)
            h.to_vertex.value = v_old_to_new[h.to_vertex.value];
    }

    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
    mVertices.shrink_to_fit();
    mFaces.shrink_to_fit();
    mHalfedges.shrink_to_fit();

    for (auto a = mVertexAttrs; a; a = a->mNextAttribute)
1301
        a->resize(size_all_vertices(), true);
Philip Trettner's avatar
Philip Trettner committed
1302
    for (auto a = mFaceAttrs; a; a = a->mNextAttribute)
1303
        a->resize(size_all_faces(), true);
Philip Trettner's avatar
Philip Trettner committed
1304
    for (auto a = mEdgeAttrs; a; a = a->mNextAttribute)
1305
        a->resize(size_all_edges(), true);
Philip Trettner's avatar
Philip Trettner committed
1306
    for (auto a = mHalfedgeAttrs; a; a = a->mNextAttribute)
1307
        a->resize(size_all_halfedges(), true);
Philip Trettner's avatar
Philip Trettner committed
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327

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

inline void Mesh::clear()
{
    for (auto &v : mVertices)
        v.set_removed();
    for (auto &h : mHalfedges)
        h.set_removed();
    for (auto &f : mFaces)
        f.set_removed();

    mCompact = false;
    compactify();
}

Philip Trettner's avatar
Philip Trettner committed
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
inline void Mesh::copy_from(const Mesh &m)
{
    // copy topo
    mVertices = m.mVertices;
    mFaces = m.mFaces;
    mHalfedges = m.mHalfedges;

    // 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
1359
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
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
inline void Mesh::reserve_faces(int capacity)
{
    for (auto a = mFaceAttrs; a; a = a->mNextAttribute)
        a->resize(capacity, false);

    mFaces.reserve(capacity);
}

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

    mVertices.reserve(capacity);
}

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);

    mHalfedges.reserve(capacity * 2);
}

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);

    mHalfedges.reserve(capacity);
}
}