Extract common modifier parameters into ModifierEvalContext struct
[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 "DNA_object_types.h"
36
37 #include "BLI_utildefines.h"
38 #include "BLI_math.h"
39 #include "BLI_string.h"
40
41 #include "BKE_cdderivedmesh.h"
42 #include "BKE_deform.h"
43 #include "BKE_modifier.h"
44
45 #include "MOD_util.h"
46
47 #include "bmesh.h"
48 #include "bmesh_tools.h"
49
50 static void initData(ModifierData *md)
51 {
52         BevelModifierData *bmd = (BevelModifierData *) md;
53
54         bmd->value = 0.1f;
55         bmd->res = 1;
56         bmd->flags = 0;
57         bmd->val_flags = MOD_BEVEL_AMT_OFFSET;
58         bmd->lim_flags = 0;
59         bmd->e_flags = 0;
60         bmd->mat = -1;
61         bmd->profile = 0.5f;
62         bmd->bevel_angle = DEG2RADF(30.0f);
63         bmd->defgrp_name[0] = '\0';
64 }
65
66 static void copyData(ModifierData *md, ModifierData *target)
67 {
68 #if 0
69         BevelModifierData *bmd = (BevelModifierData *) md;
70         BevelModifierData *tbmd = (BevelModifierData *) target;
71 #endif
72         modifier_copyData_generic(md, target);
73 }
74
75 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
76 {
77         BevelModifierData *bmd = (BevelModifierData *)md;
78         CustomDataMask dataMask = 0;
79
80         /* ask for vertexgroups if we need them */
81         if (bmd->defgrp_name[0]) dataMask |= CD_MASK_MDEFORMVERT;
82
83         return dataMask;
84 }
85
86 /*
87  * This calls the new bevel code (added since 2.64)
88  */
89 static DerivedMesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx,
90                                   DerivedMesh *dm)
91 {
92         DerivedMesh *result;
93         BMesh *bm;
94         BMIter iter;
95         BMEdge *e;
96         BMVert *v;
97         float weight, weight2;
98         int vgroup = -1;
99         MDeformVert *dvert = NULL;
100         BevelModifierData *bmd = (BevelModifierData *) md;
101         const float threshold = cosf(bmd->bevel_angle + 0.000000175f);
102         const bool vertex_only = (bmd->flags & MOD_BEVEL_VERT) != 0;
103         const bool do_clamp = !(bmd->flags & MOD_BEVEL_OVERLAP_OK);
104         const int offset_type = bmd->val_flags;
105         const int mat = CLAMPIS(bmd->mat, -1, ctx->object->totcol - 1);
106         const bool loop_slide = (bmd->flags & MOD_BEVEL_EVEN_WIDTHS) == 0;
107
108         bm = DM_to_bmesh(dm, true);
109         if ((bmd->lim_flags & MOD_BEVEL_VGROUP) && bmd->defgrp_name[0])
110                 modifier_get_vgroup(ctx->object, dm, bmd->defgrp_name, &dvert, &vgroup);
111
112         if (vertex_only) {
113                 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
114                         if (!BM_vert_is_manifold(v))
115                                 continue;
116                         if (bmd->lim_flags & MOD_BEVEL_WEIGHT) {
117                                 weight = BM_elem_float_data_get(&bm->vdata, v, CD_BWEIGHT);
118                                 if (weight == 0.0f)
119                                         continue;
120                         }
121                         else if (vgroup != -1) {
122                                 weight = defvert_array_find_weight_safe(dvert, BM_elem_index_get(v), vgroup);
123                                 /* Check is against 0.5 rather than != 0.0 because cascaded bevel modifiers will
124                                  * interpolate weights for newly created vertices, and may cause unexpected "selection" */
125                                 if (weight < 0.5f)
126                                         continue;
127                         }
128                         BM_elem_flag_enable(v, BM_ELEM_TAG);
129                 }
130         }
131         else if (bmd->lim_flags & MOD_BEVEL_ANGLE) {
132                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
133                         /* check for 1 edge having 2 face users */
134                         BMLoop *l_a, *l_b;
135                         if (BM_edge_loop_pair(e, &l_a, &l_b)) {
136                                 if (dot_v3v3(l_a->f->no, l_b->f->no) < threshold) {
137                                         BM_elem_flag_enable(e, BM_ELEM_TAG);
138                                         BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
139                                         BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
140                                 }
141                         }
142                 }
143         }
144         else {
145                 /* crummy, is there a way just to operator on all? - campbell */
146                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
147                         if (BM_edge_is_manifold(e)) {
148                                 if (bmd->lim_flags & MOD_BEVEL_WEIGHT) {
149                                         weight = BM_elem_float_data_get(&bm->edata, e, CD_BWEIGHT);
150                                         if (weight == 0.0f)
151                                                 continue;
152                                 }
153                                 else if (vgroup != -1) {
154                                         weight = defvert_array_find_weight_safe(dvert, BM_elem_index_get(e->v1), vgroup);
155                                         weight2 = defvert_array_find_weight_safe(dvert, BM_elem_index_get(e->v2), vgroup);
156                                         if (weight < 0.5f || weight2 < 0.5f)
157                                                 continue;
158                                 }
159                                 BM_elem_flag_enable(e, BM_ELEM_TAG);
160                                 BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
161                                 BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
162                         }
163                 }
164         }
165
166         BM_mesh_bevel(bm, bmd->value, offset_type, bmd->res, bmd->profile,
167                       vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp,
168                       dvert, vgroup, mat, loop_slide);
169
170         result = CDDM_from_bmesh(bm, true);
171
172         BLI_assert(bm->vtoolflagpool == NULL &&
173                    bm->etoolflagpool == NULL &&
174                    bm->ftoolflagpool == NULL);  /* make sure we never alloc'd these */
175         BM_mesh_free(bm);
176
177         result->dirty |= DM_DIRTY_NORMALS;
178
179         return result;
180 }
181
182 static bool dependsOnNormals(ModifierData *UNUSED(md))
183 {
184         return true;
185 }
186
187 ModifierTypeInfo modifierType_Bevel = {
188         /* name */              "Bevel",
189         /* structName */        "BevelModifierData",
190         /* structSize */        sizeof(BevelModifierData),
191         /* type */              eModifierTypeType_Constructive,
192         /* flags */             eModifierTypeFlag_AcceptsMesh |
193                                 eModifierTypeFlag_SupportsEditmode |
194                                 eModifierTypeFlag_EnableInEditmode,
195
196         /* copyData */          copyData,
197
198         /* deformVerts_DM */    NULL,
199         /* deformMatrices_DM */ NULL,
200         /* deformVertsEM_DM */  NULL,
201         /* deformMatricesEM_DM*/NULL,
202         /* applyModifier_DM */  applyModifier,
203         /* applyModifierEM_DM */NULL,
204
205         /* deformVerts */       NULL,
206         /* deformMatrices */    NULL,
207         /* deformVertsEM */     NULL,
208         /* deformMatricesEM */  NULL,
209         /* applyModifier */     NULL,
210         /* applyModifierEM */   NULL,
211
212         /* initData */          initData,
213         /* requiredDataMask */  requiredDataMask,
214         /* freeData */          NULL,
215         /* isDisabled */        NULL,
216         /* updateDepsgraph */   NULL,
217         /* dependsOnTime */     NULL,
218         /* dependsOnNormals */  dependsOnNormals,
219         /* foreachObjectLink */ NULL,
220         /* foreachIDLink */     NULL,
221         /* foreachTexLink */    NULL,
222 };