4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) Blender Foundation
21 * All rights reserved.
23 * Contributor(s): Daniel Genrich
25 * ***** END GPL LICENSE BLOCK *****
28 #include "MEM_guardedalloc.h"
30 #include "DNA_cloth_types.h"
31 #include "DNA_scene_types.h"
32 #include "DNA_object_types.h"
33 #include "DNA_meshdata_types.h"
36 #include "BLI_edgehash.h"
38 #include "BKE_cdderivedmesh.h"
39 #include "BKE_cloth.h"
40 #include "BKE_effect.h"
41 #include "BKE_global.h"
42 #include "BKE_modifier.h"
43 #include "BKE_pointcache.h"
44 #include "BKE_utildefines.h"
58 static struct timeval _tstart, _tend;
59 static struct timezone tz;
62 gettimeofday ( &_tstart, &tz );
66 gettimeofday ( &_tend,&tz );
71 t1 = ( double ) _tstart.tv_sec + ( double ) _tstart.tv_usec/ ( 1000*1000 );
72 t2 = ( double ) _tend.tv_sec + ( double ) _tend.tv_usec/ ( 1000*1000 );
77 /* Our available solvers. */
78 // 255 is the magic reserved number, so NEVER try to put 255 solvers in here!
80 static CM_SOLVER_DEF solvers [] =
82 { "Implicit", CM_IMPLICIT, implicit_init, implicit_solver, implicit_free },
83 // { "Implicit C++", CM_IMPLICITCPP, implicitcpp_init, implicitcpp_solver, implicitcpp_free },
86 /* ********** cloth engine ******* */
87 /* Prototypes for internal functions.
89 static void cloth_to_object (Object *ob, ClothModifierData *clmd, DerivedMesh *dm);
90 static void cloth_from_mesh ( ClothModifierData *clmd, DerivedMesh *dm );
91 static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float framenr, int first);
92 static int cloth_build_springs ( ClothModifierData *clmd, DerivedMesh *dm );
93 static void cloth_apply_vgroup ( ClothModifierData *clmd, DerivedMesh *dm );
96 /******************************************************************************
98 * External interface called by modifier.c clothModifier functions.
100 ******************************************************************************/
102 * cloth_init - creates a new cloth simulation.
105 * 2. fill object with standard values or with the GUI settings if given
107 void cloth_init ( ClothModifierData *clmd )
109 /* Initialize our new data structure to reasonable values. */
110 clmd->sim_parms->gravity [0] = 0.0;
111 clmd->sim_parms->gravity [1] = 0.0;
112 clmd->sim_parms->gravity [2] = -9.81;
113 clmd->sim_parms->structural = 15.0;
114 clmd->sim_parms->shear = 15.0;
115 clmd->sim_parms->bending = 0.5;
116 clmd->sim_parms->Cdis = 5.0;
117 clmd->sim_parms->Cvi = 1.0;
118 clmd->sim_parms->mass = 0.3f;
119 clmd->sim_parms->stepsPerFrame = 5;
120 clmd->sim_parms->flags = 0;
121 clmd->sim_parms->solver_type = 0;
122 clmd->sim_parms->preroll = 0;
123 clmd->sim_parms->maxspringlen = 10;
124 clmd->sim_parms->vgroup_mass = 0;
125 clmd->sim_parms->avg_spring_len = 0.0;
126 clmd->sim_parms->presets = 2; /* cotton as start setting */
127 clmd->sim_parms->timescale = 1.0f; /* speed factor, describes how fast cloth moves */
128 clmd->sim_parms->reset = 0;
130 clmd->coll_parms->self_friction = 5.0;
131 clmd->coll_parms->friction = 5.0;
132 clmd->coll_parms->loop_count = 2;
133 clmd->coll_parms->epsilon = 0.015f;
134 clmd->coll_parms->flags = CLOTH_COLLSETTINGS_FLAG_ENABLED;
135 clmd->coll_parms->collision_list = NULL;
136 clmd->coll_parms->self_loop_count = 1.0;
137 clmd->coll_parms->selfepsilon = 0.75;
139 /* These defaults are copied from softbody.c's
140 * softbody_calc_forces() function.
142 clmd->sim_parms->eff_force_scale = 1000.0;
143 clmd->sim_parms->eff_wind_scale = 250.0;
145 // also from softbodies
146 clmd->sim_parms->maxgoal = 1.0f;
147 clmd->sim_parms->mingoal = 0.0f;
148 clmd->sim_parms->defgoal = 0.0f;
149 clmd->sim_parms->goalspring = 1.0f;
150 clmd->sim_parms->goalfrict = 0.0f;
151 clmd->sim_parms->velocity_smooth = 0.0f;
153 if(!clmd->sim_parms->effector_weights)
154 clmd->sim_parms->effector_weights = BKE_add_effector_weights(NULL);
156 if(clmd->point_cache)
157 clmd->point_cache->step = 1;
160 static BVHTree *bvhselftree_build_from_cloth (ClothModifierData *clmd, float epsilon)
172 cloth = clmd->clothObject;
177 verts = cloth->verts;
178 mfaces = cloth->mfaces;
180 // in the moment, return zero if no faces there
184 // create quadtree with k=26
185 bvhtree = BLI_bvhtree_new(cloth->numverts, epsilon, 4, 6);
188 for(i = 0; i < cloth->numverts; i++, verts++)
190 VECCOPY(&co[0*3], verts->xold);
192 BLI_bvhtree_insert(bvhtree, i, co, 1);
196 BLI_bvhtree_balance(bvhtree);
201 static BVHTree *bvhtree_build_from_cloth (ClothModifierData *clmd, float epsilon)
213 cloth = clmd->clothObject;
218 verts = cloth->verts;
219 mfaces = cloth->mfaces;
221 // in the moment, return zero if no faces there
225 // create quadtree with k=26
226 bvhtree = BLI_bvhtree_new(cloth->numfaces, epsilon, 4, 26);
229 for(i = 0; i < cloth->numfaces; i++, mfaces++)
231 VECCOPY(&co[0*3], verts[mfaces->v1].xold);
232 VECCOPY(&co[1*3], verts[mfaces->v2].xold);
233 VECCOPY(&co[2*3], verts[mfaces->v3].xold);
236 VECCOPY(&co[3*3], verts[mfaces->v4].xold);
238 BLI_bvhtree_insert(bvhtree, i, co, (mfaces->v4 ? 4 : 3));
242 BLI_bvhtree_balance(bvhtree);
247 void bvhtree_update_from_cloth(ClothModifierData *clmd, int moving)
250 Cloth *cloth = clmd->clothObject;
251 BVHTree *bvhtree = cloth->bvhtree;
252 ClothVertex *verts = cloth->verts;
254 float co[12], co_moving[12];
260 mfaces = cloth->mfaces;
262 // update vertex position in bvh tree
265 for(i = 0; i < cloth->numfaces; i++, mfaces++)
267 VECCOPY(&co[0*3], verts[mfaces->v1].txold);
268 VECCOPY(&co[1*3], verts[mfaces->v2].txold);
269 VECCOPY(&co[2*3], verts[mfaces->v3].txold);
272 VECCOPY(&co[3*3], verts[mfaces->v4].txold);
274 // copy new locations into array
277 // update moving positions
278 VECCOPY(&co_moving[0*3], verts[mfaces->v1].tx);
279 VECCOPY(&co_moving[1*3], verts[mfaces->v2].tx);
280 VECCOPY(&co_moving[2*3], verts[mfaces->v3].tx);
283 VECCOPY(&co_moving[3*3], verts[mfaces->v4].tx);
285 ret = BLI_bvhtree_update_node(bvhtree, i, co, co_moving, (mfaces->v4 ? 4 : 3));
289 ret = BLI_bvhtree_update_node(bvhtree, i, co, NULL, (mfaces->v4 ? 4 : 3));
292 // check if tree is already full
297 BLI_bvhtree_update_tree(bvhtree);
301 void bvhselftree_update_from_cloth(ClothModifierData *clmd, int moving)
304 Cloth *cloth = clmd->clothObject;
305 BVHTree *bvhtree = cloth->bvhselftree;
306 ClothVertex *verts = cloth->verts;
308 float co[12], co_moving[12];
314 mfaces = cloth->mfaces;
316 // update vertex position in bvh tree
319 for(i = 0; i < cloth->numverts; i++, verts++)
321 VECCOPY(&co[0*3], verts->txold);
323 // copy new locations into array
326 // update moving positions
327 VECCOPY(&co_moving[0*3], verts->tx);
329 ret = BLI_bvhtree_update_node(bvhtree, i, co, co_moving, 1);
333 ret = BLI_bvhtree_update_node(bvhtree, i, co, NULL, 1);
336 // check if tree is already full
341 BLI_bvhtree_update_tree(bvhtree);
345 void cloth_clear_cache(Object *ob, ClothModifierData *clmd, float framenr)
349 BKE_ptcache_id_from_cloth(&pid, ob, clmd);
351 // don't do anything as long as we're in editmode!
352 if(pid.cache->edit && ob->mode & OB_MODE_PARTICLE_EDIT)
355 BKE_ptcache_id_clear(&pid, PTCACHE_CLEAR_AFTER, framenr);
358 static int do_init_cloth(Object *ob, ClothModifierData *clmd, DerivedMesh *result, int framenr)
362 cache= clmd->point_cache;
364 /* initialize simulation data if it didn't exist already */
365 if(clmd->clothObject == NULL) {
366 if(!cloth_from_object(ob, clmd, result, framenr, 1)) {
367 BKE_ptcache_invalidate(cache);
371 if(clmd->clothObject == NULL) {
372 BKE_ptcache_invalidate(cache);
376 implicit_set_positions(clmd);
382 static int do_step_cloth(Object *ob, ClothModifierData *clmd, DerivedMesh *result, int framenr)
384 ClothVertex *verts = NULL;
386 ListBase *effectors = NULL;
390 /* simulate 1 frame forward */
391 cloth = clmd->clothObject;
392 verts = cloth->verts;
393 mvert = result->getVertArray(result);
395 /* force any pinned verts to their constrained location. */
396 for(i = 0; i < clmd->clothObject->numverts; i++, verts++) {
397 /* save the previous position. */
398 VECCOPY(verts->xold, verts->xconst);
399 VECCOPY(verts->txold, verts->x);
401 /* Get the current position. */
402 VECCOPY(verts->xconst, mvert[i].co);
403 mul_m4_v3(ob->obmat, verts->xconst);
406 effectors = pdInitEffectors(clmd->scene, ob, NULL, clmd->sim_parms->effector_weights);
410 /* call the solver. */
411 if(solvers [clmd->sim_parms->solver_type].solver)
412 ret = solvers[clmd->sim_parms->solver_type].solver(ob, framenr, clmd, effectors);
416 pdEndEffectors(&effectors);
418 // printf ( "%f\n", ( float ) tval() );
423 /************************************************
424 * clothModifier_do - main simulation function
425 ************************************************/
426 DerivedMesh *clothModifier_do(ClothModifierData *clmd, Scene *scene, Object *ob, DerivedMesh *dm)
432 int framedelta, framenr, startframe, endframe;
435 clmd->scene= scene; /* nice to pass on later :) */
436 framenr= (int)scene->r.cfra;
437 cache= clmd->point_cache;
438 result = CDDM_copy(dm);
440 BKE_ptcache_id_from_cloth(&pid, ob, clmd);
441 BKE_ptcache_id_time(&pid, scene, framenr, &startframe, &endframe, ×cale);
442 clmd->sim_parms->timescale= timescale;
445 BKE_ptcache_invalidate(cache);
449 if(clmd->sim_parms->reset
450 || (framenr == (startframe - clmd->sim_parms->preroll) && clmd->sim_parms->preroll != 0)
451 || (clmd->clothObject && result->getNumVerts(result) != clmd->clothObject->numverts))
453 clmd->sim_parms->reset = 0;
454 cache->flag |= PTCACHE_OUTDATED;
455 BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_OUTDATED);
456 BKE_ptcache_validate(cache, 0);
457 cache->last_exact= 0;
458 cache->flag &= ~PTCACHE_REDO_NEEDED;
462 // unused in the moment, calculated separately in implicit.c
463 clmd->sim_parms->dt = clmd->sim_parms->timescale / clmd->sim_parms->stepsPerFrame;
465 /* handle continuous simulation with the play button */
466 if(BKE_ptcache_get_continue_physics() || ((clmd->sim_parms->preroll > 0) && (framenr > startframe - clmd->sim_parms->preroll) && (framenr < startframe))) {
467 BKE_ptcache_invalidate(cache);
470 if(!do_init_cloth(ob, clmd, result, framenr))
473 do_step_cloth(ob, clmd, result, framenr);
474 cloth_to_object(ob, clmd, result);
479 /* simulation is only active during a specific period */
480 if(framenr < startframe) {
481 BKE_ptcache_invalidate(cache);
484 else if(framenr > endframe) {
488 if(cache->flag & PTCACHE_SIMULATION_VALID)
489 framedelta= framenr - cache->simframe;
493 /* initialize simulation data if it didn't exist already */
494 if(!do_init_cloth(ob, clmd, result, framenr))
497 if((framenr == startframe) && (clmd->sim_parms->preroll == 0)) {
498 BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_OUTDATED);
499 do_init_cloth(ob, clmd, result, framenr);
500 BKE_ptcache_validate(cache, framenr);
501 cache->flag &= ~PTCACHE_REDO_NEEDED;
505 /* try to read from cache */
506 cache_result = BKE_ptcache_read_cache(&pid, (float)framenr+scene->r.subframe, scene->r.frs_sec);
508 if(cache_result == PTCACHE_READ_EXACT || cache_result == PTCACHE_READ_INTERPOLATED) {
509 implicit_set_positions(clmd);
510 cloth_to_object (ob, clmd, result);
512 BKE_ptcache_validate(cache, framenr);
514 if(cache_result == PTCACHE_READ_INTERPOLATED && cache->flag & PTCACHE_REDO_NEEDED)
515 BKE_ptcache_write_cache(&pid, framenr);
519 else if(cache_result==PTCACHE_READ_OLD) {
520 implicit_set_positions(clmd);
522 else if( /*ob->id.lib ||*/ (cache->flag & PTCACHE_BAKED)) { /* 2.4x disabled lib, but this can be used in some cases, testing further - campbell */
523 /* if baked and nothing in cache, do nothing */
524 BKE_ptcache_invalidate(cache);
528 /* if on second frame, write cache for first frame */
529 if(cache->simframe == startframe && (cache->flag & PTCACHE_OUTDATED || cache->last_exact==0))
530 BKE_ptcache_write_cache(&pid, startframe);
532 clmd->sim_parms->timescale *= framenr - cache->simframe;
535 BKE_ptcache_validate(cache, framenr);
537 if(!do_step_cloth(ob, clmd, result, framenr)) {
538 BKE_ptcache_invalidate(cache);
541 BKE_ptcache_write_cache(&pid, framenr);
543 cloth_to_object (ob, clmd, result);
549 void cloth_free_modifier(ClothModifierData *clmd )
556 cloth = clmd->clothObject;
561 // If our solver provides a free function, call it
562 if ( solvers [clmd->sim_parms->solver_type].free )
564 solvers [clmd->sim_parms->solver_type].free ( clmd );
568 if ( cloth->verts != NULL )
569 MEM_freeN ( cloth->verts );
575 if ( cloth->springs != NULL )
577 LinkNode *search = cloth->springs;
580 ClothSpring *spring = search->link;
582 MEM_freeN ( spring );
583 search = search->next;
585 BLI_linklist_free(cloth->springs, NULL);
587 cloth->springs = NULL;
590 cloth->springs = NULL;
591 cloth->numsprings = 0;
593 // free BVH collision tree
594 if ( cloth->bvhtree )
595 BLI_bvhtree_free ( cloth->bvhtree );
597 if ( cloth->bvhselftree )
598 BLI_bvhtree_free ( cloth->bvhselftree );
600 // we save our faces for collision objects
602 MEM_freeN ( cloth->mfaces );
605 BLI_edgehash_free ( cloth->edgehash, NULL );
609 if(clmd->clothObject->facemarks)
610 MEM_freeN(clmd->clothObject->facemarks);
613 clmd->clothObject = NULL;
618 void cloth_free_modifier_extern ( ClothModifierData *clmd )
622 printf("cloth_free_modifier_extern\n");
627 cloth = clmd->clothObject;
632 printf("cloth_free_modifier_extern in\n");
634 // If our solver provides a free function, call it
635 if ( solvers [clmd->sim_parms->solver_type].free )
637 solvers [clmd->sim_parms->solver_type].free ( clmd );
641 if ( cloth->verts != NULL )
642 MEM_freeN ( cloth->verts );
648 if ( cloth->springs != NULL )
650 LinkNode *search = cloth->springs;
653 ClothSpring *spring = search->link;
655 MEM_freeN ( spring );
656 search = search->next;
658 BLI_linklist_free(cloth->springs, NULL);
660 cloth->springs = NULL;
663 cloth->springs = NULL;
664 cloth->numsprings = 0;
666 // free BVH collision tree
667 if ( cloth->bvhtree )
668 BLI_bvhtree_free ( cloth->bvhtree );
670 if ( cloth->bvhselftree )
671 BLI_bvhtree_free ( cloth->bvhselftree );
673 // we save our faces for collision objects
675 MEM_freeN ( cloth->mfaces );
678 BLI_edgehash_free ( cloth->edgehash, NULL );
682 if(clmd->clothObject->facemarks)
683 MEM_freeN(clmd->clothObject->facemarks);
686 clmd->clothObject = NULL;
690 /******************************************************************************
692 * Internal functions.
694 ******************************************************************************/
697 * cloth_to_object - copies the deformed vertices to the object.
700 static void cloth_to_object (Object *ob, ClothModifierData *clmd, DerivedMesh *dm)
704 unsigned int numverts;
705 Cloth *cloth = clmd->clothObject;
707 if (clmd->clothObject) {
708 /* inverse matrix is not uptodate... */
709 invert_m4_m4(ob->imat, ob->obmat);
711 mvert = CDDM_get_verts(dm);
712 numverts = dm->getNumVerts(dm);
714 for (i = 0; i < numverts; i++)
716 VECCOPY (mvert[i].co, cloth->verts[i].x);
717 mul_m4_v3(ob->imat, mvert[i].co); /* cloth is in global coords */
723 int cloth_uses_vgroup(ClothModifierData *clmd)
725 return (((clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_SCALING ) ||
726 (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL )) &&
727 ((clmd->sim_parms->vgroup_mass>0) ||
728 (clmd->sim_parms->vgroup_struct>0)||
729 (clmd->sim_parms->vgroup_bend>0)));
733 * cloth_apply_vgroup - applies a vertex group as specified by type
736 /* can be optimized to do all groups in one loop */
737 static void cloth_apply_vgroup ( ClothModifierData *clmd, DerivedMesh *dm )
741 MDeformVert *dvert = NULL;
742 Cloth *clothObj = NULL;
745 ClothVertex *verts = NULL;
747 if (!clmd || !dm) return;
749 clothObj = clmd->clothObject;
751 numverts = dm->getNumVerts ( dm );
753 verts = clothObj->verts;
755 if (cloth_uses_vgroup(clmd))
757 for ( i = 0; i < numverts; i++, verts++ )
759 dvert = dm->getVertData ( dm, i, CD_MDEFORMVERT );
762 for ( j = 0; j < dvert->totweight; j++ )
764 if (( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_mass-1)) && (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL ))
766 verts->goal = dvert->dw [j].weight;
770 // Kicking goal factor to simplify things...who uses that anyway?
771 // ABS ( clmd->sim_parms->maxgoal - clmd->sim_parms->mingoal );
774 verts->goal = ( float ) pow ( verts->goal , 4.0f );
775 if ( verts->goal >=SOFTGOALSNAP )
777 verts->flags |= CLOTH_VERT_FLAG_PINNED;
781 if (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_SCALING )
783 if( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_struct-1))
785 verts->struct_stiff = dvert->dw [j].weight;
786 verts->shear_stiff = dvert->dw [j].weight;
789 if( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_bend-1))
791 verts->bend_stiff = dvert->dw [j].weight;
796 if( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_weight-1))
798 verts->mass = dvert->dw [j].weight;
807 static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float UNUSED(framenr), int first)
811 ClothVertex *verts = NULL;
812 float (*shapekey_rest)[3]= NULL;
813 float tnull[3] = {0,0,0};
817 // If we have a clothObject, free it.
818 if ( clmd->clothObject != NULL )
820 cloth_free_modifier ( clmd );
822 printf("cloth_free_modifier cloth_from_object\n");
825 // Allocate a new cloth object.
826 clmd->clothObject = MEM_callocN ( sizeof ( Cloth ), "cloth" );
827 if ( clmd->clothObject )
829 clmd->clothObject->old_solver_type = 255;
830 // clmd->clothObject->old_collision_type = 255;
831 cloth = clmd->clothObject;
832 clmd->clothObject->edgehash = NULL;
834 else if ( !clmd->clothObject )
836 modifier_setError ( & ( clmd->modifier ), "Out of memory on allocating clmd->clothObject." );
840 // mesh input objects need DerivedMesh
844 cloth_from_mesh ( clmd, dm );
847 clmd->clothObject->springs = NULL;
848 clmd->clothObject->numsprings = -1;
850 if( clmd->sim_parms->shapekey_rest )
851 shapekey_rest = dm->getVertDataArray ( dm, CD_CLOTH_ORCO );
853 mvert = dm->getVertArray ( dm );
855 verts = clmd->clothObject->verts;
857 // set initial values
858 for ( i = 0; i < dm->getNumVerts(dm); i++, verts++ )
862 copy_v3_v3( verts->x, mvert[i].co );
864 mul_m4_v3( ob->obmat, verts->x );
866 if( shapekey_rest ) {
867 verts->xrest= shapekey_rest[i];
868 mul_m4_v3( ob->obmat, verts->xrest );
871 verts->xrest = verts->x;
874 /* no GUI interface yet */
875 verts->mass = clmd->sim_parms->mass;
876 verts->impulse_count = 0;
878 if ( clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL )
879 verts->goal= clmd->sim_parms->defgoal;
884 VECCOPY ( verts->xold, verts->x );
885 VECCOPY ( verts->xconst, verts->x );
886 VECCOPY ( verts->txold, verts->x );
887 VECCOPY ( verts->tx, verts->x );
888 mul_v3_fl( verts->v, 0.0f );
890 verts->impulse_count = 0;
891 VECCOPY ( verts->impulse, tnull );
894 // apply / set vertex groups
895 // has to be happen before springs are build!
896 cloth_apply_vgroup (clmd, dm);
898 if ( !cloth_build_springs ( clmd, dm ) )
900 cloth_free_modifier ( clmd );
901 modifier_setError ( & ( clmd->modifier ), "Can't build springs." );
902 printf("cloth_free_modifier cloth_build_springs\n");
906 for ( i = 0; i < dm->getNumVerts(dm); i++)
908 if((!(cloth->verts[i].flags & CLOTH_VERT_FLAG_PINNED)) && (cloth->verts[i].goal > ALMOST_ZERO))
910 cloth_add_spring (clmd, i, i, 0.0, CLOTH_SPRING_TYPE_GOAL);
915 if ( solvers [clmd->sim_parms->solver_type].init ) {
916 solvers [clmd->sim_parms->solver_type].init ( ob, clmd );
920 implicit_set_positions(clmd);
922 clmd->clothObject->bvhtree = bvhtree_build_from_cloth ( clmd, clmd->coll_parms->epsilon );
924 for(i = 0; i < dm->getNumVerts(dm); i++)
926 maxdist = MAX2(maxdist, clmd->coll_parms->selfepsilon* ( cloth->verts[i].avg_spring_len*2.0));
929 clmd->clothObject->bvhselftree = bvhselftree_build_from_cloth ( clmd, maxdist );
934 static void cloth_from_mesh ( ClothModifierData *clmd, DerivedMesh *dm )
936 unsigned int numverts = dm->getNumVerts ( dm );
937 unsigned int numfaces = dm->getNumFaces ( dm );
938 MFace *mface = CDDM_get_faces(dm);
941 /* Allocate our vertices. */
942 clmd->clothObject->numverts = numverts;
943 clmd->clothObject->verts = MEM_callocN ( sizeof ( ClothVertex ) * clmd->clothObject->numverts, "clothVertex" );
944 if ( clmd->clothObject->verts == NULL )
946 cloth_free_modifier ( clmd );
947 modifier_setError ( & ( clmd->modifier ), "Out of memory on allocating clmd->clothObject->verts." );
948 printf("cloth_free_modifier clmd->clothObject->verts\n");
952 // save face information
953 clmd->clothObject->numfaces = numfaces;
954 clmd->clothObject->mfaces = MEM_callocN ( sizeof ( MFace ) * clmd->clothObject->numfaces, "clothMFaces" );
955 if ( clmd->clothObject->mfaces == NULL )
957 cloth_free_modifier ( clmd );
958 modifier_setError ( & ( clmd->modifier ), "Out of memory on allocating clmd->clothObject->mfaces." );
959 printf("cloth_free_modifier clmd->clothObject->mfaces\n");
962 for ( i = 0; i < numfaces; i++ )
963 memcpy ( &clmd->clothObject->mfaces[i], &mface[i], sizeof ( MFace ) );
965 /* Free the springs since they can't be correct if the vertices
968 if ( clmd->clothObject->springs != NULL )
969 MEM_freeN ( clmd->clothObject->springs );
973 /***************************************************************************************
974 * SPRING NETWORK BUILDING IMPLEMENTATION BEGIN
975 ***************************************************************************************/
977 // be carefull: implicit solver has to be resettet when using this one!
978 // --> only for implicit handling of this spring!
979 int cloth_add_spring ( ClothModifierData *clmd, unsigned int indexA, unsigned int indexB, float restlength, int spring_type)
981 Cloth *cloth = clmd->clothObject;
982 ClothSpring *spring = NULL;
986 // TODO: look if this spring is already there
988 spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
995 spring->restlen = restlength;
996 spring->type = spring_type;
998 spring->stiffness = 0;
1000 cloth->numsprings++;
1002 BLI_linklist_prepend ( &cloth->springs, spring );
1009 static void cloth_free_errorsprings(Cloth *cloth, EdgeHash *UNUSED(edgehash), LinkNode **edgelist)
1013 if ( cloth->springs != NULL )
1015 LinkNode *search = cloth->springs;
1018 ClothSpring *spring = search->link;
1020 MEM_freeN ( spring );
1021 search = search->next;
1023 BLI_linklist_free(cloth->springs, NULL);
1025 cloth->springs = NULL;
1030 for ( i = 0; i < cloth->numverts; i++ )
1032 BLI_linklist_free ( edgelist[i],NULL );
1035 MEM_freeN ( edgelist );
1039 BLI_edgehash_free ( cloth->edgehash, NULL );
1042 static int cloth_build_springs ( ClothModifierData *clmd, DerivedMesh *dm )
1044 Cloth *cloth = clmd->clothObject;
1045 ClothSpring *spring = NULL, *tspring = NULL, *tspring2 = NULL;
1046 unsigned int struct_springs = 0, shear_springs=0, bend_springs = 0;
1048 int numverts = dm->getNumVerts ( dm );
1049 int numedges = dm->getNumEdges ( dm );
1050 int numfaces = dm->getNumFaces ( dm );
1051 MEdge *medge = CDDM_get_edges ( dm );
1052 MFace *mface = CDDM_get_faces ( dm );
1053 int index2 = 0; // our second vertex index
1054 LinkNode **edgelist = NULL;
1055 EdgeHash *edgehash = NULL;
1056 LinkNode *search = NULL, *search2 = NULL;
1063 cloth->springs = NULL;
1065 edgelist = MEM_callocN ( sizeof ( LinkNode * ) * numverts, "cloth_edgelist_alloc" );
1070 for ( i = 0; i < numverts; i++ )
1075 if ( cloth->springs )
1076 MEM_freeN ( cloth->springs );
1078 // create spring network hash
1079 edgehash = BLI_edgehash_new();
1081 // structural springs
1082 for ( i = 0; i < numedges; i++ )
1084 spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
1088 spring->ij = MIN2(medge[i].v1, medge[i].v2);
1089 spring->kl = MAX2(medge[i].v2, medge[i].v1);
1090 VECSUB ( temp, cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest );
1091 spring->restlen = sqrt ( INPR ( temp, temp ) );
1092 clmd->sim_parms->avg_spring_len += spring->restlen;
1093 cloth->verts[spring->ij].avg_spring_len += spring->restlen;
1094 cloth->verts[spring->kl].avg_spring_len += spring->restlen;
1095 cloth->verts[spring->ij].spring_count++;
1096 cloth->verts[spring->kl].spring_count++;
1097 spring->type = CLOTH_SPRING_TYPE_STRUCTURAL;
1099 spring->stiffness = (cloth->verts[spring->kl].struct_stiff + cloth->verts[spring->ij].struct_stiff) / 2.0;
1102 BLI_linklist_prepend ( &cloth->springs, spring );
1106 cloth_free_errorsprings(cloth, edgehash, edgelist);
1111 if(struct_springs > 0)
1112 clmd->sim_parms->avg_spring_len /= struct_springs;
1114 for(i = 0; i < numverts; i++)
1116 cloth->verts[i].avg_spring_len = cloth->verts[i].avg_spring_len * 0.49 / ((float)cloth->verts[i].spring_count);
1120 for ( i = 0; i < numfaces; i++ )
1122 // triangle faces already have shear springs due to structural geometry
1126 spring = ( ClothSpring *) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
1130 cloth_free_errorsprings(cloth, edgehash, edgelist);
1134 spring->ij = MIN2(mface[i].v1, mface[i].v3);
1135 spring->kl = MAX2(mface[i].v3, mface[i].v1);
1136 VECSUB ( temp, cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest );
1137 spring->restlen = sqrt ( INPR ( temp, temp ) );
1138 spring->type = CLOTH_SPRING_TYPE_SHEAR;
1139 spring->stiffness = (cloth->verts[spring->kl].shear_stiff + cloth->verts[spring->ij].shear_stiff) / 2.0;
1141 BLI_linklist_append ( &edgelist[spring->ij], spring );
1142 BLI_linklist_append ( &edgelist[spring->kl], spring );
1145 BLI_linklist_prepend ( &cloth->springs, spring );
1148 // if ( mface[i].v4 ) --> Quad face
1149 spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
1153 cloth_free_errorsprings(cloth, edgehash, edgelist);
1157 spring->ij = MIN2(mface[i].v2, mface[i].v4);
1158 spring->kl = MAX2(mface[i].v4, mface[i].v2);
1159 VECSUB ( temp, cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest );
1160 spring->restlen = sqrt ( INPR ( temp, temp ) );
1161 spring->type = CLOTH_SPRING_TYPE_SHEAR;
1162 spring->stiffness = (cloth->verts[spring->kl].shear_stiff + cloth->verts[spring->ij].shear_stiff) / 2.0;
1164 BLI_linklist_append ( &edgelist[spring->ij], spring );
1165 BLI_linklist_append ( &edgelist[spring->kl], spring );
1168 BLI_linklist_prepend ( &cloth->springs, spring );
1173 search2 = cloth->springs;
1174 for ( i = struct_springs; i < struct_springs+shear_springs; i++ )
1179 tspring2 = search2->link;
1180 search = edgelist[tspring2->kl];
1183 tspring = search->link;
1184 index2 = ( ( tspring->ij==tspring2->kl ) ? ( tspring->kl ) : ( tspring->ij ) );
1186 // check for existing spring
1187 // check also if startpoint is equal to endpoint
1188 if ( !BLI_edgehash_haskey ( edgehash, MIN2(tspring2->ij, index2), MAX2(tspring2->ij, index2) )
1189 && ( index2!=tspring2->ij ) )
1191 spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
1195 cloth_free_errorsprings(cloth, edgehash, edgelist);
1199 spring->ij = MIN2(tspring2->ij, index2);
1200 spring->kl = MAX2(tspring2->ij, index2);
1201 VECSUB ( temp, cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest );
1202 spring->restlen = sqrt ( INPR ( temp, temp ) );
1203 spring->type = CLOTH_SPRING_TYPE_BENDING;
1204 spring->stiffness = (cloth->verts[spring->kl].bend_stiff + cloth->verts[spring->ij].bend_stiff) / 2.0;
1205 BLI_edgehash_insert ( edgehash, spring->ij, spring->kl, NULL );
1208 BLI_linklist_prepend ( &cloth->springs, spring );
1210 search = search->next;
1212 search2 = search2->next;
1215 else if(struct_springs > 2) {
1216 /* bending springs for hair strands */
1217 /* The current algorightm only goes through the edges in order of the mesh edges list */
1218 /* and makes springs between the outer vert of edges sharing a vertice. This works just */
1219 /* fine for hair, but not for user generated string meshes. This could/should be later */
1220 /* extended to work with non-ordered edges so that it can be used for general "rope */
1221 /* dynamics" without the need for the vertices or edges to be ordered through the length*/
1222 /* of the strands. -jahka */
1223 search = cloth->springs;
1224 search2 = search->next;
1225 while(search && search2)
1227 tspring = search->link;
1228 tspring2 = search2->link;
1230 if(tspring->ij == tspring2->kl) {
1231 spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
1235 cloth_free_errorsprings(cloth, edgehash, edgelist);
1239 spring->ij = tspring2->ij;
1240 spring->kl = tspring->kl;
1241 VECSUB ( temp, cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest );
1242 spring->restlen = sqrt ( INPR ( temp, temp ) );
1243 spring->type = CLOTH_SPRING_TYPE_BENDING;
1244 spring->stiffness = (cloth->verts[spring->kl].bend_stiff + cloth->verts[spring->ij].bend_stiff) / 2.0;
1247 BLI_linklist_prepend ( &cloth->springs, spring );
1250 search = search->next;
1251 search2 = search2->next;
1255 /* insert other near springs in edgehash AFTER bending springs are calculated (for selfcolls) */
1256 for ( i = 0; i < numedges; i++ ) // struct springs
1257 BLI_edgehash_insert ( edgehash, MIN2(medge[i].v1, medge[i].v2), MAX2(medge[i].v2, medge[i].v1), NULL );
1259 for ( i = 0; i < numfaces; i++ ) // edge springs
1263 BLI_edgehash_insert ( edgehash, MIN2(mface[i].v1, mface[i].v3), MAX2(mface[i].v3, mface[i].v1), NULL );
1265 BLI_edgehash_insert ( edgehash, MIN2(mface[i].v2, mface[i].v4), MAX2(mface[i].v2, mface[i].v4), NULL );
1270 cloth->numsprings = struct_springs + shear_springs + bend_springs;
1274 for ( i = 0; i < numverts; i++ )
1276 BLI_linklist_free ( edgelist[i],NULL );
1279 MEM_freeN ( edgelist );
1282 cloth->edgehash = edgehash;
1285 printf("avg_len: %f\n",clmd->sim_parms->avg_spring_len);
1289 } /* cloth_build_springs */
1290 /***************************************************************************************
1291 * SPRING NETWORK BUILDING IMPLEMENTATION END
1292 ***************************************************************************************/