47925e352eb1fc4cfa3c2298a408761dee861624
[blender.git] / source / blender / blenkernel / intern / cloth.c
1 /*  cloth.c      
2
3 *
4 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License.  See http://www.blender.org/BL/ for information
12 * about this.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 *
23 * The Original Code is Copyright (C) Blender Foundation
24 * All rights reserved.
25 *
26 * The Original Code is: all of this file.
27 *
28 * Contributor(s): none yet.
29 *
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
31 */
32
33
34 #include <math.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "MEM_guardedalloc.h"
39
40 /* types */
41 #include "DNA_curve_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_object_force.h"
44 #include "DNA_cloth_types.h"    
45 #include "DNA_key_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_lattice_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_modifier_types.h"
51
52 #include "BLI_blenlib.h"
53 #include "BLI_arithb.h"
54 #include "BLI_edgehash.h"
55 #include "BLI_linklist.h"
56
57 #include "BKE_curve.h"
58 #include "BKE_deform.h"
59 #include "BKE_DerivedMesh.h"
60 #include "BKE_cdderivedmesh.h"
61 #include "BKE_displist.h"
62 #include "BKE_effect.h"
63 #include "BKE_global.h"
64 #include "BKE_key.h"
65 #include "BKE_mesh.h"
66 #include "BKE_object.h"
67 #include "BKE_cloth.h"
68 #include "BKE_modifier.h"
69 #include "BKE_utildefines.h"
70 #include "BKE_DerivedMesh.h"
71 #include "BIF_editdeform.h"
72 #include "BIF_editkey.h"
73 #include "DNA_screen_types.h"
74 #include "BSE_headerbuttons.h"
75 #include "BIF_screen.h"
76 #include "BIF_space.h"
77 #include "mydevice.h"
78
79 #ifdef _WIN32
80 void tstart(void)
81 {
82 }
83 void tend(void)
84 {
85
86 }
87 double tval()
88 {
89         return 0;
90 }
91 #else
92 #include <sys/time.h>
93 static struct timeval _tstart, _tend;
94 static struct timezone tz;
95 void tstart(void)
96 {
97         gettimeofday(&_tstart, &tz);
98 }
99 void tend(void)
100 {
101         gettimeofday(&_tend,&tz);
102 }
103 double tval()
104 {
105         double t1, t2;
106         t1 =  (double)_tstart.tv_sec + (double)_tstart.tv_usec/(1000*1000);
107         t2 =  (double)_tend.tv_sec + (double)_tend.tv_usec/(1000*1000);
108         return t2-t1;
109 }
110 #endif
111
112 /* Our available solvers. */
113 // 255 is the magic reserved number, so NEVER try to put 255 solvers in here!
114 // 254 = MAX!
115 static CM_SOLVER_DEF    solvers [] = {
116         { "Implicit", CM_IMPLICIT, implicit_init, implicit_solver, implicit_free },
117         // { "Implicit C++", CM_IMPLICITCPP, implicitcpp_init, implicitcpp_solver, implicitcpp_free },
118 };
119
120 #define DEBUG_CLOTH_VERBOSE     1000
121 static int      DEBUG_CLOTH = 0;
122
123
124 /* ********** cloth engine ******* */
125 /* Prototypes for internal functions.
126 */
127 static void cloth_to_object (Object *ob, ClothModifierData *clmd, float (*vertexCos)[3], unsigned int numverts);
128 static void cloth_from_mesh (Object *ob, ClothModifierData *clmd, DerivedMesh *dm);
129 static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float (*vertexCos)[3], unsigned int numverts);
130 static int collobj_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float (*vertexCos)[3], unsigned int numverts);
131 int cloth_build_springs(Cloth *cloth, DerivedMesh *dm);
132 static void cloth_apply_vgroup(ClothModifierData *clmd, DerivedMesh *dm, short vgroup);
133
134
135 /******************************************************************************
136 *
137 * External interface called by modifier.c clothModifier functions.
138 *
139 ******************************************************************************/
140 /**
141 * cloth_init -  creates a new cloth simulation.
142 *
143 * 1. create object
144 * 2. fill object with standard values or with the GUI settings if given 
145 */
146 void cloth_init (ClothModifierData *clmd)
147 {
148         /* Initialize our new data structure to reasonable values. */
149         clmd->sim_parms.gravity [0] = 0.0;
150         clmd->sim_parms.gravity [1] = 0.0;
151         clmd->sim_parms.gravity [2] = -9.81;
152         clmd->sim_parms.structural = 100.0;
153         clmd->sim_parms.shear = 100.0;
154         clmd->sim_parms.bending = 1.0;
155         clmd->sim_parms.Cdis = 5.0;
156         clmd->sim_parms.Cvi = 1.0;
157         clmd->sim_parms.mass = 1.0f;
158         clmd->sim_parms.stepsPerFrame = 5;
159         clmd->sim_parms.sim_time = 1.0;
160         clmd->sim_parms.flags = CSIMSETT_FLAG_RESET;
161         clmd->sim_parms.solver_type = 0; 
162         clmd->sim_parms.preroll = 0;
163         clmd->sim_parms.maxspringlen = 10;
164         clmd->sim_parms.firstframe = 1;
165         clmd->sim_parms.lastframe = 250;
166         clmd->coll_parms.self_friction = 5.0;
167         clmd->coll_parms.friction = 10.0;
168         clmd->coll_parms.loop_count = 1;
169         clmd->coll_parms.epsilon = 0.01f;
170         
171         /* These defaults are copied from softbody.c's
172         * softbody_calc_forces() function.
173         */
174         clmd->sim_parms.eff_force_scale = 1000.0;
175         clmd->sim_parms.eff_wind_scale = 250.0;
176
177         // also from softbodies
178         clmd->sim_parms.maxgoal = 1.0f;
179         clmd->sim_parms.mingoal = 0.0f;
180         clmd->sim_parms.defgoal = 0.7f;
181         clmd->sim_parms.goalspring = 100.0f;
182         clmd->sim_parms.goalfrict = 0.0f;
183
184         clmd->sim_parms.cache = NULL;
185 }
186
187 // unused in the moment, cloth needs quads from mesh
188 DerivedMesh *CDDM_convert_to_triangle(DerivedMesh *dm)
189 {
190         DerivedMesh *result = NULL;
191         int i;
192         int numverts = dm->getNumVerts(dm);
193         int numedges = dm->getNumEdges(dm);
194         int numfaces = dm->getNumFaces(dm);
195
196         MVert *mvert = CDDM_get_verts(dm);
197         MEdge *medge = CDDM_get_edges(dm);
198         MFace *mface = CDDM_get_faces(dm);
199
200         MVert *mvert2;
201         MFace *mface2;
202         unsigned int numtris=0;
203         unsigned int numquads=0;
204         int a = 0;
205         int random = 0;
206         int firsttime = 0;
207         float vec1[3], vec2[3], vec3[3], vec4[3], vec5[3];
208         float mag1=0, mag2=0;
209
210         for(i = 0; i < numfaces; i++)
211         {
212                 if(mface[i].v4)
213                         numquads++;
214                 else
215                         numtris++;      
216         }
217
218         result = CDDM_from_template(dm, numverts, 0, numtris + 2*numquads);
219
220         if(!result)
221                 return NULL;
222
223         // do verts
224         mvert2 = CDDM_get_verts(result);
225         for(a=0; a<numverts; a++) 
226         {
227                 MVert *inMV;
228                 MVert *mv = &mvert2[a];
229
230                 inMV = &mvert[a];
231
232                 DM_copy_vert_data(dm, result, a, a, 1);
233                 *mv = *inMV;
234         }
235
236
237         // do faces
238         mface2 = CDDM_get_faces(result);
239         for(a=0, i=0; a<numfaces; a++) 
240         {
241                 MFace *mf = &mface2[i];
242                 MFace *inMF;
243                 inMF = &mface[a];
244
245                 /*
246                 DM_copy_face_data(dm, result, a, i, 1);
247
248                 *mf = *inMF;
249                 */
250
251                 if(mface[a].v4 && random==1)
252                 {
253                         mf->v1 = mface[a].v2;
254                         mf->v2 = mface[a].v3;
255                         mf->v3 = mface[a].v4;
256                 }
257                 else
258                 {
259                         mf->v1 = mface[a].v1;
260                         mf->v2 = mface[a].v2;
261                         mf->v3 = mface[a].v3;
262                 }
263
264                 mf->v4 = 0;
265                 mf->flag |= ME_SMOOTH;
266
267                 test_index_face(mf, NULL, 0, 3);
268
269                 if(mface[a].v4)
270                 {
271                         MFace *mf2;
272
273                         i++;
274
275                         mf2 = &mface2[i];
276                         /*
277                         DM_copy_face_data(dm, result, a, i, 1);
278
279                         *mf2 = *inMF;
280                         */
281
282                         if(random==1)
283                         {
284                                 mf2->v1 = mface[a].v1;
285                                 mf2->v2 = mface[a].v2;
286                                 mf2->v3 = mface[a].v4;
287                         }
288                         else
289                         {
290                                 mf2->v1 = mface[a].v4;
291                                 mf2->v2 = mface[a].v1;
292                                 mf2->v3 = mface[a].v3;
293                         }
294                         mf2->v4 = 0;
295                         mf2->flag |= ME_SMOOTH;
296
297                         test_index_face(mf2, NULL, 0, 3);
298                 }
299
300                 i++;
301         }
302
303         CDDM_calc_edges(result);
304         CDDM_calc_normals(result);
305
306         return result;
307
308 }
309
310
311 DerivedMesh *CDDM_create_tearing(ClothModifierData *clmd, DerivedMesh *dm)
312 {
313         DerivedMesh *result = NULL;
314         unsigned int i = 0, a = 0, j=0;
315         int numverts = dm->getNumVerts(dm);
316         int numedges = dm->getNumEdges(dm);
317         int numfaces = dm->getNumFaces(dm);
318
319         MVert *mvert = CDDM_get_verts(dm);
320         MEdge *medge = CDDM_get_edges(dm);
321         MFace *mface = CDDM_get_faces(dm);
322
323         MVert *mvert2;
324         MFace *mface2;
325         unsigned int numtris=0;
326         unsigned int numquads=0;
327         EdgeHash *edgehash = NULL;
328         Cloth *cloth = clmd->clothObject;
329         ClothSpring *springs = cloth->springs;
330         unsigned int numsprings = cloth->numsprings;
331         
332         // create spring tearing hash
333         edgehash = BLI_edgehash_new();
334         
335         for(i = 0; i < numsprings; i++)
336         {
337                 if((springs[i].flags & CSPRING_FLAG_DEACTIVATE)
338                 &&(!BLI_edgehash_haskey(edgehash, springs[i].ij, springs[i].kl)))
339                 {
340                         BLI_edgehash_insert(edgehash, springs[i].ij, springs[i].kl, NULL);
341                         BLI_edgehash_insert(edgehash, springs[i].kl, springs[i].ij, NULL);
342                         j++;
343                 }
344         }
345         
346         // printf("found %d tears\n", j);
347         
348         result = CDDM_from_template(dm, numverts, 0, numfaces);
349
350         if(!result)
351                 return NULL;
352
353         // do verts
354         mvert2 = CDDM_get_verts(result);
355         for(a=0; a<numverts; a++) 
356         {
357                 MVert *inMV;
358                 MVert *mv = &mvert2[a];
359
360                 inMV = &mvert[a];
361
362                 DM_copy_vert_data(dm, result, a, a, 1);
363                 *mv = *inMV;
364         }
365
366
367         // do faces
368         mface2 = CDDM_get_faces(result);
369         for(a=0, i=0; a<numfaces; a++) 
370         {
371                 MFace *mf = &mface2[i];
372                 MFace *inMF;
373                 inMF = &mface[a];
374
375                 /*
376                 DM_copy_face_data(dm, result, a, i, 1);
377
378                 *mf = *inMF;
379                 */
380                 
381                 if((!BLI_edgehash_haskey(edgehash, mface[a].v1, mface[a].v2))
382                 &&(!BLI_edgehash_haskey(edgehash, mface[a].v2, mface[a].v3))
383                 &&(!BLI_edgehash_haskey(edgehash, mface[a].v3, mface[a].v4))
384                 &&(!BLI_edgehash_haskey(edgehash, mface[a].v4, mface[a].v1)))
385                 {
386                         mf->v1 = mface[a].v1;
387                         mf->v2 = mface[a].v2;
388                         mf->v3 = mface[a].v3;
389                         mf->v4 = mface[a].v4;
390         
391                         test_index_face(mf, NULL, 0, 4);
392         
393                         i++;
394                 }
395         }
396
397         CDDM_lower_num_faces(result, i);
398         CDDM_calc_edges(result);
399         CDDM_calc_normals(result);
400         
401         BLI_edgehash_free(edgehash, NULL);
402
403         return result;
404 }
405
406
407 int cloth_cache_search_frame(ClothModifierData *clmd, float time)
408 {
409         Frame *frame = NULL;
410         LinkNode *search = NULL;
411         int newtime = time + clmd->sim_parms.preroll;
412
413         Cloth *cloth = NULL;
414
415         if(!clmd)
416                 return 0;
417
418         cloth = clmd->clothObject;
419
420         if(!cloth)
421                 return 0;
422
423         if(clmd->sim_parms.cache)
424         {               
425                 search = clmd->sim_parms.cache;
426
427                 // check if frame exists
428                 while(search)
429                 {
430                         frame = search->link;
431
432                         if(frame->time == newtime)
433                                 break;
434
435                         frame = NULL;
436
437                         search = search->next;   
438                 }
439         }       
440
441         if(!frame) 
442                 return 0;
443
444         return 1;
445 }
446
447 int cloth_cache_last_frame(ClothModifierData *clmd)
448 {
449         Frame *frame = NULL;
450         LinkNode *search = NULL;
451         int temptime = 0;
452
453         Cloth *cloth = NULL;
454
455         if(!clmd)
456                 return 0;
457
458         cloth = clmd->clothObject;
459
460         if(!cloth)
461                 return 0;
462
463         if(clmd->sim_parms.cache)
464         {               
465                 search = clmd->sim_parms.cache;
466
467                 // check if frame exists
468                 while(search)
469                 {
470                         frame = search->link;
471
472                         if(frame->time > temptime)
473                         {
474                                 temptime = frame->time;
475                         }
476
477                         search = search->next;
478                 }
479         }       
480
481         return temptime;
482 }
483
484 void cloth_cache_get_frame(ClothModifierData *clmd, float time)
485 {
486         Frame *frame = NULL;
487         LinkNode *search = NULL;
488         unsigned int i = 0;
489         Cloth *cloth = NULL;
490         int newtime = time + clmd->sim_parms.preroll;
491
492         if(clmd)
493         {
494                 cloth = clmd->clothObject;
495
496                 if(!cloth)
497                         return;
498
499                 // get cache
500                 if(clmd->sim_parms.cache)
501                 {
502                         search = clmd->sim_parms.cache;
503                         frame = NULL;
504                         // check if frame exists
505                         while(search)
506                         {
507                                 frame = search->link;
508                                 if(frame->time == newtime)
509                                         break;
510
511                                 frame = NULL;
512
513                                 search = search->next;   
514                         }
515
516                         if(frame)
517                         {
518                                 if(frame->verts)
519                                 {
520
521                                         // copy ClothVertex struct
522                                         memcpy(cloth->verts, frame->verts, cloth->numverts*sizeof(ClothVertex));
523                                         implicit_set_positions(clmd);
524                                 }
525                                 
526                                 if(frame->springs)
527                                 {
528                                         // copy ClothSpring struct
529                                         memcpy(cloth->springs, frame->springs, cloth->numsprings*sizeof(ClothSpring));
530                                 }
531                         }
532                 }
533         }
534
535 }
536
537 void cloth_cache_set_frame(ClothModifierData *clmd, float time)
538 {
539         Frame *frame = NULL;
540         unsigned int i = 0;
541         Cloth *cloth = NULL;
542         int newtime = time + clmd->sim_parms.preroll;
543
544         if(clmd)
545         {
546                 cloth = clmd->clothObject;
547
548                 if(cloth)
549                 {
550                         // creat new frame cache
551                         frame = (Frame *)MEM_callocN(sizeof(Frame), "cloth frame cache");
552                         frame->verts = (ClothVertex *)MEM_callocN(sizeof(ClothVertex)*cloth->numverts, "cloth frame vertex cache");
553                         frame->springs = (ClothSpring *)MEM_callocN(sizeof(ClothSpring)*cloth->numsprings, "cloth frame spring cache");
554                         frame->time = newtime;
555
556                         // copy ClothVertex struct
557                         for(i = 0; i < cloth->numverts; i++)
558                         {
559                                 memcpy(&frame->verts[i], &cloth->verts[i], sizeof(ClothVertex));
560                         }
561                         
562                         // copy ClothSpring struct
563                         for(i = 0; i < cloth->numsprings; i++)
564                         {
565                                 memcpy(&frame->springs[i], &cloth->springs[i], sizeof(ClothSpring));
566                         }
567                         
568                 }
569                 if(frame)
570                 {
571                         if(!clmd->sim_parms.cache)
572                                 BLI_linklist_prepend(&clmd->sim_parms.cache, frame);
573                         else
574                                 BLI_linklist_append(&clmd->sim_parms.cache, frame);
575                 }
576         }
577 }
578
579 void cloth_cache_free(ClothModifierData *clmd, float time)
580 {
581         Frame *frame = NULL;
582         LinkNode *search, *last_search;
583         int newtime = time + clmd->sim_parms.preroll;
584
585         // do never free first cached frame
586         if((newtime<1.0f) && !(clmd->sim_parms.flags & CSIMSETT_FLAG_CCACHE_FREE_ALL))
587                 return;
588
589         /* Calls the solver and collision frees first as they
590         * might depend on data in clmd->clothObject. */
591
592         if (clmd) 
593         {
594                 if(clmd->sim_parms.cache)
595                 {                       
596                         last_search = search = clmd->sim_parms.cache;
597                         while(search)
598                         {
599                                 LinkNode *next= search->next;
600                                 frame = search->link;
601
602                                 // free part of cache, but not preroll cache and first framer
603                                 if((clmd->sim_parms.flags & CSIMSETT_FLAG_CCACHE_FREE_PART)  
604                                         && (frame->time > newtime)) // do not delete the first frame
605                                 {
606                                         MEM_freeN(frame->verts);
607                                         MEM_freeN(frame->springs);
608                                         MEM_freeN(frame);       
609                                         MEM_freeN(search);
610                                         last_search->next = next;
611                                 }
612                                 else if(clmd->sim_parms.flags & CSIMSETT_FLAG_CCACHE_FREE_ALL) // free COMPLETE cache
613                                 {
614                                         MEM_freeN(frame->verts);
615                                         MEM_freeN(frame->springs);
616                                         MEM_freeN(frame);       
617                                 }
618                                 else
619                                         last_search = search;
620                                 search = next;
621                         }
622
623                         if(clmd->sim_parms.flags & CSIMSETT_FLAG_CCACHE_FREE_ALL)
624                         {
625                                 BLI_linklist_free(clmd->sim_parms.cache,NULL); 
626                                 clmd->sim_parms.cache = NULL;
627                         }
628                 }
629         }
630
631         /* clear flags */
632         clmd->sim_parms.flags &= ~CSIMSETT_FLAG_CCACHE_FREE_ALL;
633         clmd->sim_parms.flags &= ~CSIMSETT_FLAG_CCACHE_FREE_PART;
634
635 }
636
637
638 /**
639 * cloth_deform_verts - simulates one step, framenr is in frames.
640
641 **/
642 void clothModifier_do(ClothModifierData *clmd, Object *ob, DerivedMesh *dm,
643                 float (*vertexCos)[3], int numverts)
644 {
645         unsigned int i;
646         unsigned int numedges = -1;
647         unsigned int numfaces = -1;
648         MVert *mvert = NULL;
649         MEdge *medge = NULL;
650         MFace *mface = NULL;
651         DerivedMesh *result = NULL, *result2 = NULL;
652         Cloth *cloth = clmd->clothObject;
653         unsigned int framenr = (float)G.scene->r.cfra;
654         float current_time = bsystem_time(ob, (float)G.scene->r.cfra, 0.0);
655         ListBase        *effectors = NULL;
656         ClothVertex *newframe= NULL, *verts;
657         Frame *frame = NULL;
658         LinkNode *search = NULL;
659         float deltaTime = current_time - clmd->sim_parms.sim_time;      
660         
661         // only be active during a specific period
662         if(current_time < clmd->sim_parms.firstframe)
663                 return;
664         else if(current_time > clmd->sim_parms.lastframe)
665         {
666                 int frametime = cloth_cache_last_frame(clmd);
667                 if(cloth_cache_search_frame(clmd, frametime))
668                 {
669                         cloth_cache_get_frame(clmd, frametime);
670                         cloth_to_object (ob, clmd, vertexCos, numverts);
671                 }
672                 return;
673         }
674         else if(ABS(deltaTime) >= 2.0f ) // no timewarps allowed
675         {
676                 if(!cloth_cache_search_frame(clmd, framenr))
677                         return;
678         }
679         
680         // unused in the moment
681         clmd->sim_parms.dt = 1.0f / clmd->sim_parms.stepsPerFrame;
682         
683         clmd->sim_parms.sim_time = current_time;
684         
685         // check if cloth object was some collision object before and needs freeing now
686         if (!(clmd->sim_parms.flags & CSIMSETT_FLAG_COLLOBJ) && (clmd->clothObject != NULL) && (clmd->clothObject->old_solver_type == 255))
687         {
688                 // temporary set CSIMSETT_FLAG_COLLOBJ flag for proper freeing 
689                 clmd->sim_parms.flags |= CSIMSETT_FLAG_COLLOBJ;
690                 cloth_free_modifier(clmd);
691                 clmd->sim_parms.flags &= ~CSIMSETT_FLAG_COLLOBJ;
692         }
693
694         // This is for collisions objects: check special case CSIMSETT_FLAG_COLLOBJ
695         if (clmd->sim_parms.flags & CSIMSETT_FLAG_COLLOBJ)
696         {                               
697                 
698                 // save next position + time            
699                 if ((clmd->clothObject == NULL) || (numverts != clmd->clothObject->numverts) )
700                 {
701                         if(!collobj_from_object (ob, clmd, dm, vertexCos, framenr))
702                                 return;
703
704                         if(clmd->clothObject == NULL)
705                                 return;
706
707                         cloth = clmd->clothObject;
708                 }
709
710                 // Save old position 
711                 clmd->sim_parms.sim_time_old = clmd->sim_parms.sim_time;
712                 clmd->sim_parms.sim_time = current_time; 
713                 
714                 verts = cloth->verts;
715
716                 for (i = 0; i < clmd->clothObject->numverts; i++, verts++)
717                 {
718                         // Save the previous position. 
719                         VECCOPY (verts->xold, verts->x);
720                         VECCOPY (verts->txold, verts->x);
721
722                         // Get the current position. 
723                         VECCOPY (verts->x, vertexCos[i]);
724                         Mat4MulVecfl(ob->obmat, verts->x);
725
726                         // Compute the vertices velocity. 
727                         VECSUB (verts->v, verts->x, verts->xold);
728                 }
729                 
730                 return;
731         }       
732
733         if(deltaTime == 1.0f)
734         {
735                 if ((clmd->clothObject == NULL) || (numverts != clmd->clothObject->numverts) ) 
736                 {
737                         if(!cloth_from_object (ob, clmd, dm, vertexCos, numverts))
738                                 return;
739
740                         if(clmd->clothObject == NULL)
741                                 return;
742
743                         cloth = clmd->clothObject;
744                 }
745
746                 clmd->clothObject->old_solver_type = clmd->sim_parms.solver_type;
747
748                 // Insure we have a clmd->clothObject, in case allocation failed.
749                 if (clmd->clothObject != NULL) 
750                 {            
751                         if(!cloth_cache_search_frame(clmd, framenr))
752                         {
753                                 verts = cloth->verts;
754                                 
755                                 /* Force any pinned verts to their constrained location. */
756                                 for (i = 0; i < clmd->clothObject->numverts; i++, verts++)
757                                 {
758                                         /* Save the previous position. */
759                                         VECCOPY (verts->xold, verts->xconst);
760                                         VECCOPY (verts->txold, verts->x);
761
762                                         /* Get the current position. */
763                                         VECCOPY (verts->xconst, vertexCos[i]);
764                                         Mat4MulVecfl(ob->obmat, verts->xconst);
765
766                                         /* Compute the vertices velocity. */
767                                         VECSUB (verts->v, verts->x, verts->xold);
768                                 }
769
770                                 tstart();
771
772                                 /* Call the solver. */
773                                 if (solvers [clmd->sim_parms.solver_type].solver)
774                                         solvers [clmd->sim_parms.solver_type].solver (ob, framenr, clmd, effectors,0,0);
775
776                                 tend();
777                                 printf("Cloth simulation time: %f\n", (float)tval());
778
779                                 cloth_cache_set_frame(clmd, framenr);
780
781                         }
782                         else // just retrieve the cached frame
783                         {
784                                 cloth_cache_get_frame(clmd, framenr);
785                         }
786
787                         // Copy the result back to the object.
788                         cloth_to_object (ob, clmd, vertexCos, numverts);
789                         
790                         // bvh_free(clmd->clothObject->tree);
791                         // clmd->clothObject->tree = bvh_build(clmd, clmd->coll_parms.epsilon);
792                 } 
793
794         }
795         else if((deltaTime <= 0.0f)||(deltaTime > 1.0f))
796         {
797                 if(cloth_cache_search_frame(clmd, framenr))
798                 {
799                         cloth_cache_get_frame(clmd, framenr);
800                         cloth_to_object (ob, clmd, vertexCos, numverts);
801                 }
802         }
803 }
804
805 /* frees all */
806 void cloth_free_modifier (ClothModifierData *clmd)
807 {
808         Cloth   *cloth = NULL;
809
810         if(!clmd)
811                 return;
812
813         cloth = clmd->clothObject;
814
815         // free our frame cache
816         clmd->sim_parms.flags |= CSIMSETT_FLAG_CCACHE_FREE_ALL;
817         cloth_cache_free(clmd, 0);
818
819         if (cloth) 
820         {       
821                 // If our solver provides a free function, call it
822                 if (cloth->old_solver_type < 255 && solvers [cloth->old_solver_type].free) 
823                 {       
824                         solvers [cloth->old_solver_type].free (clmd);
825                 }
826                 
827                 // Free the verts.
828                 if (cloth->verts != NULL)
829                         MEM_freeN (cloth->verts);
830
831                 cloth->verts = NULL;
832                 cloth->numverts = -1;
833                 
834                 // Free the springs.
835                 if (cloth->springs != NULL)
836                         MEM_freeN (cloth->springs);
837
838                 cloth->springs = NULL;
839                 cloth->numsprings = -1;         
840                 
841                 // free BVH collision tree
842                 if(cloth->tree)
843                         bvh_free((BVH *)cloth->tree);
844                 
845                 // we save our faces for collision objects
846                 if(cloth->mfaces)
847                         MEM_freeN(cloth->mfaces);
848         
849                 if(clmd->clothObject->facemarks)
850                         MEM_freeN(clmd->clothObject->facemarks);
851                 
852                 MEM_freeN (cloth);
853                 clmd->clothObject = NULL;
854         }
855 }
856
857
858 /******************************************************************************
859 *
860 * Internal functions.
861 *
862 ******************************************************************************/
863
864 /**
865 * cloth_to_object - copies the deformed vertices to the object.
866 *
867 * This function is a modified version of the softbody.c:softbody_to_object() function.
868 **/
869 static void cloth_to_object (Object *ob, ClothModifierData *clmd, float (*vertexCos)[3], unsigned int numverts)
870 {
871         ClothVertex     *verts = NULL;
872         unsigned int    i = 0;
873
874         if (clmd->clothObject) {
875                 verts = clmd->clothObject->verts;
876
877                 /* inverse matrix is not uptodate... */
878                 Mat4Invert (ob->imat, ob->obmat);
879
880                 for (i = 0; i < numverts; i++, verts++)
881                 {
882                         VECCOPY (vertexCos[i], verts->x);
883                         Mat4MulVecfl (ob->imat, vertexCos[i]);  /* softbody is in global coords */
884                 }
885         }
886 }
887
888
889 /**
890 * cloth_apply_vgroup - applies a vertex group as specified by type
891 *
892 **/
893 static void cloth_apply_vgroup(ClothModifierData *clmd, DerivedMesh *dm, short vgroup)
894 {
895         unsigned int i = 0;
896         unsigned int j = 0;
897         MDeformVert *dvert = NULL;
898         Cloth *clothObj = NULL;
899         unsigned int numverts = dm->getNumVerts(dm);
900         float goalfac = 0;
901         ClothVertex *verts = NULL;
902
903         clothObj = clmd->clothObject;
904         
905         if(!dm)
906                 return;
907         
908         numverts = dm->getNumVerts(dm);
909
910         /* vgroup is 1 based, decrement so we can match the right group. */
911         --vgroup;
912         
913         verts = clothObj->verts;
914
915         for (i = 0; i < numverts; i++, verts++)
916         {                               
917                 /* so this will definily be below SOFTGOALSNAP */
918                 verts->goal= 0.0f; 
919                 
920                 // LATER ON, support also mass painting here
921                 if(clmd->sim_parms.flags & CSIMSETT_FLAG_GOAL) 
922                 {                       
923                         dvert = dm->getVertData(dm, i, CD_MDEFORMVERT);
924                         if(dvert)       
925                         {               
926                                 for(j = 0; j < dvert->totweight; j++) 
927                                 {
928                                         if(dvert->dw[j].def_nr == vgroup) 
929                                         {
930                                                 verts->goal = dvert->dw [j].weight;
931
932                                                 goalfac= ABS(clmd->sim_parms.maxgoal - clmd->sim_parms.mingoal);
933                                                 verts->goal  = (float)pow(verts->goal , 4.0f);
934
935                                                 if(dvert->dw [j].weight >=SOFTGOALSNAP)
936                                                 {
937                                                         verts->flags |= CVERT_FLAG_PINNED;
938                                                 }
939
940                                                 // TODO enable mass painting here, for the moment i let "goals" go first
941
942                                                 break;
943                                         }
944                                 }
945                         }
946                 }
947         }
948 }
949
950 // only meshes supported at the moment
951 /* collision objects */
952 static int collobj_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float (*vertexCos)[3], unsigned int numverts)
953 {
954         unsigned int i;
955         MVert *mvert = NULL; 
956         ClothVertex *verts = NULL;
957         
958         /* If we have a clothObject, free it. */
959         if (clmd->clothObject != NULL)
960                 cloth_free_modifier (clmd);
961
962         /* Allocate a new cloth object. */
963         clmd->clothObject = MEM_callocN (sizeof(Cloth), "cloth");
964         if (clmd->clothObject) 
965         {
966                 clmd->clothObject->old_solver_type = -1;
967                 clmd->clothObject->old_collision_type = -1;
968         }
969         else if (clmd->clothObject == NULL) 
970         {
971                 modifier_setError (&(clmd->modifier), "Out of memory on allocating clmd->clothObject.");
972                 return 0;
973         }
974
975         switch (ob->type)
976         {
977         case OB_MESH:
978                 
979                 // mesh input objects need DerivedMesh
980                 if(!dm)
981                         return 0;
982                 
983                 cloth_from_mesh (ob, clmd, dm);
984                 
985                 if (clmd->clothObject != NULL) 
986                 {
987                         mvert = CDDM_get_verts(dm);
988                         verts = clmd->clothObject->verts;
989                         
990                         for (i = 0; i < numverts; i++, verts++)
991                         {
992                                 VECCOPY (verts->x, mvert[i].co);
993                                 Mat4MulVecfl(ob->obmat, verts->x);
994                                 verts->flags = 0;
995                                 VECCOPY(verts->xold, verts->x);
996                                 VECCOPY(verts->txold, verts->x);
997                                 VECCOPY(verts->tx, verts->x);
998                                 VecMulf(verts->v, 0.0f);                                
999                         }
1000                         clmd->clothObject->tree =  bvh_build(clmd,clmd->coll_parms.epsilon);
1001                         
1002                 }
1003
1004                 return 1;
1005         default: return 0; // TODO - we do not support changing meshes
1006         }
1007 }
1008
1009 /*
1010 helper function to get proper spring length 
1011 when object is rescaled
1012 */
1013 float cloth_globallen(float *v1,float *v2,Object *ob)
1014 {
1015         float p1[3],p2[3];
1016         VECCOPY(p1,v1);
1017         Mat4MulVecfl(ob->obmat, p1);    
1018         VECCOPY(p2,v2);
1019         Mat4MulVecfl(ob->obmat, p2);
1020         return VecLenf(p1,p2);
1021 }
1022
1023 static void curve_surf_to_cloth(Object *ob, ClothModifierData *clmd, float (*vertexCos)[3])
1024 {
1025         Curve *cu= ob->data;
1026         Nurb *nu;
1027         BezTriple *bezt;
1028         float goalfac;
1029         unsigned int a, curindex=0, i=0;
1030         unsigned int numverts, numsprings = 0, setgoal=0;
1031         Cloth   *clothObj;
1032         ClothVertex *verts = NULL;
1033         
1034         clmd->clothObject->numverts = numverts= count_curveverts(&cu->nurb);
1035         clothObj = clmd->clothObject;
1036         
1037         if(ob->type==OB_CURVE) 
1038         {
1039                 numsprings = numverts - BLI_countlist(&cu->nurb);
1040         }
1041         
1042         /* Allocate our vertices.
1043         */
1044         clmd->clothObject->numverts = numverts;
1045         clmd->clothObject->verts = MEM_callocN (sizeof (ClothVertex) * clmd->clothObject->numverts, "clothVertex");
1046         if (clmd->clothObject->verts == NULL) 
1047         {
1048                 cloth_free_modifier (clmd);
1049                 modifier_setError (&(clmd->modifier), "Out of memory on allocating clmd->clothObject->verts.");
1050                 return;
1051         }
1052         
1053         verts = clmd->clothObject->verts;
1054         
1055         // copy vertex positions
1056         for (i = 0; i < numverts; i++)
1057         {
1058                 VECCOPY (verts->x, vertexCos[i]);
1059                 Mat4MulVecfl(ob->obmat, verts->x);
1060
1061                 verts->mass = clmd->sim_parms.mass;
1062                 // verts->goal= clmd->sim_parms.defgoal;
1063                 verts->flags = 0;
1064                 VECCOPY(verts->xold, verts->x);
1065                 VECCOPY(verts->xconst, verts->x);
1066                 VECCOPY(verts->txold, verts->x);
1067                 VecMulf(verts->v, 0.0f);
1068         }
1069         
1070         clmd->clothObject->mfaces = NULL; // update face pointer
1071         clmd->clothObject->numfaces = 0;
1072         
1073         clmd->clothObject->springs = MEM_callocN (sizeof (ClothSpring) * (numsprings), "cloth_springs_alloc");
1074                 
1075         // set vars now 
1076         goalfac= ABS(clmd->sim_parms.maxgoal - clmd->sim_parms.mingoal);
1077         // clothObj->verts [i].goal = clmd->sim_parms.mingoal + bezt->weight*goalfac;
1078         
1079         /* apply / set vertex groups */
1080         if(clmd->sim_parms.flags & CSIMSETT_FLAG_GOAL) 
1081         {
1082                 if (clmd->sim_parms.vgroup_mass > 0)
1083                 {
1084                         setgoal = 1;
1085                 }
1086         }
1087                 
1088 /*      
1089         for(nu= cu->nurb.first; nu; nu= nu->next) 
1090         {
1091                 if(nu->bezt) 
1092                 {
1093                         for(bezt=nu->bezt, a=0; a<nu->pntsu; a++, bezt++, bp+=3, curindex+=3) 
1094                         {
1095                                 if(setgoal) 
1096                                 {
1097                                         bp->goal= sb->mingoal + bezt->weight*goalfac;
1098                                         // a little ad hoc changing the goal control to be less *sharp*
1099                                         bp->goal = (float)pow(bp->goal, 4.0f);
1100                                         
1101                                         // all three triples
1102                                         (bp+1)->goal= bp->goal;
1103                                         (bp+2)->goal= bp->goal;
1104                                 }
1105                                 
1106                                 if(totspring) 
1107                                 {
1108                                         if(a>0) 
1109                                         {
1110                                                 bs->v1= curindex-1;
1111                                                 bs->v2= curindex;
1112                                                 bs->strength= 1.0;
1113                                                 bs->order=1;
1114                                                 bs->len= globallen( (bezt-1)->vec[2], bezt->vec[0], ob );
1115                                                 bs++;
1116                                         }
1117                                         bs->v1= curindex;
1118                                         bs->v2= curindex+1;
1119                                         bs->strength= 1.0;
1120                                         bs->order=1;
1121                                         bs->len= globallen( bezt->vec[0], bezt->vec[1], ob );
1122                                         bs++;
1123                                         
1124                                         bs->v1= curindex+1;
1125                                         bs->v2= curindex+2;
1126                                         bs->strength= 1.0;
1127                                         bs->order=1;
1128                                         bs->len= globallen( bezt->vec[1], bezt->vec[2], ob );
1129                                         bs++;
1130                                 }
1131                         }
1132                 }
1133                 else {
1134                         for(bpnt=nu->bp, a=0; a<nu->pntsu*nu->pntsv; a++, bpnt++, bp++, curindex++) 
1135                         {
1136                                 if(setgoal) 
1137                                 {
1138                                         bp->goal= sb->mingoal + bpnt->weight*goalfac;
1139                                         // a little ad hoc changing the goal control to be less *sharp*
1140                                         bp->goal = (float)pow(bp->goal, 4.0f);
1141                                 }
1142                                 if(totspring && a>0) 
1143                                 {
1144                                         bs->v1= curindex-1;
1145                                         bs->v2= curindex;
1146                                         bs->strength= 1.0;
1147                                         bs->order=1;
1148                                         bs->len= globallen( (bpnt-1)->vec, bpnt->vec , ob );
1149                                         bs++;
1150                                 }
1151                         }
1152                 }
1153         }
1154         */
1155 }
1156                 
1157 // only meshes supported at the moment
1158 static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float (*vertexCos)[3], unsigned int numverts)
1159 {
1160         unsigned int i = 0;
1161         // dm->getNumVerts(dm);
1162         MVert *mvert = NULL; // CDDM_get_verts(dm);
1163         ClothVertex *verts = NULL;
1164         
1165         /* If we have a clothObject, free it. */
1166         if (clmd->clothObject != NULL)
1167                 cloth_free_modifier (clmd);
1168
1169         /* Allocate a new cloth object. */
1170         clmd->clothObject = MEM_callocN (sizeof(Cloth), "cloth");
1171         if (clmd->clothObject) 
1172         {
1173                 clmd->clothObject->old_solver_type = -1;
1174                 clmd->clothObject->old_collision_type = -1;
1175         }
1176         else if (clmd->clothObject == NULL) 
1177         {
1178                 modifier_setError (&(clmd->modifier), "Out of memory on allocating clmd->clothObject.");
1179                 return 0;
1180         }
1181
1182         switch (ob->type)
1183         {
1184                 case OB_MESH:
1185                 
1186                 // mesh input objects need DerivedMesh
1187                 if(!dm)
1188                         return 0;
1189                 
1190                 cloth_from_mesh (ob, clmd, dm);
1191
1192                 if (clmd->clothObject != NULL) 
1193                 {                       
1194                         /* create springs */
1195                         clmd->clothObject->springs = NULL;
1196                         clmd->clothObject->numsprings = -1;
1197
1198                         if (!cloth_build_springs (clmd->clothObject, dm) )
1199                         {
1200                                 modifier_setError (&(clmd->modifier), "Can't build springs.");
1201                                 return 0;
1202                         }  
1203                         
1204                         mvert = CDDM_get_verts(dm);
1205                         verts = clmd->clothObject->verts;
1206
1207                         /* set initial values */
1208                         for (i = 0; i < numverts; i++, verts++)
1209                         {
1210                                 VECCOPY (verts->x, mvert[i].co);
1211                                 Mat4MulVecfl(ob->obmat, verts->x);
1212
1213                                 verts->mass = clmd->sim_parms.mass;
1214                                 verts->goal= clmd->sim_parms.defgoal;
1215                                 verts->flags = 0;
1216                                 VECCOPY(verts->xold, verts->x);
1217                                 VECCOPY(verts->xconst, verts->x);
1218                                 VECCOPY(verts->txold, verts->x);
1219                                 VecMulf(verts->v, 0.0f);
1220                         }
1221
1222                         /* apply / set vertex groups */
1223                         if (clmd->sim_parms.vgroup_mass > 0)
1224                                 cloth_apply_vgroup (clmd, dm, clmd->sim_parms.vgroup_mass);
1225
1226                         /* init our solver */
1227                         if (solvers [clmd->sim_parms.solver_type].init)
1228                                 solvers [clmd->sim_parms.solver_type].init (ob, clmd);
1229
1230                         clmd->clothObject->tree = bvh_build(clmd, clmd->coll_parms.epsilon);
1231
1232                         cloth_cache_set_frame(clmd, 1);
1233                 }
1234
1235                 return 1;
1236                 case OB_LATTICE:
1237                         printf("OB_LATTICE\n");
1238                 // lattice_to_softbody(ob);
1239                 return 1;
1240                 case OB_CURVE:
1241                 case OB_SURF:
1242                         printf("OB_SURF| OB_CURVE\n");
1243                 curve_surf_to_cloth(ob, clmd, vertexCos);
1244                 return 1;
1245                 default: return 0; // TODO - we do not support changing meshes
1246         }
1247         
1248         return 0;
1249 }
1250
1251 static void cloth_from_mesh (Object *ob, ClothModifierData *clmd, DerivedMesh *dm)
1252 {
1253         unsigned int numverts = dm->getNumVerts(dm);
1254         unsigned int numfaces = dm->getNumFaces(dm);
1255         MFace *mface = CDDM_get_faces(dm);
1256         unsigned int i = 0;
1257
1258         /* Allocate our vertices.
1259         */
1260         clmd->clothObject->numverts = numverts;
1261         clmd->clothObject->verts = MEM_callocN (sizeof (ClothVertex) * clmd->clothObject->numverts, "clothVertex");
1262         if (clmd->clothObject->verts == NULL) 
1263         {
1264                 cloth_free_modifier (clmd);
1265                 modifier_setError (&(clmd->modifier), "Out of memory on allocating clmd->clothObject->verts.");
1266                 return;
1267         }
1268         
1269         // save face information
1270         clmd->clothObject->numfaces = numfaces;
1271         clmd->clothObject->mfaces = MEM_callocN (sizeof (MFace) * clmd->clothObject->numfaces, "clothMFaces");
1272         if (clmd->clothObject->mfaces == NULL) 
1273         {
1274                 cloth_free_modifier (clmd);
1275                 modifier_setError (&(clmd->modifier), "Out of memory on allocating clmd->clothObject->mfaces.");
1276                 return;
1277         }
1278         for(i = 0; i < numfaces; i++)
1279                 memcpy(&clmd->clothObject->mfaces[i], &mface[i], sizeof(MFace));
1280
1281         
1282         // for SIP code
1283         // clmd->clothObject->facemarks = MEM_callocN (sizeof (unsigned char) * clmd->clothObject->numfaces, "clothFaceMarks");
1284
1285         /* Free the springs since they can't be correct if the vertices
1286         * changed.
1287         */
1288         if (clmd->clothObject->springs != NULL)
1289                 MEM_freeN (clmd->clothObject->springs);
1290
1291 }
1292
1293 /***************************************************************************************
1294 * SPRING NETWORK BUILDING IMPLEMENTATION BEGIN
1295 ***************************************************************************************/
1296
1297 int cloth_build_springs(Cloth *cloth, DerivedMesh *dm)
1298 {
1299         ClothSpring *springs = NULL;    
1300         unsigned int struct_springs = 0, shear_springs=0, bend_springs = 0;
1301         unsigned int i = 0;
1302         unsigned int numverts = dm->getNumVerts(dm);
1303         unsigned int numedges = dm->getNumEdges(dm);
1304         unsigned int numfaces = dm->getNumFaces(dm);
1305         MVert *mvert = CDDM_get_verts(dm);
1306         MEdge *medge = CDDM_get_edges(dm);
1307         MFace *mface = CDDM_get_faces(dm);
1308         unsigned int index2 = 0; // our second vertex index
1309         LinkNode **edgelist = NULL;
1310         EdgeHash *edgehash = NULL;
1311         LinkNode *search = NULL;
1312         float temp[3];
1313         unsigned int temp_index = 0;
1314         ClothSpring *tspring = NULL;
1315
1316         // error handling
1317         if(numedges==0)
1318                 return 0;
1319
1320         edgelist = MEM_callocN (sizeof (LinkNode *) * numverts, "cloth_edgelist_alloc");
1321         for(i = 0; i < numverts; i++)
1322         {
1323                 edgelist[i] = NULL;
1324         }
1325
1326         if(cloth->springs)
1327                 MEM_freeN(cloth->springs);
1328
1329         // create spring network hash
1330         edgehash = BLI_edgehash_new();
1331
1332         // should be 4 for maximal bending springs, using 5 to be sure ;)
1333         springs = cloth->springs = MEM_callocN (sizeof (ClothSpring) * (numedges + numfaces * 2 +  6 * numverts), "cloth_springs_alloc");
1334
1335         // structural springs
1336         for(i = 0; i < numedges; i++)
1337         {
1338                 springs[i].ij = medge[i].v1;
1339                 springs[i].kl = medge[i].v2;
1340                 VECSUB(temp, mvert[springs[i].kl].co, mvert[springs[i].ij].co);
1341                 springs[i].restlen =  sqrt(INPR(temp, temp));
1342                 springs[i].type = STRUCTURAL;
1343                 springs[i].flags = 0;
1344                 struct_springs++;
1345         }
1346
1347         // shear springs
1348         for(i = 0; i < numfaces; i++)
1349         {
1350                 temp_index = struct_springs + shear_springs;
1351
1352                 springs[temp_index].ij = mface[i].v1;
1353                 springs[temp_index].kl = mface[i].v3;
1354                 VECSUB(temp, mvert[springs[temp_index].kl].co, mvert[springs[temp_index].ij].co);
1355                 springs[temp_index].restlen =  sqrt(INPR(temp, temp));
1356                 springs[temp_index].type = SHEAR;
1357
1358                 BLI_linklist_append(&edgelist[springs[temp_index].ij], &(springs[temp_index]));         
1359                 BLI_linklist_append(&edgelist[springs[temp_index].kl], &(springs[temp_index]));
1360
1361                 shear_springs++;
1362                 temp_index++;
1363
1364                 springs[temp_index].ij = mface[i].v2;
1365                 springs[temp_index].kl = mface[i].v4;
1366                 VECSUB(temp, mvert[springs[temp_index].kl].co, mvert[springs[temp_index].ij].co);
1367                 springs[temp_index].restlen =  sqrt(INPR(temp, temp));
1368                 springs[temp_index].type = SHEAR;
1369
1370                 BLI_linklist_append(&edgelist[springs[temp_index].ij], &(springs[temp_index]));         
1371                 BLI_linklist_append(&edgelist[springs[temp_index].kl], &(springs[temp_index]));
1372
1373                 shear_springs++;
1374         }
1375
1376         // bending springs
1377         for(i = struct_springs; i < struct_springs+shear_springs; i++)
1378         {       
1379                 search = edgelist[springs[i].kl];
1380                 while(search)
1381                 {
1382                         tspring = search->link;
1383                         index2 = ((tspring->ij==springs[i].kl) ? (tspring->kl) : (tspring->ij));
1384
1385                         if(!BLI_edgehash_haskey(edgehash, index2, springs[i].ij) // check for existing spring 
1386                                 && !BLI_edgehash_haskey(edgehash, springs[i].ij, index2)  // same
1387                                 && (index2!=springs[i].ij)) // check if startpoint is equal to endpoint
1388                         {
1389                                 temp_index = struct_springs + shear_springs + bend_springs;
1390
1391                                 springs[temp_index].ij = springs[i].ij;
1392                                 springs[temp_index].kl = index2;
1393                                 VECSUB(temp, mvert[index2].co, mvert[springs[i].ij].co);
1394                                 springs[temp_index].restlen =  sqrt(INPR(temp, temp));
1395                                 springs[temp_index].type = BENDING;
1396                                 BLI_edgehash_insert(edgehash, springs[temp_index].ij, index2, NULL);
1397                                 bend_springs++;
1398
1399                         }
1400                         search = search->next;   
1401                 }
1402         }
1403
1404         cloth->numsprings = struct_springs + shear_springs + bend_springs;
1405
1406         for(i = 0; i < numverts; i++)
1407         {
1408                 BLI_linklist_free(edgelist[i],NULL); 
1409         }
1410         if(edgelist)
1411                 MEM_freeN(edgelist);    
1412
1413         BLI_edgehash_free(edgehash, NULL);
1414
1415         return 1;
1416
1417 } /* cloth_build_springs */
1418 /***************************************************************************************
1419 * SPRING NETWORK BUILDING IMPLEMENTATION END
1420 ***************************************************************************************/
1421