Subversion Repositories gelsvn

Rev

Rev 658 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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