2017-08-01 12:30:58 +00:00
|
|
|
/*
|
|
|
|
Bullet Continuous Collision Detection and Physics Library
|
|
|
|
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied warranty.
|
|
|
|
In no event will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it freely,
|
|
|
|
subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
///btSparseSdf implementation by Nathanael Presson
|
|
|
|
|
|
|
|
#ifndef BT_SPARSE_SDF_H
|
|
|
|
#define BT_SPARSE_SDF_H
|
|
|
|
|
|
|
|
#include "BulletCollision/CollisionDispatch/btCollisionObject.h"
|
|
|
|
#include "BulletCollision/NarrowPhaseCollision/btGjkEpa2.h"
|
|
|
|
|
2020-01-08 17:05:43 +00:00
|
|
|
// Fast Hash
|
|
|
|
|
|
|
|
#if !defined (get16bits)
|
|
|
|
#define get16bits(d) ((((unsigned int)(((const unsigned char *)(d))[1])) << 8)\
|
|
|
|
+(unsigned int)(((const unsigned char *)(d))[0]) )
|
|
|
|
#endif
|
|
|
|
//
|
|
|
|
// super hash function by Paul Hsieh
|
|
|
|
//
|
|
|
|
inline unsigned int HsiehHash (const char * data, int len) {
|
|
|
|
unsigned int hash = len, tmp;
|
|
|
|
len>>=2;
|
|
|
|
|
|
|
|
/* Main loop */
|
|
|
|
for (;len > 0; len--) {
|
|
|
|
hash += get16bits (data);
|
|
|
|
tmp = (get16bits (data+2) << 11) ^ hash;
|
|
|
|
hash = (hash << 16) ^ tmp;
|
|
|
|
data += 2*sizeof (unsigned short);
|
|
|
|
hash += hash >> 11;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Force "avalanching" of final 127 bits */
|
|
|
|
hash ^= hash << 3;
|
|
|
|
hash += hash >> 5;
|
|
|
|
hash ^= hash << 4;
|
|
|
|
hash += hash >> 17;
|
|
|
|
hash ^= hash << 25;
|
|
|
|
hash += hash >> 6;
|
|
|
|
|
|
|
|
return hash;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <const int CELLSIZE>
|
2019-01-03 13:26:51 +00:00
|
|
|
struct btSparseSdf
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// Inner types
|
|
|
|
//
|
|
|
|
struct IntFrac
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
int b;
|
|
|
|
int i;
|
|
|
|
btScalar f;
|
2017-08-01 12:30:58 +00:00
|
|
|
};
|
2019-01-03 13:26:51 +00:00
|
|
|
struct Cell
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar d[CELLSIZE + 1][CELLSIZE + 1][CELLSIZE + 1];
|
|
|
|
int c[3];
|
|
|
|
int puid;
|
|
|
|
unsigned hash;
|
|
|
|
const btCollisionShape* pclient;
|
|
|
|
Cell* next;
|
2017-08-01 12:30:58 +00:00
|
|
|
};
|
|
|
|
//
|
|
|
|
// Fields
|
|
|
|
//
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btAlignedObjectArray<Cell*> cells;
|
|
|
|
btScalar voxelsz;
|
2020-01-08 17:05:43 +00:00
|
|
|
btScalar m_defaultVoxelsz;
|
2019-01-03 13:26:51 +00:00
|
|
|
int puid;
|
|
|
|
int ncells;
|
|
|
|
int m_clampCells;
|
|
|
|
int nprobes;
|
|
|
|
int nqueries;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2020-01-08 17:05:43 +00:00
|
|
|
~btSparseSdf()
|
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
//
|
|
|
|
// Methods
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
2019-01-03 13:26:51 +00:00
|
|
|
void Initialize(int hashsize = 2383, int clampCells = 256 * 1024)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//avoid a crash due to running out of memory, so clamp the maximum number of cells allocated
|
|
|
|
//if this limit is reached, the SDF is reset (at the cost of some performance during the reset)
|
|
|
|
m_clampCells = clampCells;
|
2019-01-03 13:26:51 +00:00
|
|
|
cells.resize(hashsize, 0);
|
2020-01-08 17:05:43 +00:00
|
|
|
m_defaultVoxelsz = 0.25;
|
2017-08-01 12:30:58 +00:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
//
|
2020-01-08 17:05:43 +00:00
|
|
|
|
|
|
|
void setDefaultVoxelsz(btScalar sz)
|
|
|
|
{
|
|
|
|
m_defaultVoxelsz = sz;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void Reset()
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0, ni = cells.size(); i < ni; ++i)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
Cell* pc = cells[i];
|
|
|
|
cells[i] = 0;
|
|
|
|
while (pc)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
Cell* pn = pc->next;
|
2017-08-01 12:30:58 +00:00
|
|
|
delete pc;
|
2019-01-03 13:26:51 +00:00
|
|
|
pc = pn;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-08 17:05:43 +00:00
|
|
|
voxelsz = m_defaultVoxelsz;
|
2019-01-03 13:26:51 +00:00
|
|
|
puid = 0;
|
|
|
|
ncells = 0;
|
|
|
|
nprobes = 1;
|
|
|
|
nqueries = 1;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
//
|
2019-01-03 13:26:51 +00:00
|
|
|
void GarbageCollect(int lifetime = 256)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
const int life = puid - lifetime;
|
|
|
|
for (int i = 0; i < cells.size(); ++i)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
Cell*& root = cells[i];
|
|
|
|
Cell* pp = 0;
|
|
|
|
Cell* pc = root;
|
|
|
|
while (pc)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
Cell* pn = pc->next;
|
|
|
|
if (pc->puid < life)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (pp)
|
|
|
|
pp->next = pn;
|
|
|
|
else
|
|
|
|
root = pn;
|
|
|
|
delete pc;
|
|
|
|
pc = pp;
|
|
|
|
--ncells;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
pp = pc;
|
|
|
|
pc = pn;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//printf("GC[%d]: %d cells, PpQ: %f\r\n",puid,ncells,nprobes/(btScalar)nqueries);
|
2019-01-03 13:26:51 +00:00
|
|
|
nqueries = 1;
|
|
|
|
nprobes = 1;
|
|
|
|
++puid; ///@todo: Reset puid's when int range limit is reached */
|
|
|
|
/* else setup a priority list... */
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
//
|
2019-01-03 13:26:51 +00:00
|
|
|
int RemoveReferences(btCollisionShape* pcs)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
int refcount = 0;
|
|
|
|
for (int i = 0; i < cells.size(); ++i)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
Cell*& root = cells[i];
|
|
|
|
Cell* pp = 0;
|
|
|
|
Cell* pc = root;
|
|
|
|
while (pc)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
Cell* pn = pc->next;
|
|
|
|
if (pc->pclient == pcs)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (pp)
|
|
|
|
pp->next = pn;
|
|
|
|
else
|
|
|
|
root = pn;
|
|
|
|
delete pc;
|
|
|
|
pc = pp;
|
|
|
|
++refcount;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
pp = pc;
|
|
|
|
pc = pn;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
return (refcount);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
//
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar Evaluate(const btVector3& x,
|
|
|
|
const btCollisionShape* shape,
|
|
|
|
btVector3& normal,
|
|
|
|
btScalar margin)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
/* Lookup cell */
|
|
|
|
const btVector3 scx = x / voxelsz;
|
|
|
|
const IntFrac ix = Decompose(scx.x());
|
|
|
|
const IntFrac iy = Decompose(scx.y());
|
|
|
|
const IntFrac iz = Decompose(scx.z());
|
|
|
|
const unsigned h = Hash(ix.b, iy.b, iz.b, shape);
|
|
|
|
Cell*& root = cells[static_cast<int>(h % cells.size())];
|
|
|
|
Cell* c = root;
|
2017-08-01 12:30:58 +00:00
|
|
|
++nqueries;
|
2019-01-03 13:26:51 +00:00
|
|
|
while (c)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
++nprobes;
|
2019-01-03 13:26:51 +00:00
|
|
|
if ((c->hash == h) &&
|
|
|
|
(c->c[0] == ix.b) &&
|
|
|
|
(c->c[1] == iy.b) &&
|
|
|
|
(c->c[2] == iz.b) &&
|
|
|
|
(c->pclient == shape))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
else
|
2019-01-03 13:26:51 +00:00
|
|
|
{
|
2020-01-08 17:05:43 +00:00
|
|
|
// printf("c->hash/c[0][1][2]=%d,%d,%d,%d\n", c->hash, c->c[0], c->c[1],c->c[2]);
|
|
|
|
//printf("h,ixb,iyb,izb=%d,%d,%d,%d\n", h,ix.b, iy.b, iz.b);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
c = c->next;
|
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (!c)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
++nprobes;
|
2017-08-01 12:30:58 +00:00
|
|
|
++ncells;
|
|
|
|
//int sz = sizeof(Cell);
|
2019-01-03 13:26:51 +00:00
|
|
|
if (ncells > m_clampCells)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
static int numResets = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
numResets++;
|
2019-01-03 13:26:51 +00:00
|
|
|
// printf("numResets=%d\n",numResets);
|
2017-08-01 12:30:58 +00:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
c = new Cell();
|
|
|
|
c->next = root;
|
|
|
|
root = c;
|
|
|
|
c->pclient = shape;
|
|
|
|
c->hash = h;
|
|
|
|
c->c[0] = ix.b;
|
|
|
|
c->c[1] = iy.b;
|
|
|
|
c->c[2] = iz.b;
|
2017-08-01 12:30:58 +00:00
|
|
|
BuildCell(*c);
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
c->puid = puid;
|
|
|
|
/* Extract infos */
|
|
|
|
const int o[] = {ix.i, iy.i, iz.i};
|
|
|
|
const btScalar d[] = {c->d[o[0] + 0][o[1] + 0][o[2] + 0],
|
|
|
|
c->d[o[0] + 1][o[1] + 0][o[2] + 0],
|
|
|
|
c->d[o[0] + 1][o[1] + 1][o[2] + 0],
|
|
|
|
c->d[o[0] + 0][o[1] + 1][o[2] + 0],
|
|
|
|
c->d[o[0] + 0][o[1] + 0][o[2] + 1],
|
|
|
|
c->d[o[0] + 1][o[1] + 0][o[2] + 1],
|
|
|
|
c->d[o[0] + 1][o[1] + 1][o[2] + 1],
|
|
|
|
c->d[o[0] + 0][o[1] + 1][o[2] + 1]};
|
|
|
|
/* Normal */
|
2017-08-01 12:30:58 +00:00
|
|
|
#if 1
|
2019-01-03 13:26:51 +00:00
|
|
|
const btScalar gx[] = {d[1] - d[0], d[2] - d[3],
|
|
|
|
d[5] - d[4], d[6] - d[7]};
|
|
|
|
const btScalar gy[] = {d[3] - d[0], d[2] - d[1],
|
|
|
|
d[7] - d[4], d[6] - d[5]};
|
|
|
|
const btScalar gz[] = {d[4] - d[0], d[5] - d[1],
|
|
|
|
d[7] - d[3], d[6] - d[2]};
|
|
|
|
normal.setX(Lerp(Lerp(gx[0], gx[1], iy.f),
|
|
|
|
Lerp(gx[2], gx[3], iy.f), iz.f));
|
|
|
|
normal.setY(Lerp(Lerp(gy[0], gy[1], ix.f),
|
|
|
|
Lerp(gy[2], gy[3], ix.f), iz.f));
|
|
|
|
normal.setZ(Lerp(Lerp(gz[0], gz[1], ix.f),
|
|
|
|
Lerp(gz[2], gz[3], ix.f), iy.f));
|
2020-01-08 17:05:43 +00:00
|
|
|
normal.safeNormalize();
|
2017-08-01 12:30:58 +00:00
|
|
|
#else
|
2019-01-03 13:26:51 +00:00
|
|
|
normal = btVector3(d[1] - d[0], d[3] - d[0], d[4] - d[0]).normalized();
|
2017-08-01 12:30:58 +00:00
|
|
|
#endif
|
2019-01-03 13:26:51 +00:00
|
|
|
/* Distance */
|
|
|
|
const btScalar d0 = Lerp(Lerp(d[0], d[1], ix.f),
|
|
|
|
Lerp(d[3], d[2], ix.f), iy.f);
|
|
|
|
const btScalar d1 = Lerp(Lerp(d[4], d[5], ix.f),
|
|
|
|
Lerp(d[7], d[6], ix.f), iy.f);
|
|
|
|
return (Lerp(d0, d1, iz.f) - margin);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
//
|
2019-01-03 13:26:51 +00:00
|
|
|
void BuildCell(Cell& c)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
const btVector3 org = btVector3((btScalar)c.c[0],
|
|
|
|
(btScalar)c.c[1],
|
|
|
|
(btScalar)c.c[2]) *
|
|
|
|
CELLSIZE * voxelsz;
|
|
|
|
for (int k = 0; k <= CELLSIZE; ++k)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
const btScalar z = voxelsz * k + org.z();
|
|
|
|
for (int j = 0; j <= CELLSIZE; ++j)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
const btScalar y = voxelsz * j + org.y();
|
|
|
|
for (int i = 0; i <= CELLSIZE; ++i)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
const btScalar x = voxelsz * i + org.x();
|
|
|
|
c.d[i][j][k] = DistanceToShape(btVector3(x, y, z),
|
|
|
|
c.pclient);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
2019-01-03 13:26:51 +00:00
|
|
|
static inline btScalar DistanceToShape(const btVector3& x,
|
|
|
|
const btCollisionShape* shape)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btTransform unit;
|
2017-08-01 12:30:58 +00:00
|
|
|
unit.setIdentity();
|
2019-01-03 13:26:51 +00:00
|
|
|
if (shape->isConvex())
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btGjkEpaSolver2::sResults res;
|
|
|
|
const btConvexShape* csh = static_cast<const btConvexShape*>(shape);
|
|
|
|
return (btGjkEpaSolver2::SignedDistance(x, 0, csh, unit, res));
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
return (0);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
//
|
2019-01-03 13:26:51 +00:00
|
|
|
static inline IntFrac Decompose(btScalar x)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
/* That one need a lot of improvements... */
|
2019-01-03 13:26:51 +00:00
|
|
|
/* Remove test, faster floor... */
|
|
|
|
IntFrac r;
|
|
|
|
x /= CELLSIZE;
|
|
|
|
const int o = x < 0 ? (int)(-x + 1) : 0;
|
|
|
|
x += o;
|
|
|
|
r.b = (int)x;
|
|
|
|
const btScalar k = (x - r.b) * CELLSIZE;
|
|
|
|
r.i = (int)k;
|
|
|
|
r.f = k - r.i;
|
|
|
|
r.b -= o;
|
|
|
|
return (r);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
//
|
2019-01-03 13:26:51 +00:00
|
|
|
static inline btScalar Lerp(btScalar a, btScalar b, btScalar t)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
return (a + (b - a) * t);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2019-01-03 13:26:51 +00:00
|
|
|
static inline unsigned int Hash(int x, int y, int z, const btCollisionShape* shape)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
struct btS
|
2019-01-03 13:26:51 +00:00
|
|
|
{
|
2020-01-08 17:05:43 +00:00
|
|
|
int x, y, z, w;
|
2017-08-01 12:30:58 +00:00
|
|
|
void* p;
|
|
|
|
};
|
|
|
|
|
|
|
|
btS myset;
|
2020-01-08 17:05:43 +00:00
|
|
|
//memset may be needed in case of additional (uninitialized) padding!
|
|
|
|
//memset(&myset, 0, sizeof(btS));
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
myset.x = x;
|
|
|
|
myset.y = y;
|
|
|
|
myset.z = z;
|
2020-01-08 17:05:43 +00:00
|
|
|
myset.w = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
myset.p = (void*)shape;
|
2020-01-08 17:05:43 +00:00
|
|
|
const char* ptr = (const char*)&myset;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2020-01-08 17:05:43 +00:00
|
|
|
unsigned int result = HsiehHash(ptr, sizeof(btS) );
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif //BT_SPARSE_SDF_H
|