add UNUSED() to modifiers, also removed some unused args.
[blender.git] / source / blender / modifiers / intern / MOD_collision.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_scene_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_meshdata_types.h"
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_math.h"
40
41 #include "BKE_utildefines.h"
42 #include "BKE_collision.h"
43 #include "BKE_cdderivedmesh.h"
44 #include "BKE_global.h"
45 #include "BKE_modifier.h"
46 #include "BKE_object.h"
47 #include "BKE_pointcache.h"
48 #include "BKE_scene.h"
49
50
51 static void initData(ModifierData *md) 
52 {
53         CollisionModifierData *collmd = (CollisionModifierData*) md;
54         
55         collmd->x = NULL;
56         collmd->xnew = NULL;
57         collmd->current_x = NULL;
58         collmd->current_xnew = NULL;
59         collmd->current_v = NULL;
60         collmd->time = -1000;
61         collmd->numverts = 0;
62         collmd->bvhtree = NULL;
63 }
64
65 static void freeData(ModifierData *md)
66 {
67         CollisionModifierData *collmd = (CollisionModifierData*) md;
68         
69         if (collmd) 
70         {
71                 if(collmd->bvhtree)
72                         BLI_bvhtree_free(collmd->bvhtree);
73                 if(collmd->x)
74                         MEM_freeN(collmd->x);
75                 if(collmd->xnew)
76                         MEM_freeN(collmd->xnew);
77                 if(collmd->current_x)
78                         MEM_freeN(collmd->current_x);
79                 if(collmd->current_xnew)
80                         MEM_freeN(collmd->current_xnew);
81                 if(collmd->current_v)
82                         MEM_freeN(collmd->current_v);
83                 if(collmd->mfaces)
84                         MEM_freeN(collmd->mfaces);
85                 
86                 collmd->x = NULL;
87                 collmd->xnew = NULL;
88                 collmd->current_x = NULL;
89                 collmd->current_xnew = NULL;
90                 collmd->current_v = NULL;
91                 collmd->time = -1000;
92                 collmd->numverts = 0;
93                 collmd->bvhtree = NULL;
94                 collmd->mfaces = NULL;
95         }
96 }
97
98 static int dependsOnTime(ModifierData *UNUSED(md))
99 {
100         return 1;
101 }
102
103 static void deformVerts(ModifierData *md, Object *ob,
104                                                 DerivedMesh *derivedData,
105                                                 float (*vertexCos)[3],
106                                                 int UNUSED(numVerts),
107                                                 int UNUSED(useRenderParams),
108                                                 int UNUSED(isFinalCalc))
109 {
110         CollisionModifierData *collmd = (CollisionModifierData*) md;
111         DerivedMesh *dm = NULL;
112         float current_time = 0;
113         unsigned int numverts = 0, i = 0;
114         MVert *tempVert = NULL;
115         
116         /* if possible use/create DerivedMesh */
117         if(derivedData) dm = CDDM_copy(derivedData);
118         else if(ob->type==OB_MESH) dm = CDDM_from_mesh(ob->data, ob);
119         
120         if(!ob->pd)
121         {
122                 printf("CollisionModifier deformVerts: Should not happen!\n");
123                 return;
124         }
125         
126         if(dm)
127         {
128                 CDDM_apply_vert_coords(dm, vertexCos);
129                 CDDM_calc_normals(dm);
130                 
131                 current_time = BKE_curframe(md->scene);
132                 
133                 if(G.rt > 0)
134                         printf("current_time %f, collmd->time %f\n", current_time, collmd->time);
135                 
136                 numverts = dm->getNumVerts ( dm );
137                 
138                 if((current_time > collmd->time)|| (BKE_ptcache_get_continue_physics()))
139                 {       
140                         // check if mesh has changed
141                         if(collmd->x && (numverts != collmd->numverts))
142                                 freeData((ModifierData *)collmd);
143                         
144                         if(collmd->time == -1000) // first time
145                         {
146                                 collmd->x = dm->dupVertArray(dm); // frame start position
147                                 
148                                 for ( i = 0; i < numverts; i++ )
149                                 {
150                                         // we save global positions
151                                         mul_m4_v3( ob->obmat, collmd->x[i].co );
152                                 }
153                                 
154                                 collmd->xnew = MEM_dupallocN(collmd->x); // frame end position
155                                 collmd->current_x = MEM_dupallocN(collmd->x); // inter-frame
156                                 collmd->current_xnew = MEM_dupallocN(collmd->x); // inter-frame
157                                 collmd->current_v = MEM_dupallocN(collmd->x); // inter-frame
158
159                                 collmd->numverts = numverts;
160                                 
161                                 collmd->mfaces = dm->dupFaceArray(dm);
162                                 collmd->numfaces = dm->getNumFaces(dm);
163                                 
164                                 // create bounding box hierarchy
165                                 collmd->bvhtree = bvhtree_build_from_mvert(collmd->mfaces, collmd->numfaces, collmd->x, numverts, ob->pd->pdef_sboft);
166                                 
167                                 collmd->time = current_time;
168                         }
169                         else if(numverts == collmd->numverts)
170                         {
171                                 // put positions to old positions
172                                 tempVert = collmd->x;
173                                 collmd->x = collmd->xnew;
174                                 collmd->xnew = tempVert;
175                                 
176                                 memcpy(collmd->xnew, dm->getVertArray(dm), numverts*sizeof(MVert));
177                                 
178                                 for ( i = 0; i < numverts; i++ )
179                                 {
180                                         // we save global positions
181                                         mul_m4_v3( ob->obmat, collmd->xnew[i].co );
182                                 }
183                                 
184                                 memcpy(collmd->current_xnew, collmd->x, numverts*sizeof(MVert));
185                                 memcpy(collmd->current_x, collmd->x, numverts*sizeof(MVert));
186                                 
187                                 /* check if GUI setting has changed for bvh */
188                                 if(collmd->bvhtree) 
189                                 {
190                                         if(ob->pd->pdef_sboft != BLI_bvhtree_getepsilon(collmd->bvhtree))
191                                         {
192                                                 BLI_bvhtree_free(collmd->bvhtree);
193                                                 collmd->bvhtree = bvhtree_build_from_mvert(collmd->mfaces, collmd->numfaces, collmd->current_x, numverts, ob->pd->pdef_sboft);
194                                         }
195                         
196                                 }
197                                 
198                                 /* happens on file load (ONLY when i decomment changes in readfile.c) */
199                                 if(!collmd->bvhtree)
200                                 {
201                                         collmd->bvhtree = bvhtree_build_from_mvert(collmd->mfaces, collmd->numfaces, collmd->current_x, numverts, ob->pd->pdef_sboft);
202                                 }
203                                 else
204                                 {
205                                         // recalc static bounding boxes
206                                         bvhtree_update_from_mvert ( collmd->bvhtree, collmd->mfaces, collmd->numfaces, collmd->current_x, collmd->current_xnew, collmd->numverts, 1 );
207                                 }
208                                 
209                                 collmd->time = current_time;
210                         }
211                         else if(numverts != collmd->numverts)
212                         {
213                                 freeData((ModifierData *)collmd);
214                         }
215                         
216                 }
217                 else if(current_time < collmd->time)
218                 {       
219                         freeData((ModifierData *)collmd);
220                 }
221                 else
222                 {
223                         if(numverts != collmd->numverts)
224                         {
225                                 freeData((ModifierData *)collmd);
226                         }
227                 }
228         }
229         
230         if(dm)
231                 dm->release(dm);
232 }
233
234
235 ModifierTypeInfo modifierType_Collision = {
236         /* name */              "Collision",
237         /* structName */        "CollisionModifierData",
238         /* structSize */        sizeof(CollisionModifierData),
239         /* type */              eModifierTypeType_OnlyDeform,
240         /* flags */             eModifierTypeFlag_AcceptsMesh
241                                                         | eModifierTypeFlag_Single,
242
243         /* copyData */          0,
244         /* deformVerts */       deformVerts,
245         /* deformVertsEM */     0,
246         /* deformMatricesEM */  0,
247         /* applyModifier */     0,
248         /* applyModifierEM */   0,
249         /* initData */          initData,
250         /* requiredDataMask */  0,
251         /* freeData */          freeData,
252         /* isDisabled */        0,
253         /* updateDepgraph */    0,
254         /* dependsOnTime */     dependsOnTime,
255         /* dependsOnNormals */  0,
256         /* foreachObjectLink */ 0,
257         /* foreachIDLink */     0,
258 };