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