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