migrated NDOF code from soc-2010-merwin, SpaceNavigator now works on Mac blender
[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 /** \file blender/modifiers/intern/MOD_cloth.c
34  *  \ingroup modifiers
35  */
36
37
38 #include "DNA_cloth_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
47 #include "BKE_cloth.h"
48 #include "BKE_cdderivedmesh.h"
49 #include "BKE_global.h"
50 #include "BKE_modifier.h"
51 #include "BKE_pointcache.h"
52
53 #include "depsgraph_private.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 DerivedMesh *applyModifier(ModifierData *md, Object *ob,
73                                                 DerivedMesh *dm,
74                                                 int UNUSED(useRenderParams),
75                                                 int UNUSED(isFinalCalc))
76 {
77         ClothModifierData *clmd = (ClothModifierData*) md;
78         DerivedMesh *result=NULL;
79         
80         /* check for alloc failing */
81         if(!clmd->sim_parms || !clmd->coll_parms)
82         {
83                 initData(md);
84                 
85                 if(!clmd->sim_parms || !clmd->coll_parms)
86                         return dm;
87         }
88
89         result = clothModifier_do(clmd, md->scene, ob, dm);
90
91         if(result)
92         {
93                 CDDM_calc_normals(result);
94                 return result;
95         }
96         return dm;
97 }
98
99 static void updateDepgraph(
100                                          ModifierData *md, DagForest *forest, Scene *scene, Object *ob,
101           DagNode *obNode)
102 {
103         ClothModifierData *clmd = (ClothModifierData*) md;
104         
105         Base *base;
106         
107         if(clmd)
108         {
109                 for(base = scene->base.first; base; base= base->next) 
110                 {
111                         Object *ob1= base->object;
112                         if(ob1 != ob)
113                         {
114                                 CollisionModifierData *coll_clmd = (CollisionModifierData *)modifiers_findByType(ob1, eModifierType_Collision);
115                                 if(coll_clmd)
116                                 {
117                                         DagNode *curNode = dag_get_node(forest, ob1);
118                                         dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Cloth Collision");
119                                 }
120                         }
121                 }
122         }
123 }
124
125 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
126 {
127         CustomDataMask dataMask = 0;
128         ClothModifierData *clmd = (ClothModifierData*)md;
129
130         if(cloth_uses_vgroup(clmd))
131                 dataMask |= CD_MASK_MDEFORMVERT;
132
133         if(clmd->sim_parms->shapekey_rest != 0)
134                 dataMask |= CD_MASK_CLOTH_ORCO;
135
136         return dataMask;
137 }
138
139 static void copyData(ModifierData *md, ModifierData *target)
140 {
141         ClothModifierData *clmd = (ClothModifierData*) md;
142         ClothModifierData *tclmd = (ClothModifierData*) target;
143
144         if(tclmd->sim_parms) {
145                 if(tclmd->sim_parms->effector_weights)
146                         MEM_freeN(tclmd->sim_parms->effector_weights);
147                 MEM_freeN(tclmd->sim_parms);
148         }
149
150         if(tclmd->coll_parms)
151                 MEM_freeN(tclmd->coll_parms);
152         
153         BKE_ptcache_free_list(&tclmd->ptcaches);
154         tclmd->point_cache = NULL;
155
156         tclmd->sim_parms = MEM_dupallocN(clmd->sim_parms);
157         if(clmd->sim_parms->effector_weights)
158                 tclmd->sim_parms->effector_weights = MEM_dupallocN(clmd->sim_parms->effector_weights);
159         tclmd->coll_parms = MEM_dupallocN(clmd->coll_parms);
160         tclmd->point_cache = BKE_ptcache_copy_list(&tclmd->ptcaches, &clmd->ptcaches);
161         tclmd->clothObject = NULL;
162 }
163
164 static int dependsOnTime(ModifierData *UNUSED(md))
165 {
166         return 1;
167 }
168
169 static void freeData(ModifierData *md)
170 {
171         ClothModifierData *clmd = (ClothModifierData*) md;
172         
173         if (clmd) 
174         {
175                 if(G.rt > 0)
176                         printf("clothModifier_freeData\n");
177                 
178                 cloth_free_modifier_extern (clmd);
179                 
180                 if(clmd->sim_parms) {
181                         if(clmd->sim_parms->effector_weights)
182                                 MEM_freeN(clmd->sim_parms->effector_weights);
183                         MEM_freeN(clmd->sim_parms);
184                 }
185                 if(clmd->coll_parms)
186                         MEM_freeN(clmd->coll_parms);    
187                 
188                 BKE_ptcache_free_list(&clmd->ptcaches);
189                 clmd->point_cache = NULL;
190         }
191 }
192
193
194 ModifierTypeInfo modifierType_Cloth = {
195         /* name */              "Cloth",
196         /* structName */        "ClothModifierData",
197         /* structSize */        sizeof(ClothModifierData),
198         /* type */              eModifierTypeType_Nonconstructive,
199         /* flags */             eModifierTypeFlag_AcceptsMesh
200                                                         | eModifierTypeFlag_UsesPointCache
201                                                         | eModifierTypeFlag_Single,
202
203         /* copyData */          copyData,
204         /* deformVerts */       NULL,
205         /* deformMatrices */    NULL,
206         /* deformVertsEM */     NULL,
207         /* deformMatricesEM */  NULL,
208         /* applyModifier */     applyModifier,
209         /* applyModifierEM */   NULL,
210         /* initData */          initData,
211         /* requiredDataMask */  requiredDataMask,
212         /* freeData */          freeData,
213         /* isDisabled */        NULL,
214         /* updateDepgraph */    updateDepgraph,
215         /* dependsOnTime */     dependsOnTime,
216         /* dependsOnNormals */  NULL,
217         /* foreachObjectLink */ NULL,
218         /* foreachIDLink */     NULL,
219 };