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