Units: GP Canvas Offset
[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         bmd->clnordata.faceHash = NULL;
66 }
67
68 static void copyData(const ModifierData *md_src, ModifierData *md_dst, const int flag)
69 {
70         BevelModifierData *bmd_dst = (BevelModifierData *)md_dst;
71
72         modifier_copyData_generic(md_src, md_dst, flag);
73
74         bmd_dst->clnordata.faceHash = NULL;
75 }
76
77 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
78 {
79         BevelModifierData *bmd = (BevelModifierData *)md;
80         CustomDataMask dataMask = 0;
81
82         /* ask for vertexgroups if we need them */
83         if (bmd->defgrp_name[0]) dataMask |= CD_MASK_MDEFORMVERT;
84
85         return dataMask;
86 }
87
88 /*
89  * This calls the new bevel code (added since 2.64)
90  */
91 static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
92 {
93         Mesh *result;
94         BMesh *bm;
95         BMIter iter;
96         BMEdge *e;
97         BMVert *v;
98         float weight, weight2;
99         int vgroup = -1;
100         MDeformVert *dvert = NULL;
101         BevelModifierData *bmd = (BevelModifierData *) md;
102         const float threshold = cosf(bmd->bevel_angle + 0.000000175f);
103         const bool vertex_only = (bmd->flags & MOD_BEVEL_VERT) != 0;
104         const bool do_clamp = !(bmd->flags & MOD_BEVEL_OVERLAP_OK);
105         const int offset_type = bmd->val_flags;
106         const float value = bmd->value;
107         const int mat = CLAMPIS(bmd->mat, -1, ctx->object->totcol - 1);
108         const bool loop_slide = (bmd->flags & MOD_BEVEL_EVEN_WIDTHS) == 0;
109         const bool mark_seam = (bmd->edge_flags & MOD_BEVEL_MARK_SEAM);
110         const bool mark_sharp = (bmd->edge_flags & MOD_BEVEL_MARK_SHARP);
111         bool harden_normals = (bmd->flags & MOD_BEVEL_HARDEN_NORMALS);
112         const int face_strength_mode = bmd->face_str_mode;
113         const int miter_outer = bmd->miter_outer;
114         const int miter_inner = bmd->miter_inner;
115         const float spread = bmd->spread;
116
117         bm = BKE_mesh_to_bmesh_ex(
118                 mesh,
119                 &(struct BMeshCreateParams){0},
120                 &(struct BMeshFromMeshParams){
121                     .calc_face_normal = true,
122                     .add_key_index = false,
123                     .use_shapekey = false,
124                     .active_shapekey = 0,
125                     .cd_mask_extra = CD_MASK_ORIGINDEX,
126                 });
127
128         if ((bmd->lim_flags & MOD_BEVEL_VGROUP) && bmd->defgrp_name[0])
129                 MOD_get_vgroup(ctx->object, mesh, bmd->defgrp_name, &dvert, &vgroup);
130
131         if (vertex_only) {
132                 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
133                         if (!BM_vert_is_manifold(v))
134                                 continue;
135                         if (bmd->lim_flags & MOD_BEVEL_WEIGHT) {
136                                 weight = BM_elem_float_data_get(&bm->vdata, v, CD_BWEIGHT);
137                                 if (weight == 0.0f)
138                                         continue;
139                         }
140                         else if (vgroup != -1) {
141                                 weight = defvert_array_find_weight_safe(dvert, BM_elem_index_get(v), vgroup);
142                                 /* Check is against 0.5 rather than != 0.0 because cascaded bevel modifiers will
143                                  * interpolate weights for newly created vertices, and may cause unexpected "selection" */
144                                 if (weight < 0.5f)
145                                         continue;
146                         }
147                         BM_elem_flag_enable(v, BM_ELEM_TAG);
148                 }
149         }
150         else if (bmd->lim_flags & MOD_BEVEL_ANGLE) {
151                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
152                         /* check for 1 edge having 2 face users */
153                         BMLoop *l_a, *l_b;
154                         if (BM_edge_loop_pair(e, &l_a, &l_b)) {
155                                 if (dot_v3v3(l_a->f->no, l_b->f->no) < threshold) {
156                                         BM_elem_flag_enable(e, BM_ELEM_TAG);
157                                         BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
158                                         BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
159                                 }
160                         }
161                 }
162         }
163         else {
164                 /* crummy, is there a way just to operator on all? - campbell */
165                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
166                         if (BM_edge_is_manifold(e)) {
167                                 if (bmd->lim_flags & MOD_BEVEL_WEIGHT) {
168                                         weight = BM_elem_float_data_get(&bm->edata, e, CD_BWEIGHT);
169                                         if (weight == 0.0f)
170                                                 continue;
171                                 }
172                                 else if (vgroup != -1) {
173                                         weight = defvert_array_find_weight_safe(dvert, BM_elem_index_get(e->v1), vgroup);
174                                         weight2 = defvert_array_find_weight_safe(dvert, BM_elem_index_get(e->v2), vgroup);
175                                         if (weight < 0.5f || weight2 < 0.5f)
176                                                 continue;
177                                 }
178                                 BM_elem_flag_enable(e, BM_ELEM_TAG);
179                                 BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
180                                 BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
181                         }
182                 }
183         }
184
185         if (harden_normals && !(((Mesh *)ctx->object->data)->flag & ME_AUTOSMOOTH)) {
186                 modifier_setError(md, "Enable 'Auto Smooth' option in mesh settings for hardening");
187                 harden_normals = false;
188         }
189
190         BM_mesh_bevel(bm, value, offset_type, bmd->res, bmd->profile,
191                       vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp,
192                       dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp,
193                       harden_normals, face_strength_mode,
194                       miter_outer, miter_inner, spread, mesh->smoothresh);
195
196         result = BKE_mesh_from_bmesh_for_eval_nomain(bm, 0);
197
198         BLI_assert(bm->vtoolflagpool == NULL &&
199                    bm->etoolflagpool == NULL &&
200                    bm->ftoolflagpool == NULL);  /* make sure we never alloc'd these */
201         BM_mesh_free(bm);
202
203         if (bmd->clnordata.faceHash)
204                 BLI_ghash_free(bmd->clnordata.faceHash, NULL, NULL);
205
206         result->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
207
208         return result;
209 }
210
211 static bool dependsOnNormals(ModifierData *UNUSED(md))
212 {
213         return true;
214 }
215
216 ModifierTypeInfo modifierType_Bevel = {
217         /* name */              "Bevel",
218         /* structName */        "BevelModifierData",
219         /* structSize */        sizeof(BevelModifierData),
220         /* type */              eModifierTypeType_Constructive,
221         /* flags */             eModifierTypeFlag_AcceptsMesh |
222                                 eModifierTypeFlag_SupportsEditmode |
223                                 eModifierTypeFlag_EnableInEditmode |
224                                 eModifierTypeFlag_AcceptsCVs,
225
226         /* copyData */          copyData,
227
228         /* deformVerts_DM */    NULL,
229         /* deformMatrices_DM */ NULL,
230         /* deformVertsEM_DM */  NULL,
231         /* deformMatricesEM_DM*/NULL,
232         /* applyModifier_DM */  NULL,
233
234         /* deformVerts */       NULL,
235         /* deformMatrices */    NULL,
236         /* deformVertsEM */     NULL,
237         /* deformMatricesEM */  NULL,
238         /* applyModifier */     applyModifier,
239
240         /* initData */          initData,
241         /* requiredDataMask */  requiredDataMask,
242         /* freeData */          NULL,
243         /* isDisabled */        NULL,
244         /* updateDepsgraph */   NULL,
245         /* dependsOnTime */     NULL,
246         /* dependsOnNormals */  dependsOnNormals,
247         /* foreachObjectLink */ NULL,
248         /* foreachIDLink */     NULL,
249         /* foreachTexLink */    NULL,
250 };