Pass EvaluationContext argument everywhere
[blender.git] / source / blender / modifiers / intern / MOD_hook.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_hook.c
32  *  \ingroup modifiers
33  */
34
35
36 #include "DNA_meshdata_types.h"
37 #include "DNA_object_types.h"
38
39 #include "BLI_math.h"
40 #include "BLI_utildefines.h"
41
42 #include "BKE_action.h"
43 #include "BKE_cdderivedmesh.h"
44 #include "BKE_library_query.h"
45 #include "BKE_modifier.h"
46 #include "BKE_deform.h"
47 #include "BKE_colortools.h"
48
49 #include "MEM_guardedalloc.h"
50
51 #include "MOD_util.h"
52
53 static void initData(ModifierData *md) 
54 {
55         HookModifierData *hmd = (HookModifierData *) md;
56
57         hmd->force = 1.0;
58         hmd->curfalloff = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
59         hmd->falloff_type = eHook_Falloff_Smooth;
60         hmd->flag = 0;
61 }
62
63 static void copyData(ModifierData *md, ModifierData *target)
64 {
65         HookModifierData *hmd = (HookModifierData *) md;
66         HookModifierData *thmd = (HookModifierData *) target;
67
68         if (thmd->curfalloff != NULL) {
69                 curvemapping_free(thmd->curfalloff);
70         }
71         if (thmd->indexar != NULL) {
72                 MEM_freeN(thmd->indexar);
73         }
74
75         modifier_copyData_generic(md, target);
76
77         thmd->curfalloff = curvemapping_copy(hmd->curfalloff);
78
79         thmd->indexar = MEM_dupallocN(hmd->indexar);
80 }
81
82 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
83 {
84         HookModifierData *hmd = (HookModifierData *)md;
85         CustomDataMask dataMask = 0;
86
87         /* ask for vertexgroups if we need them */
88         if (hmd->name[0]) dataMask |= CD_MASK_MDEFORMVERT;
89         if (hmd->indexar) dataMask |= CD_MASK_ORIGINDEX;
90
91         return dataMask;
92 }
93
94 static void freeData(ModifierData *md)
95 {
96         HookModifierData *hmd = (HookModifierData *) md;
97
98         curvemapping_free(hmd->curfalloff);
99
100         if (hmd->indexar) MEM_freeN(hmd->indexar);
101 }
102
103 static bool isDisabled(ModifierData *md, int UNUSED(useRenderParams))
104 {
105         HookModifierData *hmd = (HookModifierData *) md;
106
107         return !hmd->object;
108 }
109
110 static void foreachObjectLink(
111         ModifierData *md, Object *ob,
112         ObjectWalkFunc walk, void *userData)
113 {
114         HookModifierData *hmd = (HookModifierData *) md;
115
116         walk(userData, ob, &hmd->object, IDWALK_CB_NOP);
117 }
118
119 static void updateDepsgraph(ModifierData *md,
120                             struct Main *UNUSED(bmain),
121                             struct Scene *UNUSED(scene),
122                             Object *ob,
123                             struct DepsNodeHandle *node)
124 {
125         HookModifierData *hmd = (HookModifierData *)md;
126         if (hmd->object != NULL) {
127                 if (hmd->subtarget[0]) {
128                         DEG_add_bone_relation(node, hmd->object, hmd->subtarget, DEG_OB_COMP_BONE, "Hook Modifier");
129                 }
130                 DEG_add_object_relation(node, hmd->object, DEG_OB_COMP_TRANSFORM, "Hook Modifier");
131         }
132         /* We need own transformation as well. */
133         DEG_add_object_relation(node, ob, DEG_OB_COMP_TRANSFORM, "Hook Modifier");
134 }
135
136 struct HookData_cb {
137         float (*vertexCos)[3];
138
139         MDeformVert *dvert;
140         int defgrp_index;
141
142         struct CurveMapping *curfalloff;
143
144         char  falloff_type;
145         float falloff;
146         float falloff_sq;
147         float fac_orig;
148
149         unsigned int use_falloff        : 1;
150         unsigned int use_uniform        : 1;
151
152         float cent[3];
153
154         float mat_uniform[3][3];
155         float mat[4][4];
156 };
157
158 static float hook_falloff(
159         const struct HookData_cb *hd,
160         const float len_sq)
161 {
162         BLI_assert(hd->falloff_sq);
163         if (len_sq > hd->falloff_sq) {
164                 return 0.0f;
165         }
166         else if (len_sq > 0.0f) {
167                 float fac;
168
169                 if (hd->falloff_type == eHook_Falloff_Const) {
170                         fac = 1.0f;
171                         goto finally;
172                 }
173                 else if (hd->falloff_type == eHook_Falloff_InvSquare) {
174                         /* avoid sqrt below */
175                         fac = 1.0f - (len_sq / hd->falloff_sq);
176                         goto finally;
177                 }
178
179                 fac = 1.0f - (sqrtf(len_sq) / hd->falloff);
180
181                 /* closely match PROP_SMOOTH and similar */
182                 switch (hd->falloff_type) {
183 #if 0
184                         case eHook_Falloff_None:
185                                 fac = 1.0f;
186                                 break;
187 #endif
188                         case eHook_Falloff_Curve:
189                                 fac = curvemapping_evaluateF(hd->curfalloff, 0, fac);
190                                 break;
191                         case eHook_Falloff_Sharp:
192                                 fac = fac * fac;
193                                 break;
194                         case eHook_Falloff_Smooth:
195                                 fac = 3.0f * fac * fac - 2.0f * fac * fac * fac;
196                                 break;
197                         case eHook_Falloff_Root:
198                                 fac = sqrtf(fac);
199                                 break;
200                         case eHook_Falloff_Linear:
201                                 /* pass */
202                                 break;
203 #if 0
204                         case eHook_Falloff_Const:
205                                 fac = 1.0f;
206                                 break;
207 #endif
208                         case eHook_Falloff_Sphere:
209                                 fac = sqrtf(2 * fac - fac * fac);
210                                 break;
211 #if 0
212                         case eHook_Falloff_InvSquare:
213                                 fac = fac * (2.0f - fac);
214                                 break;
215 #endif
216                 }
217
218 finally:
219                 return fac * hd->fac_orig;
220         }
221         else {
222                 return hd->fac_orig;
223         }
224 }
225
226 static void hook_co_apply(struct HookData_cb *hd, const int j)
227 {
228         float *co = hd->vertexCos[j];
229         float fac;
230
231         if (hd->use_falloff) {
232                 float len_sq;
233
234                 if (hd->use_uniform) {
235                         float co_uniform[3];
236                         mul_v3_m3v3(co_uniform, hd->mat_uniform, co);
237                         len_sq = len_squared_v3v3(hd->cent, co_uniform);
238                 }
239                 else {
240                         len_sq = len_squared_v3v3(hd->cent, co);
241                 }
242
243                 fac = hook_falloff(hd, len_sq);
244         }
245         else {
246                 fac = hd->fac_orig;
247         }
248
249         if (fac) {
250                 if (hd->dvert) {
251                         fac *= defvert_find_weight(&hd->dvert[j], hd->defgrp_index);
252                 }
253
254                 if (fac) {
255                         float co_tmp[3];
256                         mul_v3_m4v3(co_tmp, hd->mat, co);
257                         interp_v3_v3v3(co, co, co_tmp, fac);
258                 }
259         }
260 }
261
262 static void deformVerts_do(HookModifierData *hmd, Object *ob, DerivedMesh *dm,
263                            float (*vertexCos)[3], int numVerts)
264 {
265         bPoseChannel *pchan = BKE_pose_channel_find_name(hmd->object->pose, hmd->subtarget);
266         float dmat[4][4];
267         int i, *index_pt;
268         struct HookData_cb hd;
269         
270         if (hmd->curfalloff == NULL) {
271                 /* should never happen, but bad lib linking could cause it */
272                 hmd->curfalloff = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
273         }
274
275         if (hmd->curfalloff) {
276                 curvemapping_initialize(hmd->curfalloff);
277         }
278
279         /* Generic data needed for applying per-vertex calculations (initialize all members) */
280         hd.vertexCos = vertexCos;
281         modifier_get_vgroup(ob, dm, hmd->name, &hd.dvert, &hd.defgrp_index);
282
283         hd.curfalloff = hmd->curfalloff;
284
285         hd.falloff_type = hmd->falloff_type;
286         hd.falloff = (hmd->falloff_type == eHook_Falloff_None) ? 0.0f : hmd->falloff;
287         hd.falloff_sq = SQUARE(hd.falloff);
288         hd.fac_orig = hmd->force;
289
290         hd.use_falloff = (hd.falloff_sq != 0.0f);
291         hd.use_uniform = (hmd->flag & MOD_HOOK_UNIFORM_SPACE) != 0;
292
293         if (hd.use_uniform) {
294                 copy_m3_m4(hd.mat_uniform, hmd->parentinv);
295                 mul_v3_m3v3(hd.cent, hd.mat_uniform, hmd->cent);
296         }
297         else {
298                 unit_m3(hd.mat_uniform);  /* unused */
299                 copy_v3_v3(hd.cent, hmd->cent);
300         }
301
302         /* get world-space matrix of target, corrected for the space the verts are in */
303         if (hmd->subtarget[0] && pchan) {
304                 /* bone target if there's a matching pose-channel */
305                 mul_m4_m4m4(dmat, hmd->object->obmat, pchan->pose_mat);
306         }
307         else {
308                 /* just object target */
309                 copy_m4_m4(dmat, hmd->object->obmat);
310         }
311         invert_m4_m4(ob->imat, ob->obmat);
312         mul_m4_series(hd.mat, ob->imat, dmat, hmd->parentinv);
313         /* --- done with 'hd' init --- */
314
315
316         /* Regarding index range checking below.
317          *
318          * This should always be true and I don't generally like 
319          * "paranoid" style code like this, but old files can have
320          * indices that are out of range because old blender did
321          * not correct them on exit editmode. - zr
322          */
323
324         if (hmd->force == 0.0f) {
325                 /* do nothing, avoid annoying checks in the loop */
326         }
327         else if (hmd->indexar) { /* vertex indices? */
328                 const int *origindex_ar;
329                 
330                 /* if DerivedMesh is present and has original index data, use it */
331                 if (dm && (origindex_ar = dm->getVertDataArray(dm, CD_ORIGINDEX))) {
332                         for (i = 0, index_pt = hmd->indexar; i < hmd->totindex; i++, index_pt++) {
333                                 if (*index_pt < numVerts) {
334                                         int j;
335                                         
336                                         for (j = 0; j < numVerts; j++) {
337                                                 if (origindex_ar[j] == *index_pt) {
338                                                         hook_co_apply(&hd, j);
339                                                 }
340                                         }
341                                 }
342                         }
343                 }
344                 else { /* missing dm or ORIGINDEX */
345                         for (i = 0, index_pt = hmd->indexar; i < hmd->totindex; i++, index_pt++) {
346                                 if (*index_pt < numVerts) {
347                                         hook_co_apply(&hd, *index_pt);
348                                 }
349                         }
350                 }
351         }
352         else if (hd.dvert) {  /* vertex group hook */
353                 for (i = 0; i < numVerts; i++) {
354                         hook_co_apply(&hd, i);
355                 }
356         }
357 }
358
359 static void deformVerts(ModifierData *md, struct EvaluationContext *UNUSED(eval_ctx), Object *ob, DerivedMesh *derivedData,
360                         float (*vertexCos)[3], int numVerts,
361                         ModifierApplyFlag UNUSED(flag))
362 {
363         HookModifierData *hmd = (HookModifierData *) md;
364         DerivedMesh *dm = derivedData;
365         /* We need a valid dm for meshes when a vgroup is set... */
366         if (!dm && ob->type == OB_MESH && hmd->name[0] != '\0')
367                 dm = get_dm(ob, NULL, dm, NULL, false, false);
368
369         deformVerts_do(hmd, ob, dm, vertexCos, numVerts);
370
371         if (derivedData != dm)
372                 dm->release(dm);
373 }
374
375 static void deformVertsEM(ModifierData *md, struct EvaluationContext *UNUSED(eval_ctx), Object *ob, struct BMEditMesh *editData,
376                           DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
377 {
378         HookModifierData *hmd = (HookModifierData *) md;
379         DerivedMesh *dm = derivedData;
380         /* We need a valid dm for meshes when a vgroup is set... */
381         if (!dm && ob->type == OB_MESH && hmd->name[0] != '\0')
382                 dm = get_dm(ob, editData, dm, NULL, false, false);
383
384         deformVerts_do(hmd, ob, dm, vertexCos, numVerts);
385
386         if (derivedData != dm)
387                 dm->release(dm);
388 }
389
390
391 ModifierTypeInfo modifierType_Hook = {
392         /* name */              "Hook",
393         /* structName */        "HookModifierData",
394         /* structSize */        sizeof(HookModifierData),
395         /* type */              eModifierTypeType_OnlyDeform,
396         /* flags */             eModifierTypeFlag_AcceptsCVs |
397                                 eModifierTypeFlag_AcceptsLattice |
398                                 eModifierTypeFlag_SupportsEditmode,
399         /* copyData */          copyData,
400         /* deformVerts */       deformVerts,
401         /* deformMatrices */    NULL,
402         /* deformVertsEM */     deformVertsEM,
403         /* deformMatricesEM */  NULL,
404         /* applyModifier */     NULL,
405         /* applyModifierEM */   NULL,
406         /* initData */          initData,
407         /* requiredDataMask */  requiredDataMask,
408         /* freeData */          freeData,
409         /* isDisabled */        isDisabled,
410         /* updateDepsgraph */   updateDepsgraph,
411         /* dependsOnTime */     NULL,
412         /* dependsOnNormals */  NULL,
413         /* foreachObjectLink */ foreachObjectLink,
414         /* foreachIDLink */     NULL,
415         /* foreachTexLink */    NULL,
416 };