- add generic edge-loop utility functions for bmesh.
[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) and bridge tool.
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_NEW        2
42
43 void bmo_connect_verts_exec(BMesh *bm, BMOperator *op)
44 {
45         BMIter iter, liter;
46         BMFace *f, *f_new;
47         BMLoop *(*loops_split)[2] = NULL;
48         BLI_array_declare(loops_split);
49         BMLoop *l, *l_new;
50         BMVert *(*verts_pair)[2] = NULL;
51         BLI_array_declare(verts_pair);
52         int i;
53         
54         BMO_slot_buffer_flag_enable(bm, op->slots_in, "verts", BM_VERT, VERT_INPUT);
55
56         /* BMESH_TODO, loop over vert faces:
57          * faster then looping over all faces, then searching each for flagged verts*/
58         BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
59                 BMLoop *l_last;
60                 BLI_array_empty(loops_split);
61                 BLI_array_empty(verts_pair);
62                 
63                 if (BMO_elem_flag_test(bm, f, FACE_NEW)) {
64                         continue;
65                 }
66
67                 l_last = NULL;
68                 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
69                         if (BMO_elem_flag_test(bm, l->v, VERT_INPUT)) {
70                                 if (!l_last) {
71                                         l_last = l;
72                                         continue;
73                                 }
74
75                                 if (l_last != l->prev && l_last != l->next) {
76                                         BLI_array_grow_one(loops_split);
77                                         loops_split[BLI_array_count(loops_split) - 1][0] = l_last;
78                                         loops_split[BLI_array_count(loops_split) - 1][1] = l;
79
80                                 }
81                                 l_last = l;
82                         }
83                 }
84
85                 if (BLI_array_count(loops_split) == 0) {
86                         continue;
87                 }
88                 
89                 if (BLI_array_count(loops_split) > 1) {
90                         BLI_array_grow_one(loops_split);
91                         loops_split[BLI_array_count(loops_split) - 1][0] = loops_split[BLI_array_count(loops_split) - 2][1];
92                         loops_split[BLI_array_count(loops_split) - 1][1] = loops_split[0][0];
93                 }
94
95                 BM_face_legal_splits(bm, f, loops_split, BLI_array_count(loops_split));
96                 
97                 for (i = 0; i < BLI_array_count(loops_split); i++) {
98                         if (loops_split[i][0] == NULL) {
99                                 continue;
100                         }
101
102                         BLI_array_grow_one(verts_pair);
103                         verts_pair[BLI_array_count(verts_pair) - 1][0] = loops_split[i][0]->v;
104                         verts_pair[BLI_array_count(verts_pair) - 1][1] = loops_split[i][1]->v;
105                 }
106
107                 for (i = 0; i < BLI_array_count(verts_pair); i++) {
108                         f_new = BM_face_split(bm, f, verts_pair[i][0], verts_pair[i][1], &l_new, NULL, false);
109                         f = f_new;
110                         
111                         if (!l_new || !f_new) {
112                                 BMO_error_raise(bm, op, BMERR_CONNECTVERT_FAILED, NULL);
113                                 BLI_array_free(loops_split);
114                                 return;
115                         }
116                         BMO_elem_flag_enable(bm, f_new, FACE_NEW);
117                         BMO_elem_flag_enable(bm, l_new->e, EDGE_OUT);
118                 }
119         }
120
121         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "edges.out", BM_EDGE, EDGE_OUT);
122
123         BLI_array_free(loops_split);
124         BLI_array_free(verts_pair);
125 }