Cloth: fixed completely useless/wrong friction force; changed some initial settings
[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 magtangent;
496                         double impulse = 0.0;
497                         float vrel_t_pre[3];
498                         float 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                         magtangent = INPR(vrel_t_pre,vrel_t_pre) - MIN2(clmd->coll_parms->friction * 0.01 * magrelVel,INPR(vrel_t_pre,vrel_t_pre));
506                         
507                         // Apply friction impulse.
508                         if (magtangent > ALMOST_ZERO) 
509                         {       
510                                 Normalize(vrel_t_pre);
511
512                                 impulse = -2.0 * magtangent / ( 1.0 + w1*w1 + w2*w2 + w3*w3);
513                                 VECADDMUL(cloth1->verts[collpair->ap1].impulse, vrel_t_pre, w1 * impulse);
514                                 VECADDMUL(cloth1->verts[collpair->ap2].impulse, vrel_t_pre, w2 * impulse);
515                                 VECADDMUL(cloth1->verts[collpair->ap3].impulse, vrel_t_pre, w3 * impulse);
516                         }
517                         
518
519                         impulse = -2.0f * magrelVel / ( 1.0 + w1*w1 + w2*w2 + w3*w3);
520                         
521                         VECADDMUL(cloth1->verts[collpair->ap1].impulse, collpair->normal, w1 * impulse); 
522                         cloth1->verts[collpair->ap1].impulse_count++;
523                         
524                         VECADDMUL(cloth1->verts[collpair->ap2].impulse, collpair->normal, w2 * impulse); 
525                         cloth1->verts[collpair->ap2].impulse_count++;
526                         
527                         VECADDMUL(cloth1->verts[collpair->ap3].impulse, collpair->normal, w3 * impulse); 
528                         cloth1->verts[collpair->ap3].impulse_count++;
529                         
530                         result = 1;
531                 }
532                 
533                 search = search->next;
534         }
535         
536                 
537         return result;
538 }
539
540 int cloth_collision_response_moving_tris(ClothModifierData *clmd, ClothModifierData *coll_clmd)
541 {
542         return 1;
543 }
544
545
546 int cloth_collision_response_moving_edges(ClothModifierData *clmd, ClothModifierData *coll_clmd)
547 {
548         return 1;
549 }
550
551 void cloth_collision_static(ModifierData *md1, ModifierData *md2, CollisionTree *tree1, CollisionTree *tree2)
552 {
553         ClothModifierData *clmd = (ClothModifierData *)md1;
554         CollisionModifierData *collmd = (CollisionModifierData *)md2;
555         CollPair *collpair = NULL;
556         Cloth *cloth1=NULL;
557         MFace *face1=NULL, *face2=NULL;
558         ClothVertex *verts1=NULL;
559         double distance = 0;
560         float epsilon = clmd->coll_parms->epsilon;
561         unsigned int i = 0;
562
563         for(i = 0; i < 4; i++)
564         {
565                 collpair = (CollPair *)MEM_callocN(sizeof(CollPair), "cloth coll pair");                
566                 
567                 cloth1 = clmd->clothObject;
568                 
569                 verts1 = cloth1->verts;
570         
571                 face1 = &(cloth1->mfaces[tree1->tri_index]);
572                 face2 = &(collmd->mfaces[tree2->tri_index]);
573                 
574                 // check all possible pairs of triangles
575                 if(i == 0)
576                 {
577                         collpair->ap1 = face1->v1;
578                         collpair->ap2 = face1->v2;
579                         collpair->ap3 = face1->v3;
580                         
581                         collpair->bp1 = face2->v1;
582                         collpair->bp2 = face2->v2;
583                         collpair->bp3 = face2->v3;
584                         
585                 }
586                 
587                 if(i == 1)
588                 {
589                         if(face1->v4)
590                         {
591                                 collpair->ap1 = face1->v3;
592                                 collpair->ap2 = face1->v4;
593                                 collpair->ap3 = face1->v1;
594                                 
595                                 collpair->bp1 = face2->v1;
596                                 collpair->bp2 = face2->v2;
597                                 collpair->bp3 = face2->v3;
598                         }
599                         else
600                                 i++;
601                 }
602                 
603                 if(i == 2)
604                 {
605                         if(face2->v4)
606                         {
607                                 collpair->ap1 = face1->v1;
608                                 collpair->ap2 = face1->v2;
609                                 collpair->ap3 = face1->v3;
610                                 
611                                 collpair->bp1 = face2->v3;
612                                 collpair->bp2 = face2->v4;
613                                 collpair->bp3 = face2->v1;
614                         }
615                         else
616                                 i+=2;
617                 }
618                 
619                 if(i == 3)
620                 {
621                         if((face1->v4)&&(face2->v4))
622                         {
623                                 collpair->ap1 = face1->v3;
624                                 collpair->ap2 = face1->v4;
625                                 collpair->ap3 = face1->v1;
626                                 
627                                 collpair->bp1 = face2->v3;
628                                 collpair->bp2 = face2->v4;
629                                 collpair->bp3 = face2->v1;
630                         }
631                         else
632                                 i++;
633                 }
634                 
635                 // calc SIPcode (?)
636                 
637                 if(i < 4)
638                 {
639                         // calc distance + normal       
640 #if WITH_BULLET == 1
641                         distance = plNearestPoints(
642                                         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);
643 #else
644                         // just be sure that we don't add anything
645                         distance = 2.0 * (epsilon + ALMOST_ZERO);
646 #endif  
647                         if (distance <= (epsilon + ALMOST_ZERO))
648                         {
649                                 // printf("dist: %f\n", (float)distance);
650                                 
651                                 // collpair->face1 = tree1->tri_index;
652                                 // collpair->face2 = tree2->tri_index;
653                                 
654                                 VECCOPY(collpair->normal, collpair->vector);
655                                 Normalize(collpair->normal);
656                                 
657                                 collpair->distance = distance;
658                                 BLI_linklist_prepend(&clmd->coll_parms->collision_list, collpair);
659                                 
660                         }
661                         else
662                         {
663                                 MEM_freeN(collpair);
664                         }
665                 }
666                 else
667                 {
668                         MEM_freeN(collpair);
669                 }
670         }
671 }
672
673 int cloth_are_edges_adjacent(ClothModifierData *clmd, ClothModifierData *coll_clmd, EdgeCollPair *edgecollpair)
674 {
675         Cloth *cloth1 = NULL, *cloth2 = NULL;
676         ClothVertex *verts1 = NULL, *verts2 = NULL;
677         float temp[3];
678          
679         cloth1 = clmd->clothObject;
680         cloth2 = coll_clmd->clothObject;
681         
682         verts1 = cloth1->verts;
683         verts2 = cloth2->verts;
684         
685         VECSUB(temp, verts1[edgecollpair->p11].xold, verts2[edgecollpair->p21].xold);
686         if(ABS(INPR(temp, temp)) < ALMOST_ZERO)
687                 return 1;
688         
689         VECSUB(temp, verts1[edgecollpair->p11].xold, verts2[edgecollpair->p22].xold);
690         if(ABS(INPR(temp, temp)) < ALMOST_ZERO)
691                 return 1;
692         
693         VECSUB(temp, verts1[edgecollpair->p12].xold, verts2[edgecollpair->p21].xold);
694         if(ABS(INPR(temp, temp)) < ALMOST_ZERO)
695                 return 1;
696         
697         VECSUB(temp, verts1[edgecollpair->p12].xold, verts2[edgecollpair->p22].xold);
698         if(ABS(INPR(temp, temp)) < ALMOST_ZERO)
699                 return 1;
700                 
701         return 0;
702 }
703
704 void cloth_collision_moving_edges(ClothModifierData *clmd, ClothModifierData *coll_clmd, CollisionTree *tree1, CollisionTree *tree2)
705 {
706         EdgeCollPair edgecollpair;
707         Cloth *cloth1=NULL, *cloth2=NULL;
708         MFace *face1=NULL, *face2=NULL;
709         ClothVertex *verts1=NULL, *verts2=NULL;
710         unsigned int i = 0, j = 0, k = 0;
711         int numsolutions = 0;
712         float a[3], b[3], c[3], d[3], e[3], f[3], solution[3];
713         
714         cloth1 = clmd->clothObject;
715         cloth2 = coll_clmd->clothObject;
716         
717         verts1 = cloth1->verts;
718         verts2 = cloth2->verts;
719
720         face1 = &(cloth1->mfaces[tree1->tri_index]);
721         face2 = &(cloth2->mfaces[tree2->tri_index]);
722         
723         for( i = 0; i < 5; i++)
724         {
725                 if(i == 0) 
726                 {
727                         edgecollpair.p11 = face1->v1;
728                         edgecollpair.p12 = face1->v2;
729                 }
730                 else if(i == 1) 
731                 {
732                         edgecollpair.p11 = face1->v2;
733                         edgecollpair.p12 = face1->v3;
734                 }
735                 else if(i == 2) 
736                 {
737                         if(face1->v4) 
738                         {
739                                 edgecollpair.p11 = face1->v3;
740                                 edgecollpair.p12 = face1->v4;
741                         }
742                         else 
743                         {
744                                 edgecollpair.p11 = face1->v3;
745                                 edgecollpair.p12 = face1->v1;
746                                 i+=5; // get out of here after this edge pair is handled
747                         }
748                 }
749                 else if(i == 3) 
750                 {
751                         if(face1->v4) 
752                         {
753                                 edgecollpair.p11 = face1->v4;
754                                 edgecollpair.p12 = face1->v1;
755                         }       
756                         else
757                                 continue;
758                 }
759                 else
760                 {
761                         edgecollpair.p11 = face1->v3;
762                         edgecollpair.p12 = face1->v1;
763                 }
764
765                 
766                 for( j = 0; j < 5; j++)
767                 {
768                         if(j == 0)
769                         {
770                                 edgecollpair.p21 = face2->v1;
771                                 edgecollpair.p22 = face2->v2;
772                         }
773                         else if(j == 1)
774                         {
775                                 edgecollpair.p21 = face2->v2;
776                                 edgecollpair.p22 = face2->v3;
777                         }
778                         else if(j == 2)
779                         {
780                                 if(face2->v4) 
781                                 {
782                                         edgecollpair.p21 = face2->v3;
783                                         edgecollpair.p22 = face2->v4;
784                                 }
785                                 else 
786                                 {
787                                         edgecollpair.p21 = face2->v3;
788                                         edgecollpair.p22 = face2->v1;
789                                 }
790                         }
791                         else if(j == 3)
792                         {
793                                 if(face2->v4) 
794                                 {
795                                         edgecollpair.p21 = face2->v4;
796                                         edgecollpair.p22 = face2->v1;
797                                 }
798                                 else
799                                         continue;
800                         }
801                         else
802                         {
803                                 edgecollpair.p21 = face2->v3;
804                                 edgecollpair.p22 = face2->v1;
805                         }
806                         
807                         
808                         if(!cloth_are_edges_adjacent(clmd, coll_clmd, &edgecollpair))
809                         {
810                                 VECSUB(a, verts1[edgecollpair.p12].xold, verts1[edgecollpair.p11].xold);
811                                 VECSUB(b, verts1[edgecollpair.p12].v, verts1[edgecollpair.p11].v);
812                                 VECSUB(c, verts1[edgecollpair.p21].xold, verts1[edgecollpair.p11].xold);
813                                 VECSUB(d, verts1[edgecollpair.p21].v, verts1[edgecollpair.p11].v);
814                                 VECSUB(e, verts2[edgecollpair.p22].xold, verts1[edgecollpair.p11].xold);
815                                 VECSUB(f, verts2[edgecollpair.p22].v, verts1[edgecollpair.p11].v);
816                                 
817                                 numsolutions = cloth_get_collision_time(a, b, c, d, e, f, solution);
818                                 
819                                 for (k = 0; k < numsolutions; k++) 
820                                 {                                                               
821                                         if ((solution[k] >= 0.0) && (solution[k] <= 1.0)) 
822                                         {
823                                                 //float out_collisionTime = solution[k];
824                                                 
825                                                 // TODO: check for collisions 
826                                                 
827                                                 // TODO: put into (edge) collision list
828                                                 
829                                                 // printf("Moving edge found!\n");
830                                         }
831                                 }
832                         }
833                 }
834         }               
835 }
836
837 void cloth_collision_moving_tris(ClothModifierData *clmd, ClothModifierData *coll_clmd, CollisionTree *tree1, CollisionTree *tree2)
838 {
839         CollPair collpair;
840         Cloth *cloth1=NULL, *cloth2=NULL;
841         MFace *face1=NULL, *face2=NULL;
842         ClothVertex *verts1=NULL, *verts2=NULL;
843         unsigned int i = 0, j = 0, k = 0;
844         int numsolutions = 0;
845         float a[3], b[3], c[3], d[3], e[3], f[3], solution[3];
846
847         for(i = 0; i < 2; i++)
848         {               
849                 cloth1 = clmd->clothObject;
850                 cloth2 = coll_clmd->clothObject;
851                 
852                 verts1 = cloth1->verts;
853                 verts2 = cloth2->verts;
854         
855                 face1 = &(cloth1->mfaces[tree1->tri_index]);
856                 face2 = &(cloth2->mfaces[tree2->tri_index]);
857                 
858                 // check all possible pairs of triangles
859                 if(i == 0)
860                 {
861                         collpair.ap1 = face1->v1;
862                         collpair.ap2 = face1->v2;
863                         collpair.ap3 = face1->v3;
864                         
865                         collpair.pointsb[0] = face2->v1;
866                         collpair.pointsb[1] = face2->v2;
867                         collpair.pointsb[2] = face2->v3;
868                         collpair.pointsb[3] = face2->v4;
869                 }
870                 
871                 if(i == 1)
872                 {
873                         if(face1->v4)
874                         {
875                                 collpair.ap1 = face1->v3;
876                                 collpair.ap2 = face1->v4;
877                                 collpair.ap3 = face1->v1;
878                                 
879                                 collpair.pointsb[0] = face2->v1;
880                                 collpair.pointsb[1] = face2->v2;
881                                 collpair.pointsb[2] = face2->v3;
882                                 collpair.pointsb[3] = face2->v4;
883                         }
884                         else
885                                 i++;
886                 }
887                 
888                 // calc SIPcode (?)
889                 
890                 if(i < 2)
891                 {
892                         VECSUB(a, verts1[collpair.ap2].xold, verts1[collpair.ap1].xold);
893                         VECSUB(b, verts1[collpair.ap2].v, verts1[collpair.ap1].v);
894                         VECSUB(c, verts1[collpair.ap3].xold, verts1[collpair.ap1].xold);
895                         VECSUB(d, verts1[collpair.ap3].v, verts1[collpair.ap1].v);
896                                 
897                         for(j = 0; j < 4; j++)
898                         {                                       
899                                 if((j==3) && !(face2->v4))
900                                         break;
901                                 
902                                 VECSUB(e, verts2[collpair.pointsb[j]].xold, verts1[collpair.ap1].xold);
903                                 VECSUB(f, verts2[collpair.pointsb[j]].v, verts1[collpair.ap1].v);
904                                 
905                                 numsolutions = cloth_get_collision_time(a, b, c, d, e, f, solution);
906                                 
907                                 for (k = 0; k < numsolutions; k++) 
908                                 {                                                               
909                                         if ((solution[k] >= 0.0) && (solution[k] <= 1.0)) 
910                                         {
911                                                 //float out_collisionTime = solution[k];
912                                                 
913                                                 // TODO: check for collisions 
914                                                 
915                                                 // TODO: put into (point-face) collision list
916                                                 
917                                                 // printf("Moving found!\n");
918                                                 
919                                         }
920                                 }
921                                 
922                                 // TODO: check borders for collisions
923                         }
924                         
925                 }
926         }
927 }
928
929 void cloth_collision_moving(ClothModifierData *clmd, ClothModifierData *coll_clmd, CollisionTree *tree1, CollisionTree *tree2)
930 {
931         // TODO: check for adjacent
932         cloth_collision_moving_edges(clmd, coll_clmd, tree1, tree2);
933         
934         cloth_collision_moving_tris(clmd, coll_clmd, tree1, tree2);
935         cloth_collision_moving_tris(coll_clmd, clmd, tree2, tree1);
936 }
937
938 void cloth_collision_self_static(ModifierData *md1, ModifierData *md2, CollisionTree *tree1, CollisionTree *tree2)
939 {
940 /*
941         ClothModifierData *clmd = (ClothModifierData *)md1;
942         CollisionModifierData *collmd = (CollisionModifierData *)md2;
943         CollPair *collpair = NULL;
944         Cloth *cloth1=NULL;
945         MFace *face1=NULL, *face2=NULL;
946         ClothVertex *verts1=NULL;
947         double distance = 0;
948         float epsilon = clmd->coll_parms->epsilon;
949         unsigned int i = 0;
950 */
951         
952 }
953
954 #if 0
955 /* aye this belongs to arith.c */
956 static void Vec3PlusStVec(float *v, float s, float *v1)
957 {
958         v[0] += s*v1[0];
959         v[1] += s*v1[1];
960         v[2] += s*v1[2];
961 }
962 #endif
963
964 // cloth - object collisions
965 int cloth_bvh_objcollision(ClothModifierData * clmd, float step, float dt)
966 {
967         Base *base=NULL;
968         CollisionModifierData *collmd=NULL;
969         Cloth *cloth=NULL;
970         Object *coll_ob=NULL;
971         BVH *cloth_bvh=NULL;
972         long i=0, j = 0, numfaces = 0, numverts = 0;
973         unsigned int result = 0, ic = 0, rounds = 0; // result counts applied collisions; ic is for debug output; 
974         ClothVertex *verts = NULL;
975         float tnull[3] = {0,0,0};
976         int ret = 0;
977         ClothModifierData *tclmd;
978         int collisions = 0, count = 0;
979
980         if ((clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_COLLOBJ) || !(((Cloth *)clmd->clothObject)->tree))
981         {
982                 return 0;
983         }
984         
985         cloth = clmd->clothObject;
986         verts = cloth->verts;
987         cloth_bvh = (BVH *) cloth->tree;
988         numfaces = clmd->clothObject->numfaces;
989         numverts = clmd->clothObject->numverts;
990         
991         ////////////////////////////////////////////////////////////
992         // static collisions
993         ////////////////////////////////////////////////////////////
994
995         // update cloth bvh
996         bvh_update_from_cloth(clmd, 0); // 0 means STATIC, 1 means MOVING (see later in this function)
997         
998         do
999         {
1000                 result = 0;
1001                 ic = 0;
1002                 clmd->coll_parms->collision_list = NULL; 
1003                 
1004                 // check all collision objects
1005                 for (base = G.scene->base.first; base; base = base->next)
1006                 {
1007                         coll_ob = base->object;
1008                         collmd = (CollisionModifierData *) modifiers_findByType (coll_ob, eModifierType_Collision);
1009                         
1010                         if (!collmd)
1011                                 continue;
1012                         
1013                         tclmd = (ClothModifierData *) modifiers_findByType (coll_ob, eModifierType_Cloth);
1014                         if(tclmd == clmd)
1015                                 continue;
1016                         
1017                         if (collmd->tree) 
1018                         {
1019                                 BVH *coll_bvh = collmd->tree;
1020                                 
1021                                 collision_move_object(collmd, step + dt, step);
1022                                         
1023                                 bvh_traverse((ModifierData *)clmd, (ModifierData *)collmd, cloth_bvh->root, coll_bvh->root, step, cloth_collision_static, 0);
1024                         }
1025                         else
1026                                 printf ("cloth_bvh_objcollision: found a collision object with clothObject or collData NULL.\n");
1027                 
1028                 
1029                         // process all collisions (calculate impulses, TODO: also repulses if distance too short)
1030                         result = 1;
1031                         for(j = 0; j < 5; j++) // 5 is just a value that ensures convergence
1032                         {
1033                                 result = 0;
1034                                 
1035                                 if (collmd->tree) 
1036                                         result += cloth_collision_response_static(clmd, collmd);
1037                                 
1038                                 // apply impulses in parallel
1039                                 ic=0;
1040                                 for(i = 0; i < numverts; i++)
1041                                 {
1042                                         // calculate "velocities" (just xnew = xold + v; no dt in v)
1043                                         if(verts[i].impulse_count)
1044                                         {
1045                                                 VECADDMUL(verts[i].tv, verts[i].impulse, 1.0f / verts[i].impulse_count);
1046                                                 VECCOPY(verts[i].impulse, tnull);
1047                                                 verts[i].impulse_count = 0;
1048                                         
1049                                                 ic++;
1050                                                 ret++;
1051                                         }
1052                                 }
1053                         }
1054                         
1055                         // free collision list
1056                         if(clmd->coll_parms->collision_list)
1057                         {
1058                                 LinkNode *search = clmd->coll_parms->collision_list;
1059                                 while(search)
1060                                 {
1061                                         CollPair *coll_pair = search->link;
1062                                                         
1063                                         MEM_freeN(coll_pair);
1064                                         search = search->next;
1065                                 }
1066                                 BLI_linklist_free(clmd->coll_parms->collision_list,NULL);
1067                         
1068                                 clmd->coll_parms->collision_list = NULL;
1069                         }
1070                 }
1071                 rounds++;
1072         
1073                 ////////////////////////////////////////////////////////////
1074                 // update positions
1075                 // this is needed for bvh_calc_DOP_hull_moving() [kdop.c]
1076                 ////////////////////////////////////////////////////////////
1077                 
1078                 // verts come from clmd
1079                 for(i = 0; i < numverts; i++)
1080                 {
1081                         if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL) 
1082                         {                       
1083                                 if(verts [i].flags & CLOTH_VERT_FLAG_PINNED)
1084                                 {
1085                                         continue;
1086                                 }
1087                         }
1088                         
1089                         VECADD(verts[i].tx, verts[i].txold, verts[i].tv);
1090                 }
1091                 ////////////////////////////////////////////////////////////
1092                 
1093                 
1094                 ////////////////////////////////////////////////////////////
1095                 // Test on *simple* selfcollisions
1096                 ////////////////////////////////////////////////////////////
1097                 if (clmd->coll_parms->flags & CLOTH_COLLSETTINGS_FLAG_SELF)     
1098                 {
1099                         collisions = 1;
1100                         verts = cloth->verts; // needed for openMP
1101                         
1102                         for(count = 0; count < clmd->coll_parms->self_loop_count; count++)
1103                         {       
1104                                 if(collisions)
1105                                 {
1106                                         collisions = 0;
1107                 #pragma omp parallel for private(i,j, collisions) shared(verts, ret)
1108                                         for(i = 0; i < cloth->numverts; i++)
1109                                         {
1110                                                 for(j = i + 1; j < cloth->numverts; j++)
1111                                                 {
1112                                                         float temp[3];
1113                                                         float length = 0;
1114                                                         float mindistance = clmd->coll_parms->selfepsilon*(cloth->verts[i].avg_spring_len + cloth->verts[j].avg_spring_len);
1115                                                                 
1116                                                         if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL)
1117                                                         {                       
1118                                                                 if((cloth->verts [i].flags & CLOTH_VERT_FLAG_PINNED)
1119                                                                 && (cloth->verts [j].flags & CLOTH_VERT_FLAG_PINNED))
1120                                                                 {
1121                                                                         continue;
1122                                                                 }
1123                                                         }
1124                                                         
1125                                                         VECSUB(temp, verts[i].tx, verts[j].tx);
1126                                                         
1127                                                         if ((ABS(temp[0]) > mindistance) || (ABS(temp[1]) > mindistance) || (ABS(temp[2]) > mindistance)) continue;
1128                                                                 
1129                                                         // check for adjacent points
1130                                                         if(BLI_edgehash_haskey (cloth->edgehash, i, j ))
1131                                                         {
1132                                                                 continue;
1133                                                         }
1134                                                                 
1135                                                         length = Normalize(temp);
1136                                                                 
1137                                                         if(length < mindistance)
1138                                                         {
1139                                                                 float correction = mindistance - length;
1140                                                                         
1141                                                                 if(cloth->verts [i].flags & CLOTH_VERT_FLAG_PINNED)
1142                                                                 {
1143                                                                         VecMulf(temp, -correction);
1144                                                                         VECADD(verts[j].tx, verts[j].tx, temp);
1145                                                                 }
1146                                                                 else if(cloth->verts [j].flags & CLOTH_VERT_FLAG_PINNED)
1147                                                                 {
1148                                                                         VecMulf(temp, correction);
1149                                                                         VECADD(verts[i].tx, verts[i].tx, temp);
1150                                                                 }
1151                                                                 else
1152                                                                 {
1153                                                                         VecMulf(temp, -correction*0.5);
1154                                                                         VECADD(verts[j].tx, verts[j].tx, temp);
1155                                                                         
1156                                                                         VECSUB(verts[i].tx, verts[i].tx, temp); 
1157                                                                 }
1158                                                                 
1159                                                                 collisions = 1;
1160                                                                 
1161                                                                 if(!ret)
1162                                                                 {       
1163                         #pragma omp critical
1164                         {
1165                                                                         ret = 1;
1166                         }
1167                                                                 }
1168                                                         }
1169                                                 }
1170                                         }
1171                                 }
1172                         }
1173                         ////////////////////////////////////////////////////////////
1174                         
1175                         ////////////////////////////////////////////////////////////
1176                         // SELFCOLLISIONS: update velocities
1177                         ////////////////////////////////////////////////////////////
1178                         if(ret)
1179                         {
1180                                 for(i = 0; i < cloth->numverts; i++)
1181                                 {
1182                                         if(!(cloth->verts [i].flags & CLOTH_VERT_FLAG_PINNED))
1183                                                 VECSUB(verts[i].tv, verts[i].tx, verts[i].txold);
1184                                 }
1185                         }
1186                         ////////////////////////////////////////////////////////////
1187                 }
1188         }
1189         while(result && (clmd->coll_parms->loop_count>rounds));
1190         
1191         ////////////////////////////////////////////////////////////
1192         // moving collisions
1193         //
1194         // response code is just missing itm 
1195         ////////////////////////////////////////////////////////////
1196
1197         /*
1198         // update cloth bvh
1199         bvh_update_from_cloth(clmd, 1);  // 0 means STATIC, 1 means MOVING 
1200         
1201         // update moving bvh for collision object once
1202         for (base = G.scene->base.first; base; base = base->next)
1203         {
1204                 
1205         coll_ob = base->object;
1206         coll_clmd = (ClothModifierData *) modifiers_findByType (coll_ob, eModifierType_Cloth);
1207         if (!coll_clmd)
1208         continue;
1209                 
1210         if(!coll_clmd->clothObject)
1211         continue;
1212                 
1213                                 // if collision object go on
1214         if (coll_clmd->clothObject && coll_clmd->clothObject->tree) 
1215         {
1216         BVH *coll_bvh = coll_clmd->clothObject->tree;
1217                         
1218         bvh_update_from_cloth(coll_clmd, 1);  // 0 means STATIC, 1 means MOVING         
1219 }
1220 }
1221         
1222         
1223         do
1224         {
1225         result = 0;
1226         ic = 0;
1227         clmd->coll_parms->collision_list = NULL; 
1228                 
1229                 // check all collision objects
1230         for (base = G.scene->base.first; base; base = base->next)
1231         {
1232         coll_ob = base->object;
1233         coll_clmd = (ClothModifierData *) modifiers_findByType (coll_ob, eModifierType_Cloth);
1234                         
1235         if (!coll_clmd)
1236         continue;
1237                         
1238                         // if collision object go on
1239         if (coll_clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_COLLOBJ)
1240         {
1241         if (coll_clmd->clothObject && coll_clmd->clothObject->tree) 
1242         {
1243         BVH *coll_bvh = coll_clmd->clothObject->tree;
1244                                         
1245         bvh_traverse(clmd, coll_clmd, cloth_bvh->root, coll_bvh->root, step, cloth_collision_moving, 0);
1246 }
1247         else
1248         printf ("cloth_bvh_objcollision: found a collision object with clothObject or collData NULL.\n");
1249 }
1250 }
1251                 
1252                 // process all collisions (calculate impulses, TODO: also repulses if distance too short)
1253         result = 1;
1254         for(j = 0; j < 10; j++) // 10 is just a value that ensures convergence
1255         {
1256         result = 0;
1257                                 
1258                         // handle all collision objects
1259         for (base = G.scene->base.first; base; base = base->next)
1260         {
1261                         
1262         coll_ob = base->object;
1263         coll_clmd = (ClothModifierData *) modifiers_findByType (coll_ob, eModifierType_Cloth);
1264                                                 
1265         if (!coll_clmd)
1266         continue;
1267                                 
1268                                 // if collision object go on
1269         if (coll_clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_COLLOBJ)
1270         {
1271         if (coll_clmd->clothObject) 
1272         result += cloth_collision_response_moving_tris(clmd, coll_clmd);
1273         else
1274         printf ("cloth_bvh_objcollision: found a collision object with clothObject or collData NULL.\n");
1275 }
1276 }
1277                                                 
1278                         // apply impulses in parallel
1279         ic=0;
1280         for(i = 0; i < numverts; i++)
1281         {
1282                                 // calculate "velocities" (just xnew = xold + v; no dt in v)
1283         if(verts[i].impulse_count)
1284         {
1285         VECADDMUL(verts[i].tv, verts[i].impulse, 1.0f / verts[i].impulse_count);
1286         VECCOPY(verts[i].impulse, tnull);
1287         verts[i].impulse_count = 0;
1288                                                         
1289         ic++;
1290         ret++;
1291 }
1292 }
1293 }
1294                 
1295                 
1296                 // verts come from clmd
1297         for(i = 0; i < numverts; i++)
1298         {
1299         VECADD(verts[i].tx, verts[i].txold, verts[i].tv);
1300 }
1301                 
1302                 // update cloth bvh
1303         bvh_update_from_cloth(clmd, 1);  // 0 means STATIC, 1 means MOVING 
1304                 
1305                 
1306                 // free collision list
1307         if(clmd->coll_parms->collision_list)
1308         {
1309         LinkNode *search = clmd->coll_parms->collision_list;
1310         while(search)
1311         {
1312         CollPair *coll_pair = search->link;
1313                                                         
1314         MEM_freeN(coll_pair);
1315         search = search->next;
1316 }
1317         BLI_linklist_free(clmd->coll_parms->collision_list,NULL);
1318                         
1319         clmd->coll_parms->collision_list = NULL;
1320 }
1321                 
1322                 // printf("ic: %d\n", ic);
1323         rounds++;
1324 }
1325         while(result && (CLOTH_MAX_THRESHOLD>rounds));
1326         
1327         ////////////////////////////////////////////////////////////
1328         // update positions + velocities
1329         ////////////////////////////////////////////////////////////
1330         
1331         // verts come from clmd
1332         for(i = 0; i < numverts; i++)
1333         {
1334         VECADD(verts[i].tx, verts[i].txold, verts[i].tv);
1335 }
1336         ////////////////////////////////////////////////////////////
1337         */
1338         
1339         return MIN2(ret, 1);
1340 }