9335d7577c7d349f4dc96353248d963e1c406650
[blender-staging.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 bool dependsOnTime(ModifierData *UNUSED(md))
103 {
104         return true;
105 }
106
107 static void deformVerts(ModifierData *md, Object *ob,
108                         DerivedMesh *derivedData,
109                         float (*vertexCos)[3],
110                         int UNUSED(numVerts),
111                         ModifierApplyFlag UNUSED(flag))
112 {
113         CollisionModifierData *collmd = (CollisionModifierData *) md;
114         DerivedMesh *dm = NULL;
115         MVert *tempVert = NULL;
116         
117         /* if possible use/create DerivedMesh */
118         if (derivedData) dm = CDDM_copy(derivedData);
119         else if (ob->type == OB_MESH) dm = CDDM_from_mesh(ob->data, ob);
120         
121         if (!ob->pd) {
122                 printf("CollisionModifier deformVerts: Should not happen!\n");
123                 return;
124         }
125         
126         if (dm) {
127                 float current_time = 0;
128                 unsigned int numverts = 0;
129
130                 CDDM_apply_vert_coords(dm, vertexCos);
131                 CDDM_calc_normals(dm);
132                 
133                 current_time = BKE_scene_frame_get(md->scene);
134                 
135                 if (G.debug_value > 0)
136                         printf("current_time %f, collmd->time_xnew %f\n", current_time, collmd->time_xnew);
137                 
138                 numverts = dm->getNumVerts(dm);
139                 
140                 if (current_time > collmd->time_xnew) {
141                         unsigned int i;
142
143                         /* check if mesh has changed */
144                         if (collmd->x && (numverts != collmd->numverts))
145                                 freeData((ModifierData *)collmd);
146
147                         if (collmd->time_xnew == -1000) { /* first time */
148                                 collmd->x = dm->dupVertArray(dm); /* frame start position */
149
150                                 for (i = 0; i < numverts; i++) {
151                                         /* we save global positions */
152                                         mul_m4_v3(ob->obmat, collmd->x[i].co);
153                                 }
154                                 
155                                 collmd->xnew = MEM_dupallocN(collmd->x); // frame end position
156                                 collmd->current_x = MEM_dupallocN(collmd->x); // inter-frame
157                                 collmd->current_xnew = MEM_dupallocN(collmd->x); // inter-frame
158                                 collmd->current_v = MEM_dupallocN(collmd->x); // inter-frame
159
160                                 collmd->numverts = numverts;
161                                 
162                                 DM_ensure_tessface(dm); /* BMESH - UNTIL MODIFIER IS UPDATED FOR MPoly */
163
164                                 collmd->mfaces = dm->dupTessFaceArray(dm);
165                                 collmd->numfaces = dm->getNumTessFaces(dm);
166                                 
167                                 /* create bounding box hierarchy */
168                                 collmd->bvhtree = bvhtree_build_from_mvert(collmd->mfaces, collmd->numfaces, collmd->x, numverts, ob->pd->pdef_sboft);
169
170                                 collmd->time_x = collmd->time_xnew = current_time;
171                         }
172                         else if (numverts == collmd->numverts) {
173                                 /* put positions to old positions */
174                                 tempVert = collmd->x;
175                                 collmd->x = collmd->xnew;
176                                 collmd->xnew = tempVert;
177                                 collmd->time_x = collmd->time_xnew;
178
179                                 memcpy(collmd->xnew, dm->getVertArray(dm), numverts * sizeof(MVert));
180
181                                 for (i = 0; i < numverts; i++) {
182                                         /* we save global positions */
183                                         mul_m4_v3(ob->obmat, collmd->xnew[i].co);
184                                 }
185                                 
186                                 memcpy(collmd->current_xnew, collmd->x, numverts * sizeof(MVert));
187                                 memcpy(collmd->current_x, collmd->x, numverts * sizeof(MVert));
188                                 
189                                 /* check if GUI setting has changed for bvh */
190                                 if (collmd->bvhtree) {
191                                         if (ob->pd->pdef_sboft != BLI_bvhtree_getepsilon(collmd->bvhtree)) {
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                                         collmd->bvhtree = bvhtree_build_from_mvert(collmd->mfaces, collmd->numfaces, collmd->current_x, numverts, ob->pd->pdef_sboft);
201                                 }
202                                 else {
203                                         /* recalc static bounding boxes */
204                                         bvhtree_update_from_mvert(collmd->bvhtree, collmd->mfaces, collmd->numfaces, collmd->current_x, collmd->current_xnew, collmd->numverts, 1);
205                                 }
206                                 
207                                 collmd->time_xnew = current_time;
208                         }
209                         else if (numverts != collmd->numverts) {
210                                 freeData((ModifierData *)collmd);
211                         }
212                         
213                 }
214                 else if (current_time < collmd->time_xnew) {
215                         freeData((ModifierData *)collmd);
216                 }
217                 else {
218                         if (numverts != collmd->numverts) {
219                                 freeData((ModifierData *)collmd);
220                         }
221                 }
222         }
223         
224         if (dm)
225                 dm->release(dm);
226 }
227
228
229 ModifierTypeInfo modifierType_Collision = {
230         /* name */              "Collision",
231         /* structName */        "CollisionModifierData",
232         /* structSize */        sizeof(CollisionModifierData),
233         /* type */              eModifierTypeType_OnlyDeform,
234         /* flags */             eModifierTypeFlag_AcceptsMesh |
235                                 eModifierTypeFlag_Single,
236
237         /* copyData */          NULL,
238         /* deformVerts */       deformVerts,
239         /* deformMatrices */    NULL,
240         /* deformVertsEM */     NULL,
241         /* deformMatricesEM */  NULL,
242         /* applyModifier */     NULL,
243         /* applyModifierEM */   NULL,
244         /* initData */          initData,
245         /* requiredDataMask */  NULL,
246         /* freeData */          freeData,
247         /* isDisabled */        NULL,
248         /* updateDepgraph */    NULL,
249         /* dependsOnTime */     dependsOnTime,
250         /* dependsOnNormals */  NULL,
251         /* foreachObjectLink */ NULL,
252         /* foreachIDLink */     NULL,
253         /* foreachTexLink */    NULL,
254 };