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