BLI_stackdefines
[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 "BLI_utildefines.h"
30 #include "BLI_stackdefines.h"
31 #include "BLI_alloca.h"
32 #include "BLI_linklist_stack.h"
33
34 #include "bmesh.h"
35
36 #include "intern/bmesh_operators_private.h" /* own include */
37
38 #define VERT_INPUT      1
39 #define EDGE_OUT        1
40 #define FACE_TAG        2
41
42 static int bm_face_connect_verts(BMesh *bm, BMFace *f, const bool check_degenerate)
43 {
44         BMLoop *(*loops_split)[2] = BLI_array_alloca(loops_split, f->len);
45         STACK_DECLARE(loops_split);
46         BMVert *(*verts_pair)[2] = BLI_array_alloca(verts_pair, f->len);
47         STACK_DECLARE(verts_pair);
48
49         BMIter liter;
50         BMFace *f_new;
51         BMLoop *l;
52         BMLoop *l_last;
53         unsigned int i;
54
55         STACK_INIT(loops_split, f->len);
56         STACK_INIT(verts_pair, f->len);
57
58         l_last = NULL;
59         BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
60                 if (BMO_elem_flag_test(bm, l->v, VERT_INPUT)) {
61                         if (!l_last) {
62                                 l_last = l;
63                                 continue;
64                         }
65
66                         if (!BM_loop_is_adjacent(l_last, l)) {
67                                 BMEdge *e;
68                                 e = BM_edge_exists(l_last->v, l->v);
69                                 if (e == NULL || !BMO_elem_flag_test(bm, e, EDGE_OUT)) {
70                                         BMLoop **l_pair = STACK_PUSH_RET(loops_split);
71                                         l_pair[0] = l_last;
72                                         l_pair[1] = l;
73                                 }
74                         }
75                         l_last = l;
76                 }
77         }
78
79         if (STACK_SIZE(loops_split) == 0) {
80                 return 0;
81         }
82
83         if (STACK_SIZE(loops_split) > 1) {
84                 BMLoop **l_pair = STACK_PUSH_RET(loops_split);
85                 l_pair[0] = loops_split[STACK_SIZE(loops_split) - 2][1];
86                 l_pair[1] = loops_split[0][0];
87         }
88
89         if (check_degenerate) {
90                 BM_face_splits_check_legal(bm, f, loops_split, STACK_SIZE(loops_split));
91         }
92         else {
93                 BM_face_splits_check_optimal(f, loops_split, STACK_SIZE(loops_split));
94         }
95
96         for (i = 0; i < STACK_SIZE(loops_split); i++) {
97                 BMVert **v_pair;
98                 if (loops_split[i][0] == NULL) {
99                         continue;
100                 }
101
102                 v_pair = STACK_PUSH_RET(verts_pair);
103                 v_pair[0] = loops_split[i][0]->v;
104                 v_pair[1] = loops_split[i][1]->v;
105         }
106
107         for (i = 0; i < STACK_SIZE(verts_pair); i++) {
108                 BMLoop *l_new;
109                 BMLoop *l_a, *l_b;
110
111                 if ((l_a = BM_face_vert_share_loop(f, verts_pair[i][0])) &&
112                     (l_b = BM_face_vert_share_loop(f, verts_pair[i][1])))
113                 {
114                         f_new = BM_face_split(bm, f, l_a, l_b, &l_new, NULL, false);
115                 }
116                 else {
117                         f_new = NULL;
118                         l_new = NULL;
119                 }
120
121                 f = f_new;
122
123                 if (!l_new || !f_new) {
124                         return -1;
125                 }
126                 // BMO_elem_flag_enable(bm, f_new, FACE_NEW);
127                 BMO_elem_flag_enable(bm, l_new->e, EDGE_OUT);
128         }
129
130         return 1;
131 }
132
133
134 void bmo_connect_verts_exec(BMesh *bm, BMOperator *op)
135 {
136         BMOIter siter;
137         BMIter iter;
138         BMVert *v;
139         BMFace *f;
140         const bool check_degenerate = BMO_slot_bool_get(op->slots_in,  "check_degenerate");
141         BLI_LINKSTACK_DECLARE(faces, BMFace *);
142
143         BLI_LINKSTACK_INIT(faces);
144
145         /* add all faces connected to verts */
146         BMO_ITER (v, &siter, op->slots_in, "verts", BM_VERT) {
147                 BMO_elem_flag_enable(bm, v, VERT_INPUT);
148                 BM_ITER_ELEM (f, &iter, v, BM_FACES_OF_VERT) {
149                         if (!BMO_elem_flag_test(bm, f, FACE_TAG)) {
150                                 BMO_elem_flag_enable(bm, f, FACE_TAG);
151                                 if (f->len > 3) {
152                                         BLI_LINKSTACK_PUSH(faces, f);
153                                 }
154                         }
155                 }
156         }
157
158         /* connect faces */
159         while ((f = BLI_LINKSTACK_POP(faces))) {
160                 if (bm_face_connect_verts(bm, f, check_degenerate) == -1) {
161                         BMO_error_raise(bm, op, BMERR_CONNECTVERT_FAILED, NULL);
162                 }
163         }
164
165         BLI_LINKSTACK_FREE(faces);
166
167         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "edges.out", BM_EDGE, EDGE_OUT);
168 }