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