4a426b40995e93a22795aa4455bb55595f181dcb
[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 #include "MEM_guardedalloc.h"
24
25 #include "BLI_math.h"
26
27 #include "bmesh.h"
28
29 #include "intern/bmesh_operators_private.h" /* own include */
30
31 #define ELE_NEW         1
32
33 typedef struct SplitEdgeInfo {
34         float   no[3];
35         float   length;
36         BMEdge *e_old;
37         BMEdge *e_new;
38         BMLoop *l;
39 } SplitEdgeInfo;
40
41 static void edge_loop_tangent(BMEdge *e, BMLoop *e_loop, float r_no[3])
42 {
43         float tvec[3];
44         BMVert *v1, *v2;
45         BM_edge_ordered_verts_ex(e, &v1, &v2, e_loop);
46
47         sub_v3_v3v3(tvec, v1->co, v2->co); /* use for temp storage */
48         cross_v3_v3v3(r_no, tvec, e_loop->f->no);
49         normalize_v3(r_no);
50 }
51
52 /**
53  * return the tag loop where there is...
54  * - only 1 tagged face attached to this edge.
55  * - 1 or more untagged faces.
56  *
57  * \note this function looks to be expensive
58  * but in most cases it will only do 2 iterations.
59  */
60 static BMLoop *bm_edge_is_mixed_face_tag(BMLoop *l)
61 {
62         if (LIKELY(l != NULL)) {
63                 int tot_tag = 0;
64                 int tot_untag = 0;
65                 BMLoop *l_iter;
66                 BMLoop *l_tag = NULL;
67                 l_iter = l;
68                 do {
69                         if (BM_elem_flag_test(l_iter->f, BM_ELEM_TAG)) {
70                                 /* more then one tagged face - bail out early! */
71                                 if (tot_tag == 1) {
72                                         return NULL;
73                                 }
74                                 l_tag = l_iter;
75                                 tot_tag++;
76                         }
77                         else {
78                                 tot_untag++;
79                         }
80
81                 } while ((l_iter = l_iter->radial_next) != l);
82
83                 return ((tot_tag == 1) && (tot_untag >= 1)) ? l_tag : NULL;
84         }
85         else {
86                 return NULL;
87         }
88 }
89
90
91 /**
92  * implementation is as follows...
93  *
94  * - set all faces as tagged/untagged based on selection.
95  * - find all edges that have 1 tagged, 1 untagged face.
96  * - separate these edges and tag vertices, set their index to point to the original edge.
97  * - build faces between old/new edges.
98  * - inset the new edges into their faces.
99  */
100
101 void bmo_inset_exec(BMesh *bm, BMOperator *op)
102 {
103         const int use_outset          = BMO_slot_bool_get(op, "use_outset");
104         const int use_boundary        = BMO_slot_bool_get(op, "use_boundary") && (use_outset == FALSE);
105         const int use_even_offset     = BMO_slot_bool_get(op, "use_even_offset");
106         const int use_even_boundry    = use_even_offset; /* could make own option */
107         const int use_relative_offset = BMO_slot_bool_get(op, "use_relative_offset");
108         const float thickness = BMO_slot_float_get(op, "thickness");
109
110         int edge_info_len = 0;
111
112         BMIter iter;
113         SplitEdgeInfo *edge_info;
114         SplitEdgeInfo *es;
115
116         BMVert *v;
117         BMEdge *e;
118         BMFace *f;
119         int i, j, k;
120
121         if (use_outset == FALSE) {
122                 BM_mesh_elem_flag_disable_all(bm, BM_FACE, BM_ELEM_TAG);
123                 BMO_slot_buffer_hflag_enable(bm, op, "faces", BM_FACE, BM_ELEM_TAG, FALSE);
124         }
125         else {
126                 BM_mesh_elem_flag_enable_all(bm, BM_FACE, BM_ELEM_TAG);
127                 BMO_slot_buffer_hflag_disable(bm, op, "faces", BM_FACE, BM_ELEM_TAG, FALSE);
128         }
129
130         /* first count all inset edges we will split */
131         /* fill in array and initialize tagging */
132         BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
133                 if (
134                     /* tag if boundary is enabled */
135                     (use_boundary && BM_edge_is_boundary(e) && BM_elem_flag_test(e->l->f, BM_ELEM_TAG)) ||
136
137                     /* tag if edge is an interior edge inbetween a tagged and untagged face */
138                     (bm_edge_is_mixed_face_tag(e->l)))
139                 {
140                         /* tag */
141                         BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
142                         BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
143                         BM_elem_flag_enable(e, BM_ELEM_TAG);
144
145                         BM_elem_index_set(e, edge_info_len); /* set_dirty! */
146                         edge_info_len++;
147                 }
148                 else {
149                         BM_elem_flag_disable(e->v1, BM_ELEM_TAG);
150                         BM_elem_flag_disable(e->v2, BM_ELEM_TAG);
151                         BM_elem_flag_disable(e, BM_ELEM_TAG);
152
153                         BM_elem_index_set(e, -1); /* set_dirty! */
154                 }
155         }
156         bm->elem_index_dirty |= BM_EDGE;
157
158         edge_info = MEM_mallocN(edge_info_len * sizeof(SplitEdgeInfo), __func__);
159
160         /* fill in array and initialize tagging */
161         es = edge_info;
162         BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
163                 i = BM_elem_index_get(e);
164                 if (i != -1) {
165                         /* calc edge-split info */
166                         es->length = BM_edge_length_calc(e);
167                         es->e_old = e;
168                         es++;
169                         /* initialize no and e_new after */
170                 }
171         }
172
173         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
174                 if ((es->l = bm_edge_is_mixed_face_tag(es->e_old->l))) {
175                         /* do nothing */
176                 }
177                 else {
178                         es->l = es->e_old->l; /* must be a boundary */
179                 }
180
181
182                 /* run the separate arg */
183                 bmesh_edge_separate(bm, es->e_old, es->l);
184
185                 /* calc edge-split info */
186                 es->e_new = es->l->e;
187                 edge_loop_tangent(es->e_new, es->l, es->no);
188
189                 if (es->e_new == es->e_old) { /* happens on boundary edges */
190                         es->e_old = BM_edge_create(bm, es->e_new->v1, es->e_new->v2, es->e_new, FALSE);
191                 }
192
193                 /* store index back to original in 'edge_info' */
194                 BM_elem_index_set(es->e_new, i);
195                 BM_elem_flag_enable(es->e_new, BM_ELEM_TAG);
196
197                 /* important to tag again here */
198                 BM_elem_flag_enable(es->e_new->v1, BM_ELEM_TAG);
199                 BM_elem_flag_enable(es->e_new->v2, BM_ELEM_TAG);
200         }
201
202
203         /* show edge normals for debugging */
204 #if 0
205         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
206                 float tvec[3];
207                 BMVert *v1, *v2;
208
209                 mid_v3_v3v3(tvec, es->e_new->v1->co, es->e_new->v2->co);
210
211                 v1 = BM_vert_create(bm, tvec, NULL);
212                 v2 = BM_vert_create(bm, tvec, NULL);
213                 madd_v3_v3fl(v2->co, es->no, 0.1f);
214                 BM_edge_create(bm, v1, v2, NULL, FALSE);
215         }
216 #endif
217
218         /* execute the split and position verts, it would be most obvious to loop over verts
219          * here but don't do this since we will be splitting them off (iterating stuff you modify is bad juju)
220          * instead loop over edges then their verts */
221         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
222                 for (j = 0; j < 2; j++) {
223                         v = (j == 0) ? es->e_new->v1 : es->e_new->v2;
224
225                         /* end confusing part - just pretend this is a typical loop on verts */
226
227                         /* only split of tagged verts - used by separated edges */
228
229                         /* comment the first part because we know this verts in a tagged face */
230                         if (/* v->e && */BM_elem_flag_test(v, BM_ELEM_TAG)) {
231                                 BMVert **vout;
232                                 int r_vout_len;
233                                 BMVert *v_glue = NULL;
234
235                                 /* disable touching twice, this _will_ happen if the flags not disabled */
236                                 BM_elem_flag_disable(v, BM_ELEM_TAG);
237
238                                 bmesh_vert_separate(bm, v, &vout, &r_vout_len);
239                                 v = NULL; /* don't use again */
240
241                                 /* in some cases the edge doesnt split off */
242                                 if (r_vout_len == 1) {
243                                         MEM_freeN(vout);
244                                         continue;
245                                 }
246
247                                 for (k = 0; k < r_vout_len; k++) {
248                                         BMVert *v_split = vout[k]; /* only to avoid vout[k] all over */
249
250                                         /* need to check if this vertex is from a */
251                                         int vert_edge_tag_tot = 0;
252                                         int vecpair[2];
253
254                                         /* find adjacent */
255                                         BM_ITER(e, &iter, bm, BM_EDGES_OF_VERT, v_split) {
256                                                 if (BM_elem_flag_test(e, BM_ELEM_TAG) &&
257                                                     BM_elem_flag_test(e->l->f, BM_ELEM_TAG))
258                                                 {
259                                                         if (vert_edge_tag_tot < 2) {
260                                                                 vecpair[vert_edge_tag_tot] = BM_elem_index_get(e);
261                                                                 BLI_assert(vecpair[vert_edge_tag_tot] != -1);
262                                                         }
263
264                                                         vert_edge_tag_tot++;
265                                                 }
266                                         }
267
268                                         if (vert_edge_tag_tot != 0) {
269                                                 float tvec[3];
270
271                                                 if (vert_edge_tag_tot >= 2) { /* 2 edge users - common case */
272                                                         /* now there are 2 cases to check for,
273                                                          *
274                                                          * if both edges use the same face OR both faces have the same normal,
275                                                          * ...then we can calculate an edge that fits nicely between the 2 edge normals.
276                                                          *
277                                                          * Otherwise use the shared edge OR the corner defined by these 2 face normals,
278                                                          * when both edges faces are adjacent this works best but even when this vertex
279                                                          * fans out faces it should work ok.
280                                                          */
281
282                                                         SplitEdgeInfo *e_info_a = &edge_info[vecpair[0]];
283                                                         SplitEdgeInfo *e_info_b = &edge_info[vecpair[1]];
284
285                                                         BMFace *f_a = e_info_a->l->f;
286                                                         BMFace *f_b = e_info_b->l->f;
287
288                                                         /* we use this as either the normal OR to find the right direction for the
289                                                          * crpss product between both face normals */
290                                                         add_v3_v3v3(tvec, e_info_a->no, e_info_b->no);
291
292                                                         if ((f_a == f_b) || compare_v3v3(f_a->no, f_b->no, 0.00001f)) {
293                                                                 normalize_v3(tvec);
294                                                         }
295                                                         else {
296                                                                 /* these lookups are very quick */
297                                                                 BMLoop *l_other_a = BM_loop_other_vert_loop(e_info_a->l, v_split);
298                                                                 BMLoop *l_other_b = BM_loop_other_vert_loop(e_info_b->l, v_split);
299
300                                                                 if (l_other_a->v == l_other_b->v) {
301                                                                         /* both edges faces are adjacent, but we don't need to know the shared edge
302                                                                          * having both verts is enough. */
303                                                                         sub_v3_v3v3(tvec, l_other_a->v->co, v_split->co);
304                                                                 }
305                                                                 else {
306                                                                         /* faces don't touch,
307                                                                          * just get cross product of their normals, its *good enough*
308                                                                          */
309                                                                         float tno[3];
310                                                                         cross_v3_v3v3(tno, f_a->no, f_b->no);
311                                                                         if (dot_v3v3(tvec, tno) < 0.0f) {
312                                                                                 negate_v3(tno);
313                                                                         }
314                                                                         copy_v3_v3(tvec, tno);
315                                                                 }
316
317                                                                 normalize_v3(tvec);
318                                                         }
319
320                                                         /* scale by edge angle */
321                                                         if (use_even_offset) {
322                                                                 mul_v3_fl(tvec, shell_angle_to_dist(angle_normalized_v3v3(e_info_a->no, e_info_b->no) / 2.0f));
323                                                         }
324
325                                                         /* scale relative to edge lengths */
326                                                         if (use_relative_offset) {
327                                                                 mul_v3_fl(tvec, (edge_info[vecpair[0]].length + edge_info[vecpair[1]].length) / 2.0f);
328                                                         }
329                                                 }
330                                                 else if (vert_edge_tag_tot == 1) { /* 1 edge user - boundary vert, not so common */
331                                                         const float *e_no_a = edge_info[vecpair[0]].no;
332
333                                                         if (use_even_boundry) {
334
335                                                                 /* This case where only one edge attached to v_split
336                                                                  * is used - ei - the face to inset is on a boundary.
337                                                                  *
338                                                                  *                  We want the inset to align flush with the
339                                                                  *                  boundary edge, not the normal of the interior
340                                                                  *             <--- edge which would give an unsightly bump.
341                                                                  * --+-------------------------+---------------+--
342                                                                  *   |^v_other    ^e_other    /^v_split        |
343                                                                  *   |                       /                 |
344                                                                  *   |                      /                  |
345                                                                  *   |                     / <- tag split edge |
346                                                                  *   |                    /                    |
347                                                                  *   |                   /                     |
348                                                                  *   |                  /                      |
349                                                                  * --+-----------------+-----------------------+--
350                                                                  *   |                                         |
351                                                                  *   |                                         |
352                                                                  *
353                                                                  * note, the fact we are doing location comparisons on verts that are moved about
354                                                                  * doesn’t matter because the direction will remain the same in this case.
355                                                                  */
356
357                                                                 BMEdge *e_other;
358                                                                 BMVert *v_other;
359                                                                 /* loop will always be either next of prev */
360                                                                 BMLoop *l = v_split->e->l;
361                                                                 if (l->prev->v == v_split) {
362                                                                         l = l->prev;
363                                                                 }
364                                                                 else if (l->next->v == v_split) {
365                                                                         l = l->next;
366                                                                 }
367                                                                 else if (l->v == v_split) {
368                                                                         /* pass */
369                                                                 }
370                                                                 else {
371                                                                         /* should never happen */
372                                                                         BLI_assert(0);
373                                                                 }
374
375                                                                 /* find the edge which is _not_ being split here */
376                                                                 if (!BM_elem_flag_test(l->e, BM_ELEM_TAG)) {
377                                                                         e_other = l->e;
378                                                                 }
379                                                                 else if (!BM_elem_flag_test(l->prev->e, BM_ELEM_TAG)) {
380                                                                         e_other = l->prev->e;
381                                                                 }
382                                                                 else {
383                                                                         BLI_assert(0);
384                                                                         e_other = NULL;
385                                                                 }
386
387                                                                 v_other = BM_edge_other_vert(e_other, v_split);
388                                                                 sub_v3_v3v3(tvec, v_other->co, v_split->co);
389                                                                 normalize_v3(tvec);
390
391                                                                 if (use_even_offset) {
392                                                                         mul_v3_fl(tvec, shell_angle_to_dist(angle_normalized_v3v3(e_no_a, tvec)));
393                                                                 }
394                                                         }
395                                                         else {
396                                                                 copy_v3_v3(tvec, e_no_a);
397                                                         }
398
399                                                         /* use_even_offset - doesn't apply here */
400
401                                                         /* scale relative to edge length */
402                                                         if (use_relative_offset) {
403                                                                 mul_v3_fl(tvec, edge_info[vecpair[0]].length);
404                                                         }
405                                                 }
406                                                 else {
407                                                         /* should never happen */
408                                                         BLI_assert(0);
409                                                         zero_v3(tvec);
410                                                 }
411
412                                                 /* apply the offset */
413                                                 madd_v3_v3fl(v_split->co, tvec, thickness);
414                                         }
415
416                                         /* this saves expensive/slow glue check for common cases */
417                                         if (r_vout_len > 2) {
418                                                 int ok = TRUE;
419                                                 /* last step, NULL this vertex if has a tagged face */
420                                                 BM_ITER(f, &iter, bm, BM_FACES_OF_VERT, v_split) {
421                                                         if (BM_elem_flag_test(f, BM_ELEM_TAG)) {
422                                                                 ok = FALSE;
423                                                                 break;
424                                                         }
425                                                 }
426
427                                                 if (ok) {
428                                                         if (v_glue == NULL) {
429                                                                 v_glue = v_split;
430                                                         }
431                                                         else {
432                                                                 BM_vert_splice(bm, v_split, v_glue);
433                                                         }
434                                                 }
435                                         }
436                                         /* end glue */
437
438                                 }
439                                 MEM_freeN(vout);
440                         }
441                 }
442         }
443
444         /* create faces */
445         for (i = 0, es = edge_info; i < edge_info_len; i++, es++) {
446                 BMVert *varr[4] = {NULL};
447                 /* get the verts in the correct order */
448                 BM_edge_ordered_verts_ex(es->e_new, &varr[1], &varr[0], es->l);
449 #if 0
450                 if (varr[0] == es->e_new->v1) {
451                         varr[2] = es->e_old->v2;
452                         varr[3] = es->e_old->v1;
453                 }
454                 else {
455                         varr[2] = es->e_old->v1;
456                         varr[3] = es->e_old->v2;
457                 }
458                 j = 4;
459 #else
460                 /* slightly trickier check - since we can't assume the verts are split */
461                 j = 2; /* 2 edges are set */
462                 if (varr[0] == es->e_new->v1) {
463                         if (es->e_old->v2 != es->e_new->v2) { varr[j++] = es->e_old->v2; }
464                         if (es->e_old->v1 != es->e_new->v1) { varr[j++] = es->e_old->v1; }
465                 }
466                 else {
467                         if (es->e_old->v1 != es->e_new->v1) { varr[j++] = es->e_old->v1; }
468                         if (es->e_old->v2 != es->e_new->v2) { varr[j++] = es->e_old->v2; }
469                 }
470
471                 if (j == 2) {
472                         /* can't make face! */
473                         continue;
474                 }
475 #endif
476                 /* no need to check doubles, we KNOW there won't be any */
477                 /* yes - reverse face is correct in this case */
478                 f = BM_face_create_quad_tri_v(bm, varr, j, es->l->f, FALSE);
479                 BMO_elem_flag_enable(bm, f, ELE_NEW);
480         }
481
482         MEM_freeN(edge_info);
483
484         /* we could flag new edges/verts too, is it useful? */
485         BMO_slot_buffer_from_enabled_flag(bm, op, "faceout", BM_FACE, ELE_NEW);
486 }