Merge with trunk r41342
[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
36 #include "DNA_cloth_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_object_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_utildefines.h"
43
44
45 #include "BKE_cloth.h"
46 #include "BKE_cdderivedmesh.h"
47 #include "BKE_global.h"
48 #include "BKE_modifier.h"
49 #include "BKE_pointcache.h"
50
51 #include "depsgraph_private.h"
52
53 #include "MOD_util.h"
54
55 static void initData(ModifierData *md) 
56 {
57         ClothModifierData *clmd = (ClothModifierData*) md;
58         
59         clmd->sim_parms = MEM_callocN(sizeof(ClothSimSettings), "cloth sim parms");
60         clmd->coll_parms = MEM_callocN(sizeof(ClothCollSettings), "cloth coll parms");
61         clmd->point_cache = BKE_ptcache_add(&clmd->ptcaches);
62         
63         /* check for alloc failing */
64         if(!clmd->sim_parms || !clmd->coll_parms || !clmd->point_cache)
65                 return;
66         
67         cloth_init (clmd);
68 }
69
70 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
71                                                 DerivedMesh *dm,
72                                                 int UNUSED(useRenderParams),
73                                                 int UNUSED(isFinalCalc))
74 {
75         ClothModifierData *clmd = (ClothModifierData*) md;
76         DerivedMesh *result=NULL;
77         
78         /* check for alloc failing */
79         if(!clmd->sim_parms || !clmd->coll_parms)
80         {
81                 initData(md);
82                 
83                 if(!clmd->sim_parms || !clmd->coll_parms)
84                         return dm;
85         }
86
87         result = clothModifier_do(clmd, md->scene, ob, dm);
88
89         if(result)
90         {
91                 CDDM_calc_normals(result);
92                 return result;
93         }
94         return dm;
95 }
96
97 static void updateDepgraph(
98                                          ModifierData *md, DagForest *forest, Scene *scene, Object *ob,
99           DagNode *obNode)
100 {
101         ClothModifierData *clmd = (ClothModifierData*) md;
102         
103         Base *base;
104         
105         if(clmd)
106         {
107                 for(base = scene->base.first; base; base= base->next) 
108                 {
109                         Object *ob1= base->object;
110                         if(ob1 != ob)
111                         {
112                                 CollisionModifierData *coll_clmd = (CollisionModifierData *)modifiers_findByType(ob1, eModifierType_Collision);
113                                 if(coll_clmd)
114                                 {
115                                         DagNode *curNode = dag_get_node(forest, ob1);
116                                         dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Cloth Collision");
117                                 }
118                         }
119                 }
120         }
121 }
122
123 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
124 {
125         CustomDataMask dataMask = 0;
126         ClothModifierData *clmd = (ClothModifierData*)md;
127
128         if(cloth_uses_vgroup(clmd))
129                 dataMask |= CD_MASK_MDEFORMVERT;
130
131         if(clmd->sim_parms->shapekey_rest != 0)
132                 dataMask |= CD_MASK_CLOTH_ORCO;
133
134         return dataMask;
135 }
136
137 static void copyData(ModifierData *md, ModifierData *target)
138 {
139         ClothModifierData *clmd = (ClothModifierData*) md;
140         ClothModifierData *tclmd = (ClothModifierData*) target;
141
142         if(tclmd->sim_parms) {
143                 if(tclmd->sim_parms->effector_weights)
144                         MEM_freeN(tclmd->sim_parms->effector_weights);
145                 MEM_freeN(tclmd->sim_parms);
146         }
147
148         if(tclmd->coll_parms)
149                 MEM_freeN(tclmd->coll_parms);
150         
151         BKE_ptcache_free_list(&tclmd->ptcaches);
152         tclmd->point_cache = NULL;
153
154         tclmd->sim_parms = MEM_dupallocN(clmd->sim_parms);
155         if(clmd->sim_parms->effector_weights)
156                 tclmd->sim_parms->effector_weights = MEM_dupallocN(clmd->sim_parms->effector_weights);
157         tclmd->coll_parms = MEM_dupallocN(clmd->coll_parms);
158         tclmd->point_cache = BKE_ptcache_copy_list(&tclmd->ptcaches, &clmd->ptcaches);
159         tclmd->clothObject = NULL;
160 }
161
162 static int dependsOnTime(ModifierData *UNUSED(md))
163 {
164         return 1;
165 }
166
167 static void freeData(ModifierData *md)
168 {
169         ClothModifierData *clmd = (ClothModifierData*) md;
170         
171         if (clmd) 
172         {
173                 if(G.rt > 0)
174                         printf("clothModifier_freeData\n");
175                 
176                 cloth_free_modifier_extern (clmd);
177                 
178                 if(clmd->sim_parms) {
179                         if(clmd->sim_parms->effector_weights)
180                                 MEM_freeN(clmd->sim_parms->effector_weights);
181                         MEM_freeN(clmd->sim_parms);
182                 }
183                 if(clmd->coll_parms)
184                         MEM_freeN(clmd->coll_parms);    
185                 
186                 BKE_ptcache_free_list(&clmd->ptcaches);
187                 clmd->point_cache = NULL;
188         }
189 }
190
191 static void foreachIDLink(ModifierData *md, Object *ob,
192                                            IDWalkFunc walk, void *userData)
193 {
194         ClothModifierData *clmd = (ClothModifierData*) md;
195
196         if(clmd->coll_parms) {
197                 walk(userData, ob, (ID **)&clmd->coll_parms->group);
198         }
199
200         if(clmd->sim_parms && clmd->sim_parms->effector_weights) {
201                 walk(userData, ob, (ID **)&clmd->sim_parms->effector_weights->group);
202         }
203 }
204
205 ModifierTypeInfo modifierType_Cloth = {
206         /* name */              "Cloth",
207         /* structName */        "ClothModifierData",
208         /* structSize */        sizeof(ClothModifierData),
209         /* type */              eModifierTypeType_Nonconstructive,
210         /* flags */             eModifierTypeFlag_AcceptsMesh
211                                                         | eModifierTypeFlag_UsesPointCache
212                                                         | eModifierTypeFlag_Single,
213
214         /* copyData */          copyData,
215         /* deformVerts */       NULL,
216         /* deformMatrices */    NULL,
217         /* deformVertsEM */     NULL,
218         /* deformMatricesEM */  NULL,
219         /* applyModifier */     applyModifier,
220         /* applyModifierEM */   NULL,
221         /* initData */          initData,
222         /* requiredDataMask */  requiredDataMask,
223         /* freeData */          freeData,
224         /* isDisabled */        NULL,
225         /* updateDepgraph */    updateDepgraph,
226         /* dependsOnTime */     dependsOnTime,
227         /* dependsOnNormals */  NULL,
228         /* foreachObjectLink */ NULL,
229         /* foreachIDLink */     foreachIDLink,
230         /* foreachTexLink */    NULL,
231 };