-= Cloth cleanup =-
[blender.git] / source / blender / blenkernel / intern / implicit.c
index 7ea48d6629b0aa3c31cff60c614f42b0665ca21e..cda56f5b60166d0c4a2a157b2b3afd59173c63ab 100644 (file)
@@ -82,7 +82,7 @@ void itend(void)
 double itval()
 {
        return ((double)_itend.QuadPart -
-               (double)_itstart.QuadPart)/((double)ifreq.QuadPart);
+                       (double)_itstart.QuadPart)/((double)ifreq.QuadPart);
 }
 #else
 #include <sys/time.h>
@@ -91,9 +91,9 @@ double itval()
 // #include <pmmintrin.h>
 // #include <pthread.h>
 
-static struct timeval _itstart, _itend;
-static struct timezone itz;
-void itstart(void)
+                        static struct timeval _itstart, _itend;
+        static struct timezone itz;
+        void itstart(void)
 {
        gettimeofday(&_itstart, &itz);
 }
@@ -331,7 +331,7 @@ DO_INLINE void cp_fmatrix(float to[3][3], float from[3][3])
 DO_INLINE float det_fmatrix(float m[3][3])
 {
        return  m[0][0]*m[1][1]*m[2][2] + m[1][0]*m[2][1]*m[0][2] + m[0][1]*m[1][2]*m[2][0] 
-       -m[0][0]*m[1][2]*m[2][1] - m[0][1]*m[1][0]*m[2][2] - m[2][0]*m[1][1]*m[0][2];
+                       -m[0][0]*m[1][2]*m[2][1] - m[0][1]*m[1][0]*m[2][2] - m[2][0]*m[1][1]*m[0][2];
 }
 DO_INLINE void inverse_fmatrix(float to[3][3], float from[3][3])
 {
@@ -719,6 +719,9 @@ int implicit_init (Object *ob, ClothModifierData *clmd)
        ClothSpring *spring = NULL;
        Implicit_Data *id = NULL;
        LinkNode *search = NULL;
+       
+       if(G.rt > 0)
+       printf("implicit_init\n");
 
        // init memory guard
        // MEMORY_BASE.first = MEMORY_BASE.last = NULL;
@@ -776,7 +779,7 @@ int implicit_init (Object *ob, ClothModifierData *clmd)
 
                // dFdV_start[i].c = big_I[i].c = big_zero[i].c = 
                id->A[i+cloth->numverts].c = id->dFdV[i+cloth->numverts].c = id->dFdX[i+cloth->numverts].c = 
-                       id->P[i+cloth->numverts].c = id->Pinv[i+cloth->numverts].c = id->bigI[i+cloth->numverts].c = spring->kl;
+                               id->P[i+cloth->numverts].c = id->Pinv[i+cloth->numverts].c = id->bigI[i+cloth->numverts].c = spring->kl;
 
                spring->matrix_index = i + cloth->numverts;
                
@@ -1023,7 +1026,7 @@ int cg_filtered_pre(lfVector *dv, fmatrix3x3 *lA, lfVector *lB, lfVector *z, fma
        del_lfvector(p);
        del_lfvector(r);
        
-       // printf("iterations: %d\n", iterations);
+       printf("iterations: %d\n", iterations);
        
        return iterations<conjgrad_looplimit;
 }
@@ -1109,13 +1112,13 @@ DO_INLINE void cloth_calc_spring_force(ClothModifierData *clmd, ClothSpring *s,
                /*
                if(length>L)
                {
-                       if((clmd->sim_parms->flags & CSIMSETT_FLAG_TEARING_ENABLED) 
-                       && ((((length-L)*100.0f/L) > clmd->sim_parms->maxspringlen))) // cut spring!
-                       {
-                               s->flags |= CSPRING_FLAG_DEACTIVATE;
-                               return;
-                       }
-               
+               if((clmd->sim_parms->flags & CSIMSETT_FLAG_TEARING_ENABLED) 
+               && ((((length-L)*100.0f/L) > clmd->sim_parms->maxspringlen))) // cut spring!
+               {
+               s->flags |= CSPRING_FLAG_DEACTIVATE;
+               return;
+       }
+       } 
                */
                mul_fvector_S(dir, extent, 1.0f/length);
        }
@@ -1133,8 +1136,18 @@ DO_INLINE void cloth_calc_spring_force(ClothModifierData *clmd, ClothSpring *s,
                        
                        k = clmd->sim_parms->structural;
                                
-                       scaling = k + s->stiffness * (clmd->sim_parms->max_struct-k);
+                       scaling = k + s->stiffness * ABS(clmd->sim_parms->max_struct-k);
                        k = scaling;
+                       
+                       // printf("scaling: %f, stiffness: %f\n", k, s->stiffness);
+                       /*
+                       if((s->ij == 109) || (s->kl == 109))
+                       {
+                               printf("length-L: %f, f: %f, len: %f, L: %f\n", length-L, (k*(length-L)), length, L);
+                               printf("kl X-x: %f, f-y: %f, f-z: %f\n", X[s->kl][0], X[s->kl][1], X[s->kl][2]);
+                               printf("ij X-x: %f, f-y: %f, f-z: %f\n\n", X[s->ij][0], X[s->ij][1], X[s->ij][2]);
+                       }
+                       */
 
                        mul_fvector_S(stretch_force, dir, (k*(length-L))); 
 
@@ -1157,7 +1170,7 @@ DO_INLINE void cloth_calc_spring_force(ClothModifierData *clmd, ClothSpring *s,
                        
                        k = clmd->sim_parms->bending;   
                        
-                       scaling = k + s->stiffness * (clmd->sim_parms->max_bend-k);
+                       scaling = k + s->stiffness * ABS(clmd->sim_parms->max_bend-k);
                        cb = k = scaling;
 
                        mul_fvector_S(bending_force, dir, fbstar(length, L, k, cb));
@@ -1166,6 +1179,12 @@ DO_INLINE void cloth_calc_spring_force(ClothModifierData *clmd, ClothSpring *s,
                        dfdx_spring_type2(s->dfdx, dir,length,L,k, cb);
                }
        }
+       /*
+       if((s->ij == 109) || (s->kl == 109))
+       {
+       printf("type: %d, f-x: %f, f-y: %f, f-z: %f\n", s->type, s->f[0], s->f[1], s->f[2]);
+}
+       */
 }
 
 DO_INLINE void cloth_apply_spring_force(ClothModifierData *clmd, ClothSpring *s, lfVector *lF, lfVector *X, lfVector *V, fmatrix3x3 *dFdV, fmatrix3x3 *dFdX)
@@ -1258,7 +1277,7 @@ void cloth_calc_force(ClothModifierData *clmd, lfVector *lF, lfVector *lX, lfVec
        init_lfvector(lF, gravity, numverts);
 
        submul_lfvectorS(lF, lV, spring_air, numverts);
-
+               
        /* do goal stuff */
        if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL) 
        {       
@@ -1275,22 +1294,24 @@ void cloth_calc_force(ClothModifierData *clmd, lfVector *lF, lfVector *lX, lfVec
                                ks  = 1.0f/(1.0f- verts [i].goal*clmd->sim_parms->goalspring)-1.0f ;
                                VECADDS(lF[i], lF[i], auxvect, -ks);
 
-                               // calulate damping forces generated by goals                           
+                               // calulate damping forces generated by goals
+                               
                                VECSUB(velgoal,verts[i].xold, verts[i].xconst);
                                kd =  clmd->sim_parms->goalfrict * 0.01f; // friction force scale taken from SB
                                VECSUBADDSS(lF[i], velgoal, kd, lV[i], kd);
                                
+                               
                        }
                }       
        }
-
+       
        /* handle external forces like wind */
        if(effectors)
        {
                float speed[3] = {0.0f, 0.0f,0.0f};
                float force[3]= {0.0f, 0.0f, 0.0f};
                
-               #pragma omp parallel for private (i) shared(lF)
+#pragma omp parallel for private (i) shared(lF)
                for(i = 0; i < cloth->numverts; i++)
                {
                        float vertexnormal[3]={0,0,0};
@@ -1308,7 +1329,7 @@ void cloth_calc_force(ClothModifierData *clmd, lfVector *lF, lfVector *lX, lfVec
                        VECADDS(lF[i], lF[i], wind_normalized, -calculateVertexWindForce(speed, vertexnormal));
                }
        }
-       
+               
        // calculate spring forces
        search = cloth->springs;
        while(search)
@@ -1329,6 +1350,7 @@ void cloth_calc_force(ClothModifierData *clmd, lfVector *lF, lfVector *lX, lfVec
                cloth_apply_spring_force(clmd, search->link, lF, lX, lV, dFdV, dFdX);
                search = search->next;
        }
+       // printf("\n");
 }
 
 void simulate_implicit_euler(lfVector *Vnew, lfVector *lX, lfVector *lV, lfVector *lF, fmatrix3x3 *dFdV, fmatrix3x3 *dFdX, float dt, fmatrix3x3 *A, lfVector *B, lfVector *dV, fmatrix3x3 *S, lfVector *z, lfVector *olddV, fmatrix3x3 *P, fmatrix3x3 *Pinv)
@@ -1391,11 +1413,23 @@ int implicit_solver (Object *ob, float frame, ClothModifierData *clmd, ListBase
                effectors= pdInitEffectors(ob,NULL);
                
                // calculate 
-               cloth_calc_force(clmd, id->F, id->X, id->V, id->dFdV, id->dFdX, effectors, step );      
+               cloth_calc_force(clmd, id->F, id->X, id->V, id->dFdV, id->dFdX, effectors, step );
+               
+               // printf("F -> x: %f, y: %f; z: %f\n\n", id->F[109][0], id->F[109][1], id->F[109][2]);
+                       
                simulate_implicit_euler(id->Vnew, id->X, id->V, id->F, id->dFdV, id->dFdX, dt, id->A, id->B, id->dV, id->S, id->z, id->olddV, id->P, id->Pinv);
                
                add_lfvector_lfvectorS(id->Xnew, id->X, id->Vnew, dt, numverts);
                
+               /*
+               printf("dt: %f\n", dt);
+               printf("Xnew -> x: %f, y: %f; z: %f\n", id->Xnew[109][0], id->Xnew[109][1], id->Xnew[109][2]);
+               printf("X    -> x: %f, y: %f; z: %f\n", id->X[109][0], id->X[109][1], id->X[109][2]);
+               printf("Vnew -> x: %f, y: %f; z: %f\n\n", id->Vnew[109][0], id->Vnew[109][1], id->Vnew[109][2]);
+               */
+               
+               // clmd->coll_parms->flags &= ~CLOTH_COLLSETTINGS_FLAG_ENABLED;
+               
                if(clmd->coll_parms->flags & CLOTH_COLLSETTINGS_FLAG_ENABLED)
                {
                        // collisions 
@@ -1404,7 +1438,8 @@ int implicit_solver (Object *ob, float frame, ClothModifierData *clmd, ListBase
                        // update verts to current positions
                        for(i = 0; i < numverts; i++)
                        {               
-                               if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL) /* do goal stuff */
+                               
+                               if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL) 
                                {                       
                                        if(verts [i].goal >= SOFTGOALSNAP)
                                        {                       
@@ -1431,6 +1466,16 @@ int implicit_solver (Object *ob, float frame, ClothModifierData *clmd, ListBase
                        {               
                                if(result)
                                {
+                                       
+                                       if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL) 
+                                       {                       
+                                               if(verts [i].goal >= SOFTGOALSNAP)
+                                               {
+                                                       continue;
+                                               }
+                                       }
+                                       
+                                               
                                        // VECADD(verts[i].tx, verts[i].txold, verts[i].tv);
                                        
                                        VECCOPY(verts[i].txold, verts[i].tx);
@@ -1515,5 +1560,7 @@ void implicit_set_positions (ClothModifierData *clmd)
        {                               
                VECCOPY(id->X[i], verts[i].x);
                VECCOPY(id->V[i], verts[i].v);
-       }       
+       }
+       if(G.rt > 0)
+       printf("implicit_set_positions\n");     
 }