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

bmo_edgesplit.c « operators « bmesh « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8055da4172865a0fc32afa7ce401c53acf938c0c (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
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Contributor(s): Joseph Eagar
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include "MEM_guardedalloc.h"

#include "BLI_array.h"

#include "bmesh.h"

#include "bmesh_operators_private.h" /* own include */

typedef struct EdgeTag {
	BMVert *newv1, *newv2;
	BMEdge *newe1, *newe2;
	int tag;
} EdgeTag;

/* (EDGE_DEL == FACE_DEL) - this must be the case */
#define EDGE_DEL	1
#define EDGE_SEAM	2
#define EDGE_MARK	4
#define EDGE_RET1	8
#define EDGE_RET2	16

#define FACE_DEL	1
#define FACE_NEW	2

static BMFace *remake_face(BMesh *bm, EdgeTag *etags, BMFace *f, BMVert **verts, BMEdge **edges_tmp)
{
	BMIter liter1, liter2;
	EdgeTag *et;
	BMFace *f2;
	BMLoop *l, *l2;
	BMEdge *e;
	BMVert *lastv1, *lastv2 /* , *v1, *v2 */ /* UNUSED */;
	int i;

	/* we do final edge last */
	lastv1 = verts[f->len - 1];
	lastv2 = verts[0];
	/* v1 = verts[0]; */ /* UNUSED */
	/* v2 = verts[1]; */ /* UNUSED */
	for (i = 0; i < f->len - 1; i++) {
		e = BM_edge_create(bm, verts[i], verts[i + 1], NULL, TRUE);
		if (!e) {
			return NULL;
		}
		edges_tmp[i] = e;
	}

	edges_tmp[i] = BM_edge_create(bm, lastv1, lastv2, NULL, TRUE);

	f2 = BM_face_create(bm, verts, edges_tmp, f->len, FALSE);
	if (!f2) {
		return NULL;
	}

	BM_elem_attrs_copy(bm, bm, f, f2);

	l = BM_iter_new(&liter1, bm, BM_LOOPS_OF_FACE, f);
	l2 = BM_iter_new(&liter2, bm, BM_LOOPS_OF_FACE, f2);
	for ( ; l && l2; l = BM_iter_step(&liter1), l2 = BM_iter_step(&liter2)) {
		BM_elem_attrs_copy(bm, bm, l, l2);
		if (l->e != l2->e) {
			/* set up data for figuring out the two sides of
			 * the split */

			/* set edges index as dirty after running all */
			BM_elem_index_set(l2->e, BM_elem_index_get(l->e)); /* set_dirty! */
			et = &etags[BM_elem_index_get(l->e)];
			
			if (!et->newe1) {
				et->newe1 = l2->e;
			}
			else if (!et->newe2) {
				et->newe2 = l2->e;
			}
			else {
				/* Only two new edges should be created from each original edge
				 *  for edge split operation */

				//BLI_assert(et->newe1 == l2->e || et->newe2 == l2->e);
				et->newe2 = l2->e;
			}

			if (BMO_elem_flag_test(bm, l->e, EDGE_SEAM)) {
				BMO_elem_flag_enable(bm, l2->e, EDGE_SEAM);
			}

			BM_elem_attrs_copy(bm, bm, l->e, l2->e);
		}

		BMO_elem_flag_enable(bm, l->e, EDGE_MARK);
		BMO_elem_flag_enable(bm, l2->e, EDGE_MARK);
	}

	return f2;
}

static void tag_out_edges(BMesh *bm, EdgeTag *etags, BMOperator *UNUSED(op))
{
	EdgeTag *et;
	BMIter iter;
	BMLoop *l, *startl;
	BMEdge *e;
	BMVert *v;
	int i, ok;
	
	ok = 0;
	while (ok++ < 100000) {
		BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
			if (!BMO_elem_flag_test(bm, e, EDGE_SEAM))
				continue;

			et = &etags[BM_elem_index_get(e)];
			if (!et->tag && e->l) {
				break;
			}
		}
		
		if (!e) {
			break;
		}

		/* ok we found an edge, part of a region of splits we need
		 * to identify.  now walk along it */
		for (i = 0; i < 2; i++) {
			l = e->l;
			
			v = i ? l->next->v : l->v;

			while (1) {
				et = &etags[BM_elem_index_get(l->e)];
				if (et->newe1 == l->e) {
					if (et->newe1) {
						BMO_elem_flag_enable(bm, et->newe1, EDGE_RET1);
						BMO_elem_flag_disable(bm, et->newe1, EDGE_SEAM);
					}
					if (et->newe2) {
						BMO_elem_flag_enable(bm, et->newe2, EDGE_RET2);
						BMO_elem_flag_disable(bm, et->newe2, EDGE_SEAM);
					}
				}
				else {
					if (et->newe1) {
						BMO_elem_flag_enable(bm, et->newe1, EDGE_RET2);
						BMO_elem_flag_disable(bm, et->newe1, EDGE_SEAM);
					}
					if (et->newe2) {
						BMO_elem_flag_enable(bm, et->newe2, EDGE_RET1);
						BMO_elem_flag_disable(bm, et->newe2, EDGE_SEAM);
					}
				}

				/* If the original edge was non-manifold edges, then it is
				 * possible l->e is not et->newe1 or et->newe2. So always clear
				 * the flag on l->e as well, to prevent infinite looping. */
				BMO_elem_flag_disable(bm, l->e, EDGE_SEAM);

				startl = l;
				do {
					l = BM_face_other_loop(l->e, l->f, v);
					if (l == startl || BM_edge_face_count(l->e) != 2) {
						break;
					}
					l = l->radial_next;
				} while (l != startl && !BMO_elem_flag_test(bm, l->e, EDGE_SEAM));
				
				if (l == startl || !BMO_elem_flag_test(bm, l->e, EDGE_SEAM)) {
					break;
				}

				v = (l->v == v) ? l->next->v : l->v;
			}
		}
	}
}

void bmo_edgesplit_exec(BMesh *bm, BMOperator *op)
{
	EdgeTag *etags, *et;
	BMIter iter, liter;
	BMOIter siter;
	BMFace *f, *f2;
	BMLoop *l, *nextl, *prevl, *l2, *l3;
	BMEdge *e, *e2;
	BMVert *v, *v2, **verts = NULL;
	BLI_array_declare(verts);
	BMEdge **edges_tmp = NULL;
	BLI_array_declare(edges_tmp);
	int i, j;

	BMO_slot_buffer_flag_enable(bm, op, "edges", EDGE_SEAM, BM_EDGE);
	
	/* single marked edges unconnected to any other marked edges
	 * are illegal, go through and unmark them */
	BMO_ITER(e, &siter, bm, op, "edges", BM_EDGE) {
		for (i = 0; i < 2; i++) {
			BM_ITER(e2, &iter, bm, BM_EDGES_OF_VERT, i ? e->v2 : e->v1) {
				if (e != e2 && BMO_elem_flag_test(bm, e2, EDGE_SEAM)) {
					break;
				}
			}
			if (e2) {
				break;
			}
		}

		if (!e2) {
			BMO_elem_flag_disable(bm, e, EDGE_SEAM);
		}
	}

	etags = MEM_callocN(sizeof(EdgeTag) * bm->totedge, "EdgeTag");

	BM_mesh_elem_index_ensure(bm, BM_EDGE);

#ifdef ETV
#  undef ETV
#endif
#ifdef SETETV
#  undef SETETV
#endif

#define ETV(et, v, l) (l->e->v1 == v ? et->newv1 : et->newv2)
#define SETETV(et, v, l, vs) l->e->v1 == v ? (et->newv1 = vs) : (et->newv2 = vs)

	BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {

		if (BMO_elem_flag_test(bm, f, FACE_NEW)) {
			continue;
		}

		BLI_array_empty(verts);
		BLI_array_growitems(verts, f->len);
		memset(verts, 0, sizeof(BMVert *) * f->len);

		/* this is passed onto remake_face() so it doesnt need to allocate
		 * a new array on each call. */
		BLI_array_empty(edges_tmp);
		BLI_array_growitems(edges_tmp, f->len);

		i = 0;
		BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
			if (!BMO_elem_flag_test(bm, l->e, EDGE_SEAM)) {
				if (!verts[i]) {

					et = &etags[BM_elem_index_get(l->e)];
					if (ETV(et, l->v, l)) {
						verts[i] = ETV(et, l->v, l);
					}
					else
					{
						verts[i] = l->v;
					}
				}
				i++;
				continue;
			}

			nextl = l->next;
			prevl = l->prev;
			
			for (j = 0; j < 2; j++) {
				/* correct as long as i & j dont change during the loop */
				const int fv_index = j ? (i + 1) % f->len : i; /* face vert index */
				l2 = j ? nextl : prevl;
				v = j ? l2->v : l->v;

				if (BMO_elem_flag_test(bm, l2->e, EDGE_SEAM)) {
					if (verts[fv_index] == NULL) {
						/* make unique vert here for this face only */
						v2 = BM_vert_create(bm, v->co, v);
						verts[fv_index] = v2;
					}
					else {
						v2 = verts[fv_index];
					}
				}
				else {
					/* generate unique vert for non-seam edge(s)
					 * around the manifold vert fan if necessary */

					/* first check that we have two seam edges
					 * somewhere within this fa */
					l3 = l2;
					do {
						if (BM_edge_face_count(l3->e) != 2) {
							/* if we hit a boundary edge, tag
							 * l3 as null so we know to disconnect
							 * it */
							if (BM_edge_face_count(l3->e) == 1) {
								l3 = NULL;
							}
							break;
						}

						l3 = l3->radial_next;
						l3 = BM_face_other_loop(l3->e, l3->f, v);
					} while (l3 != l2 && !BMO_elem_flag_test(bm, l3->e, EDGE_SEAM));

					if (l3 == NULL || (BMO_elem_flag_test(bm, l3->e, EDGE_SEAM) && l3->e != l->e)) {
						et = &etags[BM_elem_index_get(l2->e)];
						if (ETV(et, v, l2) == NULL) {
							v2 = BM_vert_create(bm, v->co, v);
							
							l3 = l2;
							do {
								SETETV(et, v, l3, v2);
								if (BM_edge_face_count(l3->e) != 2) {
									break;
								}

								l3 = l3->radial_next;
								l3 = BM_face_other_loop(l3->e, l3->f, v);
								
								et = &etags[BM_elem_index_get(l3->e)];
							} while (l3 != l2 && !BMO_elem_flag_test(bm, l3->e, EDGE_SEAM));
						}
						else {
							v2 = ETV(et, v, l2);
						}

						verts[fv_index] = v2;
					}
					else {
						verts[fv_index] = v;
					}
				}
			}

			i++;
		}

		/* debugging code, quick way to find the face/vert combination
		 * which is failing assuming quads start planer - campbell */
#if 0
		if (f->len == 4) {
			float no1[3];
			float no2[3];
			float angle_error;
			printf(" ** found QUAD\n");
			normal_tri_v3(no1, verts[0]->co, verts[1]->co, verts[2]->co);
			normal_tri_v3(no2, verts[0]->co, verts[2]->co, verts[3]->co);
			if ((angle_error = angle_v3v3(no1, no2)) > 0.05) {
				printf("     ERROR %.4f\n", angle_error);
				print_v3("0", verts[0]->co);
				print_v3("1", verts[1]->co);
				print_v3("2", verts[2]->co);
				print_v3("3", verts[3]->co);

			}
		}
		else {
			printf(" ** fount %d len face\n", f->len);
		}
#endif

		f2 = remake_face(bm, etags, f, verts, edges_tmp);
		if (!f2) {
			continue;
		}

		BMO_elem_flag_enable(bm, f, FACE_DEL);
		BMO_elem_flag_enable(bm, f2, FACE_NEW);
	}
	
	/* remake_face() sets invalid indecies,
	 * likely these will be corrected on operator exit anyway */
	bm->elem_index_dirty &= ~BM_EDGE;

	/* cant call the operator because 'tag_out_edges'
	 * relies on original index values, from before editing geometry */

#if 0
	BMO_op_callf(bm, "del geom=%ff context=%i", FACE_DEL, DEL_ONLYFACES);
#else
	BMO_remove_tagged_context(bm, FACE_DEL, DEL_ONLYFACES);
#endif

	/* test EDGE_MARK'd edges if we need to delete them, EDGE_MARK
	 * is set in remake_face */
	BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
		if (BMO_elem_flag_test(bm, e, EDGE_MARK)) {
			if (!e->l) {
				BMO_elem_flag_enable(bm, e, EDGE_DEL);
			}
		}
	}

#if 0
	BMO_op_callf(bm, "del geom=%fe context=%i", EDGE_DEL, DEL_EDGES);
#else
	BMO_remove_tagged_context(bm, EDGE_DEL, DEL_EDGES);
#endif
	
	tag_out_edges(bm, etags, op);
	BMO_slot_from_flag(bm, op, "edgeout1", EDGE_RET1, BM_EDGE);
	BMO_slot_from_flag(bm, op, "edgeout2", EDGE_RET2, BM_EDGE);

	BLI_array_free(verts);
	BLI_array_free(edges_tmp);
	if (etags) MEM_freeN(etags);
}

#undef ETV
#undef SETETV