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