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