Depsgraph: New dependency graph integration commit
[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_modifier.h"
53 #include "BKE_pointcache.h"
54
55 #include "depsgraph_private.h"
56
57 #include "MOD_util.h"
58
59 static void initData(ModifierData *md) 
60 {
61         ClothModifierData *clmd = (ClothModifierData *) md;
62         
63         clmd->sim_parms = MEM_callocN(sizeof(ClothSimSettings), "cloth sim parms");
64         clmd->coll_parms = MEM_callocN(sizeof(ClothCollSettings), "cloth coll parms");
65         clmd->point_cache = BKE_ptcache_add(&clmd->ptcaches);
66         
67         /* check for alloc failing */
68         if (!clmd->sim_parms || !clmd->coll_parms || !clmd->point_cache)
69                 return;
70         
71         cloth_init(clmd);
72 }
73
74 static void deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData, float (*vertexCos)[3],
75                         int numVerts, ModifierApplyFlag UNUSED(flag))
76 {
77         DerivedMesh *dm;
78         ClothModifierData *clmd = (ClothModifierData *) md;
79         
80         /* check for alloc failing */
81         if (!clmd->sim_parms || !clmd->coll_parms) {
82                 initData(md);
83
84                 if (!clmd->sim_parms || !clmd->coll_parms)
85                         return;
86         }
87
88         dm = get_dm(ob, NULL, derivedData, NULL, false, false);
89         if (dm == derivedData)
90                 dm = CDDM_copy(dm);
91
92         /* TODO(sergey): For now it actually duplicates logic from DerivedMesh.c
93          * and needs some more generic solution. But starting experimenting with
94          * this so close to the release is not that nice..
95          *
96          * Also hopefully new cloth system will arrive soon..
97          */
98         if (derivedData == NULL && clmd->sim_parms->shapekey_rest) {
99                 KeyBlock *kb = BKE_keyblock_from_key(BKE_key_from_object(ob),
100                                                      clmd->sim_parms->shapekey_rest);
101                 if (kb && kb->data != NULL) {
102                         float (*layerorco)[3];
103                         if (!(layerorco = DM_get_vert_data_layer(dm, CD_CLOTH_ORCO))) {
104                                 DM_add_vert_layer(dm, CD_CLOTH_ORCO, CD_CALLOC, NULL);
105                                 layerorco = DM_get_vert_data_layer(dm, CD_CLOTH_ORCO);
106                         }
107
108                         memcpy(layerorco, kb->data, sizeof(float) * 3 * numVerts);
109                 }
110         }
111
112         CDDM_apply_vert_coords(dm, vertexCos);
113
114         DM_ensure_tessface(dm); /* BMESH - UNTIL MODIFIER IS UPDATED FOR MPoly */
115
116         clothModifier_do(clmd, md->scene, ob, dm, vertexCos);
117
118         dm->release(dm);
119 }
120
121 static void updateDepgraph(ModifierData *md, DagForest *forest,
122                            struct Main *UNUSED(bmain),
123                            Scene *scene, Object *ob, DagNode *obNode)
124 {
125         ClothModifierData *clmd = (ClothModifierData *) md;
126         
127         Base *base;
128         
129         if (clmd) {
130                 for (base = scene->base.first; base; base = base->next) {
131                         Object *ob1 = base->object;
132                         if (ob1 != ob) {
133                                 CollisionModifierData *coll_clmd = (CollisionModifierData *)modifiers_findByType(ob1, eModifierType_Collision);
134                                 if (coll_clmd) {
135                                         DagNode *curNode = dag_get_node(forest, ob1);
136                                         dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Cloth Collision");
137                                 }
138                         }
139                 }
140         }
141 }
142
143 static void updateDepsgraph(ModifierData *md,
144                             struct Main *UNUSED(bmain),
145                             struct Scene *scene,
146                             Object *ob,
147                             struct DepsNodeHandle *node)
148 {
149         ClothModifierData *clmd = (ClothModifierData *)md;
150         if (clmd != NULL) {
151                 Base *base;
152                 for (base = scene->base.first; base; base = base->next) {
153                         Object *ob1 = base->object;
154                         if (ob1 != ob) {
155                                 CollisionModifierData *coll_clmd = (CollisionModifierData *)modifiers_findByType(ob1, eModifierType_Collision);
156                                 if (coll_clmd) {
157                                         DEG_add_object_relation(node, ob1, DEG_OB_COMP_TRANSFORM, "Cloth Modifier");
158                                 }
159                         }
160                 }
161         }
162 }
163
164 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
165 {
166         CustomDataMask dataMask = 0;
167         ClothModifierData *clmd = (ClothModifierData *)md;
168
169         if (cloth_uses_vgroup(clmd))
170                 dataMask |= CD_MASK_MDEFORMVERT;
171
172         if (clmd->sim_parms->shapekey_rest != 0)
173                 dataMask |= CD_MASK_CLOTH_ORCO;
174
175         return dataMask;
176 }
177
178 static void copyData(ModifierData *md, ModifierData *target)
179 {
180         ClothModifierData *clmd = (ClothModifierData *) md;
181         ClothModifierData *tclmd = (ClothModifierData *) target;
182
183         if (tclmd->sim_parms) {
184                 if (tclmd->sim_parms->effector_weights)
185                         MEM_freeN(tclmd->sim_parms->effector_weights);
186                 MEM_freeN(tclmd->sim_parms);
187         }
188
189         if (tclmd->coll_parms)
190                 MEM_freeN(tclmd->coll_parms);
191         
192         BKE_ptcache_free_list(&tclmd->ptcaches);
193         tclmd->point_cache = NULL;
194
195         tclmd->sim_parms = MEM_dupallocN(clmd->sim_parms);
196         if (clmd->sim_parms->effector_weights)
197                 tclmd->sim_parms->effector_weights = MEM_dupallocN(clmd->sim_parms->effector_weights);
198         tclmd->coll_parms = MEM_dupallocN(clmd->coll_parms);
199         tclmd->point_cache = BKE_ptcache_add(&tclmd->ptcaches);
200         tclmd->point_cache->step = 1;
201         tclmd->clothObject = NULL;
202         tclmd->hairdata = NULL;
203         tclmd->solver_result = NULL;
204 }
205
206 static bool dependsOnTime(ModifierData *UNUSED(md))
207 {
208         return true;
209 }
210
211 static void freeData(ModifierData *md)
212 {
213         ClothModifierData *clmd = (ClothModifierData *) md;
214         
215         if (clmd) {
216                 if (G.debug_value > 0)
217                         printf("clothModifier_freeData\n");
218                 
219                 cloth_free_modifier_extern(clmd);
220                 
221                 if (clmd->sim_parms) {
222                         if (clmd->sim_parms->effector_weights)
223                                 MEM_freeN(clmd->sim_parms->effector_weights);
224                         MEM_freeN(clmd->sim_parms);
225                 }
226                 if (clmd->coll_parms)
227                         MEM_freeN(clmd->coll_parms);
228                 
229                 BKE_ptcache_free_list(&clmd->ptcaches);
230                 clmd->point_cache = NULL;
231                 
232                 if (clmd->hairdata)
233                         MEM_freeN(clmd->hairdata);
234                 
235                 if (clmd->solver_result)
236                         MEM_freeN(clmd->solver_result);
237         }
238 }
239
240 static void foreachIDLink(ModifierData *md, Object *ob,
241                           IDWalkFunc walk, void *userData)
242 {
243         ClothModifierData *clmd = (ClothModifierData *) md;
244
245         if (clmd->coll_parms) {
246                 walk(userData, ob, (ID **)&clmd->coll_parms->group);
247         }
248
249         if (clmd->sim_parms && clmd->sim_parms->effector_weights) {
250                 walk(userData, ob, (ID **)&clmd->sim_parms->effector_weights->group);
251         }
252 }
253
254 ModifierTypeInfo modifierType_Cloth = {
255         /* name */              "Cloth",
256         /* structName */        "ClothModifierData",
257         /* structSize */        sizeof(ClothModifierData),
258         /* type */              eModifierTypeType_OnlyDeform,
259         /* flags */             eModifierTypeFlag_AcceptsMesh |
260                                 eModifierTypeFlag_UsesPointCache |
261                                 eModifierTypeFlag_Single,
262
263         /* copyData */          copyData,
264         /* deformVerts */       deformVerts,
265         /* deformMatrices */    NULL,
266         /* deformVertsEM */     NULL,
267         /* deformMatricesEM */  NULL,
268         /* applyModifier */     NULL,
269         /* applyModifierEM */   NULL,
270         /* initData */          initData,
271         /* requiredDataMask */  requiredDataMask,
272         /* freeData */          freeData,
273         /* isDisabled */        NULL,
274         /* updateDepgraph */    updateDepgraph,
275         /* updateDepsgraph */   updateDepsgraph,
276         /* dependsOnTime */     dependsOnTime,
277         /* dependsOnNormals */  NULL,
278         /* foreachObjectLink */ NULL,
279         /* foreachIDLink */     foreachIDLink,
280         /* foreachTexLink */    NULL,
281 };