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