f12bad5c3e8f10ac655862b0abf540c440ff61d5
[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, struct Depsgraph *UNUSED(depsgraph),
90                                   struct Object *ob, DerivedMesh *dm,
91                                   ModifierApplyFlag UNUSED(flag))
92 {
93         DerivedMesh *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 int mat = CLAMPIS(bmd->mat, -1, ob->totcol - 1);
107         const bool loop_slide = (bmd->flags & MOD_BEVEL_EVEN_WIDTHS) == 0;
108
109         bm = DM_to_bmesh(dm, true);
110         if ((bmd->lim_flags & MOD_BEVEL_VGROUP) && bmd->defgrp_name[0])
111                 modifier_get_vgroup(ob, dm, bmd->defgrp_name, &dvert, &vgroup);
112
113         if (vertex_only) {
114                 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
115                         if (!BM_vert_is_manifold(v))
116                                 continue;
117                         if (bmd->lim_flags & MOD_BEVEL_WEIGHT) {
118                                 weight = BM_elem_float_data_get(&bm->vdata, v, CD_BWEIGHT);
119                                 if (weight == 0.0f)
120                                         continue;
121                         }
122                         else if (vgroup != -1) {
123                                 weight = defvert_array_find_weight_safe(dvert, BM_elem_index_get(v), vgroup);
124                                 /* Check is against 0.5 rather than != 0.0 because cascaded bevel modifiers will
125                                  * interpolate weights for newly created vertices, and may cause unexpected "selection" */
126                                 if (weight < 0.5f)
127                                         continue;
128                         }
129                         BM_elem_flag_enable(v, BM_ELEM_TAG);
130                 }
131         }
132         else if (bmd->lim_flags & MOD_BEVEL_ANGLE) {
133                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
134                         /* check for 1 edge having 2 face users */
135                         BMLoop *l_a, *l_b;
136                         if (BM_edge_loop_pair(e, &l_a, &l_b)) {
137                                 if (dot_v3v3(l_a->f->no, l_b->f->no) < threshold) {
138                                         BM_elem_flag_enable(e, BM_ELEM_TAG);
139                                         BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
140                                         BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
141                                 }
142                         }
143                 }
144         }
145         else {
146                 /* crummy, is there a way just to operator on all? - campbell */
147                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
148                         if (BM_edge_is_manifold(e)) {
149                                 if (bmd->lim_flags & MOD_BEVEL_WEIGHT) {
150                                         weight = BM_elem_float_data_get(&bm->edata, e, CD_BWEIGHT);
151                                         if (weight == 0.0f)
152                                                 continue;
153                                 }
154                                 else if (vgroup != -1) {
155                                         weight = defvert_array_find_weight_safe(dvert, BM_elem_index_get(e->v1), vgroup);
156                                         weight2 = defvert_array_find_weight_safe(dvert, BM_elem_index_get(e->v2), vgroup);
157                                         if (weight < 0.5f || weight2 < 0.5f)
158                                                 continue;
159                                 }
160                                 BM_elem_flag_enable(e, BM_ELEM_TAG);
161                                 BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
162                                 BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
163                         }
164                 }
165         }
166
167         BM_mesh_bevel(bm, bmd->value, offset_type, bmd->res, bmd->profile,
168                       vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp,
169                       dvert, vgroup, mat, loop_slide);
170
171         result = CDDM_from_bmesh(bm, true);
172
173         BLI_assert(bm->vtoolflagpool == NULL &&
174                    bm->etoolflagpool == NULL &&
175                    bm->ftoolflagpool == NULL);  /* make sure we never alloc'd these */
176         BM_mesh_free(bm);
177
178         result->dirty |= DM_DIRTY_NORMALS;
179
180         return result;
181 }
182
183 static bool dependsOnNormals(ModifierData *UNUSED(md))
184 {
185         return true;
186 }
187
188 ModifierTypeInfo modifierType_Bevel = {
189         /* name */              "Bevel",
190         /* structName */        "BevelModifierData",
191         /* structSize */        sizeof(BevelModifierData),
192         /* type */              eModifierTypeType_Constructive,
193         /* flags */             eModifierTypeFlag_AcceptsMesh |
194                                 eModifierTypeFlag_SupportsEditmode |
195                                 eModifierTypeFlag_EnableInEditmode,
196
197         /* copyData */          copyData,
198
199         /* deformVerts_DM */    NULL,
200         /* deformMatrices_DM */ NULL,
201         /* deformVertsEM_DM */  NULL,
202         /* deformMatricesEM_DM*/NULL,
203         /* applyModifier_DM */  applyModifier,
204         /* applyModifierEM_DM */NULL,
205
206         /* deformVerts */       NULL,
207         /* deformMatrices */    NULL,
208         /* deformVertsEM */     NULL,
209         /* deformMatricesEM */  NULL,
210         /* applyModifier */     NULL,
211         /* applyModifierEM */   NULL,
212
213         /* initData */          initData,
214         /* requiredDataMask */  requiredDataMask,
215         /* freeData */          NULL,
216         /* isDisabled */        NULL,
217         /* updateDepsgraph */   NULL,
218         /* dependsOnTime */     NULL,
219         /* dependsOnNormals */  dependsOnNormals,
220         /* foreachObjectLink */ NULL,
221         /* foreachIDLink */     NULL,
222         /* foreachTexLink */    NULL,
223 };