svn merge -r39286:39385 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / modifiers / intern / MOD_simpledeform.c
1 /*
2 * $Id$
3 *
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software  Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 * The Original Code is Copyright (C) 2005 by the Blender Foundation.
21 * All rights reserved.
22 *
23 * Contributor(s): Daniel Dunbar
24 *                 Ton Roosendaal,
25 *                 Ben Batt,
26 *                 Brecht Van Lommel,
27 *                 Campbell Barton
28 *
29 * ***** END GPL LICENSE BLOCK *****
30 *
31 */
32
33 /** \file blender/modifiers/intern/MOD_simpledeform.c
34  *  \ingroup modifiers
35  */
36
37
38 #include "DNA_meshdata_types.h"
39 #include "DNA_object_types.h"
40
41 #include "BLI_math.h"
42 #include "BLI_string.h"
43 #include "BLI_utildefines.h"
44
45 #include "BKE_cdderivedmesh.h"
46 #include "BKE_lattice.h"
47 #include "BKE_modifier.h"
48 #include "BKE_deform.h"
49 #include "BKE_shrinkwrap.h"
50
51
52 #include "depsgraph_private.h"
53
54 #include "MOD_util.h"
55
56
57
58 /* Clamps/Limits the given coordinate to:  limits[0] <= co[axis] <= limits[1]
59  * The amount of clamp is saved on dcut */
60 static void axis_limit(int axis, const float limits[2], float co[3], float dcut[3])
61 {
62         float val = co[axis];
63         if(limits[0] > val) val = limits[0];
64         if(limits[1] < val) val = limits[1];
65
66         dcut[axis] = co[axis] - val;
67         co[axis] = val;
68 }
69
70 static void simpleDeform_taper(const float factor, const float dcut[3], float *co)
71 {
72         float x = co[0], y = co[1], z = co[2];
73         float scale = z*factor;
74
75         co[0] = x + x*scale;
76         co[1] = y + y*scale;
77         co[2] = z;
78
79         if(dcut)
80         {
81                 co[0] += dcut[0];
82                 co[1] += dcut[1];
83                 co[2] += dcut[2];
84         }
85 }
86
87 static void simpleDeform_stretch(const float factor, const float dcut[3], float *co)
88 {
89         float x = co[0], y = co[1], z = co[2];
90         float scale;
91
92         scale = (z*z*factor-factor + 1.0f);
93
94         co[0] = x*scale;
95         co[1] = y*scale;
96         co[2] = z*(1.0f+factor);
97
98
99         if(dcut)
100         {
101                 co[0] += dcut[0];
102                 co[1] += dcut[1];
103                 co[2] += dcut[2];
104         }
105 }
106
107 static void simpleDeform_twist(const float factor, const float *dcut, float *co)
108 {
109         float x = co[0], y = co[1], z = co[2];
110         float theta, sint, cost;
111
112         theta = z*factor;
113         sint  = sin(theta);
114         cost  = cos(theta);
115
116         co[0] = x*cost - y*sint;
117         co[1] = x*sint + y*cost;
118         co[2] = z;
119
120         if(dcut)
121         {
122                 co[0] += dcut[0];
123                 co[1] += dcut[1];
124                 co[2] += dcut[2];
125         }
126 }
127
128 static void simpleDeform_bend(const float factor, const float dcut[3], float *co)
129 {
130         float x = co[0], y = co[1], z = co[2];
131         float theta, sint, cost;
132
133         theta = x*factor;
134         sint = sin(theta);
135         cost = cos(theta);
136
137         if(fabsf(factor) > 1e-7f)
138         {
139                 co[0] = -(y-1.0f/factor)*sint;
140                 co[1] =  (y-1.0f/factor)*cost + 1.0f/factor;
141                 co[2] = z;
142         }
143
144
145         if(dcut)
146         {
147                 co[0] += cost*dcut[0];
148                 co[1] += sint*dcut[0];
149                 co[2] += dcut[2];
150         }
151
152 }
153
154
155 /* simple deform modifier */
156 static void SimpleDeformModifier_do(SimpleDeformModifierData *smd, struct Object *ob, struct DerivedMesh *dm, float (*vertexCos)[3], int numVerts)
157 {
158         static const float lock_axis[2] = {0.0f, 0.0f};
159
160         int i;
161         int limit_axis = 0;
162         float smd_limit[2], smd_factor;
163         SpaceTransform *transf = NULL, tmp_transf;
164         void (*simpleDeform_callback)(const float factor, const float dcut[3], float *co) = NULL;       //Mode callback
165         int vgroup;
166         MDeformVert *dvert;
167
168         //Safe-check
169         if(smd->origin == ob) smd->origin = NULL;                                       //No self references
170
171         if(smd->limit[0] < 0.0f) smd->limit[0] = 0.0f;
172         if(smd->limit[0] > 1.0f) smd->limit[0] = 1.0f;
173
174         smd->limit[0] = MIN2(smd->limit[0], smd->limit[1]);                     //Upper limit >= than lower limit
175
176         //Calculate matrixs do convert between coordinate spaces
177         if(smd->origin)
178         {
179                 transf = &tmp_transf;
180
181                 if(smd->originOpts & MOD_SIMPLEDEFORM_ORIGIN_LOCAL)
182                 {
183                         space_transform_from_matrixs(transf, ob->obmat, smd->origin->obmat);
184                 }
185                 else
186                 {
187                         copy_m4_m4(transf->local2target, smd->origin->obmat);
188                         invert_m4_m4(transf->target2local, transf->local2target);
189                 }
190         }
191
192         //Setup vars
193         limit_axis  = (smd->mode == MOD_SIMPLEDEFORM_MODE_BEND) ? 0 : 2; //Bend limits on X.. all other modes limit on Z
194
195         //Update limits if needed
196         {
197                 float lower =  FLT_MAX;
198                 float upper = -FLT_MAX;
199
200                 for(i=0; i<numVerts; i++)
201                 {
202                         float tmp[3];
203                         VECCOPY(tmp, vertexCos[i]);
204
205                         if(transf) space_transform_apply(transf, tmp);
206
207                         lower = MIN2(lower, tmp[limit_axis]);
208                         upper = MAX2(upper, tmp[limit_axis]);
209                 }
210
211
212                 //SMD values are normalized to the BV, calculate the absolut values
213                 smd_limit[1] = lower + (upper-lower)*smd->limit[1];
214                 smd_limit[0] = lower + (upper-lower)*smd->limit[0];
215
216                 smd_factor   = smd->factor / MAX2(FLT_EPSILON, smd_limit[1]-smd_limit[0]);
217         }
218
219         modifier_get_vgroup(ob, dm, smd->vgroup_name, &dvert, &vgroup);
220
221         switch(smd->mode)
222         {
223                 case MOD_SIMPLEDEFORM_MODE_TWIST:       simpleDeform_callback = simpleDeform_twist;             break;
224                 case MOD_SIMPLEDEFORM_MODE_BEND:        simpleDeform_callback = simpleDeform_bend;              break;
225                 case MOD_SIMPLEDEFORM_MODE_TAPER:       simpleDeform_callback = simpleDeform_taper;             break;
226                 case MOD_SIMPLEDEFORM_MODE_STRETCH:     simpleDeform_callback = simpleDeform_stretch;   break;
227                 default:
228                         return; //No simpledeform mode?
229         }
230
231         for(i=0; i<numVerts; i++)
232         {
233                 float weight = defvert_array_find_weight_safe(dvert, i, vgroup);
234
235                 if(weight != 0.0f)
236                 {
237                         float co[3], dcut[3] = {0.0f, 0.0f, 0.0f};
238
239                         if(transf) space_transform_apply(transf, vertexCos[i]);
240
241                         VECCOPY(co, vertexCos[i]);
242
243                         //Apply axis limits
244                         if(smd->mode != MOD_SIMPLEDEFORM_MODE_BEND) //Bend mode shoulnt have any lock axis
245                         {
246                                 if(smd->axis & MOD_SIMPLEDEFORM_LOCK_AXIS_X) axis_limit(0, lock_axis, co, dcut);
247                                 if(smd->axis & MOD_SIMPLEDEFORM_LOCK_AXIS_Y) axis_limit(1, lock_axis, co, dcut);
248                         }
249                         axis_limit(limit_axis, smd_limit, co, dcut);
250
251                         simpleDeform_callback(smd_factor, dcut, co);            //Apply deform
252                         interp_v3_v3v3(vertexCos[i], vertexCos[i], co, weight); //Use vertex weight has coef of linear interpolation
253
254                         if(transf) space_transform_invert(transf, vertexCos[i]);
255                 }
256         }
257 }
258
259
260
261
262 /* SimpleDeform */
263 static void initData(ModifierData *md)
264 {
265         SimpleDeformModifierData *smd = (SimpleDeformModifierData*) md;
266
267         smd->mode = MOD_SIMPLEDEFORM_MODE_TWIST;
268         smd->axis = 0;
269
270         smd->origin   =  NULL;
271         smd->factor   =  0.35f;
272         smd->limit[0] =  0.0f;
273         smd->limit[1] =  1.0f;
274 }
275
276 static void copyData(ModifierData *md, ModifierData *target)
277 {
278         SimpleDeformModifierData *smd  = (SimpleDeformModifierData*)md;
279         SimpleDeformModifierData *tsmd = (SimpleDeformModifierData*)target;
280
281         tsmd->mode      = smd->mode;
282         tsmd->axis  = smd->axis;
283         tsmd->origin= smd->origin;
284         tsmd->originOpts= smd->originOpts;
285         tsmd->factor= smd->factor;
286         memcpy(tsmd->limit, smd->limit, sizeof(tsmd->limit));
287         BLI_strncpy(tsmd->vgroup_name, smd->vgroup_name, sizeof(tsmd->vgroup_name));
288 }
289
290 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
291 {
292         SimpleDeformModifierData *smd = (SimpleDeformModifierData *)md;
293         CustomDataMask dataMask = 0;
294
295         /* ask for vertexgroups if we need them */
296         if(smd->vgroup_name[0])
297                 dataMask |= CD_MASK_MDEFORMVERT;
298
299         return dataMask;
300 }
301
302 static void foreachObjectLink(ModifierData *md, Object *ob, void (*walk)(void *userData, Object *ob, Object **obpoin), void *userData)
303 {
304         SimpleDeformModifierData *smd  = (SimpleDeformModifierData*)md;
305         walk(userData, ob, &smd->origin);
306 }
307
308 static void updateDepgraph(ModifierData *md, DagForest *forest,
309                                                 struct Scene *UNUSED(scene),
310                                                 Object *UNUSED(ob),
311                                                 DagNode *obNode)
312 {
313         SimpleDeformModifierData *smd  = (SimpleDeformModifierData*)md;
314
315         if (smd->origin)
316                 dag_add_relation(forest, dag_get_node(forest, smd->origin), obNode, DAG_RL_OB_DATA, "SimpleDeform Modifier");
317 }
318
319 static void deformVerts(ModifierData *md, Object *ob,
320                                                 DerivedMesh *derivedData,
321                                                 float (*vertexCos)[3],
322                                                 int numVerts,
323                                                 int UNUSED(useRenderParams),
324                                                 int UNUSED(isFinalCalc))
325 {
326         DerivedMesh *dm = derivedData;
327         CustomDataMask dataMask = requiredDataMask(ob, md);
328
329         /* we implement requiredDataMask but thats not really usefull since
330            mesh_calc_modifiers pass a NULL derivedData */
331         if(dataMask)
332                 dm= get_dm(ob, NULL, dm, NULL, 0);
333
334         SimpleDeformModifier_do((SimpleDeformModifierData*)md, ob, dm, vertexCos, numVerts);
335
336         if(dm != derivedData)
337                 dm->release(dm);
338 }
339
340 static void deformVertsEM(ModifierData *md, Object *ob,
341                                                 struct BMEditMesh *editData,
342                                                 DerivedMesh *derivedData,
343                                                 float (*vertexCos)[3],
344                                                 int numVerts)
345 {
346         DerivedMesh *dm = derivedData;
347         CustomDataMask dataMask = requiredDataMask(ob, md);
348
349         /* we implement requiredDataMask but thats not really usefull since
350            mesh_calc_modifiers pass a NULL derivedData */
351         if(dataMask)
352                 dm= get_dm(ob, editData, dm, NULL, 0);
353
354         SimpleDeformModifier_do((SimpleDeformModifierData*)md, ob, dm, vertexCos, numVerts);
355
356         if(dm != derivedData)
357                 dm->release(dm);
358 }
359
360
361 ModifierTypeInfo modifierType_SimpleDeform = {
362         /* name */              "SimpleDeform",
363         /* structName */        "SimpleDeformModifierData",
364         /* structSize */        sizeof(SimpleDeformModifierData),
365         /* type */              eModifierTypeType_OnlyDeform,
366
367         /* flags */             eModifierTypeFlag_AcceptsMesh
368                                                         | eModifierTypeFlag_AcceptsCVs
369                                                         | eModifierTypeFlag_SupportsEditmode
370                                                         | eModifierTypeFlag_EnableInEditmode,
371
372         /* copyData */          copyData,
373         /* deformVerts */       deformVerts,
374         /* deformMatrices */    NULL,
375         /* deformVertsEM */     deformVertsEM,
376         /* deformMatricesEM */  NULL,
377         /* applyModifier */     NULL,
378         /* applyModifierEM */   NULL,
379         /* initData */          initData,
380         /* requiredDataMask */  requiredDataMask,
381         /* freeData */          NULL,
382         /* isDisabled */        NULL,
383         /* updateDepgraph */    updateDepgraph,
384         /* dependsOnTime */     NULL,
385         /* dependsOnNormals */  NULL,
386         /* foreachObjectLink */ foreachObjectLink,
387         /* foreachIDLink */     NULL,
388         /* foreachTexLink */    NULL,
389 };