- remove unused includes IMB_*, BIF_* & MEM_*
[blender.git] / source / blender / modifiers / intern / MOD_cloth.c
1 /*
2 * $Id$
3 *
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software  Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 * The Original Code is Copyright (C) 2005 by the Blender Foundation.
21 * All rights reserved.
22 *
23 * Contributor(s): Daniel Dunbar
24 *                 Ton Roosendaal,
25 *                 Ben Batt,
26 *                 Brecht Van Lommel,
27 *                 Campbell Barton
28 *
29 * ***** END GPL LICENSE BLOCK *****
30 *
31 */
32
33 #include "DNA_cloth_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_object_types.h"
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BKE_cloth.h"
40 #include "BKE_cdderivedmesh.h"
41 #include "BKE_global.h"
42 #include "BKE_modifier.h"
43 #include "BKE_pointcache.h"
44
45 #include "depsgraph_private.h"
46
47
48 static void initData(ModifierData *md) 
49 {
50         ClothModifierData *clmd = (ClothModifierData*) md;
51         
52         clmd->sim_parms = MEM_callocN(sizeof(ClothSimSettings), "cloth sim parms");
53         clmd->coll_parms = MEM_callocN(sizeof(ClothCollSettings), "cloth coll parms");
54         clmd->point_cache = BKE_ptcache_add(&clmd->ptcaches);
55         
56         /* check for alloc failing */
57         if(!clmd->sim_parms || !clmd->coll_parms || !clmd->point_cache)
58                 return;
59         
60         cloth_init (clmd);
61 }
62
63 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
64                 DerivedMesh *derivedData, int useRenderParams, int isFinalCalc)
65 {
66         ClothModifierData *clmd = (ClothModifierData*) md;
67         DerivedMesh *result=NULL;
68         
69         /* check for alloc failing */
70         if(!clmd->sim_parms || !clmd->coll_parms)
71         {
72                 initData(md);
73                 
74                 if(!clmd->sim_parms || !clmd->coll_parms)
75                         return derivedData;
76         }
77
78         result = clothModifier_do(clmd, md->scene, ob, derivedData, useRenderParams, isFinalCalc);
79
80         if(result)
81         {
82                 CDDM_calc_normals(result);
83                 return result;
84         }
85         return derivedData;
86 }
87
88 static void updateDepgraph(
89                                          ModifierData *md, DagForest *forest, Scene *scene, Object *ob,
90           DagNode *obNode)
91 {
92         ClothModifierData *clmd = (ClothModifierData*) md;
93         
94         Base *base;
95         
96         if(clmd)
97         {
98                 for(base = scene->base.first; base; base= base->next) 
99                 {
100                         Object *ob1= base->object;
101                         if(ob1 != ob)
102                         {
103                                 CollisionModifierData *coll_clmd = (CollisionModifierData *)modifiers_findByType(ob1, eModifierType_Collision);
104                                 if(coll_clmd)
105                                 {
106                                         DagNode *curNode = dag_get_node(forest, ob1);
107                                         dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Cloth Collision");
108                                 }
109                         }
110                 }
111         }
112 }
113
114 static CustomDataMask requiredDataMask(Object *ob, ModifierData *md)
115 {
116         CustomDataMask dataMask = 0;
117         ClothModifierData *clmd = (ClothModifierData*)md;
118
119         if(cloth_uses_vgroup(clmd))
120                 dataMask |= (1 << CD_MDEFORMVERT);
121
122         if(clmd->sim_parms->shapekey_rest != 0)
123                 dataMask |= (1 << CD_CLOTH_ORCO);
124
125         return dataMask;
126 }
127
128 static void copyData(ModifierData *md, ModifierData *target)
129 {
130         ClothModifierData *clmd = (ClothModifierData*) md;
131         ClothModifierData *tclmd = (ClothModifierData*) target;
132
133         if(tclmd->sim_parms) {
134                 if(tclmd->sim_parms->effector_weights)
135                         MEM_freeN(tclmd->sim_parms->effector_weights);
136                 MEM_freeN(tclmd->sim_parms);
137         }
138
139         if(tclmd->coll_parms)
140                 MEM_freeN(tclmd->coll_parms);
141         
142         BKE_ptcache_free_list(&tclmd->ptcaches);
143         tclmd->point_cache = NULL;
144
145         tclmd->sim_parms = MEM_dupallocN(clmd->sim_parms);
146         if(clmd->sim_parms->effector_weights)
147                 tclmd->sim_parms->effector_weights = MEM_dupallocN(clmd->sim_parms->effector_weights);
148         tclmd->coll_parms = MEM_dupallocN(clmd->coll_parms);
149         tclmd->point_cache = BKE_ptcache_copy_list(&tclmd->ptcaches, &clmd->ptcaches);
150         tclmd->clothObject = NULL;
151 }
152
153 static int dependsOnTime(ModifierData *md)
154 {
155         return 1;
156 }
157
158 static void freeData(ModifierData *md)
159 {
160         ClothModifierData *clmd = (ClothModifierData*) md;
161         
162         if (clmd) 
163         {
164                 if(G.rt > 0)
165                         printf("clothModifier_freeData\n");
166                 
167                 cloth_free_modifier_extern (clmd);
168                 
169                 if(clmd->sim_parms) {
170                         if(clmd->sim_parms->effector_weights)
171                                 MEM_freeN(clmd->sim_parms->effector_weights);
172                         MEM_freeN(clmd->sim_parms);
173                 }
174                 if(clmd->coll_parms)
175                         MEM_freeN(clmd->coll_parms);    
176                 
177                 BKE_ptcache_free_list(&clmd->ptcaches);
178                 clmd->point_cache = NULL;
179         }
180 }
181
182
183 ModifierTypeInfo modifierType_Cloth = {
184         /* name */              "Cloth",
185         /* structName */        "ClothModifierData",
186         /* structSize */        sizeof(ClothModifierData),
187         /* type */              eModifierTypeType_Nonconstructive,
188         /* flags */             eModifierTypeFlag_AcceptsMesh
189                                                         | eModifierTypeFlag_UsesPointCache
190                                                         | eModifierTypeFlag_Single,
191
192         /* copyData */          copyData,
193         /* deformVerts */       0,
194         /* deformVertsEM */     0,
195         /* deformMatricesEM */  0,
196         /* applyModifier */     applyModifier,
197         /* applyModifierEM */   0,
198         /* initData */          initData,
199         /* requiredDataMask */  requiredDataMask,
200         /* freeData */          freeData,
201         /* isDisabled */        0,
202         /* updateDepgraph */    updateDepgraph,
203         /* dependsOnTime */     dependsOnTime,
204         /* foreachObjectLink */ 0,
205         /* foreachIDLink */     0,
206 };