Pass EvaluationContext argument everywhere
[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         BevelModifierData *bmd = (BevelModifierData *) md;
69         BevelModifierData *tbmd = (BevelModifierData *) target;
70
71         tbmd->value = bmd->value;
72         tbmd->res = bmd->res;
73         tbmd->flags = bmd->flags;
74         tbmd->val_flags = bmd->val_flags;
75         tbmd->lim_flags = bmd->lim_flags;
76         tbmd->e_flags = bmd->e_flags;
77         tbmd->mat = bmd->mat;
78         tbmd->profile = bmd->profile;
79         tbmd->bevel_angle = bmd->bevel_angle;
80         BLI_strncpy(tbmd->defgrp_name, bmd->defgrp_name, sizeof(tbmd->defgrp_name));
81 }
82
83 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
84 {
85         BevelModifierData *bmd = (BevelModifierData *)md;
86         CustomDataMask dataMask = 0;
87
88         /* ask for vertexgroups if we need them */
89         if (bmd->defgrp_name[0]) dataMask |= CD_MASK_MDEFORMVERT;
90
91         return dataMask;
92 }
93
94 /*
95  * This calls the new bevel code (added since 2.64)
96  */
97 static DerivedMesh *applyModifier(ModifierData *md, struct EvaluationContext *UNUSED(eval_ctx),
98                                   struct Object *ob, DerivedMesh *dm,
99                                   ModifierApplyFlag UNUSED(flag))
100 {
101         DerivedMesh *result;
102         BMesh *bm;
103         BMIter iter;
104         BMEdge *e;
105         BMVert *v;
106         float weight, weight2;
107         int vgroup = -1;
108         MDeformVert *dvert = NULL;
109         BevelModifierData *bmd = (BevelModifierData *) md;
110         const float threshold = cosf(bmd->bevel_angle + 0.000000175f);
111         const bool vertex_only = (bmd->flags & MOD_BEVEL_VERT) != 0;
112         const bool do_clamp = !(bmd->flags & MOD_BEVEL_OVERLAP_OK);
113         const int offset_type = bmd->val_flags;
114         const int mat = CLAMPIS(bmd->mat, -1, ob->totcol - 1);
115         const bool loop_slide = (bmd->flags & MOD_BEVEL_EVEN_WIDTHS) == 0;
116
117         bm = DM_to_bmesh(dm, true);
118         if ((bmd->lim_flags & MOD_BEVEL_VGROUP) && bmd->defgrp_name[0])
119                 modifier_get_vgroup(ob, dm, bmd->defgrp_name, &dvert, &vgroup);
120
121         if (vertex_only) {
122                 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
123                         if (!BM_vert_is_manifold(v))
124                                 continue;
125                         if (bmd->lim_flags & MOD_BEVEL_WEIGHT) {
126                                 weight = BM_elem_float_data_get(&bm->vdata, v, CD_BWEIGHT);
127                                 if (weight == 0.0f)
128                                         continue;
129                         }
130                         else if (vgroup != -1) {
131                                 weight = defvert_array_find_weight_safe(dvert, BM_elem_index_get(v), vgroup);
132                                 /* Check is against 0.5 rather than != 0.0 because cascaded bevel modifiers will
133                                  * interpolate weights for newly created vertices, and may cause unexpected "selection" */
134                                 if (weight < 0.5f)
135                                         continue;
136                         }
137                         BM_elem_flag_enable(v, BM_ELEM_TAG);
138                 }
139         }
140         else if (bmd->lim_flags & MOD_BEVEL_ANGLE) {
141                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
142                         /* check for 1 edge having 2 face users */
143                         BMLoop *l_a, *l_b;
144                         if (BM_edge_loop_pair(e, &l_a, &l_b)) {
145                                 if (dot_v3v3(l_a->f->no, l_b->f->no) < threshold) {
146                                         BM_elem_flag_enable(e, BM_ELEM_TAG);
147                                         BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
148                                         BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
149                                 }
150                         }
151                 }
152         }
153         else {
154                 /* crummy, is there a way just to operator on all? - campbell */
155                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
156                         if (BM_edge_is_manifold(e)) {
157                                 if (bmd->lim_flags & MOD_BEVEL_WEIGHT) {
158                                         weight = BM_elem_float_data_get(&bm->edata, e, CD_BWEIGHT);
159                                         if (weight == 0.0f)
160                                                 continue;
161                                 }
162                                 else if (vgroup != -1) {
163                                         weight = defvert_array_find_weight_safe(dvert, BM_elem_index_get(e->v1), vgroup);
164                                         weight2 = defvert_array_find_weight_safe(dvert, BM_elem_index_get(e->v2), vgroup);
165                                         if (weight < 0.5f || weight2 < 0.5f)
166                                                 continue;
167                                 }
168                                 BM_elem_flag_enable(e, BM_ELEM_TAG);
169                                 BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
170                                 BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
171                         }
172                 }
173         }
174
175         BM_mesh_bevel(bm, bmd->value, offset_type, bmd->res, bmd->profile,
176                       vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp,
177                       dvert, vgroup, mat, loop_slide);
178
179         result = CDDM_from_bmesh(bm, true);
180
181         BLI_assert(bm->vtoolflagpool == NULL &&
182                    bm->etoolflagpool == NULL &&
183                    bm->ftoolflagpool == NULL);  /* make sure we never alloc'd these */
184         BM_mesh_free(bm);
185
186         result->dirty |= DM_DIRTY_NORMALS;
187
188         return result;
189 }
190
191 static bool dependsOnNormals(ModifierData *UNUSED(md))
192 {
193         return true;
194 }
195
196 ModifierTypeInfo modifierType_Bevel = {
197         /* name */              "Bevel",
198         /* structName */        "BevelModifierData",
199         /* structSize */        sizeof(BevelModifierData),
200         /* type */              eModifierTypeType_Constructive,
201         /* flags */             eModifierTypeFlag_AcceptsMesh |
202                                 eModifierTypeFlag_SupportsEditmode |
203                                 eModifierTypeFlag_EnableInEditmode,
204
205         /* copyData */          copyData,
206         /* deformVerts */       NULL,
207         /* deformMatrices */    NULL,
208         /* deformVertsEM */     NULL,
209         /* deformMatricesEM */  NULL,
210         /* applyModifier */     applyModifier,
211         /* applyModifierEM */   NULL,
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 };