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