code cleanup: make bmesh operator names more consistant since python has access to...
[blender.git] / source / blender / modifiers / intern / MOD_smooth.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_smooth.c
32  *  \ingroup modifiers
33  */
34
35
36 #include "DNA_meshdata_types.h"
37
38 #include "BLI_math.h"
39 #include "BLI_utildefines.h"
40 #include "BLI_string.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BKE_cdderivedmesh.h"
45 #include "BKE_particle.h"
46 #include "BKE_deform.h"
47
48 #include "MOD_modifiertypes.h"
49 #include "MOD_util.h"
50
51
52 static void initData(ModifierData *md)
53 {
54         SmoothModifierData *smd = (SmoothModifierData *) md;
55
56         smd->fac = 0.5f;
57         smd->repeat = 1;
58         smd->flag = MOD_SMOOTH_X | MOD_SMOOTH_Y | MOD_SMOOTH_Z;
59         smd->defgrp_name[0] = '\0';
60 }
61
62 static void copyData(ModifierData *md, ModifierData *target)
63 {
64         SmoothModifierData *smd = (SmoothModifierData *) md;
65         SmoothModifierData *tsmd = (SmoothModifierData *) target;
66
67         tsmd->fac = smd->fac;
68         tsmd->repeat = smd->repeat;
69         tsmd->flag = smd->flag;
70         BLI_strncpy(tsmd->defgrp_name, smd->defgrp_name, sizeof(tsmd->defgrp_name));
71 }
72
73 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
74 {
75         SmoothModifierData *smd = (SmoothModifierData *) md;
76         short flag;
77
78         flag = smd->flag & (MOD_SMOOTH_X | MOD_SMOOTH_Y | MOD_SMOOTH_Z);
79
80         /* disable if modifier is off for X, Y and Z or if factor is 0 */
81         if ((smd->fac == 0.0f) || flag == 0) return 1;
82
83         return 0;
84 }
85
86 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
87 {
88         SmoothModifierData *smd = (SmoothModifierData *)md;
89         CustomDataMask dataMask = 0;
90
91         /* ask for vertexgroups if we need them */
92         if (smd->defgrp_name[0]) dataMask |= CD_MASK_MDEFORMVERT;
93
94         return dataMask;
95 }
96
97 static void smoothModifier_do(
98         SmoothModifierData *smd, Object *ob, DerivedMesh *dm,
99         float (*vertexCos)[3], int numVerts)
100 {
101         MDeformVert *dvert = NULL;
102         MEdge *medges = NULL;
103
104         int i, j, numDMEdges, defgrp_index;
105         unsigned char *uctmp;
106         float *ftmp, fac, facm;
107
108         ftmp = (float *)MEM_callocN(3 * sizeof(float) * numVerts,
109                                     "smoothmodifier_f");
110         if (!ftmp) return;
111         uctmp = (unsigned char *)MEM_callocN(sizeof(unsigned char) * numVerts,
112                                              "smoothmodifier_uc");
113         if (!uctmp) {
114                 if (ftmp) MEM_freeN(ftmp);
115                 return;
116         }
117
118         fac = smd->fac;
119         facm = 1 - fac;
120
121         if (dm->getNumVerts(dm) == numVerts) {
122                 medges = dm->getEdgeArray(dm);
123                 numDMEdges = dm->getNumEdges(dm);
124         }
125         else {
126                 medges = NULL;
127                 numDMEdges = 0;
128         }
129
130         modifier_get_vgroup(ob, dm, smd->defgrp_name, &dvert, &defgrp_index);
131
132         /* NOTICE: this can be optimized a little bit by moving the
133          * if (dvert) out of the loop, if needed */
134         for (j = 0; j < smd->repeat; j++) {
135                 for (i = 0; i < numDMEdges; i++) {
136                         float fvec[3];
137                         float *v1, *v2;
138                         unsigned int idx1, idx2;
139
140                         idx1 = medges[i].v1;
141                         idx2 = medges[i].v2;
142
143                         v1 = vertexCos[idx1];
144                         v2 = vertexCos[idx2];
145
146                         mid_v3_v3v3(fvec, v1, v2);
147
148                         v1 = &ftmp[idx1 * 3];
149                         v2 = &ftmp[idx2 * 3];
150
151                         if (uctmp[idx1] < 255) {
152                                 uctmp[idx1]++;
153                                 add_v3_v3(v1, fvec);
154                         }
155                         if (uctmp[idx2] < 255) {
156                                 uctmp[idx2]++;
157                                 add_v3_v3(v2, fvec);
158                         }
159                 }
160
161                 if (dvert) {
162                         MDeformVert *dv = dvert;
163                         for (i = 0; i < numVerts; i++, dv++) {
164                                 float f, fm, facw, *fp, *v;
165                                 short flag = smd->flag;
166
167                                 v = vertexCos[i];
168                                 fp = &ftmp[i * 3];
169
170
171                                 f = defvert_find_weight(dv, defgrp_index);
172                                 if (f <= 0.0f) continue;
173
174                                 f *= fac;
175                                 fm = 1.0f - f;
176
177                                 /* fp is the sum of uctmp[i] verts, so must be averaged */
178                                 facw = 0.0f;
179                                 if (uctmp[i]) 
180                                         facw = f / (float)uctmp[i];
181
182                                 if (flag & MOD_SMOOTH_X)
183                                         v[0] = fm * v[0] + facw * fp[0];
184                                 if (flag & MOD_SMOOTH_Y)
185                                         v[1] = fm * v[1] + facw * fp[1];
186                                 if (flag & MOD_SMOOTH_Z)
187                                         v[2] = fm * v[2] + facw * fp[2];
188                         }
189                 }
190                 else { /* no vertex group */
191                         for (i = 0; i < numVerts; i++) {
192                                 float facw, *fp, *v;
193                                 short flag = smd->flag;
194
195                                 v = vertexCos[i];
196                                 fp = &ftmp[i * 3];
197
198                                 /* fp is the sum of uctmp[i] verts, so must be averaged */
199                                 facw = 0.0f;
200                                 if (uctmp[i]) 
201                                         facw = fac / (float)uctmp[i];
202
203                                 if (flag & MOD_SMOOTH_X)
204                                         v[0] = facm * v[0] + facw * fp[0];
205                                 if (flag & MOD_SMOOTH_Y)
206                                         v[1] = facm * v[1] + facw * fp[1];
207                                 if (flag & MOD_SMOOTH_Z)
208                                         v[2] = facm * v[2] + facw * fp[2];
209                         }
210
211                 }
212
213                 memset(ftmp, 0, 3 * sizeof(float) * numVerts);
214                 memset(uctmp, 0, sizeof(unsigned char) * numVerts);
215         }
216
217         MEM_freeN(ftmp);
218         MEM_freeN(uctmp);
219 }
220
221 static void deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData,
222                         float (*vertexCos)[3], int numVerts, ModifierApplyFlag UNUSED(flag))
223 {
224         DerivedMesh *dm = get_dm(ob, NULL, derivedData, NULL, 0);
225
226         smoothModifier_do((SmoothModifierData *)md, ob, dm,
227                           vertexCos, numVerts);
228
229         if (dm != derivedData)
230                 dm->release(dm);
231 }
232
233 static void deformVertsEM(
234         ModifierData *md, Object *ob, struct BMEditMesh *editData,
235         DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
236 {
237         DerivedMesh *dm = get_dm(ob, editData, derivedData, NULL, 0);
238
239         smoothModifier_do((SmoothModifierData *)md, ob, dm,
240                           vertexCos, numVerts);
241
242         if (dm != derivedData)
243                 dm->release(dm);
244 }
245
246
247 ModifierTypeInfo modifierType_Smooth = {
248         /* name */              "Smooth",
249         /* structName */        "SmoothModifierData",
250         /* structSize */        sizeof(SmoothModifierData),
251         /* type */              eModifierTypeType_OnlyDeform,
252         /* flags */             eModifierTypeFlag_AcceptsMesh |
253                                 eModifierTypeFlag_AcceptsCVs |
254                                 eModifierTypeFlag_SupportsEditmode,
255
256         /* copyData */          copyData,
257         /* deformVerts */       deformVerts,
258         /* deformMatrices */    NULL,
259         /* deformVertsEM */     deformVertsEM,
260         /* deformMatricesEM */  NULL,
261         /* applyModifier */     NULL,
262         /* applyModifierEM */   NULL,
263         /* initData */          initData,
264         /* requiredDataMask */  requiredDataMask,
265         /* freeData */          NULL,
266         /* isDisabled */        isDisabled,
267         /* updateDepgraph */    NULL,
268         /* dependsOnTime */     NULL,
269         /* dependsOnNormals */  NULL,
270         /* foreachObjectLink */ NULL,
271         /* foreachIDLink */     NULL,
272         /* foreachTexLink */    NULL,
273 };