Merging r57729 through r57815 from trunk into soc-2013-depsgraph_mt
[blender.git] / source / blender / bmesh / operators / bmo_connect.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_connect.c
24  *  \ingroup bmesh
25  *
26  * Connect verts across faces (splits faces).
27  */
28
29 #include "MEM_guardedalloc.h"
30
31 #include "BLI_math.h"
32 #include "BLI_array.h"
33 #include "BLI_utildefines.h"
34
35 #include "bmesh.h"
36
37 #include "intern/bmesh_operators_private.h" /* own include */
38
39 #define VERT_INPUT      1
40 #define EDGE_OUT        1
41 #define FACE_TAG        2
42
43 static int bm_face_connect_verts(BMesh *bm, BMFace *f)
44 {
45         BMLoop *(*loops_split)[2] = BLI_array_alloca(loops_split, f->len);
46         STACK_DECLARE(loops_split);
47         BMVert *(*verts_pair)[2] = BLI_array_alloca(verts_pair, f->len);
48         STACK_DECLARE(verts_pair);
49
50         BMIter liter;
51         BMFace *f_new;
52         BMLoop *l, *l_new;
53         BMLoop *l_last;
54         unsigned int i;
55
56         STACK_INIT(loops_split);
57         STACK_INIT(verts_pair);
58
59         l_last = NULL;
60         BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
61                 if (BMO_elem_flag_test(bm, l->v, VERT_INPUT)) {
62                         if (!l_last) {
63                                 l_last = l;
64                                 continue;
65                         }
66
67                         if (l_last != l->prev && l_last != l->next) {
68                                 BMLoop **l_pair = STACK_PUSH_RET(loops_split);
69                                 l_pair[0] = l_last;
70                                 l_pair[1] = l;
71                         }
72                         l_last = l;
73                 }
74         }
75
76         if (STACK_SIZE(loops_split) == 0) {
77                 return 0;
78         }
79
80         if (STACK_SIZE(loops_split) > 1) {
81                 BMLoop **l_pair = STACK_PUSH_RET(loops_split);
82                 l_pair[0] = loops_split[STACK_SIZE(loops_split) - 2][1];
83                 l_pair[1] = loops_split[0][0];
84         }
85
86         BM_face_legal_splits(f, loops_split, STACK_SIZE(loops_split));
87
88         for (i = 0; i < STACK_SIZE(loops_split); i++) {
89                 BMVert **v_pair;
90                 if (loops_split[i][0] == NULL) {
91                         continue;
92                 }
93
94                 v_pair = STACK_PUSH_RET(verts_pair);
95                 v_pair[0] = loops_split[i][0]->v;
96                 v_pair[1] = loops_split[i][1]->v;
97         }
98
99         for (i = 0; i < STACK_SIZE(verts_pair); i++) {
100                 f_new = BM_face_split(bm, f, verts_pair[i][0], verts_pair[i][1], &l_new, NULL, false);
101                 f = f_new;
102
103                 if (!l_new || !f_new) {
104                         return -1;
105                 }
106                 // BMO_elem_flag_enable(bm, f_new, FACE_NEW);
107                 BMO_elem_flag_enable(bm, l_new->e, EDGE_OUT);
108         }
109
110         return 1;
111 }
112
113
114 void bmo_connect_verts_exec(BMesh *bm, BMOperator *op)
115 {
116         BMOIter siter;
117         BMIter iter;
118         BMVert *v;
119         BMFace *f;
120         BMFace **faces = MEM_mallocN(sizeof(BMFace *) * bm->totface, __func__);
121         STACK_DECLARE(faces);
122
123         STACK_INIT(faces);
124
125         /* add all faces connected to verts */
126         BMO_ITER (v, &siter, op->slots_in, "verts", BM_VERT) {
127                 BMO_elem_flag_enable(bm, v, VERT_INPUT);
128                 BM_ITER_ELEM (f, &iter, v, BM_FACES_OF_VERT) {
129                         if (!BMO_elem_flag_test(bm, f, FACE_TAG)) {
130                                 BMO_elem_flag_enable(bm, f, FACE_TAG);
131                                 if (f->len > 3) {
132                                         STACK_PUSH(faces, f);
133                                 }
134                         }
135                 }
136         }
137
138         /* connect faces */
139         while ((f = STACK_POP(faces))) {
140                 if (bm_face_connect_verts(bm, f) == -1) {
141                         BMO_error_raise(bm, op, BMERR_CONNECTVERT_FAILED, NULL);
142                 }
143         }
144
145         MEM_freeN(faces);
146
147         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "edges.out", BM_EDGE, EDGE_OUT);
148 }