- Timestamp:
- Oct 20, 2008, 5:40:38 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/physics/src/bullet/BulletSoftBody/btSparseSDF.h
r1963 r1972 24 24 template <const int DWORDLEN> 25 25 unsigned int HsiehHash(const void* pdata) 26 {26 { 27 27 const unsigned short* data=(const unsigned short*)pdata; 28 28 unsigned hash=DWORDLEN<<2,tmp; 29 29 for(int i=0;i<DWORDLEN;++i) 30 {30 { 31 31 hash += data[0]; 32 32 tmp = (data[1]<<11)^hash; … … 34 34 data += 2; 35 35 hash += hash>>11; 36 }36 } 37 37 hash^=hash<<3;hash+=hash>>5; 38 38 hash^=hash<<4;hash+=hash>>17; 39 39 hash^=hash<<25;hash+=hash>>6; 40 40 return(hash); 41 }41 } 42 42 43 43 template <const int CELLSIZE> 44 44 struct btSparseSdf 45 {45 { 46 46 // 47 47 // Inner types 48 48 // 49 49 struct IntFrac 50 {50 { 51 51 int b; 52 52 int i; 53 53 btScalar f; 54 };54 }; 55 55 struct Cell 56 {56 { 57 57 btScalar d[CELLSIZE+1][CELLSIZE+1][CELLSIZE+1]; 58 58 int c[3]; … … 61 61 btCollisionShape* pclient; 62 62 Cell* next; 63 };63 }; 64 64 // 65 65 // Fields 66 66 // 67 67 68 68 btAlignedObjectArray<Cell*> cells; 69 69 btScalar voxelsz; … … 72 72 int nprobes; 73 73 int nqueries; 74 74 75 75 // 76 76 // Methods 77 77 // 78 78 79 79 // 80 80 void Initialize(int hashsize=2383) 81 {81 { 82 82 cells.resize(hashsize,0); 83 83 Reset(); 84 }84 } 85 85 // 86 86 void Reset() 87 {87 { 88 88 for(int i=0,ni=cells.size();i<ni;++i) 89 {89 { 90 90 Cell* pc=cells[i]; 91 91 cells[i]=0; 92 92 while(pc) 93 {93 { 94 94 Cell* pn=pc->next; 95 95 delete pc; 96 96 pc=pn; 97 }98 }97 } 98 } 99 99 voxelsz =0.25; 100 100 puid =0; … … 102 102 nprobes =1; 103 103 nqueries =1; 104 }104 } 105 105 // 106 106 void GarbageCollect(int lifetime=256) 107 {107 { 108 108 const int life=puid-lifetime; 109 109 for(int i=0;i<cells.size();++i) 110 {110 { 111 111 Cell*& root=cells[i]; 112 112 Cell* pp=0; 113 113 Cell* pc=root; 114 114 while(pc) 115 {115 { 116 116 Cell* pn=pc->next; 117 117 if(pc->puid<life) 118 {118 { 119 119 if(pp) pp->next=pn; else root=pn; 120 120 delete pc;pc=pp;--ncells; 121 } 122 pp=pc;pc=pn; 121 123 } 122 pp=pc;pc=pn; 123 } 124 } 124 } 125 125 //printf("GC[%d]: %d cells, PpQ: %f\r\n",puid,ncells,nprobes/(btScalar)nqueries); 126 126 nqueries=1; 127 127 nprobes=1; 128 128 ++puid; /* TODO: Reset puid's when int range limit is reached */ 129 /* else setup a priority list... */130 }129 /* else setup a priority list... */ 130 } 131 131 // 132 132 int RemoveReferences(btCollisionShape* pcs) 133 {133 { 134 134 int refcount=0; 135 135 for(int i=0;i<cells.size();++i) 136 {136 { 137 137 Cell*& root=cells[i]; 138 138 Cell* pp=0; 139 139 Cell* pc=root; 140 140 while(pc) 141 {141 { 142 142 Cell* pn=pc->next; 143 143 if(pc->pclient==pcs) 144 {144 { 145 145 if(pp) pp->next=pn; else root=pn; 146 146 delete pc;pc=pp;++refcount; 147 } 148 pp=pc;pc=pn; 147 149 } 148 pp=pc;pc=pn; 149 } 150 } 150 } 151 151 return(refcount); 152 }152 } 153 153 // 154 154 btScalar Evaluate( const btVector3& x, 155 btCollisionShape* shape,156 btVector3& normal,157 btScalar margin)158 {155 btCollisionShape* shape, 156 btVector3& normal, 157 btScalar margin) 158 { 159 159 /* Lookup cell */ 160 160 const btVector3 scx=x/voxelsz; … … 167 167 ++nqueries; 168 168 while(c) 169 {169 { 170 170 ++nprobes; 171 171 if( (c->hash==h) && … … 174 174 (c->c[2]==iz.b) && 175 175 (c->pclient==shape)) 176 { break; }177 else178 { c=c->next; }179 }176 { break; } 177 else 178 { c=c->next; } 179 } 180 180 if(!c) 181 {181 { 182 182 ++nprobes; 183 183 ++ncells; … … 188 188 c->c[0]=ix.b;c->c[1]=iy.b;c->c[2]=iz.b; 189 189 BuildCell(*c); 190 }190 } 191 191 c->puid=puid; 192 192 /* Extract infos */ 193 193 const int o[]={ ix.i,iy.i,iz.i}; 194 194 const btScalar d[]={ c->d[o[0]+0][o[1]+0][o[2]+0], 195 c->d[o[0]+1][o[1]+0][o[2]+0],196 c->d[o[0]+1][o[1]+1][o[2]+0],197 c->d[o[0]+0][o[1]+1][o[2]+0],198 c->d[o[0]+0][o[1]+0][o[2]+1],199 c->d[o[0]+1][o[1]+0][o[2]+1],200 c->d[o[0]+1][o[1]+1][o[2]+1],201 c->d[o[0]+0][o[1]+1][o[2]+1]};195 c->d[o[0]+1][o[1]+0][o[2]+0], 196 c->d[o[0]+1][o[1]+1][o[2]+0], 197 c->d[o[0]+0][o[1]+1][o[2]+0], 198 c->d[o[0]+0][o[1]+0][o[2]+1], 199 c->d[o[0]+1][o[1]+0][o[2]+1], 200 c->d[o[0]+1][o[1]+1][o[2]+1], 201 c->d[o[0]+0][o[1]+1][o[2]+1]}; 202 202 /* Normal */ 203 #if 1203 #if 1 204 204 const btScalar gx[]={ d[1]-d[0],d[2]-d[3], 205 d[5]-d[4],d[6]-d[7]};205 d[5]-d[4],d[6]-d[7]}; 206 206 const btScalar gy[]={ d[3]-d[0],d[2]-d[1], 207 d[7]-d[4],d[6]-d[5]};207 d[7]-d[4],d[6]-d[5]}; 208 208 const btScalar gz[]={ d[4]-d[0],d[5]-d[1], 209 d[7]-d[3],d[6]-d[2]};209 d[7]-d[3],d[6]-d[2]}; 210 210 normal.setX(Lerp( Lerp(gx[0],gx[1],iy.f), 211 Lerp(gx[2],gx[3],iy.f),iz.f));211 Lerp(gx[2],gx[3],iy.f),iz.f)); 212 212 normal.setY(Lerp( Lerp(gy[0],gy[1],ix.f), 213 Lerp(gy[2],gy[3],ix.f),iz.f));213 Lerp(gy[2],gy[3],ix.f),iz.f)); 214 214 normal.setZ(Lerp( Lerp(gz[0],gz[1],ix.f), 215 Lerp(gz[2],gz[3],ix.f),iy.f));215 Lerp(gz[2],gz[3],ix.f),iy.f)); 216 216 normal = normal.normalized(); 217 #else217 #else 218 218 normal = btVector3(d[1]-d[0],d[3]-d[0],d[4]-d[0]).normalized(); 219 #endif219 #endif 220 220 /* Distance */ 221 221 const btScalar d0=Lerp(Lerp(d[0],d[1],ix.f), 222 Lerp(d[3],d[2],ix.f),iy.f);222 Lerp(d[3],d[2],ix.f),iy.f); 223 223 const btScalar d1=Lerp(Lerp(d[4],d[5],ix.f), 224 Lerp(d[7],d[6],ix.f),iy.f);224 Lerp(d[7],d[6],ix.f),iy.f); 225 225 return(Lerp(d0,d1,iz.f)-margin); 226 }226 } 227 227 // 228 228 void BuildCell(Cell& c) 229 {229 { 230 230 const btVector3 org=btVector3( (btScalar)c.c[0], 231 (btScalar)c.c[1],232 (btScalar)c.c[2]) *233 CELLSIZE*voxelsz;231 (btScalar)c.c[1], 232 (btScalar)c.c[2]) * 233 CELLSIZE*voxelsz; 234 234 for(int k=0;k<=CELLSIZE;++k) 235 {235 { 236 236 const btScalar z=voxelsz*k+org.z(); 237 237 for(int j=0;j<=CELLSIZE;++j) 238 {238 { 239 239 const btScalar y=voxelsz*j+org.y(); 240 240 for(int i=0;i<=CELLSIZE;++i) 241 {241 { 242 242 const btScalar x=voxelsz*i+org.x(); 243 243 c.d[i][j][k]=DistanceToShape( btVector3(x,y,z), 244 c.pclient); 244 c.pclient); 245 } 245 246 } 246 247 } 247 248 } 248 }249 249 // 250 250 static inline btScalar DistanceToShape(const btVector3& x, 251 btCollisionShape* shape)252 {251 btCollisionShape* shape) 252 { 253 253 btTransform unit; 254 254 unit.setIdentity(); 255 255 if(shape->isConvex()) 256 {256 { 257 257 btGjkEpaSolver2::sResults res; 258 258 btConvexShape* csh=static_cast<btConvexShape*>(shape); 259 259 return(btGjkEpaSolver2::SignedDistance(x,0,csh,unit,res)); 260 }260 } 261 261 return(0); 262 }262 } 263 263 // 264 264 static inline IntFrac Decompose(btScalar x) 265 {265 { 266 266 /* That one need a lot of improvements... */ 267 267 /* Remove test, faster floor... */ … … 273 273 r.i=(int)k;r.f=k-r.i;r.b-=o; 274 274 return(r); 275 }275 } 276 276 // 277 277 static inline btScalar Lerp(btScalar a,btScalar b,btScalar t) 278 {278 { 279 279 return(a+(b-a)*t); 280 }281 282 280 } 281 282 283 283 284 284 // 285 285 static inline unsigned int Hash(int x,int y,int z,btCollisionShape* shape) 286 {286 { 287 287 struct btS 288 288 { … … 292 292 293 293 btS myset; 294 294 295 295 myset.x=x;myset.y=y;myset.z=z;myset.p=shape; 296 296 const void* ptr = &myset; 297 297 298 298 unsigned int result = HsiehHash<sizeof(btS)/4> (ptr); 299 299 300 300 301 301 return result; 302 }302 } 303 303 }; 304 304 305 305 306 306 #endif
Note: See TracChangeset
for help on using the changeset viewer.