Fix depsgraph to compute more accurate links for collision & force.
[blender.git] / source / blender / modifiers / intern / MOD_cloth.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_cloth.c
32  *  \ingroup modifiers
33  */
34
35 #include <string.h>
36
37 #include "DNA_cloth_types.h"
38 #include "DNA_key_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_object_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_utildefines.h"
45
46
47 #include "BKE_cloth.h"
48 #include "BKE_cdderivedmesh.h"
49 #include "BKE_effect.h"
50 #include "BKE_global.h"
51 #include "BKE_key.h"
52 #include "BKE_library_query.h"
53 #include "BKE_modifier.h"
54 #include "BKE_pointcache.h"
55
56 #include "depsgraph_private.h"
57
58 #include "MOD_util.h"
59
60 static void initData(ModifierData *md) 
61 {
62         ClothModifierData *clmd = (ClothModifierData *) md;
63         
64         clmd->sim_parms = MEM_callocN(sizeof(ClothSimSettings), "cloth sim parms");
65         clmd->coll_parms = MEM_callocN(sizeof(ClothCollSettings), "cloth coll parms");
66         clmd->point_cache = BKE_ptcache_add(&clmd->ptcaches);
67         
68         /* check for alloc failing */
69         if (!clmd->sim_parms || !clmd->coll_parms || !clmd->point_cache)
70                 return;
71         
72         cloth_init(clmd);
73 }
74
75 static void deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData, float (*vertexCos)[3],
76                         int numVerts, ModifierApplyFlag UNUSED(flag))
77 {
78         DerivedMesh *dm;
79         ClothModifierData *clmd = (ClothModifierData *) md;
80         
81         /* check for alloc failing */
82         if (!clmd->sim_parms || !clmd->coll_parms) {
83                 initData(md);
84
85                 if (!clmd->sim_parms || !clmd->coll_parms)
86                         return;
87         }
88
89         dm = get_dm(ob, NULL, derivedData, NULL, false, false);
90         if (dm == derivedData)
91                 dm = CDDM_copy(dm);
92
93         /* TODO(sergey): For now it actually duplicates logic from DerivedMesh.c
94          * and needs some more generic solution. But starting experimenting with
95          * this so close to the release is not that nice..
96          *
97          * Also hopefully new cloth system will arrive soon..
98          */
99         if (derivedData == NULL && clmd->sim_parms->shapekey_rest) {
100                 KeyBlock *kb = BKE_keyblock_from_key(BKE_key_from_object(ob),
101                                                      clmd->sim_parms->shapekey_rest);
102                 if (kb && kb->data != NULL) {
103                         float (*layerorco)[3];
104                         if (!(layerorco = DM_get_vert_data_layer(dm, CD_CLOTH_ORCO))) {
105                                 DM_add_vert_layer(dm, CD_CLOTH_ORCO, CD_CALLOC, NULL);
106                                 layerorco = DM_get_vert_data_layer(dm, CD_CLOTH_ORCO);
107                         }
108
109                         memcpy(layerorco, kb->data, sizeof(float) * 3 * numVerts);
110                 }
111         }
112
113         CDDM_apply_vert_coords(dm, vertexCos);
114
115         clothModifier_do(clmd, md->scene, ob, dm, vertexCos);
116
117         dm->release(dm);
118 }
119
120 static void updateDepgraph(ModifierData *md, DagForest *forest,
121                            struct Main *UNUSED(bmain),
122                            Scene *scene, Object *ob, DagNode *obNode)
123 {
124         ClothModifierData *clmd = (ClothModifierData *) md;
125         
126         if (clmd) {
127                 /* Actual code uses get_collisionobjects */
128                 dag_add_collision_relations(forest, scene, ob, obNode, clmd->coll_parms->group, ob->lay|scene->lay, eModifierType_Collision, NULL, true, "Cloth Collision");
129
130                 dag_add_forcefield_relations(forest, scene, ob, obNode, clmd->sim_parms->effector_weights, true, 0, "Cloth Field");
131         }
132 }
133
134 static void updateDepsgraph(ModifierData *md,
135                             struct Main *UNUSED(bmain),
136                             struct Scene *scene,
137                             Object *ob,
138                             struct DepsNodeHandle *node)
139 {
140         ClothModifierData *clmd = (ClothModifierData *)md;
141         if (clmd != NULL) {
142                 /* Actual code uses get_collisionobjects */
143                 DEG_add_collision_relations(node, scene, ob, clmd->coll_parms->group, ob->lay|scene->lay, eModifierType_Collision, NULL, true, "Cloth Collision");
144
145                 DEG_add_forcefield_relations(node, scene, ob, clmd->sim_parms->effector_weights, true, 0, "Cloth Field");
146         }
147 }
148
149 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
150 {
151         CustomDataMask dataMask = 0;
152         ClothModifierData *clmd = (ClothModifierData *)md;
153
154         if (cloth_uses_vgroup(clmd))
155                 dataMask |= CD_MASK_MDEFORMVERT;
156
157         if (clmd->sim_parms->shapekey_rest != 0)
158                 dataMask |= CD_MASK_CLOTH_ORCO;
159
160         return dataMask;
161 }
162
163 static void copyData(ModifierData *md, ModifierData *target)
164 {
165         ClothModifierData *clmd = (ClothModifierData *) md;
166         ClothModifierData *tclmd = (ClothModifierData *) target;
167
168         if (tclmd->sim_parms) {
169                 if (tclmd->sim_parms->effector_weights)
170                         MEM_freeN(tclmd->sim_parms->effector_weights);
171                 MEM_freeN(tclmd->sim_parms);
172         }
173
174         if (tclmd->coll_parms)
175                 MEM_freeN(tclmd->coll_parms);
176         
177         BKE_ptcache_free_list(&tclmd->ptcaches);
178         tclmd->point_cache = NULL;
179
180         tclmd->sim_parms = MEM_dupallocN(clmd->sim_parms);
181         if (clmd->sim_parms->effector_weights)
182                 tclmd->sim_parms->effector_weights = MEM_dupallocN(clmd->sim_parms->effector_weights);
183         tclmd->coll_parms = MEM_dupallocN(clmd->coll_parms);
184         tclmd->point_cache = BKE_ptcache_add(&tclmd->ptcaches);
185         tclmd->point_cache->step = 1;
186         tclmd->clothObject = NULL;
187         tclmd->hairdata = NULL;
188         tclmd->solver_result = NULL;
189 }
190
191 static bool dependsOnTime(ModifierData *UNUSED(md))
192 {
193         return true;
194 }
195
196 static void freeData(ModifierData *md)
197 {
198         ClothModifierData *clmd = (ClothModifierData *) md;
199         
200         if (clmd) {
201                 if (G.debug_value > 0)
202                         printf("clothModifier_freeData\n");
203                 
204                 cloth_free_modifier_extern(clmd);
205                 
206                 if (clmd->sim_parms) {
207                         if (clmd->sim_parms->effector_weights)
208                                 MEM_freeN(clmd->sim_parms->effector_weights);
209                         MEM_freeN(clmd->sim_parms);
210                 }
211                 if (clmd->coll_parms)
212                         MEM_freeN(clmd->coll_parms);
213                 
214                 BKE_ptcache_free_list(&clmd->ptcaches);
215                 clmd->point_cache = NULL;
216                 
217                 if (clmd->hairdata)
218                         MEM_freeN(clmd->hairdata);
219                 
220                 if (clmd->solver_result)
221                         MEM_freeN(clmd->solver_result);
222         }
223 }
224
225 static void foreachIDLink(ModifierData *md, Object *ob,
226                           IDWalkFunc walk, void *userData)
227 {
228         ClothModifierData *clmd = (ClothModifierData *) md;
229
230         if (clmd->coll_parms) {
231                 walk(userData, ob, (ID **)&clmd->coll_parms->group, IDWALK_NOP);
232         }
233
234         if (clmd->sim_parms && clmd->sim_parms->effector_weights) {
235                 walk(userData, ob, (ID **)&clmd->sim_parms->effector_weights->group, IDWALK_NOP);
236         }
237 }
238
239 ModifierTypeInfo modifierType_Cloth = {
240         /* name */              "Cloth",
241         /* structName */        "ClothModifierData",
242         /* structSize */        sizeof(ClothModifierData),
243         /* type */              eModifierTypeType_OnlyDeform,
244         /* flags */             eModifierTypeFlag_AcceptsMesh |
245                                 eModifierTypeFlag_UsesPointCache |
246                                 eModifierTypeFlag_Single,
247
248         /* copyData */          copyData,
249         /* deformVerts */       deformVerts,
250         /* deformMatrices */    NULL,
251         /* deformVertsEM */     NULL,
252         /* deformMatricesEM */  NULL,
253         /* applyModifier */     NULL,
254         /* applyModifierEM */   NULL,
255         /* initData */          initData,
256         /* requiredDataMask */  requiredDataMask,
257         /* freeData */          freeData,
258         /* isDisabled */        NULL,
259         /* updateDepgraph */    updateDepgraph,
260         /* updateDepsgraph */   updateDepsgraph,
261         /* dependsOnTime */     dependsOnTime,
262         /* dependsOnNormals */  NULL,
263         /* foreachObjectLink */ NULL,
264         /* foreachIDLink */     foreachIDLink,
265         /* foreachTexLink */    NULL,
266 };