code cleanup: make bmesh operator names more consistant since python has access to...
[blender.git] / source / blender / bmesh / operators / bmo_wireframe.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_wireframe.c
24  *  \ingroup bmesh
25  */
26
27 #include "MEM_guardedalloc.h"
28
29 #include "BLI_math.h"
30
31 #include "BKE_customdata.h"
32
33 #include "bmesh.h"
34
35 #include "intern/bmesh_operators_private.h" /* own include */
36
37 static BMLoop *bm_edge_tag_faceloop(BMEdge *e)
38 {
39         BMLoop *l, *l_first;
40
41         l = l_first = e->l;
42         do {
43                 if (BM_elem_flag_test(l->f, BM_ELEM_TAG)) {
44                         return l;
45                 }
46         } while ((l = l->radial_next) != l_first);
47
48         /* in the case this is used, we know this will never happen */
49         return NULL;
50 }
51
52 static void bm_vert_boundary_tangent(BMVert *v, float r_no[3], float r_no_face[3],
53                                      BMVert **r_va_other, BMVert **r_vb_other)
54 {
55         BMIter iter;
56         BMEdge *e_iter;
57
58         BMEdge *e_a = NULL, *e_b = NULL;
59         BMVert *v_a, *v_b;
60
61         BMLoop *l_a, *l_b;
62
63         float no_face[3], no_edge[3];
64         float tvec_a[3], tvec_b[3];
65
66         /* get 2 boundary edges, there should only _be_ 2,
67          * in case there are more - results wont be valid of course */
68         BM_ITER_ELEM (e_iter, &iter, v, BM_EDGES_OF_VERT) {
69                 if (BM_elem_flag_test(e_iter, BM_ELEM_TAG)) {
70                         if (e_a == NULL) {
71                                 e_a = e_iter;
72                         }
73                         else {
74                                 e_b = e_iter;
75                                 break;
76                         }
77                 }
78         }
79
80         if (e_a && e_b) {
81                 l_a = bm_edge_tag_faceloop(e_a);
82                 l_b = bm_edge_tag_faceloop(e_b);
83
84                 /* average edge face normal */
85                 add_v3_v3v3(no_face, l_a->f->no, l_b->f->no);
86
87                 /* average edge direction */
88                 v_a = BM_edge_other_vert(e_a, v);
89                 v_b = BM_edge_other_vert(e_b, v);
90
91                 sub_v3_v3v3(tvec_a, v->co, v_a->co);
92                 sub_v3_v3v3(tvec_b, v_b->co, v->co);
93                 normalize_v3(tvec_a);
94                 normalize_v3(tvec_b);
95                 add_v3_v3v3(no_edge, tvec_a, tvec_b); /* not unit length but this is ok */
96
97                 /* check are we flipped the right way */
98                 BM_edge_calc_face_tangent(e_a, l_a, tvec_a);
99                 BM_edge_calc_face_tangent(e_b, l_b, tvec_b);
100                 add_v3_v3(tvec_a, tvec_b);
101
102                 *r_va_other = v_a;
103                 *r_vb_other = v_b;
104         }
105         else {
106                 /* degenerate case - vertex connects a boundary edged face to other faces,
107                  * so we have only one boundary face - only use it for calculations */
108                 l_a = bm_edge_tag_faceloop(e_a);
109
110                 copy_v3_v3(no_face, l_a->f->no);
111
112                 /* edge direction */
113                 v_a = BM_edge_other_vert(e_a, v);
114                 v_b = NULL;
115
116                 sub_v3_v3v3(no_edge, v->co, v_a->co);
117
118                 /* check are we flipped the right way */
119                 BM_edge_calc_face_tangent(e_a, l_a, tvec_a);
120
121                 *r_va_other = NULL;
122                 *r_vb_other = NULL;
123         }
124
125         /* find the normal */
126         cross_v3_v3v3(r_no, no_edge, no_face);
127         normalize_v3(r_no);
128
129         if (dot_v3v3(r_no, tvec_a) > 0.0f) {
130                 negate_v3(r_no);
131         }
132
133         copy_v3_v3(r_no_face, no_face);
134 }
135
136 /* check if we are the only tagged loop-face around this edge */
137 static int bm_loop_is_radial_boundary(BMLoop *l_first)
138 {
139         BMLoop *l = l_first->radial_next;
140
141         if (l == l_first) {
142                 return TRUE; /* a real boundary */
143         }
144         else {
145                 do {
146                         if (BM_elem_flag_test(l->f, BM_ELEM_TAG)) {
147                                 return FALSE;
148                         }
149                 } while ((l = l->radial_next) != l_first);
150         }
151         return TRUE;
152 }
153
154 extern float BM_vert_calc_mean_tagged_edge_length(BMVert *v);
155
156 void bmo_wireframe_exec(BMesh *bm, BMOperator *op)
157 {
158         const int use_boundary        = BMO_slot_bool_get(op->slots_in,  "use_boundary");
159         const int use_even_offset     = BMO_slot_bool_get(op->slots_in,  "use_even_offset");
160         const int use_relative_offset = BMO_slot_bool_get(op->slots_in,  "use_relative_offset");
161         const int use_crease          = (BMO_slot_bool_get(op->slots_in,  "use_crease") &&
162                                          CustomData_has_layer(&bm->edata, CD_CREASE));
163         const float depth             = BMO_slot_float_get(op->slots_in, "thickness");
164         const float inset             = depth;
165
166         const int totvert_orig = bm->totvert;
167
168         BMOIter oiter;
169         BMIter iter;
170         BMIter itersub;
171
172         /* filled only with boundary verts */
173         BMVert **verts_src      = MEM_mallocN(sizeof(BMVert **) * totvert_orig, __func__);
174         BMVert **verts_neg      = MEM_mallocN(sizeof(BMVert **) * totvert_orig, __func__);
175         BMVert **verts_pos      = MEM_mallocN(sizeof(BMVert **) * totvert_orig, __func__);
176
177         /* will over-alloc, but makes for easy lookups by index to keep aligned  */
178         BMVert **verts_boundary = use_boundary ?
179                                   MEM_mallocN(sizeof(BMVert **) * totvert_orig, __func__) : NULL;
180
181         float  *verts_relfac    = use_relative_offset ?
182                                   MEM_mallocN(sizeof(float) * totvert_orig, __func__) : NULL;
183
184         /* may over-alloc if not all faces have wire */
185         BMVert **verts_loop;
186         int verts_loop_tot = 0;
187
188         BMVert *v_src;
189
190         BMFace *f_src;
191         BMLoop *l;
192
193         float tvec[3];
194         float fac;
195
196         int i;
197
198         BM_mesh_elem_index_ensure(bm, BM_VERT);
199
200         BM_ITER_MESH_INDEX (v_src, &iter, bm, BM_VERTS_OF_MESH, i) {
201                 BM_elem_flag_disable(v_src, BM_ELEM_TAG);
202                 verts_src[i] = v_src;
203         }
204
205         /* setup tags, all faces and verts will be tagged which will be duplicated */
206         BM_mesh_elem_hflag_disable_all(bm, BM_FACE, BM_ELEM_TAG, FALSE);
207
208         BMO_ITER (f_src, &oiter, op->slots_in, "faces", BM_FACE) {
209                 verts_loop_tot += f_src->len;
210                 BM_elem_flag_enable(f_src, BM_ELEM_TAG);
211                 BM_ITER_ELEM (l, &itersub, f_src, BM_LOOPS_OF_FACE) {
212                         BM_elem_flag_enable(l->v, BM_ELEM_TAG);
213
214                         /* also tag boundary edges */
215                         BM_elem_flag_set(l->e, BM_ELEM_TAG, bm_loop_is_radial_boundary(l));
216                 }
217         }
218
219         /* duplicate tagged verts */
220         for (i = 0, v_src = verts_src[i]; i < totvert_orig; i++, v_src = verts_src[i]) {
221                 if (BM_elem_flag_test(v_src, BM_ELEM_TAG)) {
222                         fac = depth;
223
224                         if (use_relative_offset) {
225                                 verts_relfac[i] = BM_vert_calc_mean_tagged_edge_length(v_src);
226                                 fac *= verts_relfac[i];
227                         }
228
229                         madd_v3_v3v3fl(tvec, v_src->co, v_src->no, -fac);
230                         verts_neg[i] = BM_vert_create(bm, tvec, v_src);
231                         madd_v3_v3v3fl(tvec, v_src->co, v_src->no,  fac);
232                         verts_pos[i] = BM_vert_create(bm, tvec, v_src);
233                 }
234                 else {
235                         /* could skip this */
236                         verts_src[i] = NULL;
237                         verts_neg[i] = NULL;
238                         verts_pos[i] = NULL;
239                 }
240
241                 /* conflicts with BM_vert_calc_mean_tagged_edge_length */
242                 if (use_relative_offset == FALSE) {
243                         BM_elem_flag_disable(v_src, BM_ELEM_TAG);
244                 }
245         }
246
247         if (use_relative_offset) {
248                 BM_mesh_elem_hflag_disable_all(bm, BM_VERT, BM_ELEM_TAG, FALSE);
249         }
250
251         verts_loop = MEM_mallocN(sizeof(BMVert **) * verts_loop_tot, __func__);
252         verts_loop_tot = 0; /* count up again */
253
254         BMO_ITER (f_src, &oiter, op->slots_in, "faces", BM_FACE) {
255                 BM_ITER_ELEM (l, &itersub, f_src, BM_LOOPS_OF_FACE) {
256                         BM_elem_index_set(l, verts_loop_tot); /* set_loop */
257
258                         BM_loop_calc_face_tangent(l, tvec);
259
260                         /* create offset vert */
261                         fac = inset;
262                         if (use_even_offset) {
263                                 fac *= shell_angle_to_dist(((float)M_PI - BM_loop_calc_face_angle(l)) * 0.5f);
264                         }
265                         if (use_relative_offset) {
266                                 fac *= verts_relfac[BM_elem_index_get(l->v)];
267                         }
268
269                         madd_v3_v3v3fl(tvec, l->v->co, tvec, fac);
270                         verts_loop[verts_loop_tot] = BM_vert_create(bm, tvec, l->v);
271
272
273                         if (use_boundary) {
274                                 if (BM_elem_flag_test(l->e, BM_ELEM_TAG)) {  /* is this a boundary? */
275
276                                         BMLoop *l_pair[2] = {l, l->next};
277
278                                         BM_elem_flag_enable(l->e, BM_ELEM_TAG);
279                                         for (i = 0; i < 2; i++) {
280                                                 if (!BM_elem_flag_test(l_pair[i]->v, BM_ELEM_TAG)) {
281                                                         float no_face[3];
282                                                         BMVert *va_other;
283                                                         BMVert *vb_other;
284
285                                                         BM_elem_flag_enable(l_pair[i]->v, BM_ELEM_TAG);
286
287                                                         bm_vert_boundary_tangent(l_pair[i]->v, tvec, no_face, &va_other, &vb_other);
288
289                                                         /* create offset vert */
290                                                         /* similar to code above but different angle calc */
291                                                         fac = inset;
292                                                         if (use_even_offset) {
293                                                                 if (va_other) {  /* for verts with only one boundary edge - this will be NULL */
294                                                                         fac *= shell_angle_to_dist(((float)M_PI - angle_on_axis_v3v3v3_v3(va_other->co,
295                                                                                                                                           l_pair[i]->v->co,
296                                                                                                                                           vb_other->co,
297                                                                                                                                           no_face)) * 0.5f);
298                                                                 }
299                                                         }
300                                                         if (use_relative_offset) {
301                                                                 fac *= verts_relfac[BM_elem_index_get(l_pair[i]->v)];
302                                                         }
303                                                         madd_v3_v3v3fl(tvec, l_pair[i]->v->co, tvec, fac);
304                                                         verts_boundary[BM_elem_index_get(l_pair[i]->v)] = BM_vert_create(bm, tvec, l_pair[i]->v);
305                                                 }
306                                         }
307                                 }
308                         }
309
310                         verts_loop_tot++;
311                 }
312         }
313
314         BMO_ITER (f_src, &oiter, op->slots_in, "faces", BM_FACE) {
315                 BM_elem_flag_disable(f_src, BM_ELEM_TAG);
316                 BM_ITER_ELEM (l, &itersub, f_src, BM_LOOPS_OF_FACE) {
317                         BMFace *f_new;
318                         BMLoop *l_new;
319                         BMLoop *l_next = l->next;
320                         BMVert *v_l1 = verts_loop[BM_elem_index_get(l)];
321                         BMVert *v_l2 = verts_loop[BM_elem_index_get(l_next)];
322
323                         BMVert *v_src_l1 = l->v;
324                         BMVert *v_src_l2 = l_next->v;
325
326                         const int i_1 = BM_elem_index_get(v_src_l1);
327                         const int i_2 = BM_elem_index_get(v_src_l2);
328
329                         BMVert *v_neg1 = verts_neg[i_1];
330                         BMVert *v_neg2 = verts_neg[i_2];
331
332                         BMVert *v_pos1 = verts_pos[i_1];
333                         BMVert *v_pos2 = verts_pos[i_2];
334
335                         f_new = BM_face_create_quad_tri(bm, v_l1, v_l2, v_neg2, v_neg1, f_src, FALSE);
336                         BM_elem_flag_enable(f_new, BM_ELEM_TAG);
337                         l_new = BM_FACE_FIRST_LOOP(f_new);
338
339                         BM_elem_attrs_copy(bm, bm, l,      l_new);
340                         BM_elem_attrs_copy(bm, bm, l,      l_new->prev);
341                         BM_elem_attrs_copy(bm, bm, l_next, l_new->next);
342                         BM_elem_attrs_copy(bm, bm, l_next, l_new->next->next);
343
344                         f_new = BM_face_create_quad_tri(bm, v_l2, v_l1, v_pos1, v_pos2, f_src, FALSE);
345                         BM_elem_flag_enable(f_new, BM_ELEM_TAG);
346                         l_new = BM_FACE_FIRST_LOOP(f_new);
347
348                         BM_elem_attrs_copy(bm, bm, l_next, l_new);
349                         BM_elem_attrs_copy(bm, bm, l_next, l_new->prev);
350                         BM_elem_attrs_copy(bm, bm, l,      l_new->next);
351                         BM_elem_attrs_copy(bm, bm, l,      l_new->next->next);
352
353                         if (use_boundary) {
354                                 if (BM_elem_flag_test(l->e, BM_ELEM_TAG)) {
355                                         /* we know its a boundary and this is the only face user (which is being wire'd) */
356                                         /* we know we only touch this edge/face once */
357                                         BMVert *v_b1 = verts_boundary[i_1];
358                                         BMVert *v_b2 = verts_boundary[i_2];
359
360                                         f_new = BM_face_create_quad_tri(bm, v_b2, v_b1, v_neg1, v_neg2, f_src, FALSE);
361                                         BM_elem_flag_enable(f_new, BM_ELEM_TAG);
362                                         l_new = BM_FACE_FIRST_LOOP(f_new);
363
364                                         BM_elem_attrs_copy(bm, bm, l_next, l_new);
365                                         BM_elem_attrs_copy(bm, bm, l_next, l_new->prev);
366                                         BM_elem_attrs_copy(bm, bm, l,      l_new->next);
367                                         BM_elem_attrs_copy(bm, bm, l,      l_new->next->next);
368
369                                         f_new = BM_face_create_quad_tri(bm, v_b1, v_b2, v_pos2, v_pos1, f_src, FALSE);
370                                         BM_elem_flag_enable(f_new, BM_ELEM_TAG);
371                                         l_new = BM_FACE_FIRST_LOOP(f_new);
372
373                                         BM_elem_attrs_copy(bm, bm, l,      l_new);
374                                         BM_elem_attrs_copy(bm, bm, l,      l_new->prev);
375                                         BM_elem_attrs_copy(bm, bm, l_next, l_new->next);
376                                         BM_elem_attrs_copy(bm, bm, l_next, l_new->next->next);
377
378                                         if (use_crease) {
379                                                 BMEdge *e_new;
380                                                 e_new = BM_edge_exists(v_pos1, v_b1);
381                                                 BM_elem_float_data_set(&bm->edata, e_new, CD_CREASE, 1.0f);
382
383                                                 e_new = BM_edge_exists(v_pos2, v_b2);
384                                                 BM_elem_float_data_set(&bm->edata, e_new, CD_CREASE, 1.0f);
385
386                                                 e_new = BM_edge_exists(v_neg1, v_b1);
387                                                 BM_elem_float_data_set(&bm->edata, e_new, CD_CREASE, 1.0f);
388
389                                                 e_new = BM_edge_exists(v_neg2, v_b2);
390                                                 BM_elem_float_data_set(&bm->edata, e_new, CD_CREASE, 1.0f);
391                                         }
392                                 }
393                         }
394
395                         if (use_crease) {
396                                 BMEdge *e_new;
397                                 e_new = BM_edge_exists(v_pos1, v_l1);
398                                 BM_elem_float_data_set(&bm->edata, e_new, CD_CREASE, 1.0f);
399
400                                 e_new = BM_edge_exists(v_pos2, v_l2);
401                                 BM_elem_float_data_set(&bm->edata, e_new, CD_CREASE, 1.0f);
402
403                                 e_new = BM_edge_exists(v_neg1, v_l1);
404                                 BM_elem_float_data_set(&bm->edata, e_new, CD_CREASE, 1.0f);
405
406                                 e_new = BM_edge_exists(v_neg2, v_l2);
407                                 BM_elem_float_data_set(&bm->edata, e_new, CD_CREASE, 1.0f);
408                         }
409
410                 }
411         }
412
413         if (use_boundary) {
414                 MEM_freeN(verts_boundary);
415         }
416
417         if (use_relative_offset) {
418                 MEM_freeN(verts_relfac);
419         }
420
421         MEM_freeN(verts_src);
422         MEM_freeN(verts_neg);
423         MEM_freeN(verts_pos);
424         MEM_freeN(verts_loop);
425
426         BMO_slot_buffer_from_enabled_hflag(bm, op, op->slots_out, "faces.out", BM_FACE, BM_ELEM_TAG);
427 }