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