Subversion Repositories gelsvn

Rev

Rev 160 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 160 Rev 170
1
#include <iostream>
1
#include <iostream>
2
 
2
 
3
#include "Manifold.h"
3
#include "Manifold.h"
4
#include "VertexCirculator.h"
4
#include "VertexCirculator.h"
5
#include "FaceCirculator.h"
5
#include "FaceCirculator.h"
6
 
6
 
7
 
7
 
8
namespace HMesh
8
namespace HMesh
9
{
9
{
10
	using namespace std;
10
	using namespace std;
11
	using namespace CGLA;
11
	using namespace CGLA;
12
 
12
 
13
	void Manifold::remove_unused()
13
	void Manifold::remove_unused()
14
	{
14
	{
15
    for(size_t i=0;i<unused_vertices.size(); ++i)
15
    for(size_t i=0;i<unused_vertices.size(); ++i)
16
			vertex_db.erase(unused_vertices[i]);
16
			vertex_db.erase(unused_vertices[i]);
17
 
17
 
18
    std::vector<VertexIter> vdummy(0);
18
    std::vector<VertexIter> vdummy(0);
19
    unused_vertices = vdummy;
19
    unused_vertices = vdummy;
20
 
20
 
21
    for(size_t i=0;i<unused_faces.size(); ++i)
21
    for(size_t i=0;i<unused_faces.size(); ++i)
22
			face_db.erase(unused_faces[i]);
22
			face_db.erase(unused_faces[i]);
23
 
23
 
24
    std::vector<FaceIter> fdummy(0);
24
    std::vector<FaceIter> fdummy(0);
25
    unused_faces = fdummy;
25
    unused_faces = fdummy;
26
 
26
 
27
    for(size_t i=0;i<unused_halfedges.size(); ++i)
27
    for(size_t i=0;i<unused_halfedges.size(); ++i)
28
			halfedge_db.erase(unused_halfedges[i]);
28
			halfedge_db.erase(unused_halfedges[i]);
29
 
29
 
30
    std::vector<HalfEdgeIter> hdummy(0);
30
    std::vector<HalfEdgeIter> hdummy(0);
31
    unused_halfedges = hdummy;
31
    unused_halfedges = hdummy;
32
	}
32
	}
33
	
33
	
34
	void Manifold::clear()
34
	void Manifold::clear()
35
	{
35
	{
36
		vertex_db.clear();
36
		vertex_db.clear();
37
		face_db.clear();
37
		face_db.clear();
38
		halfedge_db.clear();
38
		halfedge_db.clear();
39
 
39
 
40
		std::vector<VertexIter> vdummy(0);
40
		std::vector<VertexIter> vdummy(0);
41
		unused_vertices = vdummy;
41
		unused_vertices = vdummy;
42
					
42
					
43
		std::vector<FaceIter> fdummy(0);
43
		std::vector<FaceIter> fdummy(0);
44
		unused_faces = fdummy;
44
		unused_faces = fdummy;
45
 
45
 
46
		std::vector<HalfEdgeIter> hdummy(0);
46
		std::vector<HalfEdgeIter> hdummy(0);
47
		unused_halfedges = hdummy;
47
		unused_halfedges = hdummy;
48
	}
48
	}
49
 
49
 
50
 
50
 
51
	void Manifold::erase_halfedge(HalfEdgeIter h)
51
	void Manifold::erase_halfedge(HalfEdgeIter h)
52
	{
52
	{
53
		if(erase_immediately)
53
		if(erase_immediately)
54
			halfedge_db.erase(h);
54
			halfedge_db.erase(h);
55
		else
55
		else
56
			{
56
			{
57
				unused_halfedges.push_back(h);
57
				unused_halfedges.push_back(h);
58
				HalfEdge h_dummy;
58
				HalfEdge h_dummy;
59
				(*h) = h_dummy;
59
				(*h) = h_dummy;
60
			}
60
			}
61
	}
61
	}
62
 
62
 
63
	void Manifold::erase_vertex(VertexIter v)
63
	void Manifold::erase_vertex(VertexIter v)
64
	{
64
	{
65
		if(erase_immediately)
65
		if(erase_immediately)
66
			vertex_db.erase(v);
66
			vertex_db.erase(v);
67
		else
67
		else
68
			{
68
			{
69
				unused_vertices.push_back(v);
69
				unused_vertices.push_back(v);
70
				Vertex v_dummy(v->pos);
70
				Vertex v_dummy(v->pos);
71
				(*v) = v_dummy;
71
				(*v) = v_dummy;
72
			}
72
			}
73
	}
73
	}
74
 
74
 
75
	void Manifold::erase_face(FaceIter f)
75
	void Manifold::erase_face(FaceIter f)
76
	{
76
	{
77
		if(erase_immediately)
77
		if(erase_immediately)
78
			face_db.erase(f);
78
			face_db.erase(f);
79
		else
79
		else
80
			{
80
			{
81
				unused_faces.push_back(f);
81
				unused_faces.push_back(f);
82
				Face f_dummy;
82
				Face f_dummy;
83
				(*f) = f_dummy;
83
				(*f) = f_dummy;
84
			}
84
			}
85
	}
85
	}
86
 
86
 
87
 
87
 
88
 
88
 
89
 
89
 
90
	void Manifold::get_bbox(Vec3f& pmin, Vec3f& pmax)
90
	void Manifold::get_bbox(Vec3f& pmin, Vec3f& pmax)
91
	{
91
	{
92
    VertexIter vi = vertices_begin();
92
    VertexIter vi = vertices_begin();
93
    pmin = pmax = vi->pos;
93
    pmin = pmax = vi->pos;
94
    for(++vi;vi != vertices_end(); ++vi)
94
    for(++vi;vi != vertices_end(); ++vi)
95
			{
95
			{
96
        pmin = v_min(vi->pos, pmin);
96
        pmin = v_min(vi->pos, pmin);
97
        pmax = v_max(vi->pos, pmax);
97
        pmax = v_max(vi->pos, pmax);
98
			}
98
			}
99
	}
99
	}
100
 
100
 
101
	void Manifold::get_bsphere(CGLA::Vec3f& c, float& r)
101
	void Manifold::get_bsphere(CGLA::Vec3f& c, float& r)
102
	{
102
	{
103
    Vec3f p0,p7;
103
    Vec3f p0,p7;
104
    get_bbox(p0, p7);
104
    get_bbox(p0, p7);
105
    Vec3f rad = (p7 - p0)/2.0;
105
    Vec3f rad = (p7 - p0)/2.0;
106
    c = p0 + rad;
106
    c = p0 + rad;
107
    r = rad.length();
107
    r = rad.length();
108
	}
108
	}
109
 
109
 
110
 
110
 
111
	VertexIter Manifold::split_edge(HalfEdgeIter h)
111
	VertexIter Manifold::split_edge(HalfEdgeIter h)
112
	{
112
	{
113
    HalfEdgeIter ho = h->opp;
113
    HalfEdgeIter ho = h->opp;
114
    VertexIter v  = h->vert;
114
    VertexIter v  = h->vert;
115
    VertexIter vo = ho->vert;
115
    VertexIter vo = ho->vert;
116
    Vec3f np = (v->pos+vo->pos)/2.0f;
116
    Vec3f np = (v->pos+vo->pos)/2.0f;
117
 
117
 
118
    VertexIter vn = create_vertex(np);
118
    VertexIter vn = create_vertex(np);
119
    vn->he = h;
119
    vn->he = h;
120
 
120
 
121
    HalfEdgeIter hn = create_halfedge();
121
    HalfEdgeIter hn = create_halfedge();
122
    HalfEdgeIter hno = create_halfedge();
122
    HalfEdgeIter hno = create_halfedge();
123
 
123
 
124
    vo->he = hn;
124
    vo->he = hn;
125
    v->he = ho;
125
    v->he = ho;
126
 
126
 
127
    glue(hn,hno);
127
    glue(hn,hno);
128
    link(h->prev, hn);
128
    link(h->prev, hn);
129
    link(hn,h);
129
    link(hn,h);
130
    hn->vert = vn;
130
    hn->vert = vn;
131
 
131
 
132
    link(hno,ho->next);
132
    link(hno,ho->next);
133
    link(ho, hno);
133
    link(ho, hno);
134
    hno->vert = vo;
134
    hno->vert = vo;
135
    ho->vert = vn;
135
    ho->vert = vn;
136
 
136
 
137
    if(h->face != NULL_FACE_ITER)
137
    if(h->face != NULL_FACE_ITER)
138
			h->face->last = hn;
138
			h->face->last = hn;
139
    if(ho->face != NULL_FACE_ITER)
139
    if(ho->face != NULL_FACE_ITER)
140
			ho->face->last = ho;
140
			ho->face->last = ho;
141
 
141
 
142
    hn->face = h->face;
142
    hn->face = h->face;
143
    hno->face = ho->face;
143
    hno->face = ho->face;
144
 
144
 
145
    check_boundary_consistency(vn);
145
    check_boundary_consistency(vn);
146
    check_boundary_consistency(v);
146
    check_boundary_consistency(v);
147
    check_boundary_consistency(vo);
147
    check_boundary_consistency(vo);
148
    return vn;
148
    return vn;
149
	}
149
	}
150
 
150
 
151
 
151
 
152
	void Manifold::remove_face_if_degenerate(HalfEdgeIter h0)
152
	void Manifold::remove_face_if_degenerate(HalfEdgeIter h0)
153
	{
153
	{
154
    if(h0->next->next == h0)
154
    if(h0->next->next == h0)
155
			{
155
			{
156
        HalfEdgeIter h1 = h0->next;
156
        HalfEdgeIter h1 = h0->next;
157
 
157
 
158
        HalfEdgeIter h0o = h0->opp;
158
        HalfEdgeIter h0o = h0->opp;
159
        HalfEdgeIter h1o = h1->opp;
159
        HalfEdgeIter h1o = h1->opp;
160
 
160
 
161
        glue(h0o, h1o);
161
        glue(h0o, h1o);
162
 
162
 
163
        VertexIter v0 = h1->vert;
163
        VertexIter v0 = h1->vert;
164
        VertexIter v1 = h0->vert;
164
        VertexIter v1 = h0->vert;
165
        v0->he = h1o;
165
        v0->he = h1o;
166
        v1->he = h0o;
166
        v1->he = h0o;
167
 
167
 
168
        if(h0->face != NULL_FACE_ITER)
168
        if(h0->face != NULL_FACE_ITER)
169
					erase_face(h0->face);
169
					erase_face(h0->face);
170
        erase_halfedge(h0);
170
        erase_halfedge(h0);
171
        erase_halfedge(h1);
171
        erase_halfedge(h1);
172
 
172
 
173
        check_boundary_consistency(v0);
173
        check_boundary_consistency(v0);
174
        check_boundary_consistency(v1);
174
        check_boundary_consistency(v1);
175
 
175
 
176
			}
176
			}
177
	}
177
	}
178
 
178
 
179
	bool Manifold::collapse_precond(HalfEdgeIter h)
179
	bool Manifold::collapse_precond(HalfEdgeIter h)
180
	{
180
	{
181
    VertexIter v1 = h->opp->vert;
181
    VertexIter v1 = h->opp->vert;
182
    VertexIter v2 = h->vert;
182
    VertexIter v2 = h->vert;
183
 
183
 
184
    std::vector<Vertex*> link1;
184
    std::vector<Vertex*> link1;
185
    VertexCirculator vc1(v1);
185
    VertexCirculator vc1(v1);
186
    for(;!vc1.end();++vc1)
186
    for(;!vc1.end();++vc1)
187
			link1.push_back(&(*vc1.get_vertex()));
187
			link1.push_back(&(*vc1.get_vertex()));
188
    assert(link1.size()>=2);
188
    assert(link1.size()>=2);
189
 
189
 
190
    std::vector<Vertex*> link2;
190
    std::vector<Vertex*> link2;
191
    VertexCirculator vc2(v2);
191
    VertexCirculator vc2(v2);
192
    for(;!vc2.end();++vc2)
192
    for(;!vc2.end();++vc2)
193
			link2.push_back(&(*vc2.get_vertex()));
193
			link2.push_back(&(*vc2.get_vertex()));
194
    assert(link2.size()>=2);
194
    assert(link2.size()>=2);
195
 
195
 
196
    sort(link1.begin(),link1.end());
196
    sort(link1.begin(),link1.end());
197
    sort(link2.begin(),link2.end());
197
    sort(link2.begin(),link2.end());
198
 
198
 
199
    vector<Vertex*> lisect;
199
    vector<Vertex*> lisect;
200
    back_insert_iterator<vector<Vertex*> > lii(lisect);
200
    back_insert_iterator<vector<Vertex*> > lii(lisect);
201
 
201
 
202
    set_intersection(link1.begin(), link1.end(),
202
    set_intersection(link1.begin(), link1.end(),
203
                     link2.begin(), link2.end(),
203
                     link2.begin(), link2.end(),
204
                     lii);
204
                     lii);
205
 
205
 
206
 
206
 
207
    // If the adjacent face is a triangle (see 2)
207
    // If the adjacent face is a triangle (see 2)
208
    int k=0;
208
    int k=0;
209
    if(h->next->next->next == h)
209
    if(h->next->next->next == h)
210
			{
210
			{
211
        // VALENCY 4 TEST
211
        // VALENCY 4 TEST
212
        if(valency(h->next->vert)<4)
212
        if(valency(h->next->vert)<4)
213
					return false;
213
					return false;
214
 
214
 
215
        vector<Vertex*>::iterator iter;
215
        vector<Vertex*>::iterator iter;
216
        iter = find(lisect.begin(),	lisect.end(),&(*h->next->vert));
216
        iter = find(lisect.begin(),	lisect.end(),&(*h->next->vert));
217
        assert(iter != lisect.end());
217
        assert(iter != lisect.end());
218
        lisect.erase(iter);
218
        lisect.erase(iter);
219
        ++k;
219
        ++k;
220
 
220
 
221
			}
221
			}
222
    // If the adjacent face is a triangle (see 2)
222
    // If the adjacent face is a triangle (see 2)
223
    if(h->opp->next->next->next == h->opp)
223
    if(h->opp->next->next->next == h->opp)
224
			{
224
			{
225
        // VALENCY 4 TEST
225
        // VALENCY 4 TEST
226
        if(valency(h->opp->next->vert)<4)
226
        if(valency(h->opp->next->vert)<4)
227
					return false;
227
					return false;
228
 
228
 
229
        vector<Vertex*>::iterator iter;
229
        vector<Vertex*>::iterator iter;
230
        iter = find(lisect.begin(),	lisect.end(),&(*h->opp->next->vert));
230
        iter = find(lisect.begin(),	lisect.end(),&(*h->opp->next->vert));
231
        assert(iter != lisect.end());
231
        assert(iter != lisect.end());
232
        lisect.erase(iter);
232
        lisect.erase(iter);
233
        ++k;
233
        ++k;
234
 
234
 
235
			}
235
			}
236
    if(lisect.size() !=0) // See 3.
236
    if(lisect.size() !=0) // See 3.
237
			return false;
237
			return false;
238
 
238
 
239
    // TETRAHEDRON TEST
239
    // TETRAHEDRON TEST
240
    if(k==2 && (link1.size()+link2.size()==6))
240
    if(k==2 && (link1.size()+link2.size()==6))
241
			return false;
241
			return false;
242
 
242
 
243
    // Test that we are not merging holes (see 6)
243
    // Test that we are not merging holes (see 6)
244
    if(is_boundary(v1) && is_boundary(v2) &&
244
    if(is_boundary(v1) && is_boundary(v2) &&
245
			 (h->face != NULL_FACE_ITER) &&
245
			 (h->face != NULL_FACE_ITER) &&
246
			 (h->opp->face != NULL_FACE_ITER))
246
			 (h->opp->face != NULL_FACE_ITER))
247
			return false;
247
			return false;
248
 
248
 
249
 
249
 
250
    return true;
250
    return true;
251
	}
251
	}
252
 
252
 
253
 
253
 
254
	void Manifold::collapse_halfedge(HalfEdgeIter h, bool avg_vertices)
254
	void Manifold::collapse_halfedge(HalfEdgeIter h, bool avg_vertices)
255
	{
255
	{
256
    VertexIter v = h->opp->vert;
256
    VertexIter v = h->opp->vert;
257
    HalfEdgeIter ho = h->opp;
257
    HalfEdgeIter ho = h->opp;
258
    VertexIter n = h->vert;
258
    VertexIter n = h->vert;
259
 
259
 
260
    if(avg_vertices)
260
    if(avg_vertices)
261
			n->pos = ((v->pos + n->pos) / 2.0f);
261
			n->pos = ((v->pos + n->pos) / 2.0f);
262
 
262
 
263
    HalfEdgeIter hn = h->next;
263
    HalfEdgeIter hn = h->next;
264
    HalfEdgeIter hp = h->prev;
264
    HalfEdgeIter hp = h->prev;
265
    HalfEdgeIter hon = ho->next;
265
    HalfEdgeIter hon = ho->next;
266
    HalfEdgeIter hop = ho->prev;
266
    HalfEdgeIter hop = ho->prev;
267
 
267
 
268
    VertexCirculator vc(v);
268
    VertexCirculator vc(v);
269
    for(;!vc.end();++vc)
269
    for(;!vc.end();++vc)
270
			vc.get_opp_halfedge()->vert = n;
270
			vc.get_opp_halfedge()->vert = n;
271
 
271
 
272
    n->he = h->next;
272
    n->he = h->next;
273
 
273
 
274
    link(hp, hn);
274
    link(hp, hn);
275
    if(h->face != NULL_FACE_ITER)
275
    if(h->face != NULL_FACE_ITER)
276
			h->face->last = hn;
276
			h->face->last = hn;
277
 
277
 
278
    link(hop, hon);
278
    link(hop, hon);
279
    if(ho->face != NULL_FACE_ITER)
279
    if(ho->face != NULL_FACE_ITER)
280
			ho->face->last = hon;
280
			ho->face->last = hon;
281
 
281
 
282
 
282
 
283
 
283
 
284
    erase_vertex(v);
284
    erase_vertex(v);
285
    erase_halfedge(h);
285
    erase_halfedge(h);
286
    erase_halfedge(ho);
286
    erase_halfedge(ho);
287
 
287
 
288
    remove_face_if_degenerate(hn);
288
    remove_face_if_degenerate(hn);
289
    remove_face_if_degenerate(hon);
289
    remove_face_if_degenerate(hon);
290
 
290
 
291
    check_boundary_consistency(n);
291
    check_boundary_consistency(n);
292
	}
292
	}
293
 
293
 
294
	bool Manifold::is_valid()
294
	bool Manifold::is_valid()
295
	{
295
	{
296
    HalfEdgeIter he0 = halfedges_begin();
296
    HalfEdgeIter he0 = halfedges_begin();
297
    while(he0 != halfedges_end())
297
    while(he0 != halfedges_end())
298
			{
298
			{
299
        if(he0->prev == NULL_HALFEDGE_ITER)
299
        if(he0->prev == NULL_HALFEDGE_ITER)
300
					{
300
					{
301
            cout << "Halfedge lacks previous" << endl;
301
            cout << "Halfedge lacks previous" << endl;
302
            return false;
302
            return false;
303
					}
303
					}
304
        if(he0->next == NULL_HALFEDGE_ITER)
304
        if(he0->next == NULL_HALFEDGE_ITER)
305
					{
305
					{
306
            cout << "Halfedge lacks next" << endl;
306
            cout << "Halfedge lacks next" << endl;
307
            return false;
307
            return false;
308
					}
308
					}
309
        if(he0->opp == NULL_HALFEDGE_ITER)
309
        if(he0->opp == NULL_HALFEDGE_ITER)
310
					{
310
					{
311
            cout << "Halfedge lacks opposite" << endl;
311
            cout << "Halfedge lacks opposite" << endl;
312
            return false;
312
            return false;
313
					}
313
					}
314
        if(he0->vert == NULL_VERTEX_ITER)
314
        if(he0->vert == NULL_VERTEX_ITER)
315
					{
315
					{
316
            cout << "Halfedge lacks vertex" << endl;
316
            cout << "Halfedge lacks vertex" << endl;
317
            return false;
317
            return false;
318
					}
318
					}
319
        ++he0;
319
        ++he0;
320
			}
320
			}
321
 
321
 
322
    VertexIter vi = vertices_begin();
322
    VertexIter vi = vertices_begin();
323
    while(vi != vertices_end())
323
    while(vi != vertices_end())
324
			{
324
			{
325
        std::vector<Vertex*> link;
325
        std::vector<Vertex*> link;
326
        VertexCirculator vc(vi);
326
        VertexCirculator vc(vi);
327
        int k=0;
327
        int k=0;
328
        for(;!vc.end();++vc,++k)
328
        for(;!vc.end();++vc,++k)
329
					{
329
					{
330
            if(vc.get_halfedge() == NULL_HALFEDGE_ITER)
330
            if(vc.get_halfedge() == NULL_HALFEDGE_ITER)
331
							{
331
							{
332
                cout << "Vertex has null outgoing he" << endl;
332
                cout << "Vertex has null outgoing he" << endl;
333
                return false;
333
                return false;
334
							}
334
							}
335
            Vertex* x = &(*vc.get_vertex());
335
            Vertex* x = &(*vc.get_vertex());
336
            if(find(link.begin(), link.end(), x) != link.end())
336
            if(find(link.begin(), link.end(), x) != link.end())
337
							{
337
							{
338
                cout << "Vertex appears two times in one-ring of other vertex" << endl;
338
                cout << "Vertex appears two times in one-ring of other vertex" << endl;
339
                return false;
339
                return false;
340
							}
340
							}
341
            link.push_back(x);
341
            link.push_back(x);
342
            if(k==1e6)
342
            if(k==1e6)
343
							{
343
							{
344
                cout << "infin. loop around vertex" << endl;
344
                cout << "infin. loop around vertex" << endl;
345
                return false;
345
                return false;
346
							}
346
							}
347
					}
347
					}
348
        if(link.size()==2)
348
        if(link.size()==2)
349
					{
349
					{
-
 
350
						if(!is_boundary(vi))
350
            cout << "Warning: A vertex with only two incident edges" << endl;
351
							cout << "Warning: A vertex with only two incident edges" << endl;
-
 
352
						else
-
 
353
							cout << "Comment: A boundary vertex with only two incident edges" << endl;
351
					}
354
					}
352
        assert(link.size()>=2);
355
        assert(link.size()>=2);
353
        ++vi;
356
        ++vi;
354
			}
357
			}
355
 
358
 
356
    HMesh::FaceIter f = faces_begin();
359
    HMesh::FaceIter f = faces_begin();
357
    for(;f != faces_end(); ++f)
360
    for(;f != faces_end(); ++f)
358
			{
361
			{
359
        if(no_edges(f)<3)
362
        if(no_edges(f)<3)
360
					{
363
					{
361
            cout << "Degenerate face" << endl;
364
            cout << "Degenerate face" << endl;
362
					}
365
					}
363
        FaceCirculator fc(f);
366
        FaceCirculator fc(f);
364
        int k=0;
367
        int k=0;
365
        while(!fc.end())
368
        while(!fc.end())
366
					{
369
					{
367
            if(fc.get_halfedge()->face != f)
370
            if(fc.get_halfedge()->face != f)
368
							{
371
							{
369
                cout << "Inconsistent face" << endl;
372
                cout << "Inconsistent face" << endl;
370
                return false;
373
                return false;
371
							}
374
							}
372
            if(++k==1e6) cout << "infin. loop around face" << endl;
375
            if(++k==1e6) cout << "infin. loop around face" << endl;
373
            ++fc;
376
            ++fc;
374
					}
377
					}
375
			}
378
			}
376
    return true;
379
    return true;
377
	}
380
	}
378
 
381
 
379
	FaceIter Manifold::split_face(FaceIter f, VertexIter v0, VertexIter v1)
382
	FaceIter Manifold::split_face(FaceIter f, VertexIter v0, VertexIter v1)
380
	{
383
	{
381
    // Make sure this is not a triangle
384
    // Make sure this is not a triangle
382
    assert(no_edges(f)>3);
385
    assert(no_edges(f)>3);
383
 
386
 
384
    // Make sure we are not trying to connect a vertex to itself.
387
    // Make sure we are not trying to connect a vertex to itself.
385
    assert(v0 != v1);
388
    assert(v0 != v1);
386
 
389
 
387
    // Find the halfedge emanating from v0 which belongs to the
390
    // Find the halfedge emanating from v0 which belongs to the
388
    // face, we need to split.
391
    // face, we need to split.
389
    VertexCirculator vc0(v0);
392
    VertexCirculator vc0(v0);
390
    while(vc0.get_halfedge()->face != f && !vc0.end()) vc0++;
393
    while(vc0.get_halfedge()->face != f && !vc0.end()) vc0++;
391
    assert(!vc0.end());
394
    assert(!vc0.end());
392
    HalfEdgeIter h0 = vc0.get_halfedge();
395
    HalfEdgeIter h0 = vc0.get_halfedge();
393
    assert(h0->face == f); // Sanity check.
396
    assert(h0->face == f); // Sanity check.
394
 
397
 
395
    // The halfedge belonging to f, going out from v0, is denoted h.
398
    // The halfedge belonging to f, going out from v0, is denoted h.
396
    // Move along h until we hit v1. Now we have the halfedge which
399
    // Move along h until we hit v1. Now we have the halfedge which
397
    // belongs to f and points to v1.
400
    // belongs to f and points to v1.
398
    HalfEdgeIter h = h0;
401
    HalfEdgeIter h = h0;
399
    while(h->vert != v1)
402
    while(h->vert != v1)
400
			h = h->next;
403
			h = h->next;
401
 
404
 
402
    // Create a new halfedge ha which connects v1 and v0 closing the first
405
    // Create a new halfedge ha which connects v1 and v0 closing the first
403
    // loop. This new halfedge becomes f->last
406
    // loop. This new halfedge becomes f->last
404
    assert(h != h0);
407
    assert(h != h0);
405
    HalfEdgeIter h1 = h->next;
408
    HalfEdgeIter h1 = h->next;
406
    HalfEdgeIter ha = create_halfedge();
409
    HalfEdgeIter ha = create_halfedge();
407
    link(h,ha);
410
    link(h,ha);
408
    link(ha, h0);
411
    link(ha, h0);
409
    ha->face = f;
412
    ha->face = f;
410
    ha->vert = v0;
413
    ha->vert = v0;
411
    f->last = ha;
414
    f->last = ha;
412
 
415
 
413
    // Create a new face, f2, and set all halfedges in the remaining part of
416
    // Create a new face, f2, and set all halfedges in the remaining part of
414
    // the polygon to point to this face.
417
    // the polygon to point to this face.
415
    h = h1;
418
    h = h1;
416
    FaceIter f2 = create_face();
419
    FaceIter f2 = create_face();
417
    while(h->vert != v0)
420
    while(h->vert != v0)
418
			{
421
			{
419
        h->face = f2;
422
        h->face = f2;
420
        h = h->next;
423
        h = h->next;
421
			}
424
			}
422
    h->face = f2;
425
    h->face = f2;
423
    assert(h != h1);
426
    assert(h != h1);
424
 
427
 
425
    // Create a new halfedge hb to connect v0 and v1. this new halfedge
428
    // Create a new halfedge hb to connect v0 and v1. this new halfedge
426
    // becomes f2->last
429
    // becomes f2->last
427
    HalfEdgeIter hb = create_halfedge();
430
    HalfEdgeIter hb = create_halfedge();
428
    link(h,hb);
431
    link(h,hb);
429
    link(hb, h1);
432
    link(hb, h1);
430
    hb->face = f2;
433
    hb->face = f2;
431
    hb->vert = v1;
434
    hb->vert = v1;
432
    f2->last = hb;
435
    f2->last = hb;
433
 
436
 
434
    // Complete the operation by gluing the two new halfedges.
437
    // Complete the operation by gluing the two new halfedges.
435
    glue(ha, hb);
438
    glue(ha, hb);
436
 
439
 
437
    // Assert that the pointers are sane :-)
440
    // Assert that the pointers are sane :-)
438
    assert(h1->prev->opp->next == h0);
441
    assert(h1->prev->opp->next == h0);
439
    assert(h0->prev->opp->next == h1);
442
    assert(h0->prev->opp->next == h1);
440
    assert(hb->next->face == f2);
443
    assert(hb->next->face == f2);
441
    assert(hb->next->next->face == f2);
444
    assert(hb->next->next->face == f2);
442
    assert(hb->face == f2);
445
    assert(hb->face == f2);
443
 
446
 
444
    // Return the newly created face
447
    // Return the newly created face
445
    return f2;
448
    return f2;
446
	}
449
	}
447
 
450
 
448
	void Manifold::triangulate(FaceIter f)
451
	void Manifold::triangulate(FaceIter f)
449
	{
452
	{
450
    // Assert sanity of pointers
453
    // Assert sanity of pointers
451
    assert(f->last->next->next != f->last);
454
    assert(f->last->next->next != f->last);
452
    assert(f->last->next != f->last);
455
    assert(f->last->next != f->last);
453
    assert(--(++f) == f);
456
    assert(--(++f) == f);
454
    // As long as f is not a triangle.
457
    // As long as f is not a triangle.
455
    while(f->last->next->next->next != f->last)
458
    while(f->last->next->next->next != f->last)
456
			{
459
			{
457
        assert(no_edges(f) != 3);
460
        assert(no_edges(f) != 3);
458
        // Call split face to split f into a triangle
461
        // Call split face to split f into a triangle
459
        // from the first three vertices and a polygon
462
        // from the first three vertices and a polygon
460
        // containing the rest of the vertices. In the
463
        // containing the rest of the vertices. In the
461
        // next iteration, f becomes this polygon.
464
        // next iteration, f becomes this polygon.
462
 
465
 
463
        assert(f->last->next->next != f->last);
466
        assert(f->last->next->next != f->last);
464
        VertexIter v0 = f->last->vert;
467
        VertexIter v0 = f->last->vert;
465
        VertexIter v1 = f->last->next->next->vert;
468
        VertexIter v1 = f->last->next->next->vert;
466
        assert(v0 != v1);
469
        assert(v0 != v1);
467
        f = split_face(f, v0, v1);
470
        f = split_face(f, v0, v1);
468
			}
471
			}
469
	}
472
	}
470
 
473
 
471
	bool Manifold::merge_faces(FaceIter f1, HalfEdgeIter h)
474
	bool Manifold::merge_faces(FaceIter f1, HalfEdgeIter h)
472
	{
475
	{
473
    assert(h->face == f1);
476
    assert(h->face == f1);
474
 
477
 
475
    HalfEdgeIter ho = h->opp;
478
    HalfEdgeIter ho = h->opp;
476
    FaceIter f2 = ho->face;
479
    FaceIter f2 = ho->face;
477
    HalfEdgeIter hn = h->next;
480
    HalfEdgeIter hn = h->next;
478
    HalfEdgeIter hon = ho->next;
481
    HalfEdgeIter hon = ho->next;
479
 
482
 
480
    if(hn->vert == hon->vert) return false;
483
    if(hn->vert == hon->vert) return false;
481
 
484
 
482
    HalfEdgeIter hp = h->prev;
485
    HalfEdgeIter hp = h->prev;
483
    HalfEdgeIter hop = ho->prev;
486
    HalfEdgeIter hop = ho->prev;
484
    VertexIter v  = h->vert;
487
    VertexIter v  = h->vert;
485
    VertexIter vo = ho->vert;
488
    VertexIter vo = ho->vert;
486
 
489
 
487
    link(hop, hn);
490
    link(hop, hn);
488
    v->he = hn;
491
    v->he = hn;
489
 
492
 
490
    link(hp, hon);
493
    link(hp, hon);
491
    vo->he = hon;
494
    vo->he = hon;
492
 
495
 
493
    f1->last = hn;
496
    f1->last = hn;
494
    HalfEdgeIter hx = hon;
497
    HalfEdgeIter hx = hon;
495
    assert(hx->face == f2);
498
    assert(hx->face == f2);
496
    while(hx->face != f1)
499
    while(hx->face != f1)
497
			{
500
			{
498
        hx->face = f1;
501
        hx->face = f1;
499
        hx = hx->next;
502
        hx = hx->next;
500
			}
503
			}
501
 
504
 
502
    check_boundary_consistency(v);
505
    check_boundary_consistency(v);
503
    check_boundary_consistency(vo);
506
    check_boundary_consistency(vo);
504
 
507
 
505
    erase_halfedge(h);
508
    erase_halfedge(h);
506
    erase_halfedge(ho);
509
    erase_halfedge(ho);
507
    erase_face(f2);
510
    erase_face(f2);
508
 
511
 
509
    return true;
512
    return true;
510
	}
513
	}
511
 
514
 
512
 
515
 
513
	VertexIter Manifold::safe_triangulate(FaceIter f)
516
	VertexIter Manifold::safe_triangulate(FaceIter f)
514
	{
517
	{
515
    Vec3f p(0.0f);
518
    Vec3f p(0.0f);
516
    FaceCirculator fc(f);
519
    FaceCirculator fc(f);
517
    for(;!fc.end(); ++fc)
520
    for(;!fc.end(); ++fc)
518
			p += fc.get_vertex()->pos;
521
			p += fc.get_vertex()->pos;
519
    int n = fc.no_steps();
522
    int n = fc.no_steps();
520
    p /= n;
523
    p /= n;
521
    VertexIter v = create_vertex(p);
524
    VertexIter v = create_vertex(p);
522
    face_insert_point(f,v);
525
    face_insert_point(f,v);
523
    return v;
526
    return v;
524
	}
527
	}
525
 
528
 
526
 
529
 
527
	void Manifold::face_insert_point(FaceIter f, VertexIter v)
530
	void Manifold::face_insert_point(FaceIter f, VertexIter v)
528
	{
531
	{
529
    vector<HalfEdgeIter> eout;
532
    vector<HalfEdgeIter> eout;
530
    vector<HalfEdgeIter> ein;
533
    vector<HalfEdgeIter> ein;
531
 
534
 
532
    HalfEdgeIter hlast = f->last;
535
    HalfEdgeIter hlast = f->last;
533
    HalfEdgeIter h = hlast;
536
    HalfEdgeIter h = hlast;
534
 
537
 
535
    do
538
    do
536
			{
539
			{
537
        HalfEdgeIter hn = h->next;
540
        HalfEdgeIter hn = h->next;
538
 
541
 
539
        HalfEdgeIter o = create_halfedge();
542
        HalfEdgeIter o = create_halfedge();
540
        HalfEdgeIter i = create_halfedge();
543
        HalfEdgeIter i = create_halfedge();
541
 
544
 
542
        FaceIter fn = create_face();
545
        FaceIter fn = create_face();
543
        i->face = fn;
546
        i->face = fn;
544
        i->vert = v;
547
        i->vert = v;
545
        o->face = fn;
548
        o->face = fn;
546
        o->vert = h->opp->vert;
549
        o->vert = h->opp->vert;
547
        h->face = fn;
550
        h->face = fn;
548
        fn->last = o;
551
        fn->last = o;
549
 
552
 
550
        link(i,o);
553
        link(i,o);
551
        link(o,h);
554
        link(o,h);
552
        link(h,i);
555
        link(h,i);
553
 
556
 
554
        eout.push_back(o);
557
        eout.push_back(o);
555
        ein.push_back(i);
558
        ein.push_back(i);
556
 
559
 
557
        h = hn;
560
        h = hn;
558
			}
561
			}
559
    while(h != hlast);
562
    while(h != hlast);
560
 
563
 
561
    int N = eout.size();
564
    int N = eout.size();
562
    for(int i=0;i<N;++i)
565
    for(int i=0;i<N;++i)
563
			glue(ein[i],eout[(i+1)%N]);
566
			glue(ein[i],eout[(i+1)%N]);
564
 
567
 
565
    v->he = eout[0];
568
    v->he = eout[0];
566
    erase_face(f);
569
    erase_face(f);
567
	}
570
	}
568
 
571
 
569
 
572
 
570
	bool Manifold::flip(HalfEdgeIter h1)
573
	bool Manifold::flip(HalfEdgeIter h1)
571
	{
574
	{
572
    FaceIter f1 = h1->face;
575
    FaceIter f1 = h1->face;
573
    HalfEdgeIter h2 = h1->opp;
576
    HalfEdgeIter h2 = h1->opp;
574
    FaceIter f2 = h2->face;
577
    FaceIter f2 = h2->face;
575
 
578
 
576
    if(f1 == NULL_FACE_ITER || f2 == NULL_FACE_ITER)
579
    if(f1 == NULL_FACE_ITER || f2 == NULL_FACE_ITER)
577
			return false;
580
			return false;
578
 
581
 
579
    // We can only flip an edge if both incident polygons
582
    // We can only flip an edge if both incident polygons
580
    // are triangles. Otherwise, this operation makes no sense.
583
    // are triangles. Otherwise, this operation makes no sense.
581
    if(no_edges(f1) != 3)
584
    if(no_edges(f1) != 3)
582
			return false;
585
			return false;
583
    if(no_edges(f2) !=3)
586
    if(no_edges(f2) !=3)
584
			return false;
587
			return false;
585
 
588
 
586
    // If the valency of either vertex incident on the edge
589
    // If the valency of either vertex incident on the edge
587
    // being flipped is three, we cannot perform this operation.
590
    // being flipped is three, we cannot perform this operation.
588
    // That is because the vertex would then have valency two
591
    // That is because the vertex would then have valency two
589
    // after the operation which means it would be degenerate.
592
    // after the operation which means it would be degenerate.
590
    VertexIter va = h1->vert;
593
    VertexIter va = h1->vert;
591
    VertexIter vb = h2->vert;
594
    VertexIter vb = h2->vert;
592
    int vala = valency(va);
595
    int vala = valency(va);
593
    int valb = valency(vb);
596
    int valb = valency(vb);
594
    if((vala <= 3 && !is_boundary(va)) ||
597
    if((vala <= 3 && !is_boundary(va)) ||
595
			 (valb <= 3 && !is_boundary(vb)))
598
			 (valb <= 3 && !is_boundary(vb)))
596
			return false;
599
			return false;
597
 
600
 
598
    // If the two vertices being connected by the flip are already
601
    // If the two vertices being connected by the flip are already
599
    // connected, the mesh would become degenerate, so we disallow
602
    // connected, the mesh would become degenerate, so we disallow
600
    // the operation.
603
    // the operation.
601
    VertexIter vc = h1->next->vert;
604
    VertexIter vc = h1->next->vert;
602
    VertexIter vd = h2->next->vert;
605
    VertexIter vd = h2->next->vert;
603
    if(is_connected(vc,vd))
606
    if(is_connected(vc,vd))
604
			return false;
607
			return false;
605
 
608
 
606
    HalfEdgeIter ha = h1->next;
609
    HalfEdgeIter ha = h1->next;
607
    HalfEdgeIter hb = h1->prev;
610
    HalfEdgeIter hb = h1->prev;
608
    HalfEdgeIter hc = h2->next;
611
    HalfEdgeIter hc = h2->next;
609
    HalfEdgeIter hd = h2->prev;
612
    HalfEdgeIter hd = h2->prev;
610
 
613
 
611
    hd->face = f1;
614
    hd->face = f1;
612
    hb->face = f2;
615
    hb->face = f2;
613
    f1->last = h1;
616
    f1->last = h1;
614
    f2->last = h2;
617
    f2->last = h2;
615
 
618
 
616
    link(ha,h1);
619
    link(ha,h1);
617
    link(h1,hd);
620
    link(h1,hd);
618
    link(hd,ha);
621
    link(hd,ha);
619
 
622
 
620
    link(hc,h2);
623
    link(hc,h2);
621
    link(h2,hb);
624
    link(h2,hb);
622
    link(hb,hc);
625
    link(hb,hc);
623
 
626
 
624
    h1->vert = vd;
627
    h1->vert = vd;
625
    h2->vert = vc;
628
    h2->vert = vc;
626
 
629
 
627
    va->he = ha;
630
    va->he = ha;
628
    vb->he = hc;
631
    vb->he = hc;
629
 
632
 
630
    check_boundary_consistency(va);
633
    check_boundary_consistency(va);
631
    check_boundary_consistency(vb);
634
    check_boundary_consistency(vb);
632
    return true;
635
    return true;
633
	}
636
	}
634
 
637
 
635
	/** Give each vertex a unique id corresponding to its iterator
638
	/** Give each vertex a unique id corresponding to its iterator
636
			position */
639
			position */
637
	void Manifold::enumerate_vertices()
640
	void Manifold::enumerate_vertices()
638
	{
641
	{
639
    int i=0;
642
    int i=0;
640
    for(VertexIter vi=vertices_begin(); vi != vertices_end(); ++vi)
643
    for(VertexIter vi=vertices_begin(); vi != vertices_end(); ++vi)
641
			vi->touched = i++;
644
			vi->touched = i++;
642
	}
645
	}
643
 
646
 
644
	/** Give each halfedge a unique id corresponding to its iterator
647
	/** Give each halfedge a unique id corresponding to its iterator
645
			position */
648
			position */
646
	void Manifold::enumerate_halfedges()
649
	void Manifold::enumerate_halfedges()
647
	{
650
	{
648
    int i=0;
651
    int i=0;
649
    for(HalfEdgeIter h=halfedges_begin(); h != halfedges_end(); ++h)
652
    for(HalfEdgeIter h=halfedges_begin(); h != halfedges_end(); ++h)
650
			h->touched = i++;
653
			h->touched = i++;
651
	}
654
	}
652
 
655
 
653
	/** Give each face a unique id corresponding to its iterator
656
	/** Give each face a unique id corresponding to its iterator
654
			position */
657
			position */
655
	void Manifold::enumerate_faces()
658
	void Manifold::enumerate_faces()
656
	{
659
	{
657
    int i=0;
660
    int i=0;
658
    for(FaceIter f=faces_begin(); f != faces_end(); ++f)
661
    for(FaceIter f=faces_begin(); f != faces_end(); ++f)
659
			f->touched = i++;
662
			f->touched = i++;
660
	}
663
	}
661
 
664
 
662
 
665
 
663
}
666
}
664
 
667
 
665
 
668