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