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

bmesh_decimate_dissolve.c « tools « bmesh « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 096349e8e9cf5d22e13b8815c6787794f92d79fe (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
/*
 * ***** 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): Campbell Barton
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/bmesh/tools/bmesh_decimate_dissolve.c
 *  \ingroup bmesh
 *
 * BMesh decimator that dissolves flat areas into polygons (ngons).
 */


#include "MEM_guardedalloc.h"

#include "BLI_math.h"
#include "BLI_heap.h"

#include "bmesh.h"
#include "bmesh_decimate.h"  /* own include */

#define COST_INVALID FLT_MAX


/* multiply vertex edge angle by face angle
 * this means we are not left with sharp corners between _almost_ planer faces
 * convert angles [0-PI/2] -> [0-1], multiply together, then convert back to radians. */
static float bm_vert_edge_face_angle(BMVert *v)
{
#define UNIT_TO_ANGLE DEG2RADF(90.0f)
#define ANGLE_TO_UNIT (1.0f / UNIT_TO_ANGLE)

	const float angle = BM_vert_calc_edge_angle(v);
	/* note: could be either edge, it doesn't matter */
	if (v->e && BM_edge_is_manifold(v->e)) {
		return ((angle * ANGLE_TO_UNIT) * (BM_edge_calc_face_angle(v->e) * ANGLE_TO_UNIT)) * UNIT_TO_ANGLE;
	}
	else {
		return angle;
	}

#undef UNIT_TO_ANGLE
#undef ANGLE_TO_UNIT
}

static float bm_edge_calc_dissolve_error(const BMEdge *e, const BMO_Delimit delimit)
{
	const bool is_contig = BM_edge_is_contiguous(e);
	float angle;

	if (!BM_edge_is_manifold(e)) {
		goto fail;
	}

	if ((delimit & BMO_DELIM_SEAM) &&
	    (BM_elem_flag_test(e, BM_ELEM_SEAM)))
	{
		goto fail;
	}

	if ((delimit & BMO_DELIM_MATERIAL) &&
	    (e->l->f->mat_nr != e->l->radial_next->f->mat_nr))
	{
		goto fail;
	}

	if ((delimit & BMO_DELIM_NORMAL) &&
	    (is_contig == false))
	{
		goto fail;
	}

	angle = BM_edge_calc_face_angle(e);
	if (is_contig == false) {
		angle = (float)M_PI - angle;
	}

	return angle;

fail:
	return COST_INVALID;
}


void BM_mesh_decimate_dissolve_ex(BMesh *bm, const float angle_limit, const bool do_dissolve_boundaries,
                                  const BMO_Delimit delimit,
                                  BMVert **vinput_arr, const int vinput_len,
                                  BMEdge **einput_arr, const int einput_len,
                                  const short oflag_out)
{
	const int eheap_table_len = do_dissolve_boundaries ? einput_len : max_ii(einput_len, vinput_len);
	void *_heap_table = MEM_mallocN(sizeof(HeapNode *) * eheap_table_len, __func__);

	int i;

	/* --- first edges --- */
	if (1) {
		BMEdge **earray;
		Heap *eheap;
		HeapNode **eheap_table = _heap_table;
		HeapNode *enode_top;
		int *vert_reverse_lookup;
		BMIter iter;
		BMEdge *e_iter;

		/* --- setup heap --- */
		eheap = BLI_heap_new_ex(einput_len);
		eheap_table = _heap_table;

		/* wire -> tag */
		BM_ITER_MESH (e_iter, &iter, bm, BM_EDGES_OF_MESH) {
			BM_elem_flag_set(e_iter, BM_ELEM_TAG, BM_edge_is_wire(e_iter));
			BM_elem_index_set(e_iter, -1);  /* set dirty */
		}
		bm->elem_index_dirty |= BM_EDGE;

		/* build heap */
		for (i = 0; i < einput_len; i++) {
			BMEdge *e = einput_arr[i];
			const float cost = bm_edge_calc_dissolve_error(e, delimit);
			eheap_table[i] = BLI_heap_insert(eheap, cost, e);
			BM_elem_index_set(e, i);  /* set dirty */
		}

		while ((BLI_heap_is_empty(eheap) == false) &&
		       (BLI_heap_node_value((enode_top = BLI_heap_top(eheap))) < angle_limit))
		{
			BMFace *f_new = NULL;
			BMEdge *e;

			e = BLI_heap_node_ptr(enode_top);
			i = BM_elem_index_get(e);

			if (BM_edge_is_manifold(e)) {
				f_new = BM_faces_join_pair(bm, e->l->f,
				                           e->l->radial_next->f, e,
				                           false); /* join faces */

				if (f_new) {
					BMLoop *l_first, *l_iter;

					BLI_heap_remove(eheap, enode_top);
					eheap_table[i] = NULL;

					/* update normal */
					BM_face_normal_update(f_new);
					if (oflag_out) {
						BMO_elem_flag_enable(bm, f_new, oflag_out);
					}

					/* re-calculate costs */
					l_iter = l_first = BM_FACE_FIRST_LOOP(f_new);
					do {
						const int j = BM_elem_index_get(l_iter->e);
						if (j != -1 && eheap_table[j]) {
							const float cost = bm_edge_calc_dissolve_error(l_iter->e, delimit);
							BLI_heap_remove(eheap, eheap_table[j]);
							eheap_table[j] = BLI_heap_insert(eheap, cost, l_iter->e);
						}
					} while ((l_iter = l_iter->next) != l_first);
				}
				else {
					BMO_error_clear(bm);
				}
			}

			if (UNLIKELY(f_new == NULL)) {
				BLI_heap_remove(eheap, enode_top);
				eheap_table[i] = BLI_heap_insert(eheap, COST_INVALID, e);
			}
		}

		/* prepare for cleanup */
		BM_mesh_elem_index_ensure(bm, BM_VERT);
		vert_reverse_lookup = MEM_mallocN(sizeof(int) * bm->totvert, __func__);
		fill_vn_i(vert_reverse_lookup, bm->totvert, -1);
		for (i = 0; i < vinput_len; i++) {
			BMVert *v = vinput_arr[i];
			vert_reverse_lookup[BM_elem_index_get(v)] = i;
		}

		/* --- cleanup --- */
		earray = MEM_mallocN(sizeof(BMEdge *) * bm->totedge, __func__);
		BM_ITER_MESH_INDEX (e_iter, &iter, bm, BM_EDGES_OF_MESH, i) {
			earray[i] = e_iter;
		}
		/* remove all edges/verts left behind from dissolving, NULL'ing the vertex array so we dont re-use */
		for (i = bm->totedge - 1; i != -1; i--) {
			e_iter = earray[i];

			if (BM_edge_is_wire(e_iter) && (BM_elem_flag_test(e_iter, BM_ELEM_TAG) == false)) {
				/* edge has become wire */
				int vidx_reverse;
				BMVert *v1 = e_iter->v1;
				BMVert *v2 = e_iter->v2;
				BM_edge_kill(bm, e_iter);
				if (v1->e == NULL) {
					vidx_reverse = vert_reverse_lookup[BM_elem_index_get(v1)];
					if (vidx_reverse != -1) vinput_arr[vidx_reverse] = NULL;
					BM_vert_kill(bm, v1);
				}
				if (v2->e == NULL) {
					vidx_reverse = vert_reverse_lookup[BM_elem_index_get(v2)];
					if (vidx_reverse != -1) vinput_arr[vidx_reverse] = NULL;
					BM_vert_kill(bm, v2);
				}
			}
		}
		MEM_freeN(vert_reverse_lookup);
		MEM_freeN(earray);

		BLI_heap_free(eheap, NULL);
	}


	/* --- second verts --- */
	if (do_dissolve_boundaries) {
		/* simple version of the branch below, since we will dissolve _all_ verts that use 2 edges */
		for (i = 0; i < vinput_len; i++) {
			BMVert *v = vinput_arr[i];
			if (LIKELY(v != NULL) &&
			    BM_vert_is_edge_pair(v))
			{
				BM_vert_collapse_edge(bm, v->e, v, true, true);  /* join edges */
			}
		}
	}
	else {
		Heap *vheap;
		HeapNode **vheap_table = _heap_table;
		HeapNode *vnode_top;

		BMVert *v_iter;
		BMIter iter;

		BM_ITER_MESH (v_iter, &iter, bm, BM_VERTS_OF_MESH) {
			BM_elem_index_set(v_iter, -1);  /* set dirty */
		}
		bm->elem_index_dirty |= BM_VERT;

		vheap = BLI_heap_new_ex(vinput_len);

		for (i = 0; i < vinput_len; i++) {
			BMVert *v = vinput_arr[i];
			if (LIKELY(v != NULL)) {
				const float cost = bm_vert_edge_face_angle(v);
				vheap_table[i] = BLI_heap_insert(vheap, cost, v);
				BM_elem_index_set(v, i);  /* set dirty */
			}
		}

		while ((BLI_heap_is_empty(vheap) == false) &&
		       (BLI_heap_node_value((vnode_top = BLI_heap_top(vheap))) < angle_limit))
		{
			BMEdge *e_new = NULL;
			BMVert *v;

			v = BLI_heap_node_ptr(vnode_top);
			i = BM_elem_index_get(v);

			if (BM_vert_is_edge_pair(v)) {
				e_new = BM_vert_collapse_edge(bm, v->e, v, true, true);  /* join edges */

				if (e_new) {

					BLI_heap_remove(vheap, vnode_top);
					vheap_table[i] = NULL;

					/* update normal */
					if (e_new->l) {
						BMLoop *l_first, *l_iter;
						l_iter = l_first = e_new->l;
						do {
							BM_face_normal_update(l_iter->f);
						} while ((l_iter = l_iter->radial_next) != l_first);

					}

					/* re-calculate costs */
					BM_ITER_ELEM (v_iter, &iter, e_new, BM_VERTS_OF_EDGE) {
						const int j = BM_elem_index_get(v_iter);
						if (j != -1 && vheap_table[j]) {
							const float cost = bm_vert_edge_face_angle(v_iter);
							BLI_heap_remove(vheap, vheap_table[j]);
							vheap_table[j] = BLI_heap_insert(vheap, cost, v_iter);
						}
					}
				}
			}

			if (UNLIKELY(e_new == NULL)) {
				BLI_heap_remove(vheap, vnode_top);
				vheap_table[i] = BLI_heap_insert(vheap, COST_INVALID, v);
			}
		}

		BLI_heap_free(vheap, NULL);
	}

	MEM_freeN(_heap_table);
}

void BM_mesh_decimate_dissolve(BMesh *bm, const float angle_limit, const bool do_dissolve_boundaries,
                               const BMO_Delimit delimit)
{
	int vinput_len;
	int einput_len;

	BMVert **vinput_arr = BM_iter_as_arrayN(bm, BM_VERTS_OF_MESH, NULL, &vinput_len, NULL, 0);
	BMEdge **einput_arr = BM_iter_as_arrayN(bm, BM_EDGES_OF_MESH, NULL, &einput_len, NULL, 0);


	BM_mesh_decimate_dissolve_ex(bm, angle_limit, do_dissolve_boundaries,
	                             delimit,
	                             vinput_arr, vinput_len,
	                             einput_arr, einput_len,
	                             0);

	MEM_freeN(vinput_arr);
	MEM_freeN(einput_arr);
}