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

BME_duplicate.c « tools « bmesh « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec3cfe56fc9babf870e0711305de53397dd8d4ee (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
/*
 *  BME_DUPLICATE.C
 *
 *  This file contains functions for duplicating, copying, and splitting
 *  elements from a bmesh.
 *
 */

/*
 *  BMESH COPY VERTEX
 *
 *   Copy an existing vertex from one bmesh to another.
 *
*/

static BMVert *bmesh_copy_vertex(BMMesh *source_mesh, BMVert *source_vertex, BMMesh *target_mesh, GHash *vhash)
{
	BMVert *target_vertex = NULL;

	/*create a new vertex*/
	target_vertex = bmesh_make_vert(target, source_vertex->co,  NULL);

	/*insert new vertex into the vert hash*/
	BLI_ghash_insert(vhash, source_vertex, target_vertex);	
	
	/*copy custom data in this function since we cannot be assured that byte layout is same between meshes*/
	CustomData_bmesh_copy_data(&source_mesh->vdata, &target_mesh->vdata, source_vertex->data, &target_vertex->data);	

	/*copy flags*/
	if(bmesh_test_flag(source_vertex, BMESH_SELECT)) bmesh_set_flag(target_vertex, BMESH_SELECT);
	if(bmesh_test_flag(source_vertex, BMESH_HIDDEN)) bmesh_set_flag(target_vertex, BMESH_HIDDEN);

	return target_vertex;
}

/*
 * BMESH COPY EDGE
 *
 * Copy an existing edge from one bmesh to another.
 *
*/

static BMEdge *bmesh_copy_edge(BMMesh *source_mesh, BMEdge *source_edge, BMMesh *target_mesh, GHash *vhash, GHash *ehash)
{
	BMEdge *target_edge = NULL;
	BMVert *target_vert1, *target_vert2;
	
	/*lookup v1 and v2*/
	target_vert1 = BLI_ghash_lookup(vhash, source_edge->v1);
	target_vert2 = BLI_ghash_lookup(vhash, source_edge->v2);
	
	/*create a new edge*/
	target_edge = bmesh_make_edge(target_mesh, target_vert1, target_vert2, NULL, 0);

	/*insert new edge into the edge hash*/
	BLI_ghash_insert(ehash, source_edge, target_edge);	
	
	/*copy custom data in this function since we cannot be assured that byte layout is same between meshes*/	
	CustomData_bmesh_copy_data(&source_mesh->edata, &target_mesh->edata, source_edge->data, &target_edge->data);
	
	/*copy flags*/
	if(bmesh_test_flag(source_edge, BMESH_SELECT)) bmesh_set_flag(target_edge, BMESH_SELECT);
	if(bmesh_test_flag(source_edge, BMESH_HIDDEN)) bmesh_set_flag(target_edge, BMESH_SELECT);
	if(bmesh_test_flag(source_edge, BMESH_SHARP)) bmesh_set_flag(target_edge, BMESH_SHARP);
	if(bmesh_test_flag(source_edge, BMESH_SEAM)) bmesh_set_flag(target_edge, BMESH_SEAM);
	if(bmesh_test_flag(source_edge, BMESH_FGON)) bmesh_set_flag(target_edge, BMESH_FGON);
	
	return target_edge;
}

/*
 * BMESH COPY FACE
 *
 *  Copy an existing face from one bmesh to another.
 *
*/

static BMFace *bmesh_copy_face(BMMesh *source_mesh, BMFace *source_face, BMMesh *target_mesh, BMEdge **edar, GHash *verthash, GHash *ehash)
{
	BMEdge  *target_edge;
	BMVert *target_vert1, *target_vert2;
	BMLoop *source_loop, *target_loop;
	BMFace *target_face = NULL;
	int i;
	
	
	/*lookup the first and second verts*/
	target_vert1 = BLI_ghash_lookup(vhash, source_face->lbase->v);
	target_vert2 = BLI_ghash_lookup(vhash, source_face->lbase->next->v);
	
	/*lookup edges*/
	i = 0;
	source_loop = source_face->lbase;
	do{
		edar[i] = BLI_ghash_lookup(ehash, source_loop->e);
		i++;
		source_loop = source_loop->next;
	}while(source_loop != source_face->lbase);
	
	/*create new face*/
	target_face = bmesh_make_ngon(target_mesh, target_vert1, target_vert2, edar, source_face->len, 0);	
	
	/*we copy custom data by hand, we cannot assume that customdata byte layout will be exactly the same....*/
	CustomData_bmesh_copy_data(&source_mesh->pdata, &target_mesh->pdata, source_face->data, &target_face->data);	

	/*copy flags*/
	if(bmesh_test_flag(source_face, BMESH_SELECT)) bmesh_set_flag(target_face, BMESH_SELECT);
	if(bmesh_test_flag(source_face, BMESH_HIDDEN)) bmesh_set_flag(target_face, BMESH_HIDDEN);
	
	/*mark the face as dirty for normal and tesselation calcs*/
	bmesh_set_flag(target_face, BMESH_DIRTY);
	
	/*copy per-loop custom data*/
	source_loop = source_face->lbase;
	target_loop = target_face->lbase;
	do{
		CustomData_bmesh_copy_data(&source_mesh->ldata, &target_mesh->ldata, source_loop->data, &target_loop->data);		
		source_loop = source_loop->next;
		target_loop = target_loop->next;
	}while(source_loop != source_face->lbase);
	
	return target_face;
}
	
/*
 * BMESH COPY MESH
 *
 * Internal Copy function. copies flagged elements from 
 * source to target, which may in fact be the same mesh.
 * Note that if __flag is 0, all elements will be copied.
 *
*/

static void bmesh_copy_mesh(BMMesh *source, BMMesh *target, int __flag)
{

	BMVert *v;
	BMEdge *e, **edar;
	BMLoop *l;
	BMFace *f;
	
	BMIter verts;
	BMIter edges;
	BMIter faces;
	BMIter loops;
	
	GHash *vhash;
	GHash *ehash;
	
	int maxlength = 0, flag;

	/*initialize pointer hashes*/
	vhash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp); 
	ehash = BLI_ghash_new(BLI_ghashutil_ptrrhash, BLI_ghashutil_ptrcmp);
	
	/*initialize edge pointer array*/
	for(f = BMeshIter_init(faces, BM_FACES,  source,  0); f; f = BMeshIter_step(faces)){
		if(f->len > maxlength) maxlength = f->len;
	}
	edar = MEM_callocN(sizeof(BMEdge*) * maxlength, "BM copy mesh edge pointer array");
	
	/*begin modelling loop for target*/
	bmesh_begin_edit(target);

	/*we make special exception for __flag == 0... we copy all*/
	if(!__flag){
		flag = BMESH_DUPE; 
		for(v = BMeshIter_init(verts, BM_VERTS, source, 0); v; v = BMeshIter_step(verts)) bmesh_set_flag(v, BMESH_DUPE);
		for(e = BMeshIter_init(verts, BM_EDGES, source, 0); e; e = BMeshIter_step(edges)) bmesh_set_flag(e, BMESH_DUPE);
		for(f = BMeshIter_init(faces, BM_FACES, source, 0); f; f = BMeshIter_step(faces)) bmesh_set_flag(f, BMESH_DUPE);
	} else{
		flag = __flag;
	}
	
	/*first we dupe all flagged faces and their elements from source*/
	for(f = BMeshIter_init(faces, BM_FACES, source, 0); f; f= BMeshIter_step(faces)){
		if(bmesh_test_flag(f, flag)){
			/*vertex pass*/
			for(l = BMeshIter_init(loops, BMESH_LOOP_OF_MESH, f, 0); l; l = BMeshIter_step(loops)){
				if(!bmesh_test_flag(l->v, BMESH_DUPED)){ 
					bmesh_copy_vertex(source,l->v, target, vhash);
					bmesh_set_flag(l->v, BMESH_DUPED);
				}
			}

			/*edge pass*/
			for(l = BMeshIter_init(loops, BMESH_LOOP_OF_MESH, f, 0); l; l = BMeshIter_step(loops)){
				if(!bmesh_test_flag(l->e, BMESH_DUPED)){ 
					bmesh_copy_edge(source, l->e, target,  vhash,  ehash);
					bmesh_set_flag(l->e, BMESH_DUPED);
				}
			}
			bmesh_copy_face(source, f, target, edar, vhash, ehash);
			bmesh_set_flag(f, BMESH_DUPED);		
		}
	}
	
	/*now we dupe all the edges*/
	for(e = BMeshIter_init(edges, BM_EDGES, source, 0); e; e = BMeshIter_step(edges)){
		if(bmesh_test_flag(e, flag) && (!bmesh_test_flag(e, BMESH_DUPED))){
			/*make sure that verts are copied*/
			if(!bmesh_test_flag(e->v1, BMESH_DUPED)){
				bmesh_copy_vertex(source, e->v1, target, vhash);
				bmesh_set_flag(e->v1, BMESH_DUPED);
			}
			if(!bmesh_test_flag(e->v2, BMESH_DUPED)){
				bmesh_copy_vertex(source, e->v2, target, vhash);
				bmesh_set_flag(e->v2, BMESH_DUPED);
			}
			/*now copy the actual edge*/
			bmesh_copy_edge(source, e, target,  vhash,  ehash);			
			bmesh_set_flag(e, BMESH_DUPED);
		}
	}
	
	/*finally dupe all loose vertices*/
	for(v = BMeshIter_init(verts, BM_VERTS, bm, 0); v; v = BMeshIter_step(verts)){
		if(bmesh_test_flag(v, flag) && (!bmesh_test_flag(v, BMESH_DUPED))){
			bmesh_copy_vertex(source, v, target, vhash);
			bmesh_set_flag(v, BMESH_DUPED);
		}
	}
	
	/*finish*/
	bmesh_end_edit(target, BMESH_CALC_NORM | BMESH_CALC_TESS);
	
	/*free pointer hashes*/
	BLI_ghash_free(vhash, NULL, NULL);
	BLI_ghash_free(ehash, NULL, NULL);	

	/*free edge pointer array*/
	MEM_freeN(edar);
}

/*
 * BMESH MAKE MESH FROM MESH
 *
 * Creates a new mesh by duplicating an existing one.
 *
*/

BMMesh *bmesh_make_mesh_from_mesh(BMMesh *bm, int allocsize[4])
{
	BMMesh *target = NULL;
	target = bmesh_make_mesh(allocsize);
	
	/*copy custom data layout*/
	CustomData_copy(&bm->vdata, &target->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
	CustomData_copy(&bm->edata, &target->edata, CD_MASK_BMESH, CD_CALLOC, 0);
	CustomData_copy(&bm->ldata, &target->ldata, CD_MASK_BMESH, CD_CALLOC, 0);
	CustomData_copy(&bm->pdata, &target->pdata, CD_MASK_BMESH, CD_CALLOC, 0);
	
	/*initialize memory pools*/
	CustomData_bmesh_init_pool(&target->vdata, allocsize[0]);
	CustomData_bmesh_init_pool(&target->edata, allocsize[1]);
	CustomData_bmesh_init_pool(&target->ldata, allocsize[2]);
	CustomData_bmesh_init_pool(&target->pdata, allocsize[3]);
	
	bmesh_begin_edit(bm);
	bmesh_begin_edit(target);
	
	bmesh_copy_mesh(bm, target, 0);	/*copy all elements*/

	bmesh_end_edit(bm);
	bmesh_end_edit(target);
	
	return target;

}

/*
 * BMESH SPLIT MESH
 *
 * Copies flagged elements then deletes them.
 *
*/

void bmesh_split_mesh(BMMesh *bm, int flag){
	
	BMVert *v;
	BMEdge *e;
	BMFace *f;
	
	BMIter verts;
	BMIter edges;
	BMIter faces;
	
	bmesh_begin_edit(bm);
	bmesh_copy_mesh(bm, bm, flag);

	/*mark verts for deletion*/
	for(v = BMeshIter_init(verts, BM_VERTS, bm, 0); v; v = BMeshIter_step(verts)){
		if(bmesh_test_flag(v, flag)) bmesh_delete_vert(bm, v);
	}
	/*mark edges for deletion*/
	for(e = BMeshIter_init(edges, BM_EDGES, bm, 0); e; e = BMeshIter_step(edges)){
		if(bmesh_test_flag(e, flag)) bmesh_delete_edge(bm, e);
		
	}
	/*mark faces for deletion*/
	for(f = BMeshIter_init(faces, BM_FACES, bm, 0); f; f= BMeshIter_step(faces)){
		if(bmesh_tes t_flag(f, flag)) bmesh_delete_face(bm, f);
		
	}
	bmesh_end_edit(bm);	
}