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