style cleanup: formatting and some float/double promotion
[blender.git] / source / blender / bmesh / operators / bmo_removedoubles.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): Joseph Eagar.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/bmesh/operators/bmo_removedoubles.c
24  *  \ingroup bmesh
25  */
26
27 #include "MEM_guardedalloc.h"
28
29 #include "BLI_math.h"
30 #include "BLI_array.h"
31
32 #include "BKE_customdata.h"
33
34 #include "bmesh.h"
35 #include "intern/bmesh_private.h"
36
37 #include "intern/bmesh_operators_private.h" /* own include */
38
39 static void remdoubles_splitface(BMFace *f, BMesh *bm, BMOperator *op)
40 {
41         BMIter liter;
42         BMLoop *l;
43         BMVert *v2, *doub;
44         int split = FALSE;
45
46         BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
47                 v2 = BMO_slot_map_ptr_get(bm, op, "targetmap", l->v);
48                 /* ok: if v2 is NULL (e.g. not in the map) then it's
49                  *     a target vert, otherwise it's a double */
50                 if ((v2 && BM_vert_in_face(f, v2)) &&
51                     (v2 != l->prev->v) &&
52                     (v2 != l->next->v))
53                 {
54                         doub = l->v;
55                         split = TRUE;
56                         break;
57                 }
58         }
59
60         if (split && doub != v2) {
61                 BMLoop *nl;
62                 BMFace *f2 = BM_face_split(bm, f, doub, v2, &nl, NULL, FALSE);
63
64                 remdoubles_splitface(f, bm, op);
65                 remdoubles_splitface(f2, bm, op);
66         }
67 }
68
69 #define ELE_DEL         1
70 #define EDGE_COL        2
71 #define FACE_MARK       2
72
73 #if 0
74 int remdoubles_face_overlaps(BMesh *bm, BMVert **varr,
75                              int len, BMFace *exclude,
76                              BMFace **overlapface)
77 {
78         BMIter vertfaces;
79         BMFace *f;
80         int i, amount;
81
82         if (overlapface) *overlapface = NULL;
83
84         for (i = 0; i < len; i++) {
85                 f = BM_iter_new(&vertfaces, bm, BM_FACES_OF_VERT, varr[i]);
86                 while (f) {
87                         amount = BM_verts_in_face(bm, f, varr, len);
88                         if (amount >= len) {
89                                 if (overlapface) *overlapface = f;
90                                 return TRUE;
91                         }
92                         f = BM_iter_step(&vertfaces);
93                 }
94         }
95         return FALSE;
96 }
97 #endif
98
99 void bmo_weldverts_exec(BMesh *bm, BMOperator *op)
100 {
101         BMIter iter, liter;
102         BMVert *v, *v2;
103         BMEdge *e, *e2, **edges = NULL;
104         BLI_array_declare(edges);
105         BMLoop *l, *l2, **loops = NULL;
106         BLI_array_declare(loops);
107         BMFace *f, *f2;
108         int a, b;
109
110         /* mark merge verts for deletion */
111         BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
112                 if ((v2 = BMO_slot_map_ptr_get(bm, op, "targetmap", v))) {
113                         BMO_elem_flag_enable(bm, v, ELE_DEL);
114
115                         /* merge the vertex flags, else we get randomly selected/unselected verts */
116                         BM_elem_flag_merge(v, v2);
117                 }
118         }
119
120         /* check if any faces are getting their own corners merged
121          * together, split face if so */
122         BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
123                 remdoubles_splitface(f, bm, op);
124         }
125
126         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
127                 if (BMO_elem_flag_test(bm, e->v1, ELE_DEL) || BMO_elem_flag_test(bm, e->v2, ELE_DEL)) {
128                         v = BMO_slot_map_ptr_get(bm, op, "targetmap", e->v1);
129                         v2 = BMO_slot_map_ptr_get(bm, op, "targetmap", e->v2);
130                         
131                         if (!v) v = e->v1;
132                         if (!v2) v2 = e->v2;
133
134                         if (v == v2) {
135                                 BMO_elem_flag_enable(bm, e, EDGE_COL);
136                         }
137                         else if (!BM_edge_exists(v, v2)) {
138                                 BM_edge_create(bm, v, v2, e, TRUE);
139                         }
140
141                         BMO_elem_flag_enable(bm, e, ELE_DEL);
142                 }
143         }
144
145         /* BMESH_TODO, stop abusing face index here */
146         BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
147                 BM_elem_index_set(f, 0); /* set_dirty! */
148                 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
149                         if (BMO_elem_flag_test(bm, l->v, ELE_DEL)) {
150                                 BMO_elem_flag_enable(bm, f, FACE_MARK | ELE_DEL);
151                         }
152                         if (BMO_elem_flag_test(bm, l->e, EDGE_COL)) {
153                                 BM_elem_index_set(f, BM_elem_index_get(f) + 1); /* set_dirty! */
154                         }
155                 }
156         }
157         bm->elem_index_dirty |= BM_FACE;
158
159         /* faces get "modified" by creating new faces here, then at the
160          * end the old faces are deleted */
161         BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
162                 if (!BMO_elem_flag_test(bm, f, FACE_MARK))
163                         continue;
164
165                 if (f->len - BM_elem_index_get(f) < 3) {
166                         BMO_elem_flag_enable(bm, f, ELE_DEL);
167                         continue;
168                 }
169
170                 BLI_array_empty(edges);
171                 BLI_array_empty(loops);
172                 a = 0;
173                 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
174                         v = l->v;
175                         v2 = l->next->v;
176                         if (BMO_elem_flag_test(bm, v, ELE_DEL)) {
177                                 v = BMO_slot_map_ptr_get(bm, op, "targetmap", v);
178                         }
179                         if (BMO_elem_flag_test(bm, v2, ELE_DEL)) {
180                                 v2 = BMO_slot_map_ptr_get(bm, op, "targetmap", v2);
181                         }
182                         
183                         e2 = v != v2 ? BM_edge_exists(v, v2) : NULL;
184                         if (e2) {
185                                 for (b = 0; b < a; b++) {
186                                         if (edges[b] == e2) {
187                                                 break;
188                                         }
189                                 }
190                                 if (b != a) {
191                                         continue;
192                                 }
193
194                                 BLI_array_grow_one(edges);
195                                 BLI_array_grow_one(loops);
196
197                                 edges[a] = e2;
198                                 loops[a] = l;
199
200                                 a++;
201                         }
202                 }
203                 
204                 if (BLI_array_count(loops) < 3)
205                         continue;
206                 v = loops[0]->v;
207                 v2 = loops[1]->v;
208
209                 if (BMO_elem_flag_test(bm, v, ELE_DEL)) {
210                         v = BMO_slot_map_ptr_get(bm, op, "targetmap", v);
211                 }
212                 if (BMO_elem_flag_test(bm, v2, ELE_DEL)) {
213                         v2 = BMO_slot_map_ptr_get(bm, op, "targetmap", v2);
214                 }
215                 
216                 f2 = BM_face_create_ngon(bm, v, v2, edges, a, TRUE);
217                 if (f2 && (f2 != f)) {
218                         BM_elem_attrs_copy(bm, bm, f, f2);
219
220                         a = 0;
221                         BM_ITER_ELEM (l, &liter, f2, BM_LOOPS_OF_FACE) {
222                                 l2 = loops[a];
223                                 BM_elem_attrs_copy(bm, bm, l2, l);
224
225                                 a++;
226                         }
227                 }
228         }
229
230         BMO_op_callf(bm, "del geom=%fvef context=%i", ELE_DEL, DEL_ONLYTAGGED);
231
232         BLI_array_free(edges);
233         BLI_array_free(loops);
234 }
235
236 static int vergaverco(const void *e1, const void *e2)
237 {
238         const BMVert *v1 = *(void **)e1, *v2 = *(void **)e2;
239         float x1 = v1->co[0] + v1->co[1] + v1->co[2];
240         float x2 = v2->co[0] + v2->co[1] + v2->co[2];
241
242         if      (x1 > x2) return  1;
243         else if (x1 < x2) return -1;
244         else return 0;
245 }
246
247 #define VERT_TESTED     1
248 #define VERT_DOUBLE     2
249 #define VERT_TARGET     4
250 #define VERT_KEEP       8
251 #define VERT_MARK       16
252 #define VERT_IN         32
253
254 #define EDGE_MARK       1
255
256 void bmo_pointmerge_facedata_exec(BMesh *bm, BMOperator *op)
257 {
258         BMOIter siter;
259         BMIter iter;
260         BMVert *v, *snapv;
261         BMLoop *l, *firstl = NULL;
262         float fac;
263         int i, tot;
264
265         snapv = BMO_iter_new(&siter, bm, op, "snapv", BM_VERT);
266         tot = BM_vert_face_count(snapv);
267
268         if (!tot)
269                 return;
270
271         fac = 1.0f / tot;
272         BM_ITER_ELEM (l, &iter, snapv, BM_LOOPS_OF_VERT) {
273                 if (!firstl) {
274                         firstl = l;
275                 }
276                 
277                 for (i = 0; i < bm->ldata.totlayer; i++) {
278                         if (CustomData_layer_has_math(&bm->ldata, i)) {
279                                 int type = bm->ldata.layers[i].type;
280                                 void *e1, *e2;
281
282                                 e1 = CustomData_bmesh_get_layer_n(&bm->ldata, firstl->head.data, i);
283                                 e2 = CustomData_bmesh_get_layer_n(&bm->ldata, l->head.data, i);
284                                 
285                                 CustomData_data_multiply(type, e2, fac);
286
287                                 if (l != firstl)
288                                         CustomData_data_add(type, e1, e2);
289                         }
290                 }
291         }
292
293         BMO_ITER (v, &siter, bm, op, "verts", BM_VERT) {
294                 BM_ITER_ELEM (l, &iter, v, BM_LOOPS_OF_VERT) {
295                         if (l == firstl) {
296                                 continue;
297                         }
298
299                         CustomData_bmesh_copy_data(&bm->ldata, &bm->ldata, firstl->head.data, &l->head.data);
300                 }
301         }
302 }
303
304 void bmo_vert_average_facedata_exec(BMesh *bm, BMOperator *op)
305 {
306         BMOIter siter;
307         BMIter iter;
308         BMVert *v;
309         BMLoop *l /* , *firstl = NULL */;
310         CDBlockBytes min, max;
311         void *block;
312         int i, type;
313
314         for (i = 0; i < bm->ldata.totlayer; i++) {
315                 if (!CustomData_layer_has_math(&bm->ldata, i))
316                         continue;
317                 
318                 type = bm->ldata.layers[i].type;
319                 CustomData_data_initminmax(type, &min, &max);
320
321                 BMO_ITER (v, &siter, bm, op, "verts", BM_VERT) {
322                         BM_ITER_ELEM (l, &iter, v, BM_LOOPS_OF_VERT) {
323                                 block = CustomData_bmesh_get_layer_n(&bm->ldata, l->head.data, i);
324                                 CustomData_data_dominmax(type, block, &min, &max);
325                         }
326                 }
327
328                 CustomData_data_multiply(type, &min, 0.5f);
329                 CustomData_data_multiply(type, &max, 0.5f);
330                 CustomData_data_add(type, &min, &max);
331
332                 BMO_ITER (v, &siter, bm, op, "verts", BM_VERT) {
333                         BM_ITER_ELEM (l, &iter, v, BM_LOOPS_OF_VERT) {
334                                 block = CustomData_bmesh_get_layer_n(&bm->ldata, l->head.data, i);
335                                 CustomData_data_copy_value(type, &min, block);
336                         }
337                 }
338         }
339 }
340
341 void bmo_pointmerge_exec(BMesh *bm, BMOperator *op)
342 {
343         BMOperator weldop;
344         BMOIter siter;
345         BMVert *v, *snapv = NULL;
346         float vec[3];
347         
348         BMO_slot_vec_get(op, "mergeco", vec);
349
350         //BMO_op_callf(bm, "collapse_uvs edges=%s", op, "edges");
351         BMO_op_init(bm, &weldop, "weldverts");
352         
353         BMO_ITER (v, &siter, bm, op, "verts", BM_VERT) {
354                 if (!snapv) {
355                         snapv = v;
356                         copy_v3_v3(snapv->co, vec);
357                 }
358                 else {
359                         BMO_slot_map_ptr_insert(bm, &weldop, "targetmap", v, snapv);
360                 }
361         }
362
363         BMO_op_exec(bm, &weldop);
364         BMO_op_finish(bm, &weldop);
365 }
366
367 void bmo_collapse_exec(BMesh *bm, BMOperator *op)
368 {
369         BMOperator weldop;
370         BMWalker walker;
371         BMIter iter;
372         BMEdge *e, **edges = NULL;
373         BLI_array_declare(edges);
374         float min[3], max[3];
375         int i, tot;
376         
377         BMO_op_callf(bm, "collapse_uvs edges=%s", op, "edges");
378         BMO_op_init(bm, &weldop, "weldverts");
379
380         BMO_slot_buffer_flag_enable(bm, op, "edges", BM_EDGE, EDGE_MARK);
381
382         BMW_init(&walker, bm, BMW_SHELL,
383                  BMW_MASK_NOP, EDGE_MARK, BMW_MASK_NOP,
384                  BMW_FLAG_NOP, /* no need to use BMW_FLAG_TEST_HIDDEN, already marked data */
385                  BMW_NIL_LAY);
386
387         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
388                 if (!BMO_elem_flag_test(bm, e, EDGE_MARK))
389                         continue;
390
391                 e = BMW_begin(&walker, e->v1);
392                 BLI_array_empty(edges);
393
394                 INIT_MINMAX(min, max);
395                 for (tot = 0; e; tot++, e = BMW_step(&walker)) {
396                         BLI_array_grow_one(edges);
397                         edges[tot] = e;
398
399                         DO_MINMAX(e->v1->co, min, max);
400                         DO_MINMAX(e->v2->co, min, max);
401                 }
402
403                 add_v3_v3v3(min, min, max);
404                 mul_v3_fl(min, 0.5f);
405
406                 /* snap edges to a point.  for initial testing purposes anyway */
407                 for (i = 0; i < tot; i++) {
408                         copy_v3_v3(edges[i]->v1->co, min);
409                         copy_v3_v3(edges[i]->v2->co, min);
410                         
411                         if (edges[i]->v1 != edges[0]->v1)
412                                 BMO_slot_map_ptr_insert(bm, &weldop, "targetmap", edges[i]->v1, edges[0]->v1);
413                         if (edges[i]->v2 != edges[0]->v1)
414                                 BMO_slot_map_ptr_insert(bm, &weldop, "targetmap", edges[i]->v2, edges[0]->v1);
415                 }
416         }
417         
418         BMO_op_exec(bm, &weldop);
419         BMO_op_finish(bm, &weldop);
420
421         BMW_end(&walker);
422         BLI_array_free(edges);
423 }
424
425 /* uv collapse function */
426 static void bmo_collapsecon_do_layer(BMesh *bm, BMOperator *op, int layer)
427 {
428         BMIter iter, liter;
429         BMFace *f;
430         BMLoop *l, *l2;
431         BMWalker walker;
432         void **blocks = NULL;
433         BLI_array_declare(blocks);
434         CDBlockBytes min, max;
435         int i, tot, type = bm->ldata.layers[layer].type;
436
437         /* clear all short flags */
438         BMO_mesh_flag_disable_all(bm, op, BM_ALL, (1 << 16) - 1);
439
440         BMO_slot_buffer_flag_enable(bm, op, "edges", BM_EDGE, EDGE_MARK);
441
442         BMW_init(&walker, bm, BMW_LOOPDATA_ISLAND,
443                  BMW_MASK_NOP, EDGE_MARK, BMW_MASK_NOP,
444                  BMW_FLAG_NOP, /* no need to use BMW_FLAG_TEST_HIDDEN, already marked data */
445                  layer);
446
447         BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
448                 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
449                         if (BMO_elem_flag_test(bm, l->e, EDGE_MARK)) {
450                                 /* walk */
451                                 BLI_array_empty(blocks);
452                                 tot = 0;
453                                 l2 = BMW_begin(&walker, l);
454
455                                 CustomData_data_initminmax(type, &min, &max);
456                                 for (tot = 0; l2; tot++, l2 = BMW_step(&walker)) {
457                                         BLI_array_grow_one(blocks);
458                                         blocks[tot] = CustomData_bmesh_get_layer_n(&bm->ldata, l2->head.data, layer);
459                                         CustomData_data_dominmax(type, blocks[tot], &min, &max);
460                                 }
461
462                                 if (tot) {
463                                         CustomData_data_multiply(type, &min, 0.5f);
464                                         CustomData_data_multiply(type, &max, 0.5f);
465                                         CustomData_data_add(type, &min, &max);
466
467                                         /* snap CD (uv, vcol) points to their centroid */
468                                         for (i = 0; i < tot; i++) {
469                                                 CustomData_data_copy_value(type, &min, blocks[i]);
470                                         }
471                                 }
472                         }
473                 }
474         }
475
476         BMW_end(&walker);
477         BLI_array_free(blocks);
478 }
479
480 void bmo_collapse_uvs_exec(BMesh *bm, BMOperator *op)
481 {
482         int i;
483
484         for (i = 0; i < bm->ldata.totlayer; i++) {
485                 if (CustomData_layer_has_math(&bm->ldata, i))
486                         bmo_collapsecon_do_layer(bm, op, i);
487         }
488 }
489
490 void bmesh_finddoubles_common(BMesh *bm, BMOperator *op, BMOperator *optarget, const char *targetmapname)
491 {
492         BMOIter oiter;
493         BMVert *v, *v2;
494         BMVert **verts = NULL;
495         BLI_array_declare(verts);
496         float dist, dist3;
497         int i, j, len, keepvert = 0;
498
499         dist = BMO_slot_float_get(op, "dist");
500         dist3 = dist * 3.0f;
501
502         i = 0;
503         BMO_ITER (v, &oiter, bm, op, "verts", BM_VERT) {
504                 BLI_array_grow_one(verts);
505                 verts[i++] = v;
506         }
507
508         /* Test whether keepverts arg exists and is non-empty */
509         if (BMO_slot_exists(op, "keepverts")) {
510                 keepvert = BMO_iter_new(&oiter, bm, op, "keepverts", BM_VERT) != NULL;
511         }
512
513         /* sort by vertex coordinates added together */
514         qsort(verts, BLI_array_count(verts), sizeof(void *), vergaverco);
515
516         /* Flag keepverts */
517         if (keepvert) {
518                 BMO_slot_buffer_flag_enable(bm, op, "keepverts", BM_VERT, VERT_KEEP);
519         }
520
521         len = BLI_array_count(verts);
522         for (i = 0; i < len; i++) {
523                 v = verts[i];
524                 if (BMO_elem_flag_test(bm, v, VERT_DOUBLE)) {
525                         continue;
526                 }
527
528                 for (j = i + 1; j < len; j++) {
529                         v2 = verts[j];
530
531                         /* Compare sort values of the verts using 3x tolerance (allowing for the tolerance
532                          * on each of the three axes). This avoids the more expensive length comparison
533                          * for most vertex pairs. */
534                         if ((v2->co[0] + v2->co[1] + v2->co[2]) - (v->co[0] + v->co[1] + v->co[2]) > dist3)
535                                 break;
536
537                         if (keepvert) {
538                                 if (BMO_elem_flag_test(bm, v2, VERT_KEEP) == BMO_elem_flag_test(bm, v, VERT_KEEP))
539                                         continue;
540                         }
541
542                         if (compare_len_v3v3(v->co, v2->co, dist)) {
543
544                                 /* If one vert is marked as keep, make sure it will be the target */
545                                 if (BMO_elem_flag_test(bm, v2, VERT_KEEP)) {
546                                         SWAP(BMVert *, v, v2);
547                                 }
548
549                                 BMO_elem_flag_enable(bm, v2, VERT_DOUBLE);
550                                 BMO_elem_flag_enable(bm, v, VERT_TARGET);
551
552                                 BMO_slot_map_ptr_insert(bm, optarget, targetmapname, v2, v);
553                         }
554                 }
555         }
556
557         BLI_array_free(verts);
558 }
559
560 void bmo_removedoubles_exec(BMesh *bm, BMOperator *op)
561 {
562         BMOperator weldop;
563
564         BMO_op_init(bm, &weldop, "weldverts");
565         bmesh_finddoubles_common(bm, op, &weldop, "targetmap");
566         BMO_op_exec(bm, &weldop);
567         BMO_op_finish(bm, &weldop);
568 }
569
570
571 void bmo_finddoubles_exec(BMesh *bm, BMOperator *op)
572 {
573         bmesh_finddoubles_common(bm, op, op, "targetmapout");
574 }
575
576 void bmo_automerge_exec(BMesh *bm, BMOperator *op)
577 {
578         BMOperator findop, weldop;
579         BMIter viter;
580         BMVert *v;
581
582         /* The "verts" input sent to this op is the set of verts that
583          * can be merged away into any other verts. Mark all other verts
584          * as VERT_KEEP. */
585         BMO_slot_buffer_flag_enable(bm, op, "verts", BM_VERT, VERT_IN);
586         BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
587                 if (!BMO_elem_flag_test(bm, v, VERT_IN)) {
588                         BMO_elem_flag_enable(bm, v, VERT_KEEP);
589                 }
590         }
591
592         /* Search for doubles among all vertices, but only merge non-VERT_KEEP
593          * vertices into VERT_KEEP vertices. */
594         BMO_op_initf(bm, &findop, "finddoubles verts=%av keepverts=%fv", VERT_KEEP);
595         BMO_slot_copy(op, &findop, "dist", "dist");
596         BMO_op_exec(bm, &findop);
597
598         /* weld the vertices */
599         BMO_op_init(bm, &weldop, "weldverts");
600         BMO_slot_copy(&findop, &weldop, "targetmapout", "targetmap");
601         BMO_op_exec(bm, &weldop);
602
603         BMO_op_finish(bm, &findop);
604         BMO_op_finish(bm, &weldop);
605 }