[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [14838] branches/cloth/blender/source/ blender: Pre merge commit (includes commented moving stuff)
Daniel Genrich
daniel.genrich at gmx.net
Wed May 14 18:09:57 CEST 2008
Revision: 14838
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=14838
Author: genscher
Date: 2008-05-14 18:09:56 +0200 (Wed, 14 May 2008)
Log Message:
-----------
Pre merge commit (includes commented moving stuff)
Modified Paths:
--------------
branches/cloth/blender/source/blender/blenkernel/intern/cloth.c
branches/cloth/blender/source/blender/blenkernel/intern/collision.c
branches/cloth/blender/source/blender/blenkernel/intern/modifier.c
branches/cloth/blender/source/blender/blenlib/intern/BLI_kdopbvh.c
Modified: branches/cloth/blender/source/blender/blenkernel/intern/cloth.c
===================================================================
--- branches/cloth/blender/source/blender/blenkernel/intern/cloth.c 2008-05-14 14:53:12 UTC (rev 14837)
+++ branches/cloth/blender/source/blender/blenkernel/intern/cloth.c 2008-05-14 16:09:56 UTC (rev 14838)
@@ -47,6 +47,8 @@
#include "BLI_kdopbvh.h"
+#include <time.h>
+
#ifdef _WIN32
void tstart ( void )
{}
@@ -78,6 +80,40 @@
}
#endif
+
+/* Util macros */
+#define TO_STR(a) #a
+#define JOIN(a,b) a##b
+
+/* Benchmark macros */
+#if 1
+
+#define BENCH(a) \
+ do { \
+ clock_t _clock_init = clock(); \
+ (a); \
+ printf("%s: %fms\n", #a, (float)(clock()-_clock_init)*1000/CLOCKS_PER_SEC); \
+} while(0)
+
+#define BENCH_VAR(name) clock_t JOIN(_bench_step,name) = 0, JOIN(_bench_total,name) = 0
+#define BENCH_BEGIN(name) JOIN(_bench_step, name) = clock()
+#define BENCH_END(name) JOIN(_bench_total,name) += clock() - JOIN(_bench_step,name)
+#define BENCH_RESET(name) JOIN(_bench_total, name) = 0
+#define BENCH_REPORT(name) printf("%s: %fms\n", TO_STR(name), JOIN(_bench_total,name)*1000.0f/CLOCKS_PER_SEC)
+
+#else
+
+#define BENCH(a) (a)
+#define BENCH_VAR(name)
+#define BENCH_BEGIN(name)
+#define BENCH_END(name)
+#define BENCH_RESET(name)
+#define BENCH_REPORT(name)
+
+#endif
+
+
+
/* Our available solvers. */
// 255 is the magic reserved number, so NEVER try to put 255 solvers in here!
// 254 = MAX!
@@ -178,7 +214,7 @@
return NULL;
// create quadtree with k=26
- bvhtree = BLI_bvhtree_new(cloth->numfaces, epsilon, 4, 26);
+ bvhtree = BLI_bvhtree_new(cloth->numfaces, epsilon, 8, 6);
// fill tree
for(i = 0; i < cloth->numfaces; i++, mfaces++)
@@ -866,7 +902,7 @@
if(!first)
implicit_set_positions(clmd);
- clmd->clothObject->bvhtree = bvhtree_build_from_cloth ( clmd, clmd->coll_parms->epsilon );
+ BENCH(clmd->clothObject->bvhtree = bvhtree_build_from_cloth ( clmd, clmd->coll_parms->epsilon ));
return 1;
}
Modified: branches/cloth/blender/source/blender/blenkernel/intern/collision.c
===================================================================
--- branches/cloth/blender/source/blender/blenkernel/intern/collision.c 2008-05-14 14:53:12 UTC (rev 14837)
+++ branches/cloth/blender/source/blender/blenkernel/intern/collision.c 2008-05-14 16:09:56 UTC (rev 14838)
@@ -608,22 +608,9 @@
result = 1;
}
}
-
-
return result;
}
-int cloth_collision_response_moving_tris ( ClothModifierData *clmd, ClothModifierData *coll_clmd )
-{
- return 1;
-}
-
-
-int cloth_collision_response_moving_edges ( ClothModifierData *clmd, ClothModifierData *coll_clmd )
-{
- return 1;
-}
-
//Determines collisions on overlap, collisions are writen to collpair[i] and collision+number_collision_found is returned
CollPair* cloth_collision ( ModifierData *md1, ModifierData *md2, BVHTreeOverlap *overlap, CollPair *collpair )
{
@@ -743,25 +730,148 @@
cloth1 = clmd->clothObject;
verts1 = cloth1->verts;
- VECSUB ( temp, verts1[edgecollpair->p11].xold, verts2[edgecollpair->p21].co );
+ VECSUB ( temp, verts1[edgecollpair->p11].txold, verts2[edgecollpair->p21].co );
if ( ABS ( INPR ( temp, temp ) ) < ALMOST_ZERO )
return 1;
- VECSUB ( temp, verts1[edgecollpair->p11].xold, verts2[edgecollpair->p22].co );
+ VECSUB ( temp, verts1[edgecollpair->p11].txold, verts2[edgecollpair->p22].co );
if ( ABS ( INPR ( temp, temp ) ) < ALMOST_ZERO )
return 1;
- VECSUB ( temp, verts1[edgecollpair->p12].xold, verts2[edgecollpair->p21].co );
+ VECSUB ( temp, verts1[edgecollpair->p12].txold, verts2[edgecollpair->p21].co );
if ( ABS ( INPR ( temp, temp ) ) < ALMOST_ZERO )
return 1;
- VECSUB ( temp, verts1[edgecollpair->p12].xold, verts2[edgecollpair->p22].co );
+ VECSUB ( temp, verts1[edgecollpair->p12].txold, verts2[edgecollpair->p22].co );
if ( ABS ( INPR ( temp, temp ) ) < ALMOST_ZERO )
return 1;
+
+ VECSUB ( temp, verts1[edgecollpair->p11].txold, verts1[edgecollpair->p12].txold );
+ if ( ABS ( INPR ( temp, temp ) ) < ALMOST_ZERO )
+ return 1;
+
+ VECSUB ( temp, verts2[edgecollpair->p21].co, verts2[edgecollpair->p22].co );
+ if ( ABS ( INPR ( temp, temp ) ) < ALMOST_ZERO )
+ return 1;
+
return 0;
}
+int cloth_collision_response_moving( ClothModifierData *clmd, CollisionModifierData *collmd, CollPair *collpair, CollPair *collision_end )
+{
+ int result = 0;
+ Cloth *cloth1;
+ float w1, w2, w3, u1, u2, u3;
+ float v1[3], v2[3], relativeVelocity[3];
+ float magrelVel;
+ float epsilon2 = BLI_bvhtree_getepsilon ( collmd->bvhtree );
+
+ cloth1 = clmd->clothObject;
+
+ for ( ; collpair != collision_end; collpair++ )
+ {
+ // only handle static collisions here
+ if ( collpair->flag & COLLISION_IN_FUTURE )
+ continue;
+
+ // compute barycentric coordinates for both collision points
+ collision_compute_barycentric ( collpair->pa,
+ cloth1->verts[collpair->ap1].txold,
+ cloth1->verts[collpair->ap2].txold,
+ cloth1->verts[collpair->ap3].txold,
+ &w1, &w2, &w3 );
+
+ // was: txold
+ collision_compute_barycentric ( collpair->pb,
+ collmd->current_x[collpair->bp1].co,
+ collmd->current_x[collpair->bp2].co,
+ collmd->current_x[collpair->bp3].co,
+ &u1, &u2, &u3 );
+
+ // Calculate relative "velocity".
+ collision_interpolateOnTriangle ( v1, cloth1->verts[collpair->ap1].tv, cloth1->verts[collpair->ap2].tv, cloth1->verts[collpair->ap3].tv, w1, w2, w3 );
+
+ collision_interpolateOnTriangle ( v2, collmd->current_v[collpair->bp1].co, collmd->current_v[collpair->bp2].co, collmd->current_v[collpair->bp3].co, u1, u2, u3 );
+
+ VECSUB ( relativeVelocity, v2, v1 );
+
+ // Calculate the normal component of the relative velocity (actually only the magnitude - the direction is stored in 'normal').
+ magrelVel = INPR ( relativeVelocity, collpair->normal );
+
+ // printf("magrelVel: %f\n", magrelVel);
+
+ // Calculate masses of points.
+ // TODO
+
+ // If v_n_mag < 0 the edges are approaching each other.
+ if ( magrelVel > ALMOST_ZERO )
+ {
+ // Calculate Impulse magnitude to stop all motion in normal direction.
+ float magtangent = 0, repulse = 0, d = 0;
+ double impulse = 0.0;
+ float vrel_t_pre[3];
+ float temp[3];
+
+ // calculate tangential velocity
+ VECCOPY ( temp, collpair->normal );
+ VecMulf ( temp, magrelVel );
+ VECSUB ( vrel_t_pre, relativeVelocity, temp );
+
+ // Decrease in magnitude of relative tangential velocity due to coulomb friction
+ // in original formula "magrelVel" should be the "change of relative velocity in normal direction"
+ magtangent = MIN2 ( clmd->coll_parms->friction * 0.01 * magrelVel,sqrt ( INPR ( vrel_t_pre,vrel_t_pre ) ) );
+
+ // Apply friction impulse.
+ if ( magtangent > ALMOST_ZERO )
+ {
+ Normalize ( vrel_t_pre );
+
+ impulse = 2.0 * magtangent / ( 1.0 + w1*w1 + w2*w2 + w3*w3 );
+ 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 );
+ }
+
+ // Apply velocity stopping impulse
+ // I_c = m * v_N / 2.0
+ // no 2.0 * magrelVel normally, but looks nicer DG
+ impulse = magrelVel / ( 1.0 + w1*w1 + w2*w2 + w3*w3 );
+
+ VECADDMUL ( cloth1->verts[collpair->ap1].impulse, collpair->normal, w1 * impulse );
+ cloth1->verts[collpair->ap1].impulse_count++;
+
+ VECADDMUL ( cloth1->verts[collpair->ap2].impulse, collpair->normal, w2 * impulse );
+ cloth1->verts[collpair->ap2].impulse_count++;
+
+ VECADDMUL ( cloth1->verts[collpair->ap3].impulse, collpair->normal, w3 * impulse );
+ cloth1->verts[collpair->ap3].impulse_count++;
+
+ // Apply repulse impulse if distance too short
+ // I_r = -min(dt*kd, m(0,1d/dt - v_n))
+ /*
+ d = clmd->coll_parms->epsilon*8.0/9.0 + epsilon2*8.0/9.0 - collpair->distance;
+ if ( ( magrelVel < 0.1*d*clmd->sim_parms->stepsPerFrame ) && ( d > ALMOST_ZERO ) )
+ {
+ repulse = MIN2 ( d*1.0/clmd->sim_parms->stepsPerFrame, 0.1*d*clmd->sim_parms->stepsPerFrame - magrelVel );
+
+ // stay on the safe side and clamp repulse
+ if ( impulse > ALMOST_ZERO )
+ repulse = MIN2 ( repulse, 5.0*impulse );
+ repulse = MAX2 ( impulse, repulse );
+
+ impulse = repulse / ( 1.0 + w1*w1 + w2*w2 + w3*w3 ); // original 2.0 / 0.25
+ VECADDMUL ( cloth1->verts[collpair->ap1].impulse, collpair->normal, impulse );
+ VECADDMUL ( cloth1->verts[collpair->ap2].impulse, collpair->normal, impulse );
+ VECADDMUL ( cloth1->verts[collpair->ap3].impulse, collpair->normal, impulse );
+ }
+*/
+ result = 1;
+ }
+ }
+ return result;
+}
+
int cloth_collision_moving_edges ( ClothModifierData *clmd, CollisionModifierData *collmd, CollPair *collpair )
{
EdgeCollPair edgecollpair;
@@ -781,53 +891,6 @@
cloth1 = clmd->clothObject;
verts1 = cloth1->verts;
- /*
- double p[4][3] = {{0,0,0},{0,2,0},{1,1,-1},{1,1,1}};
- double v[4][3] = {{0,0,0},{1,0,0},{-2,0,0},{-2,0,0}};
-
- double pp[2][3] = {{-1,-1,-1}, {2,2,2}};
-
-
- VECSUB ( x1, p[1], p[0] );
- VECSUB ( v1, v[1], v[0] );
-
- VECSUB ( x2, p[2], p[0] );
- VECSUB ( v2, v[2], v[0] );
-
- VECSUB ( x3, p[3], p[0] );
- VECSUB ( v3, v[3], v[0] );
-
- printf("x1 x: %f, y: %f, z: %f\n", x1[0], x1[1], x1[2]);
- printf("x2 x: %f, y: %f, z: %f\n", x2[0], x2[1], x2[2]);
- printf("x3 x: %f, y: %f, z: %f\n", x3[0], x3[1], x3[2]);
-
- printf("v1 x: %f, y: %f, z: %f\n", v1[0], v1[1], v1[2]);
- printf("v2 x: %f, y: %f, z: %f\n", v2[0], v2[1], v2[2]);
- printf("v3 x: %f, y: %f, z: %f\n", v3[0], v3[1], v3[2]);
-
- numsolutions = cloth_get_collision_time ( x1, v1, x2, v2, x3, v3, solution );
-
- for ( k = 0; k < numsolutions; k++ )
- printf("mintime: %f\n", solution[k]);
-
- mintime = solution[0];
-
- // move triangles to collision point in time
- VECADDS(triA[0], pp[0], v[0], solution[0]);
- VECADDS(triA[1], p[0], v[0], solution[0]);
- VECADDS(triA[2], p[1], v[1], solution[0]);
-
- VECADDS(triB[0], pp[1], v[0], solution[0]);
- VECADDS(triB[1], p[2], v[2], solution[0]);
- VECADDS(triB[2], p[3], v[3], solution[0]);
-
- // check distance there
- distance = plNearestPoints (triA[0], triA[1], triA[2], triB[0], triB[1], triB[2], collpair->pa,collpair->pb,collpair->vector );
-
- printf("mintime: %f, dist: %f\n", mintime, distance);
-
- exit(0);
- */
for(i = 0; i < 9; i++)
{
// 9 edge - edge possibilities
@@ -916,19 +979,12 @@
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list