2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * Contributor(s): Campbell Barton
20 * ***** END GPL LICENSE BLOCK *****
23 /** \file blender/bmesh/operators/bmesh_wireframe.c
26 * Creates a solid wireframe from conected faces.
29 #include "MEM_guardedalloc.h"
31 #include "DNA_object_types.h"
32 #include "DNA_meshdata_types.h"
38 #include "BKE_deform.h"
39 #include "BKE_customdata.h"
41 #include "bmesh_wireframe.h"
43 static BMLoop *bm_edge_tag_faceloop(BMEdge *e)
49 if (BM_elem_flag_test(l->f, BM_ELEM_TAG)) {
52 } while ((l = l->radial_next) != l_first);
54 /* in the case this is used, we know this will never happen */
58 static void bm_vert_boundary_tangent(BMVert *v, float r_no[3], float r_no_face[3],
59 BMVert **r_va_other, BMVert **r_vb_other)
64 BMEdge *e_a = NULL, *e_b = NULL;
69 float no_face[3], no_edge[3];
70 float tvec_a[3], tvec_b[3];
72 /* get 2 boundary edges, there should only _be_ 2,
73 * in case there are more - results wont be valid of course */
74 BM_ITER_ELEM (e_iter, &iter, v, BM_EDGES_OF_VERT) {
75 if (BM_elem_flag_test(e_iter, BM_ELEM_TAG)) {
87 /* note, with an incorrectly flushed selection this can crash */
88 l_a = bm_edge_tag_faceloop(e_a);
89 l_b = bm_edge_tag_faceloop(e_b);
91 /* average edge face normal */
92 add_v3_v3v3(no_face, l_a->f->no, l_b->f->no);
94 /* average edge direction */
95 v_a = BM_edge_other_vert(e_a, v);
96 v_b = BM_edge_other_vert(e_b, v);
98 sub_v3_v3v3(tvec_a, v->co, v_a->co);
99 sub_v3_v3v3(tvec_b, v_b->co, v->co);
100 normalize_v3(tvec_a);
101 normalize_v3(tvec_b);
102 add_v3_v3v3(no_edge, tvec_a, tvec_b); /* not unit length but this is ok */
104 /* check are we flipped the right way */
105 BM_edge_calc_face_tangent(e_a, l_a, tvec_a);
106 BM_edge_calc_face_tangent(e_b, l_b, tvec_b);
107 add_v3_v3(tvec_a, tvec_b);
113 /* degenerate case - vertex connects a boundary edged face to other faces,
114 * so we have only one boundary face - only use it for calculations */
115 l_a = bm_edge_tag_faceloop(e_a);
117 copy_v3_v3(no_face, l_a->f->no);
120 v_a = BM_edge_other_vert(e_a, v);
123 sub_v3_v3v3(no_edge, v->co, v_a->co);
125 /* check are we flipped the right way */
126 BM_edge_calc_face_tangent(e_a, l_a, tvec_a);
132 /* find the normal */
133 cross_v3_v3v3(r_no, no_edge, no_face);
136 if (dot_v3v3(r_no, tvec_a) > 0.0f) {
140 copy_v3_v3(r_no_face, no_face);
143 /* check if we are the only tagged loop-face around this edge */
144 static bool bm_loop_is_radial_boundary(BMLoop *l_first)
146 BMLoop *l = l_first->radial_next;
149 return true; /* a real boundary */
153 if (BM_elem_flag_test(l->f, BM_ELEM_TAG)) {
156 } while ((l = l->radial_next) != l_first);
162 * \param def_nr -1 for no vertex groups.
164 * \note All edge tags must be cleared.
165 * \note Behavior matches MOD_solidify.c
167 void BM_mesh_wireframe(
170 const float offset_fac,
171 const float offset_fac_vg,
172 const bool use_replace,
173 const bool use_boundary,
174 const bool use_even_offset,
175 const bool use_relative_offset,
176 const bool use_crease,
177 const float crease_weight,
178 const int defgrp_index,
179 const bool defgrp_invert,
180 const short mat_offset,
186 const float ofs_orig = -(((-offset_fac + 1.0f) * 0.5f) * offset);
187 const float ofs_new = offset + ofs_orig;
188 const float ofs_mid = (ofs_orig + ofs_new) / 2.0f;
189 const float inset = offset / 2.0f;
190 int cd_edge_crease_offset = use_crease ? CustomData_get_offset(&bm->edata, CD_CREASE) : -1;
191 const int cd_dvert_offset = (defgrp_index != -1) ? CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT) : -1;
192 const float offset_fac_vg_inv = 1.0f - offset_fac_vg;
194 const int totvert_orig = bm->totvert;
199 /* filled only with boundary verts */
200 BMVert **verts_src = MEM_mallocN(sizeof(BMVert *) * totvert_orig, __func__);
201 BMVert **verts_neg = MEM_mallocN(sizeof(BMVert *) * totvert_orig, __func__);
202 BMVert **verts_pos = MEM_mallocN(sizeof(BMVert *) * totvert_orig, __func__);
204 /* will over-alloc, but makes for easy lookups by index to keep aligned */
205 BMVert **verts_boundary = use_boundary ?
206 MEM_mallocN(sizeof(BMVert *) * totvert_orig, __func__) : NULL;
208 float *verts_relfac = (use_relative_offset || (cd_dvert_offset != -1)) ?
209 MEM_mallocN(sizeof(float) * totvert_orig, __func__) : NULL;
211 /* may over-alloc if not all faces have wire */
213 int verts_loop_tot = 0;
221 float fac, fac_shell;
225 if (use_crease && cd_edge_crease_offset == -1) {
226 BM_data_layer_add(bm, &bm->edata, CD_CREASE);
227 cd_edge_crease_offset = CustomData_get_offset(&bm->edata, CD_CREASE);
230 BM_mesh_elem_index_ensure(bm, BM_VERT);
232 BM_ITER_MESH_INDEX (v_src, &iter, bm, BM_VERTS_OF_MESH, i) {
233 BM_elem_flag_disable(v_src, BM_ELEM_TAG);
234 verts_src[i] = v_src;
237 /* setup tags, all faces and verts will be tagged which will be duplicated */
239 BM_ITER_MESH (f_src, &iter, bm, BM_FACES_OF_MESH) {
242 if (!BM_elem_flag_test(f_src, BM_ELEM_TAG)) {
247 BM_elem_flag_enable(f_src, BM_ELEM_TAG);
251 verts_loop_tot += f_src->len;
252 BM_ITER_ELEM (l, &itersub, f_src, BM_LOOPS_OF_FACE) {
253 BM_elem_flag_enable(l->v, BM_ELEM_TAG);
255 /* also tag boundary edges */
256 BM_elem_flag_set(l->e, BM_ELEM_TAG, bm_loop_is_radial_boundary(l));
260 /* duplicate tagged verts */
261 for (i = 0; i < totvert_orig; i++) {
262 v_src = verts_src[i];
263 if (BM_elem_flag_test(v_src, BM_ELEM_TAG)) {
267 if (use_relative_offset) {
268 verts_relfac[i] = BM_vert_calc_mean_tagged_edge_length(v_src);
271 verts_relfac[i] = 1.0f;
275 if (cd_dvert_offset != -1) {
276 MDeformVert *dvert = BM_ELEM_CD_GET_VOID_P(v_src, cd_dvert_offset);
277 float defgrp_fac = defvert_find_weight(dvert, defgrp_index);
280 defgrp_fac = 1.0f - defgrp_fac;
283 if (offset_fac_vg > 0.0f) {
284 defgrp_fac = (offset_fac_vg + (defgrp_fac * offset_fac_vg_inv));
287 verts_relfac[i] *= defgrp_fac;
290 fac *= verts_relfac[i];
294 verts_neg[i] = BM_vert_create(bm, NULL, v_src, BM_CREATE_NOP);
295 verts_pos[i] = BM_vert_create(bm, NULL, v_src, BM_CREATE_NOP);
297 if (offset == 0.0f) {
298 madd_v3_v3v3fl(verts_neg[i]->co, v_src->co, v_src->no, ofs_orig * fac);
299 madd_v3_v3v3fl(verts_pos[i]->co, v_src->co, v_src->no, ofs_new * fac);
302 madd_v3_v3v3fl(tvec, v_src->co, v_src->no, ofs_mid * fac);
304 madd_v3_v3v3fl(verts_neg[i]->co, tvec, v_src->no, (ofs_mid - ofs_orig) * fac);
305 madd_v3_v3v3fl(verts_pos[i]->co, tvec, v_src->no, (ofs_mid - ofs_new) * fac);
309 /* could skip this */
314 /* conflicts with BM_vert_calc_mean_tagged_edge_length */
315 if (use_relative_offset == false) {
316 BM_elem_flag_disable(v_src, BM_ELEM_TAG);
320 if (use_relative_offset) {
321 BM_mesh_elem_hflag_disable_all(bm, BM_VERT, BM_ELEM_TAG, false);
324 verts_loop = MEM_mallocN(sizeof(BMVert *) * verts_loop_tot, __func__);
325 verts_loop_tot = 0; /* count up again */
327 BM_ITER_MESH (f_src, &iter, bm, BM_FACES_OF_MESH) {
329 if (use_tag && !BM_elem_flag_test(f_src, BM_ELEM_TAG)) {
333 BM_ITER_ELEM (l, &itersub, f_src, BM_LOOPS_OF_FACE) {
334 BM_elem_index_set(l, verts_loop_tot); /* set_loop */
336 BM_loop_calc_face_tangent(l, tvec);
338 /* create offset vert */
342 fac *= verts_relfac[BM_elem_index_get(l->v)];
346 if (use_even_offset) {
347 fac_shell *= shell_angle_to_dist(((float)M_PI - BM_loop_calc_face_angle(l)) * 0.5f);
351 madd_v3_v3v3fl(tvec, l->v->co, tvec, inset * fac_shell);
352 if (offset != 0.0f) {
353 madd_v3_v3fl(tvec, l->v->no, ofs_mid * fac);
355 verts_loop[verts_loop_tot] = BM_vert_create(bm, tvec, l->v, BM_CREATE_NOP);
359 if (BM_elem_flag_test(l->e, BM_ELEM_TAG)) { /* is this a boundary? */
360 BMVert *v_pair[2] = {l->v, l->next->v};
362 for (i = 0; i < 2; i++) {
363 BMVert *v_boundary = v_pair[i];
364 if (!BM_elem_flag_test(v_boundary, BM_ELEM_TAG)) {
365 const int v_boundary_index = BM_elem_index_get(v_boundary);
370 BM_elem_flag_enable(v_boundary, BM_ELEM_TAG);
372 bm_vert_boundary_tangent(v_boundary, tvec, no_face, &va_other, &vb_other);
374 /* create offset vert */
375 /* similar to code above but different angle calc */
379 fac *= verts_relfac[v_boundary_index];
383 if (use_even_offset) {
384 if (va_other) { /* for verts with only one boundary edge - this will be NULL */
385 fac_shell *= shell_angle_to_dist(((float)M_PI -
386 angle_on_axis_v3v3v3_v3(va_other->co,
394 madd_v3_v3v3fl(tvec, v_boundary->co, tvec, inset * fac_shell);
395 if (offset != 0.0f) {
396 madd_v3_v3fl(tvec, v_boundary->no, ofs_mid * fac);
398 verts_boundary[v_boundary_index] = BM_vert_create(bm, tvec, v_boundary, BM_CREATE_NOP);
408 BM_ITER_MESH (f_src, &iter, bm, BM_FACES_OF_MESH) {
410 /* skip recently added faces */
411 if (BM_elem_index_get(f_src) == -1) {
415 if (use_tag && !BM_elem_flag_test(f_src, BM_ELEM_TAG)) {
419 BM_elem_flag_disable(f_src, BM_ELEM_TAG);
421 BM_ITER_ELEM (l, &itersub, f_src, BM_LOOPS_OF_FACE) {
424 BMLoop *l_next = l->next;
425 BMVert *v_l1 = verts_loop[BM_elem_index_get(l)];
426 BMVert *v_l2 = verts_loop[BM_elem_index_get(l_next)];
428 BMVert *v_src_l1 = l->v;
429 BMVert *v_src_l2 = l_next->v;
431 const int i_1 = BM_elem_index_get(v_src_l1);
432 const int i_2 = BM_elem_index_get(v_src_l2);
434 BMVert *v_neg1 = verts_neg[i_1];
435 BMVert *v_neg2 = verts_neg[i_2];
437 BMVert *v_pos1 = verts_pos[i_1];
438 BMVert *v_pos2 = verts_pos[i_2];
440 f_new = BM_face_create_quad_tri(bm, v_l1, v_l2, v_neg2, v_neg1, f_src, false);
441 if (mat_offset) f_new->mat_nr = CLAMPIS(f_new->mat_nr + mat_offset, 0, mat_max);
442 BM_elem_flag_enable(f_new, BM_ELEM_TAG);
443 l_new = BM_FACE_FIRST_LOOP(f_new);
445 BM_elem_attrs_copy(bm, bm, l, l_new);
446 BM_elem_attrs_copy(bm, bm, l, l_new->prev);
447 BM_elem_attrs_copy(bm, bm, l_next, l_new->next);
448 BM_elem_attrs_copy(bm, bm, l_next, l_new->next->next);
450 f_new = BM_face_create_quad_tri(bm, v_l2, v_l1, v_pos1, v_pos2, f_src, false);
452 if (mat_offset) f_new->mat_nr = CLAMPIS(f_new->mat_nr + mat_offset, 0, mat_max);
453 BM_elem_flag_enable(f_new, BM_ELEM_TAG);
454 l_new = BM_FACE_FIRST_LOOP(f_new);
456 BM_elem_attrs_copy(bm, bm, l_next, l_new);
457 BM_elem_attrs_copy(bm, bm, l_next, l_new->prev);
458 BM_elem_attrs_copy(bm, bm, l, l_new->next);
459 BM_elem_attrs_copy(bm, bm, l, l_new->next->next);
462 if (BM_elem_flag_test(l->e, BM_ELEM_TAG)) {
463 /* we know its a boundary and this is the only face user (which is being wire'd) */
464 /* we know we only touch this edge/face once */
465 BMVert *v_b1 = verts_boundary[i_1];
466 BMVert *v_b2 = verts_boundary[i_2];
468 f_new = BM_face_create_quad_tri(bm, v_b2, v_b1, v_neg1, v_neg2, f_src, false);
469 if (mat_offset) f_new->mat_nr = CLAMPIS(f_new->mat_nr + mat_offset, 0, mat_max);
470 BM_elem_flag_enable(f_new, BM_ELEM_TAG);
471 l_new = BM_FACE_FIRST_LOOP(f_new);
473 BM_elem_attrs_copy(bm, bm, l_next, l_new);
474 BM_elem_attrs_copy(bm, bm, l_next, l_new->prev);
475 BM_elem_attrs_copy(bm, bm, l, l_new->next);
476 BM_elem_attrs_copy(bm, bm, l, l_new->next->next);
478 f_new = BM_face_create_quad_tri(bm, v_b1, v_b2, v_pos2, v_pos1, f_src, false);
479 if (mat_offset) f_new->mat_nr = CLAMPIS(f_new->mat_nr + mat_offset, 0, mat_max);
480 BM_elem_flag_enable(f_new, BM_ELEM_TAG);
481 l_new = BM_FACE_FIRST_LOOP(f_new);
483 BM_elem_attrs_copy(bm, bm, l, l_new);
484 BM_elem_attrs_copy(bm, bm, l, l_new->prev);
485 BM_elem_attrs_copy(bm, bm, l_next, l_new->next);
486 BM_elem_attrs_copy(bm, bm, l_next, l_new->next->next);
490 e_new = BM_edge_exists(v_pos1, v_b1);
491 BM_ELEM_CD_SET_FLOAT(e_new, cd_edge_crease_offset, crease_weight);
493 e_new = BM_edge_exists(v_pos2, v_b2);
494 BM_ELEM_CD_SET_FLOAT(e_new, cd_edge_crease_offset, crease_weight);
496 e_new = BM_edge_exists(v_neg1, v_b1);
497 BM_ELEM_CD_SET_FLOAT(e_new, cd_edge_crease_offset, crease_weight);
499 e_new = BM_edge_exists(v_neg2, v_b2);
500 BM_ELEM_CD_SET_FLOAT(e_new, cd_edge_crease_offset, crease_weight);
507 e_new = BM_edge_exists(v_pos1, v_l1);
508 BM_ELEM_CD_SET_FLOAT(e_new, cd_edge_crease_offset, crease_weight);
510 e_new = BM_edge_exists(v_pos2, v_l2);
511 BM_ELEM_CD_SET_FLOAT(e_new, cd_edge_crease_offset, crease_weight);
513 e_new = BM_edge_exists(v_neg1, v_l1);
514 BM_ELEM_CD_SET_FLOAT(e_new, cd_edge_crease_offset, crease_weight);
516 e_new = BM_edge_exists(v_neg2, v_l2);
517 BM_ELEM_CD_SET_FLOAT(e_new, cd_edge_crease_offset, crease_weight);
524 MEM_freeN(verts_boundary);
528 MEM_freeN(verts_relfac);
532 for (i = 0; i < totvert_orig; i++) {
533 BM_vert_kill(bm, verts_src[i]);
537 MEM_freeN(verts_src);
538 MEM_freeN(verts_neg);
539 MEM_freeN(verts_pos);
540 MEM_freeN(verts_loop);