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