Fix for mesh-inset modifying hidden faces
[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_alloca.h"
35 #include "BLI_memarena.h"
36 #include "BKE_customdata.h"
37
38 #include "bmesh.h"
39
40 #include "intern/bmesh_operators_private.h" /* own include */
41
42 /* Merge loop-data that diverges, see: T41445 */
43 #define USE_LOOP_CUSTOMDATA_MERGE
44
45 #define ELE_NEW         1
46
47
48 /* -------------------------------------------------------------------- */
49 /* Generic Interp Face (use for both types of inset) */
50
51 /**
52  * Interpolation, this is more complex for regions since we're not creating new faces
53  * and throwing away old ones, so instead, store face data needed for interpolation.
54  *
55  * \note This uses CustomData functions in quite a low-level way which should be
56  * avoided, but in this case its hard to do without storing a duplicate mesh. */
57
58 /* just enough of a face to store interpolation data we can use once the inset is done */
59 typedef struct InterpFace {
60         BMFace *f;
61         void **blocks_l;
62         void **blocks_v;
63         float (*cos_2d)[2];
64         float axis_mat[3][3];
65 } InterpFace;
66
67 /* basically a clone of #BM_vert_interp_from_face */
68 static void bm_interp_face_store(InterpFace *iface, BMesh *bm, BMFace *f, MemArena *interp_arena)
69 {
70         BMLoop *l_iter, *l_first;
71         void **blocks_l    = iface->blocks_l = BLI_memarena_alloc(interp_arena, sizeof(*iface->blocks_l) * f->len);
72         void **blocks_v    = iface->blocks_v = BLI_memarena_alloc(interp_arena, sizeof(*iface->blocks_v) * f->len);
73         float (*cos_2d)[2] = iface->cos_2d = BLI_memarena_alloc(interp_arena, sizeof(*iface->cos_2d) * f->len);
74         void *axis_mat     = iface->axis_mat;
75         int i;
76
77         BLI_assert(BM_face_is_normal_valid(f));
78
79         axis_dominant_v3_to_m3(axis_mat, f->no);
80
81         iface->f = f;
82
83         i = 0;
84         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
85         do {
86                 mul_v2_m3v3(cos_2d[i], axis_mat, l_iter->v->co);
87                 blocks_l[i] = NULL;
88                 CustomData_bmesh_copy_data(&bm->ldata, &bm->ldata, l_iter->head.data, &blocks_l[i]);
89                 /* if we were not modifying the loops later we would do... */
90                 // blocks[i] = l_iter->head.data;
91
92                 blocks_v[i] = NULL;
93                 CustomData_bmesh_copy_data(&bm->vdata, &bm->vdata, l_iter->v->head.data, &blocks_v[i]);
94
95                 /* use later for index lookups */
96                 BM_elem_index_set(l_iter, i); /* set_dirty */
97         } while (i++, (l_iter = l_iter->next) != l_first);
98         bm->elem_index_dirty |= BM_LOOP;
99 }
100 static void bm_interp_face_free(InterpFace *iface, BMesh *bm)
101 {
102         void **blocks_l = iface->blocks_l;
103         void **blocks_v = iface->blocks_v;
104         int i;
105
106         for (i = 0; i < iface->f->len; i++) {
107                 CustomData_bmesh_free_block(&bm->ldata, &blocks_l[i]);
108                 CustomData_bmesh_free_block(&bm->vdata, &blocks_v[i]);
109         }
110 }
111
112 #ifdef USE_LOOP_CUSTOMDATA_MERGE
113 /**
114  * This function merges loop customdata (UV's)
115  * where interpolating the values across the face causes values to diverge.
116  */
117 static void bm_loop_customdata_merge(
118         BMesh *bm,
119         BMEdge *e_connect,
120         BMLoop *l_a_outer, BMLoop *l_b_outer,
121         BMLoop *l_a_inner, BMLoop *l_b_inner)
122 {
123         /**
124          * Check for diverged values at the vert shared by
125          * \a l_a_inner & \a l_b_inner.
126          *
127          * <pre>
128          *  -----------------------+
129          *           l_a_outer--> /|<--l_b_outer
130          *                       / |
131          *      (face a)        /  |
132          *                     / <--e_connect
133          *                    /    |
134          * e_a  l_a_inner--> / <--l_b_inner
135          * -----------------+      |
136          *                 /|      |
137          * l_a/b_inner_inset| (face b)
138          *               /  |      |
139          *              /   |e_b   |
140          *  (inset face(s)) |      |
141          *            /     |      |
142          * </pre>
143          */
144
145         const bool is_flip = (l_a_inner->next == l_a_outer);
146         BMLoop *l_a_inner_inset, *l_b_inner_inset;
147         BMEdge *e_a, *e_b;
148         int layer_n;
149
150         /* paranoid sanity checks */
151         BLI_assert(l_a_outer->v == l_b_outer->v);
152         BLI_assert(l_a_inner->v == l_b_inner->v);
153
154         BLI_assert(l_b_inner->f != l_a_inner->f);
155
156         BLI_assert(l_a_outer->f == l_a_inner->f);
157         BLI_assert(l_b_outer->f == l_b_inner->f);
158
159         BLI_assert(BM_edge_in_face(e_connect, l_a_inner->f));
160         BLI_assert(BM_edge_in_face(e_connect, l_b_inner->f));
161
162         if (is_flip) {
163                 e_a = l_a_inner->prev->e;
164                 e_b = l_b_inner->e;
165         }
166         else {
167                 e_a = l_a_inner->e;
168                 e_b = l_b_inner->prev->e;
169         }
170
171         l_a_inner_inset = BM_edge_other_loop(e_a, l_a_inner);
172         l_b_inner_inset = BM_edge_other_loop(e_b, l_b_inner);
173         BLI_assert(l_a_inner_inset->v == l_b_inner_inset->v);
174
175         /* check if ther is no chance of diversion */
176         if (l_a_inner_inset->f == l_b_inner_inset->f) {
177                 return;
178         }
179
180         for (layer_n = 0; layer_n < bm->ldata.totlayer; layer_n++) {
181                 const int type = bm->ldata.layers[layer_n].type;
182                 const int offset = bm->ldata.layers[layer_n].offset;
183                 if (!CustomData_layer_has_math(&bm->ldata, layer_n))
184                         continue;
185
186                 /* check we begin with merged data */
187                 if ((CustomData_data_equals(
188                          type,
189                          BM_ELEM_CD_GET_VOID_P(l_a_outer, offset),
190                          BM_ELEM_CD_GET_VOID_P(l_b_outer, offset))  == true)
191
192                 /* epsilon for comparing UV's is too big, gives noticable problems */
193 #if 0
194                     &&
195                     /* check if the data ends up diverged */
196                     (CustomData_data_equals(
197                          type,
198                          BM_ELEM_CD_GET_VOID_P(l_a_inner, offset),
199                          BM_ELEM_CD_GET_VOID_P(l_b_inner, offset)) == false)
200 #endif
201                     )
202                 {
203                         /* no need to allocate a temp block:
204                          * a = (a + b);
205                          * a *= 0.5f;
206                          * b = a;
207                          */
208                         const void *data_src;
209
210                         CustomData_data_add(
211                                 type,
212                                 BM_ELEM_CD_GET_VOID_P(l_a_inner_inset, offset),
213                                 BM_ELEM_CD_GET_VOID_P(l_b_inner_inset, offset));
214                         CustomData_data_multiply(
215                                 type,
216                                 BM_ELEM_CD_GET_VOID_P(l_a_inner_inset, offset),
217                                 0.5f);
218                         CustomData_data_copy_value(
219                                 type,
220                                 BM_ELEM_CD_GET_VOID_P(l_a_inner_inset, offset),
221                                 BM_ELEM_CD_GET_VOID_P(l_b_inner_inset, offset));
222
223                         /* use this as a reference (could be 'l_b_inner_inset' too) */
224                         data_src = BM_ELEM_CD_GET_VOID_P(l_a_inner_inset, offset);
225
226                         /* check if the 2 faces share an edge */
227                         if (is_flip ?
228                             (l_b_inner_inset->e == l_a_inner_inset->prev->e) :
229                             (l_a_inner_inset->e == l_b_inner_inset->prev->e))
230                         {
231                                 /* simple case, we have all loops already */
232                         }
233                         else {
234                                 /* compare with (l_a_inner / l_b_inner) and assign the blended value if they match */
235                                 BMIter iter;
236                                 BMLoop *l_iter;
237                                 const void *data_cmp_a = BM_ELEM_CD_GET_VOID_P(l_b_inner, offset);
238                                 const void *data_cmp_b = BM_ELEM_CD_GET_VOID_P(l_a_inner, offset);
239                                 BM_ITER_ELEM (l_iter, &iter, l_a_inner_inset->v, BM_LOOPS_OF_VERT) {
240                                         if (BM_elem_flag_test(l_iter->f, BM_ELEM_TAG)) {
241                                                 if (!ELEM(l_iter, l_a_inner, l_b_inner, l_a_inner_inset, l_b_inner_inset)) {
242                                                         void *data_dst = BM_ELEM_CD_GET_VOID_P(l_iter, offset);
243
244                                                         if (CustomData_data_equals(type, data_dst, data_cmp_a) ||
245                                                             CustomData_data_equals(type, data_dst, data_cmp_b))
246                                                         {
247                                                                 CustomData_data_copy_value(type, data_src, data_dst);
248                                                         }
249                                                 }
250                                         }
251                                 }
252                         }
253
254                         CustomData_data_copy_value(type, data_src, BM_ELEM_CD_GET_VOID_P(l_b_inner, offset));
255                         CustomData_data_copy_value(type, data_src, BM_ELEM_CD_GET_VOID_P(l_a_inner, offset));
256                 }
257         }
258 }
259 #endif  /* USE_LOOP_CUSTOMDATA_MERGE */
260
261
262 /* -------------------------------------------------------------------- */
263 /* Inset Individual */
264
265 static void bmo_face_inset_individual(
266         BMesh *bm, BMFace *f, MemArena *interp_arena,
267         const float thickness, const float depth,
268         const bool use_even_offset, const bool use_relative_offset, const bool use_interpolate)
269 {
270         InterpFace *iface = NULL;
271
272         /* stores verts split away from the face (aligned with face verts) */
273         BMVert **verts = BLI_array_alloca(verts, f->len);
274         /* store edge normals (aligned with face-loop-edges) */
275         float (*edge_nors)[3] = BLI_array_alloca(edge_nors, f->len);
276         float (*coords)[3] = BLI_array_alloca(coords, f->len);
277
278         BMLoop *l_iter, *l_first;
279         BMLoop *l_other;
280         unsigned int i;
281         float e_length_prev;
282
283         l_first = BM_FACE_FIRST_LOOP(f);
284
285         /* split off all loops */
286         l_iter = l_first;
287         i = 0;
288         do {
289                 BMVert *v_other = l_iter->v;
290                 BMVert *v_sep = BM_face_loop_separate(bm, l_iter);
291                 if (v_sep == v_other) {
292                         v_other = BM_vert_create(bm, l_iter->v->co, l_iter->v, BM_CREATE_NOP);
293                 }
294                 verts[i] = v_other;
295
296                 /* unrelated to splitting, but calc here */
297                 BM_edge_calc_face_tangent(l_iter->e, l_iter, edge_nors[i]);
298         } while (i++, ((l_iter = l_iter->next) != l_first));
299
300
301         /* build rim faces */
302         l_iter = l_first;
303         i = 0;
304         do {
305                 BMFace *f_new_outer;
306                 BMVert *v_other = verts[i];
307                 BMVert *v_other_next = verts[(i + 1) % f->len];
308
309                 BMEdge *e_other = BM_edge_create(bm, v_other, v_other_next, l_iter->e, BM_CREATE_NO_DOUBLE);
310                 (void)e_other;
311
312                 f_new_outer = BM_face_create_quad_tri(bm,
313                                                       v_other,
314                                                       v_other_next,
315                                                       l_iter->next->v,
316                                                       l_iter->v,
317                                                       f, BM_CREATE_NOP);
318                 BMO_elem_flag_enable(bm, f_new_outer, ELE_NEW);
319
320                 /* copy loop data */
321                 l_other = l_iter->radial_next;
322                 BM_elem_attrs_copy(bm, bm, l_iter->next, l_other->prev);
323                 BM_elem_attrs_copy(bm, bm, l_iter, l_other->next->next);
324
325                 if (use_interpolate == false) {
326                         BM_elem_attrs_copy(bm, bm, l_iter->next, l_other);
327                         BM_elem_attrs_copy(bm, bm, l_iter, l_other->next);
328                 }
329         } while (i++, ((l_iter = l_iter->next) != l_first));
330
331         /* hold interpolation values */
332         if (use_interpolate) {
333                 iface = BLI_memarena_alloc(interp_arena, sizeof(*iface));
334                 bm_interp_face_store(iface, bm, f, interp_arena);
335         }
336
337         /* Calculate translation vector for new */
338         l_iter = l_first;
339         i = 0;
340
341         if (depth != 0.0f) {
342                 e_length_prev = BM_edge_calc_length(l_iter->prev->e);
343         }
344
345         do {
346                 const float *eno_prev = edge_nors[(i ? i : f->len) - 1];
347                 const float *eno_next = edge_nors[i];
348                 float tvec[3];
349                 float v_new_co[3];
350
351                 add_v3_v3v3(tvec, eno_prev, eno_next);
352                 normalize_v3(tvec);
353
354                 copy_v3_v3(v_new_co, l_iter->v->co);
355
356                 if (use_even_offset) {
357                         mul_v3_fl(tvec, shell_v3v3_mid_normalized_to_dist(eno_prev,  eno_next));
358                 }
359
360                 /* Modify vertices and their normals */
361                 if (use_relative_offset) {
362                         mul_v3_fl(tvec, (BM_edge_calc_length(l_iter->e) + BM_edge_calc_length(l_iter->prev->e)) / 2.0f);
363                 }
364
365                 madd_v3_v3fl(v_new_co, tvec, thickness);
366
367                 /* Set normal, add depth and write new vertex position*/
368                 copy_v3_v3(l_iter->v->no, f->no);
369
370                 if (depth != 0.0f) {
371                         const float e_length = BM_edge_calc_length(l_iter->e);
372                         const float fac = depth * (use_relative_offset ? ((e_length_prev + e_length) * 0.5f) : 1.0f);
373                         e_length_prev = e_length;
374
375                         madd_v3_v3fl(v_new_co, f->no, fac);
376                 }
377
378
379
380                 copy_v3_v3(coords[i], v_new_co);
381         } while (i++, ((l_iter = l_iter->next) != l_first));
382
383         /* update the coords */
384         l_iter = l_first;
385         i = 0;
386         do {
387                 copy_v3_v3(l_iter->v->co, coords[i]);
388         } while (i++, ((l_iter = l_iter->next) != l_first));
389
390
391         if (use_interpolate) {
392                 BM_face_interp_from_face_ex(bm, iface->f, iface->f, true,
393                                             iface->blocks_l, iface->blocks_v, iface->cos_2d, iface->axis_mat);
394
395                 /* build rim faces */
396                 l_iter = l_first;
397                 do {
398                         /* copy loop data */
399                         l_other = l_iter->radial_next;
400
401                         BM_elem_attrs_copy(bm, bm, l_iter->next, l_other);
402                         BM_elem_attrs_copy(bm, bm, l_iter, l_other->next);
403                 } while ((l_iter = l_iter->next) != l_first);
404
405                 bm_interp_face_free(iface, bm);
406         }
407 }
408
409
410 /**
411  * Individual Face Inset.
412  * Find all tagged faces (f), duplicate edges around faces, inset verts of
413  * created edges, create new faces between old and new edges, fill face
414  * between connected new edges, kill old face (f).
415  */
416 void bmo_inset_individual_exec(BMesh *bm, BMOperator *op)
417 {
418         BMFace *f;
419
420         BMOIter oiter;
421         MemArena *interp_arena = NULL;
422
423         const float thickness = BMO_slot_float_get(op->slots_in, "thickness");
424         const float depth = BMO_slot_float_get(op->slots_in, "depth");
425         const bool use_even_offset = BMO_slot_bool_get(op->slots_in, "use_even_offset");
426         const bool use_relative_offset = BMO_slot_bool_get(op->slots_in, "use_relative_offset");
427         const bool use_interpolate = BMO_slot_bool_get(op->slots_in, "use_interpolate");
428
429         /* Only tag faces in slot */
430         BM_mesh_elem_hflag_disable_all(bm, BM_FACE, BM_ELEM_TAG, false);
431
432         BMO_slot_buffer_hflag_enable(bm, op->slots_in, "faces", BM_FACE, BM_ELEM_TAG, false);
433
434         if (use_interpolate) {
435                 interp_arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
436         }
437
438         BMO_ITER (f, &oiter, op->slots_in, "faces", BM_FACE) {
439                 bmo_face_inset_individual(
440                         bm, f, interp_arena,
441                         thickness, depth,
442                         use_even_offset, use_relative_offset, use_interpolate);
443
444                 if (use_interpolate) {
445                         BLI_memarena_clear(interp_arena);
446                 }
447         }
448
449         /* we could flag new edges/verts too, is it useful? */
450         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "faces.out", BM_FACE, ELE_NEW);
451
452         if (use_interpolate) {
453                 BLI_memarena_free(interp_arena);
454         }
455 }
456
457
458
459 /* -------------------------------------------------------------------- */
460 /* Inset Region */
461
462 typedef struct SplitEdgeInfo {
463         float   no[3];
464         float   length;
465         BMEdge *e_old;
466         BMEdge *e_new;
467         BMLoop *l;
468 } SplitEdgeInfo;
469
470 /**
471  * return the tag loop where there is...
472  * - only 1 tagged face attached to this edge.
473  * - 1 or more untagged faces.
474  *
475  * \note this function looks to be expensive
476  * but in most cases it will only do 2 iterations.
477  */
478 static BMLoop *bm_edge_is_mixed_face_tag(BMLoop *l)
479 {
480         if (LIKELY(l != NULL)) {
481                 int tot_tag = 0;
482                 int tot_untag = 0;
483                 BMLoop *l_iter;
484                 BMLoop *l_tag = NULL;
485                 l_iter = l;
486                 do {
487                         if (BM_elem_flag_test(l_iter->f, BM_ELEM_TAG)) {
488                                 /* more than one tagged face - bail out early! */
489                                 if (tot_tag == 1) {
490                                         return NULL;
491                                 }
492                                 l_tag = l_iter;
493                                 tot_tag++;
494                         }
495                         else {
496                                 tot_untag++;
497                         }
498
499                 } while ((l_iter = l_iter->radial_next) != l);
500
501                 return ((tot_tag == 1) && (tot_untag >= 1)) ? l_tag : NULL;
502         }
503         else {
504                 return NULL;
505         }
506 }
507
508 static float bm_edge_info_average_length(BMVert *v, SplitEdgeInfo *edge_info)
509 {
510         BMIter iter;
511         BMEdge *e;
512
513         float len = 0.0f;
514         int tot = 0;
515
516
517         BM_ITER_ELEM (e, &iter, v, BM_EDGES_OF_VERT) {
518                 const int i = BM_elem_index_get(e);
519                 if (i != -1) {
520                         len += edge_info[i].length;
521                         tot++;
522                 }
523         }
524
525         BLI_assert(tot != 0);
526         return len / (float)tot;
527 }
528
529 /**
530  * implementation is as follows...
531  *
532  * - set all faces as tagged/untagged based on selection.
533  * - find all edges that have 1 tagged, 1 untagged face.
534  * - separate these edges and tag vertices, set their index to point to the original edge.
535  * - build faces between old/new edges.
536  * - inset the new edges into their faces.
537  */
538
539 void bmo_inset_region_exec(BMesh *bm, BMOperator *op)
540 {
541         const bool use_outset          = BMO_slot_bool_get(op->slots_in, "use_outset");
542         const bool use_boundary        = BMO_slot_bool_get(op->slots_in, "use_boundary") && (use_outset == false);
543         const bool use_even_offset     = BMO_slot_bool_get(op->slots_in, "use_even_offset");
544         const bool use_even_boundary   = use_even_offset; /* could make own option */
545         const bool use_relative_offset = BMO_slot_bool_get(op->slots_in, "use_relative_offset");
546         const bool use_edge_rail       = BMO_slot_bool_get(op->slots_in, "use_edge_rail");
547         const bool use_interpolate     = BMO_slot_bool_get(op->slots_in, "use_interpolate");
548         const float thickness          = BMO_slot_float_get(op->slots_in, "thickness");
549         const float depth              = BMO_slot_float_get(op->slots_in, "depth");
550 #ifdef USE_LOOP_CUSTOMDATA_MERGE
551         const bool has_math_ldata      = (use_interpolate && CustomData_has_math(&bm->ldata));
552 #endif
553
554         int edge_info_len = 0;
555
556         BMIter iter;
557         SplitEdgeInfo *edge_info;
558         SplitEdgeInfo *es;
559
560         /* Interpolation Vars */
561         /* an array alligned with faces but only fill items which are used. */
562         InterpFace **iface_array = NULL;
563         int          iface_array_len;
564         MemArena *interp_arena = NULL;
565
566         /* BMVert original location storage */
567         const bool use_vert_coords_orig = use_edge_rail;
568         MemArena *vert_coords_orig = NULL;
569         GHash *vert_coords = NULL;
570
571         BMVert *v;
572         BMEdge *e;
573         BMFace *f;
574         int i, j, k;
575
576         if (use_interpolate) {
577                 interp_arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
578                 /* warning, we could be more clever here and not over alloc */
579                 iface_array = MEM_callocN(sizeof(*iface_array) * bm->totface, __func__);
580                 iface_array_len = bm->totface;
581         }
582
583         if (use_outset == false) {
584                 BM_mesh_elem_hflag_disable_all(bm, BM_FACE, BM_ELEM_TAG, false);
585                 BMO_slot_buffer_hflag_enable(bm, op->slots_in, "faces", BM_FACE, BM_ELEM_TAG, false);
586         }
587         else {
588                 BM_mesh_elem_hflag_enable_all(bm, BM_FACE, BM_ELEM_TAG, false);
589                 BMO_slot_buffer_hflag_disable(bm, op->slots_in, "faces", BM_FACE, BM_ELEM_TAG, false);
590                 BMO_slot_buffer_hflag_disable(bm, op->slots_in, "faces_exclude", BM_FACE, BM_ELEM_TAG, false);
591         }
592
593         /* first count all inset edges we will split */
594         /* fill in array and initialize tagging */
595         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
596                 if (
597                     /* tag if boundary is enabled */
598                     (use_boundary && BM_edge_is_boundary(e) && BM_elem_flag_test(e->l->f, BM_ELEM_TAG)) ||
599
600                     /* tag if edge is an interior edge inbetween a tagged and untagged face */
601                     (bm_edge_is_mixed_face_tag(e->l)))
602                 {
603                         /* tag */
604                         BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
605                         BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
606                         BM_elem_flag_enable(e, BM_ELEM_TAG);
607
608                         BM_elem_index_set(e, edge_info_len); /* set_dirty! */
609                         edge_info_len++;
610                 }
611                 else {
612                         BM_elem_flag_disable(e->v1, BM_ELEM_TAG);
613                         BM_elem_flag_disable(e->v2, BM_ELEM_TAG);
614                         BM_elem_flag_disable(e, BM_ELEM_TAG);
615
616                         BM_elem_index_set(e, -1); /* set_dirty! */
617                 }
618         }
619         bm->elem_index_dirty |= BM_EDGE;
620
621         edge_info = MEM_mallocN(edge_info_len * sizeof(SplitEdgeInfo), __func__);
622
623         /* fill in array and initialize tagging */
624         es = edge_info;
625         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
626                 i = BM_elem_index_get(e);
627                 if (i != -1) {
628                         /* calc edge-split info */
629                         es->length = BM_edge_calc_length(e);
630                         es->e_old = e;
631                         es++;
632                         /* initialize no and e_new after */
633                 }
634         }
635
636
637         if (use_vert_coords_orig) {
638                 vert_coords_orig = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
639                 vert_coords = BLI_ghash_ptr_new(__func__);
640         }
641
642         /* util macros */
643 #define VERT_ORIG_STORE(_v)  { \
644                 float *_co = BLI_memarena_alloc(vert_coords_orig, sizeof(float[3])); \
645                 copy_v3_v3(_co, (_v)->co); \
646                 BLI_ghash_insert(vert_coords, _v, _co); \
647         } (void)0
648 #define VERT_ORIG_GET(_v)  \
649         (const float *)BLI_ghash_lookup_default(vert_coords, (_v), (_v)->co)
650
651
652         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
653                 if ((es->l = bm_edge_is_mixed_face_tag(es->e_old->l))) {
654                         /* do nothing */
655                 }
656                 else {
657                         es->l = es->e_old->l; /* must be a boundary */
658                 }
659
660
661                 /* run the separate arg */
662                 bmesh_edge_separate(bm, es->e_old, es->l, false);
663
664                 /* calc edge-split info */
665                 es->e_new = es->l->e;
666                 BM_edge_calc_face_tangent(es->e_new, es->l, es->no);
667
668                 if (es->e_new == es->e_old) { /* happens on boundary edges */
669                         /* take care here, we're creating this double edge which _must_ have its verts replaced later on */
670                         es->e_old = BM_edge_create(bm, es->e_new->v1, es->e_new->v2, es->e_new, BM_CREATE_NOP);
671                 }
672
673                 /* store index back to original in 'edge_info' */
674                 BM_elem_index_set(es->e_new, i);
675                 BM_elem_flag_enable(es->e_new, BM_ELEM_TAG);
676
677                 /* important to tag again here */
678                 BM_elem_flag_enable(es->e_new->v1, BM_ELEM_TAG);
679                 BM_elem_flag_enable(es->e_new->v2, BM_ELEM_TAG);
680
681
682                 /* initialize interpolation vars */
683                 /* this could go in its own loop,
684                  * only use the 'es->l->f' so we don't store loops for faces which have no mixed selection
685                  *
686                  * note: faces on the other side of the inset will be interpolated too since this is hard to
687                  * detect, just allow it even though it will cause some redundant interpolation */
688                 if (use_interpolate) {
689                         BMIter viter;
690                         BM_ITER_ELEM (v, &viter, es->l->e, BM_VERTS_OF_EDGE) {
691                                 BMIter fiter;
692                                 BM_ITER_ELEM (f, &fiter, v, BM_FACES_OF_VERT) {
693                                         const int j = BM_elem_index_get(f);
694                                         if (iface_array[j] == NULL) {
695                                                 InterpFace *iface = BLI_memarena_alloc(interp_arena, sizeof(*iface));
696                                                 bm_interp_face_store(iface, bm, f, interp_arena);
697                                                 iface_array[j] = iface;
698                                         }
699                                 }
700                         }
701                 }
702                 /* done interpolation */
703         }
704
705         /* show edge normals for debugging */
706 #if 0
707         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
708                 float tvec[3];
709                 BMVert *v1, *v2;
710
711                 mid_v3_v3v3(tvec, es->e_new->v1->co, es->e_new->v2->co);
712
713                 v1 = BM_vert_create(bm, tvec, NULL, BM_CREATE_NOP);
714                 v2 = BM_vert_create(bm, tvec, NULL, BM_CREATE_NOP);
715                 madd_v3_v3fl(v2->co, es->no, 0.1f);
716                 BM_edge_create(bm, v1, v2, NULL, 0);
717         }
718 #endif
719
720         /* execute the split and position verts, it would be most obvious to loop over verts
721          * here but don't do this since we will be splitting them off (iterating stuff you modify is bad juju)
722          * instead loop over edges then their verts */
723         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
724                 for (j = 0; j < 2; j++) {
725                         v = (j == 0) ? es->e_new->v1 : es->e_new->v2;
726
727                         /* end confusing part - just pretend this is a typical loop on verts */
728
729                         /* only split of tagged verts - used by separated edges */
730
731                         /* comment the first part because we know this verts in a tagged face */
732                         if (/* v->e && */BM_elem_flag_test(v, BM_ELEM_TAG)) {
733                                 BMVert **vout;
734                                 int r_vout_len;
735                                 BMVert *v_glue = NULL;
736
737                                 /* disable touching twice, this _will_ happen if the flags not disabled */
738                                 BM_elem_flag_disable(v, BM_ELEM_TAG);
739
740                                 bmesh_vert_separate(bm, v, &vout, &r_vout_len, false);
741                                 v = NULL; /* don't use again */
742
743                                 /* in some cases the edge doesn't split off */
744                                 if (r_vout_len == 1) {
745                                         if (use_vert_coords_orig) {
746                                                 VERT_ORIG_STORE(vout[0]);
747                                         }
748                                         MEM_freeN(vout);
749                                         continue;
750                                 }
751
752                                 for (k = 0; k < r_vout_len; k++) {
753                                         BMVert *v_split = vout[k]; /* only to avoid vout[k] all over */
754
755                                         /* need to check if this vertex is from a */
756                                         int vert_edge_tag_tot = 0;
757                                         int vecpair[2];
758
759                                         if (use_vert_coords_orig) {
760                                                 VERT_ORIG_STORE(v_split);
761                                         }
762
763                                         /* find adjacent */
764                                         BM_ITER_ELEM (e, &iter, v_split, BM_EDGES_OF_VERT) {
765                                                 if (BM_elem_flag_test(e, BM_ELEM_TAG) &&
766                                                     e->l && BM_elem_flag_test(e->l->f, BM_ELEM_TAG))
767                                                 {
768                                                         if (vert_edge_tag_tot < 2) {
769                                                                 vecpair[vert_edge_tag_tot] = BM_elem_index_get(e);
770                                                                 BLI_assert(vecpair[vert_edge_tag_tot] != -1);
771                                                         }
772
773                                                         vert_edge_tag_tot++;
774                                                 }
775                                         }
776
777                                         if (vert_edge_tag_tot != 0) {
778                                                 float tvec[3];
779
780                                                 if (vert_edge_tag_tot >= 2) { /* 2 edge users - common case */
781                                                         /* now there are 2 cases to check for,
782                                                          *
783                                                          * if both edges use the same face OR both faces have the same normal,
784                                                          * ...then we can calculate an edge that fits nicely between the 2 edge normals.
785                                                          *
786                                                          * Otherwise use the shared edge OR the corner defined by these 2 face normals,
787                                                          * when both edges faces are adjacent this works best but even when this vertex
788                                                          * fans out faces it should work ok.
789                                                          */
790
791                                                         SplitEdgeInfo *e_info_a = &edge_info[vecpair[0]];
792                                                         SplitEdgeInfo *e_info_b = &edge_info[vecpair[1]];
793
794                                                         BMFace *f_a = e_info_a->l->f;
795                                                         BMFace *f_b = e_info_b->l->f;
796
797                                                         /* set to true when we're not in-between (e_info_a->no, e_info_b->no) exactly
798                                                          * in this case use a check the angle of the tvec when calculating shell thickness */
799                                                         bool is_mid = true;
800
801                                                         /* we use this as either the normal OR to find the right direction for the
802                                                          * cross product between both face normals */
803                                                         add_v3_v3v3(tvec, e_info_a->no, e_info_b->no);
804
805                                                         if (use_edge_rail == false) {
806                                                                 /* pass */
807                                                         }
808                                                         else if (f_a != f_b) {
809                                                                 /* these lookups are very quick */
810                                                                 BMLoop *l_other_a = BM_loop_other_vert_loop(e_info_a->l, v_split);
811                                                                 BMLoop *l_other_b = BM_loop_other_vert_loop(e_info_b->l, v_split);
812
813                                                                 if (l_other_a->v == l_other_b->v) {
814                                                                         /* both edges faces are adjacent, but we don't need to know the shared edge
815                                                                          * having both verts is enough. */
816                                                                         const float *co_other;
817
818                                                                         /* note that we can't use 'l_other_a->v' directly since it
819                                                                          * may be inset and give a feedback loop. */
820                                                                         if (use_vert_coords_orig) {
821                                                                                 co_other = VERT_ORIG_GET(l_other_a->v);
822                                                                         }
823                                                                         else {
824                                                                                 co_other = l_other_a->v->co;
825                                                                         }
826
827                                                                         sub_v3_v3v3(tvec, co_other, v_split->co);
828                                                                         is_mid = false;
829                                                                 }
830
831                                                                 /* distable gives odd results at times, see [#39288] */
832 #if 0
833                                                                 else if (compare_v3v3(f_a->no, f_b->no, 0.001f) == false) {
834                                                                         /* epsilon increased to fix [#32329] */
835
836                                                                         /* faces don't touch,
837                                                                          * just get cross product of their normals, its *good enough*
838                                                                          */
839                                                                         float tno[3];
840                                                                         cross_v3_v3v3(tno, f_a->no, f_b->no);
841                                                                         if (dot_v3v3(tvec, tno) < 0.0f) {
842                                                                                 negate_v3(tno);
843                                                                         }
844                                                                         copy_v3_v3(tvec, tno);
845                                                                         is_mid = false;
846                                                                 }
847 #endif
848                                                         }
849                                                         normalize_v3(tvec);
850
851                                                         /* scale by edge angle */
852                                                         if (use_even_offset) {
853                                                                 if (is_mid) {
854                                                                         mul_v3_fl(tvec, shell_v3v3_mid_normalized_to_dist(e_info_a->no,
855                                                                                                                           e_info_b->no));
856                                                                 }
857                                                                 else {
858                                                                         /* use the largest angle */
859                                                                         mul_v3_fl(tvec,
860                                                                                   shell_v3v3_normalized_to_dist(tvec,
861                                                                                                                 len_squared_v3v3(tvec, e_info_a->no) >
862                                                                                                                 len_squared_v3v3(tvec, e_info_b->no) ?
863                                                                                                                     e_info_a->no : e_info_b->no));
864                                                                 }
865                                                         }
866
867                                                         /* scale relative to edge lengths */
868                                                         if (use_relative_offset) {
869                                                                 mul_v3_fl(tvec, (edge_info[vecpair[0]].length + edge_info[vecpair[1]].length) / 2.0f);
870                                                         }
871                                                 }
872                                                 else if (vert_edge_tag_tot == 1) { /* 1 edge user - boundary vert, not so common */
873                                                         const float *e_no_a = edge_info[vecpair[0]].no;
874
875                                                         if (use_even_boundary) {
876
877                                                                 /* This case where only one edge attached to v_split
878                                                                  * is used - ei - the face to inset is on a boundary.
879                                                                  *
880                                                                  *                  We want the inset to align flush with the
881                                                                  *                  boundary edge, not the normal of the interior
882                                                                  *             <--- edge which would give an unsightly bump.
883                                                                  * --+-------------------------+---------------+--
884                                                                  *   |^v_other    ^e_other    /^v_split        |
885                                                                  *   |                       /                 |
886                                                                  *   |                      /                  |
887                                                                  *   |                     / <- tag split edge |
888                                                                  *   |                    /                    |
889                                                                  *   |                   /                     |
890                                                                  *   |                  /                      |
891                                                                  * --+-----------------+-----------------------+--
892                                                                  *   |                                         |
893                                                                  *   |                                         |
894                                                                  *
895                                                                  * note, the fact we are doing location comparisons on verts that are moved about
896                                                                  * doesn't matter because the direction will remain the same in this case.
897                                                                  */
898
899                                                                 BMEdge *e_other;
900                                                                 BMVert *v_other;
901                                                                 /* loop will always be either next of prev */
902                                                                 BMLoop *l = v_split->e->l;
903                                                                 if (l->prev->v == v_split) {
904                                                                         l = l->prev;
905                                                                 }
906                                                                 else if (l->next->v == v_split) {
907                                                                         l = l->next;
908                                                                 }
909                                                                 else if (l->v == v_split) {
910                                                                         /* pass */
911                                                                 }
912                                                                 else {
913                                                                         /* should never happen */
914                                                                         BLI_assert(0);
915                                                                 }
916
917                                                                 /* find the edge which is _not_ being split here */
918                                                                 if (!BM_elem_flag_test(l->e, BM_ELEM_TAG)) {
919                                                                         e_other = l->e;
920                                                                 }
921                                                                 else if (!BM_elem_flag_test(l->prev->e, BM_ELEM_TAG)) {
922                                                                         e_other = l->prev->e;
923                                                                 }
924                                                                 else {
925                                                                         BLI_assert(0);
926                                                                         e_other = NULL;
927                                                                 }
928
929                                                                 v_other = BM_edge_other_vert(e_other, v_split);
930                                                                 sub_v3_v3v3(tvec, v_other->co, v_split->co);
931                                                                 normalize_v3(tvec);
932
933                                                                 if (use_even_offset) {
934                                                                         mul_v3_fl(tvec, shell_v3v3_normalized_to_dist(e_no_a, tvec));
935                                                                 }
936                                                         }
937                                                         else {
938                                                                 copy_v3_v3(tvec, e_no_a);
939                                                         }
940
941                                                         /* use_even_offset - doesn't apply here */
942
943                                                         /* scale relative to edge length */
944                                                         if (use_relative_offset) {
945                                                                 mul_v3_fl(tvec, edge_info[vecpair[0]].length);
946                                                         }
947                                                 }
948                                                 else {
949                                                         /* should never happen */
950                                                         BLI_assert(0);
951                                                         zero_v3(tvec);
952                                                 }
953
954                                                 /* apply the offset */
955                                                 madd_v3_v3fl(v_split->co, tvec, thickness);
956                                         }
957
958                                         /* this saves expensive/slow glue check for common cases */
959                                         if (r_vout_len > 2) {
960                                                 bool ok = true;
961                                                 /* last step, NULL this vertex if has a tagged face */
962                                                 BM_ITER_ELEM (f, &iter, v_split, BM_FACES_OF_VERT) {
963                                                         if (BM_elem_flag_test(f, BM_ELEM_TAG)) {
964                                                                 ok = false;
965                                                                 break;
966                                                         }
967                                                 }
968
969                                                 if (ok) {
970                                                         if (v_glue == NULL) {
971                                                                 v_glue = v_split;
972                                                         }
973                                                         else {
974                                                                 BM_vert_splice(bm, v_split, v_glue);
975                                                         }
976                                                 }
977                                         }
978                                         /* end glue */
979
980                                 }
981                                 MEM_freeN(vout);
982                         }
983                 }
984         }
985
986         if (use_vert_coords_orig) {
987                 BLI_memarena_free(vert_coords_orig);
988                 BLI_ghash_free(vert_coords, NULL, NULL);
989         }
990
991         if (use_interpolate) {
992                 for (i = 0; i < iface_array_len; i++) {
993                         if (iface_array[i]) {
994                                 InterpFace *iface = iface_array[i];
995                                 BM_face_interp_from_face_ex(bm, iface->f, iface->f, true,
996                                                             iface->blocks_l, iface->blocks_v, iface->cos_2d, iface->axis_mat);
997                         }
998                 }
999         }
1000
1001         /* create faces */
1002         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
1003                 BMVert *varr[4] = {NULL};
1004                 /* get the verts in the correct order */
1005                 BM_edge_ordered_verts_ex(es->e_new, &varr[1], &varr[0], es->l);
1006 #if 0
1007                 if (varr[0] == es->e_new->v1) {
1008                         varr[2] = es->e_old->v2;
1009                         varr[3] = es->e_old->v1;
1010                 }
1011                 else {
1012                         varr[2] = es->e_old->v1;
1013                         varr[3] = es->e_old->v2;
1014                 }
1015                 j = 4;
1016 #else
1017                 /* slightly trickier check - since we can't assume the verts are split */
1018                 j = 2; /* 2 edges are set */
1019                 if (varr[0] == es->e_new->v1) {
1020                         if (es->e_old->v2 != es->e_new->v2) { varr[j++] = es->e_old->v2; }
1021                         if (es->e_old->v1 != es->e_new->v1) { varr[j++] = es->e_old->v1; }
1022                 }
1023                 else {
1024                         if (es->e_old->v1 != es->e_new->v1) { varr[j++] = es->e_old->v1; }
1025                         if (es->e_old->v2 != es->e_new->v2) { varr[j++] = es->e_old->v2; }
1026                 }
1027
1028                 if (j == 2) {
1029                         /* can't make face! */
1030                         continue;
1031                 }
1032 #endif
1033                 /* no need to check doubles, we KNOW there won't be any */
1034                 /* yes - reverse face is correct in this case */
1035                 f = BM_face_create_verts(bm, varr, j, es->l->f, BM_CREATE_NOP, true);
1036                 BMO_elem_flag_enable(bm, f, ELE_NEW);
1037
1038                 /* copy for loop data, otherwise UV's and vcols are no good.
1039                  * tiny speedup here we could be more clever and copy from known adjacent data
1040                  * also - we could attempt to interpolate the loop data, this would be much slower but more useful too */
1041 #if 0
1042                 /* don't use this because face boundaries have no adjacent loops and won't be filled in.
1043                  * instead copy from the opposite side with the code below */
1044                 BM_face_copy_shared(bm, f, NULL, NULL);
1045 #else
1046                 {
1047                         /* 2 inner loops on the edge between the new face and the original */
1048                         BMLoop *l_a;
1049                         BMLoop *l_b;
1050                         BMLoop *l_a_other;
1051                         BMLoop *l_b_other;
1052
1053                         l_a = BM_FACE_FIRST_LOOP(f);
1054                         l_b = l_a->next;
1055
1056                         /* we know this side has a radial_next because of the order of created verts in the quad */
1057                         l_a_other = BM_edge_other_loop(l_a->e, l_a);
1058                         l_b_other = BM_edge_other_loop(l_a->e, l_b);
1059                         BM_elem_attrs_copy(bm, bm, l_a_other, l_a);
1060                         BM_elem_attrs_copy(bm, bm, l_b_other, l_b);
1061
1062                         BLI_assert(l_a->f != l_a_other->f);
1063                         BLI_assert(l_b->f != l_b_other->f);
1064
1065                         /* step around to the opposite side of the quad - warning, this may have no other edges! */
1066                         l_a = l_a->next->next;
1067                         l_b = l_a->next;
1068
1069                         /**
1070                          * Loops vars from newly created face (face_a/b)
1071                          * <pre>
1072                          *              l_a->e & l_b->prev->e
1073                          * +------------------------------------+
1074                          * |\ l_a                          l_b /|
1075                          * | \ l_a->prev->e            l_b->e / |
1076                          * |  \ l_a->prev          l_b->next /  |
1077                          * |   +----------------------------+   |
1078                          * |   |l_a_other    ^     l_b_other|   |
1079                          * |   |        l_b->next->e &...   |   |
1080                          * |   |        l_a->prev->prev->e  |   |
1081                          * |   |        (inset face)        |   |
1082                          * |   +----------------------------+   |
1083                          * |  /                              \  |
1084                          * | /                                \ |
1085                          * |/                                  \|
1086                          * +------------------------------------+
1087                          * </pre>
1088                          */
1089
1090                         /* swap a<->b intentionally */
1091                         if (use_interpolate) {
1092                                 InterpFace *iface = iface_array[BM_elem_index_get(es->l->f)];
1093                                 const int i_a = BM_elem_index_get(l_a_other);
1094                                 const int i_b = BM_elem_index_get(l_b_other);
1095                                 CustomData_bmesh_copy_data(&bm->ldata, &bm->ldata, iface->blocks_l[i_a], &l_b->head.data);
1096                                 CustomData_bmesh_copy_data(&bm->ldata, &bm->ldata, iface->blocks_l[i_b], &l_a->head.data);
1097
1098 #ifdef USE_LOOP_CUSTOMDATA_MERGE
1099                                 if (has_math_ldata) {
1100                                         BMEdge *e_connect;
1101
1102                                         /* connecting edge 'a' */
1103                                         e_connect = l_a->prev->e;
1104                                         if (BM_edge_is_manifold(e_connect)) {
1105                                                 bm_loop_customdata_merge(
1106                                                         bm, e_connect,
1107                                                         l_a,       BM_edge_other_loop(e_connect, l_a),
1108                                                         l_a->prev, BM_edge_other_loop(e_connect, l_a->prev));
1109                                         }
1110
1111                                         /* connecting edge 'b' */
1112                                         e_connect = l_b->e;
1113                                         if (BM_edge_is_manifold(e_connect)) {
1114                                                 /* swap arg order to maintain winding */
1115                                                 bm_loop_customdata_merge(
1116                                                         bm, e_connect,
1117                                                         l_b,       BM_edge_other_loop(e_connect, l_b),
1118                                                         l_b->next, BM_edge_other_loop(e_connect, l_b->next));
1119                                         }
1120                                 }
1121 #endif  /* USE_LOOP_CUSTOMDATA_MERGE */
1122                         }
1123                         else {
1124                                 BM_elem_attrs_copy(bm, bm, l_a_other, l_b);
1125                                 BM_elem_attrs_copy(bm, bm, l_b_other, l_a);
1126                         }
1127                 }
1128         }
1129 #endif
1130
1131         if (use_interpolate) {
1132                 for (i = 0; i < iface_array_len; i++) {
1133                         if (iface_array[i]) {
1134                                 bm_interp_face_free(iface_array[i], bm);
1135                         }
1136                 }
1137
1138                 BLI_memarena_free(interp_arena);
1139                 MEM_freeN(iface_array);
1140         }
1141
1142         /* we could flag new edges/verts too, is it useful? */
1143         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "faces.out", BM_FACE, ELE_NEW);
1144
1145         /* cheap feature to add depth to the inset */
1146         if (depth != 0.0f) {
1147                 float (*varr_co)[3];
1148                 BMOIter oiter;
1149
1150                 /* we need to re-calculate tagged normals, but for this purpose we can copy tagged verts from the
1151                  * faces they inset from,  */
1152                 for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
1153                         zero_v3(es->e_new->v1->no);
1154                         zero_v3(es->e_new->v2->no);
1155                 }
1156                 for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
1157                         const float *no = es->l->f->no;
1158                         add_v3_v3(es->e_new->v1->no, no);
1159                         add_v3_v3(es->e_new->v2->no, no);
1160                 }
1161                 for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
1162                         /* annoying, avoid normalizing twice */
1163                         if (len_squared_v3(es->e_new->v1->no) != 1.0f) {
1164                                 normalize_v3(es->e_new->v1->no);
1165                         }
1166                         if (len_squared_v3(es->e_new->v2->no) != 1.0f) {
1167                                 normalize_v3(es->e_new->v2->no);
1168                         }
1169                 }
1170                 /* done correcting edge verts normals */
1171
1172                 /* untag verts */
1173                 BM_mesh_elem_hflag_disable_all(bm, BM_VERT, BM_ELEM_TAG, false);
1174
1175                 /* tag face verts */
1176                 BMO_ITER (f, &oiter, op->slots_in, "faces", BM_FACE) {
1177                         BM_ITER_ELEM (v, &iter, f, BM_VERTS_OF_FACE) {
1178                                 BM_elem_flag_enable(v, BM_ELEM_TAG);
1179                         }
1180                 }
1181
1182                 /* do in 2 passes so moving the verts doesn't feed back into face angle checks
1183                  * which BM_vert_calc_shell_factor uses. */
1184
1185                 /* over allocate */
1186                 varr_co = MEM_callocN(sizeof(*varr_co) * bm->totvert, __func__);
1187
1188                 BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
1189                         if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
1190                                 const float fac = (depth *
1191                                                    (use_relative_offset ? bm_edge_info_average_length(v, edge_info) : 1.0f) *
1192                                                    (use_even_boundary   ? BM_vert_calc_shell_factor(v) : 1.0f));
1193                                 madd_v3_v3v3fl(varr_co[i], v->co, v->no, fac);
1194                         }
1195                 }
1196
1197                 BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
1198                         if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
1199                                 copy_v3_v3(v->co, varr_co[i]);
1200                         }
1201                 }
1202                 MEM_freeN(varr_co);
1203         }
1204
1205         MEM_freeN(edge_info);
1206 }