Subversion Repositories gelsvn

Rev

Rev 667 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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