Merge with trunk: svn merge -r 12151:12179 https://svn.blender.org/svnroot/bf-blender...
[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         return temptime;
477 }
478
479 void cloth_cache_get_frame(ClothModifierData *clmd, float time)
480 {
481         Frame *frame = NULL;
482         LinkNode *search = NULL;
483         unsigned int i = 0;
484         Cloth *cloth = NULL;
485         int newtime = time + clmd->sim_parms.preroll;
486
487         if(clmd)
488         {
489                 cloth = clmd->clothObject;
490
491                 if(!cloth)
492                         return;
493
494                 // get cache
495                 if(clmd->sim_parms.cache)
496                 {
497                         search = clmd->sim_parms.cache;
498                         frame = NULL;
499                         // check if frame exists
500                         while(search)
501                         {
502                                 frame = search->link;
503                                 if(frame->time == newtime)
504                                         break;
505
506                                 frame = NULL;
507
508                                 search = search->next;   
509                         }
510
511                         if(frame)
512                         {
513                                 if(frame->verts)
514                                 {
515
516                                         // copy ClothVertex struct
517                                         memcpy(cloth->verts, frame->verts, cloth->numverts*sizeof(ClothVertex));
518                                         implicit_set_positions(clmd);
519                                 }
520                                 /*
521                                 if(frame->springs)
522                                 {
523                                         // copy ClothSpring struct
524                                         memcpy(cloth->springs, frame->springs, cloth->numsprings*sizeof(ClothSpring));
525                                 }
526                                 */
527                         }
528                 }
529         }
530
531 }
532
533 void cloth_cache_set_frame(ClothModifierData *clmd, float time)
534 {
535         Frame *frame = NULL;
536         unsigned int i = 0;
537         Cloth *cloth = NULL;
538         int newtime = time + clmd->sim_parms.preroll;
539
540         if(clmd)
541         {
542                 cloth = clmd->clothObject;
543
544                 if(cloth)
545                 {
546                         // creat new frame cache
547                         frame = (Frame *)MEM_callocN(sizeof(Frame), "cloth frame cache");
548                         frame->verts = (ClothVertex *)MEM_callocN(sizeof(ClothVertex)*cloth->numverts, "cloth frame vertex cache");
549                         frame->springs = NULL;
550                         /*
551                         frame->springs = (ClothSpring *)MEM_callocN(sizeof(ClothSpring)*cloth->numsprings, "cloth frame spring cache");
552                         */
553                         frame->time = newtime;
554
555                         // copy ClothVertex struct
556                         for(i = 0; i < cloth->numverts; i++)
557                         {
558                                 memcpy(&frame->verts[i], &cloth->verts[i], sizeof(ClothVertex));
559                         }
560                         /*
561                         // copy ClothSpring struct
562                         for(i = 0; i < cloth->numsprings; i++)
563                         {
564                                 memcpy(&frame->springs[i], &cloth->springs[i], sizeof(ClothSpring));
565                         }
566                         */
567                 }
568                 if(frame)
569                         BLI_linklist_append(&clmd->sim_parms.cache, frame);
570         }
571 }
572
573 // free cloth cache
574 void cloth_cache_free(ClothModifierData *clmd, float time)
575 {
576         Frame *frame = NULL;
577         LinkNode *search, *last_search;
578         int newtime = time + clmd->sim_parms.preroll;
579
580         // do never free first cached frame
581         if((newtime<1.0f) && !(clmd->sim_parms.flags & CSIMSETT_FLAG_CCACHE_FREE_ALL))
582                 return;
583
584         /* Calls the solver and collision frees first as they
585         * might depend on data in clmd->clothObject. */
586
587         if (clmd) 
588         {
589                 if(clmd->sim_parms.cache)
590                 {                       
591                         last_search = search = clmd->sim_parms.cache;
592                         while(search)
593                         {
594                                 LinkNode *next= search->next;
595                                 frame = search->link;
596
597                                 // free part of cache, but not preroll cache and first framer
598                                 if((clmd->sim_parms.flags & CSIMSETT_FLAG_CCACHE_FREE_PART)  
599                                         && (frame->time > newtime)) // do not delete the first frame
600                                 {
601                                         MEM_freeN(frame->verts);
602                                         // MEM_freeN(frame->springs);
603                                         MEM_freeN(frame);       
604                                         MEM_freeN(search);
605                                         last_search->next = next;
606                                 }
607                                 else if(clmd->sim_parms.flags & CSIMSETT_FLAG_CCACHE_FREE_ALL) // free COMPLETE cache
608                                 {
609                                         MEM_freeN(frame->verts);
610                                         // MEM_freeN(frame->springs);
611                                         MEM_freeN(frame);       
612                                 }
613                                 else
614                                         last_search = search;
615                                 search = next;
616                         }
617
618                         if(clmd->sim_parms.flags & CSIMSETT_FLAG_CCACHE_FREE_ALL)
619                         {
620                                 BLI_linklist_free(clmd->sim_parms.cache,NULL); 
621                                 clmd->sim_parms.cache = NULL;
622                         }
623                 }
624         }
625
626         /* clear flags */
627         clmd->sim_parms.flags &= ~CSIMSETT_FLAG_CCACHE_FREE_ALL;
628         clmd->sim_parms.flags &= ~CSIMSETT_FLAG_CCACHE_FREE_PART;
629
630 }
631
632
633 /**
634 * cloth_deform_verts - simulates one step, framenr is in frames.
635
636 **/
637 void clothModifier_do(ClothModifierData *clmd, Object *ob, DerivedMesh *dm,
638                 float (*vertexCos)[3], int numverts)
639 {
640         unsigned int i;
641         unsigned int numedges = -1;
642         unsigned int numfaces = -1;
643         MVert *mvert = NULL;
644         MEdge *medge = NULL;
645         MFace *mface = NULL;
646         DerivedMesh *result = NULL, *result2 = NULL;
647         Cloth *cloth = clmd->clothObject;
648         unsigned int framenr = (float)G.scene->r.cfra;
649         float current_time = bsystem_time(ob, (float)G.scene->r.cfra, 0.0);
650         ListBase        *effectors = NULL;
651         ClothVertex *newframe= NULL, *verts;
652         Frame *frame = NULL;
653         LinkNode *search = NULL;
654         float deltaTime = current_time - clmd->sim_parms.sim_time;      
655                 
656         
657         // only be active during a specific period:
658         // that's "first frame" and "last frame" on GUI
659         if (!(clmd->sim_parms.flags & CSIMSETT_FLAG_COLLOBJ))
660         {
661                 if(current_time < clmd->sim_parms.firstframe)
662                         return;
663                 else if(current_time > clmd->sim_parms.lastframe)
664                 {
665                         int frametime = cloth_cache_last_frame(clmd);
666                         if(cloth_cache_search_frame(clmd, frametime))
667                         {
668                                 cloth_cache_get_frame(clmd, frametime);
669                                 cloth_to_object (ob, clmd, vertexCos, numverts);
670                         }
671                         return;
672                 }
673                 else if(ABS(deltaTime) >= 2.0f ) // no timewarps allowed
674                 {
675                         if(!cloth_cache_search_frame(clmd, framenr))
676                                 return;
677                 }
678         }
679         
680         
681         // unused in the moment, calculated seperately in implicit.c
682         clmd->sim_parms.dt = 1.0f / clmd->sim_parms.stepsPerFrame;
683         
684         clmd->sim_parms.sim_time = current_time;
685         
686         // check if cloth object was some collision object before and needs freeing now
687         if (!(clmd->sim_parms.flags & CSIMSETT_FLAG_COLLOBJ) && (clmd->clothObject != NULL) && (clmd->clothObject->old_solver_type == 255))
688         {
689                 // temporary set CSIMSETT_FLAG_COLLOBJ flag for proper freeing 
690                 clmd->sim_parms.flags |= CSIMSETT_FLAG_COLLOBJ;
691                 cloth_free_modifier(clmd);
692                 clmd->sim_parms.flags &= ~CSIMSETT_FLAG_COLLOBJ;
693         }
694
695         // This is for collisions objects: check special case CSIMSETT_FLAG_COLLOBJ
696         if (clmd->sim_parms.flags & CSIMSETT_FLAG_COLLOBJ)
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                         // (no dt correction here because of float error)
728                         VECSUB (verts->v, verts->x, verts->xold);
729                 }
730                 
731                 return;
732         }       
733
734         if(deltaTime == 1.0f)
735         {
736                 if ((clmd->clothObject == NULL) || (numverts != clmd->clothObject->numverts) ) 
737                 {
738                         if(!cloth_from_object (ob, clmd, dm, vertexCos, numverts))
739                                 return;
740
741                         if(clmd->clothObject == NULL)
742                                 return;
743
744                         cloth = clmd->clothObject;
745                 }
746
747                 clmd->clothObject->old_solver_type = clmd->sim_parms.solver_type;
748
749                 // Insure we have a clmd->clothObject, in case allocation failed.
750                 if (clmd->clothObject != NULL) 
751                 {            
752                         if(!cloth_cache_search_frame(clmd, framenr))
753                         {
754                                 verts = cloth->verts;
755                                 
756                                 /* Force any pinned verts to their constrained location. */
757                                 for (i = 0; i < clmd->clothObject->numverts; i++, verts++)
758                                 {
759                                         /* Save the previous position. */
760                                         VECCOPY (verts->xold, verts->xconst);
761                                         VECCOPY (verts->txold, verts->x);
762
763                                         /* Get the current position. */
764                                         VECCOPY (verts->xconst, vertexCos[i]);
765                                         Mat4MulVecfl(ob->obmat, verts->xconst);
766                                 }
767
768                                 tstart();
769
770                                 /* Call the solver. */
771                                 if (solvers [clmd->sim_parms.solver_type].solver)
772                                         solvers [clmd->sim_parms.solver_type].solver (ob, framenr, clmd, effectors);
773
774                                 tend();
775                                 printf("Cloth simulation time: %f\n", (float)tval());
776
777                                 cloth_cache_set_frame(clmd, framenr);
778
779                         }
780                         else // just retrieve the cached frame
781                         {
782                                 cloth_cache_get_frame(clmd, framenr);
783                         }
784
785                         // Copy the result back to the object.
786                         cloth_to_object (ob, clmd, vertexCos, numverts);
787                         
788                         // bvh_free(clmd->clothObject->tree);
789                         // clmd->clothObject->tree = bvh_build(clmd, clmd->coll_parms.epsilon);
790                 } 
791
792         }
793         else if((deltaTime <= 0.0f)||(deltaTime > 1.0f))
794         {
795                 if((clmd->clothObject != NULL) && (clmd->sim_parms.cache)) 
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
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         if(!(clmd->sim_parms.flags & CSIMSETT_FLAG_CCACHE_PROTECT))
817         {
818                 // free our frame cache
819                 clmd->sim_parms.flags |= CSIMSETT_FLAG_CCACHE_FREE_ALL;
820                 cloth_cache_free(clmd, 0);
821         
822                 if (cloth) 
823                 {       
824                         // If our solver provides a free function, call it
825                         if (cloth->old_solver_type < 255 && solvers [cloth->old_solver_type].free) 
826                         {       
827                                 solvers [cloth->old_solver_type].free (clmd);
828                         }
829                         
830                         // Free the verts.
831                         if (cloth->verts != NULL)
832                                 MEM_freeN (cloth->verts);
833         
834                         cloth->verts = NULL;
835                         cloth->numverts = -1;
836                         
837                         // Free the springs.
838                         if (cloth->springs != NULL)
839                                 MEM_freeN (cloth->springs);
840         
841                         cloth->springs = NULL;
842                         cloth->numsprings = -1;         
843                         
844                         // free BVH collision tree
845                         if(cloth->tree)
846                                 bvh_free((BVH *)cloth->tree);
847                         
848                         // we save our faces for collision objects
849                         if(cloth->mfaces)
850                                 MEM_freeN(cloth->mfaces);
851                 
852                         if(clmd->clothObject->facemarks)
853                                 MEM_freeN(clmd->clothObject->facemarks);
854                         
855                         MEM_freeN (cloth);
856                         clmd->clothObject = NULL;
857                 }
858         }
859 }
860
861
862 /******************************************************************************
863 *
864 * Internal functions.
865 *
866 ******************************************************************************/
867
868 /**
869 * cloth_to_object - copies the deformed vertices to the object.
870 *
871 * This function is a modified version of the softbody.c:softbody_to_object() function.
872 **/
873 static void cloth_to_object (Object *ob, ClothModifierData *clmd, float (*vertexCos)[3], unsigned int numverts)
874 {
875         ClothVertex     *verts = NULL;
876         unsigned int    i = 0;
877
878         if (clmd->clothObject) {
879                 verts = clmd->clothObject->verts;
880
881                 /* inverse matrix is not uptodate... */
882                 Mat4Invert (ob->imat, ob->obmat);
883
884                 for (i = 0; i < numverts; i++, verts++)
885                 {
886                         VECCOPY (vertexCos[i], verts->x);
887                         Mat4MulVecfl (ob->imat, vertexCos[i]);  /* softbody is in global coords */
888                 }
889         }
890 }
891
892
893 /**
894 * cloth_apply_vgroup - applies a vertex group as specified by type
895 *
896 **/
897 static void cloth_apply_vgroup(ClothModifierData *clmd, DerivedMesh *dm, short vgroup)
898 {
899         unsigned int i = 0;
900         unsigned int j = 0;
901         MDeformVert *dvert = NULL;
902         Cloth *clothObj = NULL;
903         unsigned int numverts = dm->getNumVerts(dm);
904         float goalfac = 0;
905         ClothVertex *verts = NULL;
906
907         clothObj = clmd->clothObject;
908         
909         if(!dm)
910                 return;
911         
912         numverts = dm->getNumVerts(dm);
913
914         /* vgroup is 1 based, decrement so we can match the right group. */
915         --vgroup;
916         
917         verts = clothObj->verts;
918
919         for (i = 0; i < numverts; i++, verts++)
920         {                                       
921                 // LATER ON, support also mass painting here
922                 if(clmd->sim_parms.flags & CSIMSETT_FLAG_GOAL) 
923                 {                                               
924                         dvert = dm->getVertData(dm, i, CD_MDEFORMVERT);
925                         if(dvert)       
926                         {               
927                                 for(j = 0; j < dvert->totweight; j++) 
928                                 {
929                                         if(dvert->dw[j].def_nr == vgroup) 
930                                         {
931                                                 verts->goal = dvert->dw [j].weight;
932
933                                                 goalfac= ABS(clmd->sim_parms.maxgoal - clmd->sim_parms.mingoal);
934                                                 verts->goal  = (float)pow(verts->goal , 4.0f);
935
936                                                 if(dvert->dw [j].weight >=SOFTGOALSNAP)
937                                                 {
938                                                         verts->flags |= CVERT_FLAG_PINNED;
939                                                 }
940
941                                                 // TODO enable mass painting here, for the moment i let "goals" go first
942
943                                                 break;
944                                         }
945                                 }
946                         }
947                 }
948         }
949 }
950
951 // only meshes supported at the moment
952 /* collision objects */
953 static int collobj_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float (*vertexCos)[3], unsigned int numverts)
954 {
955         unsigned int i;
956         MVert *mvert = NULL; 
957         ClothVertex *verts = NULL;
958         float tnull[3] = {0,0,0}; 
959         
960         /* If we have a clothObject, free it. */
961         if (clmd->clothObject != NULL)
962                 cloth_free_modifier (clmd);
963
964         /* Allocate a new cloth object. */
965         clmd->clothObject = MEM_callocN (sizeof(Cloth), "cloth");
966         if (clmd->clothObject) 
967         {
968                 clmd->clothObject->old_solver_type = -1;
969                 clmd->clothObject->old_collision_type = -1;
970         }
971         else if (clmd->clothObject == NULL) 
972         {
973                 modifier_setError (&(clmd->modifier), "Out of memory on allocating clmd->clothObject.");
974                 return 0;
975         }
976
977         switch (ob->type)
978         {
979         case OB_MESH:
980                 
981                 // mesh input objects need DerivedMesh
982                 if(!dm)
983                         return 0;
984                 
985                 cloth_from_mesh (ob, clmd, dm);
986                 
987                 if (clmd->clothObject != NULL) 
988                 {
989                         mvert = CDDM_get_verts(dm);
990                         verts = clmd->clothObject->verts;
991                         
992                         for (i = 0; i < numverts; i++, verts++)
993                         {
994                                 VECCOPY (verts->x, mvert[i].co);
995                                 Mat4MulVecfl(ob->obmat, verts->x);
996                                 verts->flags = 0;
997                                 VECCOPY(verts->xold, verts->x);
998                                 VECCOPY(verts->txold, verts->x);
999                                 VECCOPY(verts->tx, verts->x);
1000                                 VecMulf(verts->v, 0.0f);
1001                                 verts->impulse_count = 0;
1002                                 VECCOPY(verts->impulse, tnull);
1003                         }
1004                         clmd->clothObject->tree =  bvh_build(clmd,clmd->coll_parms.epsilon);
1005                         
1006                 }
1007
1008                 return 1;
1009         default: return 0; // TODO - we do not support changing meshes
1010         }
1011 }
1012
1013 /*
1014 helper function to get proper spring length 
1015 when object is rescaled
1016 */
1017 float cloth_globallen(float *v1,float *v2,Object *ob)
1018 {
1019         float p1[3],p2[3];
1020         VECCOPY(p1,v1);
1021         Mat4MulVecfl(ob->obmat, p1);    
1022         VECCOPY(p2,v2);
1023         Mat4MulVecfl(ob->obmat, p2);
1024         return VecLenf(p1,p2);
1025 }
1026
1027 static void curve_surf_to_cloth(Object *ob, ClothModifierData *clmd, float (*vertexCos)[3])
1028 {
1029         Curve *cu= ob->data;
1030         Nurb *nu;
1031         BezTriple *bezt;
1032         float goalfac;
1033         unsigned int a, curindex=0, i=0;
1034         unsigned int numverts, numsprings = 0, setgoal=0;
1035         Cloth   *clothObj;
1036         ClothVertex *verts = NULL;
1037         
1038         clmd->clothObject->numverts = numverts= count_curveverts(&cu->nurb);
1039         clothObj = clmd->clothObject;
1040         
1041         if(ob->type==OB_CURVE) 
1042         {
1043                 numsprings = numverts - BLI_countlist(&cu->nurb);
1044         }
1045         
1046         /* Allocate our vertices.
1047         */
1048         clmd->clothObject->numverts = numverts;
1049         clmd->clothObject->verts = MEM_callocN (sizeof (ClothVertex) * clmd->clothObject->numverts, "clothVertex");
1050         if (clmd->clothObject->verts == NULL) 
1051         {
1052                 cloth_free_modifier (clmd);
1053                 modifier_setError (&(clmd->modifier), "Out of memory on allocating clmd->clothObject->verts.");
1054                 return;
1055         }
1056         
1057         verts = clmd->clothObject->verts;
1058         
1059         // copy vertex positions
1060         for (i = 0; i < numverts; i++)
1061         {
1062                 VECCOPY (verts->x, vertexCos[i]);
1063                 Mat4MulVecfl(ob->obmat, verts->x);
1064
1065                 verts->mass = clmd->sim_parms.mass;
1066                 // verts->goal= clmd->sim_parms.defgoal;
1067                 verts->flags = 0;
1068                 VECCOPY(verts->xold, verts->x);
1069                 VECCOPY(verts->xconst, verts->x);
1070                 VECCOPY(verts->txold, verts->x);
1071                 VecMulf(verts->v, 0.0f);
1072         }
1073         
1074         clmd->clothObject->mfaces = NULL; // update face pointer
1075         clmd->clothObject->numfaces = 0;
1076         
1077         clmd->clothObject->springs = MEM_callocN (sizeof (ClothSpring) * (numsprings), "cloth_springs_alloc");
1078                 
1079         // set vars now 
1080         goalfac= ABS(clmd->sim_parms.maxgoal - clmd->sim_parms.mingoal);
1081         // clothObj->verts [i].goal = clmd->sim_parms.mingoal + bezt->weight*goalfac;
1082         
1083         /* apply / set vertex groups */
1084         if(clmd->sim_parms.flags & CSIMSETT_FLAG_GOAL) 
1085         {
1086                 if (clmd->sim_parms.vgroup_mass > 0)
1087                 {
1088                         setgoal = 1;
1089                 }
1090         }
1091                 
1092 /*      
1093         for(nu= cu->nurb.first; nu; nu= nu->next) 
1094         {
1095                 if(nu->bezt) 
1096                 {
1097                         for(bezt=nu->bezt, a=0; a<nu->pntsu; a++, bezt++, bp+=3, curindex+=3) 
1098                         {
1099                                 if(setgoal) 
1100                                 {
1101                                         bp->goal= sb->mingoal + bezt->weight*goalfac;
1102                                         // a little ad hoc changing the goal control to be less *sharp*
1103                                         bp->goal = (float)pow(bp->goal, 4.0f);
1104                                         
1105                                         // all three triples
1106                                         (bp+1)->goal= bp->goal;
1107                                         (bp+2)->goal= bp->goal;
1108                                 }
1109                                 
1110                                 if(totspring) 
1111                                 {
1112                                         if(a>0) 
1113                                         {
1114                                                 bs->v1= curindex-1;
1115                                                 bs->v2= curindex;
1116                                                 bs->strength= 1.0;
1117                                                 bs->order=1;
1118                                                 bs->len= globallen( (bezt-1)->vec[2], bezt->vec[0], ob );
1119                                                 bs++;
1120                                         }
1121                                         bs->v1= curindex;
1122                                         bs->v2= curindex+1;
1123                                         bs->strength= 1.0;
1124                                         bs->order=1;
1125                                         bs->len= globallen( bezt->vec[0], bezt->vec[1], ob );
1126                                         bs++;
1127                                         
1128                                         bs->v1= curindex+1;
1129                                         bs->v2= curindex+2;
1130                                         bs->strength= 1.0;
1131                                         bs->order=1;
1132                                         bs->len= globallen( bezt->vec[1], bezt->vec[2], ob );
1133                                         bs++;
1134                                 }
1135                         }
1136                 }
1137                 else {
1138                         for(bpnt=nu->bp, a=0; a<nu->pntsu*nu->pntsv; a++, bpnt++, bp++, curindex++) 
1139                         {
1140                                 if(setgoal) 
1141                                 {
1142                                         bp->goal= sb->mingoal + bpnt->weight*goalfac;
1143                                         // a little ad hoc changing the goal control to be less *sharp*
1144                                         bp->goal = (float)pow(bp->goal, 4.0f);
1145                                 }
1146                                 if(totspring && a>0) 
1147                                 {
1148                                         bs->v1= curindex-1;
1149                                         bs->v2= curindex;
1150                                         bs->strength= 1.0;
1151                                         bs->order=1;
1152                                         bs->len= globallen( (bpnt-1)->vec, bpnt->vec , ob );
1153                                         bs++;
1154                                 }
1155                         }
1156                 }
1157         }
1158         */
1159 }
1160                 
1161 // only meshes supported at the moment
1162 static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float (*vertexCos)[3], unsigned int numverts)
1163 {
1164         unsigned int i = 0;
1165         // dm->getNumVerts(dm);
1166         MVert *mvert = NULL; // CDDM_get_verts(dm);
1167         ClothVertex *verts = NULL;
1168         float tnull[3] = {0,0,0};
1169         
1170         /* If we have a clothObject, free it. */
1171         if (clmd->clothObject != NULL)
1172                 cloth_free_modifier (clmd);
1173
1174         /* Allocate a new cloth object. */
1175         clmd->clothObject = MEM_callocN (sizeof(Cloth), "cloth");
1176         if (clmd->clothObject) 
1177         {
1178                 clmd->clothObject->old_solver_type = -1;
1179                 clmd->clothObject->old_collision_type = -1;
1180         }
1181         else if (clmd->clothObject == NULL) 
1182         {
1183                 modifier_setError (&(clmd->modifier), "Out of memory on allocating clmd->clothObject.");
1184                 return 0;
1185         }
1186
1187         switch (ob->type)
1188         {
1189                 case OB_MESH:
1190                 
1191                 // mesh input objects need DerivedMesh
1192                 if(!dm)
1193                         return 0;
1194                 
1195                 cloth_from_mesh (ob, clmd, dm);
1196
1197                 if (clmd->clothObject != NULL) 
1198                 {                       
1199                         /* create springs */
1200                         clmd->clothObject->springs = NULL;
1201                         clmd->clothObject->numsprings = -1;
1202
1203                         if (!cloth_build_springs (clmd->clothObject, dm) )
1204                         {
1205                                 modifier_setError (&(clmd->modifier), "Can't build springs.");
1206                                 return 0;
1207                         }  
1208                         
1209                         mvert = CDDM_get_verts(dm);
1210                         verts = clmd->clothObject->verts;
1211
1212                         /* set initial values */
1213                         for (i = 0; i < numverts; i++, verts++)
1214                         {
1215                                 VECCOPY (verts->x, mvert[i].co);
1216                                 Mat4MulVecfl(ob->obmat, verts->x);
1217
1218                                 verts->mass = clmd->sim_parms.mass;
1219                                 
1220                                 if(clmd->sim_parms.flags & CSIMSETT_FLAG_GOAL) 
1221                                         verts->goal= clmd->sim_parms.defgoal;
1222                                 else
1223                                         verts->goal= 0.0f;
1224                                 
1225                                 verts->flags = 0;
1226                                 VECCOPY(verts->xold, verts->x);
1227                                 VECCOPY(verts->xconst, verts->x);
1228                                 VECCOPY(verts->txold, verts->x);
1229                                 VecMulf(verts->v, 0.0f);
1230                                 
1231                                 verts->impulse_count = 0;
1232                                 VECCOPY(verts->impulse, tnull);
1233                         }
1234
1235                         /* apply / set vertex groups */
1236                         if (clmd->sim_parms.vgroup_mass > 0)
1237                                 cloth_apply_vgroup (clmd, dm, clmd->sim_parms.vgroup_mass);
1238
1239                         /* init our solver */
1240                         if (solvers [clmd->sim_parms.solver_type].init)
1241                                 solvers [clmd->sim_parms.solver_type].init (ob, clmd);
1242
1243                         clmd->clothObject->tree = bvh_build(clmd, clmd->coll_parms.epsilon);
1244
1245                         cloth_cache_set_frame(clmd, 1);
1246                 }
1247
1248                 return 1;
1249                 case OB_LATTICE:
1250                         printf("OB_LATTICE\n");
1251                 // lattice_to_softbody(ob);
1252                 return 1;
1253                 case OB_CURVE:
1254                 case OB_SURF:
1255                         printf("OB_SURF| OB_CURVE\n");
1256                 curve_surf_to_cloth(ob, clmd, vertexCos);
1257                 return 1;
1258                 default: return 0; // TODO - we do not support changing meshes
1259         }
1260         
1261         return 0;
1262 }
1263
1264 static void cloth_from_mesh (Object *ob, ClothModifierData *clmd, DerivedMesh *dm)
1265 {
1266         unsigned int numverts = dm->getNumVerts(dm);
1267         unsigned int numfaces = dm->getNumFaces(dm);
1268         MFace *mface = CDDM_get_faces(dm);
1269         unsigned int i = 0;
1270
1271         /* Allocate our vertices.
1272         */
1273         clmd->clothObject->numverts = numverts;
1274         clmd->clothObject->verts = MEM_callocN (sizeof (ClothVertex) * clmd->clothObject->numverts, "clothVertex");
1275         if (clmd->clothObject->verts == NULL) 
1276         {
1277                 cloth_free_modifier (clmd);
1278                 modifier_setError (&(clmd->modifier), "Out of memory on allocating clmd->clothObject->verts.");
1279                 return;
1280         }
1281         
1282         // save face information
1283         clmd->clothObject->numfaces = numfaces;
1284         clmd->clothObject->mfaces = MEM_callocN (sizeof (MFace) * clmd->clothObject->numfaces, "clothMFaces");
1285         if (clmd->clothObject->mfaces == NULL) 
1286         {
1287                 cloth_free_modifier (clmd);
1288                 modifier_setError (&(clmd->modifier), "Out of memory on allocating clmd->clothObject->mfaces.");
1289                 return;
1290         }
1291         for(i = 0; i < numfaces; i++)
1292                 memcpy(&clmd->clothObject->mfaces[i], &mface[i], sizeof(MFace));
1293
1294         
1295         // for SIP code
1296         // clmd->clothObject->facemarks = MEM_callocN (sizeof (unsigned char) * clmd->clothObject->numfaces, "clothFaceMarks");
1297
1298         /* Free the springs since they can't be correct if the vertices
1299         * changed.
1300         */
1301         if (clmd->clothObject->springs != NULL)
1302                 MEM_freeN (clmd->clothObject->springs);
1303
1304 }
1305
1306 /***************************************************************************************
1307 * SPRING NETWORK BUILDING IMPLEMENTATION BEGIN
1308 ***************************************************************************************/
1309
1310 int cloth_build_springs(Cloth *cloth, DerivedMesh *dm)
1311 {
1312         ClothSpring *springs = NULL;    
1313         unsigned int struct_springs = 0, shear_springs=0, bend_springs = 0;
1314         unsigned int i = 0;
1315         unsigned int numverts = dm->getNumVerts(dm);
1316         unsigned int numedges = dm->getNumEdges(dm);
1317         unsigned int numfaces = dm->getNumFaces(dm);
1318         MVert *mvert = CDDM_get_verts(dm);
1319         MEdge *medge = CDDM_get_edges(dm);
1320         MFace *mface = CDDM_get_faces(dm);
1321         unsigned int index2 = 0; // our second vertex index
1322         LinkNode **edgelist = NULL;
1323         EdgeHash *edgehash = NULL;
1324         LinkNode *search = NULL;
1325         float temp[3];
1326         unsigned int temp_index = 0;
1327         ClothSpring *tspring = NULL;
1328
1329         // error handling
1330         if(numedges==0)
1331                 return 0;
1332
1333         edgelist = MEM_callocN (sizeof (LinkNode *) * numverts, "cloth_edgelist_alloc");
1334         for(i = 0; i < numverts; i++)
1335         {
1336                 edgelist[i] = NULL;
1337         }
1338
1339         if(cloth->springs)
1340                 MEM_freeN(cloth->springs);
1341
1342         // create spring network hash
1343         edgehash = BLI_edgehash_new();
1344
1345         // should be 4 for maximal bending springs, using 5 to be sure ;)
1346         springs = cloth->springs = MEM_callocN (sizeof (ClothSpring) * (numedges + numfaces * 2 +  6 * numverts), "cloth_springs_alloc");
1347
1348         // structural springs
1349         for(i = 0; i < numedges; i++)
1350         {
1351                 springs[i].ij = medge[i].v1;
1352                 springs[i].kl = medge[i].v2;
1353                 VECSUB(temp, mvert[springs[i].kl].co, mvert[springs[i].ij].co);
1354                 springs[i].restlen =  sqrt(INPR(temp, temp));
1355                 springs[i].type = STRUCTURAL;
1356                 springs[i].flags = 0;
1357                 struct_springs++;
1358         }
1359
1360         // shear springs
1361         for(i = 0; i < numfaces; i++)
1362         {
1363                 temp_index = struct_springs + shear_springs;
1364
1365                 springs[temp_index].ij = mface[i].v1;
1366                 springs[temp_index].kl = mface[i].v3;
1367                 VECSUB(temp, mvert[springs[temp_index].kl].co, mvert[springs[temp_index].ij].co);
1368                 springs[temp_index].restlen =  sqrt(INPR(temp, temp));
1369                 springs[temp_index].type = SHEAR;
1370
1371                 BLI_linklist_append(&edgelist[springs[temp_index].ij], &(springs[temp_index]));         
1372                 BLI_linklist_append(&edgelist[springs[temp_index].kl], &(springs[temp_index]));
1373
1374                 shear_springs++;
1375                 temp_index++;
1376                 
1377                 if(mface[i].v4)
1378                 {
1379                         springs[temp_index].ij = mface[i].v2;
1380                         springs[temp_index].kl = mface[i].v4;
1381                         VECSUB(temp, mvert[springs[temp_index].kl].co, mvert[springs[temp_index].ij].co);
1382                         springs[temp_index].restlen =  sqrt(INPR(temp, temp));
1383                         springs[temp_index].type = SHEAR;
1384         
1385                         BLI_linklist_append(&edgelist[springs[temp_index].ij], &(springs[temp_index]));         
1386                         BLI_linklist_append(&edgelist[springs[temp_index].kl], &(springs[temp_index]));
1387         
1388                         shear_springs++;
1389                 }
1390         }
1391
1392         // bending springs
1393         for(i = struct_springs; i < struct_springs+shear_springs; i++)
1394         {       
1395                 search = edgelist[springs[i].kl];
1396                 while(search)
1397                 {
1398                         tspring = search->link;
1399                         index2 = ((tspring->ij==springs[i].kl) ? (tspring->kl) : (tspring->ij));
1400
1401                         if(!BLI_edgehash_haskey(edgehash, index2, springs[i].ij) // check for existing spring 
1402                                 && !BLI_edgehash_haskey(edgehash, springs[i].ij, index2)  // same
1403                                 && (index2!=springs[i].ij)) // check if startpoint is equal to endpoint
1404                         {
1405                                 temp_index = struct_springs + shear_springs + bend_springs;
1406
1407                                 springs[temp_index].ij = springs[i].ij;
1408                                 springs[temp_index].kl = index2;
1409                                 VECSUB(temp, mvert[index2].co, mvert[springs[i].ij].co);
1410                                 springs[temp_index].restlen =  sqrt(INPR(temp, temp));
1411                                 springs[temp_index].type = BENDING;
1412                                 BLI_edgehash_insert(edgehash, springs[temp_index].ij, index2, NULL);
1413                                 bend_springs++;
1414
1415                         }
1416                         search = search->next;   
1417                 }
1418         }
1419
1420         cloth->numsprings = struct_springs + shear_springs + bend_springs;
1421
1422         for(i = 0; i < numverts; i++)
1423         {
1424                 BLI_linklist_free(edgelist[i],NULL); 
1425         }
1426         if(edgelist)
1427                 MEM_freeN(edgelist);    
1428
1429         BLI_edgehash_free(edgehash, NULL);
1430
1431         return 1;
1432
1433 } /* cloth_build_springs */
1434 /***************************************************************************************
1435 * SPRING NETWORK BUILDING IMPLEMENTATION END
1436 ***************************************************************************************/
1437