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