Merge branch 'master' into blender2.8
[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 #include "DNA_object_types.h"
36 #include "DNA_meshdata_types.h"
37 #include "DNA_object_force.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_math.h"
42 #include "BLI_utildefines.h"
43
44
45 #include "BKE_collision.h"
46 #include "BKE_cdderivedmesh.h"
47 #include "BKE_global.h"
48 #include "BKE_modifier.h"
49 #include "BKE_scene.h"
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_x = collmd->time_xnew = -1000;
61         collmd->mvert_num = 0;
62         collmd->tri_num = 0;
63         collmd->bvhtree = NULL;
64 }
65
66 static void freeData(ModifierData *md)
67 {
68         CollisionModifierData *collmd = (CollisionModifierData *) md;
69         
70         if (collmd) {
71                 if (collmd->bvhtree) {
72                         BLI_bvhtree_free(collmd->bvhtree);
73                         collmd->bvhtree = NULL;
74                 }
75
76                 MEM_SAFE_FREE(collmd->x);
77                 MEM_SAFE_FREE(collmd->xnew);
78                 MEM_SAFE_FREE(collmd->current_x);
79                 MEM_SAFE_FREE(collmd->current_xnew);
80                 MEM_SAFE_FREE(collmd->current_v);
81
82                 if (collmd->tri) {
83                         MEM_freeN((void *)collmd->tri);
84                         collmd->tri = NULL;
85                 }
86
87                 collmd->time_x = collmd->time_xnew = -1000;
88                 collmd->mvert_num = 0;
89                 collmd->tri_num = 0;
90         }
91 }
92
93 static bool dependsOnTime(ModifierData *UNUSED(md))
94 {
95         return true;
96 }
97
98 static void deformVerts(ModifierData *md, Object *ob,
99                         DerivedMesh *derivedData,
100                         float (*vertexCos)[3],
101                         int UNUSED(numVerts),
102                         ModifierApplyFlag UNUSED(flag))
103 {
104         CollisionModifierData *collmd = (CollisionModifierData *) md;
105         DerivedMesh *dm = NULL;
106         MVert *tempVert = NULL;
107         
108         /* if possible use/create DerivedMesh */
109         if (derivedData) dm = CDDM_copy(derivedData);
110         else if (ob->type == OB_MESH) dm = CDDM_from_mesh(ob->data);
111         
112         if (!ob->pd) {
113                 printf("CollisionModifier deformVerts: Should not happen!\n");
114                 return;
115         }
116         
117         if (dm) {
118                 float current_time = 0;
119                 unsigned int mvert_num = 0;
120
121                 CDDM_apply_vert_coords(dm, vertexCos);
122                 CDDM_calc_normals(dm);
123                 
124                 current_time = BKE_scene_frame_get(md->scene);
125                 
126                 if (G.debug_value > 0)
127                         printf("current_time %f, collmd->time_xnew %f\n", current_time, collmd->time_xnew);
128                 
129                 mvert_num = dm->getNumVerts(dm);
130                 
131                 if (current_time > collmd->time_xnew) {
132                         unsigned int i;
133
134                         /* check if mesh has changed */
135                         if (collmd->x && (mvert_num != collmd->mvert_num))
136                                 freeData((ModifierData *)collmd);
137
138                         if (collmd->time_xnew == -1000) { /* first time */
139
140                                 collmd->x = dm->dupVertArray(dm); /* frame start position */
141
142                                 for (i = 0; i < mvert_num; i++) {
143                                         /* we save global positions */
144                                         mul_m4_v3(ob->obmat, collmd->x[i].co);
145                                 }
146                                 
147                                 collmd->xnew = MEM_dupallocN(collmd->x); // frame end position
148                                 collmd->current_x = MEM_dupallocN(collmd->x); // inter-frame
149                                 collmd->current_xnew = MEM_dupallocN(collmd->x); // inter-frame
150                                 collmd->current_v = MEM_dupallocN(collmd->x); // inter-frame
151
152                                 collmd->mvert_num = mvert_num;
153                                 
154                                 DM_ensure_looptri(dm);
155
156                                 collmd->tri_num = dm->getNumLoopTri(dm);
157                                 {
158                                         const MLoop *mloop = dm->getLoopArray(dm);
159                                         const MLoopTri *looptri = dm->getLoopTriArray(dm);
160                                         MVertTri *tri = MEM_mallocN(sizeof(*tri) * collmd->tri_num, __func__);
161                                         DM_verttri_from_looptri(tri, mloop, looptri, collmd->tri_num);
162                                         collmd->tri = tri;
163                                 }
164
165                                 /* create bounding box hierarchy */
166                                 collmd->bvhtree = bvhtree_build_from_mvert(
167                                         collmd->x,
168                                         collmd->tri, collmd->tri_num,
169                                         ob->pd->pdef_sboft);
170
171                                 collmd->time_x = collmd->time_xnew = current_time;
172                         }
173                         else if (mvert_num == collmd->mvert_num) {
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), mvert_num * sizeof(MVert));
181
182                                 for (i = 0; i < mvert_num; 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, mvert_num * sizeof(MVert));
188                                 memcpy(collmd->current_x, collmd->x, mvert_num * sizeof(MVert));
189                                 
190                                 /* check if GUI setting has changed for bvh */
191                                 if (collmd->bvhtree) {
192                                         if (ob->pd->pdef_sboft != BLI_bvhtree_get_epsilon(collmd->bvhtree)) {
193                                                 BLI_bvhtree_free(collmd->bvhtree);
194                                                 collmd->bvhtree = bvhtree_build_from_mvert(
195                                                         collmd->current_x,
196                                                         collmd->tri, collmd->tri_num,
197                                                         ob->pd->pdef_sboft);
198                                         }
199                         
200                                 }
201                                 
202                                 /* happens on file load (ONLY when i decomment changes in readfile.c) */
203                                 if (!collmd->bvhtree) {
204                                         collmd->bvhtree = bvhtree_build_from_mvert(
205                                                 collmd->current_x,
206                                                 collmd->tri, collmd->tri_num,
207                                                 ob->pd->pdef_sboft);
208                                 }
209                                 else {
210                                         /* recalc static bounding boxes */
211                                         bvhtree_update_from_mvert(
212                                                 collmd->bvhtree,
213                                                 collmd->current_x, collmd->current_xnew,
214                                                 collmd->tri, collmd->tri_num,
215                                                 true);
216                                 }
217                                 
218                                 collmd->time_xnew = current_time;
219                         }
220                         else if (mvert_num != collmd->mvert_num) {
221                                 freeData((ModifierData *)collmd);
222                         }
223                         
224                 }
225                 else if (current_time < collmd->time_xnew) {
226                         freeData((ModifierData *)collmd);
227                 }
228                 else {
229                         if (mvert_num != collmd->mvert_num) {
230                                 freeData((ModifierData *)collmd);
231                         }
232                 }
233         }
234         
235         if (dm)
236                 dm->release(dm);
237 }
238
239
240 ModifierTypeInfo modifierType_Collision = {
241         /* name */              "Collision",
242         /* structName */        "CollisionModifierData",
243         /* structSize */        sizeof(CollisionModifierData),
244         /* type */              eModifierTypeType_OnlyDeform,
245         /* flags */             eModifierTypeFlag_AcceptsMesh |
246                                 eModifierTypeFlag_Single,
247
248         /* copyData */          NULL,
249         /* deformVerts */       deformVerts,
250         /* deformMatrices */    NULL,
251         /* deformVertsEM */     NULL,
252         /* deformMatricesEM */  NULL,
253         /* applyModifier */     NULL,
254         /* applyModifierEM */   NULL,
255         /* initData */          initData,
256         /* requiredDataMask */  NULL,
257         /* freeData */          freeData,
258         /* isDisabled */        NULL,
259         /* updateDepgraph */    NULL,
260         /* updateDepsgraph */   NULL,
261         /* dependsOnTime */     dependsOnTime,
262         /* dependsOnNormals */  NULL,
263         /* foreachObjectLink */ NULL,
264         /* foreachIDLink */     NULL,
265         /* foreachTexLink */    NULL,
266 };