Subversion Repositories gelsvn

Rev

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

Rev 158 Rev 160
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
            cout << "Warning: A vertex with only two incident edges" << endl;
350
            cout << "Warning: A vertex with only two incident edges" << endl;
351
					}
351
					}
352
        assert(link.size()>=2);
352
        assert(link.size()>=2);
353
        ++vi;
353
        ++vi;
354
			}
354
			}
355
 
355
 
356
    HMesh::FaceIter f = faces_begin();
356
    HMesh::FaceIter f = faces_begin();
357
    for(;f != faces_end(); ++f)
357
    for(;f != faces_end(); ++f)
358
			{
358
			{
359
        if(no_edges(f)<3)
359
        if(no_edges(f)<3)
360
					{
360
					{
361
            cout << "Degenerate face" << endl;
361
            cout << "Degenerate face" << endl;
362
					}
362
					}
363
        FaceCirculator fc(f);
363
        FaceCirculator fc(f);
364
        int k=0;
364
        int k=0;
365
        while(!fc.end())
365
        while(!fc.end())
366
					{
366
					{
367
            if(fc.get_halfedge()->face != f)
367
            if(fc.get_halfedge()->face != f)
368
							{
368
							{
369
                cout << "Inconsistent face" << endl;
369
                cout << "Inconsistent face" << endl;
370
                return false;
370
                return false;
371
							}
371
							}
372
            if(++k==1e6) cout << "infin. loop around face" << endl;
372
            if(++k==1e6) cout << "infin. loop around face" << endl;
373
            ++fc;
373
            ++fc;
374
					}
374
					}
375
			}
375
			}
376
    return true;
376
    return true;
377
	}
377
	}
378
 
378
 
379
	FaceIter Manifold::split_face(FaceIter f, VertexIter v0, VertexIter v1)
379
	FaceIter Manifold::split_face(FaceIter f, VertexIter v0, VertexIter v1)
380
	{
380
	{
381
    // Make sure this is not a triangle
381
    // Make sure this is not a triangle
382
    assert(no_edges(f)>3);
382
    assert(no_edges(f)>3);
383
 
383
 
384
    // Make sure we are not trying to connect a vertex to itself.
384
    // Make sure we are not trying to connect a vertex to itself.
385
    assert(v0 != v1);
385
    assert(v0 != v1);
386
 
386
 
387
    // Find the halfedge emanating from v0 which belongs to the
387
    // Find the halfedge emanating from v0 which belongs to the
388
    // face, we need to split.
388
    // face, we need to split.
389
    VertexCirculator vc0(v0);
389
    VertexCirculator vc0(v0);
390
    while(vc0.get_halfedge()->face != f && !vc0.end()) vc0++;
390
    while(vc0.get_halfedge()->face != f && !vc0.end()) vc0++;
391
    assert(!vc0.end());
391
    assert(!vc0.end());
392
    HalfEdgeIter h0 = vc0.get_halfedge();
392
    HalfEdgeIter h0 = vc0.get_halfedge();
393
    assert(h0->face == f); // Sanity check.
393
    assert(h0->face == f); // Sanity check.
394
 
394
 
395
    // The halfedge belonging to f, going out from v0, is denoted h.
395
    // 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
396
    // Move along h until we hit v1. Now we have the halfedge which
397
    // belongs to f and points to v1.
397
    // belongs to f and points to v1.
398
    HalfEdgeIter h = h0;
398
    HalfEdgeIter h = h0;
399
    while(h->vert != v1)
399
    while(h->vert != v1)
400
			h = h->next;
400
			h = h->next;
401
 
401
 
402
    // Create a new halfedge ha which connects v1 and v0 closing the first
402
    // Create a new halfedge ha which connects v1 and v0 closing the first
403
    // loop. This new halfedge becomes f->last
403
    // loop. This new halfedge becomes f->last
404
    assert(h != h0);
404
    assert(h != h0);
405
    HalfEdgeIter h1 = h->next;
405
    HalfEdgeIter h1 = h->next;
406
    HalfEdgeIter ha = create_halfedge();
406
    HalfEdgeIter ha = create_halfedge();
407
    link(h,ha);
407
    link(h,ha);
408
    link(ha, h0);
408
    link(ha, h0);
409
    ha->face = f;
409
    ha->face = f;
410
    ha->vert = v0;
410
    ha->vert = v0;
411
    f->last = ha;
411
    f->last = ha;
412
 
412
 
413
    // Create a new face, f2, and set all halfedges in the remaining part of
413
    // Create a new face, f2, and set all halfedges in the remaining part of
414
    // the polygon to point to this face.
414
    // the polygon to point to this face.
415
    h = h1;
415
    h = h1;
416
    FaceIter f2 = create_face();
416
    FaceIter f2 = create_face();
417
    while(h->vert != v0)
417
    while(h->vert != v0)
418
			{
418
			{
419
        h->face = f2;
419
        h->face = f2;
420
        h = h->next;
420
        h = h->next;
421
			}
421
			}
422
    h->face = f2;
422
    h->face = f2;
423
    assert(h != h1);
423
    assert(h != h1);
424
 
424
 
425
    // Create a new halfedge hb to connect v0 and v1. this new halfedge
425
    // Create a new halfedge hb to connect v0 and v1. this new halfedge
426
    // becomes f2->last
426
    // becomes f2->last
427
    HalfEdgeIter hb = create_halfedge();
427
    HalfEdgeIter hb = create_halfedge();
428
    link(h,hb);
428
    link(h,hb);
429
    link(hb, h1);
429
    link(hb, h1);
430
    hb->face = f2;
430
    hb->face = f2;
431
    hb->vert = v1;
431
    hb->vert = v1;
432
    f2->last = hb;
432
    f2->last = hb;
433
 
433
 
434
    // Complete the operation by gluing the two new halfedges.
434
    // Complete the operation by gluing the two new halfedges.
435
    glue(ha, hb);
435
    glue(ha, hb);
436
 
436
 
437
    // Assert that the pointers are sane :-)
437
    // Assert that the pointers are sane :-)
438
    assert(h1->prev->opp->next == h0);
438
    assert(h1->prev->opp->next == h0);
439
    assert(h0->prev->opp->next == h1);
439
    assert(h0->prev->opp->next == h1);
440
    assert(hb->next->face == f2);
440
    assert(hb->next->face == f2);
441
    assert(hb->next->next->face == f2);
441
    assert(hb->next->next->face == f2);
442
    assert(hb->face == f2);
442
    assert(hb->face == f2);
443
 
443
 
444
    // Return the newly created face
444
    // Return the newly created face
445
    return f2;
445
    return f2;
446
	}
446
	}
447
 
447
 
448
	void Manifold::triangulate(FaceIter f)
448
	void Manifold::triangulate(FaceIter f)
449
	{
449
	{
450
    // Assert sanity of pointers
450
    // Assert sanity of pointers
451
    assert(f->last->next->next != f->last);
451
    assert(f->last->next->next != f->last);
452
    assert(f->last->next != f->last);
452
    assert(f->last->next != f->last);
453
    assert(--(++f) == f);
453
    assert(--(++f) == f);
454
    // As long as f is not a triangle.
454
    // As long as f is not a triangle.
455
    while(f->last->next->next->next != f->last)
455
    while(f->last->next->next->next != f->last)
456
			{
456
			{
457
        assert(no_edges(f) != 3);
457
        assert(no_edges(f) != 3);
458
        // Call split face to split f into a triangle
458
        // Call split face to split f into a triangle
459
        // from the first three vertices and a polygon
459
        // from the first three vertices and a polygon
460
        // containing the rest of the vertices. In the
460
        // containing the rest of the vertices. In the
461
        // next iteration, f becomes this polygon.
461
        // next iteration, f becomes this polygon.
462
 
462
 
463
        assert(f->last->next->next != f->last);
463
        assert(f->last->next->next != f->last);
464
        VertexIter v0 = f->last->vert;
464
        VertexIter v0 = f->last->vert;
465
        VertexIter v1 = f->last->next->next->vert;
465
        VertexIter v1 = f->last->next->next->vert;
466
        assert(v0 != v1);
466
        assert(v0 != v1);
467
        f = split_face(f, v0, v1);
467
        f = split_face(f, v0, v1);
468
			}
468
			}
469
	}
469
	}
470
 
470
 
471
	bool Manifold::merge_faces(FaceIter f1, HalfEdgeIter h)
471
	bool Manifold::merge_faces(FaceIter f1, HalfEdgeIter h)
472
	{
472
	{
473
    assert(h->face == f1);
473
    assert(h->face == f1);
474
 
474
 
475
    HalfEdgeIter ho = h->opp;
475
    HalfEdgeIter ho = h->opp;
476
    FaceIter f2 = ho->face;
476
    FaceIter f2 = ho->face;
477
    HalfEdgeIter hn = h->next;
477
    HalfEdgeIter hn = h->next;
478
    HalfEdgeIter hon = ho->next;
478
    HalfEdgeIter hon = ho->next;
479
 
479
 
480
    if(hn->vert == hon->vert) return false;
480
    if(hn->vert == hon->vert) return false;
481
 
481
 
482
    HalfEdgeIter hp = h->prev;
482
    HalfEdgeIter hp = h->prev;
483
    HalfEdgeIter hop = ho->prev;
483
    HalfEdgeIter hop = ho->prev;
484
    VertexIter v  = h->vert;
484
    VertexIter v  = h->vert;
485
    VertexIter vo = ho->vert;
485
    VertexIter vo = ho->vert;
486
 
486
 
487
    link(hop, hn);
487
    link(hop, hn);
488
    v->he = hn;
488
    v->he = hn;
489
 
489
 
490
    link(hp, hon);
490
    link(hp, hon);
491
    vo->he = hon;
491
    vo->he = hon;
492
 
492
 
493
    f1->last = hn;
493
    f1->last = hn;
494
    HalfEdgeIter hx = hon;
494
    HalfEdgeIter hx = hon;
495
    assert(hx->face == f2);
495
    assert(hx->face == f2);
496
    while(hx->face != f1)
496
    while(hx->face != f1)
497
			{
497
			{
498
        hx->face = f1;
498
        hx->face = f1;
499
        hx = hx->next;
499
        hx = hx->next;
500
			}
500
			}
501
 
501
 
502
    check_boundary_consistency(v);
502
    check_boundary_consistency(v);
503
    check_boundary_consistency(vo);
503
    check_boundary_consistency(vo);
504
 
504
 
505
    erase_halfedge(h);
505
    erase_halfedge(h);
506
    erase_halfedge(ho);
506
    erase_halfedge(ho);
507
    erase_face(f2);
507
    erase_face(f2);
508
 
508
 
509
    return true;
509
    return true;
510
	}
510
	}
511
 
511
 
512
 
512
 
513
	VertexIter Manifold::safe_triangulate(FaceIter f)
513
	VertexIter Manifold::safe_triangulate(FaceIter f)
514
	{
514
	{
515
    Vec3f p;
515
    Vec3f p(0.0f);
516
    FaceCirculator fc(f);
516
    FaceCirculator fc(f);
517
    for(;!fc.end(); ++fc)
517
    for(;!fc.end(); ++fc)
518
			p += fc.get_vertex()->pos;
518
			p += fc.get_vertex()->pos;
519
    int n = fc.no_steps();
519
    int n = fc.no_steps();
520
    p /= n;
520
    p /= n;
521
    VertexIter v = create_vertex(p);
521
    VertexIter v = create_vertex(p);
522
    face_insert_point(f,v);
522
    face_insert_point(f,v);
523
    return v;
523
    return v;
524
	}
524
	}
525
 
525
 
526
 
526
 
527
	void Manifold::face_insert_point(FaceIter f, VertexIter v)
527
	void Manifold::face_insert_point(FaceIter f, VertexIter v)
528
	{
528
	{
529
    vector<HalfEdgeIter> eout;
529
    vector<HalfEdgeIter> eout;
530
    vector<HalfEdgeIter> ein;
530
    vector<HalfEdgeIter> ein;
531
 
531
 
532
    HalfEdgeIter hlast = f->last;
532
    HalfEdgeIter hlast = f->last;
533
    HalfEdgeIter h = hlast;
533
    HalfEdgeIter h = hlast;
534
 
534
 
535
    do
535
    do
536
			{
536
			{
537
        HalfEdgeIter hn = h->next;
537
        HalfEdgeIter hn = h->next;
538
 
538
 
539
        HalfEdgeIter o = create_halfedge();
539
        HalfEdgeIter o = create_halfedge();
540
        HalfEdgeIter i = create_halfedge();
540
        HalfEdgeIter i = create_halfedge();
541
 
541
 
542
        FaceIter fn = create_face();
542
        FaceIter fn = create_face();
543
        i->face = fn;
543
        i->face = fn;
544
        i->vert = v;
544
        i->vert = v;
545
        o->face = fn;
545
        o->face = fn;
546
        o->vert = h->opp->vert;
546
        o->vert = h->opp->vert;
547
        h->face = fn;
547
        h->face = fn;
548
        fn->last = o;
548
        fn->last = o;
549
 
549
 
550
        link(i,o);
550
        link(i,o);
551
        link(o,h);
551
        link(o,h);
552
        link(h,i);
552
        link(h,i);
553
 
553
 
554
        eout.push_back(o);
554
        eout.push_back(o);
555
        ein.push_back(i);
555
        ein.push_back(i);
556
 
556
 
557
        h = hn;
557
        h = hn;
558
			}
558
			}
559
    while(h != hlast);
559
    while(h != hlast);
560
 
560
 
561
    int N = eout.size();
561
    int N = eout.size();
562
    for(int i=0;i<N;++i)
562
    for(int i=0;i<N;++i)
563
			glue(ein[i],eout[(i+1)%N]);
563
			glue(ein[i],eout[(i+1)%N]);
564
 
564
 
565
    v->he = eout[0];
565
    v->he = eout[0];
566
    erase_face(f);
566
    erase_face(f);
567
	}
567
	}
568
 
568
 
569
 
569
 
570
	bool Manifold::flip(HalfEdgeIter h1)
570
	bool Manifold::flip(HalfEdgeIter h1)
571
	{
571
	{
572
    FaceIter f1 = h1->face;
572
    FaceIter f1 = h1->face;
573
    HalfEdgeIter h2 = h1->opp;
573
    HalfEdgeIter h2 = h1->opp;
574
    FaceIter f2 = h2->face;
574
    FaceIter f2 = h2->face;
575
 
575
 
576
    if(f1 == NULL_FACE_ITER || f2 == NULL_FACE_ITER)
576
    if(f1 == NULL_FACE_ITER || f2 == NULL_FACE_ITER)
577
			return false;
577
			return false;
578
 
578
 
579
    // We can only flip an edge if both incident polygons
579
    // We can only flip an edge if both incident polygons
580
    // are triangles. Otherwise, this operation makes no sense.
580
    // are triangles. Otherwise, this operation makes no sense.
581
    if(no_edges(f1) != 3)
581
    if(no_edges(f1) != 3)
582
			return false;
582
			return false;
583
    if(no_edges(f2) !=3)
583
    if(no_edges(f2) !=3)
584
			return false;
584
			return false;
585
 
585
 
586
    // If the valency of either vertex incident on the edge
586
    // If the valency of either vertex incident on the edge
587
    // being flipped is three, we cannot perform this operation.
587
    // being flipped is three, we cannot perform this operation.
588
    // That is because the vertex would then have valency two
588
    // That is because the vertex would then have valency two
589
    // after the operation which means it would be degenerate.
589
    // after the operation which means it would be degenerate.
590
    VertexIter va = h1->vert;
590
    VertexIter va = h1->vert;
591
    VertexIter vb = h2->vert;
591
    VertexIter vb = h2->vert;
592
    int vala = valency(va);
592
    int vala = valency(va);
593
    int valb = valency(vb);
593
    int valb = valency(vb);
594
    if((vala <= 3 && !is_boundary(va)) ||
594
    if((vala <= 3 && !is_boundary(va)) ||
595
			 (valb <= 3 && !is_boundary(vb)))
595
			 (valb <= 3 && !is_boundary(vb)))
596
			return false;
596
			return false;
597
 
597
 
598
    // If the two vertices being connected by the flip are already
598
    // If the two vertices being connected by the flip are already
599
    // connected, the mesh would become degenerate, so we disallow
599
    // connected, the mesh would become degenerate, so we disallow
600
    // the operation.
600
    // the operation.
601
    VertexIter vc = h1->next->vert;
601
    VertexIter vc = h1->next->vert;
602
    VertexIter vd = h2->next->vert;
602
    VertexIter vd = h2->next->vert;
603
    if(is_connected(vc,vd))
603
    if(is_connected(vc,vd))
604
			return false;
604
			return false;
605
 
605
 
606
    HalfEdgeIter ha = h1->next;
606
    HalfEdgeIter ha = h1->next;
607
    HalfEdgeIter hb = h1->prev;
607
    HalfEdgeIter hb = h1->prev;
608
    HalfEdgeIter hc = h2->next;
608
    HalfEdgeIter hc = h2->next;
609
    HalfEdgeIter hd = h2->prev;
609
    HalfEdgeIter hd = h2->prev;
610
 
610
 
611
    hd->face = f1;
611
    hd->face = f1;
612
    hb->face = f2;
612
    hb->face = f2;
613
    f1->last = h1;
613
    f1->last = h1;
614
    f2->last = h2;
614
    f2->last = h2;
615
 
615
 
616
    link(ha,h1);
616
    link(ha,h1);
617
    link(h1,hd);
617
    link(h1,hd);
618
    link(hd,ha);
618
    link(hd,ha);
619
 
619
 
620
    link(hc,h2);
620
    link(hc,h2);
621
    link(h2,hb);
621
    link(h2,hb);
622
    link(hb,hc);
622
    link(hb,hc);
623
 
623
 
624
    h1->vert = vd;
624
    h1->vert = vd;
625
    h2->vert = vc;
625
    h2->vert = vc;
626
 
626
 
627
    va->he = ha;
627
    va->he = ha;
628
    vb->he = hc;
628
    vb->he = hc;
629
 
629
 
630
    check_boundary_consistency(va);
630
    check_boundary_consistency(va);
631
    check_boundary_consistency(vb);
631
    check_boundary_consistency(vb);
632
    return true;
632
    return true;
633
	}
633
	}
634
 
634
 
635
	/** Give each vertex a unique id corresponding to its iterator
635
	/** Give each vertex a unique id corresponding to its iterator
636
			position */
636
			position */
637
	void Manifold::enumerate_vertices()
637
	void Manifold::enumerate_vertices()
638
	{
638
	{
639
    int i=0;
639
    int i=0;
640
    for(VertexIter vi=vertices_begin(); vi != vertices_end(); ++vi)
640
    for(VertexIter vi=vertices_begin(); vi != vertices_end(); ++vi)
641
			vi->touched = i++;
641
			vi->touched = i++;
642
	}
642
	}
643
 
643
 
644
	/** Give each halfedge a unique id corresponding to its iterator
644
	/** Give each halfedge a unique id corresponding to its iterator
645
			position */
645
			position */
646
	void Manifold::enumerate_halfedges()
646
	void Manifold::enumerate_halfedges()
647
	{
647
	{
648
    int i=0;
648
    int i=0;
649
    for(HalfEdgeIter h=halfedges_begin(); h != halfedges_end(); ++h)
649
    for(HalfEdgeIter h=halfedges_begin(); h != halfedges_end(); ++h)
650
			h->touched = i++;
650
			h->touched = i++;
651
	}
651
	}
652
 
652
 
653
	/** Give each face a unique id corresponding to its iterator
653
	/** Give each face a unique id corresponding to its iterator
654
			position */
654
			position */
655
	void Manifold::enumerate_faces()
655
	void Manifold::enumerate_faces()
656
	{
656
	{
657
    int i=0;
657
    int i=0;
658
    for(FaceIter f=faces_begin(); f != faces_end(); ++f)
658
    for(FaceIter f=faces_begin(); f != faces_end(); ++f)
659
			f->touched = i++;
659
			f->touched = i++;
660
	}
660
	}
661
 
661
 
662
 
662
 
663
}
663
}
664
 
664
 
665
 
665