Cloth: changes in editmode will influence the velocity by 10% now, changed/add some...
[blender.git] / source / blender / blenkernel / intern / cloth.c
1 /*  cloth.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
34 #include <math.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "MEM_guardedalloc.h"
39
40 /* types */
41 #include "DNA_curve_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_object_force.h"
44 #include "DNA_cloth_types.h"
45 #include "DNA_key_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_lattice_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_modifier_types.h"
51
52 #include "BLI_blenlib.h"
53 #include "BLI_arithb.h"
54 #include "BLI_editVert.h"
55 #include "BLI_edgehash.h"
56 #include "BLI_linklist.h"
57
58 #include "BKE_curve.h"
59 #include "BKE_deform.h"
60 #include "BKE_DerivedMesh.h"
61 #include "BKE_cdderivedmesh.h"
62 #include "BKE_displist.h"
63 #include "BKE_effect.h"
64 #include "BKE_global.h"
65 #include "BKE_key.h"
66 #include "BKE_mesh.h"
67 #include "BKE_object.h"
68 #include "BKE_cloth.h"
69 #include "BKE_modifier.h"
70 #include "BKE_utildefines.h"
71 #include "BKE_DerivedMesh.h"
72 #include "BIF_editdeform.h"
73 #include "BIF_editkey.h"
74 #include "DNA_screen_types.h"
75 #include "BSE_headerbuttons.h"
76 #include "BIF_screen.h"
77 #include "BIF_space.h"
78 #include "mydevice.h"
79
80 #include "BKE_pointcache.h"
81
82 #ifdef _WIN32
83 void tstart ( void )
84 {}
85 void tend ( void )
86 {
87 }
88 double tval()
89 {
90         return 0;
91 }
92 #else
93 #include <sys/time.h>
94                          static struct timeval _tstart, _tend;
95          static struct timezone tz;
96          void tstart ( void )
97 {
98         gettimeofday ( &_tstart, &tz );
99 }
100 void tend ( void )
101 {
102         gettimeofday ( &_tend,&tz );
103 }
104 double tval()
105 {
106         double t1, t2;
107         t1 = ( double ) _tstart.tv_sec + ( double ) _tstart.tv_usec/ ( 1000*1000 );
108         t2 = ( double ) _tend.tv_sec + ( double ) _tend.tv_usec/ ( 1000*1000 );
109         return t2-t1;
110 }
111 #endif
112
113 /* Our available solvers. */
114 // 255 is the magic reserved number, so NEVER try to put 255 solvers in here!
115 // 254 = MAX!
116 static CM_SOLVER_DEF    solvers [] =
117 {
118         { "Implicit", CM_IMPLICIT, implicit_init, implicit_solver, implicit_free },
119         // { "Implicit C++", CM_IMPLICITCPP, implicitcpp_init, implicitcpp_solver, implicitcpp_free },
120 };
121
122 /* ********** cloth engine ******* */
123 /* Prototypes for internal functions.
124 */
125 static void cloth_to_object (Object *ob,  ClothModifierData *clmd, DerivedMesh *dm);
126 static void cloth_from_mesh ( Object *ob, ClothModifierData *clmd, DerivedMesh *dm );
127 static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float framenr);
128 int cloth_build_springs ( ClothModifierData *clmd, DerivedMesh *dm );
129 static void cloth_apply_vgroup ( ClothModifierData *clmd, DerivedMesh *dm );
130
131
132 /******************************************************************************
133 *
134 * External interface called by modifier.c clothModifier functions.
135 *
136 ******************************************************************************/
137 /**
138  * cloth_init -  creates a new cloth simulation.
139  *
140  * 1. create object
141  * 2. fill object with standard values or with the GUI settings if given
142  */
143 void cloth_init ( ClothModifierData *clmd )
144 {       
145         /* Initialize our new data structure to reasonable values. */
146         clmd->sim_parms->gravity [0] = 0.0;
147         clmd->sim_parms->gravity [1] = 0.0;
148         clmd->sim_parms->gravity [2] = -9.81;
149         clmd->sim_parms->structural = 30.0;
150         clmd->sim_parms->shear = 30.0;
151         clmd->sim_parms->bending = 1.0;
152         clmd->sim_parms->Cdis = 5.0;
153         clmd->sim_parms->Cvi = 1.0;
154         clmd->sim_parms->mass = 1.0f;
155         clmd->sim_parms->stepsPerFrame = 5;
156         clmd->sim_parms->sim_time = 1.0;
157         clmd->sim_parms->flags = 0;
158         clmd->sim_parms->solver_type = 0;
159         clmd->sim_parms->preroll = 0;
160         clmd->sim_parms->maxspringlen = 10;
161         clmd->sim_parms->firstframe = 1;
162         clmd->sim_parms->lastframe = 250;
163         clmd->sim_parms->vgroup_mass = 0;
164         clmd->sim_parms->lastcachedframe = 0;
165         clmd->sim_parms->editedframe = 0;
166         clmd->sim_parms->autoprotect = 25;
167         clmd->sim_parms->firstcachedframe = -1.0;
168         clmd->sim_parms->avg_spring_len = 0.0;
169         
170         clmd->coll_parms->self_friction = 5.0;
171         clmd->coll_parms->friction = 5.0;
172         clmd->coll_parms->loop_count = 3;
173         clmd->coll_parms->epsilon = 0.015f;
174         clmd->coll_parms->flags = CLOTH_COLLSETTINGS_FLAG_ENABLED;
175         clmd->coll_parms->collision_list = NULL;
176         clmd->coll_parms->self_loop_count = 1.0;
177         clmd->coll_parms->selfepsilon = 0.75;
178
179         /* These defaults are copied from softbody.c's
180         * softbody_calc_forces() function.
181         */
182         clmd->sim_parms->eff_force_scale = 1000.0;
183         clmd->sim_parms->eff_wind_scale = 250.0;
184
185         // also from softbodies
186         clmd->sim_parms->maxgoal = 1.0f;
187         clmd->sim_parms->mingoal = 0.0f;
188         clmd->sim_parms->defgoal = 0.0f;
189         clmd->sim_parms->goalspring = 10.0f;
190         clmd->sim_parms->goalfrict = 5.0f;
191 }
192
193
194 BVH *bvh_build_from_cloth (ClothModifierData *clmd, float epsilon)
195 {
196         unsigned int i = 0;
197         BVH     *bvh=NULL;
198         Cloth *cloth = clmd->clothObject;
199         ClothVertex *verts = NULL;
200
201         if(!clmd)
202                 return NULL;
203
204         cloth = clmd->clothObject;
205
206         if(!cloth)
207                 return NULL;
208         
209         verts = cloth->verts;
210         
211         // in the moment, return zero if no faces there
212         if(!cloth->numfaces)
213                 return NULL;
214         
215         bvh = MEM_callocN(sizeof(BVH), "BVH");
216         if (bvh == NULL) 
217         {
218                 printf("bvh: Out of memory.\n");
219                 return NULL;
220         }
221         
222         // springs = cloth->springs;
223         // numsprings = cloth->numsprings;
224         
225         bvh->flags = 0;
226         bvh->leaf_tree = NULL;
227         bvh->leaf_root = NULL;
228         bvh->tree = NULL;
229
230         bvh->epsilon = epsilon;
231         bvh->numfaces = cloth->numfaces;
232         bvh->mfaces = cloth->mfaces;
233
234         bvh->numverts = cloth->numverts;
235         
236         bvh->current_x = MEM_callocN ( sizeof ( MVert ) * bvh->numverts, "bvh->current_x" );
237         bvh->current_xold = MEM_callocN ( sizeof ( MVert ) * bvh->numverts, "bvh->current_xold" );
238         
239         for(i = 0; i < bvh->numverts; i++)
240         {
241                 VECCOPY(bvh->current_x[i].co, verts[i].tx);
242                 VECCOPY(bvh->current_xold[i].co, verts[i].txold);
243         }
244         
245         bvh_build (bvh);
246         
247         return bvh;
248 }
249
250 void bvh_update_from_cloth(ClothModifierData *clmd, int moving)
251 {
252         unsigned int i = 0;
253         Cloth *cloth = clmd->clothObject;
254         BVH *bvh = cloth->tree;
255         ClothVertex *verts = cloth->verts;
256         
257         if(!bvh)
258                 return;
259         
260         if(cloth->numverts!=bvh->numverts)
261                 return;
262         
263         if(cloth->verts)
264         {
265                 for(i = 0; i < bvh->numverts; i++)
266                 {
267                         VECCOPY(bvh->current_x[i].co, verts[i].tx);
268                         VECCOPY(bvh->current_xold[i].co, verts[i].txold);
269                 }
270         }
271         
272         bvh_update(bvh, moving);
273 }
274
275 DerivedMesh *CDDM_create_tearing ( ClothModifierData *clmd, DerivedMesh *dm )
276 {
277         DerivedMesh *result = NULL;
278         unsigned int i = 0, a = 0, j=0;
279         int numverts = dm->getNumVerts ( dm );
280         int numedges = dm->getNumEdges ( dm );
281         int numfaces = dm->getNumFaces ( dm );
282
283         MVert *mvert = CDDM_get_verts ( dm );
284         MEdge *medge = CDDM_get_edges ( dm );
285         MFace *mface = CDDM_get_faces ( dm );
286
287         MVert *mvert2;
288         MFace *mface2;
289         unsigned int numtris=0;
290         unsigned int numquads=0;
291         EdgeHash *edgehash = NULL;
292         Cloth *cloth = clmd->clothObject;
293         ClothSpring *springs = cloth->springs;
294         unsigned int numsprings = cloth->numsprings;
295
296         // create spring tearing hash
297         edgehash = BLI_edgehash_new();
298
299         for ( i = 0; i < numsprings; i++ )
300         {
301                 if ( ( springs[i].flags & CLOTH_SPRING_FLAG_DEACTIVATE )
302                                      && ( !BLI_edgehash_haskey ( edgehash, springs[i].ij, springs[i].kl ) ) )
303                 {
304                         BLI_edgehash_insert ( edgehash, springs[i].ij, springs[i].kl, NULL );
305                         BLI_edgehash_insert ( edgehash, springs[i].kl, springs[i].ij, NULL );
306                         j++;
307                 }
308         }
309
310         // printf("found %d tears\n", j);
311
312         result = CDDM_from_template ( dm, numverts, 0, numfaces );
313
314         if ( !result )
315                 return NULL;
316
317         // do verts
318         mvert2 = CDDM_get_verts ( result );
319         for ( a=0; a<numverts; a++ )
320         {
321                 MVert *inMV;
322                 MVert *mv = &mvert2[a];
323
324                 inMV = &mvert[a];
325
326                 DM_copy_vert_data ( dm, result, a, a, 1 );
327                 *mv = *inMV;
328         }
329
330
331         // do faces
332         mface2 = CDDM_get_faces ( result );
333         for ( a=0, i=0; a<numfaces; a++ )
334         {
335                 MFace *mf = &mface2[i];
336                 MFace *inMF;
337                 inMF = &mface[a];
338
339                 /*
340                 DM_copy_face_data(dm, result, a, i, 1);
341
342                 *mf = *inMF;
343                 */
344
345                 if ( ( !BLI_edgehash_haskey ( edgehash, mface[a].v1, mface[a].v2 ) )
346                                       && ( !BLI_edgehash_haskey ( edgehash, mface[a].v2, mface[a].v3 ) )
347                                       && ( !BLI_edgehash_haskey ( edgehash, mface[a].v3, mface[a].v4 ) )
348                                       && ( !BLI_edgehash_haskey ( edgehash, mface[a].v4, mface[a].v1 ) ) )
349                 {
350                         mf->v1 = mface[a].v1;
351                         mf->v2 = mface[a].v2;
352                         mf->v3 = mface[a].v3;
353                         mf->v4 = mface[a].v4;
354
355                         test_index_face ( mf, NULL, 0, 4 );
356
357                         i++;
358                 }
359         }
360
361         CDDM_lower_num_faces ( result, i );
362         CDDM_calc_edges ( result );
363         CDDM_calc_normals ( result );
364
365         BLI_edgehash_free ( edgehash, NULL );
366
367         return result;
368 }
369
370 int modifiers_indexInObject(Object *ob, ModifierData *md_seek);
371
372 int cloth_read_cache(Object *ob, ClothModifierData *clmd, float framenr)
373 {
374         FILE *fp = NULL;
375         int stack_index = -1;
376         unsigned int a, ret = 1;
377         Cloth *cloth = clmd->clothObject;
378         
379         if(!cloth)
380                 return 0;
381         
382         stack_index = modifiers_indexInObject(ob, (ModifierData *)clmd);
383         
384         fp = BKE_ptcache_id_fopen((ID *)ob, 'r', framenr, stack_index);
385         if(!fp)
386                 ret = 0;
387         else {
388                 for(a = 0; a < cloth->numverts; a++)
389                 {
390                         if(fread(&cloth->verts[a].x, sizeof(float), 3, fp) != 3) 
391                         {
392                                 ret = 0;
393                                 break;
394                         }
395                         if(fread(&cloth->verts[a].xconst, sizeof(float), 3, fp) != 3) 
396                         {
397                                 ret = 0;
398                                 break;
399                         }
400                         if(fread(&cloth->verts[a].v, sizeof(float), 3, fp) != 3) 
401                         {
402                                 ret = 0;
403                                 break;
404                         }
405                 }
406                 
407                 fclose(fp);
408                 
409                 if(clmd->sim_parms->lastcachedframe < framenr)
410                 {
411                         if(G.rt > 0)
412                                 printf("cloth_read_cache problem: lnex - f#: %f, lastCF: %d\n", framenr, clmd->sim_parms->lastcachedframe);
413                 }
414                 
415                 if(G.rt > 0)
416                         printf("cloth_read_cache: %f successfully \n", framenr);
417         }
418         
419         if(G.rt > 0)
420                 printf("cloth_read_cache: %f\n", framenr);
421         
422         return ret;
423 }
424
425 void cloth_clear_cache(Object *ob, ClothModifierData *clmd, float framenr)
426 {
427         int stack_index = -1;
428         
429         // don't do anything as long as we're in editmode!
430         if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_EDITMODE)
431         {
432                 /* delete cache free request */
433                 clmd->sim_parms->flags &= ~CLOTH_SIMSETTINGS_FLAG_CCACHE_FFREE;
434                 
435                 return;
436         }
437         
438         /* clear cache if specific frame cleaning requested or cache is not protected */
439         if((!(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_CCACHE_PROTECT)) || (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_CCACHE_FFREE))
440         {
441                 stack_index = modifiers_indexInObject(ob, (ModifierData *)clmd);
442                 
443                 BKE_ptcache_id_clear((ID *)ob, PTCACHE_CLEAR_AFTER, framenr, stack_index);
444                 
445                 /* update last cached frame # */
446                 clmd->sim_parms->lastcachedframe = framenr;
447                 
448                 /* update first cached frame # */
449                 if((framenr < clmd->sim_parms->firstcachedframe) && (clmd->sim_parms->firstcachedframe >=0.0))
450                         clmd->sim_parms->firstcachedframe = -1.0;
451                 
452                 if(G.rt > 0)
453                         printf("cloth_clear_cache: %f\n", framenr);
454         }
455         
456         /* delete cache free request */
457         clmd->sim_parms->flags &= ~CLOTH_SIMSETTINGS_FLAG_CCACHE_FFREE;
458         
459         
460 }
461 void cloth_write_cache(Object *ob, ClothModifierData *clmd, float framenr)
462 {
463         FILE *fp = NULL;
464         int stack_index = -1;
465         unsigned int a;
466         Cloth *cloth = clmd->clothObject;
467         
468         if(G.rt > 0)
469                 printf("cloth_write_cache: %f\n", framenr);
470         
471         if(!cloth)
472         {
473                 if(G.rt > 0)
474                         printf("cloth_write_cache: no cloth\n");
475                 return;
476         }
477         
478         stack_index = modifiers_indexInObject(ob, (ModifierData *)clmd);
479         
480         fp = BKE_ptcache_id_fopen((ID *)ob, 'w', framenr, stack_index);
481         if(!fp)
482         {
483                 if(G.rt > 0)
484                         printf("cloth_write_cache: no fp\n");
485                 return;
486         }
487         
488         for(a = 0; a < cloth->numverts; a++)
489         {
490                 fwrite(&cloth->verts[a].x, sizeof(float),3,fp);
491                 fwrite(&cloth->verts[a].xconst, sizeof(float),3,fp);
492                 fwrite(&cloth->verts[a].v, sizeof(float),3,fp);
493         }
494         
495         /* update last cached frame # */
496         clmd->sim_parms->lastcachedframe = MAX2(clmd->sim_parms->lastcachedframe, framenr);
497         
498         /* update first cached frame # */
499         if((clmd->sim_parms->firstcachedframe < 0.0) || ((framenr < clmd->sim_parms->firstcachedframe) && (clmd->sim_parms->firstcachedframe > 0.0)))
500                 clmd->sim_parms->firstcachedframe = framenr;
501         
502         if(G.rt > 0)
503                 printf("lcf: %d, framenr: %f\n", clmd->sim_parms->lastcachedframe, framenr);
504
505         fclose(fp);
506 }
507
508
509
510 /************************************************
511  * clothModifier_do - main simulation function
512 ************************************************/
513 DerivedMesh *clothModifier_do(ClothModifierData *clmd,Object *ob, DerivedMesh *dm, int useRenderParams, int isFinalCalc)
514
515 {
516         unsigned int i;
517         Cloth *cloth = clmd->clothObject;
518         float framenr = G.scene->r.cfra;
519         float current_time = bsystem_time ( ob, ( float ) G.scene->r.cfra, 0.0 );
520         ListBase *effectors = NULL;
521         ClothVertex *verts = NULL;
522         float deltaTime = current_time - clmd->sim_parms->sim_time;
523         unsigned int numverts = -1;
524         unsigned int numedges = -1;
525         unsigned int numfaces = -1;
526         MVert *mvert = NULL;
527         MEdge *medge = NULL;
528         MFace *mface = NULL;
529         DerivedMesh *result = NULL;
530         int ret = 0;
531         
532         if(G.rt > 0)
533                 printf("clothModifier_do start\n");
534         
535         /* we're getting called two times during file load,
536         resulting in a not valid G.relbase on the first time (cache makes problems)
537         --> just return back */
538         if((clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_LOADED) && (!G.relbase_valid)) 
539         {
540                 clmd->sim_parms->flags &= ~CLOTH_SIMSETTINGS_FLAG_LOADED;
541                 return dm;
542         }
543         
544         result = CDDM_copy(dm);
545         
546         if(!result)
547         {
548                 return dm;
549         }
550         
551         numverts = result->getNumVerts(result);
552         numedges = result->getNumEdges(result);
553         numfaces = result->getNumFaces(result);
554         mvert = dm->getVertArray(result);
555         medge = dm->getEdgeArray(result);
556         mface = dm->getFaceArray(result);
557         
558         /* check if cache is active / if file is already saved */
559         /*
560         if ((!G.relbase_valid) && ( deltaTime != 1.0f ))
561         {
562         clmd->sim_parms->flags |= CLOTH_SIMSETTINGS_FLAG_RESET;
563 }
564         */
565         
566         if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_RESET)
567         {       
568                 cloth_free_modifier (ob, clmd);
569                 if(G.rt > 0)
570                         printf("clothModifier_do CLOTH_SIMSETTINGS_FLAG_RESET\n");
571         }
572         
573         // unused in the moment, calculated seperately in implicit.c
574         clmd->sim_parms->dt = 1.0f / clmd->sim_parms->stepsPerFrame;
575         
576         if ( ( clmd->clothObject == NULL ) || (clmd->clothObject && (numverts != clmd->clothObject->numverts )) )
577         {       
578                 /* only force free the cache if we have a different number of verts */
579                 if(clmd->clothObject && (numverts != clmd->clothObject->numverts ))
580                 {
581                         if(G.rt > 0)
582                                 printf("Force Freeing: numverts != clmd->clothObject->numverts\n");
583                         
584                         clmd->sim_parms->flags |= CLOTH_SIMSETTINGS_FLAG_CCACHE_FFREE;
585                         cloth_free_modifier ( ob, clmd );
586                 }
587                 
588                 cloth_clear_cache(ob, clmd, 0);
589                                 
590                 if ( !cloth_from_object ( ob, clmd, result, framenr ) )
591                         return result;
592         
593                 if ( clmd->clothObject == NULL )
594                         return result;
595         
596                 cloth = clmd->clothObject;
597                 
598                 if(!cloth_read_cache(ob, clmd, framenr))
599                 {
600                         /* save first frame in case we have a reseted object 
601                         and we move one frame forward.
602                         In that case we would only start with the SECOND frame
603                         if we don't save the current state before 
604                         TODO PROBLEM: IMHO we can't track external movement from the
605                         first frame in this case! */
606                         /*
607                         if ( deltaTime == 1.0f )
608                         cloth_write_cache(ob, clmd, framenr-1.0);
609                         */
610                         if(G.rt > 0)
611                                 printf("cloth_from_object NO cloth_read_cache cloth_write_cache\n");
612                 }
613                 else
614                 {
615                         if(G.rt > 0)
616                                 printf("cloth_from_object cloth_read_cache\n");
617                         
618                         implicit_set_positions(clmd);
619                 }
620                 
621                 clmd->sim_parms->sim_time = current_time;
622         }
623         
624         // only be active during a specific period:
625         // that's "first frame" and "last frame" on GUI
626         if ( current_time < clmd->sim_parms->firstframe )
627         {
628                 if(G.rt > 0)
629                         printf("current_time < clmd->sim_parms->firstframe\n");
630                 return result;
631         }
632         else if ( current_time > clmd->sim_parms->lastframe )
633         {
634                 int stack_index = modifiers_indexInObject(ob, (ModifierData *)clmd);
635                         
636                 if(G.rt > 0)
637                         printf("current_time > clmd->sim_parms->lastframe\n");
638                 
639                 if(BKE_ptcache_id_exist((ID *)ob, clmd->sim_parms->lastcachedframe, stack_index))
640                 {
641                         if(cloth_read_cache(ob, clmd, clmd->sim_parms->lastcachedframe))
642                         {
643                                 implicit_set_positions(clmd);
644                                 
645                                 // Copy the result back to the object.
646                                 cloth_to_object (ob, clmd, result);
647                         }
648                 }
649                 return result;
650         }
651         
652         // check for autoprotection, but only if cache active
653         if((framenr >= clmd->sim_parms->autoprotect) && (G.relbase_valid))
654         {
655                 if(G.rt > 0)
656                         printf("fr#: %f, auto: %d\n", framenr, clmd->sim_parms->autoprotect);
657                 clmd->sim_parms->flags |= CLOTH_SIMSETTINGS_FLAG_CCACHE_PROTECT;
658         }
659         if(G.rt > 0)
660                 printf("clothModifier_do deltaTime=1 cachewrite\n");
661         
662         /* nice moving one frame forward */
663         if ( deltaTime == 1.0f )
664         {
665                 clmd->sim_parms->sim_time = current_time;
666                 
667                 if(G.rt > 0)
668                         printf("clothModifier_do deltaTime=1\n");
669                 
670                 if(!cloth_read_cache(ob, clmd, framenr))
671                 {
672                         verts = cloth->verts;
673
674                         // Force any pinned verts to their constrained location.
675                         for ( i = 0; i < clmd->clothObject->numverts; i++, verts++ )
676                         {
677                                 // Save the previous position.
678                                 VECCOPY ( verts->xold, verts->xconst );
679                                 VECCOPY ( verts->txold, verts->x );
680
681                                 // Get the current position.
682                                 VECCOPY ( verts->xconst, mvert[i].co );
683                                 Mat4MulVecfl ( ob->obmat, verts->xconst );
684                         }
685                         
686                         tstart();
687
688                         // Call the solver.
689                         if ( solvers [clmd->sim_parms->solver_type].solver )
690                         {
691                                 ret = solvers [clmd->sim_parms->solver_type].solver ( ob, framenr, clmd, effectors );
692                         }
693
694                         tend();
695                         // printf ( "Cloth simulation time: %f\n", ( float ) tval() );
696                         
697                         if(ret)
698                                 cloth_write_cache(ob, clmd, framenr);
699                         else
700                                 clmd->sim_parms->sim_time--;
701                 }
702                 else
703                 {
704                         if(G.rt > 0)
705                                 printf("clothModifier_do deltaTime=1 cacheread\n");
706                         implicit_set_positions(clmd);
707                 }
708                 
709                 // Copy the result back to the object.
710                 cloth_to_object (ob, clmd, result);
711         }
712         else if(deltaTime == 0.0f) 
713         {       
714                 if(G.rt > 0)
715                         printf("clothModifier_do deltaTime!=1 clmd->clothObject != NULL\n");
716                 if(cloth_read_cache(ob, clmd, framenr))
717                 {
718                         cloth_to_object (ob, clmd, result);
719                         implicit_set_positions(clmd);
720                 }
721                 else /* same cache parts are missing */
722                 {
723                         /*
724                         clmd->sim_parms->flags |= CLOTH_SIMSETTINGS_FLAG_RESET;
725                         */
726                         clmd->sim_parms->flags |= CLOTH_SIMSETTINGS_FLAG_CCACHE_FFREE;
727                         cloth_clear_cache(ob, clmd, 0);
728                         
729                         cloth_write_cache(ob, clmd, framenr);
730                 }
731         }
732         else
733         {       
734                 if(G.rt > 0)
735                         printf("clothModifier_do deltaTime!=1 clmd->clothObject != NULL\n");
736                 if(cloth_read_cache(ob, clmd, framenr))
737                 {
738                         cloth_to_object (ob, clmd, result);
739                         implicit_set_positions(clmd);
740                         clmd->sim_parms->sim_time = current_time;
741                 }
742                 else
743                 {
744                         /* jump to a non-existing frame makes sim reset */
745                         clmd->sim_parms->flags |= CLOTH_SIMSETTINGS_FLAG_RESET;
746                 }
747         }
748         
749         return result;
750 }
751
752 /* frees all */
753 void cloth_free_modifier ( Object *ob, ClothModifierData *clmd )
754 {
755         Cloth   *cloth = NULL;
756         
757         if ( !clmd )
758                 return;
759
760         cloth = clmd->clothObject;
761
762         
763         if ( cloth )
764         {       
765                 // If our solver provides a free function, call it
766                 if ( solvers [clmd->sim_parms->solver_type].free )
767                 {
768                         solvers [clmd->sim_parms->solver_type].free ( clmd );
769                 }
770
771                 // Free the verts.
772                 if ( cloth->verts != NULL )
773                         MEM_freeN ( cloth->verts );
774
775                 cloth->verts = NULL;
776                 cloth->numverts = 0;
777
778                 // Free the springs.
779                 if ( cloth->springs != NULL )
780                 {
781                         LinkNode *search = cloth->springs;
782                         while(search)
783                         {
784                                 ClothSpring *spring = search->link;
785                                                 
786                                 MEM_freeN ( spring );
787                                 search = search->next;
788                         }
789                         BLI_linklist_free(cloth->springs, NULL);
790                 
791                         cloth->springs = NULL;
792                 }
793
794                 cloth->springs = NULL;
795                 cloth->numsprings = 0;
796
797                 // free BVH collision tree
798                 if ( cloth->tree )
799                         bvh_free ( ( BVH * ) cloth->tree );
800
801                 // we save our faces for collision objects
802                 if ( cloth->mfaces )
803                         MEM_freeN ( cloth->mfaces );
804                 
805                 if(cloth->edgehash)
806                         BLI_edgehash_free ( cloth->edgehash, NULL );
807                 
808                 
809                 /*
810                 if(clmd->clothObject->facemarks)
811                 MEM_freeN(clmd->clothObject->facemarks);
812                 */
813                 MEM_freeN ( cloth );
814                 clmd->clothObject = NULL;
815         }
816         clmd->sim_parms->flags &= ~CLOTH_SIMSETTINGS_FLAG_RESET;
817 }
818
819 /* frees all */
820 void cloth_free_modifier_extern ( ClothModifierData *clmd )
821 {
822         Cloth   *cloth = NULL;
823         if(G.rt > 0)
824                 printf("cloth_free_modifier_extern\n");
825         
826         if ( !clmd )
827                 return;
828
829         cloth = clmd->clothObject;
830         
831         if ( cloth )
832         {       
833                 if(G.rt > 0)
834                         printf("cloth_free_modifier_extern in\n");
835                 
836                 // If our solver provides a free function, call it
837                 if ( solvers [clmd->sim_parms->solver_type].free )
838                 {
839                         solvers [clmd->sim_parms->solver_type].free ( clmd );
840                 }
841
842                 // Free the verts.
843                 if ( cloth->verts != NULL )
844                         MEM_freeN ( cloth->verts );
845
846                 cloth->verts = NULL;
847                 cloth->numverts = 0;
848
849                 // Free the springs.
850                 if ( cloth->springs != NULL )
851                 {
852                         LinkNode *search = cloth->springs;
853                         while(search)
854                         {
855                                 ClothSpring *spring = search->link;
856                                                 
857                                 MEM_freeN ( spring );
858                                 search = search->next;
859                         }
860                         BLI_linklist_free(cloth->springs, NULL);
861                 
862                         cloth->springs = NULL;
863                 }
864
865                 cloth->springs = NULL;
866                 cloth->numsprings = 0;
867
868                 // free BVH collision tree
869                 if ( cloth->tree )
870                         bvh_free ( ( BVH * ) cloth->tree );
871
872                 // we save our faces for collision objects
873                 if ( cloth->mfaces )
874                         MEM_freeN ( cloth->mfaces );
875                 
876                 if(cloth->edgehash)
877                         BLI_edgehash_free ( cloth->edgehash, NULL );
878                 
879                 
880                 /*
881                 if(clmd->clothObject->facemarks)
882                 MEM_freeN(clmd->clothObject->facemarks);
883                 */
884                 MEM_freeN ( cloth );
885                 clmd->clothObject = NULL;
886         }
887 }
888
889 /******************************************************************************
890 *
891 * Internal functions.
892 *
893 ******************************************************************************/
894
895 /**
896  * cloth_to_object - copies the deformed vertices to the object.
897  *
898  **/
899 static void cloth_to_object (Object *ob,  ClothModifierData *clmd, DerivedMesh *dm)
900 {
901         unsigned int    i = 0;
902         MVert *mvert = NULL;
903         unsigned int numverts;
904         Cloth *cloth = clmd->clothObject;
905
906         if (clmd->clothObject) {
907                 /* inverse matrix is not uptodate... */
908                 Mat4Invert (ob->imat, ob->obmat);
909
910                 mvert = CDDM_get_verts(dm);
911                 numverts = dm->getNumVerts(dm);
912
913                 for (i = 0; i < numverts; i++)
914                 {
915                         VECCOPY (mvert[i].co, cloth->verts[i].x);
916                         Mat4MulVecfl (ob->imat, mvert[i].co);   /* cloth is in global coords */
917                 }
918         }
919 }
920
921
922 /**
923  * cloth_apply_vgroup - applies a vertex group as specified by type
924  *
925  **/
926 /* can be optimized to do all groups in one loop */
927 static void cloth_apply_vgroup ( ClothModifierData *clmd, DerivedMesh *dm )
928 {
929         unsigned int i = 0;
930         unsigned int j = 0;
931         MDeformVert *dvert = NULL;
932         Cloth *clothObj = NULL;
933         unsigned int numverts = dm->getNumVerts ( dm );
934         float goalfac = 0;
935         ClothVertex *verts = NULL;
936
937         clothObj = clmd->clothObject;
938
939         if ( !dm )
940                 return;
941         
942         numverts = dm->getNumVerts ( dm );
943
944         verts = clothObj->verts;
945         
946         if (((clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_SCALING ) || 
947                      (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL )) && 
948                      ((clmd->sim_parms->vgroup_mass>0) || 
949                      (clmd->sim_parms->vgroup_struct>0)||
950                      (clmd->sim_parms->vgroup_bend>0)))
951         {
952                 for ( i = 0; i < numverts; i++, verts++ )
953                 {
954                         verts->mass = 1.0; // standard mass
955                         
956                         dvert = dm->getVertData ( dm, i, CD_MDEFORMVERT );
957                         if ( dvert )
958                         {
959                                 for ( j = 0; j < dvert->totweight; j++ )
960                                 {
961                                         if (( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_mass-1)) && (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL ))
962                                         {
963                                                 verts->goal = dvert->dw [j].weight;
964                                                 goalfac= 1.0f;
965                                                 
966                                                 /*
967                                                 // Kicking goal factor to simplify things...who uses that anyway?
968                                                 // ABS ( clmd->sim_parms->maxgoal - clmd->sim_parms->mingoal );
969                                                 */
970                                                 
971                                                 verts->goal  = ( float ) pow ( verts->goal , 4.0f );
972                                                 if ( verts->goal >=SOFTGOALSNAP )
973                                                 {
974                                                         verts->flags |= CLOTH_VERT_FLAG_PINNED;
975                                                 }
976                                         }
977                                         
978                                         if (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_SCALING )
979                                         {
980                                                 if( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_struct-1))
981                                                 {
982                                                         verts->struct_stiff = dvert->dw [j].weight;
983                                                         verts->shear_stiff = dvert->dw [j].weight;
984                                                 }
985                                                 
986                                                 if( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_bend-1))
987                                                 {
988                                                         verts->bend_stiff = dvert->dw [j].weight;
989                                                 }
990                                         }
991                                         /*
992                                         // for later
993                                         if( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_weight-1))
994                                         {
995                                                 verts->mass = dvert->dw [j].weight;
996                                         }
997                                         */
998                                 }
999                         }
1000                 }
1001         }
1002 }
1003
1004 static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float framenr)
1005 {
1006         unsigned int i = 0;
1007         MVert *mvert = NULL;
1008         ClothVertex *verts = NULL;
1009         float tnull[3] = {0,0,0};
1010         int cache_there = 0;
1011         Cloth *cloth = NULL;
1012
1013         // If we have a clothObject, free it. 
1014         if ( clmd->clothObject != NULL )
1015         {
1016                 cloth_free_modifier ( ob, clmd );
1017                 if(G.rt > 0)
1018                         printf("cloth_free_modifier cloth_from_object\n");
1019         }
1020
1021         // Allocate a new cloth object.
1022         clmd->clothObject = MEM_callocN ( sizeof ( Cloth ), "cloth" );
1023         if ( clmd->clothObject )
1024         {
1025                 clmd->clothObject->old_solver_type = 255;
1026                 // clmd->clothObject->old_collision_type = 255;
1027                 cloth = clmd->clothObject;
1028                 clmd->clothObject->edgehash = NULL;
1029         }
1030         else if ( !clmd->clothObject )
1031         {
1032                 modifier_setError ( & ( clmd->modifier ), "Out of memory on allocating clmd->clothObject." );
1033                 return 0;
1034         }
1035
1036         // mesh input objects need DerivedMesh
1037         if ( !dm )
1038                 return 0;
1039
1040         cloth_from_mesh ( ob, clmd, dm );
1041
1042         if((clmd->sim_parms->firstcachedframe < 0.0) || ((clmd->sim_parms->firstcachedframe >= 0.0) && (!cloth_read_cache(ob, clmd, clmd->sim_parms->firstcachedframe))))
1043         {
1044                 // no cache there
1045                 cache_there = 0;
1046                 if(G.rt > 0)
1047                         printf("cache_there = 0\n");
1048         }
1049         else
1050         {
1051                 // we have a cache
1052                 cache_there = 1;
1053                 if(G.rt > 0)
1054                         printf("cache_there = 1, fcf: %d\n", clmd->sim_parms->firstcachedframe);
1055         }
1056         
1057         // create springs 
1058         clmd->clothObject->springs = NULL;
1059         clmd->clothObject->numsprings = -1;
1060         
1061         mvert = dm->getVertArray ( dm );
1062         verts = clmd->clothObject->verts;
1063
1064         // set initial values
1065         for ( i = 0; i < dm->getNumVerts(dm); i++, verts++ )
1066         {
1067                 if(!cache_there)
1068                 {
1069                         VECCOPY ( verts->x, mvert[i].co );
1070                         Mat4MulVecfl ( ob->obmat, verts->x );
1071                 }
1072                 
1073                 /* no GUI interface yet */
1074                 verts->mass = clmd->sim_parms->mass = 1.0f;
1075
1076                 if ( clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL )
1077                         verts->goal= clmd->sim_parms->defgoal;
1078                 else
1079                         verts->goal= 0.0f;
1080
1081                 verts->flags = 0;
1082                 VECCOPY ( verts->xold, verts->x );
1083                 VECCOPY ( verts->xconst, verts->x );
1084                 VECCOPY ( verts->txold, verts->x );
1085                 VecMulf ( verts->v, 0.0f );
1086
1087                 verts->impulse_count = 0;
1088                 VECCOPY ( verts->impulse, tnull );
1089         }
1090         
1091         // apply / set vertex groups
1092         // has to be happen before springs are build!
1093         cloth_apply_vgroup (clmd, dm);
1094         
1095         
1096         if ( !cloth_build_springs ( clmd, dm ) )
1097         {
1098                 cloth_free_modifier ( ob, clmd );
1099                 modifier_setError ( & ( clmd->modifier ), "Can't build springs." );
1100                 printf("cloth_free_modifier cloth_build_springs\n");
1101                 return 0;
1102         }
1103         
1104         for ( i = 0; i < dm->getNumVerts(dm); i++)
1105         {
1106                 if((!(cloth->verts[i].flags & CLOTH_VERT_FLAG_PINNED)) && (cloth->verts[i].goal > ALMOST_ZERO))
1107                 {
1108                         cloth_add_spring (clmd, i, i, 0.0, CLOTH_SPRING_TYPE_GOAL);
1109                 }
1110         }
1111         
1112         // init our solver
1113         if ( solvers [clmd->sim_parms->solver_type].init )
1114                 solvers [clmd->sim_parms->solver_type].init ( ob, clmd );
1115         
1116         if(cache_there)
1117                 implicit_set_positions(clmd);
1118
1119         clmd->clothObject->tree = bvh_build_from_cloth ( clmd, clmd->coll_parms->epsilon );
1120
1121         return 1;
1122 }
1123
1124
1125 static void cloth_from_mesh ( Object *ob, ClothModifierData *clmd, DerivedMesh *dm )
1126 {
1127         unsigned int numverts = dm->getNumVerts ( dm );
1128         unsigned int numfaces = dm->getNumFaces ( dm );
1129         MFace *mface = CDDM_get_faces(dm);
1130         unsigned int i = 0;
1131
1132         /* Allocate our vertices. */
1133         clmd->clothObject->numverts = numverts;
1134         clmd->clothObject->verts = MEM_callocN ( sizeof ( ClothVertex ) * clmd->clothObject->numverts, "clothVertex" );
1135         if ( clmd->clothObject->verts == NULL )
1136         {
1137                 cloth_free_modifier ( ob, clmd );
1138                 modifier_setError ( & ( clmd->modifier ), "Out of memory on allocating clmd->clothObject->verts." );
1139                 printf("cloth_free_modifier clmd->clothObject->verts\n");
1140                 return;
1141         }
1142
1143         // save face information
1144         clmd->clothObject->numfaces = numfaces;
1145         clmd->clothObject->mfaces = MEM_callocN ( sizeof ( MFace ) * clmd->clothObject->numfaces, "clothMFaces" );
1146         if ( clmd->clothObject->mfaces == NULL )
1147         {
1148                 cloth_free_modifier ( ob, clmd );
1149                 modifier_setError ( & ( clmd->modifier ), "Out of memory on allocating clmd->clothObject->mfaces." );
1150                 printf("cloth_free_modifier clmd->clothObject->mfaces\n");
1151                 return;
1152         }
1153         for ( i = 0; i < numfaces; i++ )
1154                 memcpy ( &clmd->clothObject->mfaces[i], &mface[i], sizeof ( MFace ) );
1155
1156         /* Free the springs since they can't be correct if the vertices
1157         * changed.
1158         */
1159         if ( clmd->clothObject->springs != NULL )
1160                 MEM_freeN ( clmd->clothObject->springs );
1161
1162 }
1163
1164 /***************************************************************************************
1165 * SPRING NETWORK BUILDING IMPLEMENTATION BEGIN
1166 ***************************************************************************************/
1167
1168 // be carefull: implicit solver has to be resettet when using this one!
1169 // --> only for implicit handling of this spring!
1170 int cloth_add_spring ( ClothModifierData *clmd, unsigned int indexA, unsigned int indexB, float restlength, int spring_type)
1171 {
1172         Cloth *cloth = clmd->clothObject;
1173         ClothSpring *spring = NULL;
1174         
1175         if(cloth)
1176         {
1177                 // TODO: look if this spring is already there
1178                 
1179                 spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
1180                 
1181                 spring->ij = indexA;
1182                 spring->kl = indexB;
1183                 spring->restlen =  restlength;
1184                 spring->type = spring_type;
1185                 spring->flags = 0;
1186                 spring->stiffness = 0;
1187                 
1188                 cloth->numsprings++;
1189         
1190                 BLI_linklist_prepend ( &cloth->springs, spring );
1191                 
1192                 return 1;
1193         }
1194         return 0;
1195 }
1196
1197 int cloth_build_springs ( ClothModifierData *clmd, DerivedMesh *dm )
1198 {
1199         Cloth *cloth = clmd->clothObject;
1200         ClothSpring *spring = NULL, *tspring = NULL, *tspring2 = NULL;
1201         unsigned int struct_springs = 0, shear_springs=0, bend_springs = 0;
1202         unsigned int i = 0;
1203         unsigned int numverts = dm->getNumVerts ( dm );
1204         unsigned int numedges = dm->getNumEdges ( dm );
1205         unsigned int numfaces = dm->getNumFaces ( dm );
1206         MEdge *medge = CDDM_get_edges ( dm );
1207         MFace *mface = CDDM_get_faces ( dm );
1208         unsigned int index2 = 0; // our second vertex index
1209         LinkNode **edgelist = NULL;
1210         EdgeHash *edgehash = NULL;
1211         LinkNode *search = NULL, *search2 = NULL;
1212         float temp[3];
1213         LinkNode *node = NULL, *node2 = NULL;
1214         
1215         // error handling
1216         if ( numedges==0 )
1217                 return 0;
1218
1219         cloth->springs = NULL;
1220
1221         edgelist = MEM_callocN ( sizeof ( LinkNode * ) * numverts, "cloth_edgelist_alloc" );
1222         for ( i = 0; i < numverts; i++ )
1223         {
1224                 edgelist[i] = NULL;
1225         }
1226
1227         if ( cloth->springs )
1228                 MEM_freeN ( cloth->springs );
1229
1230         // create spring network hash
1231         edgehash = BLI_edgehash_new();
1232
1233         // structural springs
1234         for ( i = 0; i < numedges; i++ )
1235         {
1236                 spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
1237
1238                 if ( spring )
1239                 {
1240                         spring->ij = medge[i].v1;
1241                         spring->kl = medge[i].v2;
1242                         VECSUB ( temp, cloth->verts[spring->kl].x, cloth->verts[spring->ij].x );
1243                         spring->restlen =  sqrt ( INPR ( temp, temp ) );
1244                         clmd->sim_parms->avg_spring_len += spring->restlen;
1245                         cloth->verts[spring->ij].avg_spring_len += spring->restlen;
1246                         cloth->verts[spring->kl].avg_spring_len += spring->restlen;
1247                         cloth->verts[spring->ij].spring_count++;
1248                         cloth->verts[spring->kl].spring_count++;
1249                         spring->type = CLOTH_SPRING_TYPE_STRUCTURAL;
1250                         spring->flags = 0;
1251                         spring->stiffness = (cloth->verts[spring->kl].struct_stiff + cloth->verts[spring->ij].struct_stiff) / 2.0;
1252                         struct_springs++;
1253                         
1254                         if(!i)
1255                                 node2 = BLI_linklist_append_fast ( &cloth->springs, spring );
1256                         else
1257                                 node2 = BLI_linklist_append_fast ( &node->next, spring );
1258                         node = node2;
1259                 }
1260         }
1261         
1262         clmd->sim_parms->avg_spring_len /= struct_springs;
1263         
1264         for(i = 0; i < numverts; i++)
1265         {
1266                 cloth->verts[i].avg_spring_len = cloth->verts[i].avg_spring_len * 0.49 / ((float)cloth->verts[i].spring_count);
1267         }
1268         
1269         // shear springs
1270         for ( i = 0; i < numfaces; i++ )
1271         {
1272                 spring = ( ClothSpring *) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
1273
1274                 spring->ij = mface[i].v1;
1275                 spring->kl = mface[i].v3;
1276                 VECSUB ( temp, cloth->verts[spring->kl].x, cloth->verts[spring->ij].x );
1277                 spring->restlen =  sqrt ( INPR ( temp, temp ) );
1278                 spring->type = CLOTH_SPRING_TYPE_SHEAR;
1279                 spring->stiffness = (cloth->verts[spring->kl].shear_stiff + cloth->verts[spring->ij].shear_stiff) / 2.0;
1280
1281                 BLI_linklist_append ( &edgelist[spring->ij], spring );
1282                 BLI_linklist_append ( &edgelist[spring->kl], spring );
1283                 shear_springs++;
1284
1285                 node2 = BLI_linklist_append_fast ( &node->next, spring );
1286                 node = node2;
1287
1288                 if ( mface[i].v4 )
1289                 {
1290                         spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
1291
1292                         spring->ij = mface[i].v2;
1293                         spring->kl = mface[i].v4;
1294                         VECSUB ( temp, cloth->verts[spring->kl].x, cloth->verts[spring->ij].x );
1295                         spring->restlen =  sqrt ( INPR ( temp, temp ) );
1296                         spring->type = CLOTH_SPRING_TYPE_SHEAR;
1297                         spring->stiffness = (cloth->verts[spring->kl].shear_stiff + cloth->verts[spring->ij].shear_stiff) / 2.0;
1298
1299                         BLI_linklist_append ( &edgelist[spring->ij], spring );
1300                         BLI_linklist_append ( &edgelist[spring->kl], spring );
1301                         shear_springs++;
1302
1303                         node2 = BLI_linklist_append_fast ( &node->next, spring );
1304                         node = node2;
1305                 }
1306         }
1307         
1308         // bending springs
1309         search2 = cloth->springs;
1310         for ( i = struct_springs; i < struct_springs+shear_springs; i++ )
1311         {
1312                 if ( !search2 )
1313                         break;
1314
1315                 tspring2 = search2->link;
1316                 search = edgelist[tspring2->kl];
1317                 while ( search )
1318                 {
1319                         tspring = search->link;
1320                         index2 = ( ( tspring->ij==tspring2->kl ) ? ( tspring->kl ) : ( tspring->ij ) );
1321                         
1322                         // check for existing spring
1323                         // check also if startpoint is equal to endpoint
1324                         if ( !BLI_edgehash_haskey ( edgehash, index2, tspring2->ij )
1325                                                    && !BLI_edgehash_haskey ( edgehash, tspring2->ij, index2 )
1326                                                    && ( index2!=tspring2->ij ) )
1327                         {
1328                                 spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
1329
1330                                 spring->ij = tspring2->ij;
1331                                 spring->kl = index2;
1332                                 VECSUB ( temp, cloth->verts[index2].x, cloth->verts[tspring2->ij].x );
1333                                 spring->restlen =  sqrt ( INPR ( temp, temp ) );
1334                                 spring->type = CLOTH_SPRING_TYPE_BENDING;
1335                                 spring->stiffness = (cloth->verts[spring->kl].bend_stiff + cloth->verts[spring->ij].bend_stiff) / 2.0;
1336                                 BLI_edgehash_insert ( edgehash, spring->ij, index2, NULL );
1337                                 bend_springs++;
1338
1339                                 node2 = BLI_linklist_append_fast ( &node->next, spring );
1340                                 node = node2;
1341                         }
1342                         search = search->next;
1343                 }
1344                 search2 = search2->next;
1345         }
1346         
1347         cloth->numsprings = struct_springs + shear_springs + bend_springs;
1348         
1349         for ( i = 0; i < numverts; i++ )
1350         {
1351                 BLI_linklist_free ( edgelist[i],NULL );
1352         }
1353         if ( edgelist )
1354                 MEM_freeN ( edgelist );
1355         
1356         cloth->edgehash = edgehash;
1357         
1358         if(G.rt>0)
1359                 printf("avg_len: %f\n",clmd->sim_parms->avg_spring_len);
1360
1361         return 1;
1362
1363 } /* cloth_build_springs */
1364 /***************************************************************************************
1365 * SPRING NETWORK BUILDING IMPLEMENTATION END
1366 ***************************************************************************************/
1367