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