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