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