use static functions where possible for some local functions.
[blender.git] / source / blender / blenkernel / intern / collision.c
index 5b3cec2577258b6282bc514c3fe893be87f47a34..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;
@@ -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,120 +1293,10 @@ int cloth_collision_moving ( ClothModifierData *clmd, CollisionModifierData *col
        return 1;
 }
 
-int cloth_do_selfcollisions(ClothModifierData * clmd)
-{      
-       int ret2 = 0, l;
-       Cloth *cloth = clmd->clothObject;
-       
-       if ( clmd->clothObject->bvhselftree )
-       {
-               for(l = 0; l < clmd->coll_parms->self_loop_count; l++)
-               {
-                       BVHTreeOverlap *overlap = NULL;
-                       ClothVertex *verts = clmd->clothObject->verts; // needed for openMP
-                       int k;
-                       int ret = 0, result = 0;
-                       
-                       // search for overlapping collision pairs 
-                       overlap = BLI_bvhtree_overlap ( cloth->bvhselftree, cloth->bvhselftree, &result );
-       
-// #pragma omp parallel for private(k, i, j) schedule(static)
-                       for ( k = 0; k < result; k++ )
-                       {
-                               float temp[3];
-                               float length = 0;
-                               float mindistance;
-                               int i, j;
-       
-                               i = overlap[k].indexA;
-                               j = overlap[k].indexB;
-       
-                               mindistance = clmd->coll_parms->selfepsilon* ( cloth->verts[i].avg_spring_len + cloth->verts[j].avg_spring_len );
-       
-                               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 ) )
-                                       {
-                                               continue;
-                                       }
-                               }
-       
-                               VECSUB ( temp, verts[i].tx, verts[j].tx );
-       
-                               if ( ( ABS ( temp[0] ) > mindistance ) || ( ABS ( temp[1] ) > mindistance ) || ( ABS ( temp[2] ) > mindistance ) ) continue;
-       
-                               // check for adjacent points (i must be smaller j)
-                               if ( BLI_edgehash_haskey ( cloth->edgehash, MIN2(i, j), MAX2(i, j) ) )
-                               {
-                                       continue;
-                               }
-       
-                               length = Normalize ( temp );
-       
-                               if ( length < mindistance )
-                               {
-                                       float correction = mindistance - length;
-       
-                                       if ( cloth->verts [i].flags & CLOTH_VERT_FLAG_PINNED )
-                                       {
-                                               VecMulf ( temp, -correction );
-                                               VECADD ( verts[j].tx, verts[j].tx, temp );
-                                       }
-                                       else if ( cloth->verts [j].flags & CLOTH_VERT_FLAG_PINNED )
-                                       {
-                                               VecMulf ( temp, correction );
-                                               VECADD ( verts[i].tx, verts[i].tx, temp );
-                                       }
-                                       else
-                                       {
-                                               VecMulf ( temp, -correction*0.5 );
-                                               VECADD ( verts[j].tx, verts[j].tx, temp );
-       
-                                               VECSUB ( verts[i].tx, verts[i].tx, temp );
-                                       }
-                                       ret = 1;
-                                       ret2 += ret;
-                               }
-                               else
-                               {
-                                       // check for approximated time collisions
-                               }
-                       }
-       
-                       if ( overlap )
-                               MEM_freeN ( overlap );
-               
-                       if(!ret)
-                               break;
-                       
-               }
-               ////////////////////////////////////////////////////////////
-       
-               ////////////////////////////////////////////////////////////
-               // SELFCOLLISIONS: update velocities
-               ////////////////////////////////////////////////////////////
-               if ( ret2 )
-               {
-                       int i; 
-                       ClothVertex *verts = clmd->clothObject->verts; // needed for openMP
-                       
-                       for ( i = 0; i < cloth->numverts; i++ )
-                       {
-                               if ( ! ( verts [i].flags & CLOTH_VERT_FLAG_PINNED ) )
-                               {
-                                       VECSUB ( verts[i].tv, verts[i].tx, verts[i].txold );
-                               }
-                       }
-               }
-               ////////////////////////////////////////////////////////////
-       }
-       return ret2;
-}
 
 // return all collision objects in scene
 // collision object will exclude self 
-CollisionModifierData **get_collisionobjects(Object *self, int *numcollobj)
+CollisionModifierData **get_collisionobjects(Scene *scene, Object *self, int *numcollobj)
 {
        Base *base=NULL;
        CollisionModifierData **objs = NULL;
@@ -1414,12 +1306,43 @@ CollisionModifierData **get_collisionobjects(Object *self, int *numcollobj)
        
        objs = MEM_callocN(sizeof(CollisionModifierData *)*maxobj, "CollisionObjectsArray");
        // check all collision objects
-       for ( base = G.scene->base.first; base; base = base->next )
+       for ( base = scene->base.first; base; base = base->next )
        {
+               /*Only proceed for mesh object in same layer */
+               if(!(base->object->type==OB_MESH && (base->lay & self->lay))) 
+                       continue;
+               
                coll_ob = base->object;
-               collmd = ( CollisionModifierData * ) modifiers_findByType ( coll_ob, eModifierType_Collision );
-       
-               if ( !collmd )
+               
+               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 )
                        {
@@ -1429,13 +1352,19 @@ CollisionModifierData **get_collisionobjects(Object *self, int *numcollobj)
                                for ( go= group->gobject.first; go; go= go->next )
                                {
                                        coll_ob = go->ob;
-
-                                       collmd = ( CollisionModifierData * ) modifiers_findByType ( coll_ob, eModifierType_Collision );
-
-                                       if ( !collmd )
+                                       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;
 
-                                       if(coll_ob == self)
+                                       if ( !collmd )
                                                continue;
                                        
                                        if( !collmd->bvhtree)
@@ -1457,34 +1386,13 @@ CollisionModifierData **get_collisionobjects(Object *self, int *numcollobj)
                                        numobj++;
                                }
                        }
-               }
-               else
-               {
-                       if(coll_ob == self)
-                               continue;
-                       
-                       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;
 }
 
-void cloth_bvh_objcollisions_nearcheck ( ClothModifierData * clmd, CollisionModifierData *collmd, CollPair **collisions, CollPair **collisions_index, int numresult, BVHTreeOverlap *overlap)
+static void cloth_bvh_objcollisions_nearcheck ( ClothModifierData * clmd, CollisionModifierData *collmd, CollPair **collisions, CollPair **collisions_index, int numresult, BVHTreeOverlap *overlap)
 {
        int i;
        
@@ -1497,7 +1405,7 @@ void cloth_bvh_objcollisions_nearcheck ( ClothModifierData * clmd, CollisionModi
        }
 }
 
-int cloth_bvh_objcollisions_resolve ( ClothModifierData * clmd, CollisionModifierData *collmd, CollPair *collisions, CollPair *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;
@@ -1543,11 +1451,11 @@ int cloth_bvh_objcollisions_resolve ( ClothModifierData * clmd, CollisionModifie
 }
 
 // cloth - object collisions
-int cloth_bvh_objcollision ( Object *ob, ClothModifierData * clmd, float step, float dt )
+int cloth_bvh_objcollision (Object *ob, ClothModifierData * clmd, float step, float dt )
 {
        Cloth *cloth=NULL;
        BVHTree *cloth_bvh=NULL;
-       int i=0, numfaces = 0, numverts = 0;
+       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;
@@ -1573,7 +1481,7 @@ int cloth_bvh_objcollision ( Object *ob, ClothModifierData * clmd, float step, f
        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(ob, &numcollobj);
+       collobjs = get_collisionobjects(clmd->scene, ob, &numcollobj);
        
        if(!collobjs)
                return 0;
@@ -1594,6 +1502,9 @@ int cloth_bvh_objcollision ( Object *ob, ClothModifierData * clmd, float step, f
                        BVHTreeOverlap *overlap = NULL;
                        int result = 0;
                        
+                       if(!collmd->bvhtree)
+                               continue;
+                       
                        /* move object to position (step) in time */
                        collision_move_object ( collmd, step + dt, step );
                        
@@ -1647,21 +1558,122 @@ int cloth_bvh_objcollision ( Object *ob, ClothModifierData * clmd, float step, f
                        VECADD ( verts[i].tx, verts[i].txold, verts[i].tv );
                }
                ////////////////////////////////////////////////////////////
-
+               
                
                ////////////////////////////////////////////////////////////
                // Test on *simple* selfcollisions
                ////////////////////////////////////////////////////////////
                if ( clmd->coll_parms->flags & CLOTH_COLLSETTINGS_FLAG_SELF )
                {
-                       ret2 += cloth_do_selfcollisions(clmd);
+                       for(l = 0; l < clmd->coll_parms->self_loop_count; l++)
+                       {
+                               // TODO: add coll quality rounds again
+                               BVHTreeOverlap *overlap = NULL;
+                               int result = 0;
+       
+                               // collisions = 1;
+                               verts = cloth->verts; // needed for openMP
+       
+                               numfaces = clmd->clothObject->numfaces;
+                               numverts = clmd->clothObject->numverts;
+       
+                               verts = cloth->verts;
+       
+                               if ( cloth->bvhselftree )
+                               {
+                                       // search for overlapping collision pairs 
+                                       overlap = BLI_bvhtree_overlap ( cloth->bvhselftree, cloth->bvhselftree, &result );
+       
+       // #pragma omp parallel for private(k, i, j) schedule(static)
+                                       for ( k = 0; k < result; k++ )
+                                       {
+                                               float temp[3];
+                                               float length = 0;
+                                               float mindistance;
+       
+                                               i = overlap[k].indexA;
+                                               j = overlap[k].indexB;
+       
+                                               mindistance = clmd->coll_parms->selfepsilon* ( cloth->verts[i].avg_spring_len + cloth->verts[j].avg_spring_len );
+       
+                                               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 ) )
+                                                       {
+                                                               continue;
+                                                       }
+                                               }
+       
+                                               VECSUB ( temp, verts[i].tx, verts[j].tx );
+       
+                                               if ( ( ABS ( temp[0] ) > mindistance ) || ( ABS ( temp[1] ) > mindistance ) || ( ABS ( temp[2] ) > mindistance ) ) continue;
+       
+                                               // check for adjacent points (i must be smaller j)
+                                               if ( BLI_edgehash_haskey ( cloth->edgehash, MIN2(i, j), MAX2(i, j) ) )
+                                               {
+                                                       continue;
+                                               }
+       
+                                               length = Normalize ( temp );
+       
+                                               if ( length < mindistance )
+                                               {
+                                                       float correction = mindistance - length;
+       
+                                                       if ( cloth->verts [i].flags & CLOTH_VERT_FLAG_PINNED )
+                                                       {
+                                                               VecMulf ( temp, -correction );
+                                                               VECADD ( verts[j].tx, verts[j].tx, temp );
+                                                       }
+                                                       else if ( cloth->verts [j].flags & CLOTH_VERT_FLAG_PINNED )
+                                                       {
+                                                               VecMulf ( temp, correction );
+                                                               VECADD ( verts[i].tx, verts[i].tx, temp );
+                                                       }
+                                                       else
+                                                       {
+                                                               VecMulf ( temp, -correction*0.5 );
+                                                               VECADD ( verts[j].tx, verts[j].tx, temp );
+       
+                                                               VECSUB ( verts[i].tx, verts[i].tx, temp );
+                                                       }
+                                                       ret = 1;
+                                                       ret2 += ret;
+                                               }
+                                               else
+                                               {
+                                                       // check for approximated time collisions
+                                               }
+                                       }
+       
+                                       if ( overlap )
+                                               MEM_freeN ( overlap );
+       
+                               }
+                       }
+                       ////////////////////////////////////////////////////////////
+
+                       ////////////////////////////////////////////////////////////
+                       // SELFCOLLISIONS: update velocities
+                       ////////////////////////////////////////////////////////////
+                       if ( ret2 )
+                       {
+                               for ( i = 0; i < cloth->numverts; i++ )
+                               {
+                                       if ( ! ( verts [i].flags & CLOTH_VERT_FLAG_PINNED ) )
+                                       {
+                                               VECSUB ( verts[i].tv, verts[i].tx, verts[i].txold );
+                                       }
+                               }
+                       }
+                       ////////////////////////////////////////////////////////////
                }
-               ////////////////////////////////////////////////////////////
        }
        while ( ret2 && ( clmd->coll_parms->loop_count>rounds ) );
        
        if(collobjs)
-               +               MEM_freeN(collobjs);
+               MEM_freeN(collobjs);
 
        return MIN2 ( ret, 1 );
 }