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