1
0

btMultiSapBroadphase.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. #include "btMultiSapBroadphase.h"
  14. #include "btSimpleBroadphase.h"
  15. #include "bullet/LinearMath/btAabbUtil2.h"
  16. #include "btQuantizedBvh.h"
  17. /// btSapBroadphaseArray m_sapBroadphases;
  18. /// btOverlappingPairCache* m_overlappingPairs;
  19. extern int gOverlappingPairs;
  20. /*
  21. class btMultiSapSortedOverlappingPairCache : public btSortedOverlappingPairCache
  22. {
  23. public:
  24. virtual btBroadphasePair* addOverlappingPair(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1)
  25. {
  26. return btSortedOverlappingPairCache::addOverlappingPair((btBroadphaseProxy*)proxy0->m_multiSapParentProxy,(btBroadphaseProxy*)proxy1->m_multiSapParentProxy);
  27. }
  28. };
  29. */
  30. btMultiSapBroadphase::btMultiSapBroadphase(int /*maxProxies*/,btOverlappingPairCache* pairCache)
  31. :m_overlappingPairs(pairCache),
  32. m_optimizedAabbTree(0),
  33. m_ownsPairCache(false),
  34. m_invalidPair(0)
  35. {
  36. if (!m_overlappingPairs)
  37. {
  38. m_ownsPairCache = true;
  39. void* mem = btAlignedAlloc(sizeof(btSortedOverlappingPairCache),16);
  40. m_overlappingPairs = new (mem)btSortedOverlappingPairCache();
  41. }
  42. struct btMultiSapOverlapFilterCallback : public btOverlapFilterCallback
  43. {
  44. virtual ~btMultiSapOverlapFilterCallback()
  45. {}
  46. // return true when pairs need collision
  47. virtual bool needBroadphaseCollision(btBroadphaseProxy* childProxy0,btBroadphaseProxy* childProxy1) const
  48. {
  49. btBroadphaseProxy* multiProxy0 = (btBroadphaseProxy*)childProxy0->m_multiSapParentProxy;
  50. btBroadphaseProxy* multiProxy1 = (btBroadphaseProxy*)childProxy1->m_multiSapParentProxy;
  51. bool collides = (multiProxy0->m_collisionFilterGroup & multiProxy1->m_collisionFilterMask) != 0;
  52. collides = collides && (multiProxy1->m_collisionFilterGroup & multiProxy0->m_collisionFilterMask);
  53. return collides;
  54. }
  55. };
  56. void* mem = btAlignedAlloc(sizeof(btMultiSapOverlapFilterCallback),16);
  57. m_filterCallback = new (mem)btMultiSapOverlapFilterCallback();
  58. m_overlappingPairs->setOverlapFilterCallback(m_filterCallback);
  59. // mem = btAlignedAlloc(sizeof(btSimpleBroadphase),16);
  60. // m_simpleBroadphase = new (mem) btSimpleBroadphase(maxProxies,m_overlappingPairs);
  61. }
  62. btMultiSapBroadphase::~btMultiSapBroadphase()
  63. {
  64. if (m_ownsPairCache)
  65. {
  66. m_overlappingPairs->~btOverlappingPairCache();
  67. btAlignedFree(m_overlappingPairs);
  68. }
  69. }
  70. void btMultiSapBroadphase::buildTree(const btVector3& bvhAabbMin,const btVector3& bvhAabbMax)
  71. {
  72. m_optimizedAabbTree = new btQuantizedBvh();
  73. m_optimizedAabbTree->setQuantizationValues(bvhAabbMin,bvhAabbMax);
  74. QuantizedNodeArray& nodes = m_optimizedAabbTree->getLeafNodeArray();
  75. for (int i=0;i<m_sapBroadphases.size();i++)
  76. {
  77. btQuantizedBvhNode node;
  78. btVector3 aabbMin,aabbMax;
  79. m_sapBroadphases[i]->getBroadphaseAabb(aabbMin,aabbMax);
  80. m_optimizedAabbTree->quantize(&node.m_quantizedAabbMin[0],aabbMin,0);
  81. m_optimizedAabbTree->quantize(&node.m_quantizedAabbMax[0],aabbMax,1);
  82. int partId = 0;
  83. node.m_escapeIndexOrTriangleIndex = (partId<<(31-MAX_NUM_PARTS_IN_BITS)) | i;
  84. nodes.push_back(node);
  85. }
  86. m_optimizedAabbTree->buildInternal();
  87. }
  88. btBroadphaseProxy* btMultiSapBroadphase::createProxy( const btVector3& aabbMin, const btVector3& aabbMax,int shapeType,void* userPtr, short int collisionFilterGroup,short int collisionFilterMask, btDispatcher* dispatcher,void* /*ignoreMe*/)
  89. {
  90. //void* ignoreMe -> we could think of recursive multi-sap, if someone is interested
  91. void* mem = btAlignedAlloc(sizeof(btMultiSapProxy),16);
  92. btMultiSapProxy* proxy = new (mem)btMultiSapProxy(aabbMin, aabbMax,shapeType,userPtr, collisionFilterGroup,collisionFilterMask);
  93. m_multiSapProxies.push_back(proxy);
  94. ///this should deal with inserting/removal into child broadphases
  95. setAabb(proxy,aabbMin,aabbMax,dispatcher);
  96. return proxy;
  97. }
  98. void btMultiSapBroadphase::destroyProxy(btBroadphaseProxy* /*proxy*/,btDispatcher* /*dispatcher*/)
  99. {
  100. ///not yet
  101. btAssert(0);
  102. }
  103. void btMultiSapBroadphase::addToChildBroadphase(btMultiSapProxy* parentMultiSapProxy, btBroadphaseProxy* childProxy, btBroadphaseInterface* childBroadphase)
  104. {
  105. void* mem = btAlignedAlloc(sizeof(btBridgeProxy),16);
  106. btBridgeProxy* bridgeProxyRef = new(mem) btBridgeProxy;
  107. bridgeProxyRef->m_childProxy = childProxy;
  108. bridgeProxyRef->m_childBroadphase = childBroadphase;
  109. parentMultiSapProxy->m_bridgeProxies.push_back(bridgeProxyRef);
  110. }
  111. bool boxIsContainedWithinBox(const btVector3& amin,const btVector3& amax,const btVector3& bmin,const btVector3& bmax);
  112. bool boxIsContainedWithinBox(const btVector3& amin,const btVector3& amax,const btVector3& bmin,const btVector3& bmax)
  113. {
  114. return
  115. amin.getX() >= bmin.getX() && amax.getX() <= bmax.getX() &&
  116. amin.getY() >= bmin.getY() && amax.getY() <= bmax.getY() &&
  117. amin.getZ() >= bmin.getZ() && amax.getZ() <= bmax.getZ();
  118. }
  119. void btMultiSapBroadphase::getAabb(btBroadphaseProxy* proxy,btVector3& aabbMin, btVector3& aabbMax ) const
  120. {
  121. btMultiSapProxy* multiProxy = static_cast<btMultiSapProxy*>(proxy);
  122. aabbMin = multiProxy->m_aabbMin;
  123. aabbMax = multiProxy->m_aabbMax;
  124. }
  125. void btMultiSapBroadphase::rayTest(const btVector3& rayFrom,const btVector3& rayTo, btBroadphaseRayCallback& rayCallback, const btVector3& aabbMin,const btVector3& aabbMax)
  126. {
  127. for (int i=0;i<m_multiSapProxies.size();i++)
  128. {
  129. rayCallback.process(m_multiSapProxies[i]);
  130. }
  131. }
  132. //#include <stdio.h>
  133. void btMultiSapBroadphase::setAabb(btBroadphaseProxy* proxy,const btVector3& aabbMin,const btVector3& aabbMax, btDispatcher* dispatcher)
  134. {
  135. btMultiSapProxy* multiProxy = static_cast<btMultiSapProxy*>(proxy);
  136. multiProxy->m_aabbMin = aabbMin;
  137. multiProxy->m_aabbMax = aabbMax;
  138. // bool fullyContained = false;
  139. // bool alreadyInSimple = false;
  140. struct MyNodeOverlapCallback : public btNodeOverlapCallback
  141. {
  142. btMultiSapBroadphase* m_multiSap;
  143. btMultiSapProxy* m_multiProxy;
  144. btDispatcher* m_dispatcher;
  145. MyNodeOverlapCallback(btMultiSapBroadphase* multiSap,btMultiSapProxy* multiProxy,btDispatcher* dispatcher)
  146. :m_multiSap(multiSap),
  147. m_multiProxy(multiProxy),
  148. m_dispatcher(dispatcher)
  149. {
  150. }
  151. virtual void processNode(int /*nodeSubPart*/, int broadphaseIndex)
  152. {
  153. btBroadphaseInterface* childBroadphase = m_multiSap->getBroadphaseArray()[broadphaseIndex];
  154. int containingBroadphaseIndex = -1;
  155. //already found?
  156. for (int i=0;i<m_multiProxy->m_bridgeProxies.size();i++)
  157. {
  158. if (m_multiProxy->m_bridgeProxies[i]->m_childBroadphase == childBroadphase)
  159. {
  160. containingBroadphaseIndex = i;
  161. break;
  162. }
  163. }
  164. if (containingBroadphaseIndex<0)
  165. {
  166. //add it
  167. btBroadphaseProxy* childProxy = childBroadphase->createProxy(m_multiProxy->m_aabbMin,m_multiProxy->m_aabbMax,m_multiProxy->m_shapeType,m_multiProxy->m_clientObject,m_multiProxy->m_collisionFilterGroup,m_multiProxy->m_collisionFilterMask, m_dispatcher,m_multiProxy);
  168. m_multiSap->addToChildBroadphase(m_multiProxy,childProxy,childBroadphase);
  169. }
  170. }
  171. };
  172. MyNodeOverlapCallback myNodeCallback(this,multiProxy,dispatcher);
  173. if (m_optimizedAabbTree)
  174. m_optimizedAabbTree->reportAabbOverlappingNodex(&myNodeCallback,aabbMin,aabbMax);
  175. int i;
  176. for ( i=0;i<multiProxy->m_bridgeProxies.size();i++)
  177. {
  178. btVector3 worldAabbMin,worldAabbMax;
  179. multiProxy->m_bridgeProxies[i]->m_childBroadphase->getBroadphaseAabb(worldAabbMin,worldAabbMax);
  180. bool overlapsBroadphase = TestAabbAgainstAabb2(worldAabbMin,worldAabbMax,multiProxy->m_aabbMin,multiProxy->m_aabbMax);
  181. if (!overlapsBroadphase)
  182. {
  183. //remove it now
  184. btBridgeProxy* bridgeProxy = multiProxy->m_bridgeProxies[i];
  185. btBroadphaseProxy* childProxy = bridgeProxy->m_childProxy;
  186. bridgeProxy->m_childBroadphase->destroyProxy(childProxy,dispatcher);
  187. multiProxy->m_bridgeProxies.swap( i,multiProxy->m_bridgeProxies.size()-1);
  188. multiProxy->m_bridgeProxies.pop_back();
  189. }
  190. }
  191. /*
  192. if (1)
  193. {
  194. //find broadphase that contain this multiProxy
  195. int numChildBroadphases = getBroadphaseArray().size();
  196. for (int i=0;i<numChildBroadphases;i++)
  197. {
  198. btBroadphaseInterface* childBroadphase = getBroadphaseArray()[i];
  199. btVector3 worldAabbMin,worldAabbMax;
  200. childBroadphase->getBroadphaseAabb(worldAabbMin,worldAabbMax);
  201. bool overlapsBroadphase = TestAabbAgainstAabb2(worldAabbMin,worldAabbMax,multiProxy->m_aabbMin,multiProxy->m_aabbMax);
  202. // fullyContained = fullyContained || boxIsContainedWithinBox(worldAabbMin,worldAabbMax,multiProxy->m_aabbMin,multiProxy->m_aabbMax);
  203. int containingBroadphaseIndex = -1;
  204. //if already contains this
  205. for (int i=0;i<multiProxy->m_bridgeProxies.size();i++)
  206. {
  207. if (multiProxy->m_bridgeProxies[i]->m_childBroadphase == childBroadphase)
  208. {
  209. containingBroadphaseIndex = i;
  210. }
  211. alreadyInSimple = alreadyInSimple || (multiProxy->m_bridgeProxies[i]->m_childBroadphase == m_simpleBroadphase);
  212. }
  213. if (overlapsBroadphase)
  214. {
  215. if (containingBroadphaseIndex<0)
  216. {
  217. btBroadphaseProxy* childProxy = childBroadphase->createProxy(aabbMin,aabbMax,multiProxy->m_shapeType,multiProxy->m_clientObject,multiProxy->m_collisionFilterGroup,multiProxy->m_collisionFilterMask, dispatcher);
  218. childProxy->m_multiSapParentProxy = multiProxy;
  219. addToChildBroadphase(multiProxy,childProxy,childBroadphase);
  220. }
  221. } else
  222. {
  223. if (containingBroadphaseIndex>=0)
  224. {
  225. //remove
  226. btBridgeProxy* bridgeProxy = multiProxy->m_bridgeProxies[containingBroadphaseIndex];
  227. btBroadphaseProxy* childProxy = bridgeProxy->m_childProxy;
  228. bridgeProxy->m_childBroadphase->destroyProxy(childProxy,dispatcher);
  229. multiProxy->m_bridgeProxies.swap( containingBroadphaseIndex,multiProxy->m_bridgeProxies.size()-1);
  230. multiProxy->m_bridgeProxies.pop_back();
  231. }
  232. }
  233. }
  234. ///If we are in no other child broadphase, stick the proxy in the global 'simple' broadphase (brute force)
  235. ///hopefully we don't end up with many entries here (can assert/provide feedback on stats)
  236. if (0)//!multiProxy->m_bridgeProxies.size())
  237. {
  238. ///we don't pass the userPtr but our multisap proxy. We need to patch this, before processing an actual collision
  239. ///this is needed to be able to calculate the aabb overlap
  240. btBroadphaseProxy* childProxy = m_simpleBroadphase->createProxy(aabbMin,aabbMax,multiProxy->m_shapeType,multiProxy->m_clientObject,multiProxy->m_collisionFilterGroup,multiProxy->m_collisionFilterMask, dispatcher);
  241. childProxy->m_multiSapParentProxy = multiProxy;
  242. addToChildBroadphase(multiProxy,childProxy,m_simpleBroadphase);
  243. }
  244. }
  245. if (!multiProxy->m_bridgeProxies.size())
  246. {
  247. ///we don't pass the userPtr but our multisap proxy. We need to patch this, before processing an actual collision
  248. ///this is needed to be able to calculate the aabb overlap
  249. btBroadphaseProxy* childProxy = m_simpleBroadphase->createProxy(aabbMin,aabbMax,multiProxy->m_shapeType,multiProxy->m_clientObject,multiProxy->m_collisionFilterGroup,multiProxy->m_collisionFilterMask, dispatcher);
  250. childProxy->m_multiSapParentProxy = multiProxy;
  251. addToChildBroadphase(multiProxy,childProxy,m_simpleBroadphase);
  252. }
  253. */
  254. //update
  255. for ( i=0;i<multiProxy->m_bridgeProxies.size();i++)
  256. {
  257. btBridgeProxy* bridgeProxyRef = multiProxy->m_bridgeProxies[i];
  258. bridgeProxyRef->m_childBroadphase->setAabb(bridgeProxyRef->m_childProxy,aabbMin,aabbMax,dispatcher);
  259. }
  260. }
  261. bool stopUpdating=false;
  262. class btMultiSapBroadphasePairSortPredicate
  263. {
  264. public:
  265. bool operator() ( const btBroadphasePair& a1, const btBroadphasePair& b1 ) const
  266. {
  267. btMultiSapBroadphase::btMultiSapProxy* aProxy0 = a1.m_pProxy0 ? (btMultiSapBroadphase::btMultiSapProxy*)a1.m_pProxy0->m_multiSapParentProxy : 0;
  268. btMultiSapBroadphase::btMultiSapProxy* aProxy1 = a1.m_pProxy1 ? (btMultiSapBroadphase::btMultiSapProxy*)a1.m_pProxy1->m_multiSapParentProxy : 0;
  269. btMultiSapBroadphase::btMultiSapProxy* bProxy0 = b1.m_pProxy0 ? (btMultiSapBroadphase::btMultiSapProxy*)b1.m_pProxy0->m_multiSapParentProxy : 0;
  270. btMultiSapBroadphase::btMultiSapProxy* bProxy1 = b1.m_pProxy1 ? (btMultiSapBroadphase::btMultiSapProxy*)b1.m_pProxy1->m_multiSapParentProxy : 0;
  271. return aProxy0 > bProxy0 ||
  272. (aProxy0 == bProxy0 && aProxy1 > bProxy1) ||
  273. (aProxy0 == bProxy0 && aProxy1 == bProxy1 && a1.m_algorithm > b1.m_algorithm);
  274. }
  275. };
  276. ///calculateOverlappingPairs is optional: incremental algorithms (sweep and prune) might do it during the set aabb
  277. void btMultiSapBroadphase::calculateOverlappingPairs(btDispatcher* dispatcher)
  278. {
  279. // m_simpleBroadphase->calculateOverlappingPairs(dispatcher);
  280. if (!stopUpdating && getOverlappingPairCache()->hasDeferredRemoval())
  281. {
  282. btBroadphasePairArray& overlappingPairArray = getOverlappingPairCache()->getOverlappingPairArray();
  283. // quicksort(overlappingPairArray,0,overlappingPairArray.size());
  284. overlappingPairArray.quickSort(btMultiSapBroadphasePairSortPredicate());
  285. //perform a sort, to find duplicates and to sort 'invalid' pairs to the end
  286. // overlappingPairArray.heapSort(btMultiSapBroadphasePairSortPredicate());
  287. overlappingPairArray.resize(overlappingPairArray.size() - m_invalidPair);
  288. m_invalidPair = 0;
  289. int i;
  290. btBroadphasePair previousPair;
  291. previousPair.m_pProxy0 = 0;
  292. previousPair.m_pProxy1 = 0;
  293. previousPair.m_algorithm = 0;
  294. for (i=0;i<overlappingPairArray.size();i++)
  295. {
  296. btBroadphasePair& pair = overlappingPairArray[i];
  297. btMultiSapProxy* aProxy0 = pair.m_pProxy0 ? (btMultiSapProxy*)pair.m_pProxy0->m_multiSapParentProxy : 0;
  298. btMultiSapProxy* aProxy1 = pair.m_pProxy1 ? (btMultiSapProxy*)pair.m_pProxy1->m_multiSapParentProxy : 0;
  299. btMultiSapProxy* bProxy0 = previousPair.m_pProxy0 ? (btMultiSapProxy*)previousPair.m_pProxy0->m_multiSapParentProxy : 0;
  300. btMultiSapProxy* bProxy1 = previousPair.m_pProxy1 ? (btMultiSapProxy*)previousPair.m_pProxy1->m_multiSapParentProxy : 0;
  301. bool isDuplicate = (aProxy0 == bProxy0) && (aProxy1 == bProxy1);
  302. previousPair = pair;
  303. bool needsRemoval = false;
  304. if (!isDuplicate)
  305. {
  306. bool hasOverlap = testAabbOverlap(pair.m_pProxy0,pair.m_pProxy1);
  307. if (hasOverlap)
  308. {
  309. needsRemoval = false;//callback->processOverlap(pair);
  310. } else
  311. {
  312. needsRemoval = true;
  313. }
  314. } else
  315. {
  316. //remove duplicate
  317. needsRemoval = true;
  318. //should have no algorithm
  319. btAssert(!pair.m_algorithm);
  320. }
  321. if (needsRemoval)
  322. {
  323. getOverlappingPairCache()->cleanOverlappingPair(pair,dispatcher);
  324. // m_overlappingPairArray.swap(i,m_overlappingPairArray.size()-1);
  325. // m_overlappingPairArray.pop_back();
  326. pair.m_pProxy0 = 0;
  327. pair.m_pProxy1 = 0;
  328. m_invalidPair++;
  329. gOverlappingPairs--;
  330. }
  331. }
  332. ///if you don't like to skip the invalid pairs in the array, execute following code:
  333. #define CLEAN_INVALID_PAIRS 1
  334. #ifdef CLEAN_INVALID_PAIRS
  335. //perform a sort, to sort 'invalid' pairs to the end
  336. //overlappingPairArray.heapSort(btMultiSapBroadphasePairSortPredicate());
  337. overlappingPairArray.quickSort(btMultiSapBroadphasePairSortPredicate());
  338. overlappingPairArray.resize(overlappingPairArray.size() - m_invalidPair);
  339. m_invalidPair = 0;
  340. #endif//CLEAN_INVALID_PAIRS
  341. //printf("overlappingPairArray.size()=%d\n",overlappingPairArray.size());
  342. }
  343. }
  344. bool btMultiSapBroadphase::testAabbOverlap(btBroadphaseProxy* childProxy0,btBroadphaseProxy* childProxy1)
  345. {
  346. btMultiSapProxy* multiSapProxy0 = (btMultiSapProxy*)childProxy0->m_multiSapParentProxy;
  347. btMultiSapProxy* multiSapProxy1 = (btMultiSapProxy*)childProxy1->m_multiSapParentProxy;
  348. return TestAabbAgainstAabb2(multiSapProxy0->m_aabbMin,multiSapProxy0->m_aabbMax,
  349. multiSapProxy1->m_aabbMin,multiSapProxy1->m_aabbMax);
  350. }
  351. void btMultiSapBroadphase::printStats()
  352. {
  353. /* printf("---------------------------------\n");
  354. printf("btMultiSapBroadphase.h\n");
  355. printf("numHandles = %d\n",m_multiSapProxies.size());
  356. //find broadphase that contain this multiProxy
  357. int numChildBroadphases = getBroadphaseArray().size();
  358. for (int i=0;i<numChildBroadphases;i++)
  359. {
  360. btBroadphaseInterface* childBroadphase = getBroadphaseArray()[i];
  361. childBroadphase->printStats();
  362. }
  363. */
  364. }
  365. void btMultiSapBroadphase::resetPool(btDispatcher* dispatcher)
  366. {
  367. // not yet
  368. }