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