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