svn merge ^/trunk/blender -r42521:42550
[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_mesh_types.h"
37 #include "DNA_meshdata_types.h"
38 #include "DNA_object_types.h"
39
40 #include "BLI_math.h"
41 #include "BLI_utildefines.h"
42 #include "BLI_string.h"
43
44 #include "BKE_action.h"
45 #include "BKE_cdderivedmesh.h"
46 #include "BKE_modifier.h"
47 #include "BKE_deform.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 }
61
62 static void copyData(ModifierData *md, ModifierData *target)
63 {
64         HookModifierData *hmd = (HookModifierData*) md;
65         HookModifierData *thmd = (HookModifierData*) target;
66
67         copy_v3_v3(thmd->cent, hmd->cent);
68         thmd->falloff = hmd->falloff;
69         thmd->force = hmd->force;
70         thmd->object = hmd->object;
71         thmd->totindex = hmd->totindex;
72         thmd->indexar = MEM_dupallocN(hmd->indexar);
73         memcpy(thmd->parentinv, hmd->parentinv, sizeof(hmd->parentinv));
74         BLI_strncpy(thmd->name, hmd->name, 32);
75         BLI_strncpy(thmd->subtarget, hmd->subtarget, 32);
76 }
77
78 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
79 {
80         HookModifierData *hmd = (HookModifierData *)md;
81         CustomDataMask dataMask = 0;
82
83         /* ask for vertexgroups if we need them */
84         if(hmd->name[0]) dataMask |= CD_MASK_MDEFORMVERT;
85         if(hmd->indexar) dataMask |= CD_MASK_ORIGINDEX;
86
87         return dataMask;
88 }
89
90 static void freeData(ModifierData *md)
91 {
92         HookModifierData *hmd = (HookModifierData*) md;
93
94         if (hmd->indexar) MEM_freeN(hmd->indexar);
95 }
96
97 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
98 {
99         HookModifierData *hmd = (HookModifierData*) md;
100
101         return !hmd->object;
102 }
103
104 static void foreachObjectLink(
105                                            ModifierData *md, Object *ob,
106         void (*walk)(void *userData, Object *ob, Object **obpoin),
107                    void *userData)
108 {
109         HookModifierData *hmd = (HookModifierData*) md;
110
111         walk(userData, ob, &hmd->object);
112 }
113
114 static void updateDepgraph(ModifierData *md, DagForest *forest,
115                                                 struct Scene *UNUSED(scene),
116                                                 Object *UNUSED(ob),
117                                                 DagNode *obNode)
118 {
119         HookModifierData *hmd = (HookModifierData*) md;
120
121         if (hmd->object) {
122                 DagNode *curNode = dag_get_node(forest, hmd->object);
123                 
124                 if (hmd->subtarget[0])
125                         dag_add_relation(forest, curNode, obNode, DAG_RL_OB_DATA|DAG_RL_DATA_DATA, "Hook Modifier");
126                 else
127                         dag_add_relation(forest, curNode, obNode, DAG_RL_OB_DATA, "Hook Modifier");
128         }
129 }
130
131 static float hook_falloff(float *co_1, float *co_2, const float falloff_squared, float fac)
132 {
133         if(falloff_squared) {
134                 float len_squared = len_squared_v3v3(co_1, co_2);
135                 if(len_squared > falloff_squared) {
136                         return 0.0f;
137                 }
138                 else if(len_squared > 0.0f) {
139                         return fac * (1.0f - (len_squared / falloff_squared));
140                 }
141         }
142
143         return fac;
144 }
145
146 static void deformVerts_do(HookModifierData *hmd, Object *ob, DerivedMesh *dm,
147                            float (*vertexCos)[3], int numVerts)
148 {
149         bPoseChannel *pchan= get_pose_channel(hmd->object->pose, hmd->subtarget);
150         float vec[3], mat[4][4], dmat[4][4];
151         int i, *index_pt;
152         const float falloff_squared= hmd->falloff * hmd->falloff; /* for faster comparisons */
153         
154         MDeformVert *dvert;
155         int defgrp_index, max_dvert;
156         
157         /* get world-space matrix of target, corrected for the space the verts are in */
158         if (hmd->subtarget[0] && pchan) {
159                 /* bone target if there's a matching pose-channel */
160                 mul_m4_m4m4(dmat, pchan->pose_mat, hmd->object->obmat);
161         }
162         else {
163                 /* just object target */
164                 copy_m4_m4(dmat, hmd->object->obmat);
165         }
166         invert_m4_m4(ob->imat, ob->obmat);
167         mul_serie_m4(mat, ob->imat, dmat, hmd->parentinv,
168                      NULL, NULL, NULL, NULL, NULL);
169
170         modifier_get_vgroup(ob, dm, hmd->name, &dvert, &defgrp_index);
171         max_dvert = (dvert)? numVerts: 0;
172
173         /* Regarding index range checking below.
174          *
175          * This should always be true and I don't generally like 
176          * "paranoid" style code like this, but old files can have
177          * indices that are out of range because old blender did
178          * not correct them on exit editmode. - zr
179          */
180         
181         if(hmd->force == 0.0f) {
182                 /* do nothing, avoid annoying checks in the loop */
183         }
184         else if(hmd->indexar) { /* vertex indices? */
185                 const float fac_orig= hmd->force;
186                 float fac;
187                 const int *origindex_ar;
188                 
189                 /* if DerivedMesh is present and has original index data, use it */
190                 if(dm && (origindex_ar= dm->getVertDataArray(dm, CD_ORIGINDEX))) {
191                         for(i= 0, index_pt= hmd->indexar; i < hmd->totindex; i++, index_pt++) {
192                                 if(*index_pt < numVerts) {
193                                         int j;
194                                         
195                                         for(j = 0; j < numVerts; j++) {
196                                                 if(origindex_ar[j] == *index_pt) {
197                                                         float *co = vertexCos[j];
198                                                         if((fac= hook_falloff(hmd->cent, co, falloff_squared, fac_orig))) {
199                                                                 if(dvert)
200                                                                         fac *= defvert_find_weight(dvert+j, defgrp_index);
201                                                                 
202                                                                 if(fac) {
203                                                                         mul_v3_m4v3(vec, mat, co);
204                                                                         interp_v3_v3v3(co, co, vec, fac);
205                                                                 }
206                                                         }
207                                                 }
208                                         }
209                                 }
210                         }
211                 }
212                 else { /* missing dm or ORIGINDEX */
213                         for(i= 0, index_pt= hmd->indexar; i < hmd->totindex; i++, index_pt++) {
214                                 if(*index_pt < numVerts) {
215                                         float *co = vertexCos[*index_pt];
216                                         if((fac= hook_falloff(hmd->cent, co, falloff_squared, fac_orig))) {
217                                                 if(dvert)
218                                                         fac *= defvert_find_weight(dvert+(*index_pt), defgrp_index);
219                                                 
220                                                 if(fac) {
221                                                         mul_v3_m4v3(vec, mat, co);
222                                                         interp_v3_v3v3(co, co, vec, fac);
223                                                 }
224                                         }
225                                 }
226                         }
227                 }
228         }
229         else if(dvert) {        /* vertex group hook */
230                 const float fac_orig= hmd->force;
231                 
232                 for(i = 0; i < max_dvert; i++, dvert++) {
233                         float fac;
234                         float *co = vertexCos[i];
235                         
236                         if((fac= hook_falloff(hmd->cent, co, falloff_squared, fac_orig))) {
237                                 fac *= defvert_find_weight(dvert, defgrp_index);
238                                 if(fac) {
239                                         mul_v3_m4v3(vec, mat, co);
240                                         interp_v3_v3v3(co, co, vec, fac);
241                                 }
242                         }
243                 }
244         }
245 }
246
247 static void deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData,
248                         float (*vertexCos)[3], int numVerts,
249                         int UNUSED(useRenderParams), int UNUSED(isFinalCalc))
250 {
251         HookModifierData *hmd = (HookModifierData*) md;
252         
253         deformVerts_do(hmd, ob, derivedData, vertexCos, numVerts);
254 }
255
256 static void deformVertsEM(ModifierData *md, Object *ob, struct BMEditMesh *editData,
257                           DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
258 {
259         HookModifierData *hmd = (HookModifierData*) md;
260         DerivedMesh *dm = get_dm(ob, editData, derivedData, NULL, 0);
261
262         deformVerts_do(hmd, ob, dm, vertexCos, numVerts);
263
264         if(derivedData != dm)
265                 dm->release(dm);
266 }
267
268
269 ModifierTypeInfo modifierType_Hook = {
270         /* name */              "Hook",
271         /* structName */        "HookModifierData",
272         /* structSize */        sizeof(HookModifierData),
273         /* type */              eModifierTypeType_OnlyDeform,
274         /* flags */             eModifierTypeFlag_AcceptsCVs
275                                                         | eModifierTypeFlag_SupportsEditmode,
276         /* copyData */          copyData,
277         /* deformVerts */       deformVerts,
278         /* deformMatrices */    NULL,
279         /* deformVertsEM */     deformVertsEM,
280         /* deformMatricesEM */  NULL,
281         /* applyModifier */     NULL,
282         /* applyModifierEM */   NULL,
283         /* initData */          initData,
284         /* requiredDataMask */  requiredDataMask,
285         /* freeData */          freeData,
286         /* isDisabled */        isDisabled,
287         /* updateDepgraph */    updateDepgraph,
288         /* dependsOnTime */     NULL,
289         /* dependsOnNormals */  NULL,
290         /* foreachObjectLink */ foreachObjectLink,
291         /* foreachIDLink */     NULL,
292         /* foreachTexLink */    NULL,
293 };