b3a42ba533efafe68f036f799c692f853f9b0f71
[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_utildefines.h"
33 #include "BLI_alloca.h"
34 #include "BLI_linklist_stack.h"
35
36 #include "bmesh.h"
37
38 #include "intern/bmesh_operators_private.h" /* own include */
39
40 #define VERT_INPUT      1
41 #define EDGE_OUT        1
42 #define FACE_TAG        2
43
44 static int bm_face_connect_verts(BMesh *bm, BMFace *f)
45 {
46         BMLoop *(*loops_split)[2] = BLI_array_alloca(loops_split, f->len);
47         STACK_DECLARE(loops_split);
48         BMVert *(*verts_pair)[2] = BLI_array_alloca(verts_pair, f->len);
49         STACK_DECLARE(verts_pair);
50
51         BMIter liter;
52         BMFace *f_new;
53         BMLoop *l, *l_new;
54         BMLoop *l_last;
55         unsigned int i;
56
57         STACK_INIT(loops_split);
58         STACK_INIT(verts_pair);
59
60         l_last = NULL;
61         BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
62                 if (BMO_elem_flag_test(bm, l->v, VERT_INPUT)) {
63                         if (!l_last) {
64                                 l_last = l;
65                                 continue;
66                         }
67
68                         if (l_last != l->prev && l_last != l->next) {
69                                 BMLoop **l_pair = STACK_PUSH_RET(loops_split);
70                                 l_pair[0] = l_last;
71                                 l_pair[1] = l;
72                         }
73                         l_last = l;
74                 }
75         }
76
77         if (STACK_SIZE(loops_split) == 0) {
78                 return 0;
79         }
80
81         if (STACK_SIZE(loops_split) > 1) {
82                 BMLoop **l_pair = STACK_PUSH_RET(loops_split);
83                 l_pair[0] = loops_split[STACK_SIZE(loops_split) - 2][1];
84                 l_pair[1] = loops_split[0][0];
85         }
86
87         BM_face_legal_splits(f, loops_split, STACK_SIZE(loops_split));
88
89         for (i = 0; i < STACK_SIZE(loops_split); i++) {
90                 BMVert **v_pair;
91                 if (loops_split[i][0] == NULL) {
92                         continue;
93                 }
94
95                 v_pair = STACK_PUSH_RET(verts_pair);
96                 v_pair[0] = loops_split[i][0]->v;
97                 v_pair[1] = loops_split[i][1]->v;
98         }
99
100         for (i = 0; i < STACK_SIZE(verts_pair); i++) {
101                 f_new = BM_face_split(bm, f, verts_pair[i][0], verts_pair[i][1], &l_new, NULL, false);
102                 f = f_new;
103
104                 if (!l_new || !f_new) {
105                         return -1;
106                 }
107                 // BMO_elem_flag_enable(bm, f_new, FACE_NEW);
108                 BMO_elem_flag_enable(bm, l_new->e, EDGE_OUT);
109         }
110
111         return 1;
112 }
113
114
115 void bmo_connect_verts_exec(BMesh *bm, BMOperator *op)
116 {
117         BMOIter siter;
118         BMIter iter;
119         BMVert *v;
120         BMFace *f;
121         BLI_LINKSTACK_DECLARE(faces, BMFace *);
122
123         BLI_LINKSTACK_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                                         BLI_LINKSTACK_PUSH(faces, f);
133                                 }
134                         }
135                 }
136         }
137
138         /* connect faces */
139         while ((f = BLI_LINKSTACK_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         BLI_LINKSTACK_FREE(faces);
146
147         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "edges.out", BM_EDGE, EDGE_OUT);
148 }