code cleanup: make bmesh operator names more consistant since python has access to...
[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 "BLI_utildefines.h"
36 #include "BLI_math.h"
37 #include "BLI_string.h"
38
39 #include "BKE_cdderivedmesh.h"
40 #include "BKE_modifier.h"
41 #include "BKE_mesh.h"
42 #include "BKE_bmesh.h" /* only for defines */
43
44 #include "bmesh.h"
45
46 #include "DNA_object_types.h"
47
48 #include "MEM_guardedalloc.h"
49
50
51 static void initData(ModifierData *md)
52 {
53         BevelModifierData *bmd = (BevelModifierData *) md;
54
55         bmd->value = 0.1f;
56         bmd->res = 1;
57         bmd->flags = 0;
58         bmd->val_flags = 0;
59         bmd->lim_flags = 0;
60         bmd->e_flags = 0;
61         bmd->bevel_angle = 30;
62         bmd->defgrp_name[0] = '\0';
63 }
64
65 static void copyData(ModifierData *md, ModifierData *target)
66 {
67         BevelModifierData *bmd = (BevelModifierData *) md;
68         BevelModifierData *tbmd = (BevelModifierData *) target;
69
70         tbmd->value = bmd->value;
71         tbmd->res = bmd->res;
72         tbmd->flags = bmd->flags;
73         tbmd->val_flags = bmd->val_flags;
74         tbmd->lim_flags = bmd->lim_flags;
75         tbmd->e_flags = bmd->e_flags;
76         tbmd->bevel_angle = bmd->bevel_angle;
77         BLI_strncpy(tbmd->defgrp_name, bmd->defgrp_name, sizeof(tbmd->defgrp_name));
78 }
79
80 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
81 {
82         BevelModifierData *bmd = (BevelModifierData *)md;
83         CustomDataMask dataMask = 0;
84
85         /* ask for vertexgroups if we need them */
86         if (bmd->defgrp_name[0]) dataMask |= CD_MASK_MDEFORMVERT;
87
88         return dataMask;
89 }
90
91 // #define USE_BM_BEVEL_OP_AS_MOD
92
93 #ifdef USE_BM_BEVEL_OP_AS_MOD
94
95 /* BMESH_TODO
96  *
97  * this bevel calls the new bevel code (added since 2.64)
98  * which is missing many of the options which the bevel modifier from 2.4x has.
99  * - no vertex bevel
100  * - no weight bevel
101  *
102  * These will need to be added to the bmesh operator.
103  * - campbell
104  */
105 static DerivedMesh *applyModifier(ModifierData *md, struct Object *UNUSED(ob),
106                                   DerivedMesh *dm,
107                                   ModifierApplyFlag UNUSED(flag))
108 {
109         DerivedMesh *result;
110         BMesh *bm;
111         BMIter iter;
112         BMEdge *e;
113         BevelModifierData *bmd = (BevelModifierData *) md;
114         const float threshold = cosf((bmd->bevel_angle + 0.00001f) * (float)M_PI / 180.0f);
115         const int segments = 16;  /* XXX */
116
117         bm = DM_to_bmesh(dm);
118
119         if (bmd->lim_flags & BME_BEVEL_ANGLE) {
120                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
121                         /* check for 1 edge having 2 face users */
122                         BMLoop *l_a, *l_b;
123                         if (BM_edge_loop_pair(e, &l_a, &l_b)) {
124                                 if (dot_v3v3(l_a->f->no, l_b->f->no) < threshold) {
125                                         BM_elem_flag_enable(e, BM_ELEM_TAG);
126                                         BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
127                                         BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
128                                 }
129                         }
130                 }
131         }
132         else {
133                 /* crummy, is there a way just to operator on all? - campbell */
134                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
135                         if (BM_edge_is_manifold(e)) {
136                                 BM_elem_flag_enable(e, BM_ELEM_TAG);
137                                 BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
138                                 BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
139                         }
140                 }
141         }
142
143         BM_mesh_bevel(bm, bmd->value, segments);
144
145         result = CDDM_from_bmesh(bm, TRUE);
146
147         BLI_assert(bm->toolflagpool == NULL);  /* make sure we never alloc'd this */
148         BM_mesh_free(bm);
149
150         CDDM_calc_normals(result);
151
152         return result;
153 }
154
155
156 #else /* from trunk, see note above */
157
158 static DerivedMesh *applyModifier(ModifierData *md, Object *UNUSED(ob),
159                                   DerivedMesh *derivedData,
160                                   ModifierApplyFlag UNUSED(flag))
161 {
162         DerivedMesh *result;
163         BMesh *bm;
164
165         /*bDeformGroup *def;*/
166         int /*i,*/ options, defgrp_index = -1;
167         BevelModifierData *bmd = (BevelModifierData *) md;
168
169         options = bmd->flags | bmd->val_flags | bmd->lim_flags | bmd->e_flags;
170
171 #if 0
172         if ((options & BME_BEVEL_VWEIGHT) && bmd->defgrp_name[0]) {
173                 defgrp_index = defgroup_name_index(ob, bmd->defgrp_name);
174                 if (defgrp_index == -1) {
175                         options &= ~BME_BEVEL_VWEIGHT;
176                 }
177         }
178 #endif
179
180         bm = DM_to_bmesh(derivedData);
181         BME_bevel(bm, bmd->value, bmd->res, options, defgrp_index, DEG2RADF(bmd->bevel_angle), NULL);
182         result = CDDM_from_bmesh(bm, TRUE);
183         BM_mesh_free(bm);
184
185         /* until we allow for dirty normal flag, always calc,
186          * note: calculating on the CDDM is faster then the BMesh equivalent */
187         CDDM_calc_normals(result);
188
189         return result;
190 }
191
192 #endif
193
194 static DerivedMesh *applyModifierEM(ModifierData *md, Object *ob,
195                                     struct BMEditMesh *UNUSED(editData),
196                                     DerivedMesh *derivedData)
197 {
198         return applyModifier(md, ob, derivedData, MOD_APPLY_USECACHE);
199 }
200
201
202 ModifierTypeInfo modifierType_Bevel = {
203         /* name */              "Bevel",
204         /* structName */        "BevelModifierData",
205         /* structSize */        sizeof(BevelModifierData),
206         /* type */              eModifierTypeType_Constructive,
207         /* flags */             eModifierTypeFlag_AcceptsMesh |
208                                 eModifierTypeFlag_SupportsEditmode |
209                                 eModifierTypeFlag_EnableInEditmode,
210
211         /* copyData */          copyData,
212         /* deformVerts */       NULL,
213         /* deformMatrices */    NULL,
214         /* deformVertsEM */     NULL,
215         /* deformMatricesEM */  NULL,
216         /* applyModifier */     applyModifier,
217         /* applyModifierEM */   applyModifierEM,
218         /* initData */          initData,
219         /* requiredDataMask */  requiredDataMask,
220         /* freeData */          NULL,
221         /* isDisabled */        NULL,
222         /* updateDepgraph */    NULL,
223         /* dependsOnTime */     NULL,
224         /* dependsOnNormals */  NULL,
225         /* foreachObjectLink */ NULL,
226         /* foreachIDLink */     NULL,
227         /* foreachTexLink */    NULL,
228 };