17#ifndef BT_SPARSE_SDF_H
18#define BT_SPARSE_SDF_H
25#if !defined(get16bits)
26#define get16bits(d) ((((unsigned int)(((const unsigned char*)(d))[1])) << 8) + (unsigned int)(((const unsigned char*)(d))[0]))
31inline unsigned int HsiehHash(
const char* data,
int len)
33 unsigned int hash = len, tmp;
37 for (; len > 0; len--)
41 hash = (hash << 16) ^ tmp;
42 data += 2 *
sizeof(
unsigned short);
57template <const
int CELLSIZE>
71 btScalar d[CELLSIZE + 1][CELLSIZE + 1][CELLSIZE + 1];
100 void Initialize(
int hashsize = 2383,
int clampCells = 256 * 1024)
105 cells.resize(hashsize, 0);
118 for (
int i = 0, ni =
cells.size(); i < ni; ++i)
138 const int life =
puid - lifetime;
139 for (
int i = 0; i <
cells.size(); ++i)
171 for (
int i = 0; i <
cells.size(); ++i)
206 const unsigned h =
Hash(ix.
b, iy.
b, iz.
b, shape);
213 if ((c->hash == h) &&
217 (c->pclient == shape))
236 static int numResets = 0;
254 const int o[] = {ix.
i, iy.
i, iz.
i};
255 const btScalar d[] = {c->d[o[0] + 0][o[1] + 0][o[2] + 0],
256 c->d[o[0] + 1][o[1] + 0][o[2] + 0],
257 c->d[o[0] + 1][o[1] + 1][o[2] + 0],
258 c->d[o[0] + 0][o[1] + 1][o[2] + 0],
259 c->d[o[0] + 0][o[1] + 0][o[2] + 1],
260 c->d[o[0] + 1][o[1] + 0][o[2] + 1],
261 c->d[o[0] + 1][o[1] + 1][o[2] + 1],
262 c->d[o[0] + 0][o[1] + 1][o[2] + 1]};
265 const btScalar gx[] = {d[1] - d[0], d[2] - d[3],
266 d[5] - d[4], d[6] - d[7]};
267 const btScalar gy[] = {d[3] - d[0], d[2] - d[1],
268 d[7] - d[4], d[6] - d[5]};
269 const btScalar gz[] = {d[4] - d[0], d[5] - d[1],
270 d[7] - d[3], d[6] - d[2]};
272 Lerp(gx[2], gx[3], iy.
f), iz.
f));
274 Lerp(gy[2], gy[3], ix.
f), iz.
f));
276 Lerp(gz[2], gz[3], ix.
f), iy.
f));
283 Lerp(d[3], d[2], ix.
f), iy.
f);
285 Lerp(d[7], d[6], ix.
f), iy.
f);
286 return (
Lerp(d0, d1, iz.
f) - margin);
295 for (
int k = 0; k <= CELLSIZE; ++k)
298 for (
int j = 0; j <= CELLSIZE; ++j)
301 for (
int i = 0; i <= CELLSIZE; ++i)
331 const int o = x < 0 ? (int)(-x + 1) : 0;
343 return (a + (b - a) * t);
363 myset.p = (
void*)shape;
364 const char* ptr = (
const char*)&myset;
366 unsigned int result =
HsiehHash(ptr,
sizeof(btS));
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
unsigned int HsiehHash(const char *data, int len)
#define get16bits(d)
btSparseSdf implementation by Nathanael Presson
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
The btCollisionShape class provides an interface for collision shapes that can be shared among btColl...
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
btVector3 can be used to represent 3D points and vectors.
void setZ(btScalar _z)
Set the z value.
const btScalar & z() const
Return the z value.
btVector3 & safeNormalize()
btVector3 normalized() const
Return a normalized version of this vector.
void setY(btScalar _y)
Set the y value.
void setX(btScalar _x)
Set the x value.
const btScalar & x() const
Return the x value.
const btScalar & y() const
Return the y value.
static btScalar SignedDistance(const btVector3 &position, btScalar margin, const btConvexShape *shape, const btTransform &wtrs, sResults &results)
const btCollisionShape * pclient
btScalar d[CELLSIZE+1][CELLSIZE+1][CELLSIZE+1]
void GarbageCollect(int lifetime=256)
static IntFrac Decompose(btScalar x)
static btScalar DistanceToShape(const btVector3 &x, const btCollisionShape *shape)
int RemoveReferences(btCollisionShape *pcs)
btScalar m_defaultVoxelsz
btAlignedObjectArray< Cell * > cells
btScalar Evaluate(const btVector3 &x, const btCollisionShape *shape, btVector3 &normal, btScalar margin)
static btScalar Lerp(btScalar a, btScalar b, btScalar t)
static unsigned int Hash(int x, int y, int z, const btCollisionShape *shape)
void setDefaultVoxelsz(btScalar sz)
void Initialize(int hashsize=2383, int clampCells=256 *1024)