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