Bevel mod: cleanup flags and extra data.
[blender.git] / source / blender / modifiers / intern / MOD_bevel.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software  Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup modifiers
22  */
23
24 #include "MEM_guardedalloc.h"
25
26 #include "BLI_utildefines.h"
27
28 #include "BLI_math.h"
29
30 #include "DNA_mesh_types.h"
31 #include "DNA_meshdata_types.h"
32 #include "DNA_object_types.h"
33 #include "DNA_scene_types.h"
34
35 #include "BKE_deform.h"
36 #include "BKE_mesh.h"
37 #include "BKE_modifier.h"
38
39 #include "MOD_util.h"
40
41 #include "bmesh.h"
42 #include "bmesh_tools.h"
43
44 #include "DEG_depsgraph_query.h"
45
46 static void initData(ModifierData *md)
47 {
48         BevelModifierData *bmd = (BevelModifierData *) md;
49
50         bmd->value = 0.1f;
51         bmd->res = 1;
52         bmd->flags = 0;
53         bmd->val_flags = MOD_BEVEL_AMT_OFFSET;
54         bmd->lim_flags = 0;
55         bmd->e_flags = 0;
56         bmd->edge_flags = 0;
57         bmd->face_str_mode = MOD_BEVEL_FACE_STRENGTH_NONE;
58         bmd->miter_inner = MOD_BEVEL_MITER_SHARP;
59         bmd->miter_outer = MOD_BEVEL_MITER_SHARP;
60         bmd->spread = 0.1f;
61         bmd->mat = -1;
62         bmd->profile = 0.5f;
63         bmd->bevel_angle = DEG2RADF(30.0f);
64         bmd->defgrp_name[0] = '\0';
65 }
66
67 static void copyData(const ModifierData *md_src, ModifierData *md_dst, const int flag)
68 {
69         modifier_copyData_generic(md_src, md_dst, flag);
70 }
71
72 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
73 {
74         BevelModifierData *bmd = (BevelModifierData *)md;
75         CustomDataMask dataMask = 0;
76
77         /* ask for vertexgroups if we need them */
78         if (bmd->defgrp_name[0]) dataMask |= CD_MASK_MDEFORMVERT;
79
80         return dataMask;
81 }
82
83 /*
84  * This calls the new bevel code (added since 2.64)
85  */
86 static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
87 {
88         Mesh *result;
89         BMesh *bm;
90         BMIter iter;
91         BMEdge *e;
92         BMVert *v;
93         float weight, weight2;
94         int vgroup = -1;
95         MDeformVert *dvert = NULL;
96         BevelModifierData *bmd = (BevelModifierData *) md;
97         const float threshold = cosf(bmd->bevel_angle + 0.000000175f);
98         const bool vertex_only = (bmd->flags & MOD_BEVEL_VERT) != 0;
99         const bool do_clamp = !(bmd->flags & MOD_BEVEL_OVERLAP_OK);
100         const int offset_type = bmd->val_flags;
101         const float value = bmd->value;
102         const int mat = CLAMPIS(bmd->mat, -1, ctx->object->totcol - 1);
103         const bool loop_slide = (bmd->flags & MOD_BEVEL_EVEN_WIDTHS) == 0;
104         const bool mark_seam = (bmd->edge_flags & MOD_BEVEL_MARK_SEAM);
105         const bool mark_sharp = (bmd->edge_flags & MOD_BEVEL_MARK_SHARP);
106         bool harden_normals = (bmd->flags & MOD_BEVEL_HARDEN_NORMALS);
107         const int face_strength_mode = bmd->face_str_mode;
108         const int miter_outer = bmd->miter_outer;
109         const int miter_inner = bmd->miter_inner;
110         const float spread = bmd->spread;
111
112         bm = BKE_mesh_to_bmesh_ex(
113                 mesh,
114                 &(struct BMeshCreateParams){0},
115                 &(struct BMeshFromMeshParams){
116                     .calc_face_normal = true,
117                     .add_key_index = false,
118                     .use_shapekey = false,
119                     .active_shapekey = 0,
120                     .cd_mask_extra = CD_MASK_ORIGINDEX,
121                 });
122
123         if ((bmd->lim_flags & MOD_BEVEL_VGROUP) && bmd->defgrp_name[0])
124                 MOD_get_vgroup(ctx->object, mesh, bmd->defgrp_name, &dvert, &vgroup);
125
126         if (vertex_only) {
127                 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
128                         if (!BM_vert_is_manifold(v))
129                                 continue;
130                         if (bmd->lim_flags & MOD_BEVEL_WEIGHT) {
131                                 weight = BM_elem_float_data_get(&bm->vdata, v, CD_BWEIGHT);
132                                 if (weight == 0.0f)
133                                         continue;
134                         }
135                         else if (vgroup != -1) {
136                                 weight = defvert_array_find_weight_safe(dvert, BM_elem_index_get(v), vgroup);
137                                 /* Check is against 0.5 rather than != 0.0 because cascaded bevel modifiers will
138                                  * interpolate weights for newly created vertices, and may cause unexpected "selection" */
139                                 if (weight < 0.5f)
140                                         continue;
141                         }
142                         BM_elem_flag_enable(v, BM_ELEM_TAG);
143                 }
144         }
145         else if (bmd->lim_flags & MOD_BEVEL_ANGLE) {
146                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
147                         /* check for 1 edge having 2 face users */
148                         BMLoop *l_a, *l_b;
149                         if (BM_edge_loop_pair(e, &l_a, &l_b)) {
150                                 if (dot_v3v3(l_a->f->no, l_b->f->no) < threshold) {
151                                         BM_elem_flag_enable(e, BM_ELEM_TAG);
152                                         BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
153                                         BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
154                                 }
155                         }
156                 }
157         }
158         else {
159                 /* crummy, is there a way just to operator on all? - campbell */
160                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
161                         if (BM_edge_is_manifold(e)) {
162                                 if (bmd->lim_flags & MOD_BEVEL_WEIGHT) {
163                                         weight = BM_elem_float_data_get(&bm->edata, e, CD_BWEIGHT);
164                                         if (weight == 0.0f)
165                                                 continue;
166                                 }
167                                 else if (vgroup != -1) {
168                                         weight = defvert_array_find_weight_safe(dvert, BM_elem_index_get(e->v1), vgroup);
169                                         weight2 = defvert_array_find_weight_safe(dvert, BM_elem_index_get(e->v2), vgroup);
170                                         if (weight < 0.5f || weight2 < 0.5f)
171                                                 continue;
172                                 }
173                                 BM_elem_flag_enable(e, BM_ELEM_TAG);
174                                 BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
175                                 BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
176                         }
177                 }
178         }
179
180         if (harden_normals && !(((Mesh *)ctx->object->data)->flag & ME_AUTOSMOOTH)) {
181                 modifier_setError(md, "Enable 'Auto Smooth' option in mesh settings for hardening");
182                 harden_normals = false;
183         }
184
185         BM_mesh_bevel(bm, value, offset_type, bmd->res, bmd->profile,
186                       vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp,
187                       dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp,
188                       harden_normals, face_strength_mode,
189                       miter_outer, miter_inner, spread, mesh->smoothresh);
190
191         result = BKE_mesh_from_bmesh_for_eval_nomain(bm, 0);
192
193         BLI_assert(bm->vtoolflagpool == NULL &&
194                    bm->etoolflagpool == NULL &&
195                    bm->ftoolflagpool == NULL);  /* make sure we never alloc'd these */
196         BM_mesh_free(bm);
197
198         result->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
199
200         return result;
201 }
202
203 static bool dependsOnNormals(ModifierData *UNUSED(md))
204 {
205         return true;
206 }
207
208 ModifierTypeInfo modifierType_Bevel = {
209         /* name */              "Bevel",
210         /* structName */        "BevelModifierData",
211         /* structSize */        sizeof(BevelModifierData),
212         /* type */              eModifierTypeType_Constructive,
213         /* flags */             eModifierTypeFlag_AcceptsMesh |
214                                 eModifierTypeFlag_SupportsEditmode |
215                                 eModifierTypeFlag_EnableInEditmode |
216                                 eModifierTypeFlag_AcceptsCVs,
217
218         /* copyData */          copyData,
219
220         /* deformVerts_DM */    NULL,
221         /* deformMatrices_DM */ NULL,
222         /* deformVertsEM_DM */  NULL,
223         /* deformMatricesEM_DM*/NULL,
224         /* applyModifier_DM */  NULL,
225
226         /* deformVerts */       NULL,
227         /* deformMatrices */    NULL,
228         /* deformVertsEM */     NULL,
229         /* deformMatricesEM */  NULL,
230         /* applyModifier */     applyModifier,
231
232         /* initData */          initData,
233         /* requiredDataMask */  requiredDataMask,
234         /* freeData */          NULL,
235         /* isDisabled */        NULL,
236         /* updateDepsgraph */   NULL,
237         /* dependsOnTime */     NULL,
238         /* dependsOnNormals */  dependsOnNormals,
239         /* foreachObjectLink */ NULL,
240         /* foreachIDLink */     NULL,
241         /* foreachTexLink */    NULL,
242 };