Modifiers: Add wrapper functions with Mesh / DerivedMesh conversion
[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 Depsgraph *depsgraph, 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, depsgraph, md->scene, ob, dm, vertexCos);
113
114         dm->release(dm);
115 }
116
117 static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
118 {
119         ClothModifierData *clmd = (ClothModifierData *)md;
120         if (clmd != NULL) {
121                 /* Actual code uses get_collisionobjects */
122                 DEG_add_collision_relations(ctx->node, ctx->scene, ctx->object, clmd->coll_parms->group, eModifierType_Collision, NULL, true, "Cloth Collision");
123
124                 DEG_add_forcefield_relations(ctx->node, ctx->scene, ctx->object, clmd->sim_parms->effector_weights, true, 0, "Cloth Field");
125         }
126 }
127
128 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
129 {
130         CustomDataMask dataMask = 0;
131         ClothModifierData *clmd = (ClothModifierData *)md;
132
133         if (cloth_uses_vgroup(clmd))
134                 dataMask |= CD_MASK_MDEFORMVERT;
135
136         if (clmd->sim_parms->shapekey_rest != 0)
137                 dataMask |= CD_MASK_CLOTH_ORCO;
138
139         return dataMask;
140 }
141
142 static void copyData(ModifierData *md, ModifierData *target)
143 {
144         ClothModifierData *clmd = (ClothModifierData *) md;
145         ClothModifierData *tclmd = (ClothModifierData *) target;
146
147         if (tclmd->sim_parms) {
148                 if (tclmd->sim_parms->effector_weights)
149                         MEM_freeN(tclmd->sim_parms->effector_weights);
150                 MEM_freeN(tclmd->sim_parms);
151         }
152
153         if (tclmd->coll_parms)
154                 MEM_freeN(tclmd->coll_parms);
155         
156         BKE_ptcache_free_list(&tclmd->ptcaches);
157         tclmd->point_cache = NULL;
158
159         tclmd->sim_parms = MEM_dupallocN(clmd->sim_parms);
160         if (clmd->sim_parms->effector_weights)
161                 tclmd->sim_parms->effector_weights = MEM_dupallocN(clmd->sim_parms->effector_weights);
162         tclmd->coll_parms = MEM_dupallocN(clmd->coll_parms);
163         tclmd->point_cache = BKE_ptcache_add(&tclmd->ptcaches);
164         tclmd->point_cache->step = 1;
165         tclmd->clothObject = NULL;
166         tclmd->hairdata = NULL;
167         tclmd->solver_result = NULL;
168 }
169
170 static bool dependsOnTime(ModifierData *UNUSED(md))
171 {
172         return true;
173 }
174
175 static void freeData(ModifierData *md)
176 {
177         ClothModifierData *clmd = (ClothModifierData *) md;
178         
179         if (clmd) {
180                 if (G.debug_value > 0)
181                         printf("clothModifier_freeData\n");
182                 
183                 cloth_free_modifier_extern(clmd);
184                 
185                 if (clmd->sim_parms) {
186                         if (clmd->sim_parms->effector_weights)
187                                 MEM_freeN(clmd->sim_parms->effector_weights);
188                         MEM_freeN(clmd->sim_parms);
189                 }
190                 if (clmd->coll_parms)
191                         MEM_freeN(clmd->coll_parms);
192                 
193                 BKE_ptcache_free_list(&clmd->ptcaches);
194                 clmd->point_cache = NULL;
195                 
196                 if (clmd->hairdata)
197                         MEM_freeN(clmd->hairdata);
198                 
199                 if (clmd->solver_result)
200                         MEM_freeN(clmd->solver_result);
201         }
202 }
203
204 static void foreachIDLink(ModifierData *md, Object *ob,
205                           IDWalkFunc walk, void *userData)
206 {
207         ClothModifierData *clmd = (ClothModifierData *) md;
208
209         if (clmd->coll_parms) {
210                 walk(userData, ob, (ID **)&clmd->coll_parms->group, IDWALK_CB_NOP);
211         }
212
213         if (clmd->sim_parms && clmd->sim_parms->effector_weights) {
214                 walk(userData, ob, (ID **)&clmd->sim_parms->effector_weights->group, IDWALK_CB_NOP);
215         }
216 }
217
218 ModifierTypeInfo modifierType_Cloth = {
219         /* name */              "Cloth",
220         /* structName */        "ClothModifierData",
221         /* structSize */        sizeof(ClothModifierData),
222         /* type */              eModifierTypeType_OnlyDeform,
223         /* flags */             eModifierTypeFlag_AcceptsMesh |
224                                 eModifierTypeFlag_UsesPointCache |
225                                 eModifierTypeFlag_Single,
226
227         /* copyData */          copyData,
228
229         /* deformVerts_DM */    deformVerts,
230         /* deformMatrices_DM */ NULL,
231         /* deformVertsEM_DM */  NULL,
232         /* deformMatricesEM_DM*/NULL,
233         /* applyModifier_DM */  NULL,
234         /* applyModifierEM_DM */NULL,
235
236         /* deformVerts */       NULL,
237         /* deformMatrices */    NULL,
238         /* deformVertsEM */     NULL,
239         /* deformMatricesEM */  NULL,
240         /* applyModifier */     NULL,
241         /* applyModifierEM */   NULL,
242
243         /* initData */          initData,
244         /* requiredDataMask */  requiredDataMask,
245         /* freeData */          freeData,
246         /* isDisabled */        NULL,
247         /* updateDepsgraph */   updateDepsgraph,
248         /* dependsOnTime */     dependsOnTime,
249         /* dependsOnNormals */  NULL,
250         /* foreachObjectLink */ NULL,
251         /* foreachIDLink */     foreachIDLink,
252         /* foreachTexLink */    NULL,
253 };