btSparseSDF.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. // Modified Paul Hsieh hash
  19. template <const int DWORDLEN>
  20. unsigned int HsiehHash(const void* pdata)
  21. {
  22. const unsigned short* data=(const unsigned short*)pdata;
  23. unsigned hash=DWORDLEN<<2,tmp;
  24. for(int i=0;i<DWORDLEN;++i)
  25. {
  26. hash += data[0];
  27. tmp = (data[1]<<11)^hash;
  28. hash = (hash<<16)^tmp;
  29. data += 2;
  30. hash += hash>>11;
  31. }
  32. hash^=hash<<3;hash+=hash>>5;
  33. hash^=hash<<4;hash+=hash>>17;
  34. hash^=hash<<25;hash+=hash>>6;
  35. return(hash);
  36. }
  37. template <const int CELLSIZE>
  38. struct btSparseSdf
  39. {
  40. //
  41. // Inner types
  42. //
  43. struct IntFrac
  44. {
  45. int b;
  46. int i;
  47. btScalar f;
  48. };
  49. struct Cell
  50. {
  51. btScalar d[CELLSIZE+1][CELLSIZE+1][CELLSIZE+1];
  52. int c[3];
  53. int puid;
  54. unsigned hash;
  55. const btCollisionShape* pclient;
  56. Cell* next;
  57. };
  58. //
  59. // Fields
  60. //
  61. btAlignedObjectArray<Cell*> cells;
  62. btScalar voxelsz;
  63. int puid;
  64. int ncells;
  65. int m_clampCells;
  66. int nprobes;
  67. int nqueries;
  68. //
  69. // Methods
  70. //
  71. //
  72. void Initialize(int hashsize=2383, int clampCells = 256*1024)
  73. {
  74. //avoid a crash due to running out of memory, so clamp the maximum number of cells allocated
  75. //if this limit is reached, the SDF is reset (at the cost of some performance during the reset)
  76. m_clampCells = clampCells;
  77. cells.resize(hashsize,0);
  78. Reset();
  79. }
  80. //
  81. void Reset()
  82. {
  83. for(int i=0,ni=cells.size();i<ni;++i)
  84. {
  85. Cell* pc=cells[i];
  86. cells[i]=0;
  87. while(pc)
  88. {
  89. Cell* pn=pc->next;
  90. delete pc;
  91. pc=pn;
  92. }
  93. }
  94. voxelsz =0.25;
  95. puid =0;
  96. ncells =0;
  97. nprobes =1;
  98. nqueries =1;
  99. }
  100. //
  101. void GarbageCollect(int lifetime=256)
  102. {
  103. const int life=puid-lifetime;
  104. for(int i=0;i<cells.size();++i)
  105. {
  106. Cell*& root=cells[i];
  107. Cell* pp=0;
  108. Cell* pc=root;
  109. while(pc)
  110. {
  111. Cell* pn=pc->next;
  112. if(pc->puid<life)
  113. {
  114. if(pp) pp->next=pn; else root=pn;
  115. delete pc;pc=pp;--ncells;
  116. }
  117. pp=pc;pc=pn;
  118. }
  119. }
  120. //printf("GC[%d]: %d cells, PpQ: %f\r\n",puid,ncells,nprobes/(btScalar)nqueries);
  121. nqueries=1;
  122. nprobes=1;
  123. ++puid; ///@todo: Reset puid's when int range limit is reached */
  124. /* else setup a priority list... */
  125. }
  126. //
  127. int RemoveReferences(btCollisionShape* pcs)
  128. {
  129. int refcount=0;
  130. for(int i=0;i<cells.size();++i)
  131. {
  132. Cell*& root=cells[i];
  133. Cell* pp=0;
  134. Cell* pc=root;
  135. while(pc)
  136. {
  137. Cell* pn=pc->next;
  138. if(pc->pclient==pcs)
  139. {
  140. if(pp) pp->next=pn; else root=pn;
  141. delete pc;pc=pp;++refcount;
  142. }
  143. pp=pc;pc=pn;
  144. }
  145. }
  146. return(refcount);
  147. }
  148. //
  149. btScalar Evaluate( const btVector3& x,
  150. const btCollisionShape* shape,
  151. btVector3& normal,
  152. btScalar margin)
  153. {
  154. /* Lookup cell */
  155. const btVector3 scx=x/voxelsz;
  156. const IntFrac ix=Decompose(scx.x());
  157. const IntFrac iy=Decompose(scx.y());
  158. const IntFrac iz=Decompose(scx.z());
  159. const unsigned h=Hash(ix.b,iy.b,iz.b,shape);
  160. Cell*& root=cells[static_cast<int>(h%cells.size())];
  161. Cell* c=root;
  162. ++nqueries;
  163. while(c)
  164. {
  165. ++nprobes;
  166. if( (c->hash==h) &&
  167. (c->c[0]==ix.b) &&
  168. (c->c[1]==iy.b) &&
  169. (c->c[2]==iz.b) &&
  170. (c->pclient==shape))
  171. { break; }
  172. else
  173. { c=c->next; }
  174. }
  175. if(!c)
  176. {
  177. ++nprobes;
  178. ++ncells;
  179. //int sz = sizeof(Cell);
  180. if (ncells>m_clampCells)
  181. {
  182. static int numResets=0;
  183. numResets++;
  184. // printf("numResets=%d\n",numResets);
  185. Reset();
  186. }
  187. c=new Cell();
  188. c->next=root;root=c;
  189. c->pclient=shape;
  190. c->hash=h;
  191. c->c[0]=ix.b;c->c[1]=iy.b;c->c[2]=iz.b;
  192. BuildCell(*c);
  193. }
  194. c->puid=puid;
  195. /* Extract infos */
  196. const int o[]={ ix.i,iy.i,iz.i};
  197. const btScalar d[]={ c->d[o[0]+0][o[1]+0][o[2]+0],
  198. c->d[o[0]+1][o[1]+0][o[2]+0],
  199. c->d[o[0]+1][o[1]+1][o[2]+0],
  200. c->d[o[0]+0][o[1]+1][o[2]+0],
  201. c->d[o[0]+0][o[1]+0][o[2]+1],
  202. c->d[o[0]+1][o[1]+0][o[2]+1],
  203. c->d[o[0]+1][o[1]+1][o[2]+1],
  204. c->d[o[0]+0][o[1]+1][o[2]+1]};
  205. /* Normal */
  206. #if 1
  207. const btScalar gx[]={ d[1]-d[0],d[2]-d[3],
  208. d[5]-d[4],d[6]-d[7]};
  209. const btScalar gy[]={ d[3]-d[0],d[2]-d[1],
  210. d[7]-d[4],d[6]-d[5]};
  211. const btScalar gz[]={ d[4]-d[0],d[5]-d[1],
  212. d[7]-d[3],d[6]-d[2]};
  213. normal.setX(Lerp( Lerp(gx[0],gx[1],iy.f),
  214. Lerp(gx[2],gx[3],iy.f),iz.f));
  215. normal.setY(Lerp( Lerp(gy[0],gy[1],ix.f),
  216. Lerp(gy[2],gy[3],ix.f),iz.f));
  217. normal.setZ(Lerp( Lerp(gz[0],gz[1],ix.f),
  218. Lerp(gz[2],gz[3],ix.f),iy.f));
  219. normal = normal.normalized();
  220. #else
  221. normal = btVector3(d[1]-d[0],d[3]-d[0],d[4]-d[0]).normalized();
  222. #endif
  223. /* Distance */
  224. const btScalar d0=Lerp(Lerp(d[0],d[1],ix.f),
  225. Lerp(d[3],d[2],ix.f),iy.f);
  226. const btScalar d1=Lerp(Lerp(d[4],d[5],ix.f),
  227. Lerp(d[7],d[6],ix.f),iy.f);
  228. return(Lerp(d0,d1,iz.f)-margin);
  229. }
  230. //
  231. void BuildCell(Cell& c)
  232. {
  233. const btVector3 org=btVector3( (btScalar)c.c[0],
  234. (btScalar)c.c[1],
  235. (btScalar)c.c[2]) *
  236. CELLSIZE*voxelsz;
  237. for(int k=0;k<=CELLSIZE;++k)
  238. {
  239. const btScalar z=voxelsz*k+org.z();
  240. for(int j=0;j<=CELLSIZE;++j)
  241. {
  242. const btScalar y=voxelsz*j+org.y();
  243. for(int i=0;i<=CELLSIZE;++i)
  244. {
  245. const btScalar x=voxelsz*i+org.x();
  246. c.d[i][j][k]=DistanceToShape( btVector3(x,y,z),
  247. c.pclient);
  248. }
  249. }
  250. }
  251. }
  252. //
  253. static inline btScalar DistanceToShape(const btVector3& x,
  254. const btCollisionShape* shape)
  255. {
  256. btTransform unit;
  257. unit.setIdentity();
  258. if(shape->isConvex())
  259. {
  260. btGjkEpaSolver2::sResults res;
  261. const btConvexShape* csh=static_cast<const btConvexShape*>(shape);
  262. return(btGjkEpaSolver2::SignedDistance(x,0,csh,unit,res));
  263. }
  264. return(0);
  265. }
  266. //
  267. static inline IntFrac Decompose(btScalar x)
  268. {
  269. /* That one need a lot of improvements... */
  270. /* Remove test, faster floor... */
  271. IntFrac r;
  272. x/=CELLSIZE;
  273. const int o=x<0?(int)(-x+1):0;
  274. x+=o;r.b=(int)x;
  275. const btScalar k=(x-r.b)*CELLSIZE;
  276. r.i=(int)k;r.f=k-r.i;r.b-=o;
  277. return(r);
  278. }
  279. //
  280. static inline btScalar Lerp(btScalar a,btScalar b,btScalar t)
  281. {
  282. return(a+(b-a)*t);
  283. }
  284. //
  285. static inline unsigned int Hash(int x,int y,int z,const btCollisionShape* shape)
  286. {
  287. struct btS
  288. {
  289. int x,y,z;
  290. void* p;
  291. };
  292. btS myset;
  293. myset.x=x;myset.y=y;myset.z=z;myset.p=(void*)shape;
  294. const void* ptr = &myset;
  295. unsigned int result = HsiehHash<sizeof(btS)/4> (ptr);
  296. return result;
  297. }
  298. };
  299. #endif //BT_SPARSE_SDF_H