style cleanup: bmesh - have continue on new line, also avoid unneeded loop in bmo_ext...
[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 #include "MEM_guardedalloc.h"
24
25
26 #include "bmesh.h"
27
28 #include "BLI_math.h"
29 #include "BLI_array.h"
30
31 #define VERT_INPUT      1
32 #define EDGE_OUT        1
33 #define FACE_NEW        2
34 #define EDGE_MARK       4
35 #define EDGE_DONE       8
36
37 void bmo_connectverts_exec(BMesh *bm, BMOperator *op)
38 {
39         BMIter iter, liter;
40         BMFace *f, *nf;
41         BMLoop **loops = NULL, *lastl = NULL;
42         BLI_array_declare(loops);
43         BMLoop *l, *nl;
44         BMVert **verts = NULL;
45         BLI_array_declare(verts);
46         int i;
47         
48         BMO_slot_buffer_flag_enable(bm, op, "verts", VERT_INPUT, BM_VERT);
49
50         for (f = BM_iter_new(&iter, bm, BM_FACES_OF_MESH, NULL); f; f = BM_iter_step(&iter)) {
51                 BLI_array_empty(loops);
52                 BLI_array_empty(verts);
53                 
54                 if (BMO_elem_flag_test(bm, f, FACE_NEW)) {
55                         continue;
56                 }
57
58                 l = BM_iter_new(&liter, bm, BM_LOOPS_OF_FACE, f);
59                 lastl = NULL;
60                 for ( ; l; l = BM_iter_step(&liter)) {
61                         if (BMO_elem_flag_test(bm, l->v, VERT_INPUT)) {
62                                 if (!lastl) {
63                                         lastl = l;
64                                         continue;
65                                 }
66
67                                 if (lastl != l->prev && lastl != l->next) {
68                                         BLI_array_growone(loops);
69                                         loops[BLI_array_count(loops) - 1] = lastl;
70
71                                         BLI_array_growone(loops);
72                                         loops[BLI_array_count(loops) - 1] = l;
73
74                                 }
75                                 lastl = l;
76                         }
77                 }
78
79                 if (BLI_array_count(loops) == 0) {
80                         continue;
81                 }
82                 
83                 if (BLI_array_count(loops) > 2) {
84                         BLI_array_growone(loops);
85                         loops[BLI_array_count(loops) - 1] = loops[BLI_array_count(loops) - 2];
86
87                         BLI_array_growone(loops);
88                         loops[BLI_array_count(loops) - 1] = loops[0];
89                 }
90
91                 BM_face_legal_splits(bm, f, (BMLoop *(*)[2])loops, BLI_array_count(loops) / 2);
92                 
93                 for (i = 0; i < BLI_array_count(loops) / 2; i++) {
94                         if (loops[i * 2] == NULL) {
95                                 continue;
96                         }
97
98                         BLI_array_growone(verts);
99                         verts[BLI_array_count(verts) - 1] = loops[i * 2]->v;
100
101                         BLI_array_growone(verts);
102                         verts[BLI_array_count(verts) - 1] = loops[i * 2 + 1]->v;
103                 }
104
105                 for (i = 0; i < BLI_array_count(verts) / 2; i++) {
106                         nf = BM_face_split(bm, f, verts[i * 2], verts[i * 2 + 1], &nl, NULL);
107                         f = nf;
108                         
109                         if (!nl || !nf) {
110                                 BMO_error_raise(bm, op, BMERR_CONNECTVERT_FAILED, NULL);
111                                 BLI_array_free(loops);
112                                 return;
113                         }
114                         BMO_elem_flag_enable(bm, nf, FACE_NEW);
115                         BMO_elem_flag_enable(bm, nl->e, EDGE_OUT);
116                 }
117         }
118
119         BMO_slot_from_flag(bm, op, "edgeout", EDGE_OUT, BM_EDGE);
120
121         BLI_array_free(loops);
122         BLI_array_free(verts);
123 }
124
125 static BMVert *get_outer_vert(BMesh *bm, BMEdge *e)
126 {
127         BMIter iter;
128         BMEdge *e2;
129         int i;
130
131         i = 0;
132         BM_ITER(e2, &iter, bm, BM_EDGES_OF_VERT, e->v1) {
133                 if (BMO_elem_flag_test(bm, e2, EDGE_MARK)) {
134                         i++;
135                 }
136         }
137
138         return (i == 2) ? e->v2 : e->v1;
139 }
140
141 /* Clamp x to the interval {0..len-1}, with wrap-around */
142 static int clamp_index(const int x, const int len)
143 {
144         return (x < 0) ? (len - (-x % len)) : (x % len);
145 }
146
147 /* There probably is a better way to swap BLI_arrays, or if there
148  * isn't there should be... */
149 #define ARRAY_SWAP(elemtype, arr1, arr2)                                      \
150         {                                                                         \
151                 int i;                                                                \
152                 elemtype *arr_tmp = NULL;                                             \
153                 BLI_array_declare(arr_tmp);                                           \
154                 for (i = 0; i < BLI_array_count(arr1); i++) {                         \
155                         BLI_array_append(arr_tmp, arr1[i]);                               \
156                 }                                                                     \
157                 BLI_array_empty(arr1);                                                \
158                 for (i = 0; i < BLI_array_count(arr2); i++) {                         \
159                         BLI_array_append(arr1, arr2[i]);                                  \
160                 }                                                                     \
161                 BLI_array_empty(arr2);                                                \
162                 for (i = 0; i < BLI_array_count(arr_tmp); i++) {                      \
163                         BLI_array_append(arr2, arr_tmp[i]);                               \
164                 }                                                                     \
165                 BLI_array_free(arr_tmp);                                              \
166         }
167
168 /* get the 2 loops matching 2 verts.
169  * first attempt to get the face corners that use the edge defined by v1 & v2,
170  * if that fails just get any loop thats on the vert (the first one) */
171 static void bm_vert_loop_pair(BMesh *bm, BMVert *v1, BMVert *v2, BMLoop **l1, BMLoop **l2)
172 {
173         BMIter liter;
174         BMLoop *l;
175
176         if ((v1->e && v1->e->l) &&
177             (v2->e && v2->e->l))
178         {
179                 BM_ITER(l, &liter, bm, BM_LOOPS_OF_VERT, v1) {
180                         if (l->prev->v == v2) {
181                                 *l1 = l;
182                                 *l2 = l->prev;
183                                 return;
184                         }
185                         else if (l->next->v == v2) {
186                                 *l1 = l;
187                                 *l2 = l->next;
188                                 return;
189                         }
190                 }
191         }
192
193         /* fallback to _any_ loop */
194         *l1 = BM_iter_at_index(bm, BM_LOOPS_OF_VERT, v1, 0);
195         *l2 = BM_iter_at_index(bm, BM_LOOPS_OF_VERT, v2, 0);
196 }
197
198 void bmo_bridge_loops_exec(BMesh *bm, BMOperator *op)
199 {
200         BMEdge **ee1 = NULL, **ee2 = NULL;
201         BMVert **vv1 = NULL, **vv2 = NULL;
202         BLI_array_declare(ee1);
203         BLI_array_declare(ee2);
204         BLI_array_declare(vv1);
205         BLI_array_declare(vv2);
206         BMOIter siter;
207         BMIter iter;
208         BMEdge *e, *nexte;
209         int c = 0, cl1 = 0, cl2 = 0;
210
211         BMO_slot_buffer_flag_enable(bm, op, "edges", EDGE_MARK, BM_EDGE);
212
213         BMO_ITER(e, &siter, bm, op, "edges", BM_EDGE) {
214                 if (!BMO_elem_flag_test(bm, e, EDGE_DONE)) {
215                         BMVert *v, *ov;
216                         /* BMEdge *e2, *e3, *oe = e; */ /* UNUSED */
217                         BMEdge *e2, *e3;
218                         
219                         if (c > 2) {
220                                 BMO_error_raise(bm, op, BMERR_INVALID_SELECTION, "Select only two edge loops");
221                                 goto cleanup;
222                         }
223                         
224                         e2 = e;
225                         v = e->v1;
226                         do {
227                                 v = BM_edge_other_vert(e2, v);
228                                 nexte = NULL;
229                                 BM_ITER(e3, &iter, bm, BM_EDGES_OF_VERT, v) {
230                                         if (e3 != e2 && BMO_elem_flag_test(bm, e3, EDGE_MARK)) {
231                                                 if (nexte == NULL) {
232                                                         nexte = e3;
233                                                 }
234                                                 else {
235                                                         /* edges do not form a loop: there is a disk
236                                                          * with more than two marked edges. */
237                                                         BMO_error_raise(bm, op, BMERR_INVALID_SELECTION,
238                                                                         "Selection must only contain edges from two edge loops");
239                                                         goto cleanup;
240                                                 }
241                                         }
242                                 }
243                                 
244                                 if (nexte)
245                                         e2 = nexte;
246                         } while (nexte && e2 != e);
247                         
248                         if (!e2)
249                                 e2 = e;
250
251                         e = e2;
252                         ov = v;
253                         do {
254                                 if (c == 0) {
255                                         BLI_array_append(ee1, e2);
256                                         BLI_array_append(vv1, v);
257                                 }
258                                 else {
259                                         BLI_array_append(ee2, e2);
260                                         BLI_array_append(vv2, v);
261                                 }
262                                 
263                                 BMO_elem_flag_enable(bm, e2, EDGE_DONE);
264                                 
265                                 v = BM_edge_other_vert(e2, v);
266                                 BM_ITER(e3, &iter, bm, BM_EDGES_OF_VERT, v) {
267                                         if (e3 != e2 && BMO_elem_flag_test(bm, e3, EDGE_MARK) && !BMO_elem_flag_test(bm, e3, EDGE_DONE)) {
268                                                 break;
269                                         }
270                                 }
271                                 if (e3)
272                                         e2 = e3;
273                         } while (e3 && e2 != e);
274                         
275                         if (v && !e3) {
276                                 if (c == 0) {
277                                         if (BLI_array_count(vv1) && v == vv1[BLI_array_count(vv1) - 1]) {
278                                                 printf("%s: internal state waning *TODO DESCRIPTION!*\n", __func__);
279                                         }
280                                         BLI_array_append(vv1, v);
281                                 }
282                                 else {
283                                         BLI_array_append(vv2, v);
284                                 }
285                         }
286                         
287                         /* test for connected loops, and set cl1 or cl2 if so */
288                         if (v == ov) {
289                                 if (c == 0) {
290                                         cl1 = 1;
291                                 }
292                                 else {
293                                         cl2 = 1;
294                                 }
295                         }
296                         
297                         c++;
298                 }
299         }
300
301         if (ee1 && ee2) {
302                 int i, j;
303                 BMVert *v1, *v2, *v3, *v4;
304                 int starti = 0, dir1 = 1, wdir = 0, lenv1, lenv2;
305
306                 /* Simplify code below by avoiding the (!cl1 && cl2) case */
307                 if (!cl1 && cl2) {
308                         SWAP(int, cl1, cl2);
309                         ARRAY_SWAP(BMVert *, vv1, vv2);
310                         ARRAY_SWAP(BMEdge *, ee1, ee2);
311                 }
312
313                 lenv1 = lenv2 = BLI_array_count(vv1);
314
315                 /* Below code assumes vv1/vv2 each have at least two verts. should always be
316                  * a safe assumption, since ee1/ee2 are non-empty and an edge has two verts. */
317                 BLI_assert((lenv1 > 1) && (lenv2 > 1));
318
319                 /* BMESH_TODO: Would be nice to handle cases where the edge loops
320                  * have different edge counts by generating triangles & quads for
321                  * the bridge instead of quads only. */
322                 if (BLI_array_count(ee1) != BLI_array_count(ee2)) {
323                         BMO_error_raise(bm, op, BMERR_INVALID_SELECTION,
324                                         "Selected loops must have equal edge counts");
325                         goto cleanup;
326                 }
327
328                 j = 0;
329                 if (vv1[0] == vv1[lenv1 - 1]) {
330                         lenv1--;
331                 }
332                 if (vv2[0] == vv2[lenv2 - 1]) {
333                         lenv2--;
334                 }
335
336                 /* Find starting point and winding direction for two unclosed loops */
337                 if (!cl1 && !cl2) {
338                         /* First point of loop 1 */
339                         v1 = get_outer_vert(bm, ee1[0]);
340                         /* Last point of loop 1 */
341                         v2 = get_outer_vert(bm, ee1[clamp_index(-1, BLI_array_count(ee1))]);
342                         /* First point of loop 2 */
343                         v3 = get_outer_vert(bm, ee2[0]);
344                         /* Last point of loop 2 */
345                         v4 = get_outer_vert(bm, ee2[clamp_index(-1, BLI_array_count(ee2))]);
346
347                         /* If v1 is a better match for v4 than v3, AND v2 is a better match
348                          * for v3 than v4, the loops are in opposite directions, so reverse
349                          * the order of reads from vv1. We can avoid sqrt for comparison */
350                         if (len_squared_v3v3(v1->co, v3->co) > len_squared_v3v3(v1->co, v4->co) &&
351                             len_squared_v3v3(v2->co, v4->co) > len_squared_v3v3(v2->co, v3->co))
352                         {
353                                 dir1 = -1;
354                                 starti = clamp_index(-1, lenv1);
355                         }
356                 }
357
358                 /* Find the shortest distance from a vert in vv1 to vv2[0]. Use that
359                  * vertex in vv1 as a starting point in the first loop, while starting
360                  * from vv2[0] in the second loop. This is a simplistic attempt to get
361                  * a better edge-to-edge match between the two loops. */
362                 if (cl1) {
363                         int previ, nexti;
364                         float min = 1e32;
365
366                         /* BMESH_TODO: Would be nice to do a more thorough analysis of all
367                          * the vertices in both loops to find a more accurate match for the
368                          * starting point and winding direction of the bridge generation. */
369                         
370                         for (i = 0; i < BLI_array_count(vv1); i++) {
371                                 if (len_v3v3(vv1[i]->co, vv2[0]->co) < min) {
372                                         min = len_v3v3(vv1[i]->co, vv2[0]->co);
373                                         starti = i;
374                                 }
375                         }
376
377                         /* Reverse iteration order for the first loop if the distance of
378                          * the (starti - 1) vert from vv1 is a better match for vv2[1] than
379                          * the (starti + 1) vert.
380                          *
381                          * This is not always going to be right, but it will work better in
382                          * the average case.
383                          */
384                         previ = clamp_index(starti - 1, lenv1);
385                         nexti = clamp_index(starti + 1, lenv1);
386
387                         /* avoid sqrt for comparison */
388                         if (len_squared_v3v3(vv1[nexti]->co, vv2[1]->co) > len_squared_v3v3(vv1[previ]->co, vv2[1]->co)) {
389                                 /* reverse direction for reading vv1 (1 is forward, -1 is backward) */
390                                 dir1 = -1;
391                         }
392                 }
393
394                 /* Vert rough attempt to determine proper winding for the bridge quads:
395                  * just uses the first loop it finds for any of the edges of ee2 or ee1 */
396                 if (wdir == 0) {
397                         for (i = 0; i < BLI_array_count(ee2); i++) {
398                                 if (ee2[i]->l) {
399                                         wdir = (ee2[i]->l->v == vv2[i]) ? (-1) : (1);
400                                         break;
401                                 }
402                         }
403                 }
404                 if (wdir == 0) {
405                         for (i = 0; i < BLI_array_count(ee1); i++) {
406                                 j = clamp_index((i * dir1) + starti, BLI_array_count(ee1));
407                                 if (ee1[j]->l && ee2[j]->l) {
408                                         wdir = (ee2[j]->l->v == vv2[j]) ? (1) : (-1);
409                                         break;
410                                 }
411                         }
412                 }
413                 
414                 /* Generate the bridge quads */
415                 for (i = 0; i < BLI_array_count(ee1) && i < BLI_array_count(ee2); i++) {
416                         BMFace *f;
417
418                         BMLoop *l_1 = NULL;
419                         BMLoop *l_2 = NULL;
420                         BMLoop *l_1_next = NULL;
421                         BMLoop *l_2_next = NULL;
422                         BMLoop *l_iter;
423                         BMFace *f_example;
424
425                         int i1, i1next, i2, i2next;
426
427                         i1 = clamp_index(i * dir1 + starti, lenv1);
428                         i1next = clamp_index((i + 1) * dir1 + starti, lenv1);
429                         i2 = i;
430                         i2next = clamp_index(i + 1, lenv2);
431
432                         if (vv1[i1] ==  vv1[i1next]) {
433                                 continue;
434                         }
435
436                         if (wdir < 0) {
437                                 SWAP(int, i1, i1next);
438                                 SWAP(int, i2, i2next);
439                         }
440
441                         /* get loop data - before making the face */
442                         bm_vert_loop_pair(bm, vv1[i1], vv2[i2], &l_1, &l_2);
443                         bm_vert_loop_pair(bm, vv1[i1next], vv2[i2next], &l_1_next, &l_2_next);
444                         /* copy if loop data if its is missing on one ring */
445                         if (l_1 && l_1_next == NULL) l_1_next = l_1;
446                         if (l_1_next && l_1 == NULL) l_1 = l_1_next;
447                         if (l_2 && l_2_next == NULL) l_2_next = l_2;
448                         if (l_2_next && l_2 == NULL) l_2 = l_2_next;
449                         f_example = l_1 ? l_1->f : (l_2 ? l_2->f : NULL);
450
451                         f = BM_face_create_quad_tri(bm,
452                                                     vv1[i1],
453                                                     vv2[i2],
454                                                     vv2[i2next],
455                                                     vv1[i1next],
456                                                     f_example, TRUE);
457                         if (!f || f->len != 4) {
458                                 fprintf(stderr, "%s: in bridge! (bmesh internal error)\n", __func__);
459                         }
460                         else {
461                                 l_iter = BM_FACE_FIRST_LOOP(f);
462
463                                 if (l_1)      BM_elem_attrs_copy(bm, bm, l_1,      l_iter); l_iter = l_iter->next;
464                                 if (l_2)      BM_elem_attrs_copy(bm, bm, l_2,      l_iter); l_iter = l_iter->next;
465                                 if (l_2_next) BM_elem_attrs_copy(bm, bm, l_2_next, l_iter); l_iter = l_iter->next;
466                                 if (l_1_next) BM_elem_attrs_copy(bm, bm, l_1_next, l_iter);
467                         }
468                 }
469         }
470
471 cleanup:
472         BLI_array_free(ee1);
473         BLI_array_free(ee2);
474         BLI_array_free(vv1);
475         BLI_array_free(vv2);
476 }