Subversion Repositories gelsvn

Rev

Rev 630 | Rev 633 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
595 jab 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
561 awk 6
#include <iterator>
39 bj 7
#include "Manifold.h"
8
 
595 jab 9
#include <iostream>
10
#include <vector>
11
#include <map>
12
#include <iterator>
39 bj 13
 
601 jab 14
#include "../Geometry/TriMesh.h"
595 jab 15
 
39 bj 16
namespace HMesh
17
{
336 jab 18
 
595 jab 19
    using namespace std;
20
    using namespace Geometry;
21
    using namespace CGLA;
178 bj 22
 
595 jab 23
    namespace
24
    {
25
        /************************************************************
26
		 * Edgekeys and Edges for halfedge identification during build
27
		 ************************************************************/
28
        class EdgeKey
29
        {
30
        public:
31
            EdgeKey(VertexID va, VertexID vb)
32
            {
33
                if(va < vb){
34
                    v0 = va;
35
                    v1 = vb;
36
                }
37
                else{
38
                    v0 = vb;
39
                    v1 = va;
40
                }
41
            }
42
 
43
            bool operator<(const EdgeKey& k2) const
44
            {
45
                if(v0 < k2.v0){
46
                    return true;
47
                }
48
                else if( k2.v0 < v0){
49
                    return false;
50
                }
51
                else{
52
                    return v1 < k2.v1;
53
                }
54
            }
55
        private:
56
            VertexID v0;
57
            VertexID v1;
58
        };
336 jab 59
 
595 jab 60
        struct Edge
61
        {
62
            HalfEdgeID h0;
63
            HalfEdgeID h1;
64
            int count;
65
            Edge() : count(0){}
66
        };
67
    }
336 jab 68
 
595 jab 69
    /*********************************************
70
	 * Public functions
71
	 *********************************************/
72
    void Manifold::build(const TriMesh& mesh)
73
    {
74
        // A vector of 3's - used to tell build how many indices each face consists of
75
        vector<int> faces(mesh.geometry.no_faces(), 3);
336 jab 76
 
595 jab 77
        build_template( static_cast<size_t>(mesh.geometry.no_vertices()),
78
					   reinterpret_cast<const float*>(&mesh.geometry.vertex(0)),
79
					   static_cast<size_t>(faces.size()),
80
					   static_cast<const int*>(&faces[0]),
81
					   reinterpret_cast<const int*>(&mesh.geometry.face(0)));
82
    }
83
 
84
    void Manifold::build(   size_t no_vertices,
85
						 const float* vertvec,
86
						 size_t no_faces,
87
						 const int* facevec,
88
						 const int* indices)
89
    {
90
        build_template(no_vertices, vertvec, no_faces, facevec, indices);
91
    }
92
 
93
    void Manifold::build(   size_t no_vertices,
94
						 const double* vertvec,
95
						 size_t no_faces,
96
						 const int* facevec,
97
						 const int* indices)
98
    {
99
        build_template(no_vertices, vertvec, no_faces, facevec, indices);
100
    }
101
 
102
    FaceID Manifold::add_face(std::vector<Manifold::Vec> points)
103
    {
104
        int F = points.size();
105
        vector<int> indices;
605 jab 106
        for(size_t i=0;i<points.size(); ++i)
595 jab 107
            indices.push_back(i);
108
        FaceID fid = *faces_end();
109
        build(points.size(), reinterpret_cast<double*>(&points[0]), 1, &F, &indices[0]);
110
        return fid;
111
    }
112
 
600 jab 113
    bool Manifold::remove_face(FaceID fid)
114
    {
115
        if(!in_use(fid))
116
            return false;
117
 
118
        HalfEdgeID he = kernel.last(fid);
119
        HalfEdgeID last = he;
120
 
121
        vector<HalfEdgeID> halfedges;
122
        do
123
        {
124
            halfedges.push_back(he);
125
            kernel.set_face(he, InvalidFaceID);
126
            he = kernel.next(he);
127
        }
128
        while(he != last);
129
 
130
        vector<HalfEdgeID> halfedges_garbage;
131
        vector<VertexID> vertices_garbage;
605 jab 132
        for(size_t i=0;i<halfedges.size(); ++i)
600 jab 133
        {
134
            HalfEdgeID h = halfedges[i];
135
            HalfEdgeID hopp = kernel.opp(h);
136
            if(kernel.face(hopp) == InvalidFaceID)
137
            {
138
                halfedges_garbage.push_back(h);
139
                VertexID v0 = kernel.vert(hopp);
140
                if(valency(*this, v0) <= 2 )
141
                    vertices_garbage.push_back(v0);
142
                else {
143
                    link(kernel.prev(h), kernel.next(hopp));
144
                    kernel.set_out(v0, kernel.opp(kernel.prev(h)));
145
                }
146
                VertexID v1 = kernel.vert(h);
147
                if(valency(*this, v1)>2)
148
                {
149
                    link(kernel.prev(hopp),kernel.next(h));
150
                    kernel.set_out(v1, kernel.opp(kernel.prev(hopp)));
151
                }
152
 
153
            }
154
        }
605 jab 155
        for(size_t i=0;i<halfedges_garbage.size();++i){
600 jab 156
            kernel.remove_halfedge(kernel.opp(halfedges_garbage[i]));
157
            kernel.remove_halfedge(halfedges_garbage[i]);
158
        }
605 jab 159
        for(size_t i=0;i<vertices_garbage.size(); ++i)
600 jab 160
            kernel.remove_vertex(vertices_garbage[i]);
161
 
162
        kernel.remove_face(fid);
163
        return true;
164
    }
165
 
595 jab 166
 
600 jab 167
    bool Manifold::remove_edge(HalfEdgeID hid)
168
    {
169
        if(!in_use(hid))
170
            return false;
171
 
172
        FaceID f0 = kernel.face(hid);
173
        FaceID f1 = kernel.face(kernel.opp(hid));
174
 
175
        remove_face(f0);
176
        remove_face(f1);
177
 
178
        return true;
179
    }
180
 
595 jab 181
 
600 jab 182
 
183
    bool Manifold::remove_vertex(VertexID vid)
184
    {
185
        if(!in_use(vid))
186
            return false;
187
 
188
        Walker w = Manifold::walker(vid);
189
 
190
        vector<FaceID> faces;
191
 
192
        for(; !w.full_circle(); w = w.circulate_vertex_ccw())
193
            faces.push_back(w.face());
194
 
605 jab 195
        for(size_t i=0;i<faces.size();++i)
600 jab 196
            remove_face(faces[i]);
197
 
198
        return true;
199
    }
200
 
201
 
336 jab 202
 
595 jab 203
    void Manifold::collapse_edge(HalfEdgeID h, bool avg_vertices)
204
    {
205
        HalfEdgeID ho = kernel.opp(h);
206
        VertexID hv = kernel.vert(h);
207
        VertexID hov = kernel.vert(ho);
208
        HalfEdgeID hn = kernel.next(h);
209
        HalfEdgeID hp = kernel.prev(h);
210
        HalfEdgeID hon = kernel.next(ho);
211
        HalfEdgeID hop = kernel.prev(ho);
212
		FaceID f = kernel.face(h);
213
		FaceID fo = kernel.face(ho);
336 jab 214
 
595 jab 215
        // average the vertex positions
216
        pos(hv) = avg_vertices ? (0.5f * (pos(hov) + pos(hv))) : pos(hv);
336 jab 217
 
595 jab 218
        // update all halfedges pointing to hov to point to hv, effectively removing hov from all loops
219
        HalfEdgeID he = kernel.out(hov);
220
        HalfEdgeID last = he;
221
		do {
222
			assert(kernel.vert(kernel.opp(he)) == hov);
223
            kernel.set_vert(kernel.opp(he), hv);
224
            he = kernel.next(kernel.opp(he));
225
        }
226
		while(he != last);
227
        kernel.set_out(hv, hn);
336 jab 228
 
595 jab 229
        // link hp and hn, effectively removing h from opposite loop
230
        link(hp, hn);
231
        // make face owning h own hn instead
232
        if(kernel.face(h) != InvalidFaceID)
233
            kernel.set_last(f, hn);
336 jab 234
 
595 jab 235
        // link hop and hon, effectively removing h from opposite face loop
236
        link(hop, hon);
237
        // make opposite face owning h own hon instead
238
        if(kernel.face(ho) != InvalidFaceID)
239
            kernel.set_last(fo, hon);
336 jab 240
 
595 jab 241
        // remove the obsolete entities
242
        kernel.remove_vertex(hov);
243
        kernel.remove_halfedge(h);
244
        kernel.remove_halfedge(ho);
336 jab 245
 
595 jab 246
        // verify that remaining faces haven't become degenerate because of collapse
247
        remove_face_if_degenerate(hn);
248
        remove_face_if_degenerate(hon);
336 jab 249
 
595 jab 250
        // verify that v is sane after collapse
251
        ensure_boundary_consistency(hv);
252
    }
336 jab 253
 
595 jab 254
    FaceID Manifold::split_face_by_edge(FaceID f, VertexID v0, VertexID v1)
255
    {
256
		assert(!connected(*this, v0, v1));
257
        HalfEdgeID he = kernel.last(f);
258
        HalfEdgeID last = he;
259
        int steps = 0;
260
        while(he != last || steps == 0){
261
            ++steps;
262
            he = kernel.next(he);
263
        }
336 jab 264
 
595 jab 265
        // make sure this is not a triangle
266
        assert(steps > 3);
267
        // make sure we are not trying to connect a vertex to itself
268
        assert(v0 != v1);
336 jab 269
 
595 jab 270
		HalfEdgeID h0 = kernel.out(v0);
271
		for(Walker w = walker(v0); !w.full_circle(); w = w.circulate_vertex_cw()){
272
			if(w.face() == f){
273
				h0 = w.halfedge();
274
				break;
39 bj 275
			}
336 jab 276
		}
595 jab 277
		assert(kernel.face(h0) != InvalidFaceID);
278
        assert(kernel.face(h0) == f);
336 jab 279
 
595 jab 280
        // the halfedge belonging to f, going out from v0, is denoted h. Move along h until we hit v1.
281
        // now we have the halfedge which belongs to f and points to v1.
282
        HalfEdgeID h = h0;
283
        while(kernel.vert(h) != v1){
284
            h = kernel.next(h);
285
        }
286
        assert(h != h0);
336 jab 287
 
595 jab 288
        // create a new halfedge ha which connects v1 and v0 closing the first loop.
289
        HalfEdgeID h1 = kernel.next(h);
290
        HalfEdgeID ha = kernel.add_halfedge();
291
        link(h, ha);
292
        link(ha, h0);
293
        kernel.set_face(ha, f);
294
        kernel.set_vert(ha, v0);
295
        kernel.set_last(f, ha);
336 jab 296
 
595 jab 297
        // create a new face, f2, and set all halfedges in the remaining part of the polygon to point to this face.
298
        h = h1;
299
        FaceID f2 = kernel.add_face();
300
        while(kernel.vert(h) != v0){
301
            kernel.set_face(h, f2);
302
            h = kernel.next(h);
303
        }
304
        kernel.set_face(h, f2);
305
        assert(h != h1);
336 jab 306
 
595 jab 307
        // create a new halfedge hb to connect v0 and v1.
308
        HalfEdgeID hb = kernel.add_halfedge();
309
        link(h, hb);
310
        link(hb, h1);
311
        kernel.set_face(hb, f2);
312
        kernel.set_vert(hb, v1);
313
        kernel.set_last(f2, hb);
336 jab 314
 
595 jab 315
        // complete the operation by gluing the two new halfedges
316
        glue(ha, hb);
336 jab 317
 
595 jab 318
        // assert sanity of operation
319
        assert(kernel.next(kernel.opp(kernel.prev(h1))) == h0);
320
        assert(kernel.next(kernel.opp(kernel.prev(h0))) == h1);
321
        assert(kernel.face(kernel.next(hb)) == f2);
322
        assert(kernel.face(kernel.next(kernel.next(hb))) == f2);
323
        assert(kernel.face(hb) == f2);
336 jab 324
 
595 jab 325
        // return handle to newly created face
326
        return f2;
327
    }
336 jab 328
 
595 jab 329
    VertexID Manifold::split_face_by_vertex(FaceID f)
330
    {
331
        //create the new vertex, with the barycenter of the face as position
332
        Manifold::Vec p(0.0f);
333
        HalfEdgeID last_he = kernel.last(f);
334
        HalfEdgeID he = last_he;
335
        int steps = 0;
336 jab 336
 
595 jab 337
        while(he != last_he || steps == 0){
338
            p += positions[kernel.vert(he)];
339
            ++steps;
340
            he = kernel.next(he);
341
        }
336 jab 342
 
595 jab 343
        p /= steps;
336 jab 344
 
595 jab 345
        VertexID v = kernel.add_vertex();
346
        positions[v]  = p;
336 jab 347
 
595 jab 348
        //circulate the face, create halfedges and connect to vertex
349
        vector<HalfEdgeID> eout;
350
        vector<HalfEdgeID> ein;
351
        last_he = kernel.last(f);
352
        he = last_he;
336 jab 353
 
595 jab 354
        do{
355
            HalfEdgeID hn = kernel.next(he);
356
 
357
            HalfEdgeID ho = kernel.add_halfedge();
358
            HalfEdgeID hi = kernel.add_halfedge();
359
 
360
            FaceID fn = kernel.add_face();
361
            kernel.set_face(hi, fn);
362
            kernel.set_vert(hi, v);
363
            kernel.set_face(ho, fn);
364
            kernel.set_vert(ho, kernel.vert(kernel.opp(he)));
365
            kernel.set_face(he, fn);
366
            kernel.set_last(fn, ho);
367
 
368
            link(hi, ho);
369
            link(ho, he);
370
            link(he, hi);
371
 
372
            eout.push_back(ho);
373
            ein.push_back(hi);
374
 
375
            he = hn;
376
        }
377
        while(he != last_he);
336 jab 378
 
595 jab 379
        // glue new halfedges together
380
        size_t N = eout.size();
381
        for(size_t i = 0; i < N; ++i){
382
            glue(ein[i], eout[(i+1)%N]);
383
        }
384
        kernel.set_out(v, eout[0]);
336 jab 385
 
595 jab 386
        //remove the now replaced face
387
        kernel.remove_face(f);
336 jab 388
 
595 jab 389
        return v;
390
    }
391
    VertexID Manifold::split_edge(HalfEdgeID h)
392
    {
393
        HalfEdgeID ho = kernel.opp(h);
394
        VertexID v = kernel.vert(h);
395
        VertexID vo = kernel.vert(ho);
336 jab 396
 
595 jab 397
        //create the new vertex with middle of edge as position and update connectivity
398
        VertexID vn = kernel.add_vertex();
399
        positions[vn] = .5f * (positions[v] + positions[vo]);
400
        kernel.set_out(vn, h);
336 jab 401
 
595 jab 402
        //create two necessary halfedges, and update connectivity
403
        HalfEdgeID hn = kernel.add_halfedge();
404
        HalfEdgeID hno = kernel.add_halfedge();
336 jab 405
 
595 jab 406
        kernel.set_out(vo, hn);
631 janba 407
        kernel.set_out(v, hno);
336 jab 408
 
631 janba 409
        glue(h, hno);
410
        glue(hn, ho);
336 jab 411
 
595 jab 412
        link(kernel.prev(h), hn);
413
        link(hn, h);
414
        kernel.set_vert(hn, vn);
336 jab 415
 
631 janba 416
        link(kernel.prev(ho), hno);
417
        link(hno, ho);
418
        kernel.set_vert(hno, vn);
336 jab 419
 
595 jab 420
        // update faces in case of non boundary edge
421
        if(kernel.face(h) != InvalidFaceID)
422
            kernel.set_last(kernel.face(h), hn);
336 jab 423
 
595 jab 424
        if(kernel.face(ho) != InvalidFaceID)
425
            kernel.set_last(kernel.face(ho), ho);
336 jab 426
 
595 jab 427
        // update new edge with faces from existing edge
428
        kernel.set_face(hn, kernel.face(h));
429
        kernel.set_face(hno, kernel.face(ho));
336 jab 430
 
595 jab 431
        //if split occurs on a boundary, consistency must be ensured.
432
        ensure_boundary_consistency(vn);
433
        ensure_boundary_consistency(v);
434
        ensure_boundary_consistency(vo);
336 jab 435
 
595 jab 436
        return vn;
437
    }
438
 
439
    size_t link_intersection(const Manifold& m, VertexID v0, VertexID v1, vector<VertexID>& lisect)
440
    {
441
        // get the one-ring of v0
442
        vector<VertexID> link0;
443
        for(Walker vj = m.walker(v0);
444
			!vj.full_circle(); vj = vj.circulate_vertex_ccw())
445
            link0.push_back(vj.vertex());
336 jab 446
 
595 jab 447
        // get the one-ring of v1
448
        vector<VertexID> link1;
449
        for(Walker vj = m.walker(v1);
450
			!vj.full_circle(); vj = vj.circulate_vertex_ccw())
451
            link1.push_back(vj.vertex());
336 jab 452
 
595 jab 453
        // sort the vertices of the two rings
454
        sort(link0.begin(), link0.end());
455
        sort(link1.begin(), link1.end());
336 jab 456
 
595 jab 457
        // get the intersection of the shared vertices from both rings
458
        std::back_insert_iterator<vector<VertexID> > lii(lisect);
459
        set_intersection(link0.begin(), link0.end(),
460
						 link1.begin(), link1.end(),
461
						 lii);
462
 
463
        return lisect.size();
464
    }
465
 
466
    bool Manifold::stitch_boundary_edges(HalfEdgeID h0, HalfEdgeID h1)
467
    {
468
        // Cannot stitch an edge with itself
469
        if(h0 == h1)
470
            return false;
471
 
472
        // Only stitch a pair of boundary edges.
473
        if(kernel.face(h0) == InvalidFaceID && kernel.face(h1) == InvalidFaceID)
474
        {
475
            HalfEdgeID h0o = kernel.opp(h0);
476
            HalfEdgeID h1o = kernel.opp(h1);
477
            VertexID v0a = kernel.vert(h0);
478
            VertexID v0b = kernel.vert(kernel.opp(h1));
479
            VertexID v1b = kernel.vert(h1);
480
            VertexID v1a = kernel.vert(kernel.opp(h0));
481
 
482
            // Case below implies that h0 and h1 are the same edge with different ID
483
            // That should not happen.
484
            assert(!(v0a == v0b && v1a == v1b));
485
 
486
            if(connected(*this, v0a, v0b))
487
                return false;
488
            if(connected(*this, v1a, v1b))
489
                return false;
490
 
491
 
492
            if(v0b != v0a)
493
            {
494
 
495
                // Check the link intersection v0a and v0b are welded together
496
                // if they share a neighbouring vertex, it will appear twice in the combined
497
                // one ring unless it v1a and v1a==v1b
498
                vector<VertexID> lisect;
499
                if(link_intersection(*this, v0a, v0b, lisect))
500
                {
501
                    vector<VertexID>::iterator iter;
502
 
503
                    if(v1a == v1b)
504
                    {
505
                        iter = find(lisect.begin(), lisect.end(), v1a);
506
                        if(iter != lisect.end())
507
                            lisect.erase(iter);
508
                    }
509
                    iter = find(lisect.begin(), lisect.end(), kernel.vert(kernel.next(h0)));
510
                    if(iter != lisect.end())
511
                        lisect.erase(iter);
512
                    if(lisect.size() > 0)
513
                        return false;
514
                }
515
            }
516
 
517
            if(v1a != v1b)
518
            {
519
                // Check the same for the other endpoints.
520
                vector<VertexID> lisect;
521
                if(link_intersection(*this, v1a, v1b, lisect))
522
                {
523
                    vector<VertexID>::iterator iter;
524
 
525
                    if(v0a == v0b)
526
                    {
527
                        iter = find(lisect.begin(), lisect.end(), v1a);
528
                        if(iter != lisect.end())
529
                            lisect.erase(iter);
530
                    }
531
                    iter = find(lisect.begin(), lisect.end(), kernel.vert(kernel.next(h1)));
532
                    if(iter != lisect.end())
533
                        lisect.erase(iter);
534
                    if(lisect.size() > 0)
535
                        return false;
536
                }
537
            }
538
 
539
 
540
            if(v0b != v0a)
541
            {
542
                Walker hew = walker(v0b);
543
                for(;!hew.full_circle(); hew = hew.circulate_vertex_ccw())
544
                    kernel.set_vert(hew.opp().halfedge(), v0a);
545
            }
546
 
547
            if(v1b != v1a)
548
            {
549
                Walker hew = walker(v1b);
550
                for(;!hew.full_circle(); hew = hew.circulate_vertex_ccw())
551
                    kernel.set_vert(hew.opp().halfedge(), v1a);
552
            }
553
 
554
            if(v0a != v0b)
555
            {
556
                HalfEdgeID h1p = kernel.prev(h1);
557
                HalfEdgeID h0n = kernel.next(h0);
558
 
559
                if(kernel.next(h0n) == h1p)
560
                {
561
                    glue(kernel.opp(h0n), kernel.opp(h1p));
562
                    kernel.set_out(kernel.vert(h0n),kernel.opp(h0n));
563
                    kernel.remove_halfedge(h0n);
564
                    kernel.remove_halfedge(h1p);
565
                }
566
                else
567
                    link(h1p, h0n);
568
                kernel.remove_vertex(v0b);
569
            }
570
 
571
            if(v1a != v1b)
572
            {
573
                HalfEdgeID h0p = kernel.prev(h0);
574
                HalfEdgeID h1n = kernel.next(h1);
575
                if(kernel.next(h1n) == h0p)
576
                {
577
                    glue(kernel.opp(h0p), kernel.opp(h1n));
578
                    kernel.set_out(kernel.vert(h1n), kernel.opp(h1n));
579
                    kernel.remove_halfedge(h0p);
580
                    kernel.remove_halfedge(h1n);
581
                }
582
                else
583
                    link(h0p, h1n);
584
                kernel.remove_vertex(v1b);
585
            }
586
            glue(h0o, h1o);
587
 
588
            kernel.remove_halfedge(h0);
589
            kernel.remove_halfedge(h1);
590
 
591
            kernel.set_out(v1a, h1o);
592
            kernel.set_out(v0a, h0o);
593
 
594
            ensure_boundary_consistency(v1a);
595
            ensure_boundary_consistency(v0a);
596
 
597
            return true;
598
        }
599
        return false;
600
    }
601
 
602
 
603
 
604
    FaceID Manifold::merge_one_ring(VertexID v, float max_loop_length)
605
    {
606
        // If the vertex is either not in use or has just
607
        // one incident edge (or less), bail out.
608
        int val = valency(*this,v);
609
        if(!in_use(v) || val<2)
610
            return InvalidFaceID;
611
 
612
        // If the vertex is  a boundary vertex, we preparte the walker so that the
613
        // first face visited is not the invalid face outside the boundary. If the boundary
614
        // vertex is adjacent to only one vertex, there is little to do and we bail out.
615
        bool vertex_is_boundary = false;
616
        Walker hew = walker(v);
617
        if(boundary(*this, v))
618
        {
619
            if(val==2) return InvalidFaceID;
620
            vertex_is_boundary = true;
621
            hew = hew.circulate_vertex_ccw();
622
        }
623
 
624
        // Prepare some vectors for taking out the trash: We remove all old faces and all orphaned edges
625
        // and vertices
626
        vector<HalfEdgeID> garbage_halfedges;
627
        vector<FaceID> garbage_faces;
628
        vector<VertexID> garbage_vertices;
629
 
630
        vector<HalfEdgeID> loop; // The halfedges which form the outer loop of the merged one ring.
631
 
632
        // Below we loop over all faces adjacent to the vertex and add their halfedges to a big loop
633
        // which will form the loop of the new merged face. Below we remove from the loop edges
634
        // that appear twice (as each other's opposite).
635
        for(;!hew.full_circle(); hew = hew.circulate_vertex_ccw())
636
        {
637
            garbage_faces.push_back(hew.face());
638
            for(Walker hew2 = walker(hew.halfedge());
639
                !hew2.full_circle(); hew2 = hew2.circulate_face_ccw())
640
                loop.push_back(hew2.halfedge());
641
        }
642
 
643
 
644
        // Now merge the loops. We iteratively remove pairs of adjacent halfedges from the loop
645
        // if we find that the second is the opposite of the first since this is a degenerate
646
        // situation. However, we stop at four remaining halfedges since otherwise the loop degenerates
647
        // to zero after the next step if these four are also pairwise each other's opposites.
648
        int did_work;
649
        do
650
        {
651
            did_work = 0;
652
            vector<HalfEdgeID> loop_tmp(0);
605 jab 653
            for(size_t i=0;i<loop.size();++i)
595 jab 654
                if(walker(loop[i]).opp().halfedge() == loop[(i+1)%loop.size()])
655
                {
656
                    VertexID vid = walker(loop[i]).vertex();
657
                    if(vid != v)
658
                        garbage_vertices.push_back(walker(loop[i]).vertex());
659
                    garbage_halfedges.push_back(loop[i]);
660
                    garbage_halfedges.push_back(loop[(i+1)%loop.size()]);
661
                    loop[i] = InvalidHalfEdgeID;
662
                    loop[(i+1)%loop.size()] = InvalidHalfEdgeID;
663
                    ++did_work;
664
                    ++i;
665
                }
605 jab 666
            for(size_t i=0;i<loop.size();++i)
595 jab 667
                if(loop[i] != InvalidHalfEdgeID)
668
                    loop_tmp.push_back(loop[i]);
669
            loop = loop_tmp;
670
        } while(did_work > 0 && loop.size() > 4);
671
 
672
        // Check whether the loop is too long
673
        float loop_len=0.0;
605 jab 674
        for(size_t i=0;i<loop.size();++i)
595 jab 675
            loop_len += length(*this, loop[i]);
676
        if(loop_len > max_loop_length)
677
            return InvalidFaceID;
678
 
679
        // The following block checks wheteher the same halfedge appears twice. In this
680
        // case we fail since it means that the one ring contains the same face twice.
681
        vector<HalfEdgeID> loop_tmp = loop;
682
        sort(loop_tmp.begin(), loop_tmp.end());
683
        vector<HalfEdgeID>::iterator end_iter = unique(loop_tmp.begin(), loop_tmp.end());
684
        if(end_iter != loop_tmp.end())
685
            return InvalidFaceID;
686
 
687
        // Remove all faces and connected halfedges and the original vertex v.
605 jab 688
        for(size_t i=0;i<garbage_vertices.size(); ++i)
595 jab 689
            kernel.remove_vertex(garbage_vertices[i]);
605 jab 690
        for(size_t i=0;i<garbage_faces.size(); ++i)
595 jab 691
            kernel.remove_face(garbage_faces[i]);
605 jab 692
        for(size_t i=0;i<garbage_halfedges.size(); ++i)
595 jab 693
            kernel.remove_halfedge(garbage_halfedges[i]);
694
        if(!vertex_is_boundary)
695
            kernel.remove_vertex(v);
696
 
697
        // Create a new face for the merged one ring and link up all the halfedges
698
        // in the loop
699
        FaceID f = kernel.add_face();
700
        kernel.set_last(f,loop[0]);
605 jab 701
        for(size_t i=0;i<loop.size(); ++i)
595 jab 702
        {
703
            kernel.set_face(loop[i], f);
704
            Walker hw = walker(loop[i]);
705
            kernel.set_out(hw.vertex(),hw.opp().halfedge());
706
            link(loop[i],loop[(i+1)%loop.size()]);
707
            assert(hw.vertex() == walker(loop[(i+1)%loop.size()]).opp().vertex());
708
        }
709
 
710
        // Finally, we ensure boundary consitency for all vertices in the loop.
605 jab 711
        for(size_t i=0;i<loop.size(); ++i)
595 jab 712
        {
713
            Walker hw = walker(loop[i]);
714
            ensure_boundary_consistency(hw.vertex());
715
        }
716
 
717
        // Return the brand new merged face.
718
        return f;
719
    }
720
 
721
 
722
 
723
    bool Manifold::merge_faces(FaceID f, HalfEdgeID h)
724
    {
725
        //assert that we're merging a valid face with the corresponding halfedge
726
        assert(kernel.face(h) == f);
727
 
728
        HalfEdgeID ho = kernel.opp(h);
729
        FaceID fo = kernel.face(ho);
730
        HalfEdgeID hn = kernel.next(h);
731
        HalfEdgeID hon = kernel.next(ho);
732
 
733
        if(fo == f)
734
            return false;
735
 
736
        //boundary case
737
        if(kernel.vert(hn) == kernel.vert(hon))
738
            return false;
739
 
740
        HalfEdgeID hp = kernel.prev(h);
741
        HalfEdgeID hop = kernel.prev(ho);
742
        VertexID v = kernel.vert(h);
743
        VertexID vo = kernel.vert(ho);
744
 
745
        if(valency(*this, v) < 3 || valency(*this, vo) < 3)
746
            return false;
747
 
748
        link(hop, hn);
749
        link(hp, hon);
750
        kernel.set_out(vo, hon);
751
        kernel.set_out(v, hn);
752
        kernel.set_last(f, hn);
753
 
754
        HalfEdgeID hx = hon;
755
 
756
        assert(kernel.face(hx) == fo);
757
        while(kernel.face(hx) != f){
758
            kernel.set_face(hx, f);
759
            hx = kernel.next(hx);
760
        }
761
 
762
        ensure_boundary_consistency(v);
763
        ensure_boundary_consistency(vo);
764
 
765
        kernel.remove_halfedge(h);
766
        kernel.remove_halfedge(ho);
767
        kernel.remove_face(fo);
768
 
769
        return true;
770
    }
771
 
772
    FaceID Manifold::close_hole(HalfEdgeID h)
773
    {
774
        // invalid face is a hole
775
        if(kernel.face(h) == InvalidFaceID){
776
            FaceID f = kernel.add_face();
777
            kernel.set_last(f, h);
778
            do{
779
                kernel.set_face(h, f);
780
                h = kernel.next(h);
781
            }
782
            while(kernel.face(h) != f);
783
            return f;
784
        }
785
        return kernel.face(h);
786
    }
787
    void Manifold::flip_edge(HalfEdgeID h)
788
    {
789
        HalfEdgeID hn = kernel.next(h);
790
        HalfEdgeID hp = kernel.prev(h);
791
        HalfEdgeID ho = kernel.opp(h);
792
        HalfEdgeID hon = kernel.next(ho);
793
        HalfEdgeID hop = kernel.prev(ho);
794
 
795
        FaceID hf = kernel.face(h);
796
        FaceID hof = kernel.face(ho);
797
 
798
        VertexID hv = kernel.vert(h);
799
        VertexID hnv = kernel.vert(hn);
800
        VertexID hov = kernel.vert(ho);
801
        VertexID honv = kernel.vert(hon);
802
 
803
        // update face connectivity of halfedges changing face
804
        kernel.set_face(hop, hf);
805
        kernel.set_face(hp, hof);
806
 
807
        // connectivity of faces with halfedges of flipped edge
808
        kernel.set_last(hf, h);
809
        kernel.set_last(hof, ho);
810
 
811
        // connectivity of halfedges of first face after flip
812
        link(hn, h);
813
        link(h, hop);
814
        link(hop, hn);
815
 
816
        // connectivity of halfedges of second face after flip
817
        link(hon, ho);
818
        link(ho, hp);
819
        link(hp, hon);
820
 
821
        // connectivity of flipped edge and corresponding vertices
822
        kernel.set_vert(h, honv);
823
        kernel.set_vert(ho, hnv);
824
 
825
        if(kernel.out(hv) == ho)
826
            kernel.set_out(hv, hn);
827
        if(kernel.out(hov) == h)
828
            kernel.set_out(hov, hon);
829
 
830
        //
831
        //        // if the flip occurs next to a boundary, ensure the boundary consistency
832
        //        ensure_boundary_consistency(hv);
833
        //        ensure_boundary_consistency(hov);
834
    }
835
 
836
 
837
    /**********************************************
838
     * Private functions
839
     **********************************************/
840
 
841
    template<typename size_type, typename float_type, typename int_type>
842
    void Manifold::build_template(  size_type no_vertices,
843
                                  const float_type* vertvec,
844
                                  size_type no_faces,
845
                                  const int_type* facevec,
846
                                  const int_type* indices)
847
    {
848
        vector<VertexID> vids(no_vertices);
849
 
850
        // create vertices corresponding to positions stored in vertvec
605 jab 851
        for(size_t i=0;i<no_vertices;++i)
595 jab 852
        {
853
            const float_type* v0 = &vertvec[i*3];
854
            pos(vids[i] = kernel.add_vertex()) = Manifold::Vec(v0[0], v0[1], v0[2]);
855
        }
856
 
857
        //map over the created edges - needed to preserve edge uniqueness
858
        typedef map<EdgeKey, Edge> EdgeMap;
859
        EdgeMap edge_map;
860
 
861
        // counter that jumps between faces in indices vector
862
        int_type n  = 0;
863
 
864
        // create faces correspponding to faces stored in facevec
865
        for(size_type i = 0; i < no_faces; ++i){
866
            //amount of vertices in current face
867
            size_type N = facevec[i];
868
            vector<HalfEdgeID> fh;
869
 
870
            //each face indice results corresponds to 1 edge, 2 halfedges
871
            for(size_type j = 0; j < N; ++j){
872
                // ensure indice integrity
873
 
874
                assert(static_cast<size_type>(indices[j + n]) < no_vertices);
875
                assert(static_cast<size_type>(indices[(j + 1) % N + n]) < no_vertices);
876
 
877
 
878
                // each iteration uses two indices from the face
879
                VertexID v0 = vids[static_cast<size_type>(indices[j + n])];
880
                VertexID v1 = vids[static_cast<size_type>(indices[(j + 1) % N + n])];
881
 
882
                // create key and search map for edge
883
                EdgeKey ek(v0, v1);
884
                EdgeMap::iterator em_iter = edge_map.find(ek);
885
 
886
                // current edge has not been created
887
                if(em_iter == edge_map.end()){
888
                    // create edge for map
889
                    Edge e;
890
                    e.h0 = kernel.add_halfedge();
891
                    e.h1 = kernel.add_halfedge();
892
                    e.count = 1;
893
 
894
                    // glue operation: 1 edge = 2 glued halfedges
895
                    glue(e.h0, e.h1);
896
 
897
                    // update vertices with their outgoing halfedges
898
                    kernel.set_out(v0, e.h0);
899
                    kernel.set_out(v1, e.h1);
900
 
901
                    // update halfedges with the vertices they point to
902
                    kernel.set_vert(e.h0, v1);
903
                    kernel.set_vert(e.h1, v0);
904
 
905
                    // update map
906
                    edge_map[ek] = e;
907
 
908
                    // update vector of halfedges belonging to current face
909
                    fh.push_back(e.h0);
910
                }
911
                else{
912
                    // update current face with halfedge from edge
913
                    fh.push_back(em_iter->second.h1);
914
 
915
                    // asserting that a halfedge is visited exactly twice;
916
                    // once for each face on either side of the edge.
917
                    em_iter->second.count++;
918
                    assert(em_iter->second.count == 2);
919
                }
920
            }
921
 
922
            FaceID fid = kernel.add_face();
923
            for(size_type j = 0; j < N; ++j){
924
                // update halfedge with face
925
                kernel.set_face(fh[j], fid);
926
 
927
                // link operation: link two halfedges in the same face
928
                link(fh[j], fh[(j + 1) % N]);
929
            }
930
            //update face with the first halfedge created
931
            kernel.set_last(fid, fh[0]);
932
 
933
            // step to first index of next face
934
            n += N;
935
        }
936
 
937
        // test for unused vertices
938
        for(VertexIDIterator v = vertices_begin(); v != vertices_end(); ++v){
939
            assert( (*v) != InvalidVertexID);
940
            if(kernel.out(*v) == InvalidHalfEdgeID)
941
                kernel.remove_vertex(*v);
942
        }
943
 
944
        // boundary check while avoiding unused vertices
945
        for(VertexIDIterator v = vertices_begin(); v != vertices_end(); ++v){
946
            if((*v) != InvalidVertexID)
947
                ensure_boundary_consistency(*v);
948
        }
949
    }
950
    void Manifold::link(HalfEdgeID h0, HalfEdgeID h1)
951
    {
952
        kernel.set_next(h0, h1);
953
        kernel.set_prev(h1, h0);
954
    }
955
    void Manifold::glue(HalfEdgeID h0, HalfEdgeID h1)
956
    {
957
        kernel.set_opp(h0, h1);
958
        kernel.set_opp(h1, h0);
959
    }
960
    void Manifold::ensure_boundary_consistency(VertexID v)
961
    {
962
        // boundary consistency check by performing two vertex circulations
963
        HalfEdgeID h = kernel.out(v);
964
        HalfEdgeID last = h;
965
 
966
        // step 1: circle through edges pointing away from vertex until reaching a null face
967
        while(kernel.face(h) != InvalidFaceID){
968
            h = kernel.opp(kernel.prev(h));
969
            if(h == last) // We came full circle - vertex not boundary - return.
970
                return;
971
        }
972
        // null face encountered, we update our vertex with half edge index and prepare for step 2
973
        kernel.set_out(v, h);
974
        HalfEdgeID ho = kernel.opp(h);
975
 
976
        // step 2: circle through edges pointing towards vertex until reaching a null face
977
        while(kernel.face(ho) != InvalidFaceID){
978
            ho = kernel.opp(kernel.next(ho));
979
        }
980
        // null face encountered again, we update our edge with vertex index
981
        kernel.set_vert(ho, v);
982
 
983
        // remaining step is to make the in and out going edges link to each other
984
        link(ho, h);
985
    }
986
    void Manifold::remove_face_if_degenerate(HalfEdgeID h)
987
    {
988
        // face is degenerate if there is only two halfedges in face loop
989
        if(kernel.next(kernel.next(h)) == h)
990
        {
991
            HalfEdgeID hn = kernel.next(h);
992
            HalfEdgeID ho = kernel.opp(h);
993
            HalfEdgeID hno = kernel.opp(hn);
994
            VertexID hv = kernel.vert(h);
995
            VertexID hnv = kernel.vert(hn);
996
            FaceID f = kernel.face(h);
997
 
998
            assert(ho != hn);
999
            assert(h != hno);
1000
            assert(hv != hnv);
1001
 
1002
            // glue opposite halfedge to halfedge opposite next halfedge, obsoleting h and hn from loop
1003
            glue(ho, hno);
1004
 
1005
            // make v and vn point to opposite and next opposite halfedge, obsoleting h and hn from loop
1006
            kernel.set_out(hnv, hno);
1007
            kernel.set_out(hv, ho);
1008
 
1009
            // if face owning h is valid, remove face
1010
            if(f != InvalidFaceID)
1011
                kernel.remove_face(f);
1012
            // remove the two invalid halfedges and the invalid face loop
1013
            kernel.remove_halfedge(h);
1014
            kernel.remove_halfedge(hn);
1015
 
1016
            // verify that v and vn is sane after removing the degenerate face
1017
            ensure_boundary_consistency(hv);
1018
            ensure_boundary_consistency(hnv);
1019
        }
1020
    }
1021
 
1022
    vector<HalfEdgeID> Manifold::bridge_faces(FaceID f0, FaceID f1, const vector<pair<VertexID, VertexID> >& pairs)
1023
    {
1024
        // Let N be the number of vertex pairs to connect by edges
1025
        size_t N = pairs.size();
1026
 
1027
        // We now create N pairs of edges, glue each pair and let them point to the appropriate
1028
        // vertices.
1029
        vector<HalfEdgeID> new_halfedges(N);
1030
        vector<HalfEdgeID> new_halfedges_opp(N);
605 jab 1031
        for(size_t i=0;i<N; ++i)
595 jab 1032
        {
1033
            new_halfedges[i] = kernel.add_halfedge();
1034
            new_halfedges_opp[i] = kernel.add_halfedge();
1035
            glue(new_halfedges[i], new_halfedges_opp[i]);
1036
            kernel.set_vert(new_halfedges[i], pairs[i].second);
1037
            kernel.set_vert(new_halfedges_opp[i], pairs[i].first);
1038
        }
1039
 
1040
        // We now maintain some halfedge indices to right before
1041
        // and right after the point we are trying to connect on
1042
        // each of the two loops.
1043
        HalfEdgeID h0p = kernel.last(f0);
1044
        HalfEdgeID h1n = kernel.last(f1);
1045
 
1046
        // loop over all connections and link the new halfedges with the old
1047
        // ones.
605 jab 1048
        for(size_t i=0;i<N; ++i)
595 jab 1049
        {
1050
            while(kernel.vert(h0p) != pairs[i].first)
1051
                h0p = kernel.next(h0p);
1052
            while(kernel.vert(kernel.prev(h1n)) != pairs[i].second)
1053
                h1n = kernel.prev(h1n);
1054
 
1055
            HalfEdgeID h0n = kernel.next(h0p);
1056
            HalfEdgeID h1p = kernel.prev(h1n);
1057
 
1058
            link(h0p, new_halfedges[i]);
1059
            link(new_halfedges[i],h1n);
1060
            link(h1p, new_halfedges_opp[i]);
1061
            link(new_halfedges_opp[i],h0n);
1062
 
1063
            h0p = new_halfedges_opp[i];
1064
            h1n = new_halfedges_opp[i];
1065
        }
1066
 
1067
        // Create the faces and their connections
605 jab 1068
        for(size_t i=0;i<N; ++i)
595 jab 1069
        {
1070
            HalfEdgeID last = new_halfedges[i];
1071
            FaceID f = kernel.add_face();
1072
            kernel.set_last(f, last);
1073
            HalfEdgeID h = last;
1074
            do
1075
            {
1076
                kernel.set_face(h, f);
1077
                h = kernel.next(h);
1078
            } while(h != last);
1079
        }
1080
 
1081
        // Delete the old faces that were bridged.
1082
        kernel.remove_face(f0);
1083
        kernel.remove_face(f1);
1084
 
1085
        new_halfedges.insert(new_halfedges.end(), new_halfedges_opp.begin(), new_halfedges_opp.end());
1086
        return new_halfedges;
1087
    }
1088
 
1089
    /***************************************************
1090
     * Namespace functions
1091
     ***************************************************/
1092
    bool valid(const Manifold& m)
1093
    {
1094
        // Verify components of halfedges
1095
        for(HalfEdgeIDIterator h = m.halfedges_begin(); h != m.halfedges_end(); ++h){
1096
            Walker j = m.walker(*h);
1097
 
1098
            if(j.vertex() == InvalidVertexID){
1099
                cout << "Halfedge lacks vert" << endl;
1100
                return false;
1101
            }
1102
            if(j.next().halfedge() == InvalidHalfEdgeID){
1103
                cout << "Halfedge lacks next" << endl;
1104
                return false;
1105
            }
1106
            if(j.prev().halfedge() == InvalidHalfEdgeID){
1107
                cout << "Halfedge lacks prev" << endl;
1108
                return false;
1109
            }
1110
            if(j.opp().halfedge() == InvalidHalfEdgeID){
1111
                cout << "Halfedge lacks opp" << endl;
1112
                return false;
1113
            }
1114
 
1115
        }
1116
        // Verify components of vertices
1117
        for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
1118
            vector<VertexID> link;
1119
 
1120
            // circulate the halfedges of vertex
1121
            for(Walker j = m.walker(*v); !j.full_circle(); j = j.circulate_vertex_cw()){
1122
                // test halfedges around v
1123
                if(j.halfedge() == InvalidHalfEdgeID){
1124
                    cout << "Vertex circulation produced invalid halfedge" << endl;
1125
                    return false;
1126
                }
1127
                VertexID ring_v = j.vertex();
1128
 
1129
                // test one-ring for multiple occurences of vertex
1130
                if(find(link.begin(), link.end(), ring_v) != link.end()){
1131
                    cout << "Vertex appears two times in one-ring of vertex" << endl;
1132
                    return false;
1133
                }
1134
                link.push_back(ring_v);
1135
 
1136
                // test for infinite loop around vertex
1137
                if(static_cast<size_t>(j.no_steps()) > m.no_vertices()){
1138
                    cout << "Vertex loop contains more vertices than manifold" << endl;
1139
                    return false;
1140
                }
1141
            }
1142
 
1143
            // test one_ring size for boundary consistency
1144
            if(link.size() <= 2){
1145
                Walker j = m.walker(*v);
1146
 
1147
                if(j.face() != InvalidFaceID && j.opp().face() != InvalidFaceID)
1148
                {
1149
                    if(link.size()==1)
1150
                        cout << "Vertex contains only a single incident edge" << endl;
1151
                    //
1152
                    //                    cout << "Vertex contains only " << link.size() <<" incident edges" << endl;
1153
                }
1154
                else
1155
                    cout << "Boundary vertex contains only " << link.size() <<" incident edges" << endl;
1156
            }
1157
        }
1158
        // verify components of faces
1159
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){
1160
            // count edges on face
1161
            Walker j = m.walker(*f);
1162
 
1163
            for(; !j.full_circle(); j = j.circulate_face_cw()){
1164
                // test that all halfedges in faces bind properly to their face
1165
                if(j.face() != *f){
1166
                    cout << "Face is inconsistent, halfedge is not bound to face" << endl;
1167
                    return false;
1168
                }
1169
            }
1170
            // test faces for valid geometrical properties
1171
            if(j.no_steps() < 3){
1172
                cout << "Face contains less than 3 edges" << endl;
1173
                return false;
1174
            }
1175
            // test for infinite loop around face
1176
            if(j.no_steps() > m.no_halfedges() * 0.5f){
1177
                cout << "Face loop contains more halfedges than manifold" << endl;
1178
                return false;
1179
            }
1180
        }
1181
        return true;
1182
    }
1183
 
1184
    void bbox(const Manifold& m, Manifold::Vec& pmin, Manifold::Vec& pmax)
1185
    {
1186
 
1187
        VertexIDIterator v = m.vertices_begin();
1188
        pmin = pmax = m.pos(*v);
1189
        ++v;
1190
        for(; v != m.vertices_end(); ++v){
1191
            pmin = v_min(m.pos(*v), pmin);
1192
            pmax = v_max(m.pos(*v), pmax);
1193
        }
1194
    }
1195
 
1196
    void bsphere(const Manifold& m, Manifold::Vec& c, float& r)
1197
    {
1198
        Manifold::Vec p0, p7;
1199
        bbox(m, p0, p7);
1200
        Manifold::Vec rad = (p7 - p0) * 0.5f;
1201
        c = p0 + rad;
1202
        r = rad.length();
1203
    }
1204
 
1205
 
1206
 
1207
    bool precond_collapse_edge(const Manifold& m, HalfEdgeID h)
1208
    {
1209
        Walker hew = m.walker(h);
1210
        HalfEdgeID ho = hew.opp().halfedge();
1211
        VertexID v0 = hew.opp().vertex();
1212
        VertexID v1 = hew.vertex();
1213
 
1214
        // get the one-ring of v0
1215
        vector<VertexID> link0;
1216
        vector<FaceID> faces0;
1217
        for(Walker vj = m.walker(h);
1218
            !vj.full_circle(); vj = vj.circulate_vertex_ccw()){
1219
            link0.push_back(vj.vertex());
1220
            if(vj.halfedge() != h)
1221
                faces0.push_back(vj.face());
1222
        }
1223
        assert(link0.size() > 1);
1224
 
1225
        // get the one-ring of v1
1226
        vector<VertexID> link1;
1227
        vector<FaceID> faces1;
1228
        for(Walker vj = m.walker(ho);
1229
            !vj.full_circle(); vj = vj.circulate_vertex_ccw()){
1230
            link1.push_back(vj.vertex());
1231
            if(vj.halfedge() != ho)
1232
                faces1.push_back(vj.face());
1233
        }
1234
        assert(link1.size() > 1);
1235
 
1236
        // sort the vertices of the two rings
1237
        sort(link0.begin(), link0.end());
1238
        sort(link1.begin(), link1.end());
1239
 
1240
        // get the intersection of the shared vertices from both rings
1241
        vector<VertexID> lisect;
1242
        std::back_insert_iterator<vector<VertexID> > lii(lisect);
1243
        set_intersection(link0.begin(), link0.end(),
1244
                         link1.begin(), link1.end(),
1245
                         lii);
1246
 
1247
        // sort the faces of the two rings
1248
        sort(faces0.begin(), faces0.end());
1249
        sort(faces1.begin(), faces1.end());
1250
 
1251
        // get the intersection of the shared faces from both rings
1252
        vector<FaceID> fisect;
1253
        std::back_insert_iterator<vector<FaceID> > fii(fisect);
1254
        set_intersection(faces0.begin(), faces0.end(),
1255
                         faces1.begin(), faces1.end(),
1256
                         fii);
1257
        if(fisect.size() > 0)
1258
            return false;
1259
 
1260
        int k = 0;
1261
        // if the adjacent face is a triangle (see 2)
1262
        if(hew.next().next().next().halfedge() == h){
1263
            VertexID v = hew.next().vertex();
1264
 
1265
            // valency test (see 5)
1266
            if(valency(m, v) < 3)
1267
                return false;
1268
 
1269
            // remove the vertex shared by the two rings from the intersection
1270
            vector<VertexID>::iterator iter;
1271
            iter = find(lisect.begin(), lisect.end(), v);
1272
            assert(iter != lisect.end());
1273
            lisect.erase(iter);
1274
            ++k;
1275
        }
1276
        // if the adjacent face is a triangle (see 2)
1277
        if(hew.opp().next().next().next().halfedge() == hew.opp().halfedge()){
1278
            VertexID v = hew.opp().next().vertex();
1279
 
1280
            // valency test (see 5)
1281
            if(valency(m, v) < 3)
1282
                return false;
1283
 
1284
            // remove the vertex shared by the two rings from the intersection
1285
            vector<VertexID>::iterator iter;
1286
            iter = find(lisect.begin(), lisect.end(), v);
1287
            assert(iter != lisect.end());
1288
            lisect.erase(iter);
1289
            ++k;
1290
        }
1291
        // double edge test (see 3)
1292
        if(lisect.size() != 0)
1293
            return false;
1294
 
1295
        // tetrahedon test (see 4)
1296
        if(k == 2 && (link0.size() + link1.size() == 6))
1297
            return false;
1298
 
1299
        // test that we do not merge holes (see 6)
1300
        if(boundary(m, v0) && boundary(m, v1) && hew.face() != InvalidFaceID && hew.opp().face() != InvalidFaceID)
1301
            return false;
1302
 
1303
        return true;
1304
    }
1305
 
1306
    bool precond_flip_edge(const Manifold& m, HalfEdgeID h)
1307
    {
1308
        Walker j = m.walker(h);
1309
 
1310
        FaceID hf = j.face();
1311
        //        HalfEdgeID ho = j.opp().halfedge();
1312
        FaceID hof = j.opp().face();
1313
 
1314
        // boundary case
1315
        if(hf == InvalidFaceID || hof == InvalidFaceID)
1316
            return false;
1317
 
1318
        // We can only flip an edge if both incident polygons are triangles.
1319
        if(no_edges(m, hf) != 3 || no_edges(m, hof) !=3)
1320
            return false;
1321
 
1322
        // non boundary vertices with a valency of less than 4(less than 3 after operation) degenerates mesh.
1323
        VertexID hv = j.vertex();
1324
        VertexID hov = j.opp().vertex();
1325
        if((valency(m, hv) < 4 && !boundary(m, hv)) || (valency(m, hov) < 4 && !boundary(m, hov)))
1326
            return false;
1327
 
1328
        // Disallow flip if vertices being connected already are.
1329
        VertexID hnv = j.next().vertex();
1330
        VertexID honv = j.opp().next().vertex();
1331
        if(connected(m, hnv, honv))
1332
            return false;
1333
 
1334
        return true;
1335
    }
1336
 
1337
    bool boundary(const Manifold& m, VertexID v)
1338
    {
1339
        Walker j  = m.walker(v);
1340
        return boundary(m, j.halfedge());
1341
    }
1342
    int valency(const Manifold& m, VertexID v)
1343
    {
1344
        // perform full circulation to get valency
1345
        Walker vj = m.walker(v);
1346
        while(!vj.full_circle())
1347
            vj = vj.circulate_vertex_cw();
1348
        return vj.no_steps();
1349
    }
1350
 
1351
    Manifold::Vec normal(const Manifold& m, VertexID v)
1352
    {
1353
        Manifold::Vec p0 = m.pos(v);
1354
        vector<Manifold::Vec> one_ring;
1355
 
1356
        // run through outgoing edges, and store them normalized
1357
        for(Walker vj = m.walker(v); !vj.full_circle(); vj = vj.circulate_vertex_ccw()){
1358
            Manifold::Vec edge = m.pos(vj.vertex())-p0;
1359
            float l = length(edge);
1360
            if(l > 0.0f) one_ring.push_back(edge/l);
1361
        }
1362
 
1363
        Manifold::Vec n(0);
1364
        size_t N = one_ring.size();
1365
        if(N<2)
1366
            return Manifold::Vec(0);
1367
 
1368
        size_t N_count = N;
631 janba 1369
        size_t N_start = 0;
595 jab 1370
        if(boundary(m, v))
631 janba 1371
            N_start = 1;
595 jab 1372
 
1373
        // sum up the normals of each face surrounding the vertex
631 janba 1374
        for(size_t i = N_start; i < N_count; ++i){
595 jab 1375
            Manifold::Vec e0 = one_ring[i];
1376
            Manifold::Vec e1 = one_ring[(i+1) % N];
1377
 
1378
            Manifold::Vec n_part = normalize(cross(e0, e1));
1379
            n += n_part * acos(max(-1.0, min(1.0, dot(e0, e1))));
1380
        }
1381
 
1382
        // normalize and return the normal
1383
        float sqr_l = sqr_length(n);
1384
        if(sqr_l > 0.0f) return n / sqrt(sqr_l);
1385
 
1386
        return n;
1387
    }
1388
 
1389
    bool connected(const Manifold& m, VertexID v0, VertexID v1)
1390
    {
1391
        for(Walker vj = m.walker(v0); !vj.full_circle(); vj = vj.circulate_vertex_cw()){
1392
            if(vj.vertex() == v1)
1393
                return true;
1394
        }
1395
        return false;
1396
    }
1397
 
1398
    int no_edges(const Manifold& m, FaceID f)
1399
    {
1400
        // perform full circulation to get valency
1401
        Walker w = m.walker(f);
1402
        for(; !w.full_circle(); w = w.circulate_face_cw());
1403
        return w.no_steps();
1404
    }
1405
    Manifold::Vec normal(const Manifold& m, FaceID f)
1406
    {
1407
        vector<Manifold::Vec> v;
1408
 
1409
        int k=0;
1410
        for(Walker  w = m.walker(f); !w.full_circle(); w = w.circulate_face_ccw(),++k)
1411
            v.push_back(m.pos(w.vertex()));
1412
 
1413
        Manifold::Vec norm(0);
1414
        for(int i=0;i<k;++i)
1415
        {
1416
            norm[0] += (v[i][1]-v[(i+1)%k][1])*(v[i][2]+v[(i+1)%k][2]);
1417
            norm[1] += (v[i][2]-v[(i+1)%k][2])*(v[i][0]+v[(i+1)%k][0]);
1418
            norm[2] += (v[i][0]-v[(i+1)%k][0])*(v[i][1]+v[(i+1)%k][1]);
1419
        }
1420
        float l = sqr_length(norm);
1421
        if(l>0.0f)
1422
            norm /= sqrt(l);
1423
        return norm;
1424
    }
1425
 
1426
 
631 janba 1427
    float area(const Manifold& m, FaceID fid)
595 jab 1428
    {
1429
        // Get all projected vertices
1430
        vector<Manifold::Vec> vertices;
631 janba 1431
        Walker w = m.walker(fid);
595 jab 1432
        for(; !w.full_circle(); w = w.circulate_face_ccw())
1433
            vertices.push_back(m.pos(w.vertex()));
1434
        float area = 0;
631 janba 1435
        size_t N = vertices.size();
1436
        Manifold::Vec norm = normal(m,fid);
1437
        for(int i = 1; i < N-1; ++i)
1438
            area += 0.5 * dot(norm,cross(vertices[i]-vertices[0], vertices[(i+1 )]-vertices[0]));
1439
        return area;
595 jab 1440
    }
1441
 
1442
    Manifold::Vec centre(const Manifold& m, FaceID f)
1443
    {
1444
        Manifold::Vec c(0);
1445
        Walker w = m.walker(f);
1446
 
1447
        for(; !w.full_circle(); w = w.circulate_face_ccw()) c += m.pos(w.vertex());
1448
 
1449
        c /= w.no_steps();
1450
        return c;
1451
    }
1452
 
1453
    float perimeter(const Manifold& m, FaceID f)
1454
    {
1455
        float p=0.0;
1456
        Walker w = m.walker(f);
1457
 
1458
        for(; !w.full_circle(); w = w.circulate_face_cw()) p += length(m, w.halfedge());
1459
        return p;
1460
    }
1461
 
1462
    bool boundary(const Manifold& m, HalfEdgeID h)
1463
    {
1464
        Walker w = m.walker(h);
1465
        return w.face() == InvalidFaceID || w.opp().face() == InvalidFaceID;
1466
    }
1467
 
1468
    float length(const Manifold& m, HalfEdgeID h)
1469
    {
1470
        Walker w = m.walker(h);
1471
        return (m.pos(w.vertex()) - m.pos(w.opp().vertex())).length();
1472
    }
39 bj 1473
}