simplify inset loop-customdata-copying, no real benefit in using adjacent faces as...
[blender.git] / source / blender / bmesh / operators / bmo_inset.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): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/bmesh/operators/bmo_inset.c
24  *  \ingroup bmesh
25  *
26  * Inset face regions.
27  * Inset individual faces.
28  *
29  */
30
31 #include "MEM_guardedalloc.h"
32
33 #include "BLI_math.h"
34 #include "BLI_array.h"
35
36 #include "bmesh.h"
37
38 #include "intern/bmesh_operators_private.h" /* own include */
39
40 #define ELE_NEW         1
41
42
43
44 /* -------------------------------------------------------------------- */
45 /* Inset Individual */
46
47
48 /* Holds Per-Face Inset Edge Data */
49 typedef struct EdgeInsetInfo {
50         float no[3];
51         BMEdge *e_old;
52         BMEdge *e_new;
53 } EdgeInsetInfo;
54
55 /**
56  * Individual Face Inset.
57  * Find all tagged faces (f), duplicate edges around faces, inset verts of
58  * created edges, create new faces between old and new edges, fill face
59  * between connected new edges, kill old face (f).
60  */
61 void bmo_inset_individual_exec(BMesh *bm, BMOperator *op)
62 {
63         BMEdge **f_edges = NULL;
64         BMVert **f_verts = NULL;
65         BMFace *f;
66
67         BMOIter oiter;
68         EdgeInsetInfo *eiinfo_arr = NULL;
69
70         BLI_array_declare(eiinfo_arr);
71         BLI_array_declare(f_edges);
72         BLI_array_declare(f_verts);
73
74         const float thickness = BMO_slot_float_get(op->slots_in, "thickness");
75         const float depth = BMO_slot_float_get(op->slots_in, "depth");
76         const bool use_even_offset = BMO_slot_bool_get(op->slots_in, "use_even_offset");
77
78         /* Only tag faces in slot */
79         BM_mesh_elem_hflag_disable_all(bm, BM_FACE, BM_ELEM_TAG, false);
80
81         BMO_slot_buffer_hflag_enable(bm, op->slots_in, "faces", BM_FACE, BM_ELEM_TAG, false);
82
83         BMO_ITER(f, &oiter, op->slots_in, "faces", BM_FACE) {
84                 BMLoop *l_iter, *l_first;
85                 BMLoop *l_iter_inner = NULL;
86                 int i;
87
88                 BLI_array_empty(f_verts);
89                 BLI_array_empty(f_edges);
90                 BLI_array_empty(eiinfo_arr);
91                 BLI_array_grow_items(f_verts, f->len);
92                 BLI_array_grow_items(f_edges, f->len);
93                 BLI_array_grow_items(eiinfo_arr, f->len);
94
95                 /* create verts */
96                 i = 0;
97                 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
98                 do {
99                         f_verts[i] = BM_vert_create(bm, l_iter->v->co, l_iter->v, 0);
100                         i++;
101                 } while ((l_iter = l_iter->next) != l_first);
102
103                 /* make edges */
104                 i = 0;
105                 l_iter = l_first;
106                 do {
107                         f_edges[i] = BM_edge_create(bm, f_verts[i], f_verts[(i + 1) % f->len], l_iter->e, 0);
108
109                         eiinfo_arr[i].e_new = f_edges[i];
110                         eiinfo_arr[i].e_old = l_iter->e;
111                         BM_edge_calc_face_tangent(l_iter->e, l_iter, eiinfo_arr[i].no);
112
113                         /* Tagging (old elements) required when iterating over edges
114                          * connected to verts for translation vector calculation */
115                         BM_elem_flag_enable(l_iter->e, BM_ELEM_TAG);
116                         BM_elem_index_set(l_iter->e, i);  /* set_dirty! */
117                         i++;
118                 } while ((l_iter = l_iter->next) != l_first);
119                 /* done with edges */
120
121                 bm->elem_index_dirty |= BM_EDGE;
122
123                 /* Calculate translation vector for new  */
124                 l_iter = l_first;
125                 do {
126                         EdgeInsetInfo *ei_prev = &eiinfo_arr[BM_elem_index_get(l_iter->prev->e)];
127                         EdgeInsetInfo *ei_next = &eiinfo_arr[BM_elem_index_get(l_iter->e)];
128                         float tvec[3];
129                         float v_new_co[3];
130                         int index = 0;
131
132                         add_v3_v3v3(tvec, ei_prev->no, ei_next->no);
133                         normalize_v3(tvec);
134
135                         /* l->e is traversed in order */
136                         index = BM_elem_index_get(l_iter->e);
137
138                         copy_v3_v3(v_new_co, eiinfo_arr[index].e_new->v1->co);
139
140                         if (use_even_offset) {
141                                 mul_v3_fl(tvec, shell_angle_to_dist(angle_normalized_v3v3(ei_prev->no,  ei_next->no) / 2.0f));
142                         }
143
144                         /* Modify vertices and their normals */
145                         madd_v3_v3fl(v_new_co, tvec, thickness);
146
147                         /* Set normal, add depth and write new vertex position*/
148                         copy_v3_v3(eiinfo_arr[index].e_new->v1->no, f->no);
149
150                         madd_v3_v3fl(v_new_co, f->no, depth);
151
152                         copy_v3_v3(eiinfo_arr[index].e_new->v1->co, v_new_co);
153                 } while ((l_iter = l_iter->next) != l_first);
154
155                 {
156                         BMFace *f_new_inner;
157                         /* Create New Inset Faces */
158                         f_new_inner = BM_face_create(bm, f_verts, f_edges, f->len, 0);
159                         BLI_assert(f_new_inner != NULL);  /* no reason it should fail */
160
161                         /* Copy Face Data */
162                         BM_elem_attrs_copy(bm, bm, f, f_new_inner);
163                         // Don't tag, gives more useful inner/outer select option
164                         // BMO_elem_flag_enable(bm, f_new_inner, ELE_NEW);
165
166                         l_iter_inner = BM_FACE_FIRST_LOOP(f_new_inner);
167                 }
168
169                 l_iter = l_first;
170                 do {
171                         BMFace *f_new_outer;
172
173                         BMLoop *l_a;
174                         BMLoop *l_b;
175
176                         BM_elem_attrs_copy(bm, bm, l_iter, l_iter_inner);
177
178                         f_new_outer = BM_face_create_quad_tri(bm,
179                                                               l_iter->v,
180                                                               l_iter->next->v,
181                                                               l_iter_inner->next->v,
182                                                               l_iter_inner->v,
183                                                               f, false);
184
185                         BLI_assert(f_new_outer != NULL);  /* no reason it should fail */
186
187                         BM_elem_attrs_copy(bm, bm, f, f_new_outer);
188                         BMO_elem_flag_enable(bm, f_new_outer, ELE_NEW);
189                         BM_elem_flag_enable(f_new_outer, BM_ELEM_TAG);
190
191                         /* Copy Loop Data */
192                         l_a = BM_FACE_FIRST_LOOP(f_new_outer);
193                         l_b = l_a->next;
194
195                         /* first pair */
196                         BM_elem_attrs_copy(bm, bm, l_iter, l_a);
197                         BM_elem_attrs_copy(bm, bm,  l_iter->next, l_b);
198
199
200                         /* Move to the last two loops in new face */
201                         l_a = l_b->next;
202                         l_b = l_a->next;
203
204                         /* This loop should always have >1 radials
205                          * (associated edge connects new and old face) */
206                         BM_elem_attrs_copy(bm, bm, l_iter, l_b);
207                         BM_elem_attrs_copy(bm, bm, l_iter->next, l_a);
208
209                 } while ((l_iter_inner = l_iter_inner->next),
210                          (l_iter = l_iter->next) != l_first);
211
212                 BM_face_kill(bm, f);
213         }
214
215         /* we could flag new edges/verts too, is it useful? */
216         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "faces.out", BM_FACE, ELE_NEW);
217
218         BLI_array_free(f_verts);
219         BLI_array_free(f_edges);
220         BLI_array_free(eiinfo_arr);
221 }
222
223
224
225 /* -------------------------------------------------------------------- */
226 /* Inset Region */
227
228 typedef struct SplitEdgeInfo {
229         float   no[3];
230         float   length;
231         BMEdge *e_old;
232         BMEdge *e_new;
233         BMLoop *l;
234 } SplitEdgeInfo;
235
236 /**
237  * return the tag loop where there is...
238  * - only 1 tagged face attached to this edge.
239  * - 1 or more untagged faces.
240  *
241  * \note this function looks to be expensive
242  * but in most cases it will only do 2 iterations.
243  */
244 static BMLoop *bm_edge_is_mixed_face_tag(BMLoop *l)
245 {
246         if (LIKELY(l != NULL)) {
247                 int tot_tag = 0;
248                 int tot_untag = 0;
249                 BMLoop *l_iter;
250                 BMLoop *l_tag = NULL;
251                 l_iter = l;
252                 do {
253                         if (BM_elem_flag_test(l_iter->f, BM_ELEM_TAG)) {
254                                 /* more then one tagged face - bail out early! */
255                                 if (tot_tag == 1) {
256                                         return NULL;
257                                 }
258                                 l_tag = l_iter;
259                                 tot_tag++;
260                         }
261                         else {
262                                 tot_untag++;
263                         }
264
265                 } while ((l_iter = l_iter->radial_next) != l);
266
267                 return ((tot_tag == 1) && (tot_untag >= 1)) ? l_tag : NULL;
268         }
269         else {
270                 return NULL;
271         }
272 }
273
274 /**
275  * implementation is as follows...
276  *
277  * - set all faces as tagged/untagged based on selection.
278  * - find all edges that have 1 tagged, 1 untagged face.
279  * - separate these edges and tag vertices, set their index to point to the original edge.
280  * - build faces between old/new edges.
281  * - inset the new edges into their faces.
282  */
283
284 void bmo_inset_region_exec(BMesh *bm, BMOperator *op)
285 {
286         const bool use_outset          = BMO_slot_bool_get(op->slots_in, "use_outset");
287         const bool use_boundary        = BMO_slot_bool_get(op->slots_in, "use_boundary") && (use_outset == false);
288         const bool use_even_offset     = BMO_slot_bool_get(op->slots_in, "use_even_offset");
289         const bool use_even_boundry    = use_even_offset; /* could make own option */
290         const bool use_relative_offset = BMO_slot_bool_get(op->slots_in, "use_relative_offset");
291         const float thickness          = BMO_slot_float_get(op->slots_in, "thickness");
292         const float depth              = BMO_slot_float_get(op->slots_in, "depth");
293
294         int edge_info_len = 0;
295
296         BMIter iter;
297         SplitEdgeInfo *edge_info;
298         SplitEdgeInfo *es;
299
300         BMVert *v;
301         BMEdge *e;
302         BMFace *f;
303         int i, j, k;
304
305         if (use_outset == false) {
306                 BM_mesh_elem_hflag_disable_all(bm, BM_FACE, BM_ELEM_TAG, false);
307                 BMO_slot_buffer_hflag_enable(bm, op->slots_in, "faces", BM_FACE, BM_ELEM_TAG, false);
308         }
309         else {
310                 BM_mesh_elem_hflag_enable_all(bm, BM_FACE, BM_ELEM_TAG, false);
311                 BMO_slot_buffer_hflag_disable(bm, op->slots_in, "faces", BM_FACE, BM_ELEM_TAG, false);
312         }
313
314         /* first count all inset edges we will split */
315         /* fill in array and initialize tagging */
316         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
317                 if (
318                     /* tag if boundary is enabled */
319                     (use_boundary && BM_edge_is_boundary(e) && BM_elem_flag_test(e->l->f, BM_ELEM_TAG)) ||
320
321                     /* tag if edge is an interior edge inbetween a tagged and untagged face */
322                     (bm_edge_is_mixed_face_tag(e->l)))
323                 {
324                         /* tag */
325                         BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
326                         BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
327                         BM_elem_flag_enable(e, BM_ELEM_TAG);
328
329                         BM_elem_index_set(e, edge_info_len); /* set_dirty! */
330                         edge_info_len++;
331                 }
332                 else {
333                         BM_elem_flag_disable(e->v1, BM_ELEM_TAG);
334                         BM_elem_flag_disable(e->v2, BM_ELEM_TAG);
335                         BM_elem_flag_disable(e, BM_ELEM_TAG);
336
337                         BM_elem_index_set(e, -1); /* set_dirty! */
338                 }
339         }
340         bm->elem_index_dirty |= BM_EDGE;
341
342         edge_info = MEM_mallocN(edge_info_len * sizeof(SplitEdgeInfo), __func__);
343
344         /* fill in array and initialize tagging */
345         es = edge_info;
346         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
347                 i = BM_elem_index_get(e);
348                 if (i != -1) {
349                         /* calc edge-split info */
350                         es->length = BM_edge_calc_length(e);
351                         es->e_old = e;
352                         es++;
353                         /* initialize no and e_new after */
354                 }
355         }
356
357         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
358                 if ((es->l = bm_edge_is_mixed_face_tag(es->e_old->l))) {
359                         /* do nothing */
360                 }
361                 else {
362                         es->l = es->e_old->l; /* must be a boundary */
363                 }
364
365
366                 /* run the separate arg */
367                 bmesh_edge_separate(bm, es->e_old, es->l);
368
369                 /* calc edge-split info */
370                 es->e_new = es->l->e;
371                 BM_edge_calc_face_tangent(es->e_new, es->l, es->no);
372
373                 if (es->e_new == es->e_old) { /* happens on boundary edges */
374                         /* take care here, we're creating this double edge which _must_ have its verts replaced later on */
375                         es->e_old = BM_edge_create(bm, es->e_new->v1, es->e_new->v2, es->e_new, 0);
376                 }
377
378                 /* store index back to original in 'edge_info' */
379                 BM_elem_index_set(es->e_new, i);
380                 BM_elem_flag_enable(es->e_new, BM_ELEM_TAG);
381
382                 /* important to tag again here */
383                 BM_elem_flag_enable(es->e_new->v1, BM_ELEM_TAG);
384                 BM_elem_flag_enable(es->e_new->v2, BM_ELEM_TAG);
385         }
386
387
388         /* show edge normals for debugging */
389 #if 0
390         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
391                 float tvec[3];
392                 BMVert *v1, *v2;
393
394                 mid_v3_v3v3(tvec, es->e_new->v1->co, es->e_new->v2->co);
395
396                 v1 = BM_vert_create(bm, tvec, NULL);
397                 v2 = BM_vert_create(bm, tvec, NULL);
398                 madd_v3_v3fl(v2->co, es->no, 0.1f);
399                 BM_edge_create(bm, v1, v2, NULL, 0);
400         }
401 #endif
402
403         /* execute the split and position verts, it would be most obvious to loop over verts
404          * here but don't do this since we will be splitting them off (iterating stuff you modify is bad juju)
405          * instead loop over edges then their verts */
406         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
407                 for (j = 0; j < 2; j++) {
408                         v = (j == 0) ? es->e_new->v1 : es->e_new->v2;
409
410                         /* end confusing part - just pretend this is a typical loop on verts */
411
412                         /* only split of tagged verts - used by separated edges */
413
414                         /* comment the first part because we know this verts in a tagged face */
415                         if (/* v->e && */BM_elem_flag_test(v, BM_ELEM_TAG)) {
416                                 BMVert **vout;
417                                 int r_vout_len;
418                                 BMVert *v_glue = NULL;
419
420                                 /* disable touching twice, this _will_ happen if the flags not disabled */
421                                 BM_elem_flag_disable(v, BM_ELEM_TAG);
422
423                                 bmesh_vert_separate(bm, v, &vout, &r_vout_len);
424                                 v = NULL; /* don't use again */
425
426                                 /* in some cases the edge doesn't split off */
427                                 if (r_vout_len == 1) {
428                                         MEM_freeN(vout);
429                                         continue;
430                                 }
431
432                                 for (k = 0; k < r_vout_len; k++) {
433                                         BMVert *v_split = vout[k]; /* only to avoid vout[k] all over */
434
435                                         /* need to check if this vertex is from a */
436                                         int vert_edge_tag_tot = 0;
437                                         int vecpair[2];
438
439                                         /* find adjacent */
440                                         BM_ITER_ELEM (e, &iter, v_split, BM_EDGES_OF_VERT) {
441                                                 if (BM_elem_flag_test(e, BM_ELEM_TAG) &&
442                                                     e->l && BM_elem_flag_test(e->l->f, BM_ELEM_TAG))
443                                                 {
444                                                         if (vert_edge_tag_tot < 2) {
445                                                                 vecpair[vert_edge_tag_tot] = BM_elem_index_get(e);
446                                                                 BLI_assert(vecpair[vert_edge_tag_tot] != -1);
447                                                         }
448
449                                                         vert_edge_tag_tot++;
450                                                 }
451                                         }
452
453                                         if (vert_edge_tag_tot != 0) {
454                                                 float tvec[3];
455
456                                                 if (vert_edge_tag_tot >= 2) { /* 2 edge users - common case */
457                                                         /* now there are 2 cases to check for,
458                                                          *
459                                                          * if both edges use the same face OR both faces have the same normal,
460                                                          * ...then we can calculate an edge that fits nicely between the 2 edge normals.
461                                                          *
462                                                          * Otherwise use the shared edge OR the corner defined by these 2 face normals,
463                                                          * when both edges faces are adjacent this works best but even when this vertex
464                                                          * fans out faces it should work ok.
465                                                          */
466
467                                                         SplitEdgeInfo *e_info_a = &edge_info[vecpair[0]];
468                                                         SplitEdgeInfo *e_info_b = &edge_info[vecpair[1]];
469
470                                                         BMFace *f_a = e_info_a->l->f;
471                                                         BMFace *f_b = e_info_b->l->f;
472
473                                                         /* we use this as either the normal OR to find the right direction for the
474                                                          * cross product between both face normals */
475                                                         add_v3_v3v3(tvec, e_info_a->no, e_info_b->no);
476
477                                                         /* epsilon increased to fix [#32329] */
478                                                         if ((f_a == f_b) || compare_v3v3(f_a->no, f_b->no, 0.001f)) {
479                                                                 normalize_v3(tvec);
480                                                         }
481                                                         else {
482                                                                 /* these lookups are very quick */
483                                                                 BMLoop *l_other_a = BM_loop_other_vert_loop(e_info_a->l, v_split);
484                                                                 BMLoop *l_other_b = BM_loop_other_vert_loop(e_info_b->l, v_split);
485
486                                                                 if (l_other_a->v == l_other_b->v) {
487                                                                         /* both edges faces are adjacent, but we don't need to know the shared edge
488                                                                          * having both verts is enough. */
489                                                                         sub_v3_v3v3(tvec, l_other_a->v->co, v_split->co);
490                                                                 }
491                                                                 else {
492                                                                         /* faces don't touch,
493                                                                          * just get cross product of their normals, its *good enough*
494                                                                          */
495                                                                         float tno[3];
496                                                                         cross_v3_v3v3(tno, f_a->no, f_b->no);
497                                                                         if (dot_v3v3(tvec, tno) < 0.0f) {
498                                                                                 negate_v3(tno);
499                                                                         }
500                                                                         copy_v3_v3(tvec, tno);
501                                                                 }
502
503                                                                 normalize_v3(tvec);
504                                                         }
505
506                                                         /* scale by edge angle */
507                                                         if (use_even_offset) {
508                                                                 mul_v3_fl(tvec, shell_angle_to_dist(angle_normalized_v3v3(e_info_a->no,
509                                                                                                                           e_info_b->no) / 2.0f));
510                                                         }
511
512                                                         /* scale relative to edge lengths */
513                                                         if (use_relative_offset) {
514                                                                 mul_v3_fl(tvec, (edge_info[vecpair[0]].length + edge_info[vecpair[1]].length) / 2.0f);
515                                                         }
516                                                 }
517                                                 else if (vert_edge_tag_tot == 1) { /* 1 edge user - boundary vert, not so common */
518                                                         const float *e_no_a = edge_info[vecpair[0]].no;
519
520                                                         if (use_even_boundry) {
521
522                                                                 /* This case where only one edge attached to v_split
523                                                                  * is used - ei - the face to inset is on a boundary.
524                                                                  *
525                                                                  *                  We want the inset to align flush with the
526                                                                  *                  boundary edge, not the normal of the interior
527                                                                  *             <--- edge which would give an unsightly bump.
528                                                                  * --+-------------------------+---------------+--
529                                                                  *   |^v_other    ^e_other    /^v_split        |
530                                                                  *   |                       /                 |
531                                                                  *   |                      /                  |
532                                                                  *   |                     / <- tag split edge |
533                                                                  *   |                    /                    |
534                                                                  *   |                   /                     |
535                                                                  *   |                  /                      |
536                                                                  * --+-----------------+-----------------------+--
537                                                                  *   |                                         |
538                                                                  *   |                                         |
539                                                                  *
540                                                                  * note, the fact we are doing location comparisons on verts that are moved about
541                                                                  * doesn't matter because the direction will remain the same in this case.
542                                                                  */
543
544                                                                 BMEdge *e_other;
545                                                                 BMVert *v_other;
546                                                                 /* loop will always be either next of prev */
547                                                                 BMLoop *l = v_split->e->l;
548                                                                 if (l->prev->v == v_split) {
549                                                                         l = l->prev;
550                                                                 }
551                                                                 else if (l->next->v == v_split) {
552                                                                         l = l->next;
553                                                                 }
554                                                                 else if (l->v == v_split) {
555                                                                         /* pass */
556                                                                 }
557                                                                 else {
558                                                                         /* should never happen */
559                                                                         BLI_assert(0);
560                                                                 }
561
562                                                                 /* find the edge which is _not_ being split here */
563                                                                 if (!BM_elem_flag_test(l->e, BM_ELEM_TAG)) {
564                                                                         e_other = l->e;
565                                                                 }
566                                                                 else if (!BM_elem_flag_test(l->prev->e, BM_ELEM_TAG)) {
567                                                                         e_other = l->prev->e;
568                                                                 }
569                                                                 else {
570                                                                         BLI_assert(0);
571                                                                         e_other = NULL;
572                                                                 }
573
574                                                                 v_other = BM_edge_other_vert(e_other, v_split);
575                                                                 sub_v3_v3v3(tvec, v_other->co, v_split->co);
576                                                                 normalize_v3(tvec);
577
578                                                                 if (use_even_offset) {
579                                                                         mul_v3_fl(tvec, shell_angle_to_dist(angle_normalized_v3v3(e_no_a, tvec)));
580                                                                 }
581                                                         }
582                                                         else {
583                                                                 copy_v3_v3(tvec, e_no_a);
584                                                         }
585
586                                                         /* use_even_offset - doesn't apply here */
587
588                                                         /* scale relative to edge length */
589                                                         if (use_relative_offset) {
590                                                                 mul_v3_fl(tvec, edge_info[vecpair[0]].length);
591                                                         }
592                                                 }
593                                                 else {
594                                                         /* should never happen */
595                                                         BLI_assert(0);
596                                                         zero_v3(tvec);
597                                                 }
598
599                                                 /* apply the offset */
600                                                 madd_v3_v3fl(v_split->co, tvec, thickness);
601                                         }
602
603                                         /* this saves expensive/slow glue check for common cases */
604                                         if (r_vout_len > 2) {
605                                                 bool ok = true;
606                                                 /* last step, NULL this vertex if has a tagged face */
607                                                 BM_ITER_ELEM (f, &iter, v_split, BM_FACES_OF_VERT) {
608                                                         if (BM_elem_flag_test(f, BM_ELEM_TAG)) {
609                                                                 ok = false;
610                                                                 break;
611                                                         }
612                                                 }
613
614                                                 if (ok) {
615                                                         if (v_glue == NULL) {
616                                                                 v_glue = v_split;
617                                                         }
618                                                         else {
619                                                                 BM_vert_splice(bm, v_split, v_glue);
620                                                         }
621                                                 }
622                                         }
623                                         /* end glue */
624
625                                 }
626                                 MEM_freeN(vout);
627                         }
628                 }
629         }
630
631         /* create faces */
632         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
633                 BMVert *varr[4] = {NULL};
634                 /* get the verts in the correct order */
635                 BM_edge_ordered_verts_ex(es->e_new, &varr[1], &varr[0], es->l);
636 #if 0
637                 if (varr[0] == es->e_new->v1) {
638                         varr[2] = es->e_old->v2;
639                         varr[3] = es->e_old->v1;
640                 }
641                 else {
642                         varr[2] = es->e_old->v1;
643                         varr[3] = es->e_old->v2;
644                 }
645                 j = 4;
646 #else
647                 /* slightly trickier check - since we can't assume the verts are split */
648                 j = 2; /* 2 edges are set */
649                 if (varr[0] == es->e_new->v1) {
650                         if (es->e_old->v2 != es->e_new->v2) { varr[j++] = es->e_old->v2; }
651                         if (es->e_old->v1 != es->e_new->v1) { varr[j++] = es->e_old->v1; }
652                 }
653                 else {
654                         if (es->e_old->v1 != es->e_new->v1) { varr[j++] = es->e_old->v1; }
655                         if (es->e_old->v2 != es->e_new->v2) { varr[j++] = es->e_old->v2; }
656                 }
657
658                 if (j == 2) {
659                         /* can't make face! */
660                         continue;
661                 }
662 #endif
663                 /* no need to check doubles, we KNOW there won't be any */
664                 /* yes - reverse face is correct in this case */
665                 f = BM_face_create_quad_tri_v(bm, varr, j, es->l->f, false);
666                 BMO_elem_flag_enable(bm, f, ELE_NEW);
667
668                 /* copy for loop data, otherwise UV's and vcols are no good.
669                  * tiny speedup here we could be more clever and copy from known adjacent data
670                  * also - we could attempt to interpolate the loop data, this would be much slower but more useful too */
671 #if 0
672                 /* don't use this because face boundaries have no adjacent loops and won't be filled in.
673                  * instead copy from the opposite side with the code below */
674                 BM_face_copy_shared(bm, f);
675 #else
676                 {
677                         /* 2 inner loops on the edge between the new face and the original */
678                         BMLoop *l_a;
679                         BMLoop *l_b;
680                         BMLoop *l_a_other;
681                         BMLoop *l_b_other;
682
683                         l_a = BM_FACE_FIRST_LOOP(f);
684                         l_b = l_a->next;
685
686                         /* we know this side has a radial_next because of the order of created verts in the quad */
687                         l_a_other = BM_edge_other_loop(l_a->e, l_a);
688                         l_b_other = BM_edge_other_loop(l_a->e, l_b);
689                         BM_elem_attrs_copy(bm, bm, l_a_other, l_a);
690                         BM_elem_attrs_copy(bm, bm, l_b_other, l_b);
691
692                         /* step around to the opposite side of the quad - warning, this may have no other edges! */
693                         l_a = l_a->next->next;
694                         l_b = l_a->next;
695
696                         /* swap a<->b intentionally */
697                         BM_elem_attrs_copy(bm, bm, l_a_other, l_b);
698                         BM_elem_attrs_copy(bm, bm, l_b_other, l_a);
699                 }
700 #endif
701         }
702
703         /* we could flag new edges/verts too, is it useful? */
704         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "faces.out", BM_FACE, ELE_NEW);
705
706         /* cheap feature to add depth to the inset */
707         if (depth != 0.0f) {
708                 float (*varr_co)[3];
709                 BMOIter oiter;
710
711                 /* we need to re-calculate tagged normals, but for this purpose we can copy tagged verts from the
712                  * faces they inset from,  */
713                 for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
714                         zero_v3(es->e_new->v1->no);
715                         zero_v3(es->e_new->v2->no);
716                 }
717                 for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
718                         float *no = es->l->f->no;
719                         add_v3_v3(es->e_new->v1->no, no);
720                         add_v3_v3(es->e_new->v2->no, no);
721                 }
722                 for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
723                         /* annoying, avoid normalizing twice */
724                         if (len_squared_v3(es->e_new->v1->no) != 1.0f) {
725                                 normalize_v3(es->e_new->v1->no);
726                         }
727                         if (len_squared_v3(es->e_new->v2->no) != 1.0f) {
728                                 normalize_v3(es->e_new->v2->no);
729                         }
730                 }
731                 /* done correcting edge verts normals */
732
733                 /* untag verts */
734                 BM_mesh_elem_hflag_disable_all(bm, BM_VERT, BM_ELEM_TAG, false);
735
736                 /* tag face verts */
737                 BMO_ITER (f, &oiter, op->slots_in, "faces", BM_FACE) {
738                         BM_ITER_ELEM (v, &iter, f, BM_VERTS_OF_FACE) {
739                                 BM_elem_flag_enable(v, BM_ELEM_TAG);
740                         }
741                 }
742
743                 /* do in 2 passes so moving the verts doesn't feed back into face angle checks
744                  * which BM_vert_calc_shell_factor uses. */
745
746                 /* over allocate */
747                 varr_co = MEM_callocN(sizeof(*varr_co) * bm->totvert, __func__);
748
749                 BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
750                         if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
751                                 const float fac = (depth *
752                                                    (use_relative_offset ? BM_vert_calc_mean_tagged_edge_length(v) : 1.0f) *
753                                                    (use_even_boundry    ? BM_vert_calc_shell_factor(v) : 1.0f));
754                                 madd_v3_v3v3fl(varr_co[i], v->co, v->no, fac);
755                         }
756                 }
757
758                 BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
759                         if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
760                                 copy_v3_v3(v->co, varr_co[i]);
761                         }
762                 }
763                 MEM_freeN(varr_co);
764         }
765
766         MEM_freeN(edge_info);
767 }