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