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