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