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