17 #ifndef BT_SPARSE_SDF_H
18 #define BT_SPARSE_SDF_H
24 template <const
int DWORDLEN>
27 const unsigned short* data=(
const unsigned short*)pdata;
28 unsigned hash=DWORDLEN<<2,tmp;
29 for(
int i=0;i<DWORDLEN;++i)
32 tmp = (data[1]<<11)^hash;
33 hash = (hash<<16)^tmp;
37 hash^=hash<<3;hash+=hash>>5;
38 hash^=hash<<4;hash+=hash>>17;
39 hash^=hash<<25;hash+=hash>>6;
43 template <const
int CELLSIZE>
81 void Initialize(
int hashsize=2383,
int clampCells = 256*1024)
86 cells.resize(hashsize,0);
92 for(
int i=0,ni=
cells.size();i<ni;++i)
112 const int life=
puid-lifetime;
113 for(
int i=0;i<
cells.size();++i)
115 Cell*& root=
cells[i];
123 if(pp) pp->next=pn;
else root=pn;
139 for(
int i=0;i<
cells.size();++i)
141 Cell*& root=
cells[i];
149 if(pp) pp->next=pn;
else root=pn;
150 delete pc;pc=pp;++refcount;
168 const unsigned h=
Hash(ix.b,iy.b,iz.b,shape);
169 Cell*& root=
cells[
static_cast<int>(h%
cells.size())];
188 int sz =
sizeof(Cell);
191 static int numResets=0;
201 c->c[0]=ix.b;c->c[1]=iy.b;c->c[2]=iz.b;
206 const int o[]={ ix.i,iy.i,iz.i};
207 const btScalar d[]={ c->d[o[0]+0][o[1]+0][o[2]+0],
208 c->d[o[0]+1][o[1]+0][o[2]+0],
209 c->d[o[0]+1][o[1]+1][o[2]+0],
210 c->d[o[0]+0][o[1]+1][o[2]+0],
211 c->d[o[0]+0][o[1]+0][o[2]+1],
212 c->d[o[0]+1][o[1]+0][o[2]+1],
213 c->d[o[0]+1][o[1]+1][o[2]+1],
214 c->d[o[0]+0][o[1]+1][o[2]+1]};
217 const btScalar gx[]={ d[1]-d[0],d[2]-d[3],
218 d[5]-d[4],d[6]-d[7]};
219 const btScalar gy[]={ d[3]-d[0],d[2]-d[1],
220 d[7]-d[4],d[6]-d[5]};
221 const btScalar gz[]={ d[4]-d[0],d[5]-d[1],
222 d[7]-d[3],d[6]-d[2]};
224 Lerp(gx[2],gx[3],iy.f),iz.f));
226 Lerp(gy[2],gy[3],ix.f),iz.f));
228 Lerp(gz[2],gz[3],ix.f),iy.f));
235 Lerp(d[3],d[2],ix.f),iy.f);
237 Lerp(d[7],d[6],ix.f),iy.f);
238 return(
Lerp(d0,d1,iz.f)-margin);
247 for(
int k=0;k<=CELLSIZE;++k)
250 for(
int j=0;j<=CELLSIZE;++j)
253 for(
int i=0;i<=CELLSIZE;++i)
283 const int o=x<0?(int)(-x+1):0;
286 r.i=(int)k;r.f=k-r.i;r.b-=o;
308 myset.x=x;myset.y=y;myset.z=z;myset.p=(
void*)shape;
309 const void* ptr = &myset;
311 unsigned int result =
HsiehHash<
sizeof(btS)/4> (ptr);
319 #endif //BT_SPARSE_SDF_H
void GarbageCollect(int lifetime=256)
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
void setZ(btScalar _z)
Set the z value.
unsigned int HsiehHash(const void *pdata)
btSparseSdf implementation by Nathanael Presson
The btCollisionShape class provides an interface for collision shapes that can be shared among btColl...
btAlignedObjectArray< Cell * > cells
const btScalar & x() const
Return the x value.
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
void setX(btScalar _x)
Set the x value.
int RemoveReferences(btCollisionShape *pcs)
static btScalar Lerp(btScalar a, btScalar b, btScalar t)
void setY(btScalar _y)
Set the y value.
const btScalar & y() const
Return the y value.
btVector3 can be used to represent 3D points and vectors.
btScalar d[CELLSIZE+1][CELLSIZE+1][CELLSIZE+1]
btVector3 normalized() const
Return a normalized version of this vector.
btScalar Evaluate(const btVector3 &x, const btCollisionShape *shape, btVector3 &normal, btScalar margin)
static unsigned int Hash(int x, int y, int z, const btCollisionShape *shape)
static btScalar SignedDistance(const btVector3 &position, btScalar margin, const btConvexShape *shape, const btTransform &wtrs, sResults &results)
const btCollisionShape * pclient
static IntFrac Decompose(btScalar x)
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
static btScalar DistanceToShape(const btVector3 &x, const btCollisionShape *shape)
void Initialize(int hashsize=2383, int clampCells=256 *1024)
const btScalar & z() const
Return the z value.