Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bmesh_mods.c « intern « bmesh « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 33b131f5f1d33779dba0f8bce94fc4c1cbd7979c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include <limits.h>
#include "MEM_guardedalloc.h"

#include "DNA_listBase.h"
#include "BKE_utildefines.h"
#include "BLI_blenlib.h"
#include "BLI_linklist.h"
#include "BLI_ghash.h"
#include "BLI_arithb.h"
#include "BLI_array.h"

#include "bmesh.h"
#include "bmesh_private.h"

#include <stdlib.h>
#include <string.h>

/*
 * BME_MODS.C
 *
 * This file contains functions for locally modifying
 * the topology of existing mesh data. (split, join, flip ect).
 *
*/

/**
 *			bmesh_dissolve_disk
 *
 *  Turns the face region surrounding a manifold vertex into 
 *  A single polygon.
 *
 * 
 * Example:
 * 
 *          |=========|             |=========|
 *          |  \   /  |             |         |
 * Before:  |    V    |      After: |         |
 *          |  /   \  |             |         |
 *          |=========|             |=========|
 * 
 * 
 */
#if 1
int BM_Dissolve_Vert(BMesh *bm, BMVert *v) {
	BMIter iter;
	BMEdge *e;
	int len=0;

	if (!v) return 0;
	
	e = BMIter_New(&iter, bm, BM_EDGES_OF_VERT, v);
	for (; e; e=BMIter_Step(&iter)) {
		len++;
	}
	
	if (len == 1) {
		bmesh_ke(bm, v->edge);
		bmesh_kv(bm, v);
		return 1;
	}

	if(BM_Nonmanifold_Vert(bm, v)) {
		if (!v->edge) bmesh_kv(bm, v);
		else if (!v->edge->loop) {
			bmesh_ke(bm, v->edge);
			bmesh_kv(bm, v);
		} else return 0;

		return 1;
	}

	return BM_Dissolve_Disk(bm, v);
}

int BM_Dissolve_Disk(BMesh *bm, BMVert *v) {
	BMFace *f, *f2;
	BMEdge *e, *keepedge=NULL, *baseedge=NULL;
	BMLoop *loop;
	int done, len;

	if(BM_Nonmanifold_Vert(bm, v)) {
		return 0;
	}
	
	if(v->edge){
		/*v->edge we keep, what else?*/
		e = v->edge;
		len = 0;
		do{
			e = bmesh_disk_nextedge(e,v);
			if(!(BM_Edge_Share_Faces(e, v->edge))){
				keepedge = e;
				baseedge = v->edge;
				break;
			}
			len++;
		}while(e != v->edge);
	}
	
	/*this code for handling 2 and 3-valence verts
	  may be totally bad.*/
	if (keepedge == NULL && len == 3) {
		/*handle specific case for three-valence.  solve it by
		  increasing valence to four.  this may be hackish. . .*/
		loop = e->loop;
		if (loop->v == v) loop = (BMLoop*) loop->head.next;
		if (!BM_Split_Face(bm, loop->f, v, loop->v, NULL, NULL))
			return 0;

		BM_Dissolve_Disk(bm, v);
		return 1;
	} else if (keepedge == NULL && len == 2) {
		/*handle two-valence*/
		f = v->edge->loop->f;
		f2 = ((BMLoop*)v->edge->loop->radial.next->data)->f;
		/*collapse the vertex*/
		BM_Collapse_Vert(bm, v->edge, v, 1.0);
		BM_Join_Faces(bm, f, f2, NULL);

		return 1;
	}

	if(keepedge){
		done = 0;
		while(!done){
			done = 1;
			e = v->edge;
			do{
				f = NULL;
				len = bmesh_cycle_length(&(e->loop->radial));
				if(len == 2 && (e!=baseedge) && (e!=keepedge)) {
					f = BM_Join_Faces(bm, e->loop->f, ((BMLoop*)(e->loop->radial.next->data))->f, e); 
					/*return if couldn't join faces in manifold
					  conditions.*/
					//!disabled for testing why bad things happen
					if (!f) return 0;
				}

				if(f){
					done = 0;
					break;
				}
				e = bmesh_disk_nextedge(e, v);
			}while(e != v->edge);
		}

		/*get remaining two faces*/
		f = v->edge->loop->f;
		f2 = ((BMLoop*)v->edge->loop->radial.next->data)->f;

		/*collapse the vertex*/
		BM_Collapse_Vert(bm, baseedge, v, 1.0);
		
		if (f != f2) {
			/*join two remaining faces*/
			if (!BM_Join_Faces(bm, f, f2, NULL)) return 0;
		}
	}

	return 1;
}
#else
void BM_Dissolve_Disk(BMesh *bm, BMVert *v){
	BMFace *f;
	BMEdge *e;
	BMIter iter;
	int done, len;
	
	if(v->edge){
		done = 0;
		while(!done){
			done = 1;
			
			/*loop the edges looking for an edge to dissolve*/
			for (e=BMIter_New(&iter, bm, BM_EDGES_OF_VERT, v); e;
			     e = BMIter_Step(&iter)) {
				f = NULL;
				len = bmesh_cycle_length(&(e->loop->radial));
				if(len == 2){
					f = BM_Join_Faces(bm,e->loop->f,((BMLoop*)
					      (e->loop->radial.next->data))->f, 
					       e);
				}
				if(f){ 
					done = 0;
					break;
				}
			};
		}
		BM_Collapse_Vert(bm, v->edge, v, 1.0);
	}
}
#endif

/**
 *			bmesh_join_faces
 *
 *  joins two adjacenct faces togather.
 * 
 *  Returns -
 *	BMFace pointer
 */
 
BMFace *BM_Join_Faces(BMesh *bm, BMFace *f1, BMFace *f2, BMEdge *e) {

	BMLoop *l1, *l2;
	BMEdge *jed=NULL;
	
	jed = e;
	if(!jed){
		/*search for an edge that has both these faces in its radial cycle*/
		l1 = f1->loopbase;
		do{
			if( ((BMLoop*)l1->radial.next->data)->f == f2 ){
				jed = l1->e;
				break;
			}
			l1 = ((BMLoop*)(l1->head.next));
		}while(l1!=f1->loopbase);
	}

	l1 = jed->loop;
	l2 = l1->radial.next->data;
	if (l1->v == l2->v) {
		bmesh_loop_reverse(bm, f2);
	}

	f1 = bmesh_jfke(bm, f1, f2, jed);
	
	return f1;
}

/*connects two verts together, automatically (if very naively) finding the
  face they both share (if there is one) and splittling it.  use this at your 
  own risk, as it doesn't handle the many complex cases it should (like zero-area faces,
  multiple faces, etc).

  this is really only meant for cases where you don't know before hand the face
  the two verts belong to for splitting (e.g. the subdivision operator).
*/

BMEdge *BM_Connect_Verts(BMesh *bm, BMVert *v1, BMVert *v2, BMFace **nf) {
	BMIter iter, iter2;
	BMVert *v;
	BMLoop *nl;
	BMFace *face;

	/*this isn't the best thing in the world.  it doesn't handle cases where there's
	  multiple faces yet.  that might require a convexity test to figure out which
	  face is "best," and who knows what for non-manifold conditions.*/
	for (face = BMIter_New(&iter, bm, BM_FACES_OF_VERT, v1); face; face=BMIter_Step(&iter)) {
		for (v=BMIter_New(&iter2, bm, BM_VERTS_OF_FACE, face); v; v=BMIter_Step(&iter2)) {
			if (v == v2) {
				face = BM_Split_Face(bm, face, v1, v2, &nl, NULL);

				if (nf) *nf = face;
				return nl->e;
			}
		}
	}

	return NULL;
}

/**
 *			BM_split_face
 *
 *  Splits a single face into two.
 * 
 *  Returns -
 *	BMFace pointer
 */
 
BMFace *BM_Split_Face(BMesh *bm, BMFace *f, BMVert *v1, BMVert *v2, BMLoop **nl, BMEdge *example)
{
	BMFace *nf;
	nf = bmesh_sfme(bm,f,v1,v2,nl);
	
	if (nf) {
		BM_Copy_Attributes(bm, bm, f, nf);
		VECCOPY(nf->no, f->no);
	}

	return nf;
}

/**
 *			bmesh_collapse_vert
 *
 *  Collapses a vertex that has only two manifold edges
 *  onto a vertex it shares an edge with. Fac defines
 *  the amount of interpolation for Custom Data.
 *
 *  Note that this is not a general edge collapse function. For
 *  that see BM_manifold_edge_collapse 
 *
 *  TODO:
 *    Insert error checking for KV valance.
 *
 *  Returns -
 *	Nothing
 */
 
void BM_Collapse_Vert(BMesh *bm, BMEdge *ke, BMVert *kv, float fac){
	void *src[2];
	float w[2];
	BMLoop *l=NULL, *kvloop=NULL, *tvloop=NULL;
	BMVert *tv = bmesh_edge_getothervert(ke,kv);

	w[0] = 1.0f - fac;
	w[1] = fac;

	if(ke->loop){
		l = ke->loop;
		do{
			if(l->v == tv && ((BMLoop*)(l->head.next))->v == kv){
				tvloop = l;
				kvloop = ((BMLoop*)(l->head.next));

				src[0] = kvloop->head.data;
				src[1] = tvloop->head.data;
				CustomData_bmesh_interp(&bm->ldata, src,w, NULL, 2, kvloop->head.data);
			}
			l=l->radial.next->data;
		}while(l!=ke->loop);
	}
	BM_Data_Interp_From_Verts(bm, kv, tv, kv, fac);   
	bmesh_jekv(bm,ke,kv);
}

/**
 *			BM_split_edge
 *	
 *	Splits an edge. v should be one of the vertices in e and
 *  defines the direction of the splitting operation for interpolation
 *  purposes.
 *
 *  Returns -
 *	the new vert
 */

BMVert *BM_Split_Edge(BMesh *bm, BMVert *v, BMEdge *e, BMEdge **ne, float percent) {
	BMVert *nv, *v2;

	v2 = bmesh_edge_getothervert(e,v);
	nv = bmesh_semv(bm,v,e,ne);
	if (nv == NULL) return NULL;

	VECSUB(nv->co,v2->co,v->co);
	VECADDFAC(nv->co,v->co,nv->co,percent);

	if (ne) {
		(*ne)->head.flag = e->head.flag;
		BM_Copy_Attributes(bm, bm, e, *ne);
	}

	/*v->nv->v2*/
	BM_Data_Facevert_Edgeinterp(bm,v2, v, nv, e, percent);	
	BM_Data_Interp_From_Verts(bm, v2, v, nv, percent);
	return nv;
}

BMVert  *BM_Split_Edge_Multi(BMesh *bm, BMEdge *e, int numcuts)
{
	int i;
	float percent;
	BMVert *nv = NULL;
	
	for(i=0; i < numcuts; i++){
		percent = 1.0f / (float)(numcuts + 1 - i);
		nv = BM_Split_Edge(bm, e->v2, e, NULL, percent);
	}
	return nv;
}

int BM_Validate_Face(BMesh *bm, BMFace *face, FILE *err) 
{
	BMIter iter;
	BLI_array_declare(verts);
	BMVert **verts = NULL;
	BMLoop *l;
	int ret = 1, i, j;
	
	if (face->len == 2) {
		fprintf(err, "warning: found two-edged face. face ptr: %p\n", face);
		fflush(err);
	}

	for (l=BMIter_New(&iter, bm, BM_LOOPS_OF_FACE, face);l;l=BMIter_Step(&iter)) {
		BLI_array_growone(verts);
		verts[BLI_array_count(verts)-1] = l->v;
		
		if (l->e->v1 == l->e->v2) {
			fprintf(err, "Found bmesh edge with identical verts!\n");
			fprintf(err, "  edge ptr: %p, vert: %p\n",  l->e, l->e->v1);
			fflush(err);
			ret = 0;
		}
	}

	for (i=0; i<BLI_array_count(verts); i++) {
		for (j=0; j<BLI_array_count(verts); j++) {
			if (j == i) continue;
			if (verts[i] == verts[j]) {
				fprintf(err, "Found duplicate verts in bmesh face!\n");
				fprintf(err, "  face ptr: %p, vert: %p\n", face, verts[i]);
				fflush(err);
				ret = 0;
			}
		}
	}
	
	BLI_array_free(verts);
	return ret;
}

/*
            BM Rotate Edge

    Spins an edge topologically, either counter-clockwise or clockwise.
    If ccw is true, the edge is spun counter-clockwise, otherwise it is
    spun clockwise.
    
    Returns the spun edge.  Note that this works by dissolving the edge
    then re-creating it, so the returned edge won't have the same pointer
    address as the original one.

    Returns NULL on error (e.g., if the edge isn't surrounded by exactly
    two faces).
*/
BMEdge *BM_Rotate_Edge(BMesh *bm, BMEdge *e, int ccw)
{
	BMVert *v1, *v2;
	BMLoop *l, *l1, *l2, *nl;
	BMFace *f;
	BMIter liter;

	v1 = e->v1;
	v2 = e->v2;

	if (BM_Edge_FaceCount(e) != 2)
		return NULL;

	f = BM_Join_Faces(bm, e->loop->f, ((BMLoop*)e->loop->radial.next->data)->f, e);
	
	BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
		if (l->v == v1)
			l1 = l;
		else if (l->v == v2)
			l2 = l;
	}
	
	if (ccw) {
		l1 = (BMLoop*) l1->head.prev;
		l2 = (BMLoop*) l2->head.prev;
	} else {
		l1 = (BMLoop*) l1->head.next;
		l2 = (BMLoop*) l2->head.next;
	}

	if (!BM_Split_Face(bm, f, l1->v, l2->v, &nl, NULL))
		return NULL;

	return nl->e;
}