c9e29c9f94a8cc8e5c1fe0ea29a8e644576d2491
[blender.git] / source / blender / bmesh / operators / dissolveops.c
1 #include "MEM_guardedalloc.h"
2
3 #include "BKE_utildefines.h"
4
5 #include "bmesh.h"
6 #include "mesh_intern.h"
7 #include "bmesh_private.h"
8 #include "BLI_arithb.h"
9
10 #include <stdio.h>
11
12 #define FACE_MARK       1
13
14 #define VERT_MARK       1
15
16 void dissolvefaces_exec(BMesh *bm, BMOperator *op)
17 {
18         BMOIter iter;
19         BMIter liter;
20         BMLoop *l;
21         BMFace *f, *f2, *nf = NULL;
22
23         //BMO_Flag_Buffer(bm, op, BMOP_DISFACES_FACEIN, FACE_MARK);
24
25         /*TODO: need to discuss with Briggs how best to implement this, seems this would be
26           a great time to use the walker api, get it up to snuff.  perhaps have a walker
27           that goes over inner vertices of a contiguously-flagged region?  then you
28           could just use dissolve disk on them.*/
29         if (BMO_GetSlot(op, BMOP_DISFACES_FACEIN)->len != 2) return;
30
31         /*HACK: for debugging purposes, handle cases of two adjacent faces*/
32         f = BMO_IterNew(&iter, bm, op, BMOP_DISFACES_FACEIN);
33         f2 = BMO_IterStep(&iter);
34
35         for (l=BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);l;l=BMIter_Step(&liter)) {
36                 if (!l->radial.next) continue;
37                 if (((BMLoop*)l->radial.next->data)->f == f2) {
38                         nf = BM_Join_Faces(bm, f, f2, l->e, 1, 0);
39                         break;
40                 }
41         }
42
43         if (nf) {
44                 BMO_SetFlag(bm, nf, 1);
45                 BMO_Flag_To_Slot(bm, op, BMOP_DISFACES_REGIONOUT, 1, BM_FACE);
46         }
47
48 }
49
50 /*returns 1 if any faces were dissolved*/
51 int BM_DissolveFaces(EditMesh *em, int flag) {
52         BMesh *bm = editmesh_to_bmesh(em);
53         EditMesh *em2;
54         BMOperator op;
55
56         BMO_Init_Op(&op, BMOP_DISSOLVE_FACES);
57         BMO_HeaderFlag_To_Slot(bm, &op, BMOP_DISFACES_FACEIN, flag, BM_FACE);
58         BMO_Exec_Op(bm, &op);
59         BMO_Finish_Op(bm, &op);
60         
61         em2 = bmesh_to_editmesh(bm);
62         set_editMesh(em, em2);
63         MEM_freeN(em2);
64
65         return BMO_GetSlot(&op, BMOP_DISFACES_REGIONOUT)->len > 0;
66 }
67
68 void dissolveverts_exec(BMesh *bm, BMOperator *op)
69 {
70         BMOpSlot *vinput;
71         BMIter iter, liter, fiter;
72         BMVert *v;
73         BMFace *f, *f2;
74         BMEdge *fe;
75         BMLoop *l;
76         int found, found2, found3, len, oldlen=0;
77         
78         vinput = BMO_GetSlot(op, BMOP_DISVERTS_VERTIN);
79
80         BMO_Flag_Buffer(bm, op, BMOP_DISVERTS_VERTIN, VERT_MARK);
81         
82         found = 1;
83         while (found) {
84                 found = 0;
85                 len = 0;
86                 for (v=BMIter_New(&iter, bm, BM_VERTS, NULL); v; v=BMIter_Step(&iter)) {
87                         if (BMO_TestFlag(bm, v, VERT_MARK)) {
88                                 BM_Dissolve_Disk(bm, v);
89                                 found = 1;
90                                 len++;
91                         }
92                 }
93
94
95                 /*clean up two-edged faces*/
96                 /*basic idea is to keep joining 2-edged faces until their
97                   gone.  this however relies on joining two 2-edged faces
98                   together to work, which doesn't.*/
99                 found3 = 1;
100                 while (found3) {
101                         found3 = 0;
102                         for (f=BMIter_New(&iter, bm, BM_FACES, NULL); f; f=BMIter_Step(&iter)){
103                                 if (BM_Validate_Face(bm, f, stderr)) {
104                                         printf("error.\n");
105                                 }
106
107                                 if (f->len == 2) {
108                                         //this design relies on join faces working
109                                         //with two-edged faces properly.
110                                         //commenting this line disables the
111                                         //outermost loop.
112                                         //found3 = 1;
113                                         found2 = 0;
114                                         l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
115                                         fe = l->e;
116                                         for (; l; l=BMIter_Step(&liter)) {
117                                                 f2 = BMIter_New(&fiter, bm,
118                                                                 BM_FACES_OF_EDGE, l->e);
119                                                 for (; f2; f2=BMIter_Step(&fiter)) {
120                                                         if (f2 != f) {
121                                                                 BM_Join_Faces(bm, f, f2, l->e, 
122                                                                               1, 0);
123                                                                 found2 = 1;
124                                                                 break;
125                                                         }
126                                                 }
127                                                 if (found2) break;
128                                         }
129
130                                         if (!found2) {
131                                                 bmesh_kf(bm, f);
132                                                 bmesh_ke(bm, fe);
133                                         }
134                                 } /*else if (f->len == 3) {
135                                         BMEdge *ed[3];
136                                         BMVert *vt[3];
137                                         BMLoop *lp[3];
138                                         int i=0;
139
140                                         //check for duplicate edges
141                                         l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
142                                         for (; l; l=BMIter_Step(&liter)) {
143                                                 ed[i] = l->e;   
144                                                 lp[i] = l;
145                                                 vt[i++] = l->v;
146                                         }
147                                         if (vt[0] == vt[1] || vt[0] == vt[2]) {
148                                                 i += 1;
149                                         }
150                                 }*/
151                         }
152                 }
153                 if (oldlen == len) break;
154                 oldlen = len;
155         }
156
157 }