Enable new bevel tool code in bevel modifier.
[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 #include "BKE_mesh.h"
45 #include "BKE_bmesh.h" /* only for defines */
46
47 #include "MOD_util.h"
48
49 #include "bmesh.h"
50
51 #include "MEM_guardedalloc.h"
52
53
54 static void initData(ModifierData *md)
55 {
56         BevelModifierData *bmd = (BevelModifierData *) md;
57
58         bmd->value = 0.1f;
59         bmd->res = 1;
60         bmd->flags = 0;
61         bmd->val_flags = 0;
62         bmd->lim_flags = 0;
63         bmd->e_flags = 0;
64         bmd->bevel_angle = 30;
65         bmd->defgrp_name[0] = '\0';
66 }
67
68 static void copyData(ModifierData *md, ModifierData *target)
69 {
70         BevelModifierData *bmd = (BevelModifierData *) md;
71         BevelModifierData *tbmd = (BevelModifierData *) target;
72
73         tbmd->value = bmd->value;
74         tbmd->res = bmd->res;
75         tbmd->flags = bmd->flags;
76         tbmd->val_flags = bmd->val_flags;
77         tbmd->lim_flags = bmd->lim_flags;
78         tbmd->e_flags = bmd->e_flags;
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 #ifdef USE_BM_BEVEL_OP_AS_MOD
95
96 /*
97  * This calls the new bevel code (added since 2.64)
98  */
99 static DerivedMesh *applyModifier(ModifierData *md, struct Object *ob,
100                                   DerivedMesh *dm,
101                                   ModifierApplyFlag UNUSED(flag))
102 {
103         DerivedMesh *result;
104         BMesh *bm;
105         BMIter iter;
106         BMEdge *e;
107         BMVert *v;
108         float weight;
109         int vgroup = -1;
110         MDeformVert *dvert = NULL;
111         BevelModifierData *bmd = (BevelModifierData *) md;
112         const float threshold = cosf((bmd->bevel_angle + 0.00001f) * (float)M_PI / 180.0f);
113         const bool vertex_only = bmd->flags & BME_BEVEL_VERT;
114
115         bm = DM_to_bmesh(dm);
116
117         if (vertex_only) {
118                 if ((bmd->lim_flags & BME_BEVEL_VGROUP) && bmd->defgrp_name[0]) {
119                         modifier_get_vgroup(ob, dm, bmd->defgrp_name, &dvert, &vgroup);
120                 }
121                 BM_ITER_MESH(v, &iter, bm, BM_VERTS_OF_MESH) {
122                         if (!BM_vert_is_manifold(v))
123                                 continue;
124                         if (vgroup != -1) {
125                                 /* Is it safe to assume bmesh indices and dvert array line up?? */
126                                 weight = defvert_array_find_weight_safe(dvert, BM_elem_index_get(v), vgroup);
127                                 if (weight <= 0.0f)
128                                         continue;
129                         }
130                         BM_elem_flag_enable(v, BM_ELEM_TAG);
131                 }
132         }
133         else if (bmd->lim_flags & BME_BEVEL_ANGLE) {
134                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
135                         /* check for 1 edge having 2 face users */
136                         BMLoop *l_a, *l_b;
137                         if (BM_edge_loop_pair(e, &l_a, &l_b)) {
138                                 if (dot_v3v3(l_a->f->no, l_b->f->no) < threshold) {
139                                         BM_elem_flag_enable(e, BM_ELEM_TAG);
140                                         BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
141                                         BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
142                                 }
143                         }
144                 }
145         }
146         else {
147                 /* crummy, is there a way just to operator on all? - campbell */
148                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
149                         if (BM_edge_is_manifold(e)) {
150                                 if (bmd->lim_flags & BME_BEVEL_WEIGHT) {
151                                         weight = BM_elem_float_data_get(&bm->edata, e, CD_BWEIGHT);
152                                         if (weight == 0.0f)
153                                                 continue;
154                                 }
155                                 BM_elem_flag_enable(e, BM_ELEM_TAG);
156                                 BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
157                                 BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
158                         }
159                 }
160         }
161
162         BM_mesh_bevel(bm, bmd->value, bmd->res,
163                       vertex_only, bmd->lim_flags & BME_BEVEL_WEIGHT, dvert, vgroup);
164
165         result = CDDM_from_bmesh(bm, TRUE);
166
167         BLI_assert(bm->vtoolflagpool == NULL &&
168                    bm->etoolflagpool == NULL &&
169                    bm->ftoolflagpool == NULL);  /* make sure we never alloc'd these */
170         BM_mesh_free(bm);
171
172         CDDM_calc_normals(result);
173
174         return result;
175 }
176
177
178 #else /* from trunk, see note above */
179
180 static DerivedMesh *applyModifier(ModifierData *md, Object *UNUSED(ob),
181                                   DerivedMesh *derivedData,
182                                   ModifierApplyFlag UNUSED(flag))
183 {
184         DerivedMesh *result;
185         BMesh *bm;
186
187         /*bDeformGroup *def;*/
188         int /*i,*/ options, defgrp_index = -1;
189         BevelModifierData *bmd = (BevelModifierData *) md;
190
191         options = bmd->flags | bmd->val_flags | bmd->lim_flags | bmd->e_flags;
192
193 #if 0
194         if ((options & BME_BEVEL_VWEIGHT) && bmd->defgrp_name[0]) {
195                 defgrp_index = defgroup_name_index(ob, bmd->defgrp_name);
196                 if (defgrp_index == -1) {
197                         options &= ~BME_BEVEL_VWEIGHT;
198                 }
199         }
200 #endif
201
202         bm = DM_to_bmesh(derivedData);
203         BME_bevel(bm, bmd->value, bmd->res, options, defgrp_index, DEG2RADF(bmd->bevel_angle), NULL);
204         result = CDDM_from_bmesh(bm, TRUE);
205         BM_mesh_free(bm);
206
207         /* until we allow for dirty normal flag, always calc,
208          * note: calculating on the CDDM is faster then the BMesh equivalent */
209         CDDM_calc_normals(result);
210
211         return result;
212 }
213
214 #endif
215
216 static DerivedMesh *applyModifierEM(ModifierData *md, Object *ob,
217                                     struct BMEditMesh *UNUSED(editData),
218                                     DerivedMesh *derivedData)
219 {
220         return applyModifier(md, ob, derivedData, MOD_APPLY_USECACHE);
221 }
222
223
224 ModifierTypeInfo modifierType_Bevel = {
225         /* name */              "Bevel",
226         /* structName */        "BevelModifierData",
227         /* structSize */        sizeof(BevelModifierData),
228         /* type */              eModifierTypeType_Constructive,
229         /* flags */             eModifierTypeFlag_AcceptsMesh |
230                                 eModifierTypeFlag_SupportsEditmode |
231                                 eModifierTypeFlag_EnableInEditmode,
232
233         /* copyData */          copyData,
234         /* deformVerts */       NULL,
235         /* deformMatrices */    NULL,
236         /* deformVertsEM */     NULL,
237         /* deformMatricesEM */  NULL,
238         /* applyModifier */     applyModifier,
239         /* applyModifierEM */   applyModifierEM,
240         /* initData */          initData,
241         /* requiredDataMask */  requiredDataMask,
242         /* freeData */          NULL,
243         /* isDisabled */        NULL,
244         /* updateDepgraph */    NULL,
245         /* dependsOnTime */     NULL,
246         /* dependsOnNormals */  NULL,
247         /* foreachObjectLink */ NULL,
248         /* foreachIDLink */     NULL,
249         /* foreachTexLink */    NULL,
250 };