code cleanup: redundant includes and add minor comments.
[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_interpolate     = BMO_slot_bool_get(op->slots_in, "use_interpolate");
394         const float thickness          = BMO_slot_float_get(op->slots_in, "thickness");
395         const float depth              = BMO_slot_float_get(op->slots_in, "depth");
396
397         int edge_info_len = 0;
398
399         BMIter iter;
400         SplitEdgeInfo *edge_info;
401         SplitEdgeInfo *es;
402
403         /* Interpolation Vars */
404         /* an array alligned with faces but only fill items which are used. */
405         InterpFace **iface_array = NULL;
406         int          iface_array_len;
407         MemArena *interp_arena = NULL;
408
409         BMVert *v;
410         BMEdge *e;
411         BMFace *f;
412         int i, j, k;
413
414         if (use_interpolate) {
415                 interp_arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
416                 /* warning, we could be more clever here and not over alloc */
417                 iface_array = MEM_callocN(sizeof(*iface_array) * bm->totface, __func__);
418                 iface_array_len = bm->totface;
419         }
420
421         if (use_outset == false) {
422                 BM_mesh_elem_hflag_disable_all(bm, BM_FACE, BM_ELEM_TAG, false);
423                 BMO_slot_buffer_hflag_enable(bm, op->slots_in, "faces", BM_FACE, BM_ELEM_TAG, false);
424         }
425         else {
426                 BM_mesh_elem_hflag_enable_all(bm, BM_FACE, BM_ELEM_TAG, false);
427                 BMO_slot_buffer_hflag_disable(bm, op->slots_in, "faces", BM_FACE, BM_ELEM_TAG, false);
428         }
429
430         /* first count all inset edges we will split */
431         /* fill in array and initialize tagging */
432         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
433                 if (
434                     /* tag if boundary is enabled */
435                     (use_boundary && BM_edge_is_boundary(e) && BM_elem_flag_test(e->l->f, BM_ELEM_TAG)) ||
436
437                     /* tag if edge is an interior edge inbetween a tagged and untagged face */
438                     (bm_edge_is_mixed_face_tag(e->l)))
439                 {
440                         /* tag */
441                         BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
442                         BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
443                         BM_elem_flag_enable(e, BM_ELEM_TAG);
444
445                         BM_elem_index_set(e, edge_info_len); /* set_dirty! */
446                         edge_info_len++;
447                 }
448                 else {
449                         BM_elem_flag_disable(e->v1, BM_ELEM_TAG);
450                         BM_elem_flag_disable(e->v2, BM_ELEM_TAG);
451                         BM_elem_flag_disable(e, BM_ELEM_TAG);
452
453                         BM_elem_index_set(e, -1); /* set_dirty! */
454                 }
455         }
456         bm->elem_index_dirty |= BM_EDGE;
457
458         edge_info = MEM_mallocN(edge_info_len * sizeof(SplitEdgeInfo), __func__);
459
460         /* fill in array and initialize tagging */
461         es = edge_info;
462         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
463                 i = BM_elem_index_get(e);
464                 if (i != -1) {
465                         /* calc edge-split info */
466                         es->length = BM_edge_calc_length(e);
467                         es->e_old = e;
468                         es++;
469                         /* initialize no and e_new after */
470                 }
471         }
472
473         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
474                 if ((es->l = bm_edge_is_mixed_face_tag(es->e_old->l))) {
475                         /* do nothing */
476                 }
477                 else {
478                         es->l = es->e_old->l; /* must be a boundary */
479                 }
480
481
482                 /* run the separate arg */
483                 bmesh_edge_separate(bm, es->e_old, es->l, false);
484
485                 /* calc edge-split info */
486                 es->e_new = es->l->e;
487                 BM_edge_calc_face_tangent(es->e_new, es->l, es->no);
488
489                 if (es->e_new == es->e_old) { /* happens on boundary edges */
490                         /* take care here, we're creating this double edge which _must_ have its verts replaced later on */
491                         es->e_old = BM_edge_create(bm, es->e_new->v1, es->e_new->v2, es->e_new, BM_CREATE_NOP);
492                 }
493
494                 /* store index back to original in 'edge_info' */
495                 BM_elem_index_set(es->e_new, i);
496                 BM_elem_flag_enable(es->e_new, BM_ELEM_TAG);
497
498                 /* important to tag again here */
499                 BM_elem_flag_enable(es->e_new->v1, BM_ELEM_TAG);
500                 BM_elem_flag_enable(es->e_new->v2, BM_ELEM_TAG);
501
502
503                 /* initialize interpolation vars */
504                 /* this could go in its own loop,
505                  * only use the 'es->l->f' so we don't store loops for faces which have no mixed selection
506                  *
507                  * note: faces on the other side of the inset will be interpolated too since this is hard to
508                  * detect, just allow it even though it will cause some redundant interpolation */
509                 if (use_interpolate) {
510                         BMIter viter;
511                         BM_ITER_ELEM (v, &viter, es->l->e, BM_VERTS_OF_EDGE) {
512                                 BMIter fiter;
513                                 BM_ITER_ELEM (f, &fiter, v, BM_FACES_OF_VERT) {
514                                         const int j = BM_elem_index_get(f);
515                                         if (iface_array[j] == NULL) {
516                                                 InterpFace *iface = BLI_memarena_alloc(interp_arena, sizeof(*iface));
517                                                 bm_interp_face_store(iface, bm, f, interp_arena);
518                                                 iface_array[j] = iface;
519                                         }
520                                 }
521                         }
522                 }
523                 /* done interpolation */
524         }
525
526         /* show edge normals for debugging */
527 #if 0
528         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
529                 float tvec[3];
530                 BMVert *v1, *v2;
531
532                 mid_v3_v3v3(tvec, es->e_new->v1->co, es->e_new->v2->co);
533
534                 v1 = BM_vert_create(bm, tvec, NULL, BM_CREATE_NOP);
535                 v2 = BM_vert_create(bm, tvec, NULL, BM_CREATE_NOP);
536                 madd_v3_v3fl(v2->co, es->no, 0.1f);
537                 BM_edge_create(bm, v1, v2, NULL, 0);
538         }
539 #endif
540
541         /* execute the split and position verts, it would be most obvious to loop over verts
542          * here but don't do this since we will be splitting them off (iterating stuff you modify is bad juju)
543          * instead loop over edges then their verts */
544         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
545                 for (j = 0; j < 2; j++) {
546                         v = (j == 0) ? es->e_new->v1 : es->e_new->v2;
547
548                         /* end confusing part - just pretend this is a typical loop on verts */
549
550                         /* only split of tagged verts - used by separated edges */
551
552                         /* comment the first part because we know this verts in a tagged face */
553                         if (/* v->e && */BM_elem_flag_test(v, BM_ELEM_TAG)) {
554                                 BMVert **vout;
555                                 int r_vout_len;
556                                 BMVert *v_glue = NULL;
557
558                                 /* disable touching twice, this _will_ happen if the flags not disabled */
559                                 BM_elem_flag_disable(v, BM_ELEM_TAG);
560
561                                 bmesh_vert_separate(bm, v, &vout, &r_vout_len, false);
562                                 v = NULL; /* don't use again */
563
564                                 /* in some cases the edge doesn't split off */
565                                 if (r_vout_len == 1) {
566                                         MEM_freeN(vout);
567                                         continue;
568                                 }
569
570                                 for (k = 0; k < r_vout_len; k++) {
571                                         BMVert *v_split = vout[k]; /* only to avoid vout[k] all over */
572
573                                         /* need to check if this vertex is from a */
574                                         int vert_edge_tag_tot = 0;
575                                         int vecpair[2];
576
577                                         /* find adjacent */
578                                         BM_ITER_ELEM (e, &iter, v_split, BM_EDGES_OF_VERT) {
579                                                 if (BM_elem_flag_test(e, BM_ELEM_TAG) &&
580                                                     e->l && BM_elem_flag_test(e->l->f, BM_ELEM_TAG))
581                                                 {
582                                                         if (vert_edge_tag_tot < 2) {
583                                                                 vecpair[vert_edge_tag_tot] = BM_elem_index_get(e);
584                                                                 BLI_assert(vecpair[vert_edge_tag_tot] != -1);
585                                                         }
586
587                                                         vert_edge_tag_tot++;
588                                                 }
589                                         }
590
591                                         if (vert_edge_tag_tot != 0) {
592                                                 float tvec[3];
593
594                                                 if (vert_edge_tag_tot >= 2) { /* 2 edge users - common case */
595                                                         /* now there are 2 cases to check for,
596                                                          *
597                                                          * if both edges use the same face OR both faces have the same normal,
598                                                          * ...then we can calculate an edge that fits nicely between the 2 edge normals.
599                                                          *
600                                                          * Otherwise use the shared edge OR the corner defined by these 2 face normals,
601                                                          * when both edges faces are adjacent this works best but even when this vertex
602                                                          * fans out faces it should work ok.
603                                                          */
604
605                                                         SplitEdgeInfo *e_info_a = &edge_info[vecpair[0]];
606                                                         SplitEdgeInfo *e_info_b = &edge_info[vecpair[1]];
607
608                                                         BMFace *f_a = e_info_a->l->f;
609                                                         BMFace *f_b = e_info_b->l->f;
610
611                                                         /* we use this as either the normal OR to find the right direction for the
612                                                          * cross product between both face normals */
613                                                         add_v3_v3v3(tvec, e_info_a->no, e_info_b->no);
614
615                                                         /* epsilon increased to fix [#32329] */
616                                                         if ((f_a == f_b) || compare_v3v3(f_a->no, f_b->no, 0.001f)) {
617                                                                 normalize_v3(tvec);
618                                                         }
619                                                         else {
620                                                                 /* these lookups are very quick */
621                                                                 BMLoop *l_other_a = BM_loop_other_vert_loop(e_info_a->l, v_split);
622                                                                 BMLoop *l_other_b = BM_loop_other_vert_loop(e_info_b->l, v_split);
623
624                                                                 if (l_other_a->v == l_other_b->v) {
625                                                                         /* both edges faces are adjacent, but we don't need to know the shared edge
626                                                                          * having both verts is enough. */
627                                                                         sub_v3_v3v3(tvec, l_other_a->v->co, v_split->co);
628                                                                 }
629                                                                 else {
630                                                                         /* faces don't touch,
631                                                                          * just get cross product of their normals, its *good enough*
632                                                                          */
633                                                                         float tno[3];
634                                                                         cross_v3_v3v3(tno, f_a->no, f_b->no);
635                                                                         if (dot_v3v3(tvec, tno) < 0.0f) {
636                                                                                 negate_v3(tno);
637                                                                         }
638                                                                         copy_v3_v3(tvec, tno);
639                                                                 }
640
641                                                                 normalize_v3(tvec);
642                                                         }
643
644                                                         /* scale by edge angle */
645                                                         if (use_even_offset) {
646                                                                 mul_v3_fl(tvec, shell_angle_to_dist(angle_normalized_v3v3(e_info_a->no,
647                                                                                                                           e_info_b->no) / 2.0f));
648                                                         }
649
650                                                         /* scale relative to edge lengths */
651                                                         if (use_relative_offset) {
652                                                                 mul_v3_fl(tvec, (edge_info[vecpair[0]].length + edge_info[vecpair[1]].length) / 2.0f);
653                                                         }
654                                                 }
655                                                 else if (vert_edge_tag_tot == 1) { /* 1 edge user - boundary vert, not so common */
656                                                         const float *e_no_a = edge_info[vecpair[0]].no;
657
658                                                         if (use_even_boundry) {
659
660                                                                 /* This case where only one edge attached to v_split
661                                                                  * is used - ei - the face to inset is on a boundary.
662                                                                  *
663                                                                  *                  We want the inset to align flush with the
664                                                                  *                  boundary edge, not the normal of the interior
665                                                                  *             <--- edge which would give an unsightly bump.
666                                                                  * --+-------------------------+---------------+--
667                                                                  *   |^v_other    ^e_other    /^v_split        |
668                                                                  *   |                       /                 |
669                                                                  *   |                      /                  |
670                                                                  *   |                     / <- tag split edge |
671                                                                  *   |                    /                    |
672                                                                  *   |                   /                     |
673                                                                  *   |                  /                      |
674                                                                  * --+-----------------+-----------------------+--
675                                                                  *   |                                         |
676                                                                  *   |                                         |
677                                                                  *
678                                                                  * note, the fact we are doing location comparisons on verts that are moved about
679                                                                  * doesn't matter because the direction will remain the same in this case.
680                                                                  */
681
682                                                                 BMEdge *e_other;
683                                                                 BMVert *v_other;
684                                                                 /* loop will always be either next of prev */
685                                                                 BMLoop *l = v_split->e->l;
686                                                                 if (l->prev->v == v_split) {
687                                                                         l = l->prev;
688                                                                 }
689                                                                 else if (l->next->v == v_split) {
690                                                                         l = l->next;
691                                                                 }
692                                                                 else if (l->v == v_split) {
693                                                                         /* pass */
694                                                                 }
695                                                                 else {
696                                                                         /* should never happen */
697                                                                         BLI_assert(0);
698                                                                 }
699
700                                                                 /* find the edge which is _not_ being split here */
701                                                                 if (!BM_elem_flag_test(l->e, BM_ELEM_TAG)) {
702                                                                         e_other = l->e;
703                                                                 }
704                                                                 else if (!BM_elem_flag_test(l->prev->e, BM_ELEM_TAG)) {
705                                                                         e_other = l->prev->e;
706                                                                 }
707                                                                 else {
708                                                                         BLI_assert(0);
709                                                                         e_other = NULL;
710                                                                 }
711
712                                                                 v_other = BM_edge_other_vert(e_other, v_split);
713                                                                 sub_v3_v3v3(tvec, v_other->co, v_split->co);
714                                                                 normalize_v3(tvec);
715
716                                                                 if (use_even_offset) {
717                                                                         mul_v3_fl(tvec, shell_angle_to_dist(angle_normalized_v3v3(e_no_a, tvec)));
718                                                                 }
719                                                         }
720                                                         else {
721                                                                 copy_v3_v3(tvec, e_no_a);
722                                                         }
723
724                                                         /* use_even_offset - doesn't apply here */
725
726                                                         /* scale relative to edge length */
727                                                         if (use_relative_offset) {
728                                                                 mul_v3_fl(tvec, edge_info[vecpair[0]].length);
729                                                         }
730                                                 }
731                                                 else {
732                                                         /* should never happen */
733                                                         BLI_assert(0);
734                                                         zero_v3(tvec);
735                                                 }
736
737                                                 /* apply the offset */
738                                                 madd_v3_v3fl(v_split->co, tvec, thickness);
739                                         }
740
741                                         /* this saves expensive/slow glue check for common cases */
742                                         if (r_vout_len > 2) {
743                                                 bool ok = true;
744                                                 /* last step, NULL this vertex if has a tagged face */
745                                                 BM_ITER_ELEM (f, &iter, v_split, BM_FACES_OF_VERT) {
746                                                         if (BM_elem_flag_test(f, BM_ELEM_TAG)) {
747                                                                 ok = false;
748                                                                 break;
749                                                         }
750                                                 }
751
752                                                 if (ok) {
753                                                         if (v_glue == NULL) {
754                                                                 v_glue = v_split;
755                                                         }
756                                                         else {
757                                                                 BM_vert_splice(bm, v_split, v_glue);
758                                                         }
759                                                 }
760                                         }
761                                         /* end glue */
762
763                                 }
764                                 MEM_freeN(vout);
765                         }
766                 }
767         }
768
769         if (use_interpolate) {
770                 for (i = 0; i < iface_array_len; i++) {
771                         if (iface_array[i]) {
772                                 InterpFace *iface = iface_array[i];
773                                 BM_face_interp_from_face_ex(bm, iface->f, iface->f, true,
774                                                             iface->blocks_l, iface->blocks_v, iface->cos_2d, iface->axis_mat);
775                         }
776                 }
777         }
778
779         /* create faces */
780         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
781                 BMVert *varr[4] = {NULL};
782                 /* get the verts in the correct order */
783                 BM_edge_ordered_verts_ex(es->e_new, &varr[1], &varr[0], es->l);
784 #if 0
785                 if (varr[0] == es->e_new->v1) {
786                         varr[2] = es->e_old->v2;
787                         varr[3] = es->e_old->v1;
788                 }
789                 else {
790                         varr[2] = es->e_old->v1;
791                         varr[3] = es->e_old->v2;
792                 }
793                 j = 4;
794 #else
795                 /* slightly trickier check - since we can't assume the verts are split */
796                 j = 2; /* 2 edges are set */
797                 if (varr[0] == es->e_new->v1) {
798                         if (es->e_old->v2 != es->e_new->v2) { varr[j++] = es->e_old->v2; }
799                         if (es->e_old->v1 != es->e_new->v1) { varr[j++] = es->e_old->v1; }
800                 }
801                 else {
802                         if (es->e_old->v1 != es->e_new->v1) { varr[j++] = es->e_old->v1; }
803                         if (es->e_old->v2 != es->e_new->v2) { varr[j++] = es->e_old->v2; }
804                 }
805
806                 if (j == 2) {
807                         /* can't make face! */
808                         continue;
809                 }
810 #endif
811                 /* no need to check doubles, we KNOW there won't be any */
812                 /* yes - reverse face is correct in this case */
813                 f = BM_face_create_verts(bm, varr, j, es->l->f, BM_CREATE_NOP, true);
814                 BMO_elem_flag_enable(bm, f, ELE_NEW);
815
816                 /* copy for loop data, otherwise UV's and vcols are no good.
817                  * tiny speedup here we could be more clever and copy from known adjacent data
818                  * also - we could attempt to interpolate the loop data, this would be much slower but more useful too */
819 #if 0
820                 /* don't use this because face boundaries have no adjacent loops and won't be filled in.
821                  * instead copy from the opposite side with the code below */
822                 BM_face_copy_shared(bm, f, NULL, NULL);
823 #else
824                 {
825                         /* 2 inner loops on the edge between the new face and the original */
826                         BMLoop *l_a;
827                         BMLoop *l_b;
828                         BMLoop *l_a_other;
829                         BMLoop *l_b_other;
830
831                         l_a = BM_FACE_FIRST_LOOP(f);
832                         l_b = l_a->next;
833
834                         /* we know this side has a radial_next because of the order of created verts in the quad */
835                         l_a_other = BM_edge_other_loop(l_a->e, l_a);
836                         l_b_other = BM_edge_other_loop(l_a->e, l_b);
837                         BM_elem_attrs_copy(bm, bm, l_a_other, l_a);
838                         BM_elem_attrs_copy(bm, bm, l_b_other, l_b);
839
840                         /* step around to the opposite side of the quad - warning, this may have no other edges! */
841                         l_a = l_a->next->next;
842                         l_b = l_a->next;
843
844                         /* swap a<->b intentionally */
845                         if (use_interpolate) {
846                                 InterpFace *iface = iface_array[BM_elem_index_get(es->l->f)];
847                                 const int i_a = BM_elem_index_get(l_a_other);
848                                 const int i_b = BM_elem_index_get(l_b_other);
849                                 CustomData_bmesh_copy_data(&bm->ldata, &bm->ldata, iface->blocks_l[i_a], &l_b->head.data);
850                                 CustomData_bmesh_copy_data(&bm->ldata, &bm->ldata, iface->blocks_l[i_b], &l_a->head.data);
851                         }
852                         else {
853                                 BM_elem_attrs_copy(bm, bm, l_a_other, l_b);
854                                 BM_elem_attrs_copy(bm, bm, l_b_other, l_a);
855                         }
856                 }
857         }
858 #endif
859
860         if (use_interpolate) {
861                 for (i = 0; i < iface_array_len; i++) {
862                         if (iface_array[i]) {
863                                 bm_interp_face_free(iface_array[i], bm);
864                         }
865                 }
866
867                 BLI_memarena_free(interp_arena);
868                 MEM_freeN(iface_array);
869         }
870
871         /* we could flag new edges/verts too, is it useful? */
872         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "faces.out", BM_FACE, ELE_NEW);
873
874         /* cheap feature to add depth to the inset */
875         if (depth != 0.0f) {
876                 float (*varr_co)[3];
877                 BMOIter oiter;
878
879                 /* we need to re-calculate tagged normals, but for this purpose we can copy tagged verts from the
880                  * faces they inset from,  */
881                 for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
882                         zero_v3(es->e_new->v1->no);
883                         zero_v3(es->e_new->v2->no);
884                 }
885                 for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
886                         float *no = es->l->f->no;
887                         add_v3_v3(es->e_new->v1->no, no);
888                         add_v3_v3(es->e_new->v2->no, no);
889                 }
890                 for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
891                         /* annoying, avoid normalizing twice */
892                         if (len_squared_v3(es->e_new->v1->no) != 1.0f) {
893                                 normalize_v3(es->e_new->v1->no);
894                         }
895                         if (len_squared_v3(es->e_new->v2->no) != 1.0f) {
896                                 normalize_v3(es->e_new->v2->no);
897                         }
898                 }
899                 /* done correcting edge verts normals */
900
901                 /* untag verts */
902                 BM_mesh_elem_hflag_disable_all(bm, BM_VERT, BM_ELEM_TAG, false);
903
904                 /* tag face verts */
905                 BMO_ITER (f, &oiter, op->slots_in, "faces", BM_FACE) {
906                         BM_ITER_ELEM (v, &iter, f, BM_VERTS_OF_FACE) {
907                                 BM_elem_flag_enable(v, BM_ELEM_TAG);
908                         }
909                 }
910
911                 /* do in 2 passes so moving the verts doesn't feed back into face angle checks
912                  * which BM_vert_calc_shell_factor uses. */
913
914                 /* over allocate */
915                 varr_co = MEM_callocN(sizeof(*varr_co) * bm->totvert, __func__);
916
917                 BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
918                         if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
919                                 const float fac = (depth *
920                                                    (use_relative_offset ? bm_edge_info_average_length(v, edge_info) : 1.0f) *
921                                                    (use_even_boundry    ? BM_vert_calc_shell_factor(v) : 1.0f));
922                                 madd_v3_v3v3fl(varr_co[i], v->co, v->no, fac);
923                         }
924                 }
925
926                 BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
927                         if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
928                                 copy_v3_v3(v->co, varr_co[i]);
929                         }
930                 }
931                 MEM_freeN(varr_co);
932         }
933
934         MEM_freeN(edge_info);
935 }