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