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