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