ee2b08fe824e93b9e140f014497980c036ebf002
[blender.git] / source / blender / modifiers / intern / MOD_bevel.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  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Daniel Dunbar
22  *                 Ton Roosendaal,
23  *                 Ben Batt,
24  *                 Brecht Van Lommel,
25  *                 Campbell Barton
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  */
30
31 /** \file blender/modifiers/intern/MOD_bevel.c
32  *  \ingroup modifiers
33  */
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_mesh_types.h"
38 #include "DNA_meshdata_types.h"
39 #include "DNA_object_types.h"
40 #include "DNA_scene_types.h"
41
42 #include "BLI_utildefines.h"
43 #include "BLI_linklist_stack.h"
44 #include "BLI_math.h"
45 #include "BLI_string.h"
46
47 #include "BKE_deform.h"
48 #include "BKE_mesh.h"
49 #include "BKE_modifier.h"
50
51 #include "MOD_util.h"
52
53 #include "bmesh.h"
54 #include "bmesh_tools.h"
55
56 #include "DEG_depsgraph_query.h"
57
58 static void initData(ModifierData *md)
59 {
60         BevelModifierData *bmd = (BevelModifierData *) md;
61
62         bmd->value = 0.1f;
63         bmd->res = 1;
64         bmd->flags = 0;
65         bmd->val_flags = MOD_BEVEL_AMT_OFFSET;
66         bmd->lim_flags = 0;
67         bmd->e_flags = 0;
68         bmd->edge_flags = 0;
69         bmd->mat = -1;
70         bmd->profile = 0.5f;
71         bmd->bevel_angle = DEG2RADF(30.0f);
72         bmd->defgrp_name[0] = '\0';
73         bmd->hnmode = MOD_BEVEL_HN_NONE;
74         bmd->hn_strength = 0.5f;
75         bmd->clnordata.faceHash = NULL;
76 }
77
78 static void copyData(const ModifierData *md_src, ModifierData *md_dst, const int UNUSED(flag))
79 {
80         BevelModifierData *bmd_src = (BevelModifierData *)md_src;
81         BevelModifierData *bmd_dst = (BevelModifierData *)md_dst;
82
83         *bmd_dst = *bmd_src;
84         bmd_dst->clnordata.faceHash = NULL;
85 }
86
87 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
88 {
89         BevelModifierData *bmd = (BevelModifierData *)md;
90         CustomDataMask dataMask = 0;
91
92         /* ask for vertexgroups if we need them */
93         if (bmd->defgrp_name[0]) dataMask |= CD_MASK_MDEFORMVERT;
94
95         return dataMask;
96 }
97
98 static void bevel_set_weighted_normal_face_strength(BMesh *bm, Scene *scene)
99 {
100         BMFace *f;
101         BMIter fiter;
102         const char *wn_layer_id = MOD_WEIGHTEDNORMALS_FACEWEIGHT_CDLAYER_ID;
103         int cd_prop_int_idx = CustomData_get_named_layer_index(&bm->pdata, CD_PROP_INT, wn_layer_id);
104
105         if (cd_prop_int_idx == -1) {
106                 BM_data_layer_add_named(bm, &bm->pdata, CD_PROP_INT, wn_layer_id);
107                 cd_prop_int_idx = CustomData_get_named_layer_index(&bm->pdata, CD_PROP_INT, wn_layer_id);
108         }
109         cd_prop_int_idx -= CustomData_get_layer_index(&bm->pdata, CD_PROP_INT);
110         const int cd_prop_int_offset = CustomData_get_n_offset(&bm->pdata, CD_PROP_INT, cd_prop_int_idx);
111
112         const int face_strength = scene->toolsettings->face_strength;
113
114         BM_ITER_MESH(f, &fiter, bm, BM_FACES_OF_MESH) {
115                 if (BM_elem_flag_test(f, BM_ELEM_TAG)) {
116                         int *strength = BM_ELEM_CD_GET_VOID_P(f, cd_prop_int_offset);
117                         *strength = face_strength;
118                 }
119         }
120 }
121
122 static void bevel_mod_harden_normals(
123         BevelModifierData *bmd, BMesh *bm, const float hn_strength,
124         const int hnmode, MDeformVert *dvert, int vgroup)
125 {
126         if (bmd->res > 20 || bmd->value == 0)
127                 return;
128
129         BM_mesh_normals_update(bm);
130         BM_lnorspace_update(bm);
131         BM_normals_loops_edges_tag(bm, true);
132
133         const bool vertex_only = (bmd->flags & MOD_BEVEL_VERT) != 0;
134         const int cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
135         const bool do_normal_to_recon = (hn_strength == 1.0f);
136
137         BMFace *f;
138         BMLoop *l, *l_cur, *l_first;
139         BMIter fiter;
140         GHash *faceHash = bmd->clnordata.faceHash;
141
142         /* Iterate throught all loops of a face */
143         BM_ITER_MESH(f, &fiter, bm, BM_FACES_OF_MESH) {
144
145                 l_cur = l_first = BM_FACE_FIRST_LOOP(f);
146                 do {
147                         if ((!BM_elem_flag_test(l_cur->e, BM_ELEM_TAG)) ||
148                             (!BM_elem_flag_test(l_cur, BM_ELEM_TAG) && BM_loop_check_cyclic_smooth_fan(l_cur)))
149                         {
150
151                                 /* previous and next edge is sharp, accumulate face normals into loop */
152                                 if (!BM_elem_flag_test(l_cur->e, BM_ELEM_TAG) && !BM_elem_flag_test(l_cur->prev->e, BM_ELEM_TAG)) {
153                                         const int loop_index = BM_elem_index_get(l_cur);
154                                         short *clnors = BM_ELEM_CD_GET_VOID_P(l_cur, cd_clnors_offset);
155                                         BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[loop_index], f->no, clnors);
156                                 }
157                                 else {
158                                         BMVert *v_pivot = l_cur->v;
159                                         BMEdge *e_next;
160                                         const BMEdge *e_org = l_cur->e;
161                                         BMLoop *lfan_pivot, *lfan_pivot_next;
162                                         UNUSED_VARS_NDEBUG(v_pivot);
163
164                                         lfan_pivot = l_cur;
165                                         e_next = lfan_pivot->e;
166                                         BLI_SMALLSTACK_DECLARE(loops, BMLoop *);
167                                         float cn_wght[3] = { 0.0f, 0.0f, 0.0f };
168                                         int recon_face_count = 0;               /* Counts number of reconstructed faces current vert is connected to */
169                                         BMFace *recon_face = NULL;              /* Reconstructed face */
170
171                                         while (true) {
172                                                 lfan_pivot_next = BM_vert_step_fan_loop(lfan_pivot, &e_next);
173                                                 if (lfan_pivot_next) {
174                                                         BLI_assert(lfan_pivot_next->v == v_pivot);
175                                                 }
176                                                 else {
177                                                         e_next = (lfan_pivot->e == e_next) ? lfan_pivot->prev->e : lfan_pivot->e;
178                                                 }
179
180                                                 BLI_SMALLSTACK_PUSH(loops, lfan_pivot);
181
182                                                 if (bmd->lim_flags & MOD_BEVEL_WEIGHT) {
183                                                         int weight = BM_elem_float_data_get(&bm->edata, lfan_pivot->f, CD_BWEIGHT);
184                                                         if (weight) {
185                                                                 if (hnmode == MOD_BEVEL_HN_FACE) {
186                                                                         float cur[3];                                   //Add area weighted face normals
187                                                                         mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f));
188                                                                         add_v3_v3(cn_wght, cur);
189                                                                 }
190                                                                 else
191                                                                         add_v3_v3(cn_wght, lfan_pivot->f->no);          //Else simply add face normals
192                                                         }
193                                                         else
194                                                                 add_v3_v3(cn_wght, lfan_pivot->f->no);
195
196                                                 }
197                                                 else if (bmd->lim_flags & MOD_BEVEL_VGROUP) {
198                                                         const bool has_vgroup = dvert != NULL;
199                                                         const bool vert_of_group = (
200                                                                 has_vgroup &&
201                                                                 (defvert_find_index(&dvert[BM_elem_index_get(lfan_pivot->v)], vgroup) != NULL));
202
203                                                         if (vert_of_group && hnmode == MOD_BEVEL_HN_FACE) {
204                                                                 float cur[3];
205                                                                 mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f));
206                                                                 add_v3_v3(cn_wght, cur);
207                                                         }
208                                                         else
209                                                                 add_v3_v3(cn_wght, lfan_pivot->f->no);
210                                                 }
211                                                 else {
212                                                         float cur[3];
213                                                         mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f));
214                                                         add_v3_v3(cn_wght, cur);
215                                                 }
216                                                 if (!BLI_ghash_haskey(faceHash, lfan_pivot->f)) {
217                                                         recon_face = lfan_pivot->f;
218                                                         recon_face_count++;
219                                                 }
220                                                 if (!BM_elem_flag_test(e_next, BM_ELEM_TAG) || (e_next == e_org)) {
221                                                         break;
222                                                 }
223                                                 lfan_pivot = lfan_pivot_next;
224                                         }
225
226                                         normalize_v3(cn_wght);
227                                         mul_v3_fl(cn_wght, hn_strength);
228                                         float n_final[3];
229
230                                         while ((l = BLI_SMALLSTACK_POP(loops))) {
231                                                 const int l_index = BM_elem_index_get(l);
232                                                 short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset);
233
234                                                 /* If vertex is edge vert with 1 reconnected face */
235                                                 if (recon_face_count == 1 || (recon_face != NULL && do_normal_to_recon)) {
236                                                         BKE_lnor_space_custom_normal_to_data(
237                                                                 bm->lnor_spacearr->lspacearr[l_index], recon_face->no, clnors);
238                                                 }
239                                                 else if (vertex_only == false || recon_face_count == 0) {
240                                                         copy_v3_v3(n_final, l->f->no);
241                                                         mul_v3_fl(n_final, 1.0f - hn_strength);
242                                                         add_v3_v3(n_final, cn_wght);
243                                                         normalize_v3(n_final);
244                                                         BKE_lnor_space_custom_normal_to_data(
245                                                                 bm->lnor_spacearr->lspacearr[l_index], n_final, clnors);
246                                                 }
247                                                 else if (BLI_ghash_haskey(faceHash, l->f)) {
248                                                         BKE_lnor_space_custom_normal_to_data(
249                                                                 bm->lnor_spacearr->lspacearr[l_index], l->v->no, clnors);
250                                                 }
251                                         }
252                                 }
253                         }
254                 } while ((l_cur = l_cur->next) != l_first);
255         }
256 }
257
258 static void bevel_fix_normal_shading_continuity(BevelModifierData *bmd, BMesh *bm)
259 {
260         const bool vertex_only = (bmd->flags & MOD_BEVEL_VERT) != 0;
261         if (bmd->value == 0 || (bmd->clnordata.faceHash == NULL && vertex_only))
262                 return;
263
264         BM_mesh_normals_update(bm);
265         BM_lnorspace_update(bm);
266
267         GHash *faceHash = bmd->clnordata.faceHash;
268         BMEdge *e;
269         BMLoop *l;
270         BMIter liter, eiter;
271
272         const int cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
273         const float hn_strength = bmd->hn_strength;
274         float ref = 10.0f;
275
276         BM_ITER_MESH(e, &eiter, bm, BM_EDGES_OF_MESH) {
277                 BMFace *f_a, *f_b;
278                 BM_edge_face_pair(e, &f_a, &f_b);
279
280                 bool has_f_a = false, has_f_b = false;
281                 if (f_a)
282                         has_f_a = BLI_ghash_haskey(faceHash, f_a);
283                 if (f_b)
284                         has_f_b = BLI_ghash_haskey(faceHash, f_b);
285                 if (has_f_a ^ has_f_b) {
286                         /* If one of both faces is present in faceHash then we are at a border
287                          * between new vmesh created and reconstructed face */
288
289                         for (int i = 0; i < 2; i++) {
290                                 BMVert *v = (i == 0) ? e->v1 : e->v2;
291                                 BM_ITER_ELEM(l, &liter, v, BM_LOOPS_OF_VERT) {
292
293                                         if (l->f == f_a || l->f == f_b) {
294                                                 const int l_index = BM_elem_index_get(l);
295                                                 short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset);
296                                                 float n_final[3], pow_a[3], pow_b[3];
297
298                                                 zero_v3(n_final);
299                                                 copy_v3_v3(pow_a, f_a->no);
300                                                 copy_v3_v3(pow_b, f_b->no);
301                                                 if (has_f_a) {
302                                                         mul_v3_fl(pow_a, bmd->res / ref);
303                                                         mul_v3_fl(pow_b, ref / bmd->res);
304                                                 }
305                                                 else {
306                                                         mul_v3_fl(pow_b, bmd->res / ref);
307                                                         mul_v3_fl(pow_a, ref / bmd->res);
308                                                 }
309                                                 add_v3_v3(n_final, pow_a);
310                                                 add_v3_v3(n_final, pow_b);
311                                                 normalize_v3(n_final);
312
313                                                 BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], n_final, clnors);
314                                         }
315                                 }
316                         }
317                 }
318                 else if (has_f_a == true  && has_f_b == true) {
319                         /* Else if both faces are present we assign clnor corresponding
320                          * to vert normal and face normal */
321                         for (int i = 0; i < 2; i++) {
322                                 BMVert *v = (i == 0) ? e->v1 : e->v2;
323                                 BM_ITER_ELEM(l, &liter, v, BM_LOOPS_OF_VERT) {
324
325                                         if (l->f == f_a || l->f == f_b) {
326                                                 const int l_index = BM_elem_index_get(l);
327                                                 short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset);
328                                                 float n_final[3], cn_wght[3];
329
330                                                 copy_v3_v3(n_final, v->no);
331                                                 mul_v3_fl(n_final, hn_strength);
332
333                                                 copy_v3_v3(cn_wght, l->f->no);
334                                                 mul_v3_fl(cn_wght, 1.0f - hn_strength);
335
336                                                 add_v3_v3(n_final, cn_wght);
337                                                 normalize_v3(n_final);
338                                                 BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], n_final, clnors);
339                                         }
340                                 }
341                         }
342                 }
343         }
344 }
345
346 /*
347  * This calls the new bevel code (added since 2.64)
348  */
349 static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
350 {
351         Mesh *result;
352         BMesh *bm;
353         BMIter iter;
354         BMEdge *e;
355         BMVert *v;
356         float weight, weight2;
357         int vgroup = -1;
358         MDeformVert *dvert = NULL;
359         BevelModifierData *bmd = (BevelModifierData *) md;
360         const float threshold = cosf(bmd->bevel_angle + 0.000000175f);
361         const bool vertex_only = (bmd->flags & MOD_BEVEL_VERT) != 0;
362         const bool do_clamp = !(bmd->flags & MOD_BEVEL_OVERLAP_OK);
363         const int offset_type = bmd->val_flags;
364         const int mat = CLAMPIS(bmd->mat, -1, ctx->object->totcol - 1);
365         const bool loop_slide = (bmd->flags & MOD_BEVEL_EVEN_WIDTHS) == 0;
366         const bool mark_seam = (bmd->edge_flags & MOD_BEVEL_MARK_SEAM);
367         const bool mark_sharp = (bmd->edge_flags & MOD_BEVEL_MARK_SHARP);
368         const bool set_wn_strength = (bmd->flags & MOD_BEVEL_SET_WN_STR);
369
370         struct Scene *scene = DEG_get_evaluated_scene(ctx->depsgraph);
371
372         bm = BKE_mesh_to_bmesh_ex(
373                 mesh,
374                 &(struct BMeshCreateParams){0},
375                 &(struct BMeshFromMeshParams){
376                     .calc_face_normal = true,
377                     .add_key_index = false,
378                     .use_shapekey = true,
379                     .active_shapekey = ctx->object->shapenr,
380                     .cd_mask_extra = CD_MASK_ORIGINDEX,
381                 });
382
383         if ((bmd->lim_flags & MOD_BEVEL_VGROUP) && bmd->defgrp_name[0])
384                 MOD_get_vgroup(ctx->object, mesh, bmd->defgrp_name, &dvert, &vgroup);
385
386         if (vertex_only) {
387                 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
388                         if (!BM_vert_is_manifold(v))
389                                 continue;
390                         if (bmd->lim_flags & MOD_BEVEL_WEIGHT) {
391                                 weight = BM_elem_float_data_get(&bm->vdata, v, CD_BWEIGHT);
392                                 if (weight == 0.0f)
393                                         continue;
394                         }
395                         else if (vgroup != -1) {
396                                 weight = defvert_array_find_weight_safe(dvert, BM_elem_index_get(v), vgroup);
397                                 /* Check is against 0.5 rather than != 0.0 because cascaded bevel modifiers will
398                                  * interpolate weights for newly created vertices, and may cause unexpected "selection" */
399                                 if (weight < 0.5f)
400                                         continue;
401                         }
402                         BM_elem_flag_enable(v, BM_ELEM_TAG);
403                 }
404         }
405         else if (bmd->lim_flags & MOD_BEVEL_ANGLE) {
406                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
407                         /* check for 1 edge having 2 face users */
408                         BMLoop *l_a, *l_b;
409                         if (BM_edge_loop_pair(e, &l_a, &l_b)) {
410                                 if (dot_v3v3(l_a->f->no, l_b->f->no) < threshold) {
411                                         BM_elem_flag_enable(e, BM_ELEM_TAG);
412                                         BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
413                                         BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
414                                 }
415                         }
416                 }
417         }
418         else {
419                 /* crummy, is there a way just to operator on all? - campbell */
420                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
421                         if (BM_edge_is_manifold(e)) {
422                                 if (bmd->lim_flags & MOD_BEVEL_WEIGHT) {
423                                         weight = BM_elem_float_data_get(&bm->edata, e, CD_BWEIGHT);
424                                         if (weight == 0.0f)
425                                                 continue;
426                                 }
427                                 else if (vgroup != -1) {
428                                         weight = defvert_array_find_weight_safe(dvert, BM_elem_index_get(e->v1), vgroup);
429                                         weight2 = defvert_array_find_weight_safe(dvert, BM_elem_index_get(e->v2), vgroup);
430                                         if (weight < 0.5f || weight2 < 0.5f)
431                                                 continue;
432                                 }
433                                 BM_elem_flag_enable(e, BM_ELEM_TAG);
434                                 BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
435                                 BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
436                         }
437                 }
438         }
439
440         BM_mesh_bevel(bm, bmd->value, offset_type, bmd->res, bmd->profile,
441                       vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp,
442                       dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp, bmd->hnmode, &bmd->clnordata);
443
444         if (bmd->hnmode != BEVEL_HN_FIX_SHA && bmd->hnmode != MOD_BEVEL_HN_NONE) {
445                 bevel_mod_harden_normals(bmd, bm, bmd->hn_strength, bmd->hnmode, dvert, vgroup);
446         }
447         if (bmd->hnmode == BEVEL_HN_FIX_SHA)
448                 bevel_fix_normal_shading_continuity(bmd, bm);
449         if (set_wn_strength)
450                 bevel_set_weighted_normal_face_strength(bm, scene);
451
452         result = BKE_mesh_from_bmesh_for_eval_nomain(bm, 0);
453
454         BLI_assert(bm->vtoolflagpool == NULL &&
455                    bm->etoolflagpool == NULL &&
456                    bm->ftoolflagpool == NULL);  /* make sure we never alloc'd these */
457         BM_mesh_free(bm);
458
459         if (bmd->clnordata.faceHash)
460                 BLI_ghash_free(bmd->clnordata.faceHash, NULL, NULL);
461
462         result->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
463
464         return result;
465 }
466
467 static bool dependsOnNormals(ModifierData *UNUSED(md))
468 {
469         return true;
470 }
471
472 ModifierTypeInfo modifierType_Bevel = {
473         /* name */              "Bevel",
474         /* structName */        "BevelModifierData",
475         /* structSize */        sizeof(BevelModifierData),
476         /* type */              eModifierTypeType_Constructive,
477         /* flags */             eModifierTypeFlag_AcceptsMesh |
478                                 eModifierTypeFlag_SupportsEditmode |
479                                 eModifierTypeFlag_EnableInEditmode |
480                                 eModifierTypeFlag_AcceptsCVs,
481
482         /* copyData */          copyData,
483
484         /* deformVerts_DM */    NULL,
485         /* deformMatrices_DM */ NULL,
486         /* deformVertsEM_DM */  NULL,
487         /* deformMatricesEM_DM*/NULL,
488         /* applyModifier_DM */  NULL,
489
490         /* deformVerts */       NULL,
491         /* deformMatrices */    NULL,
492         /* deformVertsEM */     NULL,
493         /* deformMatricesEM */  NULL,
494         /* applyModifier */     applyModifier,
495
496         /* initData */          initData,
497         /* requiredDataMask */  requiredDataMask,
498         /* freeData */          NULL,
499         /* isDisabled */        NULL,
500         /* updateDepsgraph */   NULL,
501         /* dependsOnTime */     NULL,
502         /* dependsOnNormals */  dependsOnNormals,
503         /* foreachObjectLink */ NULL,
504         /* foreachIDLink */     NULL,
505         /* foreachTexLink */    NULL,
506 };