793a49734755bb323a01b70527bc0d23b1390521
[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 Collision modifier code start
75 ***********************************/
76
77 /* step is limited from 0 (frame start position) to 1 (frame end position) */
78 void collision_move_object(CollisionModifierData *collmd, float step, float prevstep)
79 {
80         float tv[3] = {0,0,0};
81         unsigned int i = 0;
82         
83         for ( i = 0; i < collmd->numverts; i++ )
84         {
85                 VECSUB(tv, collmd->xnew[i].co, collmd->x[i].co);
86                 VECADDS(collmd->current_x[i].co, collmd->x[i].co, tv, prevstep);
87                 VECADDS(collmd->current_xnew[i].co, collmd->x[i].co, tv, step);
88                 VECSUB(collmd->current_v[i].co, collmd->current_xnew[i].co, collmd->current_x[i].co);
89         }
90 }
91
92 /* build bounding volume hierarchy from mverts (see kdop.c for whole BVH code) */
93 BVH *bvh_build_from_mvert (MFace *mfaces, unsigned int numfaces, MVert *x, unsigned int numverts, float epsilon)
94 {
95         BVH *bvh=NULL;
96         
97         bvh = MEM_callocN(sizeof(BVH), "BVH");
98         if (bvh == NULL) 
99         {
100                 printf("bvh: Out of memory.\n");
101                 return NULL;
102         }
103         
104         // in the moment, return zero if no faces there
105         if(!numfaces)
106                 return NULL;
107         
108         bvh->flags = 0;
109         bvh->leaf_tree = NULL;
110         bvh->leaf_root = NULL;
111         bvh->tree = NULL;
112
113         bvh->epsilon = epsilon;
114         bvh->numfaces = numfaces;
115         bvh->mfaces = mfaces;
116         
117         // we have no faces, we save seperate points
118         if(!mfaces)
119         {
120                 bvh->numfaces = numverts;
121         }
122
123         bvh->numverts = numverts;
124         bvh->current_x = MEM_dupallocN(x);      
125         bvh->current_xold = MEM_dupallocN(x);   
126         
127         bvh_build(bvh);
128         
129         return bvh;
130 }
131
132 void bvh_update_from_mvert(BVH * bvh, MVert *x, unsigned int numverts, MVert *xnew, int moving)
133 {
134         if(!bvh)
135                 return;
136         
137         if(numverts!=bvh->numverts)
138                 return;
139         
140         if(x)
141                 memcpy(bvh->current_xold, x, sizeof(MVert) * numverts);
142         
143         if(xnew)
144                 memcpy(bvh->current_x, xnew, sizeof(MVert) * numverts);
145         
146         bvh_update(bvh, moving);
147 }
148
149 /***********************************
150 Collision modifier code end
151 ***********************************/
152
153 /**
154  * gsl_poly_solve_cubic -
155  *
156  * copied from SOLVE_CUBIC.C --> GSL
157  */
158
159 /* DG: debug hint! don't forget that all functions were "fabs", "sinf", etc before */
160 #define mySWAP(a,b) { float tmp = b ; b = a ; a = tmp ; }
161
162 int gsl_poly_solve_cubic (float a, float b, float c, float *x0, float *x1, float *x2)
163 {
164         float q = (a * a - 3 * b);
165         float r = (2 * a * a * a - 9 * a * b + 27 * c);
166
167         float Q = q / 9;
168         float R = r / 54;
169
170         float Q3 = Q * Q * Q;
171         float R2 = R * R;
172
173         float CR2 = 729 * r * r;
174         float CQ3 = 2916 * q * q * q;
175
176         if (R == 0 && Q == 0)
177         {
178                 *x0 = - a / 3 ;
179                 *x1 = - a / 3 ;
180                 *x2 = - a / 3 ;
181                 return 3 ;
182         }
183         else if (CR2 == CQ3) 
184         {
185           /* this test is actually R2 == Q3, written in a form suitable
186                 for exact computation with integers */
187
188           /* Due to finite precision some float roots may be missed, and
189                 considered to be a pair of complex roots z = x +/- epsilon i
190                 close to the real axis. */
191
192                 float sqrtQ = sqrt (Q);
193
194                 if (R > 0)
195                 {
196                         *x0 = -2 * sqrtQ  - a / 3;
197                         *x1 = sqrtQ - a / 3;
198                         *x2 = sqrtQ - a / 3;
199                 }
200                 else
201                 {
202                         *x0 = - sqrtQ  - a / 3;
203                         *x1 = - sqrtQ - a / 3;
204                         *x2 = 2 * sqrtQ - a / 3;
205                 }
206                 return 3 ;
207         }
208         else if (CR2 < CQ3) /* equivalent to R2 < Q3 */
209         {
210                 float sqrtQ = sqrt (Q);
211                 float sqrtQ3 = sqrtQ * sqrtQ * sqrtQ;
212                 float theta = acos (R / sqrtQ3);
213                 float norm = -2 * sqrtQ;
214                 *x0 = norm * cos (theta / 3) - a / 3;
215                 *x1 = norm * cos ((theta + 2.0 * M_PI) / 3) - a / 3;
216                 *x2 = norm * cos ((theta - 2.0 * M_PI) / 3) - a / 3;
217       
218                 /* Sort *x0, *x1, *x2 into increasing order */
219
220                 if (*x0 > *x1)
221                         mySWAP(*x0, *x1) ;
222       
223                 if (*x1 > *x2)
224                 {
225                         mySWAP(*x1, *x2) ;
226           
227                         if (*x0 > *x1)
228                                 mySWAP(*x0, *x1) ;
229                 }
230       
231                 return 3;
232         }
233         else
234         {
235                 float sgnR = (R >= 0 ? 1 : -1);
236                 float A = -sgnR * pow (ABS (R) + sqrt (R2 - Q3), 1.0/3.0);
237                 float B = Q / A ;
238                 *x0 = A + B - a / 3;
239                 return 1;
240         }
241 }
242
243
244 /**
245  * gsl_poly_solve_quadratic
246  *
247  * copied from GSL
248  */
249 int gsl_poly_solve_quadratic (float a, float b, float c,  float *x0, float *x1)
250 {
251         float disc = b * b - 4 * a * c;
252
253         if (disc > 0)
254         {
255                 if (b == 0)
256                 {
257                         float r = ABS (0.5 * sqrt (disc) / a);
258                         *x0 = -r;
259                         *x1 =  r;
260                 }
261                 else
262                 {
263                         float sgnb = (b > 0 ? 1 : -1);
264                         float temp = -0.5 * (b + sgnb * sqrt (disc));
265                         float r1 = temp / a ;
266                         float r2 = c / temp ;
267
268                         if (r1 < r2) 
269                         {
270                                 *x0 = r1 ;
271                                 *x1 = r2 ;
272                         } 
273                         else 
274                         {
275                                 *x0 = r2 ;
276                                 *x1 = r1 ;
277                         }
278                 }
279                 return 2;
280         }
281         else if (disc == 0) 
282         {
283                 *x0 = -0.5 * b / a ;
284                 *x1 = -0.5 * b / a ;
285                 return 2 ;
286         }
287         else
288         {
289                 return 0;
290         }
291 }
292
293
294
295 /*
296  * See Bridson et al. "Robust Treatment of Collision, Contact and Friction for Cloth Animation"
297  *     page 4, left column
298  */
299
300 int cloth_get_collision_time(float a[3], float b[3], float c[3], float d[3], float e[3], float f[3], float solution[3]) 
301 {
302         int num_sols = 0;
303         
304         float g = -a[2] * c[1] * e[0] + a[1] * c[2] * e[0] +
305                         a[2] * c[0] * e[1] - a[0] * c[2] * e[1] -
306                         a[1] * c[0] * e[2] + a[0] * c[1] * e[2];
307
308         float h = -b[2] * c[1] * e[0] + b[1] * c[2] * e[0] - a[2] * d[1] * e[0] +
309                         a[1] * d[2] * e[0] + b[2] * c[0] * e[1] - b[0] * c[2] * e[1] +
310                         a[2] * d[0] * e[1] - a[0] * d[2] * e[1] - b[1] * c[0] * e[2] +
311                         b[0] * c[1] * e[2] - a[1] * d[0] * e[2] + a[0] * d[1] * e[2] -
312                         a[2] * c[1] * f[0] + a[1] * c[2] * f[0] + a[2] * c[0] * f[1] -
313                         a[0] * c[2] * f[1] - a[1] * c[0] * f[2] + a[0] * c[1] * f[2];
314
315         float i = -b[2] * d[1] * e[0] + b[1] * d[2] * e[0] +
316                         b[2] * d[0] * e[1] - b[0] * d[2] * e[1] -
317                         b[1] * d[0] * e[2] + b[0] * d[1] * e[2] -
318                         b[2] * c[1] * f[0] + b[1] * c[2] * f[0] -
319                         a[2] * d[1] * f[0] + a[1] * d[2] * f[0] +
320                         b[2] * c[0] * f[1] - b[0] * c[2] * f[1] + 
321                         a[2] * d[0] * f[1] - a[0] * d[2] * f[1] -
322                         b[1] * c[0] * f[2] + b[0] * c[1] * f[2] -
323                         a[1] * d[0] * f[2] + a[0] * d[1] * f[2];
324
325         float j = -b[2] * d[1] * f[0] + b[1] * d[2] * f[0] +
326                         b[2] * d[0] * f[1] - b[0] * d[2] * f[1] -
327                         b[1] * d[0] * f[2] + b[0] * d[1] * f[2];
328
329         // Solve cubic equation to determine times t1, t2, t3, when the collision will occur.
330         if(ABS(j) > ALMOST_ZERO)
331         {
332                 i /= j;
333                 h /= j;
334                 g /= j;
335                 
336                 num_sols = gsl_poly_solve_cubic(i, h, g, &solution[0], &solution[1], &solution[2]);
337         }
338         else if(ABS(i) > ALMOST_ZERO)
339         {       
340                 num_sols = gsl_poly_solve_quadratic(i, h, g, &solution[0], &solution[1]);
341                 solution[2] = -1.0;
342         }
343         else if(ABS(h) > ALMOST_ZERO)
344         {
345                 solution[0] = -g / h;
346                 solution[1] = solution[2] = -1.0;
347                 num_sols = 1;
348         }
349         else if(ABS(g) > ALMOST_ZERO)
350         {
351                 solution[0] = 0;
352                 solution[1] = solution[2] = -1.0;
353                 num_sols = 1;
354         }
355
356         // Discard negative solutions
357         if ((num_sols >= 1) && (solution[0] < 0)) 
358         {
359                 --num_sols;
360                 solution[0] = solution[num_sols];
361         }
362         if ((num_sols >= 2) && (solution[1] < 0)) 
363         {
364                 --num_sols;
365                 solution[1] = solution[num_sols];
366         }
367         if ((num_sols == 3) && (solution[2] < 0)) 
368         {
369                 --num_sols;
370         }
371
372         // Sort
373         if (num_sols == 2) 
374         {
375                 if (solution[0] > solution[1]) 
376                 {
377                         double tmp = solution[0];
378                         solution[0] = solution[1];
379                         solution[1] = tmp;
380                 }
381         }
382         else if (num_sols == 3) 
383         {
384
385                 // Bubblesort
386                 if (solution[0] > solution[1]) {
387                         double tmp = solution[0]; solution[0] = solution[1]; solution[1] = tmp;
388                 }
389                 if (solution[1] > solution[2]) {
390                         double tmp = solution[1]; solution[1] = solution[2]; solution[2] = tmp;
391                 }
392                 if (solution[0] > solution[1]) {
393                         double tmp = solution[0]; solution[0] = solution[1]; solution[1] = tmp;
394                 }
395         }
396
397         return num_sols;
398 }
399
400 // w3 is not perfect
401 void cloth_compute_barycentric (float pv[3], float p1[3], float p2[3], float p3[3], float *w1, float *w2, float *w3)
402 {
403         double  tempV1[3], tempV2[3], tempV4[3];
404         double  a,b,c,d,e,f;
405
406         VECSUB (tempV1, p1, p3);        
407         VECSUB (tempV2, p2, p3);        
408         VECSUB (tempV4, pv, p3);        
409         
410         a = INPR (tempV1, tempV1);      
411         b = INPR (tempV1, tempV2);      
412         c = INPR (tempV2, tempV2);      
413         e = INPR (tempV1, tempV4);      
414         f = INPR (tempV2, tempV4);      
415         
416         d = (a * c - b * b);
417         
418         if (ABS(d) < ALMOST_ZERO) {
419                 *w1 = *w2 = *w3 = 1.0 / 3.0;
420                 return;
421         }
422         
423         w1[0] = (float)((e * c - b * f) / d);
424         
425         if(w1[0] < 0)
426                 w1[0] = 0;
427         
428         w2[0] = (float)((f - b * (double)w1[0]) / c);
429         
430         if(w2[0] < 0)
431                 w2[0] = 0;
432         
433         w3[0] = 1.0f - w1[0] - w2[0];
434 }
435
436 DO_INLINE void interpolateOnTriangle(float to[3], float v1[3], float v2[3], float v3[3], double w1, double w2, double w3) 
437 {
438         to[0] = to[1] = to[2] = 0;
439         VECADDMUL(to, v1, w1);
440         VECADDMUL(to, v2, w2);
441         VECADDMUL(to, v3, w3);
442 }
443
444 int cloth_collision_response_static(ClothModifierData *clmd, CollisionModifierData *collmd)
445 {
446         int result = 0;
447         LinkNode *search = NULL;
448         CollPair *collpair = NULL;
449         Cloth *cloth1;
450         float w1, w2, w3, u1, u2, u3;
451         float v1[3], v2[3], relativeVelocity[3];
452         float magrelVel;
453         
454         cloth1 = clmd->clothObject;
455
456         search = clmd->coll_parms->collision_list;
457         
458         while(search)
459         {
460                 collpair = search->link;
461                 
462                 // compute barycentric coordinates for both collision points
463                 cloth_compute_barycentric(collpair->pa,
464                                           cloth1->verts[collpair->ap1].txold,
465        cloth1->verts[collpair->ap2].txold,
466        cloth1->verts[collpair->ap3].txold, 
467        &w1, &w2, &w3);
468                 
469                 // was: txold
470                 cloth_compute_barycentric(collpair->pb,
471                                           collmd->current_x[collpair->bp1].co,
472                                         collmd->current_x[collpair->bp2].co,
473                                         collmd->current_x[collpair->bp3].co,
474                                         &u1, &u2, &u3);
475         
476                 // Calculate relative "velocity".
477                 interpolateOnTriangle(v1, cloth1->verts[collpair->ap1].tv, cloth1->verts[collpair->ap2].tv, cloth1->verts[collpair->ap3].tv, w1, w2, w3);
478                 
479                 interpolateOnTriangle(v2, collmd->current_v[collpair->bp1].co, collmd->current_v[collpair->bp2].co, collmd->current_v[collpair->bp3].co, u1, u2, u3);
480                 
481                 VECSUB(relativeVelocity, v1, v2);
482                         
483                 // Calculate the normal component of the relative velocity (actually only the magnitude - the direction is stored in 'normal').
484                 magrelVel = INPR(relativeVelocity, collpair->normal);
485                 
486                 // printf("magrelVel: %f\n", magrelVel);
487                                 
488                 // Calculate masses of points.
489                 // TODO
490                 
491                 // If v_n_mag < 0 the edges are approaching each other.
492                 if(magrelVel < -ALMOST_ZERO) 
493                 {
494                         // Calculate Impulse magnitude to stop all motion in normal direction.
495                         float tangential[3], magtangent;
496                         double impulse = 0.0;
497                         float vrel_t_pre[3];
498                         float vrel_t[3], temp[3];
499                         
500                         // calculate tangential velocity
501                         VECCOPY(temp, collpair->normal);
502                         VecMulf(temp, magrelVel);
503                         VECSUB(vrel_t_pre, relativeVelocity, temp);
504                         
505                         VECCOPY(vrel_t, vrel_t_pre);
506                         
507                         VecMulf(vrel_t, MAX2(1.0 - (clmd->coll_parms->friction * magrelVel / sqrt(INPR(vrel_t_pre,vrel_t_pre))), 0.0));
508                         
509                         VECSUB(tangential, vrel_t_pre, vrel_t);
510                         VecMulf(tangential, 0.5);
511                         
512                         magtangent = INPR(tangential, tangential);
513                         
514                         // Apply friction impulse.
515                         if (magtangent > ALMOST_ZERO) 
516                         {
517                                 impulse = magtangent / ( 1.0 + w1*w1 + w2*w2 + w3*w3);
518                                 magtangent = sqrt(magtangent);
519                                 VECADDMUL(cloth1->verts[collpair->ap1].impulse, tangential, w1 * impulse/magtangent);
520                                 VECADDMUL(cloth1->verts[collpair->ap2].impulse, tangential, w2 * impulse/magtangent);
521                                 VECADDMUL(cloth1->verts[collpair->ap3].impulse, tangential, w3 * impulse/magtangent);
522                         }
523                         
524
525                         impulse = -2.0f * magrelVel / ( 1.0 + w1*w1 + w2*w2 + w3*w3);
526                         
527                         VECADDMUL(cloth1->verts[collpair->ap1].impulse, collpair->normal, w1 * impulse); 
528                         cloth1->verts[collpair->ap1].impulse_count++;
529                         
530                         VECADDMUL(cloth1->verts[collpair->ap2].impulse, collpair->normal, w2 * impulse); 
531                         cloth1->verts[collpair->ap2].impulse_count++;
532                         
533                         VECADDMUL(cloth1->verts[collpair->ap3].impulse, collpair->normal, w3 * impulse); 
534                         cloth1->verts[collpair->ap3].impulse_count++;
535                         
536                         result = 1;
537                 }
538                 
539                 search = search->next;
540         }
541         
542                 
543         return result;
544 }
545
546 int cloth_collision_response_moving_tris(ClothModifierData *clmd, ClothModifierData *coll_clmd)
547 {
548         return 1;
549 }
550
551
552 int cloth_collision_response_moving_edges(ClothModifierData *clmd, ClothModifierData *coll_clmd)
553 {
554         return 1;
555 }
556
557 void cloth_collision_static(ModifierData *md1, ModifierData *md2, CollisionTree *tree1, CollisionTree *tree2)
558 {
559         ClothModifierData *clmd = (ClothModifierData *)md1;
560         CollisionModifierData *collmd = (CollisionModifierData *)md2;
561         CollPair *collpair = NULL;
562         Cloth *cloth1=NULL;
563         MFace *face1=NULL, *face2=NULL;
564         ClothVertex *verts1=NULL;
565         double distance = 0;
566         float epsilon = clmd->coll_parms->epsilon;
567         unsigned int i = 0;
568
569         for(i = 0; i < 4; i++)
570         {
571                 collpair = (CollPair *)MEM_callocN(sizeof(CollPair), "cloth coll pair");                
572                 
573                 cloth1 = clmd->clothObject;
574                 
575                 verts1 = cloth1->verts;
576         
577                 face1 = &(cloth1->mfaces[tree1->tri_index]);
578                 face2 = &(collmd->mfaces[tree2->tri_index]);
579                 
580                 // check all possible pairs of triangles
581                 if(i == 0)
582                 {
583                         collpair->ap1 = face1->v1;
584                         collpair->ap2 = face1->v2;
585                         collpair->ap3 = face1->v3;
586                         
587                         collpair->bp1 = face2->v1;
588                         collpair->bp2 = face2->v2;
589                         collpair->bp3 = face2->v3;
590                         
591                 }
592                 
593                 if(i == 1)
594                 {
595                         if(face1->v4)
596                         {
597                                 collpair->ap1 = face1->v3;
598                                 collpair->ap2 = face1->v4;
599                                 collpair->ap3 = face1->v1;
600                                 
601                                 collpair->bp1 = face2->v1;
602                                 collpair->bp2 = face2->v2;
603                                 collpair->bp3 = face2->v3;
604                         }
605                         else
606                                 i++;
607                 }
608                 
609                 if(i == 2)
610                 {
611                         if(face2->v4)
612                         {
613                                 collpair->ap1 = face1->v1;
614                                 collpair->ap2 = face1->v2;
615                                 collpair->ap3 = face1->v3;
616                                 
617                                 collpair->bp1 = face2->v3;
618                                 collpair->bp2 = face2->v4;
619                                 collpair->bp3 = face2->v1;
620                         }
621                         else
622                                 i+=2;
623                 }
624                 
625                 if(i == 3)
626                 {
627                         if((face1->v4)&&(face2->v4))
628                         {
629                                 collpair->ap1 = face1->v3;
630                                 collpair->ap2 = face1->v4;
631                                 collpair->ap3 = face1->v1;
632                                 
633                                 collpair->bp1 = face2->v3;
634                                 collpair->bp2 = face2->v4;
635                                 collpair->bp3 = face2->v1;
636                         }
637                         else
638                                 i++;
639                 }
640                 
641                 // calc SIPcode (?)
642                 
643                 if(i < 4)
644                 {
645                         // calc distance + normal       
646 #if WITH_BULLET == 1
647                         distance = plNearestPoints(
648                                         verts1[collpair->ap1].txold, verts1[collpair->ap2].txold, verts1[collpair->ap3].txold, collmd->current_x[collpair->bp1].co, collmd->current_x[collpair->bp2].co, collmd->current_x[collpair->bp3].co, collpair->pa,collpair->pb,collpair->vector);
649 #else
650                         // just be sure that we don't add anything
651                         distance = 2.0 * (epsilon + ALMOST_ZERO);
652 #endif  
653                         if (distance <= (epsilon + ALMOST_ZERO))
654                         {
655                                 // printf("dist: %f\n", (float)distance);
656                                 
657                                 // collpair->face1 = tree1->tri_index;
658                                 // collpair->face2 = tree2->tri_index;
659                                 
660                                 VECCOPY(collpair->normal, collpair->vector);
661                                 Normalize(collpair->normal);
662                                 
663                                 collpair->distance = distance;
664                                 BLI_linklist_prepend(&clmd->coll_parms->collision_list, collpair);
665                                 
666                         }
667                         else
668                         {
669                                 MEM_freeN(collpair);
670                         }
671                 }
672                 else
673                 {
674                         MEM_freeN(collpair);
675                 }
676         }
677 }
678
679 int cloth_are_edges_adjacent(ClothModifierData *clmd, ClothModifierData *coll_clmd, EdgeCollPair *edgecollpair)
680 {
681         Cloth *cloth1 = NULL, *cloth2 = NULL;
682         ClothVertex *verts1 = NULL, *verts2 = NULL;
683         float temp[3];
684          
685         cloth1 = clmd->clothObject;
686         cloth2 = coll_clmd->clothObject;
687         
688         verts1 = cloth1->verts;
689         verts2 = cloth2->verts;
690         
691         VECSUB(temp, verts1[edgecollpair->p11].xold, verts2[edgecollpair->p21].xold);
692         if(ABS(INPR(temp, temp)) < ALMOST_ZERO)
693                 return 1;
694         
695         VECSUB(temp, verts1[edgecollpair->p11].xold, verts2[edgecollpair->p22].xold);
696         if(ABS(INPR(temp, temp)) < ALMOST_ZERO)
697                 return 1;
698         
699         VECSUB(temp, verts1[edgecollpair->p12].xold, verts2[edgecollpair->p21].xold);
700         if(ABS(INPR(temp, temp)) < ALMOST_ZERO)
701                 return 1;
702         
703         VECSUB(temp, verts1[edgecollpair->p12].xold, verts2[edgecollpair->p22].xold);
704         if(ABS(INPR(temp, temp)) < ALMOST_ZERO)
705                 return 1;
706                 
707         return 0;
708 }
709
710 void cloth_collision_moving_edges(ClothModifierData *clmd, ClothModifierData *coll_clmd, CollisionTree *tree1, CollisionTree *tree2)
711 {
712         EdgeCollPair edgecollpair;
713         Cloth *cloth1=NULL, *cloth2=NULL;
714         MFace *face1=NULL, *face2=NULL;
715         ClothVertex *verts1=NULL, *verts2=NULL;
716         unsigned int i = 0, j = 0, k = 0;
717         int numsolutions = 0;
718         float a[3], b[3], c[3], d[3], e[3], f[3], solution[3];
719         
720         cloth1 = clmd->clothObject;
721         cloth2 = coll_clmd->clothObject;
722         
723         verts1 = cloth1->verts;
724         verts2 = cloth2->verts;
725
726         face1 = &(cloth1->mfaces[tree1->tri_index]);
727         face2 = &(cloth2->mfaces[tree2->tri_index]);
728         
729         for( i = 0; i < 5; i++)
730         {
731                 if(i == 0) 
732                 {
733                         edgecollpair.p11 = face1->v1;
734                         edgecollpair.p12 = face1->v2;
735                 }
736                 else if(i == 1) 
737                 {
738                         edgecollpair.p11 = face1->v2;
739                         edgecollpair.p12 = face1->v3;
740                 }
741                 else if(i == 2) 
742                 {
743                         if(face1->v4) 
744                         {
745                                 edgecollpair.p11 = face1->v3;
746                                 edgecollpair.p12 = face1->v4;
747                         }
748                         else 
749                         {
750                                 edgecollpair.p11 = face1->v3;
751                                 edgecollpair.p12 = face1->v1;
752                                 i+=5; // get out of here after this edge pair is handled
753                         }
754                 }
755                 else if(i == 3) 
756                 {
757                         if(face1->v4) 
758                         {
759                                 edgecollpair.p11 = face1->v4;
760                                 edgecollpair.p12 = face1->v1;
761                         }       
762                         else
763                                 continue;
764                 }
765                 else
766                 {
767                         edgecollpair.p11 = face1->v3;
768                         edgecollpair.p12 = face1->v1;
769                 }
770
771                 
772                 for( j = 0; j < 5; j++)
773                 {
774                         if(j == 0)
775                         {
776                                 edgecollpair.p21 = face2->v1;
777                                 edgecollpair.p22 = face2->v2;
778                         }
779                         else if(j == 1)
780                         {
781                                 edgecollpair.p21 = face2->v2;
782                                 edgecollpair.p22 = face2->v3;
783                         }
784                         else if(j == 2)
785                         {
786                                 if(face2->v4) 
787                                 {
788                                         edgecollpair.p21 = face2->v3;
789                                         edgecollpair.p22 = face2->v4;
790                                 }
791                                 else 
792                                 {
793                                         edgecollpair.p21 = face2->v3;
794                                         edgecollpair.p22 = face2->v1;
795                                 }
796                         }
797                         else if(j == 3)
798                         {
799                                 if(face2->v4) 
800                                 {
801                                         edgecollpair.p21 = face2->v4;
802                                         edgecollpair.p22 = face2->v1;
803                                 }
804                                 else
805                                         continue;
806                         }
807                         else
808                         {
809                                 edgecollpair.p21 = face2->v3;
810                                 edgecollpair.p22 = face2->v1;
811                         }
812                         
813                         
814                         if(!cloth_are_edges_adjacent(clmd, coll_clmd, &edgecollpair))
815                         {
816                                 VECSUB(a, verts1[edgecollpair.p12].xold, verts1[edgecollpair.p11].xold);
817                                 VECSUB(b, verts1[edgecollpair.p12].v, verts1[edgecollpair.p11].v);
818                                 VECSUB(c, verts1[edgecollpair.p21].xold, verts1[edgecollpair.p11].xold);
819                                 VECSUB(d, verts1[edgecollpair.p21].v, verts1[edgecollpair.p11].v);
820                                 VECSUB(e, verts2[edgecollpair.p22].xold, verts1[edgecollpair.p11].xold);
821                                 VECSUB(f, verts2[edgecollpair.p22].v, verts1[edgecollpair.p11].v);
822                                 
823                                 numsolutions = cloth_get_collision_time(a, b, c, d, e, f, solution);
824                                 
825                                 for (k = 0; k < numsolutions; k++) 
826                                 {                                                               
827                                         if ((solution[k] >= 0.0) && (solution[k] <= 1.0)) 
828                                         {
829                                                 //float out_collisionTime = solution[k];
830                                                 
831                                                 // TODO: check for collisions 
832                                                 
833                                                 // TODO: put into (edge) collision list
834                                                 
835                                                 // printf("Moving edge found!\n");
836                                         }
837                                 }
838                         }
839                 }
840         }               
841 }
842
843 void cloth_collision_moving_tris(ClothModifierData *clmd, ClothModifierData *coll_clmd, CollisionTree *tree1, CollisionTree *tree2)
844 {
845         CollPair collpair;
846         Cloth *cloth1=NULL, *cloth2=NULL;
847         MFace *face1=NULL, *face2=NULL;
848         ClothVertex *verts1=NULL, *verts2=NULL;
849         unsigned int i = 0, j = 0, k = 0;
850         int numsolutions = 0;
851         float a[3], b[3], c[3], d[3], e[3], f[3], solution[3];
852
853         for(i = 0; i < 2; i++)
854         {               
855                 cloth1 = clmd->clothObject;
856                 cloth2 = coll_clmd->clothObject;
857                 
858                 verts1 = cloth1->verts;
859                 verts2 = cloth2->verts;
860         
861                 face1 = &(cloth1->mfaces[tree1->tri_index]);
862                 face2 = &(cloth2->mfaces[tree2->tri_index]);
863                 
864                 // check all possible pairs of triangles
865                 if(i == 0)
866                 {
867                         collpair.ap1 = face1->v1;
868                         collpair.ap2 = face1->v2;
869                         collpair.ap3 = face1->v3;
870                         
871                         collpair.pointsb[0] = face2->v1;
872                         collpair.pointsb[1] = face2->v2;
873                         collpair.pointsb[2] = face2->v3;
874                         collpair.pointsb[3] = face2->v4;
875                 }
876                 
877                 if(i == 1)
878                 {
879                         if(face1->v4)
880                         {
881                                 collpair.ap1 = face1->v3;
882                                 collpair.ap2 = face1->v4;
883                                 collpair.ap3 = face1->v1;
884                                 
885                                 collpair.pointsb[0] = face2->v1;
886                                 collpair.pointsb[1] = face2->v2;
887                                 collpair.pointsb[2] = face2->v3;
888                                 collpair.pointsb[3] = face2->v4;
889                         }
890                         else
891                                 i++;
892                 }
893                 
894                 // calc SIPcode (?)
895                 
896                 if(i < 2)
897                 {
898                         VECSUB(a, verts1[collpair.ap2].xold, verts1[collpair.ap1].xold);
899                         VECSUB(b, verts1[collpair.ap2].v, verts1[collpair.ap1].v);
900                         VECSUB(c, verts1[collpair.ap3].xold, verts1[collpair.ap1].xold);
901                         VECSUB(d, verts1[collpair.ap3].v, verts1[collpair.ap1].v);
902                                 
903                         for(j = 0; j < 4; j++)
904                         {                                       
905                                 if((j==3) && !(face2->v4))
906                                         break;
907                                 
908                                 VECSUB(e, verts2[collpair.pointsb[j]].xold, verts1[collpair.ap1].xold);
909                                 VECSUB(f, verts2[collpair.pointsb[j]].v, verts1[collpair.ap1].v);
910                                 
911                                 numsolutions = cloth_get_collision_time(a, b, c, d, e, f, solution);
912                                 
913                                 for (k = 0; k < numsolutions; k++) 
914                                 {                                                               
915                                         if ((solution[k] >= 0.0) && (solution[k] <= 1.0)) 
916                                         {
917                                                 //float out_collisionTime = solution[k];
918                                                 
919                                                 // TODO: check for collisions 
920                                                 
921                                                 // TODO: put into (point-face) collision list
922                                                 
923                                                 // printf("Moving found!\n");
924                                                 
925                                         }
926                                 }
927                                 
928                                 // TODO: check borders for collisions
929                         }
930                         
931                 }
932         }
933 }
934
935 void cloth_collision_moving(ClothModifierData *clmd, ClothModifierData *coll_clmd, CollisionTree *tree1, CollisionTree *tree2)
936 {
937         // TODO: check for adjacent
938         cloth_collision_moving_edges(clmd, coll_clmd, tree1, tree2);
939         
940         cloth_collision_moving_tris(clmd, coll_clmd, tree1, tree2);
941         cloth_collision_moving_tris(coll_clmd, clmd, tree2, tree1);
942 }
943
944 void cloth_collision_self_static(ModifierData *md1, ModifierData *md2, CollisionTree *tree1, CollisionTree *tree2)
945 {
946 /*
947         ClothModifierData *clmd = (ClothModifierData *)md1;
948         CollisionModifierData *collmd = (CollisionModifierData *)md2;
949         CollPair *collpair = NULL;
950         Cloth *cloth1=NULL;
951         MFace *face1=NULL, *face2=NULL;
952         ClothVertex *verts1=NULL;
953         double distance = 0;
954         float epsilon = clmd->coll_parms->epsilon;
955         unsigned int i = 0;
956 */
957         
958 }
959
960 #if 0
961 /* aye this belongs to arith.c */
962 static void Vec3PlusStVec(float *v, float s, float *v1)
963 {
964         v[0] += s*v1[0];
965         v[1] += s*v1[1];
966         v[2] += s*v1[2];
967 }
968 #endif
969
970 // cloth - object collisions
971 int cloth_bvh_objcollision(ClothModifierData * clmd, float step, float dt)
972 {
973         Base *base=NULL;
974         CollisionModifierData *collmd=NULL;
975         Cloth *cloth=NULL;
976         Object *coll_ob=NULL;
977         BVH *cloth_bvh=NULL;
978         long i=0, j = 0, numfaces = 0, numverts = 0;
979         unsigned int result = 0, ic = 0, rounds = 0; // result counts applied collisions; ic is for debug output; 
980         ClothVertex *verts = NULL;
981         float tnull[3] = {0,0,0};
982         int ret = 0;
983         ClothModifierData *tclmd;
984         int collisions = 0, count = 0;
985
986         if ((clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_COLLOBJ) || !(((Cloth *)clmd->clothObject)->tree))
987         {
988                 return 0;
989         }
990         
991         cloth = clmd->clothObject;
992         verts = cloth->verts;
993         cloth_bvh = (BVH *) cloth->tree;
994         numfaces = clmd->clothObject->numfaces;
995         numverts = clmd->clothObject->numverts;
996         
997         ////////////////////////////////////////////////////////////
998         // static collisions
999         ////////////////////////////////////////////////////////////
1000
1001         // update cloth bvh
1002         bvh_update_from_cloth(clmd, 0); // 0 means STATIC, 1 means MOVING (see later in this function)
1003         
1004         do
1005         {
1006                 result = 0;
1007                 ic = 0;
1008                 clmd->coll_parms->collision_list = NULL; 
1009                 
1010                 // check all collision objects
1011                 for (base = G.scene->base.first; base; base = base->next)
1012                 {
1013                         coll_ob = base->object;
1014                         collmd = (CollisionModifierData *) modifiers_findByType (coll_ob, eModifierType_Collision);
1015                         
1016                         if (!collmd)
1017                                 continue;
1018                         
1019                         tclmd = (ClothModifierData *) modifiers_findByType (coll_ob, eModifierType_Cloth);
1020                         if(tclmd == clmd)
1021                                 continue;
1022                         
1023                         if (collmd->tree) 
1024                         {
1025                                 BVH *coll_bvh = collmd->tree;
1026                                 
1027                                 collision_move_object(collmd, step + dt, step);
1028                                         
1029                                 bvh_traverse((ModifierData *)clmd, (ModifierData *)collmd, cloth_bvh->root, coll_bvh->root, step, cloth_collision_static, 0);
1030                         }
1031                         else
1032                                 printf ("cloth_bvh_objcollision: found a collision object with clothObject or collData NULL.\n");
1033                 
1034                 
1035                         // process all collisions (calculate impulses, TODO: also repulses if distance too short)
1036                         result = 1;
1037                         for(j = 0; j < 5; j++) // 5 is just a value that ensures convergence
1038                         {
1039                                 result = 0;
1040                                 
1041                                 if (collmd->tree) 
1042                                         result += cloth_collision_response_static(clmd, collmd);
1043                                 
1044                                 // apply impulses in parallel
1045                                 ic=0;
1046                                 for(i = 0; i < numverts; i++)
1047                                 {
1048                                         // calculate "velocities" (just xnew = xold + v; no dt in v)
1049                                         if(verts[i].impulse_count)
1050                                         {
1051                                                 VECADDMUL(verts[i].tv, verts[i].impulse, 1.0f / verts[i].impulse_count);
1052                                                 VECCOPY(verts[i].impulse, tnull);
1053                                                 verts[i].impulse_count = 0;
1054                                         
1055                                                 ic++;
1056                                                 ret++;
1057                                         }
1058                                 }
1059                         }
1060                         
1061                         // free collision list
1062                         if(clmd->coll_parms->collision_list)
1063                         {
1064                                 LinkNode *search = clmd->coll_parms->collision_list;
1065                                 while(search)
1066                                 {
1067                                         CollPair *coll_pair = search->link;
1068                                                         
1069                                         MEM_freeN(coll_pair);
1070                                         search = search->next;
1071                                 }
1072                                 BLI_linklist_free(clmd->coll_parms->collision_list,NULL);
1073                         
1074                                 clmd->coll_parms->collision_list = NULL;
1075                         }
1076                 }
1077                 rounds++;
1078         
1079                 ////////////////////////////////////////////////////////////
1080                 // update positions
1081                 // this is needed for bvh_calc_DOP_hull_moving() [kdop.c]
1082                 ////////////////////////////////////////////////////////////
1083                 
1084                 // verts come from clmd
1085                 for(i = 0; i < numverts; i++)
1086                 {
1087                         if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL) 
1088                         {                       
1089                                 if(verts [i].flags & CLOTH_VERT_FLAG_PINNED)
1090                                 {
1091                                         continue;
1092                                 }
1093                         }
1094                         
1095                         VECADD(verts[i].tx, verts[i].txold, verts[i].tv);
1096                 }
1097                 ////////////////////////////////////////////////////////////
1098                 
1099                 
1100                 ////////////////////////////////////////////////////////////
1101                 // Test on *simple* selfcollisions
1102                 ////////////////////////////////////////////////////////////
1103                 if (clmd->coll_parms->flags & CLOTH_COLLSETTINGS_FLAG_SELF)     
1104                 {
1105                         collisions = 1;
1106                         verts = cloth->verts; // needed for openMP
1107                         
1108                         for(count = 0; count < clmd->coll_parms->self_loop_count; count++)
1109                         {       
1110                                 if(collisions)
1111                                 {
1112                                         collisions = 0;
1113                 #pragma omp parallel for private(i,j, collisions) shared(verts, ret)
1114                                         for(i = 0; i < cloth->numverts; i++)
1115                                         {
1116                                                 for(j = i + 1; j < cloth->numverts; j++)
1117                                                 {
1118                                                         float temp[3];
1119                                                         float length = 0;
1120                                                         float mindistance = clmd->coll_parms->selfepsilon*(cloth->verts[i].avg_spring_len + cloth->verts[j].avg_spring_len);
1121                                                                 
1122                                                         if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL)
1123                                                         {                       
1124                                                                 if((cloth->verts [i].flags & CLOTH_VERT_FLAG_PINNED)
1125                                                                 && (cloth->verts [j].flags & CLOTH_VERT_FLAG_PINNED))
1126                                                                 {
1127                                                                         continue;
1128                                                                 }
1129                                                         }
1130                                                         
1131                                                         VECSUB(temp, verts[i].tx, verts[j].tx);
1132                                                         
1133                                                         if ((ABS(temp[0]) > mindistance) || (ABS(temp[1]) > mindistance) || (ABS(temp[2]) > mindistance)) continue;
1134                                                                 
1135                                                         // check for adjacent points
1136                                                         if(BLI_edgehash_haskey (cloth->edgehash, i, j ))
1137                                                         {
1138                                                                 continue;
1139                                                         }
1140                                                                 
1141                                                         length = Normalize(temp);
1142                                                                 
1143                                                         if(length < mindistance)
1144                                                         {
1145                                                                 float correction = mindistance - length;
1146                                                                         
1147                                                                 if(cloth->verts [i].flags & CLOTH_VERT_FLAG_PINNED)
1148                                                                 {
1149                                                                         VecMulf(temp, -correction);
1150                                                                         VECADD(verts[j].tx, verts[j].tx, temp);
1151                                                                 }
1152                                                                 else if(cloth->verts [j].flags & CLOTH_VERT_FLAG_PINNED)
1153                                                                 {
1154                                                                         VecMulf(temp, correction);
1155                                                                         VECADD(verts[i].tx, verts[i].tx, temp);
1156                                                                 }
1157                                                                 else
1158                                                                 {
1159                                                                         VecMulf(temp, -correction*0.5);
1160                                                                         VECADD(verts[j].tx, verts[j].tx, temp);
1161                                                                         
1162                                                                         VECSUB(verts[i].tx, verts[i].tx, temp); 
1163                                                                 }
1164                                                                 
1165                                                                 collisions = 1;
1166                                                                 
1167                                                                 if(!ret)
1168                                                                 {       
1169                         #pragma omp critical
1170                         {
1171                                                                         ret = 1;
1172                         }
1173                                                                 }
1174                                                         }
1175                                                 }
1176                                         }
1177                                 }
1178                         }
1179                         ////////////////////////////////////////////////////////////
1180                         
1181                         ////////////////////////////////////////////////////////////
1182                         // SELFCOLLISIONS: update velocities
1183                         ////////////////////////////////////////////////////////////
1184                         if(ret)
1185                         {
1186                                 for(i = 0; i < cloth->numverts; i++)
1187                                 {
1188                                         if(!(cloth->verts [i].flags & CLOTH_VERT_FLAG_PINNED))
1189                                                 VECSUB(verts[i].tv, verts[i].tx, verts[i].txold);
1190                                 }
1191                         }
1192                         ////////////////////////////////////////////////////////////
1193                 }
1194         }
1195         while(result && (clmd->coll_parms->loop_count>rounds));
1196         
1197         ////////////////////////////////////////////////////////////
1198         // moving collisions
1199         //
1200         // response code is just missing itm 
1201         ////////////////////////////////////////////////////////////
1202
1203         /*
1204         // update cloth bvh
1205         bvh_update_from_cloth(clmd, 1);  // 0 means STATIC, 1 means MOVING 
1206         
1207         // update moving bvh for collision object once
1208         for (base = G.scene->base.first; base; base = base->next)
1209         {
1210                 
1211         coll_ob = base->object;
1212         coll_clmd = (ClothModifierData *) modifiers_findByType (coll_ob, eModifierType_Cloth);
1213         if (!coll_clmd)
1214         continue;
1215                 
1216         if(!coll_clmd->clothObject)
1217         continue;
1218                 
1219                                 // if collision object go on
1220         if (coll_clmd->clothObject && coll_clmd->clothObject->tree) 
1221         {
1222         BVH *coll_bvh = coll_clmd->clothObject->tree;
1223                         
1224         bvh_update_from_cloth(coll_clmd, 1);  // 0 means STATIC, 1 means MOVING         
1225 }
1226 }
1227         
1228         
1229         do
1230         {
1231         result = 0;
1232         ic = 0;
1233         clmd->coll_parms->collision_list = NULL; 
1234                 
1235                 // check all collision objects
1236         for (base = G.scene->base.first; base; base = base->next)
1237         {
1238         coll_ob = base->object;
1239         coll_clmd = (ClothModifierData *) modifiers_findByType (coll_ob, eModifierType_Cloth);
1240                         
1241         if (!coll_clmd)
1242         continue;
1243                         
1244                         // if collision object go on
1245         if (coll_clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_COLLOBJ)
1246         {
1247         if (coll_clmd->clothObject && coll_clmd->clothObject->tree) 
1248         {
1249         BVH *coll_bvh = coll_clmd->clothObject->tree;
1250                                         
1251         bvh_traverse(clmd, coll_clmd, cloth_bvh->root, coll_bvh->root, step, cloth_collision_moving, 0);
1252 }
1253         else
1254         printf ("cloth_bvh_objcollision: found a collision object with clothObject or collData NULL.\n");
1255 }
1256 }
1257                 
1258                 // process all collisions (calculate impulses, TODO: also repulses if distance too short)
1259         result = 1;
1260         for(j = 0; j < 10; j++) // 10 is just a value that ensures convergence
1261         {
1262         result = 0;
1263                                 
1264                         // handle all collision objects
1265         for (base = G.scene->base.first; base; base = base->next)
1266         {
1267                         
1268         coll_ob = base->object;
1269         coll_clmd = (ClothModifierData *) modifiers_findByType (coll_ob, eModifierType_Cloth);
1270                                                 
1271         if (!coll_clmd)
1272         continue;
1273                                 
1274                                 // if collision object go on
1275         if (coll_clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_COLLOBJ)
1276         {
1277         if (coll_clmd->clothObject) 
1278         result += cloth_collision_response_moving_tris(clmd, coll_clmd);
1279         else
1280         printf ("cloth_bvh_objcollision: found a collision object with clothObject or collData NULL.\n");
1281 }
1282 }
1283                                                 
1284                         // apply impulses in parallel
1285         ic=0;
1286         for(i = 0; i < numverts; i++)
1287         {
1288                                 // calculate "velocities" (just xnew = xold + v; no dt in v)
1289         if(verts[i].impulse_count)
1290         {
1291         VECADDMUL(verts[i].tv, verts[i].impulse, 1.0f / verts[i].impulse_count);
1292         VECCOPY(verts[i].impulse, tnull);
1293         verts[i].impulse_count = 0;
1294                                                         
1295         ic++;
1296         ret++;
1297 }
1298 }
1299 }
1300                 
1301                 
1302                 // verts come from clmd
1303         for(i = 0; i < numverts; i++)
1304         {
1305         VECADD(verts[i].tx, verts[i].txold, verts[i].tv);
1306 }
1307                 
1308                 // update cloth bvh
1309         bvh_update_from_cloth(clmd, 1);  // 0 means STATIC, 1 means MOVING 
1310                 
1311                 
1312                 // free collision list
1313         if(clmd->coll_parms->collision_list)
1314         {
1315         LinkNode *search = clmd->coll_parms->collision_list;
1316         while(search)
1317         {
1318         CollPair *coll_pair = search->link;
1319                                                         
1320         MEM_freeN(coll_pair);
1321         search = search->next;
1322 }
1323         BLI_linklist_free(clmd->coll_parms->collision_list,NULL);
1324                         
1325         clmd->coll_parms->collision_list = NULL;
1326 }
1327                 
1328                 // printf("ic: %d\n", ic);
1329         rounds++;
1330 }
1331         while(result && (CLOTH_MAX_THRESHOLD>rounds));
1332         
1333         ////////////////////////////////////////////////////////////
1334         // update positions + velocities
1335         ////////////////////////////////////////////////////////////
1336         
1337         // verts come from clmd
1338         for(i = 0; i < numverts; i++)
1339         {
1340         VECADD(verts[i].tx, verts[i].txold, verts[i].tv);
1341 }
1342         ////////////////////////////////////////////////////////////
1343         */
1344         
1345         return MIN2(ret, 1);
1346 }