Initial checkin of cloth files + changes
[blender.git] / source / blender / blenkernel / intern / collision.c
1 /*  collision.c      
2
3 *
4 * ***** BEGIN GPL/BL DUAL 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. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License.  See http://www.blender.org/BL/ for information
12 * about this.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 *
23 * The Original Code is Copyright (C) Blender Foundation
24 * All rights reserved.
25 *
26 * The Original Code is: all of this file.
27 *
28 * Contributor(s): none yet.
29 *
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
31 */
32
33 #include <math.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "MEM_guardedalloc.h"
37 /* types */
38 #include "DNA_curve_types.h"
39 #include "DNA_object_types.h"
40 #include "DNA_object_force.h"
41 #include "DNA_cloth_types.h"    
42 #include "DNA_key_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meshdata_types.h"
45 #include "DNA_lattice_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_modifier_types.h"
48 #include "BLI_blenlib.h"
49 #include "BLI_arithb.h"
50 #include "BLI_edgehash.h"
51 #include "BLI_linklist.h"
52 #include "BKE_curve.h"
53 #include "BKE_deform.h"
54 #include "BKE_DerivedMesh.h"
55 #include "BKE_cdderivedmesh.h"
56 #include "BKE_displist.h"
57 #include "BKE_effect.h"
58 #include "BKE_global.h"
59 #include "BKE_mesh.h"
60 #include "BKE_object.h"
61 #include "BKE_cloth.h"
62 #include "BKE_modifier.h"
63 #include "BKE_utildefines.h"
64 #include "BKE_DerivedMesh.h"
65 #include "DNA_screen_types.h"
66 #include "BSE_headerbuttons.h"
67 #include "BIF_screen.h"
68 #include "BIF_space.h"
69 #include "mydevice.h"
70
71 #include "Bullet-C-Api.h"
72
73
74 #define DERANDOMIZE 1
75
76
77 enum TRIANGLE_MARK 
78
79         TM_MV = 1,
80         TM_ME = 2,
81         TM_V1 = 4,
82         TM_V2 = 8,
83         TM_V3 = 16,
84         TM_E1 = 32,
85         TM_E2 = 64,
86         TM_E3 = 128 
87 };
88
89 DO_INLINE int hasTriangleMark(unsigned char mark, unsigned char bit) { return mark & bit; }
90 DO_INLINE void setTriangleMark(unsigned char *mark, unsigned char bit) { mark[0] |= bit; }
91 DO_INLINE void clearTriangleMark(unsigned char *mark, unsigned char bit) { mark[0] &= ~bit; }
92
93
94 void generateTriangleMarks() 
95 {
96         /*
97         unsigned int firstEdge = 0;
98         
99         // 1. Initialization
100         memset(m_triangleMarks, 0, sizeof(unsigned char) * m_triangleCount);
101
102         // 2. The Marking Process
103         
104         // 2.1 Randomly mark triangles for covering vertices.
105         for (unsigned int v = 0; v < m_vertexCount; ++v) 
106         {
107                 if (vertexCover(v) == 0) 
108                 {
109
110                         // Randomly select an edge whose first triangle we're going to flag. 
111
112 #ifndef DERANDOMIZE
113                         firstEdge = (unsigned int)((float)(random() & 0x7FFFFFFF) /
114                                         (float)(0x80000000) *
115                                         (float)(m_vertices[v].getEdgeCount()));
116 #endif
117                         for (unsigned int ofs = 0; ofs < m_vertices[v].getEdgeCount(); ++ofs) 
118                         {
119                                 unsigned int edgeIdx = (firstEdge + ofs) % m_vertices[v].getEdgeCount();
120                                 if (m_edges[m_vertices[v].getEdge(edgeIdx)].getTriangleCount())
121                                         setTriangleMark(m_triangleMarks[m_edges[m_vertices[v].getEdge(edgeIdx)].getTriangle(0)], TM_MV);
122                         }
123                 }
124         }
125         */
126         /* If the Cloth is malformed (vertices without adjacent triangles) there might still be uncovered vertices. (Bad luck.) */
127         /*
128         // 2.2 Randomly mark triangles for covering edges.
129         for (unsigned int e = 0; e < m_edgeCount; ++e) 
130         {
131                 if (m_edges[e].getTriangleCount() && (edgeCover(e) == 0)) 
132                 {
133 #ifndef DERANDOMIZE
134                         setTriangleMark(m_triangleMarks[m_edges[e].getTriangle(static_cast<UINT32>((float)(random() & 0x7FFFFFFF) /
135                                         (float)(0x80000000) *
136                                         (float)(m_edges[e].getTriangleCount())))], TM_ME);
137 #else
138                         setTriangleMark(m_triangleMarks[m_edges[e].getTriangle(0)], TM_ME);
139 #endif
140                 }
141         }
142
143         
144         // 3. The Unmarking Process
145         for (unsigned int t = 0; (t < m_triangleCount); ++t) 
146         {
147                 bool overCoveredVertices = true;
148                 bool overCoveredEdges = true;
149                 for (unsigned char i = 0; (i < 3) && (overCoveredVertices || overCoveredEdges); ++i) 
150                 {
151
152                         if (vertexCover(m_triangles[t].getVertex(i)) == 1)
153                                 overCoveredVertices = false;
154                         if (edgeCover(m_triangles[t].getEdge(i)) == 1)
155                                 overCoveredEdges = false;
156
157                         assert(vertexCover(m_triangles[t].getVertex(i)) > 0);
158                         assert(edgeCover(m_triangles[t].getEdge(i)) > 0);
159                 }
160                 if (overCoveredVertices)
161                         clearTriangleMark(m_triangleMarks[t], TM_MV);
162                 if (overCoveredEdges)
163                         clearTriangleMark(m_triangleMarks[t], TM_ME);
164         }
165
166
167         // 4. The Bit Masking Process
168         vector<bool> vertexAssigned(m_vertexCount, false);
169         vector<bool> edgeAssigned(m_edgeCount, false);
170         for (unsigned int t = 0; (t < m_triangleCount); ++t) 
171         {
172                 for (unsigned char i = 0; i < 3; ++i) 
173                 {
174                         if (!vertexAssigned[m_triangles[t].getVertex(i)]) 
175                         {
176                                 vertexAssigned[m_triangles[t].getVertex(i)] = true;
177                                 setTriangleMark(m_triangleMarks[t], 1 << (2 + i));
178                         }
179                         if (!edgeAssigned[m_triangles[t].getEdge(i)]) 
180                         {
181                                 edgeAssigned[m_triangles[t].getEdge(i)] = true;
182                                 setTriangleMark(m_triangleMarks[t], 1 << (5 + i));
183                         }
184                 }
185         }
186         */
187 }
188
189
190 void bvh_compute_barycentric (float pv[3], float p1[3], float p2[3], float p3[3], double *w1, double *w2, double *w3)
191 {
192         float   tempV1[3], tempV2[3], tempV4[3];
193         double  a,b,c,e,f;
194
195         VECSUB (tempV1, p1, p3);        /* x1 - x3 */
196         VECSUB (tempV2, p2, p3);        /* x2 - x3 */
197         VECSUB (tempV4, pv, p3);        /* pv - x3 */
198         
199         a = INPR (tempV1, tempV1);      
200         b = INPR (tempV1, tempV2);      
201         c = INPR (tempV2, tempV2);      
202         e = INPR (tempV1, tempV4);      
203         f = INPR (tempV2, tempV4);      
204         
205         
206         w1[0] = (e * c - b * f) / (a * c - b * b);
207         w2[0] = (f - b * w1[0]) / c;
208         w3[0] = 1.0 - w1[0] - w2[0];
209 }
210
211 DO_INLINE void interpolateOnTriangle(float to[3], float v1[3], float v2[3], float v3[3], double w1, double w2, double w3) 
212 {
213         to[0] = to[1] = to[2] = 0;
214         VECADDMUL(to, v1, w1);
215         VECADDMUL(to, v2, w2);
216         VECADDMUL(to, v3, w3);
217 }
218
219 DO_INLINE void calculateFrictionImpulse(float to[3], float vrel[3], float normal[3], double normalVelocity,
220         double frictionConstant, double delta_V_n) 
221 {
222         float vrel_t_pre[3];
223         float vrel_t[3];
224         VECSUBS(vrel_t_pre, vrel, normal, normalVelocity);
225         VECCOPY(vrel_t, vrel_t_pre);
226         VecMulf(vrel_t, MAX2(1.0f - frictionConstant * delta_V_n / INPR(vrel_t_pre,vrel_t_pre), 0.0f));
227         VECSUB(to, vrel_t_pre, vrel_t);
228         VecMulf(to, 1.0f / 2.0f);
229 }
230
231 int collision_static(ClothModifierData *clmd, ClothModifierData *coll_clmd, LinkNode **collision_list)
232 {
233         unsigned int i = 0, numverts=0;
234         int result = 0;
235         LinkNode *search = NULL;
236         CollPair *collpair = NULL;
237         Cloth *cloth1, *cloth2;
238         MFace *face1, *face2;
239         double w1, w2, w3, u1, u2, u3;
240         float v1[3], v2[3], relativeVelocity[3];
241         float magrelVel;
242         
243         cloth1 = clmd->clothObject;
244         cloth2 = coll_clmd->clothObject;
245         
246         numverts = clmd->clothObject->numverts;
247         
248         /*
249         for(i = 0; i < LIST_LENGTH; i++)
250         {
251                 // calc SIP-code
252                 //  TODO for later: calculateSipCode()
253         
254                 // calc distance (?)
255                 
256                 // calc impulse
257                 
258                 // apply impulse
259         }
260         */
261         
262         for(i = 0; i < numverts; i++)
263         {
264                 search = collision_list[i];
265                 
266                 while(search)
267                 {
268                         collpair = search->link;
269                         
270                         face1 = &(cloth1->mfaces[collpair->face1]);
271                         face2 = &(cloth2->mfaces[collpair->face2]);
272                         
273                         // compute barycentric coordinates for both collision points
274                         if(!collpair->quadA)
275                                 bvh_compute_barycentric(collpair->p1,
276                                                         cloth1->verts[face1->v1].txold,
277                                                         cloth1->verts[face1->v2].txold,
278                                                         cloth1->verts[face1->v3].txold, 
279                                                         &w1, &w2, &w3);
280                         else
281                                 bvh_compute_barycentric(collpair->p1,
282                                                         cloth1->verts[face1->v4].txold,
283                                                         cloth1->verts[face1->v1].txold,
284                                                         cloth1->verts[face1->v3].txold, 
285                                                         &w1, &w2, &w3);
286                         
287                         if(!collpair->quadB)
288                                 bvh_compute_barycentric(collpair->p2,
289                                                         cloth2->verts[face2->v1].txold,
290                                                         cloth2->verts[face2->v2].txold,
291                                                         cloth2->verts[face2->v3].txold, 
292                                                         &u1, &u2, &u3);
293                         else
294                                 bvh_compute_barycentric(collpair->p2,
295                                                         cloth2->verts[face2->v4].txold,
296                                                         cloth2->verts[face2->v1].txold,
297                                                         cloth2->verts[face2->v3].txold, 
298                                                         &u1, &u2, &u3);
299                         
300                         // Calculate relative velocity.
301                         if(!collpair->quadA)
302                                 interpolateOnTriangle(v1, cloth1->verts[face1->v1].v, cloth1->verts[face1->v2].v, cloth1->verts[face1->v3].v, w1, w2, w3);
303                         else
304                                 interpolateOnTriangle(v1, cloth1->verts[face1->v4].v, cloth1->verts[face1->v1].v, cloth1->verts[face1->v3].v, w1, w2, w3);
305                         
306                         if(!collpair->quadB)
307                                 interpolateOnTriangle(v2, cloth2->verts[face2->v1].v, cloth2->verts[face2->v2].v, cloth2->verts[face2->v3].v, u1, u2, u3);
308                         else
309                                 interpolateOnTriangle(v2, cloth2->verts[face2->v4].v, cloth2->verts[face2->v1].v, cloth2->verts[face2->v3].v, u1, u2, u3);
310                         
311                         VECSUB(relativeVelocity, v1, v2);
312                                 
313                         // Calculate the normal component of the relative velocity (actually only the magnitude - the direction is stored in 'normal').
314                         magrelVel = INPR(relativeVelocity, collpair->normal);
315                                         
316                         // Calculate masses of points.
317                         
318                         // printf("relativeVelocity -> x: %f, y: %f, z: %f\n", relativeVelocity[0], relativeVelocity[1],relativeVelocity[2]); 
319                         
320                         // If v_n_mag > 0 the edges are approaching each other.
321                         if(magrelVel > ALMOST_ZERO)
322                         {
323                                 // Calculate Impulse magnitude to stop all motion in normal direction.
324                                 // const double I_mag = v_n_mag / (1/m1 + 1/m2);
325                                 float magnitude_i = magrelVel / 2.0f; // TODO implement masses
326                                 float tangential[3], magtangent;
327                                 
328                                 calculateFrictionImpulse(tangential, relativeVelocity, collpair->normal, magrelVel, clmd->coll_parms.friction*0.01, magrelVel);
329                                 
330                                 magtangent = INPR(tangential, tangential);
331                                 
332                                 // Apply friction impulse.
333                                 if (magtangent > ALMOST_ZERO) 
334                                 {
335                                         /*
336                                         printf("friction applied: %f\n", magtangent);
337                                         // TODO check original code 
338                                         VECSUB(cloth1->verts[face1->v1].tv, cloth1->verts[face1->v1].tv,tangential);
339                                         VECSUB(cloth1->verts[face1->v1].tv, cloth1->verts[face1->v2].tv,tangential);
340                                         VECSUB(cloth1->verts[face1->v1].tv, cloth1->verts[face1->v3].tv,tangential);
341                                         VECSUB(cloth1->verts[face1->v1].tv, cloth1->verts[face1->v4].tv,tangential);
342                                         */
343                                 }
344                         
345                                 // printf("magnitude_i: %f\n", magnitude_i); // negative before collision in my case
346                                 
347                                 // Apply the impulse and increase impulse counters.
348                                 /*
349                                 VECADDMUL(cloth1->verts[face1->v1].tv,collpair->normal, -magnitude_i);
350                                 VECADDMUL(cloth1->verts[face1->v2].tv,collpair->normal, -magnitude_i);
351                                 VECADDMUL(cloth1->verts[face1->v3].tv,collpair->normal, -magnitude_i);
352                                 VECADDMUL(cloth1->verts[face1->v4].tv,collpair->normal, -magnitude_i);
353                                 */
354                                 
355                                 // my try
356                                 magtangent = INPR(cloth1->verts[face1->v1].tv, collpair->normal);
357                                 VECADDMUL(cloth1->verts[face1->v1].tv, collpair->normal, -magtangent); 
358                                 
359                                 magtangent = INPR(cloth1->verts[face1->v2].tv, collpair->normal);
360                                 VECADDMUL(cloth1->verts[face1->v2].tv, collpair->normal, -magtangent); 
361                                 
362                                 magtangent = INPR(cloth1->verts[face1->v3].tv, collpair->normal);
363                                 VECADDMUL(cloth1->verts[face1->v3].tv, collpair->normal, -magtangent); 
364                                 
365                                 magtangent = INPR(cloth1->verts[face1->v4].tv, collpair->normal);
366                                 VECADDMUL(cloth1->verts[face1->v4].tv, collpair->normal, -magtangent); 
367                                 
368                                 result = 1;
369                                 
370                         }
371                         
372                         search = search->next;
373                 }
374         }
375                 
376         return result;
377 }
378
379 // return distance between two triangles using bullet engine
380 double implicit_tri_check_coherence (ClothModifierData *clmd, ClothModifierData *coll_clmd, unsigned int tri_index1, unsigned int tri_index2, float pa[3], float pb[3], float normal[3], int quadA, int quadB)
381 {
382         MFace *face1=NULL, *face2=NULL;
383         float  a[3][3];
384         float  b[3][3];
385         double distance=0, tempdistance=0;
386         Cloth *cloth1=NULL, *cloth2=NULL;
387         float tpa[3], tpb[3], tnormal[3];
388         unsigned int indexA=0, indexB=0, indexC=0, indexD=0, indexE=0, indexF=0;
389         int i = 0;
390         
391         cloth1 = clmd->clothObject;
392         cloth2 = coll_clmd->clothObject;
393         
394         face1 = &(cloth1->mfaces[tri_index1]);
395         face2 = &(cloth2->mfaces[tri_index2]);
396         
397         // face a1 + face b1
398         VECCOPY(a[0], cloth1->verts[face1->v1].txold);
399         VECCOPY(a[1], cloth1->verts[face1->v2].txold);
400         VECCOPY(a[2], cloth1->verts[face1->v3].txold);
401         
402         
403         VECCOPY(b[0], cloth2->verts[face2->v1].txold);
404         VECCOPY(b[1], cloth2->verts[face2->v2].txold);
405         VECCOPY(b[2], cloth2->verts[face2->v3].txold);
406
407         distance = plNearestPoints(a,b,pa,pb,normal);
408         
409         quadA = quadB = 0;
410         
411         for(i = 0; i < 3; i++)
412         {
413                 if(i == 0)
414                 {
415                         indexA = face1->v4;
416                         indexB = face1->v1;
417                         indexC = face1->v3;
418                         
419                         indexD = face2->v1;
420                         indexE = face2->v2;
421                         indexF = face2->v3;
422                 }
423                 else if(i == 1)
424                 {
425                         indexA = face1->v4;
426                         indexB = face1->v1;
427                         indexC = face1->v3;
428                 
429                         indexD = face2->v4;
430                         indexE = face2->v1;
431                         indexF = face2->v3;
432                 }
433                 else if(i == 2)
434                 {
435                         indexA = face1->v1;
436                         indexB = face1->v2;
437                         indexC = face1->v3;
438                 
439                         indexD = face2->v4;
440                         indexE = face2->v1;
441                         indexF = face2->v3;
442                 }
443                 
444                 // face a2 + face b1
445                 VECCOPY(a[0], cloth1->verts[indexA].txold);
446                 VECCOPY(a[1], cloth1->verts[indexB].txold);
447                 VECCOPY(a[2], cloth1->verts[indexC].txold);
448                 
449                 
450                 VECCOPY(b[0], cloth2->verts[indexD].txold);
451                 VECCOPY(b[1], cloth2->verts[indexE].txold);
452                 VECCOPY(b[2], cloth2->verts[indexF].txold);
453                 
454                 tempdistance = plNearestPoints(a,b,tpa,tpb,tnormal);
455                 
456                 if(tempdistance < distance)
457                 {
458                         VECCOPY(pa, tpa);
459                         VECCOPY(pb, tpb);
460                         VECCOPY(normal, tnormal);
461                         distance = tempdistance;
462                         
463                         if(i == 0)
464                         {
465                                 quadA = 1; quadB = 0;
466                         }
467                         else if(i == 1)
468                         {
469                                 quadA = quadB = 1;
470                         }
471                         else if(i == 2)
472                         {
473                                 quadA = 0; quadB = 1;
474                         }
475                 }
476         }
477         return distance;
478 }
479
480 void bvh_collision_response(ClothModifierData *clmd, ClothModifierData *coll_clmd, Tree * tree1, Tree * tree2)
481 {
482         CollPair *collpair = NULL;
483         LinkNode **linknode;
484         double distance = 0;
485         float epsilon = clmd->coll_parms.epsilon;
486         
487         collpair = (CollPair *)MEM_callocN(sizeof(CollPair), "cloth coll pair");
488         linknode = clmd->coll_parms.temp;
489         
490         // calc SIPcode (?)
491         
492         // calc distance + normal       
493         distance = implicit_tri_check_coherence(clmd, coll_clmd, tree1->tri_index, tree2->tri_index, collpair->p1, collpair->p2, collpair->vector, collpair->quadA, collpair->quadB);
494         
495         if (ABS(distance) <= (epsilon + ALMOST_ZERO))
496         {
497                 // printf("distance: %f, epsilon: %f\n", (float)distance, epsilon + ALMOST_ZERO);
498                         
499                 collpair->face1 = tree1->tri_index;
500                 collpair->face2 = tree2->tri_index;
501                 
502                 VECCOPY(collpair->normal, collpair->vector);
503                 Normalize(collpair->normal);
504                 
505                 // printf("normal x: %f, y: %f, z: %f\n", collpair->normal[0], collpair->normal[1], collpair->normal[2]);
506                 
507                 collpair->distance = distance;
508                 
509                 BLI_linklist_append(&linknode[tree1->tri_index], collpair);     
510         }
511         else
512         {
513                 MEM_freeN(collpair);
514         }
515 }
516
517
518 int cloth_bvh_objcollision(ClothModifierData * clmd, float step, CM_COLLISION_RESPONSE collision_response, float dt)
519 {
520         Base *base=NULL;
521         ClothModifierData *coll_clmd=NULL;
522         Cloth *cloth=NULL;
523         Object *coll_ob=NULL;
524         BVH *cloth_bvh=NULL;
525         unsigned int i=0, numverts=0;
526         int result = 0;
527
528         if ((clmd->sim_parms.flags & CSIMSETT_FLAG_COLLOBJ) || !(((Cloth *)clmd->clothObject)->tree))
529         {
530                 return 0;
531         }
532         cloth = clmd->clothObject;
533         cloth_bvh = (BVH *) cloth->tree;
534         numverts = clmd->clothObject->numverts;
535
536         ////////////////////////////////////////////////////////////
537         // static collisions
538         ////////////////////////////////////////////////////////////
539
540         // update cloth bvh
541         bvh_update_static(clmd, cloth_bvh);
542
543         // search all objects for collision object
544         for (base = G.scene->base.first; base; base = base->next)
545         {
546
547                 coll_ob = base->object;
548                 coll_clmd = (ClothModifierData *) modifiers_findByType (coll_ob, eModifierType_Cloth);
549                 if (!coll_clmd)
550                         continue;
551
552                 // if collision object go on
553                 if (coll_clmd->sim_parms.flags & CSIMSETT_FLAG_COLLOBJ)
554                 {
555                         if (coll_clmd->clothObject && coll_clmd->clothObject->tree) 
556                         {                       
557                                 unsigned int coll_numverts = coll_clmd->clothObject->numverts;
558                                 Cloth *coll_cloth = coll_clmd->clothObject;
559
560                                 LinkNode **collision_list = MEM_callocN (sizeof(LinkNode *)*numverts, "collision_list");
561                                 BVH *coll_bvh = coll_clmd->clothObject->tree;
562
563                                 if(collision_list)
564                                 {                                       
565                                         // memset(collision_list, 0, sizeof(LinkNode *)*numverts); 
566                                         
567                                         for(i = 0; i < numverts; i++)
568                                         {
569                                                 collision_list[i] = NULL;
570                                         }
571
572                                         clmd->coll_parms.temp = collision_list;
573                                         
574                                         // update position of collision object
575                                         for(i = 0; i < coll_numverts; i++)
576                                         {
577                                                 VECCOPY(coll_cloth->verts[i].txold, coll_cloth->verts[i].tx);
578
579                                                 VECADDS(coll_cloth->verts[i].tx, coll_cloth->verts[i].xold, coll_cloth->verts[i].v, step);
580                                                 
581                                                 VECSUB(coll_cloth->verts[i].tv, coll_cloth->verts[i].tx, coll_cloth->verts[i].txold);
582                                         }
583                                                         
584                                         // update BVH of collision object
585                                         bvh_update_static(coll_clmd, coll_bvh);
586
587                                         bvh_traverse(clmd, coll_clmd, cloth_bvh->root, coll_bvh->root, step, collision_response);
588                                         
589                                         result += collision_static(clmd, coll_clmd, collision_list);
590                                         
591                                         // calculate velocities
592                                         
593                                         // free temporary list 
594                                         for(i = 0; i < numverts; i++)
595                                         {
596                                                 LinkNode *search = collision_list[i];
597                                                 while(search)
598                                                 {
599                                                         LinkNode *next= search->next;
600                                                         CollPair *collpair = search->link;
601                                                         
602                                                         if(collpair)
603                                                                 MEM_freeN(collpair);    
604
605                                                         search = next;
606                                                 }
607
608                                                 BLI_linklist_free(collision_list[i],NULL); 
609                                         }
610                                         if(collision_list)
611                                                 MEM_freeN(collision_list);
612
613                                         clmd->coll_parms.temp = NULL;
614                                 }
615                                 
616
617                         }
618                         else
619                                 printf ("cloth_bvh_objcollision: found a collision object with clothObject or collData NULL.\n");
620                 }
621         }
622
623         ////////////////////////////////////////////////////////////
624         // update positions + velocities
625         ////////////////////////////////////////////////////////////
626
627         // TODO 
628
629
630         ////////////////////////////////////////////////////////////
631         // moving collisions
632         ////////////////////////////////////////////////////////////
633
634         // TODO 
635         // bvh_update_moving(clmd, clmd->clothObject->tree);
636
637         return MIN2(result, 1);
638 }