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