btSparseSDF.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. ///btSparseSdf implementation by Nathanael Presson
  14. #ifndef BT_SPARSE_SDF_H
  15. #define BT_SPARSE_SDF_H
  16. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  17. #include "BulletCollision/NarrowPhaseCollision/btGjkEpa2.h"
  18. // Fast Hash
  19. #if !defined(get16bits)
  20. #define get16bits(d) ((((unsigned int)(((const unsigned char*)(d))[1])) << 8) + (unsigned int)(((const unsigned char*)(d))[0]))
  21. #endif
  22. //
  23. // super hash function by Paul Hsieh
  24. //
  25. inline unsigned int HsiehHash(const char* data, int len)
  26. {
  27. unsigned int hash = len, tmp;
  28. len >>= 2;
  29. /* Main loop */
  30. for (; len > 0; len--)
  31. {
  32. hash += get16bits(data);
  33. tmp = (get16bits(data + 2) << 11) ^ hash;
  34. hash = (hash << 16) ^ tmp;
  35. data += 2 * sizeof(unsigned short);
  36. hash += hash >> 11;
  37. }
  38. /* Force "avalanching" of final 127 bits */
  39. hash ^= hash << 3;
  40. hash += hash >> 5;
  41. hash ^= hash << 4;
  42. hash += hash >> 17;
  43. hash ^= hash << 25;
  44. hash += hash >> 6;
  45. return hash;
  46. }
  47. template <const int CELLSIZE>
  48. struct btSparseSdf
  49. {
  50. //
  51. // Inner types
  52. //
  53. struct IntFrac
  54. {
  55. int b;
  56. int i;
  57. btScalar f;
  58. };
  59. struct Cell
  60. {
  61. btScalar d[CELLSIZE + 1][CELLSIZE + 1][CELLSIZE + 1];
  62. int c[3];
  63. int puid;
  64. unsigned hash;
  65. const btCollisionShape* pclient;
  66. Cell* next;
  67. };
  68. //
  69. // Fields
  70. //
  71. btAlignedObjectArray<Cell*> cells;
  72. btScalar voxelsz;
  73. btScalar m_defaultVoxelsz;
  74. int puid;
  75. int ncells;
  76. int m_clampCells;
  77. int nprobes;
  78. int nqueries;
  79. ~btSparseSdf()
  80. {
  81. Reset();
  82. }
  83. //
  84. // Methods
  85. //
  86. //
  87. void Initialize(int hashsize = 2383, int clampCells = 256 * 1024)
  88. {
  89. //avoid a crash due to running out of memory, so clamp the maximum number of cells allocated
  90. //if this limit is reached, the SDF is reset (at the cost of some performance during the reset)
  91. m_clampCells = clampCells;
  92. cells.resize(hashsize, 0);
  93. m_defaultVoxelsz = 0.25;
  94. Reset();
  95. }
  96. //
  97. void setDefaultVoxelsz(btScalar sz)
  98. {
  99. m_defaultVoxelsz = sz;
  100. }
  101. void Reset()
  102. {
  103. for (int i = 0, ni = cells.size(); i < ni; ++i)
  104. {
  105. Cell* pc = cells[i];
  106. cells[i] = 0;
  107. while (pc)
  108. {
  109. Cell* pn = pc->next;
  110. delete pc;
  111. pc = pn;
  112. }
  113. }
  114. voxelsz = m_defaultVoxelsz;
  115. puid = 0;
  116. ncells = 0;
  117. nprobes = 1;
  118. nqueries = 1;
  119. }
  120. //
  121. void GarbageCollect(int lifetime = 256)
  122. {
  123. const int life = puid - lifetime;
  124. for (int i = 0; i < cells.size(); ++i)
  125. {
  126. Cell*& root = cells[i];
  127. Cell* pp = 0;
  128. Cell* pc = root;
  129. while (pc)
  130. {
  131. Cell* pn = pc->next;
  132. if (pc->puid < life)
  133. {
  134. if (pp)
  135. pp->next = pn;
  136. else
  137. root = pn;
  138. delete pc;
  139. pc = pp;
  140. --ncells;
  141. }
  142. pp = pc;
  143. pc = pn;
  144. }
  145. }
  146. //printf("GC[%d]: %d cells, PpQ: %f\r\n",puid,ncells,nprobes/(btScalar)nqueries);
  147. nqueries = 1;
  148. nprobes = 1;
  149. ++puid; ///@todo: Reset puid's when int range limit is reached */
  150. /* else setup a priority list... */
  151. }
  152. //
  153. int RemoveReferences(btCollisionShape* pcs)
  154. {
  155. int refcount = 0;
  156. for (int i = 0; i < cells.size(); ++i)
  157. {
  158. Cell*& root = cells[i];
  159. Cell* pp = 0;
  160. Cell* pc = root;
  161. while (pc)
  162. {
  163. Cell* pn = pc->next;
  164. if (pc->pclient == pcs)
  165. {
  166. if (pp)
  167. pp->next = pn;
  168. else
  169. root = pn;
  170. delete pc;
  171. pc = pp;
  172. ++refcount;
  173. }
  174. pp = pc;
  175. pc = pn;
  176. }
  177. }
  178. return (refcount);
  179. }
  180. //
  181. btScalar Evaluate(const btVector3& x,
  182. const btCollisionShape* shape,
  183. btVector3& normal,
  184. btScalar margin)
  185. {
  186. /* Lookup cell */
  187. const btVector3 scx = x / voxelsz;
  188. const IntFrac ix = Decompose(scx.x());
  189. const IntFrac iy = Decompose(scx.y());
  190. const IntFrac iz = Decompose(scx.z());
  191. const unsigned h = Hash(ix.b, iy.b, iz.b, shape);
  192. Cell*& root = cells[static_cast<int>(h % cells.size())];
  193. Cell* c = root;
  194. ++nqueries;
  195. while (c)
  196. {
  197. ++nprobes;
  198. if ((c->hash == h) &&
  199. (c->c[0] == ix.b) &&
  200. (c->c[1] == iy.b) &&
  201. (c->c[2] == iz.b) &&
  202. (c->pclient == shape))
  203. {
  204. break;
  205. }
  206. else
  207. {
  208. // printf("c->hash/c[0][1][2]=%d,%d,%d,%d\n", c->hash, c->c[0], c->c[1],c->c[2]);
  209. //printf("h,ixb,iyb,izb=%d,%d,%d,%d\n", h,ix.b, iy.b, iz.b);
  210. c = c->next;
  211. }
  212. }
  213. if (!c)
  214. {
  215. ++nprobes;
  216. ++ncells;
  217. //int sz = sizeof(Cell);
  218. if (ncells > m_clampCells)
  219. {
  220. //static int numResets = 0;
  221. //numResets++;
  222. //printf("numResets=%d\n",numResets);
  223. Reset();
  224. }
  225. c = new Cell();
  226. c->next = root;
  227. root = c;
  228. c->pclient = shape;
  229. c->hash = h;
  230. c->c[0] = ix.b;
  231. c->c[1] = iy.b;
  232. c->c[2] = iz.b;
  233. BuildCell(*c);
  234. }
  235. c->puid = puid;
  236. /* Extract infos */
  237. const int o[] = {ix.i, iy.i, iz.i};
  238. const btScalar d[] = {c->d[o[0] + 0][o[1] + 0][o[2] + 0],
  239. c->d[o[0] + 1][o[1] + 0][o[2] + 0],
  240. c->d[o[0] + 1][o[1] + 1][o[2] + 0],
  241. c->d[o[0] + 0][o[1] + 1][o[2] + 0],
  242. c->d[o[0] + 0][o[1] + 0][o[2] + 1],
  243. c->d[o[0] + 1][o[1] + 0][o[2] + 1],
  244. c->d[o[0] + 1][o[1] + 1][o[2] + 1],
  245. c->d[o[0] + 0][o[1] + 1][o[2] + 1]};
  246. /* Normal */
  247. #if 1
  248. const btScalar gx[] = {d[1] - d[0], d[2] - d[3],
  249. d[5] - d[4], d[6] - d[7]};
  250. const btScalar gy[] = {d[3] - d[0], d[2] - d[1],
  251. d[7] - d[4], d[6] - d[5]};
  252. const btScalar gz[] = {d[4] - d[0], d[5] - d[1],
  253. d[7] - d[3], d[6] - d[2]};
  254. normal.setX(Lerp(Lerp(gx[0], gx[1], iy.f),
  255. Lerp(gx[2], gx[3], iy.f), iz.f));
  256. normal.setY(Lerp(Lerp(gy[0], gy[1], ix.f),
  257. Lerp(gy[2], gy[3], ix.f), iz.f));
  258. normal.setZ(Lerp(Lerp(gz[0], gz[1], ix.f),
  259. Lerp(gz[2], gz[3], ix.f), iy.f));
  260. normal.safeNormalize();
  261. #else
  262. normal = btVector3(d[1] - d[0], d[3] - d[0], d[4] - d[0]).normalized();
  263. #endif
  264. /* Distance */
  265. const btScalar d0 = Lerp(Lerp(d[0], d[1], ix.f),
  266. Lerp(d[3], d[2], ix.f), iy.f);
  267. const btScalar d1 = Lerp(Lerp(d[4], d[5], ix.f),
  268. Lerp(d[7], d[6], ix.f), iy.f);
  269. return (Lerp(d0, d1, iz.f) - margin);
  270. }
  271. //
  272. void BuildCell(Cell& c)
  273. {
  274. const btVector3 org = btVector3((btScalar)c.c[0],
  275. (btScalar)c.c[1],
  276. (btScalar)c.c[2]) *
  277. CELLSIZE * voxelsz;
  278. for (int k = 0; k <= CELLSIZE; ++k)
  279. {
  280. const btScalar z = voxelsz * k + org.z();
  281. for (int j = 0; j <= CELLSIZE; ++j)
  282. {
  283. const btScalar y = voxelsz * j + org.y();
  284. for (int i = 0; i <= CELLSIZE; ++i)
  285. {
  286. const btScalar x = voxelsz * i + org.x();
  287. c.d[i][j][k] = DistanceToShape(btVector3(x, y, z),
  288. c.pclient);
  289. }
  290. }
  291. }
  292. }
  293. //
  294. static inline btScalar DistanceToShape(const btVector3& x,
  295. const btCollisionShape* shape)
  296. {
  297. btTransform unit;
  298. unit.setIdentity();
  299. if (shape->isConvex())
  300. {
  301. btGjkEpaSolver2::sResults res;
  302. const btConvexShape* csh = static_cast<const btConvexShape*>(shape);
  303. return (btGjkEpaSolver2::SignedDistance(x, 0, csh, unit, res));
  304. }
  305. return (0);
  306. }
  307. //
  308. static inline IntFrac Decompose(btScalar x)
  309. {
  310. /* That one need a lot of improvements... */
  311. /* Remove test, faster floor... */
  312. IntFrac r;
  313. x /= CELLSIZE;
  314. const int o = x < 0 ? (int)(-x + 1) : 0;
  315. x += o;
  316. r.b = (int)x;
  317. const btScalar k = (x - r.b) * CELLSIZE;
  318. r.i = (int)k;
  319. r.f = k - r.i;
  320. r.b -= o;
  321. return (r);
  322. }
  323. //
  324. static inline btScalar Lerp(btScalar a, btScalar b, btScalar t)
  325. {
  326. return (a + (b - a) * t);
  327. }
  328. //
  329. static inline unsigned int Hash(int x, int y, int z, const btCollisionShape* shape)
  330. {
  331. struct btS
  332. {
  333. int x, y, z, w;
  334. void* p;
  335. };
  336. btS myset;
  337. //memset may be needed in case of additional (uninitialized) padding!
  338. //memset(&myset, 0, sizeof(btS));
  339. myset.x = x;
  340. myset.y = y;
  341. myset.z = z;
  342. myset.w = 0;
  343. myset.p = (void*)shape;
  344. const char* ptr = (const char*)&myset;
  345. unsigned int result = HsiehHash(ptr, sizeof(btS));
  346. return result;
  347. }
  348. };
  349. #endif //BT_SPARSE_SDF_H