use static functions where possible for some local functions.
[blender.git] / source / blender / blenkernel / intern / collision.c
index 7f41ca033d330183c749c42f6a2fa83de57fcda1..8ef1c28537080f9fb3e20fc88872949227fee7af 100644 (file)
 
 #include "BKE_cloth.h"
 
-#include "DNA_group_types.h"
-#include "DNA_object_types.h"
 #include "DNA_cloth_types.h"
+#include "DNA_group_types.h"
 #include "DNA_mesh_types.h"
+#include "DNA_object_types.h"
+#include "DNA_object_force.h"
 #include "DNA_scene_types.h"
 
 #include "BKE_DerivedMesh.h"
@@ -44,7 +45,6 @@
 #include "BKE_modifier.h"
 #include "BKE_utildefines.h"
 #include "BKE_DerivedMesh.h"
-#include "mydevice.h"
 
 #include "Bullet-C-Api.h"
 
@@ -313,7 +313,7 @@ gsl_poly_solve_quadratic (double a, double b, double c,
 * See Bridson et al. "Robust Treatment of Collision, Contact and Friction for Cloth Animation"
 *     page 4, left column
 */
-int cloth_get_collision_time ( double a[3], double b[3], double c[3], double d[3], double e[3], double f[3], double solution[3] )
+static int cloth_get_collision_time ( double a[3], double b[3], double c[3], double d[3], double e[3], double f[3], double solution[3] )
 {
        int num_sols = 0;
 
@@ -427,7 +427,7 @@ int cloth_get_collision_time ( double a[3], double b[3], double c[3], double d[3
 
 
 // w3 is not perfect
-void collision_compute_barycentric ( float pv[3], float p1[3], float p2[3], float p3[3], float *w1, float *w2, float *w3 )
+static void collision_compute_barycentric ( float pv[3], float p1[3], float p2[3], float p3[3], float *w1, float *w2, float *w3 )
 {
        double  tempV1[3], tempV2[3], tempV4[3];
        double  a,b,c,d,e,f;
@@ -541,7 +541,7 @@ int cloth_collision_response_static ( ClothModifierData *clmd, CollisionModifier
                        {
                                Normalize ( vrel_t_pre );
 
-                               impulse = 2.0 * magtangent / ( 1.0 + w1*w1 + w2*w2 + w3*w3 );
+                               impulse = magtangent / ( 1.0 + w1*w1 + w2*w2 + w3*w3 ); // 2.0 * 
                                VECADDMUL ( cloth1->verts[collpair->ap1].impulse, vrel_t_pre, w1 * impulse );
                                VECADDMUL ( cloth1->verts[collpair->ap2].impulse, vrel_t_pre, w2 * impulse );
                                VECADDMUL ( cloth1->verts[collpair->ap3].impulse, vrel_t_pre, w3 * impulse );
@@ -591,7 +591,9 @@ CollPair* cloth_collision ( ModifierData *md1, ModifierData *md2, BVHTreeOverlap
        ClothModifierData *clmd = ( ClothModifierData * ) md1;
        CollisionModifierData *collmd = ( CollisionModifierData * ) md2;
        MFace *face1=NULL, *face2 = NULL;
+#ifdef USE_BULLET
        ClothVertex *verts1 = clmd->clothObject->verts;
+#endif
        double distance = 0;
        float epsilon1 = clmd->coll_parms->epsilon;
        float epsilon2 = BLI_bvhtree_getepsilon ( collmd->bvhtree );
@@ -667,7 +669,7 @@ CollPair* cloth_collision ( ModifierData *md1, ModifierData *md2, BVHTreeOverlap
                                break;
                }
 
-#ifdef WITH_BULLET
+#ifdef USE_BULLET
                // calc distance + normal
                distance = plNearestPoints (
                        verts1[collpair->ap1].txold, verts1[collpair->ap2].txold, verts1[collpair->ap3].txold, collmd->current_x[collpair->bp1].co, collmd->current_x[collpair->bp2].co, collmd->current_x[collpair->bp3].co, collpair->pa,collpair->pb,collpair->vector );
@@ -724,7 +726,7 @@ CollPair* cloth_collision ( ModifierData *md1, ModifierData *md2, BVHTreeOverlap
        return collpair;
 }
 
-int cloth_collision_response_moving( ClothModifierData *clmd, CollisionModifierData *collmd, CollPair *collpair, CollPair *collision_end )
+static int cloth_collision_response_moving( ClothModifierData *clmd, CollisionModifierData *collmd, CollPair *collpair, CollPair *collision_end )
 {
        int result = 0;
        Cloth *cloth1;
@@ -889,7 +891,7 @@ static void findClosestPointsEE(float *x1, float *x2, float *x3, float *x4, floa
 }
 
 // calculates the distance of 2 edges
-float edgedge_distance(float np11[3], float np12[3], float np21[3], float np22[3], float *out_a1, float *out_a2, float *out_normal)
+static float edgedge_distance(float np11[3], float np12[3], float np21[3], float np22[3], float *out_a1, float *out_a2, float *out_normal)
 {
        float line1[3], line2[3], cross[3];
        float length;
@@ -1063,7 +1065,7 @@ float edgedge_distance(float np11[3], float np12[3], float np21[3], float np22[3
        return 0;
 }
 
-int cloth_collision_moving_edges ( ClothModifierData *clmd, CollisionModifierData *collmd, CollPair *collpair )
+static int cloth_collision_moving_edges ( ClothModifierData *clmd, CollisionModifierData *collmd, CollPair *collpair )
 {
        EdgeCollPair edgecollpair;
        Cloth *cloth1=NULL;
@@ -1235,7 +1237,7 @@ int cloth_collision_moving_edges ( ClothModifierData *clmd, CollisionModifierDat
                                                if(out_normalVelocity < 0.0)
                                                {
                                                        out_normalVelocity*= -1.0;
-                                                       VecMulf(out_normal, -1.0);
+                                                       VecNegf(out_normal);
                                                }
 */
                                                /* Inelastic repulsion impulse. */
@@ -1273,7 +1275,7 @@ int cloth_collision_moving_edges ( ClothModifierData *clmd, CollisionModifierDat
        return result;
 }
 
-int cloth_collision_moving ( ClothModifierData *clmd, CollisionModifierData *collmd, CollPair *collpair, CollPair *collision_end )
+static int cloth_collision_moving ( ClothModifierData *clmd, CollisionModifierData *collmd, CollPair *collpair, CollPair *collision_end )
 {
        Cloth *cloth1;
        cloth1 = clmd->clothObject;
@@ -1291,52 +1293,132 @@ int cloth_collision_moving ( ClothModifierData *clmd, CollisionModifierData *col
        return 1;
 }
 
-int cloth_bvh_objcollisions_do ( ClothModifierData * clmd, CollisionModifierData *collmd, float step, float dt )
-{
-       Cloth *cloth = clmd->clothObject;
-       BVHTree *cloth_bvh= ( BVHTree * ) cloth->bvhtree;
-       long i=0, j = 0, numfaces = 0, numverts = 0;
-       ClothVertex *verts = NULL;
-       CollPair *collisions = NULL, *collisions_index = NULL;
-       int ret = 0;
-       int result = 0;
-       float tnull[3] = {0,0,0};
-       BVHTreeOverlap *overlap = NULL;
-
-
-       numfaces = clmd->clothObject->numfaces;
-       numverts = clmd->clothObject->numverts;
-
-       verts = cloth->verts;
 
-       if ( collmd->bvhtree )
+// return all collision objects in scene
+// collision object will exclude self 
+CollisionModifierData **get_collisionobjects(Scene *scene, Object *self, int *numcollobj)
+{
+       Base *base=NULL;
+       CollisionModifierData **objs = NULL;
+       Object *coll_ob = NULL;
+       CollisionModifierData *collmd = NULL;
+       int numobj = 0, maxobj = 100;
+       
+       objs = MEM_callocN(sizeof(CollisionModifierData *)*maxobj, "CollisionObjectsArray");
+       // check all collision objects
+       for ( base = scene->base.first; base; base = base->next )
        {
-               /* get pointer to bounding volume hierarchy */
-               BVHTree *coll_bvh = collmd->bvhtree;
+               /*Only proceed for mesh object in same layer */
+               if(!(base->object->type==OB_MESH && (base->lay & self->lay))) 
+                       continue;
+               
+               coll_ob = base->object;
+               
+               if(coll_ob == self)
+                               continue;
+               
+               if(coll_ob->pd && coll_ob->pd->deflect)
+               {
+                       collmd = ( CollisionModifierData * ) modifiers_findByType ( coll_ob, eModifierType_Collision );
+               }
+               else
+                       collmd = NULL;
+               
+               if ( collmd )
+               {       
+                       if(numobj >= maxobj)
+                       {
+                               // realloc
+                               int oldmax = maxobj;
+                               CollisionModifierData **tmp;
+                               maxobj *= 2;
+                               tmp = MEM_callocN(sizeof(CollisionModifierData *)*maxobj, "CollisionObjectsArray");
+                               memcpy(tmp, objs, sizeof(CollisionModifierData *)*oldmax);
+                               MEM_freeN(objs);
+                               objs = tmp;
+                               
+                       }
+                       
+                       objs[numobj] = collmd;
+                       numobj++;
+               }
+               else
+               {
+                       if ( coll_ob->dup_group )
+                       {
+                               GroupObject *go;
+                               Group *group = coll_ob->dup_group;
 
-               /* move object to position (step) in time */
-               collision_move_object ( collmd, step + dt, step );
+                               for ( go= group->gobject.first; go; go= go->next )
+                               {
+                                       coll_ob = go->ob;
+                                       collmd = NULL;
+                                       
+                                       if(coll_ob == self)
+                                               continue;
+                                       
+                                       if(coll_ob->pd && coll_ob->pd->deflect)
+                                       {
+                                               collmd = ( CollisionModifierData * ) modifiers_findByType ( coll_ob, eModifierType_Collision );
+                                       }
+                                       else
+                                               collmd = NULL;
 
-               /* search for overlapping collision pairs */
-               overlap = BLI_bvhtree_overlap ( cloth_bvh, coll_bvh, &result );
+                                       if ( !collmd )
+                                               continue;
+                                       
+                                       if( !collmd->bvhtree)
+                                               continue;
 
-               collisions = ( CollPair* ) MEM_mallocN ( sizeof ( CollPair ) * result*4, "collision array" ); //*4 since cloth_collision_static can return more than 1 collision
-               collisions_index = collisions;
+                                       if(numobj >= maxobj)
+                                       {
+                                               // realloc
+                                               int oldmax = maxobj;
+                                               CollisionModifierData **tmp;
+                                               maxobj *= 2;
+                                               tmp = MEM_callocN(sizeof(CollisionModifierData *)*maxobj, "CollisionObjectsArray");
+                                               memcpy(tmp, objs, sizeof(CollisionModifierData *)*oldmax);
+                                               MEM_freeN(objs);
+                                               objs = tmp;
+                                       }
+                                       
+                                       objs[numobj] = collmd;
+                                       numobj++;
+                               }
+                       }
+               }       
+       }
+       *numcollobj = numobj;
+       return objs;
+}
 
-               for ( i = 0; i < result; i++ )
-               {
-                       collisions_index = cloth_collision ( ( ModifierData * ) clmd, ( ModifierData * ) collmd, overlap+i, collisions_index );
-               }
+static void cloth_bvh_objcollisions_nearcheck ( ClothModifierData * clmd, CollisionModifierData *collmd, CollPair **collisions, CollPair **collisions_index, int numresult, BVHTreeOverlap *overlap)
+{
+       int i;
+       
+       *collisions = ( CollPair* ) MEM_mallocN ( sizeof ( CollPair ) * numresult * 4, "collision array" ); //*4 since cloth_collision_static can return more than 1 collision
+       *collisions_index = *collisions;
 
-               if ( overlap )
-                       MEM_freeN ( overlap );
-       }
-       else
+       for ( i = 0; i < numresult; i++ )
        {
-               if ( G.rt > 0 )
-                       printf ( "cloth_bvh_objcollision: found a collision object with clothObject or collData NULL.\n" );
+               *collisions_index = cloth_collision ( ( ModifierData * ) clmd, ( ModifierData * ) collmd, overlap+i, *collisions_index );
        }
+}
 
+static int cloth_bvh_objcollisions_resolve ( ClothModifierData * clmd, CollisionModifierData *collmd, CollPair *collisions, CollPair *collisions_index)
+{
+       Cloth *cloth = clmd->clothObject;
+       int i=0, j = 0, numfaces = 0, numverts = 0;
+       ClothVertex *verts = NULL;
+       int ret = 0;
+       int result = 0;
+       float tnull[3] = {0,0,0};
+       
+       numfaces = clmd->clothObject->numfaces;
+       numverts = clmd->clothObject->numverts;
+       verts = cloth->verts;
+       
        // process all collisions (calculate impulses, TODO: also repulses if distance too short)
        result = 1;
        for ( j = 0; j < 5; j++ ) // 5 is just a value that ensures convergence
@@ -1363,48 +1445,22 @@ int cloth_bvh_objcollisions_do ( ClothModifierData * clmd, CollisionModifierData
                                        }
                                }
                        }
-/*
-                       result += cloth_collision_moving ( clmd, collmd, collisions, collisions_index );
-
-                       // apply impulses in parallel
-                       if ( result )
-                       {
-                               for ( i = 0; i < numverts; i++ )
-                               {
-                                       // calculate "velocities" (just xnew = xold + v; no dt in v)
-                                       if ( verts[i].impulse_count )
-                                       {
-                                               VECADDMUL ( verts[i].tv, verts[i].impulse, 1.0f / verts[i].impulse_count );
-                                               VECCOPY ( verts[i].impulse, tnull );
-                                               verts[i].impulse_count = 0;
-
-                                               ret++;
-                                       }
-                               }
-                       }
-*/
                }
        }
-
-       if ( collisions ) MEM_freeN ( collisions );
-
        return ret;
 }
 
 // cloth - object collisions
-int cloth_bvh_objcollision ( ClothModifierData * clmd, float step, float dt )
+int cloth_bvh_objcollision (Object *ob, ClothModifierData * clmd, float step, float dt )
 {
-       Base *base=NULL;
-       CollisionModifierData *collmd=NULL;
        Cloth *cloth=NULL;
-       Object *coll_ob=NULL;
        BVHTree *cloth_bvh=NULL;
-       long i=0, j = 0, k = 0, l = 0, numfaces = 0, numverts = 0;
-       int result = 0, rounds = 0; // result counts applied collisions; ic is for debug output;
+       int i=0, numfaces = 0, numverts = 0, k, l, j;
+       int rounds = 0; // result counts applied collisions; ic is for debug output;
        ClothVertex *verts = NULL;
        int ret = 0, ret2 = 0;
-       ClothModifierData *tclmd;
-       int collisions = 0;
+       CollisionModifierData **collobjs = NULL;
+       int numcollobj = 0;
 
        if ( ( clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_COLLOBJ ) || ! ( ( ( Cloth * ) clmd->clothObject )->bvhtree ) )
        {
@@ -1424,54 +1480,64 @@ int cloth_bvh_objcollision ( ClothModifierData * clmd, float step, float dt )
        // update cloth bvh
        bvhtree_update_from_cloth ( clmd, 1 ); // 0 means STATIC, 1 means MOVING (see later in this function)
        bvhselftree_update_from_cloth ( clmd, 0 ); // 0 means STATIC, 1 means MOVING (see later in this function)
+       
+       collobjs = get_collisionobjects(clmd->scene, ob, &numcollobj);
+       
+       if(!collobjs)
+               return 0;
 
        do
        {
-               result = 0;
+               CollPair **collisions, **collisions_index;
+               
                ret2 = 0;
 
+               collisions = MEM_callocN(sizeof(CollPair *) *numcollobj , "CollPair");
+               collisions_index = MEM_callocN(sizeof(CollPair *) *numcollobj , "CollPair");
+               
                // check all collision objects
-               for ( base = G.scene->base.first; base; base = base->next )
+               for(i = 0; i < numcollobj; i++)
                {
-                       coll_ob = base->object;
-                       collmd = ( CollisionModifierData * ) modifiers_findByType ( coll_ob, eModifierType_Collision );
-
-                       if ( !collmd )
-                       {
-                               if ( coll_ob->dup_group )
-                               {
-                                       GroupObject *go;
-                                       Group *group = coll_ob->dup_group;
-
-                                       for ( go= group->gobject.first; go; go= go->next )
-                                       {
-                                               coll_ob = go->ob;
-
-                                               collmd = ( CollisionModifierData * ) modifiers_findByType ( coll_ob, eModifierType_Collision );
-
-                                               if ( !collmd )
-                                                       continue;
-
-                                               tclmd = ( ClothModifierData * ) modifiers_findByType ( coll_ob, eModifierType_Cloth );
-                                               if ( tclmd == clmd )
-                                                       continue;
-
-                                               ret += cloth_bvh_objcollisions_do ( clmd, collmd, step, dt );
-                                               ret2 += ret;
-                                       }
-                               }
-                       }
-                       else
+                       CollisionModifierData *collmd = collobjs[i];
+                       BVHTreeOverlap *overlap = NULL;
+                       int result = 0;
+                       
+                       if(!collmd->bvhtree)
+                               continue;
+                       
+                       /* move object to position (step) in time */
+                       collision_move_object ( collmd, step + dt, step );
+                       
+                       /* search for overlapping collision pairs */
+                       overlap = BLI_bvhtree_overlap ( cloth_bvh, collmd->bvhtree, &result );
+                               
+                       // go to next object if no overlap is there
+                       if(!result || !overlap)
                        {
-                               tclmd = ( ClothModifierData * ) modifiers_findByType ( coll_ob, eModifierType_Cloth );
-                               if ( tclmd == clmd )
-                                       continue;
-
-                               ret += cloth_bvh_objcollisions_do ( clmd, collmd, step, dt );
-                               ret2 += ret;
+                               if ( overlap )
+                                       MEM_freeN ( overlap );
+                               continue;
                        }
+                       
+                       /* check if collisions really happen (costly near check) */
+                       cloth_bvh_objcollisions_nearcheck ( clmd, collmd, &collisions[i], &collisions_index[i], result, overlap);
+                       
+                       // resolve nearby collisions
+                       ret += cloth_bvh_objcollisions_resolve ( clmd, collmd, collisions[i],  collisions_index[i]);
+                       ret2 += ret;
+                       
+                       if ( overlap )
+                               MEM_freeN ( overlap );
                }
                rounds++;
+               
+               for(i = 0; i < numcollobj; i++)
+               {
+                       if ( collisions[i] ) MEM_freeN ( collisions[i] );
+               }
+                       
+               MEM_freeN(collisions);
+               MEM_freeN(collisions_index);
 
                ////////////////////////////////////////////////////////////
                // update positions
@@ -1492,8 +1558,8 @@ int cloth_bvh_objcollision ( ClothModifierData * clmd, float step, float dt )
                        VECADD ( verts[i].tx, verts[i].txold, verts[i].tv );
                }
                ////////////////////////////////////////////////////////////
-
-
+               
+               
                ////////////////////////////////////////////////////////////
                // Test on *simple* selfcollisions
                ////////////////////////////////////////////////////////////
@@ -1503,8 +1569,9 @@ int cloth_bvh_objcollision ( ClothModifierData * clmd, float step, float dt )
                        {
                                // TODO: add coll quality rounds again
                                BVHTreeOverlap *overlap = NULL;
+                               int result = 0;
        
-                               collisions = 1;
+                               // collisions = 1;
                                verts = cloth->verts; // needed for openMP
        
                                numfaces = clmd->clothObject->numfaces;
@@ -1532,7 +1599,7 @@ int cloth_bvh_objcollision ( ClothModifierData * clmd, float step, float dt )
                                                if ( clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL )
                                                {
                                                        if ( ( cloth->verts [i].flags & CLOTH_VERT_FLAG_PINNED )
-                                                               && ( cloth->verts [j].flags & CLOTH_VERT_FLAG_PINNED ) )
+                                                                               && ( cloth->verts [j].flags & CLOTH_VERT_FLAG_PINNED ) )
                                                        {
                                                                continue;
                                                        }
@@ -1604,6 +1671,9 @@ int cloth_bvh_objcollision ( ClothModifierData * clmd, float step, float dt )
                }
        }
        while ( ret2 && ( clmd->coll_parms->loop_count>rounds ) );
+       
+       if(collobjs)
+               MEM_freeN(collobjs);
 
        return MIN2 ( ret, 1 );
 }