3bcf9a3cd0bd604ad312dc48cb0f1b0c2e2ef4ce
[blender-staging.git] / source / blender / bmesh / tools / bmesh_decimate_collapse.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/bmesh/tools/bmesh_decimate_collapse.c
24  *  \ingroup bmesh
25  *
26  * BMesh decimator that uses an edge collapse method.
27  */
28
29 #include <stddef.h>
30
31 #include "MEM_guardedalloc.h"
32
33
34 #include "BLI_math.h"
35 #include "BLI_quadric.h"
36 #include "BLI_heap.h"
37
38 #include "BKE_customdata.h"
39
40 #include "bmesh.h"
41 #include "bmesh_decimate.h"  /* own include */
42
43 #include "../intern/bmesh_structure.h"
44
45 /* defines for testing */
46 #define USE_CUSTOMDATA
47 #define USE_TRIANGULATE
48 #define USE_VERT_NORMAL_INTERP  /* has the advantage that flipped faces don't mess up vertex normals */
49
50 /* if the cost from #BLI_quadric_evaluate is 'noise', fallback to topology */
51 #define USE_TOPOLOGY_FALLBACK
52 #ifdef  USE_TOPOLOGY_FALLBACK
53 #  define   TOPOLOGY_FALLBACK_EPS  FLT_EPSILON
54 #endif
55
56 /* these checks are for rare cases that we can't avoid since they are valid meshes still */
57 #define USE_SAFETY_CHECKS
58
59 #define BOUNDARY_PRESERVE_WEIGHT 100.0f
60 #define OPTIMIZE_EPS 0.01f  /* FLT_EPSILON is too small, see [#33106] */
61 #define COST_INVALID FLT_MAX
62
63 typedef enum CD_UseFlag {
64         CD_DO_VERT = (1 << 0),
65         CD_DO_EDGE = (1 << 1),
66         CD_DO_LOOP = (1 << 2)
67 } CD_UseFlag;
68
69
70 /* BMesh Helper Functions
71  * ********************** */
72
73 /**
74  * \param vquadrics must be calloc'd
75  */
76 static void bm_decim_build_quadrics(BMesh *bm, Quadric *vquadrics)
77 {
78         BMIter iter;
79         BMFace *f;
80         BMEdge *e;
81
82         BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
83                 BMLoop *l_first;
84                 BMLoop *l_iter;
85
86                 float center[3];
87                 double plane_db[4];
88                 Quadric q;
89
90                 BM_face_calc_center_mean(f, center);
91                 copy_v3db_v3fl(plane_db, f->no);
92                 plane_db[3] = -dot_v3db_v3fl(plane_db, center);
93
94                 BLI_quadric_from_plane(&q, plane_db);
95
96                 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
97                 do {
98                         BLI_quadric_add_qu_qu(&vquadrics[BM_elem_index_get(l_iter->v)], &q);
99                 } while ((l_iter = l_iter->next) != l_first);
100         }
101
102         /* boundary edges */
103         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
104                 if (UNLIKELY(BM_edge_is_boundary(e))) {
105                         float edge_vector[3];
106                         float edge_plane[3];
107                         double edge_plane_db[4];
108                         sub_v3_v3v3(edge_vector, e->v2->co, e->v1->co);
109                         f = e->l->f;
110
111                         cross_v3_v3v3(edge_plane, edge_vector, f->no);
112                         copy_v3db_v3fl(edge_plane_db, edge_plane);
113
114                         if (normalize_v3_d(edge_plane_db) > FLT_EPSILON) {
115                                 Quadric q;
116                                 float center[3];
117
118                                 mid_v3_v3v3(center, e->v1->co, e->v2->co);
119
120                                 edge_plane_db[3] = -dot_v3db_v3fl(edge_plane_db, center);
121                                 BLI_quadric_from_plane(&q, edge_plane_db);
122                                 BLI_quadric_mul(&q, BOUNDARY_PRESERVE_WEIGHT);
123
124                                 BLI_quadric_add_qu_qu(&vquadrics[BM_elem_index_get(e->v1)], &q);
125                                 BLI_quadric_add_qu_qu(&vquadrics[BM_elem_index_get(e->v2)], &q);
126                         }
127                 }
128         }
129 }
130
131
132 static void bm_decim_calc_target_co(
133         BMEdge *e, float optimize_co[3],
134         const Quadric *vquadrics)
135 {
136         /* compute an edge contraction target for edge 'e'
137          * this is computed by summing it's vertices quadrics and
138          * optimizing the result. */
139         Quadric q;
140
141         BLI_quadric_add_qu_ququ(
142                 &q,
143                 &vquadrics[BM_elem_index_get(e->v1)],
144                 &vquadrics[BM_elem_index_get(e->v2)]);
145
146         if (BLI_quadric_optimize(&q, optimize_co, OPTIMIZE_EPS)) {
147                 return;  /* all is good */
148         }
149         else {
150                 mid_v3_v3v3(optimize_co, e->v1->co, e->v2->co);
151         }
152 }
153
154 static bool bm_edge_collapse_is_degenerate_flip(BMEdge *e, const float optimize_co[3])
155 {
156         BMIter liter;
157         BMLoop *l;
158         unsigned int i;
159
160         for (i = 0; i < 2; i++) {
161                 /* loop over both verts */
162                 BMVert *v = *((&e->v1) + i);
163
164                 BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
165                         if (l->e != e && l->prev->e != e) {
166                                 const float *co_prev = l->prev->v->co;
167                                 const float *co_next = l->next->v->co;
168                                 float cross_exist[3];
169                                 float cross_optim[3];
170
171 #if 1
172                                 float vec_other[3];  /* line between the two outer verts, re-use for both cross products */
173                                 float vec_exist[3];  /* before collapse */
174                                 float vec_optim[3];  /* after collapse */
175
176                                 sub_v3_v3v3(vec_other, co_prev, co_next);
177                                 sub_v3_v3v3(vec_exist, co_prev, v->co);
178                                 sub_v3_v3v3(vec_optim, co_prev, optimize_co);
179
180                                 cross_v3_v3v3(cross_exist, vec_other, vec_exist);
181                                 cross_v3_v3v3(cross_optim, vec_other, vec_optim);
182
183                                 /* avoid normalize */
184                                 if (dot_v3v3(cross_exist, cross_optim) <=
185                                     (len_squared_v3(cross_exist) + len_squared_v3(cross_optim)) * 0.01f)
186                                 {
187                                         return true;
188                                 }
189 #else
190                                 normal_tri_v3(cross_exist, v->co,       co_prev, co_next);
191                                 normal_tri_v3(cross_optim, optimize_co, co_prev, co_next);
192
193                                 /* use a small value rather then zero so we don't flip a face in multiple steps
194                                  * (first making it zero area, then flipping again) */
195                                 if (dot_v3v3(cross_exist, cross_optim) <= FLT_EPSILON) {
196                                         //printf("no flip\n");
197                                         return true;
198                                 }
199 #endif
200
201                         }
202                 }
203         }
204
205         return false;
206 }
207
208 #ifdef USE_TOPOLOGY_FALLBACK
209 /**
210  * when the cost is so small that its not useful (flat surfaces),
211  * fallback to using a 'topology' cost.
212  *
213  * This avoids cases where a flat (or near flat) areas get very un-even geometry.
214  */
215 static float bm_decim_build_edge_cost_single__topology(BMEdge *e)
216 {
217         return fabsf(dot_v3v3(e->v1->no, e->v2->no)) / min_ff(-len_squared_v3v3(e->v1->co, e->v2->co), -FLT_EPSILON);
218 }
219 #endif  /* USE_TOPOLOGY_FALLBACK */
220
221 static void bm_decim_build_edge_cost_single(
222         BMEdge *e,
223         const Quadric *vquadrics, const float *vweights,
224         Heap *eheap, HeapNode **eheap_table)
225 {
226         const Quadric *q1, *q2;
227         float optimize_co[3];
228         float cost;
229
230         if (eheap_table[BM_elem_index_get(e)]) {
231                 BLI_heap_remove(eheap, eheap_table[BM_elem_index_get(e)]);
232         }
233
234         /* check we can collapse, some edges we better not touch */
235         if (BM_edge_is_boundary(e)) {
236                 if (e->l->f->len == 3) {
237                         /* pass */
238                 }
239                 else {
240                         /* only collapse tri's */
241                         goto clear;
242                 }
243         }
244         else if (BM_edge_is_manifold(e)) {
245                 if ((e->l->f->len == 3) && (e->l->radial_next->f->len == 3)) {
246                         /* pass */
247                 }
248                 else {
249                         /* only collapse tri's */
250                         goto clear;
251                 }
252         }
253         else {
254                 goto clear;
255         }
256
257         if (vweights) {
258                 if ((vweights[BM_elem_index_get(e->v1)] >= BM_MESH_DECIM_WEIGHT_MAX) &&
259                     (vweights[BM_elem_index_get(e->v2)] >= BM_MESH_DECIM_WEIGHT_MAX))
260                 {
261                         /* skip collapsing this edge */
262                         goto clear;
263                 }
264         }
265         /* end sanity check */
266
267
268         bm_decim_calc_target_co(e, optimize_co, vquadrics);
269
270         q1 = &vquadrics[BM_elem_index_get(e->v1)];
271         q2 = &vquadrics[BM_elem_index_get(e->v2)];
272
273         if (vweights == NULL) {
274                 cost = (BLI_quadric_evaluate(q1, optimize_co) +
275                         BLI_quadric_evaluate(q2, optimize_co));
276         }
277         else {
278                 /* add 1.0 so planar edges are still weighted against */
279                 cost = (((BLI_quadric_evaluate(q1, optimize_co) + 1.0f) * vweights[BM_elem_index_get(e->v1)]) +
280                         ((BLI_quadric_evaluate(q2, optimize_co) + 1.0f) * vweights[BM_elem_index_get(e->v2)]));
281         }
282         // print("COST %.12f\n");
283
284         /* note, 'cost' shouldn't be negative but happens sometimes with small values.
285          * this can cause faces that make up a flat surface to over-collapse, see [#37121] */
286         cost = fabsf(cost);
287
288 #ifdef USE_TOPOLOGY_FALLBACK
289         if (UNLIKELY(cost < TOPOLOGY_FALLBACK_EPS)) {
290                 /* subtract existing cost to further differentiate edges from one another
291                  *
292                  * keep topology cost below 0.0 so their values don't interfere with quadric cost,
293                  * (and they get handled first).
294                  * */
295                 cost = bm_decim_build_edge_cost_single__topology(e) - cost;
296                 BLI_assert(cost <= 0.0f);
297         }
298 #endif
299
300         eheap_table[BM_elem_index_get(e)] = BLI_heap_insert(eheap, cost, e);
301         return;
302
303 clear:
304         eheap_table[BM_elem_index_get(e)] = NULL;
305 }
306
307
308 /* use this for degenerate cases - add back to the heap with an invalid cost,
309  * this way it may be calculated again if surrounding geometry changes */
310 static void bm_decim_invalid_edge_cost_single(
311         BMEdge *e,
312         Heap *eheap, HeapNode **eheap_table)
313 {
314         BLI_assert(eheap_table[BM_elem_index_get(e)] == NULL);
315         eheap_table[BM_elem_index_get(e)] = BLI_heap_insert(eheap, COST_INVALID, e);
316 }
317
318 static void bm_decim_build_edge_cost(
319         BMesh *bm,
320         const Quadric *vquadrics, const float *vweights,
321         Heap *eheap, HeapNode **eheap_table)
322 {
323         BMIter iter;
324         BMEdge *e;
325         unsigned int i;
326
327         BM_ITER_MESH_INDEX (e, &iter, bm, BM_EDGES_OF_MESH, i) {
328                 eheap_table[i] = NULL;  /* keep sanity check happy */
329                 bm_decim_build_edge_cost_single(e, vquadrics, vweights, eheap, eheap_table);
330         }
331 }
332
333 #ifdef USE_TRIANGULATE
334 /* Temp Triangulation
335  * ****************** */
336
337 /**
338  * To keep things simple we can only collapse edges on triangulated data
339  * (limitation with edge collapse and error calculation functions).
340  *
341  * But to avoid annoying users by only giving triangle results, we can
342  * triangulate, keeping a reference between the faces, then join after
343  * if the edges don't collapse, this will also allow more choices when
344  * collapsing edges so even has some advantage over decimating quads
345  * directly.
346  *
347  * \return true if any faces were triangulated.
348  */
349
350 static bool bm_decim_triangulate_begin(BMesh *bm)
351 {
352         BMIter iter;
353         BMFace *f;
354         // bool has_quad;  // could optimize this a little
355         bool has_cut = false;
356
357         BLI_assert((bm->elem_index_dirty & BM_VERT) == 0);
358
359         /* first clear loop index values */
360         BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
361                 BMLoop *l_iter;
362                 BMLoop *l_first;
363
364                 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
365                 do {
366                         BM_elem_index_set(l_iter, -1);  /* set_dirty */
367                 } while ((l_iter = l_iter->next) != l_first);
368
369                 // has_quad |= (f->len == 4)
370         }
371
372         bm->elem_index_dirty |= BM_LOOP;
373
374         /* adding new faces as we loop over faces
375          * is normally best avoided, however in this case its not so bad because any face touched twice
376          * will already be triangulated*/
377         BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
378                 if (f->len == 4) {
379                         BMLoop *f_l[4];
380                         BMLoop *l_a, *l_b;
381
382                         {
383                                 BMLoop *l_iter = BM_FACE_FIRST_LOOP(f);
384
385                                 f_l[0] = l_iter; l_iter = l_iter->next;
386                                 f_l[1] = l_iter; l_iter = l_iter->next;
387                                 f_l[2] = l_iter; l_iter = l_iter->next;
388                                 f_l[3] = l_iter;
389                         }
390
391                         if (len_squared_v3v3(f_l[0]->v->co, f_l[2]->v->co) <
392                             len_squared_v3v3(f_l[1]->v->co, f_l[3]->v->co))
393                         {
394                                 l_a = f_l[0];
395                                 l_b = f_l[2];
396                         }
397                         else {
398                                 l_a = f_l[1];
399                                 l_b = f_l[3];
400                         }
401
402 #ifdef USE_SAFETY_CHECKS
403                         if (BM_edge_exists(l_a->v, l_b->v) == NULL)
404 #endif
405                         {
406                                 BMFace *f_new;
407                                 BMLoop *l_new;
408
409                                 /* warning, NO_DOUBLE option here isn't handled as nice as it could be
410                                  * - if there is a quad that has a free standing edge joining it along
411                                  * where we want to split the face, there isnt a good way we can handle this.
412                                  * currently that edge will get removed when joining the tris back into a quad. */
413                                 f_new = BM_face_split(bm, f, l_a, l_b, &l_new, NULL, false);
414
415                                 if (f_new) {
416                                         /* the value of this doesn't matter, only that the 2 loops match and have unique values */
417                                         const int f_index = BM_elem_index_get(f);
418
419                                         /* since we just split theres only ever 2 loops */
420                                         BLI_assert(BM_edge_is_manifold(l_new->e));
421
422                                         BM_elem_index_set(l_new, f_index);  /* set_dirty */
423                                         BM_elem_index_set(l_new->radial_next, f_index);  /* set_dirty */
424
425                                         BM_face_normal_update(f);
426                                         BM_face_normal_update(f_new);
427
428                                         has_cut = true;
429                                 }
430                         }
431                 }
432         }
433
434         BLI_assert((bm->elem_index_dirty & BM_VERT) == 0);
435
436         if (has_cut) {
437                 /* now triangulation is done we need to correct index values */
438                 BM_mesh_elem_index_ensure(bm, BM_EDGE | BM_FACE);
439         }
440
441         return has_cut;
442 }
443
444 static void bm_decim_triangulate_end(BMesh *bm)
445 {
446         /* decimation finished, now re-join */
447         BMIter iter;
448         BMEdge *e, *e_next;
449
450         /* boundary edges */
451         BM_ITER_MESH_MUTABLE (e, e_next, &iter, bm, BM_EDGES_OF_MESH) {
452                 BMLoop *l_a, *l_b;
453                 if (BM_edge_loop_pair(e, &l_a, &l_b)) {
454                         const int l_a_index = BM_elem_index_get(l_a);
455                         if (l_a_index != -1) {
456                                 const int l_b_index = BM_elem_index_get(l_b);
457                                 if (l_a_index == l_b_index) {
458                                         if (LIKELY(l_a->f->len == 3 && l_b->f->len == 3)) {
459                                                 if (l_a->v != l_b->v) {  /* if this is the case, faces have become flipped */
460                                                         /* check we are not making a degenerate quad */
461                                                         BMVert *vquad[4] = {
462                                                                 e->v1,
463                                                                 BM_vert_in_edge(e, l_a->next->v) ? l_a->prev->v : l_a->next->v,
464                                                                 e->v2,
465                                                                 BM_vert_in_edge(e, l_b->next->v) ? l_b->prev->v : l_b->next->v,
466                                                         };
467
468                                                         BLI_assert(ELEM(vquad[0], vquad[1], vquad[2], vquad[3]) == false);
469                                                         BLI_assert(ELEM(vquad[1], vquad[0], vquad[2], vquad[3]) == false);
470                                                         BLI_assert(ELEM(vquad[2], vquad[1], vquad[0], vquad[3]) == false);
471                                                         BLI_assert(ELEM(vquad[3], vquad[1], vquad[2], vquad[0]) == false);
472
473                                                         if (is_quad_convex_v3(vquad[0]->co, vquad[1]->co, vquad[2]->co, vquad[3]->co)) {
474                                                                 /* highly unlikely to fail, but prevents possible double-ups */
475                                                                 BMFace *f[2] = {l_a->f, l_b->f};
476                                                                 BM_faces_join(bm, f, 2, true);
477                                                         }
478                                                 }
479                                         }
480                                 }
481                         }
482                 }
483         }
484 }
485
486 #endif  /* USE_TRIANGULATE */
487
488 /* Edge Collapse Functions
489  * *********************** */
490
491 #ifdef USE_CUSTOMDATA
492
493 /**
494  * \param l: defines the vert to collapse into.
495  */
496 static void bm_edge_collapse_loop_customdata(
497         BMesh *bm, BMLoop *l, BMVert *v_clear, BMVert *v_other,
498         const float customdata_fac)
499 {
500         /* disable seam check - the seam check would have to be done per layer, its not really that important */
501 //#define USE_SEAM
502         /* these don't need to be updated, since they will get removed when the edge collapses */
503         BMLoop *l_clear, *l_other;
504         const bool is_manifold = BM_edge_is_manifold(l->e);
505         int side;
506
507         /* first find the loop of 'v_other' thats attached to the face of 'l' */
508         if (l->v == v_clear) {
509                 l_clear = l;
510                 l_other = l->next;
511         }
512         else {
513                 l_clear = l->next;
514                 l_other = l;
515         }
516
517         BLI_assert(l_clear->v == v_clear);
518         BLI_assert(l_other->v == v_other);
519         (void)v_other;  /* quiet warnings for release */
520
521         /* now we have both corners of the face 'l->f' */
522         for (side = 0; side < 2; side++) {
523 #ifdef USE_SEAM
524                 bool is_seam = false;
525 #endif
526                 void *src[2];
527                 BMFace *f_exit = is_manifold ? l->radial_next->f : NULL;
528                 BMEdge *e_prev = l->e;
529                 BMLoop *l_first;
530                 BMLoop *l_iter;
531                 float w[2];
532
533                 if (side == 0) {
534                         l_iter = l_first = l_clear;
535                         src[0] = l_clear->head.data;
536                         src[1] = l_other->head.data;
537
538                         w[0] = customdata_fac;
539                         w[1] = 1.0f - customdata_fac;
540                 }
541                 else {
542                         l_iter = l_first = l_other;
543                         src[0] = l_other->head.data;
544                         src[1] = l_clear->head.data;
545
546                         w[0] = 1.0f - customdata_fac;
547                         w[1] = customdata_fac;
548                 }
549
550                 // print_v2("weights", w);
551
552                 /* WATCH IT! - should NOT reference (_clear or _other) vars for this while loop */
553
554                 /* walk around the fan using 'e_prev' */
555                 while (((l_iter = BM_vert_step_fan_loop(l_iter, &e_prev)) != l_first) && (l_iter != NULL)) {
556                         int i;
557                         /* quit once we hit the opposite face, if we have one */
558                         if (f_exit && UNLIKELY(f_exit == l_iter->f)) {
559                                 break;
560                         }
561
562 #ifdef USE_SEAM
563                         /* break out unless we find a match */
564                         is_seam = true;
565 #endif
566
567                         /* ok. we have a loop. now be smart with it! */
568                         for (i = 0; i < bm->ldata.totlayer; i++) {
569                                 if (CustomData_layer_has_math(&bm->ldata, i)) {
570                                         const int offset = bm->ldata.layers[i].offset;
571                                         const int type = bm->ldata.layers[i].type;
572                                         const void *cd_src[2] = {
573                                             POINTER_OFFSET(src[0], offset),
574                                             POINTER_OFFSET(src[1], offset),
575                                         };
576                                         void *cd_iter = POINTER_OFFSET(l_iter->head.data, offset);
577
578                                         /* detect seams */
579                                         if (CustomData_data_equals(type, cd_src[0], cd_iter)) {
580                                                 CustomData_bmesh_interp_n(
581                                                         &bm->ldata, cd_src, w, NULL, ARRAY_SIZE(cd_src),
582                                                         POINTER_OFFSET(l_iter->head.data, offset), i);
583 #ifdef USE_SEAM
584                                                 is_seam = false;
585 #endif
586                                         }
587                                 }
588                         }
589
590 #ifdef USE_SEAM
591                         if (is_seam) {
592                                 break;
593                         }
594 #endif
595                 }
596         }
597
598 //#undef USE_SEAM
599
600 }
601 #endif  /* USE_CUSTOMDATA */
602
603 /**
604  * Check if the collapse will result in a degenerate mesh,
605  * that is - duplicate edges or faces.
606  *
607  * This situation could be checked for when calculating collapse cost
608  * however its quite slow and a degenerate collapse could eventuate
609  * after the cost is calculated, so instead, check just before collapsing.
610  */
611
612 static void bm_edge_tag_enable(BMEdge *e)
613 {
614         BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
615         BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
616         if (e->l) {
617                 BM_elem_flag_enable(e->l->f, BM_ELEM_TAG);
618                 if (e->l != e->l->radial_next) {
619                         BM_elem_flag_enable(e->l->radial_next->f, BM_ELEM_TAG);
620                 }
621         }
622 }
623
624 static void bm_edge_tag_disable(BMEdge *e)
625 {
626         BM_elem_flag_disable(e->v1, BM_ELEM_TAG);
627         BM_elem_flag_disable(e->v2, BM_ELEM_TAG);
628         if (e->l) {
629                 BM_elem_flag_disable(e->l->f, BM_ELEM_TAG);
630                 if (e->l != e->l->radial_next) {
631                         BM_elem_flag_disable(e->l->radial_next->f, BM_ELEM_TAG);
632                 }
633         }
634 }
635
636 static bool bm_edge_tag_test(BMEdge *e)
637 {
638         /* is the edge or one of its faces tagged? */
639         return (BM_elem_flag_test(e->v1, BM_ELEM_TAG) ||
640                 BM_elem_flag_test(e->v2, BM_ELEM_TAG) ||
641                 (e->l && (BM_elem_flag_test(e->l->f, BM_ELEM_TAG) ||
642                           (e->l != e->l->radial_next &&
643                           BM_elem_flag_test(e->l->radial_next->f, BM_ELEM_TAG))))
644                 );
645 }
646
647 /* takes the edges loop */
648 BLI_INLINE int bm_edge_is_manifold_or_boundary(BMLoop *l)
649 {
650 #if 0
651         /* less optimized version of check below */
652         return (BM_edge_is_manifold(l->e) || BM_edge_is_boundary(l->e);
653 #else
654         /* if the edge is a boundary it points to its self, else this must be a manifold */
655         return LIKELY(l) && LIKELY(l->radial_next->radial_next == l);
656 #endif
657 }
658
659 static bool bm_edge_collapse_is_degenerate_topology(BMEdge *e_first)
660 {
661         /* simply check that there is no overlap between faces and edges of each vert,
662          * (excluding the 2 faces attached to 'e' and 'e' its self) */
663
664         BMEdge *e_iter;
665
666         /* clear flags on both disks */
667         e_iter = e_first;
668         do {
669                 if (!bm_edge_is_manifold_or_boundary(e_iter->l)) {
670                         return true;
671                 }
672                 bm_edge_tag_disable(e_iter);
673         } while ((e_iter = bmesh_disk_edge_next(e_iter, e_first->v1)) != e_first);
674
675         e_iter = e_first;
676         do {
677                 if (!bm_edge_is_manifold_or_boundary(e_iter->l)) {
678                         return true;
679                 }
680                 bm_edge_tag_disable(e_iter);
681         } while ((e_iter = bmesh_disk_edge_next(e_iter, e_first->v2)) != e_first);
682
683         /* now enable one side... */
684         e_iter = e_first;
685         do {
686                 bm_edge_tag_enable(e_iter);
687         } while ((e_iter = bmesh_disk_edge_next(e_iter, e_first->v1)) != e_first);
688
689         /* ... except for the edge we will collapse, we know thats shared,
690          * disable this to avoid false positive. We could be smart and never enable these
691          * face/edge tags in the first place but easier to do this */
692         // bm_edge_tag_disable(e_first);
693         /* do inline... */
694         {
695 #if 0
696                 BMIter iter;
697                 BMIter liter;
698                 BMLoop *l;
699                 BMVert *v;
700                 BM_ITER_ELEM (l, &liter, e_first, BM_LOOPS_OF_EDGE) {
701                         BM_elem_flag_disable(l->f, BM_ELEM_TAG);
702                         BM_ITER_ELEM (v, &iter, l->f, BM_VERTS_OF_FACE) {
703                                 BM_elem_flag_disable(v, BM_ELEM_TAG);
704                         }
705                 }
706 #else
707                 /* we know each face is a triangle, no looping/iterators needed here */
708
709                 BMLoop *l_radial;
710                 BMLoop *l_face;
711
712                 l_radial = e_first->l;
713                 l_face = l_radial;
714                 BLI_assert(l_face->f->len == 3);
715                 BM_elem_flag_disable(l_face->f, BM_ELEM_TAG);
716                 BM_elem_flag_disable((l_face = l_radial)->v,     BM_ELEM_TAG);
717                 BM_elem_flag_disable((l_face = l_face->next)->v, BM_ELEM_TAG);
718                 BM_elem_flag_disable((         l_face->next)->v, BM_ELEM_TAG);
719                 l_face = l_radial->radial_next;
720                 if (l_radial != l_face) {
721                         BLI_assert(l_face->f->len == 3);
722                         BM_elem_flag_disable(l_face->f, BM_ELEM_TAG);
723                         BM_elem_flag_disable((l_face = l_radial->radial_next)->v, BM_ELEM_TAG);
724                         BM_elem_flag_disable((l_face = l_face->next)->v,          BM_ELEM_TAG);
725                         BM_elem_flag_disable((         l_face->next)->v,          BM_ELEM_TAG);
726                 }
727 #endif
728         }
729
730         /* and check for overlap */
731         e_iter = e_first;
732         do {
733                 if (bm_edge_tag_test(e_iter)) {
734                         return true;
735                 }
736         } while ((e_iter = bmesh_disk_edge_next(e_iter, e_first->v2)) != e_first);
737
738         return false;
739 }
740
741 /**
742  * special, highly limited edge collapse function
743  * intended for speed over flexibility.
744  * can only collapse edges connected to (1, 2) tris.
745  *
746  * Important - dont add vert/edge/face data on collapsing!
747  *
748  * \param r_e_clear_other: Let caller know what edges we remove besides \a e_clear
749  * \param customdata_flag: Merge factor, scales from 0 - 1 ('v_clear' -> 'v_other')
750  */
751 static bool bm_edge_collapse(
752         BMesh *bm, BMEdge *e_clear, BMVert *v_clear, int r_e_clear_other[2],
753 #ifdef USE_CUSTOMDATA
754         const CD_UseFlag customdata_flag,
755         const float customdata_fac
756 #else
757         const CD_UseFlag UNUSED(customdata_flag),
758         const float UNUSED(customdata_fac)
759 #endif
760         )
761 {
762         BMVert *v_other;
763
764         v_other = BM_edge_other_vert(e_clear, v_clear);
765         BLI_assert(v_other != NULL);
766
767         if (BM_edge_is_manifold(e_clear)) {
768                 BMLoop *l_a, *l_b;
769                 BMEdge *e_a_other[2], *e_b_other[2];
770                 bool ok;
771
772                 ok = BM_edge_loop_pair(e_clear, &l_a, &l_b);
773
774                 BLI_assert(ok == true);
775                 BLI_assert(l_a->f->len == 3);
776                 BLI_assert(l_b->f->len == 3);
777                 UNUSED_VARS_NDEBUG(ok);
778
779                 /* keep 'v_clear' 0th */
780                 if (BM_vert_in_edge(l_a->prev->e, v_clear)) {
781                         e_a_other[0] = l_a->prev->e;
782                         e_a_other[1] = l_a->next->e;
783                 }
784                 else {
785                         e_a_other[1] = l_a->prev->e;
786                         e_a_other[0] = l_a->next->e;
787                 }
788
789                 if (BM_vert_in_edge(l_b->prev->e, v_clear)) {
790                         e_b_other[0] = l_b->prev->e;
791                         e_b_other[1] = l_b->next->e;
792                 }
793                 else {
794                         e_b_other[1] = l_b->prev->e;
795                         e_b_other[0] = l_b->next->e;
796                 }
797
798                 /* we could assert this case, but better just bail out */
799 #if 0
800                 BLI_assert(e_a_other[0] != e_b_other[0]);
801                 BLI_assert(e_a_other[0] != e_b_other[1]);
802                 BLI_assert(e_b_other[0] != e_a_other[0]);
803                 BLI_assert(e_b_other[0] != e_a_other[1]);
804 #endif
805                 /* not totally common but we want to avoid */
806                 if (ELEM(e_a_other[0], e_b_other[0], e_b_other[1]) ||
807                     ELEM(e_a_other[1], e_b_other[0], e_b_other[1]))
808                 {
809                         return false;
810                 }
811
812                 BLI_assert(BM_edge_share_vert(e_a_other[0], e_b_other[0]));
813                 BLI_assert(BM_edge_share_vert(e_a_other[1], e_b_other[1]));
814
815                 r_e_clear_other[0] = BM_elem_index_get(e_a_other[0]);
816                 r_e_clear_other[1] = BM_elem_index_get(e_b_other[0]);
817
818 #ifdef USE_CUSTOMDATA
819                 /* before killing, do customdata */
820                 if (customdata_flag & CD_DO_VERT) {
821                         BM_data_interp_from_verts(bm, v_other, v_clear, v_other, customdata_fac);
822                 }
823                 if (customdata_flag & CD_DO_EDGE) {
824                         BM_data_interp_from_edges(bm, e_a_other[1], e_a_other[0], e_a_other[1], customdata_fac);
825                         BM_data_interp_from_edges(bm, e_b_other[1], e_b_other[0], e_b_other[1], customdata_fac);
826                 }
827                 if (customdata_flag & CD_DO_LOOP) {
828                         bm_edge_collapse_loop_customdata(bm, e_clear->l,              v_clear, v_other, customdata_fac);
829                         bm_edge_collapse_loop_customdata(bm, e_clear->l->radial_next, v_clear, v_other, customdata_fac);
830                 }
831 #endif
832
833                 BM_edge_kill(bm, e_clear);
834
835                 v_other->head.hflag |= v_clear->head.hflag;
836                 BM_vert_splice(bm, v_other, v_clear);
837
838                 e_a_other[1]->head.hflag |= e_a_other[0]->head.hflag;
839                 e_b_other[1]->head.hflag |= e_b_other[0]->head.hflag;
840                 BM_edge_splice(bm, e_a_other[1], e_a_other[0]);
841                 BM_edge_splice(bm, e_b_other[1], e_b_other[0]);
842
843                 // BM_mesh_validate(bm);
844
845                 return true;
846         }
847         else if (BM_edge_is_boundary(e_clear)) {
848                 /* same as above but only one triangle */
849                 BMLoop *l_a;
850                 BMEdge *e_a_other[2];
851
852                 l_a = e_clear->l;
853
854                 BLI_assert(l_a->f->len == 3);
855
856                 /* keep 'v_clear' 0th */
857                 if (BM_vert_in_edge(l_a->prev->e, v_clear)) {
858                         e_a_other[0] = l_a->prev->e;
859                         e_a_other[1] = l_a->next->e;
860                 }
861                 else {
862                         e_a_other[1] = l_a->prev->e;
863                         e_a_other[0] = l_a->next->e;
864                 }
865
866                 r_e_clear_other[0] = BM_elem_index_get(e_a_other[0]);
867                 r_e_clear_other[1] = -1;
868
869 #ifdef USE_CUSTOMDATA
870                 /* before killing, do customdata */
871                 if (customdata_flag & CD_DO_VERT) {
872                         BM_data_interp_from_verts(bm, v_other, v_clear, v_other, customdata_fac);
873                 }
874                 if (customdata_flag & CD_DO_EDGE) {
875                         BM_data_interp_from_edges(bm, e_a_other[1], e_a_other[0], e_a_other[1], customdata_fac);
876                 }
877                 if (customdata_flag & CD_DO_LOOP) {
878                         bm_edge_collapse_loop_customdata(bm, e_clear->l, v_clear, v_other, customdata_fac);
879                 }
880 #endif
881
882                 BM_edge_kill(bm, e_clear);
883
884                 v_other->head.hflag |= v_clear->head.hflag;
885                 BM_vert_splice(bm, v_other, v_clear);
886
887                 e_a_other[1]->head.hflag |= e_a_other[0]->head.hflag;
888                 BM_edge_splice(bm, e_a_other[1], e_a_other[0]);
889
890                 // BM_mesh_validate(bm);
891
892                 return true;
893         }
894         else {
895                 return false;
896         }
897 }
898
899
900 /* collapse e the edge, removing e->v2 */
901 static void bm_decim_edge_collapse(
902         BMesh *bm, BMEdge *e,
903         Quadric *vquadrics, float *vweights,
904         Heap *eheap, HeapNode **eheap_table,
905         const CD_UseFlag customdata_flag)
906 {
907         int e_clear_other[2];
908         BMVert *v_other = e->v1;
909         const int v_other_index = BM_elem_index_get(e->v1);
910         const int v_clear_index = BM_elem_index_get(e->v2);  /* the vert is removed so only store the index */
911         float optimize_co[3];
912         float customdata_fac;
913
914 #ifdef USE_VERT_NORMAL_INTERP
915         float v_clear_no[3];
916         copy_v3_v3(v_clear_no, e->v2->no);
917 #endif
918
919         /* disallow collapsing which results in degenerate cases */
920         if (UNLIKELY(bm_edge_collapse_is_degenerate_topology(e))) {
921                 bm_decim_invalid_edge_cost_single(e, eheap, eheap_table);  /* add back with a high cost */
922                 return;
923         }
924
925         bm_decim_calc_target_co(e, optimize_co, vquadrics);
926
927         /* check if this would result in an overlapping face */
928         if (UNLIKELY(bm_edge_collapse_is_degenerate_flip(e, optimize_co))) {
929                 bm_decim_invalid_edge_cost_single(e, eheap, eheap_table);  /* add back with a high cost */
930                 return;
931         }
932
933         /* use for customdata merging */
934         if (LIKELY(compare_v3v3(e->v1->co, e->v2->co, FLT_EPSILON) == false)) {
935                 customdata_fac = line_point_factor_v3(optimize_co, e->v1->co, e->v2->co);
936 #if 0
937                 /* simple test for stupid collapse */
938                 if (customdata_fac < 0.0 - FLT_EPSILON || customdata_fac > 1.0f + FLT_EPSILON) {
939                         return;
940                 }
941 #endif
942         }
943         else {
944                 /* avoid divide by zero */
945                 customdata_fac = 0.5f;
946         }
947
948         if (bm_edge_collapse(bm, e, e->v2, e_clear_other, customdata_flag, customdata_fac)) {
949                 /* update collapse info */
950                 int i;
951
952                 if (vweights) {
953                         vweights[v_other_index] += vweights[v_clear_index];
954                 }
955
956                 e = NULL;  /* paranoid safety check */
957
958                 copy_v3_v3(v_other->co, optimize_co);
959
960                 /* remove eheap */
961                 for (i = 0; i < 2; i++) {
962                         /* highly unlikely 'eheap_table[ke_other[i]]' would be NULL, but do for sanity sake */
963                         if ((e_clear_other[i] != -1) && (eheap_table[e_clear_other[i]] != NULL)) {
964                                 BLI_heap_remove(eheap, eheap_table[e_clear_other[i]]);
965                                 eheap_table[e_clear_other[i]] = NULL;
966                         }
967                 }
968
969                 /* update vertex quadric, add kept vertex from killed vertex */
970                 BLI_quadric_add_qu_qu(&vquadrics[v_other_index], &vquadrics[v_clear_index]);
971
972                 /* update connected normals */
973
974                 /* in fact face normals are not used for progressive updates, no need to update them */
975                 // BM_vert_normal_update_all(v);
976 #ifdef USE_VERT_NORMAL_INTERP
977                 interp_v3_v3v3(v_other->no, v_other->no, v_clear_no, customdata_fac);
978                 normalize_v3(v_other->no);
979 #else
980                 BM_vert_normal_update(v_other);
981 #endif
982
983
984                 /* update error costs and the eheap */
985                 if (LIKELY(v_other->e)) {
986                         BMEdge *e_iter;
987                         BMEdge *e_first;
988                         e_iter = e_first = v_other->e;
989                         do {
990                                 BLI_assert(BM_edge_find_double(e_iter) == NULL);
991                                 bm_decim_build_edge_cost_single(e_iter, vquadrics, vweights, eheap, eheap_table);
992                         } while ((e_iter = bmesh_disk_edge_next(e_iter, v_other)) != e_first);
993                 }
994
995                 /* this block used to be disabled,
996                  * but enable now since surrounding faces may have been
997                  * set to COST_INVALID because of a face overlap that no longer occurs */
998 #if 1
999                 /* optional, update edges around the vertex face fan */
1000                 {
1001                         BMIter liter;
1002                         BMLoop *l;
1003                         BM_ITER_ELEM (l, &liter, v_other, BM_LOOPS_OF_VERT) {
1004                                 if (l->f->len == 3) {
1005                                         BMEdge *e_outer;
1006                                         if (BM_vert_in_edge(l->prev->e, l->v))
1007                                                 e_outer = l->next->e;
1008                                         else
1009                                                 e_outer = l->prev->e;
1010
1011                                         BLI_assert(BM_vert_in_edge(e_outer, l->v) == false);
1012
1013                                         bm_decim_build_edge_cost_single(e_outer, vquadrics, vweights, eheap, eheap_table);
1014                                 }
1015                         }
1016                 }
1017                 /* end optional update */
1018 #endif
1019         }
1020         else {
1021                 /* add back with a high cost */
1022                 bm_decim_invalid_edge_cost_single(e, eheap, eheap_table);
1023         }
1024 }
1025
1026
1027 /* Main Decimate Function
1028  * ********************** */
1029
1030 /**
1031  * \brief BM_mesh_decimate
1032  * \param bm The mesh
1033  * \param factor face count multiplier [0 - 1]
1034  * \param vweights Optional array of vertex  aligned weights [0 - 1],
1035  *        a vertex group is the usual source for this.
1036  */
1037 void BM_mesh_decimate_collapse(BMesh *bm, const float factor, float *vweights, const bool do_triangulate)
1038 {
1039         Heap *eheap;             /* edge heap */
1040         HeapNode **eheap_table;  /* edge index aligned table pointing to the eheap */
1041         Quadric *vquadrics;      /* vert index aligned quadrics */
1042         int tot_edge_orig;
1043         int face_tot_target;
1044         bool use_triangulate;
1045
1046         CD_UseFlag customdata_flag = 0;
1047
1048 #ifdef USE_TRIANGULATE
1049         /* temp convert quads to triangles */
1050         use_triangulate = bm_decim_triangulate_begin(bm);
1051 #endif
1052
1053
1054         /* alloc vars */
1055         vquadrics = MEM_callocN(sizeof(Quadric) * bm->totvert, __func__);
1056         /* since some edges may be degenerate, we might be over allocing a little here */
1057         eheap = BLI_heap_new_ex(bm->totedge);
1058         eheap_table = MEM_mallocN(sizeof(HeapNode *) * bm->totedge, __func__);
1059         tot_edge_orig = bm->totedge;
1060
1061
1062         /* build initial edge collapse cost data */
1063         bm_decim_build_quadrics(bm, vquadrics);
1064
1065         bm_decim_build_edge_cost(bm, vquadrics, vweights, eheap, eheap_table);
1066
1067         face_tot_target = bm->totface * factor;
1068         bm->elem_index_dirty |= BM_ALL;
1069
1070
1071 #ifdef USE_CUSTOMDATA
1072         /* initialize customdata flag, we only need math for loops */
1073         if (CustomData_has_interp(&bm->vdata))  customdata_flag |= CD_DO_VERT;
1074         if (CustomData_has_interp(&bm->edata))  customdata_flag |= CD_DO_EDGE;
1075         if (CustomData_has_math(&bm->ldata))    customdata_flag |= CD_DO_LOOP;
1076 #endif
1077
1078         /* iterative edge collapse and maintain the eheap */
1079         while ((bm->totface > face_tot_target) &&
1080                (BLI_heap_is_empty(eheap) == false) &&
1081                (BLI_heap_node_value(BLI_heap_top(eheap)) != COST_INVALID))
1082         {
1083                 // const float value = BLI_heap_node_value(BLI_heap_top(eheap));
1084                 BMEdge *e = BLI_heap_popmin(eheap);
1085                 BLI_assert(BM_elem_index_get(e) < tot_edge_orig);  /* handy to detect corruptions elsewhere */
1086
1087                 // printf("COST %.10f\n", value);
1088
1089                 /* under normal conditions wont be accessed again,
1090                  * but NULL just incase so we don't use freed node */
1091                 eheap_table[BM_elem_index_get(e)] = NULL;
1092
1093                 bm_decim_edge_collapse(bm, e, vquadrics, vweights, eheap, eheap_table, customdata_flag);
1094         }
1095
1096
1097 #ifdef USE_TRIANGULATE
1098         if (do_triangulate == false) {
1099                 /* its possible we only had triangles, skip this step in that case */
1100                 if (LIKELY(use_triangulate)) {
1101                         /* temp convert quads to triangles */
1102                         bm_decim_triangulate_end(bm);
1103                 }
1104         }
1105 #endif
1106
1107         /* free vars */
1108         MEM_freeN(vquadrics);
1109         MEM_freeN(eheap_table);
1110         BLI_heap_free(eheap, NULL);
1111
1112         /* testing only */
1113         // BM_mesh_validate(bm);
1114
1115         (void)tot_edge_orig;  /* quiet release build warning */
1116 }