btAxisSweep3.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. //Bullet Continuous Collision Detection and Physics Library
  2. //Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  3. //
  4. // btAxisSweep3.h
  5. //
  6. // Copyright (c) 2006 Simon Hobbs
  7. //
  8. // 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.
  9. //
  10. // 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:
  11. //
  12. // 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.
  13. //
  14. // 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  15. //
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. #ifndef BT_AXIS_SWEEP_3_H
  18. #define BT_AXIS_SWEEP_3_H
  19. #include "bullet/LinearMath/btVector3.h"
  20. #include "btOverlappingPairCache.h"
  21. #include "btBroadphaseInterface.h"
  22. #include "btBroadphaseProxy.h"
  23. #include "btOverlappingPairCallback.h"
  24. #include "btDbvtBroadphase.h"
  25. //#define DEBUG_BROADPHASE 1
  26. #define USE_OVERLAP_TEST_ON_REMOVES 1
  27. /// The internal templace class btAxisSweep3Internal implements the sweep and prune broadphase.
  28. /// It uses quantized integers to represent the begin and end points for each of the 3 axis.
  29. /// Dont use this class directly, use btAxisSweep3 or bt32BitAxisSweep3 instead.
  30. template <typename BP_FP_INT_TYPE>
  31. class btAxisSweep3Internal : public btBroadphaseInterface
  32. {
  33. protected:
  34. BP_FP_INT_TYPE m_bpHandleMask;
  35. BP_FP_INT_TYPE m_handleSentinel;
  36. public:
  37. BT_DECLARE_ALIGNED_ALLOCATOR();
  38. class Edge
  39. {
  40. public:
  41. BP_FP_INT_TYPE m_pos; // low bit is min/max
  42. BP_FP_INT_TYPE m_handle;
  43. BP_FP_INT_TYPE IsMax() const {return static_cast<BP_FP_INT_TYPE>(m_pos & 1);}
  44. };
  45. public:
  46. class Handle : public btBroadphaseProxy
  47. {
  48. public:
  49. BT_DECLARE_ALIGNED_ALLOCATOR();
  50. // indexes into the edge arrays
  51. BP_FP_INT_TYPE m_minEdges[3], m_maxEdges[3]; // 6 * 2 = 12
  52. // BP_FP_INT_TYPE m_uniqueId;
  53. btBroadphaseProxy* m_dbvtProxy;//for faster raycast
  54. //void* m_pOwner; this is now in btBroadphaseProxy.m_clientObject
  55. SIMD_FORCE_INLINE void SetNextFree(BP_FP_INT_TYPE next) {m_minEdges[0] = next;}
  56. SIMD_FORCE_INLINE BP_FP_INT_TYPE GetNextFree() const {return m_minEdges[0];}
  57. }; // 24 bytes + 24 for Edge structures = 44 bytes total per entry
  58. protected:
  59. btVector3 m_worldAabbMin; // overall system bounds
  60. btVector3 m_worldAabbMax; // overall system bounds
  61. btVector3 m_quantize; // scaling factor for quantization
  62. BP_FP_INT_TYPE m_numHandles; // number of active handles
  63. BP_FP_INT_TYPE m_maxHandles; // max number of handles
  64. Handle* m_pHandles; // handles pool
  65. BP_FP_INT_TYPE m_firstFreeHandle; // free handles list
  66. Edge* m_pEdges[3]; // edge arrays for the 3 axes (each array has m_maxHandles * 2 + 2 sentinel entries)
  67. void* m_pEdgesRawPtr[3];
  68. btOverlappingPairCache* m_pairCache;
  69. ///btOverlappingPairCallback is an additional optional user callback for adding/removing overlapping pairs, similar interface to btOverlappingPairCache.
  70. btOverlappingPairCallback* m_userPairCallback;
  71. bool m_ownsPairCache;
  72. int m_invalidPair;
  73. ///additional dynamic aabb structure, used to accelerate ray cast queries.
  74. ///can be disabled using a optional argument in the constructor
  75. btDbvtBroadphase* m_raycastAccelerator;
  76. btOverlappingPairCache* m_nullPairCache;
  77. // allocation/deallocation
  78. BP_FP_INT_TYPE allocHandle();
  79. void freeHandle(BP_FP_INT_TYPE handle);
  80. bool testOverlap2D(const Handle* pHandleA, const Handle* pHandleB,int axis0,int axis1);
  81. #ifdef DEBUG_BROADPHASE
  82. void debugPrintAxis(int axis,bool checkCardinality=true);
  83. #endif //DEBUG_BROADPHASE
  84. //Overlap* AddOverlap(BP_FP_INT_TYPE handleA, BP_FP_INT_TYPE handleB);
  85. //void RemoveOverlap(BP_FP_INT_TYPE handleA, BP_FP_INT_TYPE handleB);
  86. void sortMinDown(int axis, BP_FP_INT_TYPE edge, btDispatcher* dispatcher, bool updateOverlaps );
  87. void sortMinUp(int axis, BP_FP_INT_TYPE edge, btDispatcher* dispatcher, bool updateOverlaps );
  88. void sortMaxDown(int axis, BP_FP_INT_TYPE edge, btDispatcher* dispatcher, bool updateOverlaps );
  89. void sortMaxUp(int axis, BP_FP_INT_TYPE edge, btDispatcher* dispatcher, bool updateOverlaps );
  90. public:
  91. btAxisSweep3Internal(const btVector3& worldAabbMin,const btVector3& worldAabbMax, BP_FP_INT_TYPE handleMask, BP_FP_INT_TYPE handleSentinel, BP_FP_INT_TYPE maxHandles = 16384, btOverlappingPairCache* pairCache=0,bool disableRaycastAccelerator = false);
  92. virtual ~btAxisSweep3Internal();
  93. BP_FP_INT_TYPE getNumHandles() const
  94. {
  95. return m_numHandles;
  96. }
  97. virtual void calculateOverlappingPairs(btDispatcher* dispatcher);
  98. BP_FP_INT_TYPE addHandle(const btVector3& aabbMin,const btVector3& aabbMax, void* pOwner,short int collisionFilterGroup,short int collisionFilterMask,btDispatcher* dispatcher,void* multiSapProxy);
  99. void removeHandle(BP_FP_INT_TYPE handle,btDispatcher* dispatcher);
  100. void updateHandle(BP_FP_INT_TYPE handle, const btVector3& aabbMin,const btVector3& aabbMax,btDispatcher* dispatcher);
  101. SIMD_FORCE_INLINE Handle* getHandle(BP_FP_INT_TYPE index) const {return m_pHandles + index;}
  102. virtual void resetPool(btDispatcher* dispatcher);
  103. void processAllOverlappingPairs(btOverlapCallback* callback);
  104. //Broadphase Interface
  105. virtual btBroadphaseProxy* createProxy( const btVector3& aabbMin, const btVector3& aabbMax,int shapeType,void* userPtr ,short int collisionFilterGroup,short int collisionFilterMask,btDispatcher* dispatcher,void* multiSapProxy);
  106. virtual void destroyProxy(btBroadphaseProxy* proxy,btDispatcher* dispatcher);
  107. virtual void setAabb(btBroadphaseProxy* proxy,const btVector3& aabbMin,const btVector3& aabbMax,btDispatcher* dispatcher);
  108. virtual void getAabb(btBroadphaseProxy* proxy,btVector3& aabbMin, btVector3& aabbMax ) const;
  109. virtual void rayTest(const btVector3& rayFrom,const btVector3& rayTo, btBroadphaseRayCallback& rayCallback, const btVector3& aabbMin=btVector3(0,0,0), const btVector3& aabbMax = btVector3(0,0,0));
  110. virtual void aabbTest(const btVector3& aabbMin, const btVector3& aabbMax, btBroadphaseAabbCallback& callback);
  111. void quantize(BP_FP_INT_TYPE* out, const btVector3& point, int isMax) const;
  112. ///unQuantize should be conservative: aabbMin/aabbMax should be larger then 'getAabb' result
  113. void unQuantize(btBroadphaseProxy* proxy,btVector3& aabbMin, btVector3& aabbMax ) const;
  114. bool testAabbOverlap(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1);
  115. btOverlappingPairCache* getOverlappingPairCache()
  116. {
  117. return m_pairCache;
  118. }
  119. const btOverlappingPairCache* getOverlappingPairCache() const
  120. {
  121. return m_pairCache;
  122. }
  123. void setOverlappingPairUserCallback(btOverlappingPairCallback* pairCallback)
  124. {
  125. m_userPairCallback = pairCallback;
  126. }
  127. const btOverlappingPairCallback* getOverlappingPairUserCallback() const
  128. {
  129. return m_userPairCallback;
  130. }
  131. ///getAabb returns the axis aligned bounding box in the 'global' coordinate frame
  132. ///will add some transform later
  133. virtual void getBroadphaseAabb(btVector3& aabbMin,btVector3& aabbMax) const
  134. {
  135. aabbMin = m_worldAabbMin;
  136. aabbMax = m_worldAabbMax;
  137. }
  138. virtual void printStats()
  139. {
  140. /* printf("btAxisSweep3.h\n");
  141. printf("numHandles = %d, maxHandles = %d\n",m_numHandles,m_maxHandles);
  142. printf("aabbMin=%f,%f,%f,aabbMax=%f,%f,%f\n",m_worldAabbMin.getX(),m_worldAabbMin.getY(),m_worldAabbMin.getZ(),
  143. m_worldAabbMax.getX(),m_worldAabbMax.getY(),m_worldAabbMax.getZ());
  144. */
  145. }
  146. };
  147. ////////////////////////////////////////////////////////////////////
  148. #ifdef DEBUG_BROADPHASE
  149. #include <stdio.h>
  150. template <typename BP_FP_INT_TYPE>
  151. void btAxisSweep3<BP_FP_INT_TYPE>::debugPrintAxis(int axis, bool checkCardinality)
  152. {
  153. int numEdges = m_pHandles[0].m_maxEdges[axis];
  154. printf("SAP Axis %d, numEdges=%d\n",axis,numEdges);
  155. int i;
  156. for (i=0;i<numEdges+1;i++)
  157. {
  158. Edge* pEdge = m_pEdges[axis] + i;
  159. Handle* pHandlePrev = getHandle(pEdge->m_handle);
  160. int handleIndex = pEdge->IsMax()? pHandlePrev->m_maxEdges[axis] : pHandlePrev->m_minEdges[axis];
  161. char beginOrEnd;
  162. beginOrEnd=pEdge->IsMax()?'E':'B';
  163. printf(" [%c,h=%d,p=%x,i=%d]\n",beginOrEnd,pEdge->m_handle,pEdge->m_pos,handleIndex);
  164. }
  165. if (checkCardinality)
  166. btAssert(numEdges == m_numHandles*2+1);
  167. }
  168. #endif //DEBUG_BROADPHASE
  169. template <typename BP_FP_INT_TYPE>
  170. btBroadphaseProxy* btAxisSweep3Internal<BP_FP_INT_TYPE>::createProxy( const btVector3& aabbMin, const btVector3& aabbMax,int shapeType,void* userPtr,short int collisionFilterGroup,short int collisionFilterMask,btDispatcher* dispatcher,void* multiSapProxy)
  171. {
  172. (void)shapeType;
  173. BP_FP_INT_TYPE handleId = addHandle(aabbMin,aabbMax, userPtr,collisionFilterGroup,collisionFilterMask,dispatcher,multiSapProxy);
  174. Handle* handle = getHandle(handleId);
  175. if (m_raycastAccelerator)
  176. {
  177. btBroadphaseProxy* rayProxy = m_raycastAccelerator->createProxy(aabbMin,aabbMax,shapeType,userPtr,collisionFilterGroup,collisionFilterMask,dispatcher,0);
  178. handle->m_dbvtProxy = rayProxy;
  179. }
  180. return handle;
  181. }
  182. template <typename BP_FP_INT_TYPE>
  183. void btAxisSweep3Internal<BP_FP_INT_TYPE>::destroyProxy(btBroadphaseProxy* proxy,btDispatcher* dispatcher)
  184. {
  185. Handle* handle = static_cast<Handle*>(proxy);
  186. if (m_raycastAccelerator)
  187. m_raycastAccelerator->destroyProxy(handle->m_dbvtProxy,dispatcher);
  188. removeHandle(static_cast<BP_FP_INT_TYPE>(handle->m_uniqueId), dispatcher);
  189. }
  190. template <typename BP_FP_INT_TYPE>
  191. void btAxisSweep3Internal<BP_FP_INT_TYPE>::setAabb(btBroadphaseProxy* proxy,const btVector3& aabbMin,const btVector3& aabbMax,btDispatcher* dispatcher)
  192. {
  193. Handle* handle = static_cast<Handle*>(proxy);
  194. handle->m_aabbMin = aabbMin;
  195. handle->m_aabbMax = aabbMax;
  196. updateHandle(static_cast<BP_FP_INT_TYPE>(handle->m_uniqueId), aabbMin, aabbMax,dispatcher);
  197. if (m_raycastAccelerator)
  198. m_raycastAccelerator->setAabb(handle->m_dbvtProxy,aabbMin,aabbMax,dispatcher);
  199. }
  200. template <typename BP_FP_INT_TYPE>
  201. void btAxisSweep3Internal<BP_FP_INT_TYPE>::rayTest(const btVector3& rayFrom,const btVector3& rayTo, btBroadphaseRayCallback& rayCallback,const btVector3& aabbMin,const btVector3& aabbMax)
  202. {
  203. if (m_raycastAccelerator)
  204. {
  205. m_raycastAccelerator->rayTest(rayFrom,rayTo,rayCallback,aabbMin,aabbMax);
  206. } else
  207. {
  208. //choose axis?
  209. BP_FP_INT_TYPE axis = 0;
  210. //for each proxy
  211. for (BP_FP_INT_TYPE i=1;i<m_numHandles*2+1;i++)
  212. {
  213. if (m_pEdges[axis][i].IsMax())
  214. {
  215. rayCallback.process(getHandle(m_pEdges[axis][i].m_handle));
  216. }
  217. }
  218. }
  219. }
  220. template <typename BP_FP_INT_TYPE>
  221. void btAxisSweep3Internal<BP_FP_INT_TYPE>::aabbTest(const btVector3& aabbMin, const btVector3& aabbMax, btBroadphaseAabbCallback& callback)
  222. {
  223. if (m_raycastAccelerator)
  224. {
  225. m_raycastAccelerator->aabbTest(aabbMin,aabbMax,callback);
  226. } else
  227. {
  228. //choose axis?
  229. BP_FP_INT_TYPE axis = 0;
  230. //for each proxy
  231. for (BP_FP_INT_TYPE i=1;i<m_numHandles*2+1;i++)
  232. {
  233. if (m_pEdges[axis][i].IsMax())
  234. {
  235. Handle* handle = getHandle(m_pEdges[axis][i].m_handle);
  236. if (TestAabbAgainstAabb2(aabbMin,aabbMax,handle->m_aabbMin,handle->m_aabbMax))
  237. {
  238. callback.process(handle);
  239. }
  240. }
  241. }
  242. }
  243. }
  244. template <typename BP_FP_INT_TYPE>
  245. void btAxisSweep3Internal<BP_FP_INT_TYPE>::getAabb(btBroadphaseProxy* proxy,btVector3& aabbMin, btVector3& aabbMax ) const
  246. {
  247. Handle* pHandle = static_cast<Handle*>(proxy);
  248. aabbMin = pHandle->m_aabbMin;
  249. aabbMax = pHandle->m_aabbMax;
  250. }
  251. template <typename BP_FP_INT_TYPE>
  252. void btAxisSweep3Internal<BP_FP_INT_TYPE>::unQuantize(btBroadphaseProxy* proxy,btVector3& aabbMin, btVector3& aabbMax ) const
  253. {
  254. Handle* pHandle = static_cast<Handle*>(proxy);
  255. unsigned short vecInMin[3];
  256. unsigned short vecInMax[3];
  257. vecInMin[0] = m_pEdges[0][pHandle->m_minEdges[0]].m_pos ;
  258. vecInMax[0] = m_pEdges[0][pHandle->m_maxEdges[0]].m_pos +1 ;
  259. vecInMin[1] = m_pEdges[1][pHandle->m_minEdges[1]].m_pos ;
  260. vecInMax[1] = m_pEdges[1][pHandle->m_maxEdges[1]].m_pos +1 ;
  261. vecInMin[2] = m_pEdges[2][pHandle->m_minEdges[2]].m_pos ;
  262. vecInMax[2] = m_pEdges[2][pHandle->m_maxEdges[2]].m_pos +1 ;
  263. aabbMin.setValue((btScalar)(vecInMin[0]) / (m_quantize.getX()),(btScalar)(vecInMin[1]) / (m_quantize.getY()),(btScalar)(vecInMin[2]) / (m_quantize.getZ()));
  264. aabbMin += m_worldAabbMin;
  265. aabbMax.setValue((btScalar)(vecInMax[0]) / (m_quantize.getX()),(btScalar)(vecInMax[1]) / (m_quantize.getY()),(btScalar)(vecInMax[2]) / (m_quantize.getZ()));
  266. aabbMax += m_worldAabbMin;
  267. }
  268. template <typename BP_FP_INT_TYPE>
  269. btAxisSweep3Internal<BP_FP_INT_TYPE>::btAxisSweep3Internal(const btVector3& worldAabbMin,const btVector3& worldAabbMax, BP_FP_INT_TYPE handleMask, BP_FP_INT_TYPE handleSentinel,BP_FP_INT_TYPE userMaxHandles, btOverlappingPairCache* pairCache , bool disableRaycastAccelerator)
  270. :m_bpHandleMask(handleMask),
  271. m_handleSentinel(handleSentinel),
  272. m_pairCache(pairCache),
  273. m_userPairCallback(0),
  274. m_ownsPairCache(false),
  275. m_invalidPair(0),
  276. m_raycastAccelerator(0)
  277. {
  278. BP_FP_INT_TYPE maxHandles = static_cast<BP_FP_INT_TYPE>(userMaxHandles+1);//need to add one sentinel handle
  279. if (!m_pairCache)
  280. {
  281. void* ptr = btAlignedAlloc(sizeof(btHashedOverlappingPairCache),16);
  282. m_pairCache = new(ptr) btHashedOverlappingPairCache();
  283. m_ownsPairCache = true;
  284. }
  285. if (!disableRaycastAccelerator)
  286. {
  287. m_nullPairCache = new (btAlignedAlloc(sizeof(btNullPairCache),16)) btNullPairCache();
  288. m_raycastAccelerator = new (btAlignedAlloc(sizeof(btDbvtBroadphase),16)) btDbvtBroadphase(m_nullPairCache);//m_pairCache);
  289. m_raycastAccelerator->m_deferedcollide = true;//don't add/remove pairs
  290. }
  291. //btAssert(bounds.HasVolume());
  292. // init bounds
  293. m_worldAabbMin = worldAabbMin;
  294. m_worldAabbMax = worldAabbMax;
  295. btVector3 aabbSize = m_worldAabbMax - m_worldAabbMin;
  296. BP_FP_INT_TYPE maxInt = m_handleSentinel;
  297. m_quantize = btVector3(btScalar(maxInt),btScalar(maxInt),btScalar(maxInt)) / aabbSize;
  298. // allocate handles buffer, using btAlignedAlloc, and put all handles on free list
  299. m_pHandles = new Handle[maxHandles];
  300. m_maxHandles = maxHandles;
  301. m_numHandles = 0;
  302. // handle 0 is reserved as the null index, and is also used as the sentinel
  303. m_firstFreeHandle = 1;
  304. {
  305. for (BP_FP_INT_TYPE i = m_firstFreeHandle; i < maxHandles; i++)
  306. m_pHandles[i].SetNextFree(static_cast<BP_FP_INT_TYPE>(i + 1));
  307. m_pHandles[maxHandles - 1].SetNextFree(0);
  308. }
  309. {
  310. // allocate edge buffers
  311. for (int i = 0; i < 3; i++)
  312. {
  313. m_pEdgesRawPtr[i] = btAlignedAlloc(sizeof(Edge)*maxHandles*2,16);
  314. m_pEdges[i] = new(m_pEdgesRawPtr[i]) Edge[maxHandles * 2];
  315. }
  316. }
  317. //removed overlap management
  318. // make boundary sentinels
  319. m_pHandles[0].m_clientObject = 0;
  320. for (int axis = 0; axis < 3; axis++)
  321. {
  322. m_pHandles[0].m_minEdges[axis] = 0;
  323. m_pHandles[0].m_maxEdges[axis] = 1;
  324. m_pEdges[axis][0].m_pos = 0;
  325. m_pEdges[axis][0].m_handle = 0;
  326. m_pEdges[axis][1].m_pos = m_handleSentinel;
  327. m_pEdges[axis][1].m_handle = 0;
  328. #ifdef DEBUG_BROADPHASE
  329. debugPrintAxis(axis);
  330. #endif //DEBUG_BROADPHASE
  331. }
  332. }
  333. template <typename BP_FP_INT_TYPE>
  334. btAxisSweep3Internal<BP_FP_INT_TYPE>::~btAxisSweep3Internal()
  335. {
  336. if (m_raycastAccelerator)
  337. {
  338. m_nullPairCache->~btOverlappingPairCache();
  339. btAlignedFree(m_nullPairCache);
  340. m_raycastAccelerator->~btDbvtBroadphase();
  341. btAlignedFree (m_raycastAccelerator);
  342. }
  343. for (int i = 2; i >= 0; i--)
  344. {
  345. btAlignedFree(m_pEdgesRawPtr[i]);
  346. }
  347. delete [] m_pHandles;
  348. if (m_ownsPairCache)
  349. {
  350. m_pairCache->~btOverlappingPairCache();
  351. btAlignedFree(m_pairCache);
  352. }
  353. }
  354. template <typename BP_FP_INT_TYPE>
  355. void btAxisSweep3Internal<BP_FP_INT_TYPE>::quantize(BP_FP_INT_TYPE* out, const btVector3& point, int isMax) const
  356. {
  357. #ifdef OLD_CLAMPING_METHOD
  358. ///problem with this clamping method is that the floating point during quantization might still go outside the range [(0|isMax) .. (m_handleSentinel&m_bpHandleMask]|isMax]
  359. ///see http://code.google.com/p/bullet/issues/detail?id=87
  360. btVector3 clampedPoint(point);
  361. clampedPoint.setMax(m_worldAabbMin);
  362. clampedPoint.setMin(m_worldAabbMax);
  363. btVector3 v = (clampedPoint - m_worldAabbMin) * m_quantize;
  364. out[0] = (BP_FP_INT_TYPE)(((BP_FP_INT_TYPE)v.getX() & m_bpHandleMask) | isMax);
  365. out[1] = (BP_FP_INT_TYPE)(((BP_FP_INT_TYPE)v.getY() & m_bpHandleMask) | isMax);
  366. out[2] = (BP_FP_INT_TYPE)(((BP_FP_INT_TYPE)v.getZ() & m_bpHandleMask) | isMax);
  367. #else
  368. btVector3 v = (point - m_worldAabbMin) * m_quantize;
  369. out[0]=(v[0]<=0)?(BP_FP_INT_TYPE)isMax:(v[0]>=m_handleSentinel)?(BP_FP_INT_TYPE)((m_handleSentinel&m_bpHandleMask)|isMax):(BP_FP_INT_TYPE)(((BP_FP_INT_TYPE)v[0]&m_bpHandleMask)|isMax);
  370. out[1]=(v[1]<=0)?(BP_FP_INT_TYPE)isMax:(v[1]>=m_handleSentinel)?(BP_FP_INT_TYPE)((m_handleSentinel&m_bpHandleMask)|isMax):(BP_FP_INT_TYPE)(((BP_FP_INT_TYPE)v[1]&m_bpHandleMask)|isMax);
  371. out[2]=(v[2]<=0)?(BP_FP_INT_TYPE)isMax:(v[2]>=m_handleSentinel)?(BP_FP_INT_TYPE)((m_handleSentinel&m_bpHandleMask)|isMax):(BP_FP_INT_TYPE)(((BP_FP_INT_TYPE)v[2]&m_bpHandleMask)|isMax);
  372. #endif //OLD_CLAMPING_METHOD
  373. }
  374. template <typename BP_FP_INT_TYPE>
  375. BP_FP_INT_TYPE btAxisSweep3Internal<BP_FP_INT_TYPE>::allocHandle()
  376. {
  377. btAssert(m_firstFreeHandle);
  378. BP_FP_INT_TYPE handle = m_firstFreeHandle;
  379. m_firstFreeHandle = getHandle(handle)->GetNextFree();
  380. m_numHandles++;
  381. return handle;
  382. }
  383. template <typename BP_FP_INT_TYPE>
  384. void btAxisSweep3Internal<BP_FP_INT_TYPE>::freeHandle(BP_FP_INT_TYPE handle)
  385. {
  386. btAssert(handle > 0 && handle < m_maxHandles);
  387. getHandle(handle)->SetNextFree(m_firstFreeHandle);
  388. m_firstFreeHandle = handle;
  389. m_numHandles--;
  390. }
  391. template <typename BP_FP_INT_TYPE>
  392. BP_FP_INT_TYPE btAxisSweep3Internal<BP_FP_INT_TYPE>::addHandle(const btVector3& aabbMin,const btVector3& aabbMax, void* pOwner,short int collisionFilterGroup,short int collisionFilterMask,btDispatcher* dispatcher,void* multiSapProxy)
  393. {
  394. // quantize the bounds
  395. BP_FP_INT_TYPE min[3], max[3];
  396. quantize(min, aabbMin, 0);
  397. quantize(max, aabbMax, 1);
  398. // allocate a handle
  399. BP_FP_INT_TYPE handle = allocHandle();
  400. Handle* pHandle = getHandle(handle);
  401. pHandle->m_uniqueId = static_cast<int>(handle);
  402. //pHandle->m_pOverlaps = 0;
  403. pHandle->m_clientObject = pOwner;
  404. pHandle->m_collisionFilterGroup = collisionFilterGroup;
  405. pHandle->m_collisionFilterMask = collisionFilterMask;
  406. pHandle->m_multiSapParentProxy = multiSapProxy;
  407. // compute current limit of edge arrays
  408. BP_FP_INT_TYPE limit = static_cast<BP_FP_INT_TYPE>(m_numHandles * 2);
  409. // insert new edges just inside the max boundary edge
  410. for (BP_FP_INT_TYPE axis = 0; axis < 3; axis++)
  411. {
  412. m_pHandles[0].m_maxEdges[axis] += 2;
  413. m_pEdges[axis][limit + 1] = m_pEdges[axis][limit - 1];
  414. m_pEdges[axis][limit - 1].m_pos = min[axis];
  415. m_pEdges[axis][limit - 1].m_handle = handle;
  416. m_pEdges[axis][limit].m_pos = max[axis];
  417. m_pEdges[axis][limit].m_handle = handle;
  418. pHandle->m_minEdges[axis] = static_cast<BP_FP_INT_TYPE>(limit - 1);
  419. pHandle->m_maxEdges[axis] = limit;
  420. }
  421. // now sort the new edges to their correct position
  422. sortMinDown(0, pHandle->m_minEdges[0], dispatcher,false);
  423. sortMaxDown(0, pHandle->m_maxEdges[0], dispatcher,false);
  424. sortMinDown(1, pHandle->m_minEdges[1], dispatcher,false);
  425. sortMaxDown(1, pHandle->m_maxEdges[1], dispatcher,false);
  426. sortMinDown(2, pHandle->m_minEdges[2], dispatcher,true);
  427. sortMaxDown(2, pHandle->m_maxEdges[2], dispatcher,true);
  428. return handle;
  429. }
  430. template <typename BP_FP_INT_TYPE>
  431. void btAxisSweep3Internal<BP_FP_INT_TYPE>::removeHandle(BP_FP_INT_TYPE handle,btDispatcher* dispatcher)
  432. {
  433. Handle* pHandle = getHandle(handle);
  434. //explicitly remove the pairs containing the proxy
  435. //we could do it also in the sortMinUp (passing true)
  436. ///@todo: compare performance
  437. if (!m_pairCache->hasDeferredRemoval())
  438. {
  439. m_pairCache->removeOverlappingPairsContainingProxy(pHandle,dispatcher);
  440. }
  441. // compute current limit of edge arrays
  442. int limit = static_cast<int>(m_numHandles * 2);
  443. int axis;
  444. for (axis = 0;axis<3;axis++)
  445. {
  446. m_pHandles[0].m_maxEdges[axis] -= 2;
  447. }
  448. // remove the edges by sorting them up to the end of the list
  449. for ( axis = 0; axis < 3; axis++)
  450. {
  451. Edge* pEdges = m_pEdges[axis];
  452. BP_FP_INT_TYPE max = pHandle->m_maxEdges[axis];
  453. pEdges[max].m_pos = m_handleSentinel;
  454. sortMaxUp(axis,max,dispatcher,false);
  455. BP_FP_INT_TYPE i = pHandle->m_minEdges[axis];
  456. pEdges[i].m_pos = m_handleSentinel;
  457. sortMinUp(axis,i,dispatcher,false);
  458. pEdges[limit-1].m_handle = 0;
  459. pEdges[limit-1].m_pos = m_handleSentinel;
  460. #ifdef DEBUG_BROADPHASE
  461. debugPrintAxis(axis,false);
  462. #endif //DEBUG_BROADPHASE
  463. }
  464. // free the handle
  465. freeHandle(handle);
  466. }
  467. template <typename BP_FP_INT_TYPE>
  468. void btAxisSweep3Internal<BP_FP_INT_TYPE>::resetPool(btDispatcher* /*dispatcher*/)
  469. {
  470. if (m_numHandles == 0)
  471. {
  472. m_firstFreeHandle = 1;
  473. {
  474. for (BP_FP_INT_TYPE i = m_firstFreeHandle; i < m_maxHandles; i++)
  475. m_pHandles[i].SetNextFree(static_cast<BP_FP_INT_TYPE>(i + 1));
  476. m_pHandles[m_maxHandles - 1].SetNextFree(0);
  477. }
  478. }
  479. }
  480. extern int gOverlappingPairs;
  481. //#include <stdio.h>
  482. template <typename BP_FP_INT_TYPE>
  483. void btAxisSweep3Internal<BP_FP_INT_TYPE>::calculateOverlappingPairs(btDispatcher* dispatcher)
  484. {
  485. if (m_pairCache->hasDeferredRemoval())
  486. {
  487. btBroadphasePairArray& overlappingPairArray = m_pairCache->getOverlappingPairArray();
  488. //perform a sort, to find duplicates and to sort 'invalid' pairs to the end
  489. overlappingPairArray.quickSort(btBroadphasePairSortPredicate());
  490. overlappingPairArray.resize(overlappingPairArray.size() - m_invalidPair);
  491. m_invalidPair = 0;
  492. int i;
  493. btBroadphasePair previousPair;
  494. previousPair.m_pProxy0 = 0;
  495. previousPair.m_pProxy1 = 0;
  496. previousPair.m_algorithm = 0;
  497. for (i=0;i<overlappingPairArray.size();i++)
  498. {
  499. btBroadphasePair& pair = overlappingPairArray[i];
  500. bool isDuplicate = (pair == previousPair);
  501. previousPair = pair;
  502. bool needsRemoval = false;
  503. if (!isDuplicate)
  504. {
  505. ///important to use an AABB test that is consistent with the broadphase
  506. bool hasOverlap = testAabbOverlap(pair.m_pProxy0,pair.m_pProxy1);
  507. if (hasOverlap)
  508. {
  509. needsRemoval = false;//callback->processOverlap(pair);
  510. } else
  511. {
  512. needsRemoval = true;
  513. }
  514. } else
  515. {
  516. //remove duplicate
  517. needsRemoval = true;
  518. //should have no algorithm
  519. btAssert(!pair.m_algorithm);
  520. }
  521. if (needsRemoval)
  522. {
  523. m_pairCache->cleanOverlappingPair(pair,dispatcher);
  524. // m_overlappingPairArray.swap(i,m_overlappingPairArray.size()-1);
  525. // m_overlappingPairArray.pop_back();
  526. pair.m_pProxy0 = 0;
  527. pair.m_pProxy1 = 0;
  528. m_invalidPair++;
  529. gOverlappingPairs--;
  530. }
  531. }
  532. ///if you don't like to skip the invalid pairs in the array, execute following code:
  533. #define CLEAN_INVALID_PAIRS 1
  534. #ifdef CLEAN_INVALID_PAIRS
  535. //perform a sort, to sort 'invalid' pairs to the end
  536. overlappingPairArray.quickSort(btBroadphasePairSortPredicate());
  537. overlappingPairArray.resize(overlappingPairArray.size() - m_invalidPair);
  538. m_invalidPair = 0;
  539. #endif//CLEAN_INVALID_PAIRS
  540. //printf("overlappingPairArray.size()=%d\n",overlappingPairArray.size());
  541. }
  542. }
  543. template <typename BP_FP_INT_TYPE>
  544. bool btAxisSweep3Internal<BP_FP_INT_TYPE>::testAabbOverlap(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1)
  545. {
  546. const Handle* pHandleA = static_cast<Handle*>(proxy0);
  547. const Handle* pHandleB = static_cast<Handle*>(proxy1);
  548. //optimization 1: check the array index (memory address), instead of the m_pos
  549. for (int axis = 0; axis < 3; axis++)
  550. {
  551. if (pHandleA->m_maxEdges[axis] < pHandleB->m_minEdges[axis] ||
  552. pHandleB->m_maxEdges[axis] < pHandleA->m_minEdges[axis])
  553. {
  554. return false;
  555. }
  556. }
  557. return true;
  558. }
  559. template <typename BP_FP_INT_TYPE>
  560. bool btAxisSweep3Internal<BP_FP_INT_TYPE>::testOverlap2D(const Handle* pHandleA, const Handle* pHandleB,int axis0,int axis1)
  561. {
  562. //optimization 1: check the array index (memory address), instead of the m_pos
  563. if (pHandleA->m_maxEdges[axis0] < pHandleB->m_minEdges[axis0] ||
  564. pHandleB->m_maxEdges[axis0] < pHandleA->m_minEdges[axis0] ||
  565. pHandleA->m_maxEdges[axis1] < pHandleB->m_minEdges[axis1] ||
  566. pHandleB->m_maxEdges[axis1] < pHandleA->m_minEdges[axis1])
  567. {
  568. return false;
  569. }
  570. return true;
  571. }
  572. template <typename BP_FP_INT_TYPE>
  573. void btAxisSweep3Internal<BP_FP_INT_TYPE>::updateHandle(BP_FP_INT_TYPE handle, const btVector3& aabbMin,const btVector3& aabbMax,btDispatcher* dispatcher)
  574. {
  575. // btAssert(bounds.IsFinite());
  576. //btAssert(bounds.HasVolume());
  577. Handle* pHandle = getHandle(handle);
  578. // quantize the new bounds
  579. BP_FP_INT_TYPE min[3], max[3];
  580. quantize(min, aabbMin, 0);
  581. quantize(max, aabbMax, 1);
  582. // update changed edges
  583. for (int axis = 0; axis < 3; axis++)
  584. {
  585. BP_FP_INT_TYPE emin = pHandle->m_minEdges[axis];
  586. BP_FP_INT_TYPE emax = pHandle->m_maxEdges[axis];
  587. int dmin = (int)min[axis] - (int)m_pEdges[axis][emin].m_pos;
  588. int dmax = (int)max[axis] - (int)m_pEdges[axis][emax].m_pos;
  589. m_pEdges[axis][emin].m_pos = min[axis];
  590. m_pEdges[axis][emax].m_pos = max[axis];
  591. // expand (only adds overlaps)
  592. if (dmin < 0)
  593. sortMinDown(axis, emin,dispatcher,true);
  594. if (dmax > 0)
  595. sortMaxUp(axis, emax,dispatcher,true);
  596. // shrink (only removes overlaps)
  597. if (dmin > 0)
  598. sortMinUp(axis, emin,dispatcher,true);
  599. if (dmax < 0)
  600. sortMaxDown(axis, emax,dispatcher,true);
  601. #ifdef DEBUG_BROADPHASE
  602. debugPrintAxis(axis);
  603. #endif //DEBUG_BROADPHASE
  604. }
  605. }
  606. // sorting a min edge downwards can only ever *add* overlaps
  607. template <typename BP_FP_INT_TYPE>
  608. void btAxisSweep3Internal<BP_FP_INT_TYPE>::sortMinDown(int axis, BP_FP_INT_TYPE edge, btDispatcher* /* dispatcher */, bool updateOverlaps)
  609. {
  610. Edge* pEdge = m_pEdges[axis] + edge;
  611. Edge* pPrev = pEdge - 1;
  612. Handle* pHandleEdge = getHandle(pEdge->m_handle);
  613. while (pEdge->m_pos < pPrev->m_pos)
  614. {
  615. Handle* pHandlePrev = getHandle(pPrev->m_handle);
  616. if (pPrev->IsMax())
  617. {
  618. // if previous edge is a maximum check the bounds and add an overlap if necessary
  619. const int axis1 = (1 << axis) & 3;
  620. const int axis2 = (1 << axis1) & 3;
  621. if (updateOverlaps && testOverlap2D(pHandleEdge, pHandlePrev,axis1,axis2))
  622. {
  623. m_pairCache->addOverlappingPair(pHandleEdge,pHandlePrev);
  624. if (m_userPairCallback)
  625. m_userPairCallback->addOverlappingPair(pHandleEdge,pHandlePrev);
  626. //AddOverlap(pEdge->m_handle, pPrev->m_handle);
  627. }
  628. // update edge reference in other handle
  629. pHandlePrev->m_maxEdges[axis]++;
  630. }
  631. else
  632. pHandlePrev->m_minEdges[axis]++;
  633. pHandleEdge->m_minEdges[axis]--;
  634. // swap the edges
  635. Edge swap = *pEdge;
  636. *pEdge = *pPrev;
  637. *pPrev = swap;
  638. // decrement
  639. pEdge--;
  640. pPrev--;
  641. }
  642. #ifdef DEBUG_BROADPHASE
  643. debugPrintAxis(axis);
  644. #endif //DEBUG_BROADPHASE
  645. }
  646. // sorting a min edge upwards can only ever *remove* overlaps
  647. template <typename BP_FP_INT_TYPE>
  648. void btAxisSweep3Internal<BP_FP_INT_TYPE>::sortMinUp(int axis, BP_FP_INT_TYPE edge, btDispatcher* dispatcher, bool updateOverlaps)
  649. {
  650. Edge* pEdge = m_pEdges[axis] + edge;
  651. Edge* pNext = pEdge + 1;
  652. Handle* pHandleEdge = getHandle(pEdge->m_handle);
  653. while (pNext->m_handle && (pEdge->m_pos >= pNext->m_pos))
  654. {
  655. Handle* pHandleNext = getHandle(pNext->m_handle);
  656. if (pNext->IsMax())
  657. {
  658. Handle* handle0 = getHandle(pEdge->m_handle);
  659. Handle* handle1 = getHandle(pNext->m_handle);
  660. const int axis1 = (1 << axis) & 3;
  661. const int axis2 = (1 << axis1) & 3;
  662. // if next edge is maximum remove any overlap between the two handles
  663. if (updateOverlaps
  664. #ifdef USE_OVERLAP_TEST_ON_REMOVES
  665. && testOverlap2D(handle0,handle1,axis1,axis2)
  666. #endif //USE_OVERLAP_TEST_ON_REMOVES
  667. )
  668. {
  669. m_pairCache->removeOverlappingPair(handle0,handle1,dispatcher);
  670. if (m_userPairCallback)
  671. m_userPairCallback->removeOverlappingPair(handle0,handle1,dispatcher);
  672. }
  673. // update edge reference in other handle
  674. pHandleNext->m_maxEdges[axis]--;
  675. }
  676. else
  677. pHandleNext->m_minEdges[axis]--;
  678. pHandleEdge->m_minEdges[axis]++;
  679. // swap the edges
  680. Edge swap = *pEdge;
  681. *pEdge = *pNext;
  682. *pNext = swap;
  683. // increment
  684. pEdge++;
  685. pNext++;
  686. }
  687. }
  688. // sorting a max edge downwards can only ever *remove* overlaps
  689. template <typename BP_FP_INT_TYPE>
  690. void btAxisSweep3Internal<BP_FP_INT_TYPE>::sortMaxDown(int axis, BP_FP_INT_TYPE edge, btDispatcher* dispatcher, bool updateOverlaps)
  691. {
  692. Edge* pEdge = m_pEdges[axis] + edge;
  693. Edge* pPrev = pEdge - 1;
  694. Handle* pHandleEdge = getHandle(pEdge->m_handle);
  695. while (pEdge->m_pos < pPrev->m_pos)
  696. {
  697. Handle* pHandlePrev = getHandle(pPrev->m_handle);
  698. if (!pPrev->IsMax())
  699. {
  700. // if previous edge was a minimum remove any overlap between the two handles
  701. Handle* handle0 = getHandle(pEdge->m_handle);
  702. Handle* handle1 = getHandle(pPrev->m_handle);
  703. const int axis1 = (1 << axis) & 3;
  704. const int axis2 = (1 << axis1) & 3;
  705. if (updateOverlaps
  706. #ifdef USE_OVERLAP_TEST_ON_REMOVES
  707. && testOverlap2D(handle0,handle1,axis1,axis2)
  708. #endif //USE_OVERLAP_TEST_ON_REMOVES
  709. )
  710. {
  711. //this is done during the overlappingpairarray iteration/narrowphase collision
  712. m_pairCache->removeOverlappingPair(handle0,handle1,dispatcher);
  713. if (m_userPairCallback)
  714. m_userPairCallback->removeOverlappingPair(handle0,handle1,dispatcher);
  715. }
  716. // update edge reference in other handle
  717. pHandlePrev->m_minEdges[axis]++;;
  718. }
  719. else
  720. pHandlePrev->m_maxEdges[axis]++;
  721. pHandleEdge->m_maxEdges[axis]--;
  722. // swap the edges
  723. Edge swap = *pEdge;
  724. *pEdge = *pPrev;
  725. *pPrev = swap;
  726. // decrement
  727. pEdge--;
  728. pPrev--;
  729. }
  730. #ifdef DEBUG_BROADPHASE
  731. debugPrintAxis(axis);
  732. #endif //DEBUG_BROADPHASE
  733. }
  734. // sorting a max edge upwards can only ever *add* overlaps
  735. template <typename BP_FP_INT_TYPE>
  736. void btAxisSweep3Internal<BP_FP_INT_TYPE>::sortMaxUp(int axis, BP_FP_INT_TYPE edge, btDispatcher* /* dispatcher */, bool updateOverlaps)
  737. {
  738. Edge* pEdge = m_pEdges[axis] + edge;
  739. Edge* pNext = pEdge + 1;
  740. Handle* pHandleEdge = getHandle(pEdge->m_handle);
  741. while (pNext->m_handle && (pEdge->m_pos >= pNext->m_pos))
  742. {
  743. Handle* pHandleNext = getHandle(pNext->m_handle);
  744. const int axis1 = (1 << axis) & 3;
  745. const int axis2 = (1 << axis1) & 3;
  746. if (!pNext->IsMax())
  747. {
  748. // if next edge is a minimum check the bounds and add an overlap if necessary
  749. if (updateOverlaps && testOverlap2D(pHandleEdge, pHandleNext,axis1,axis2))
  750. {
  751. Handle* handle0 = getHandle(pEdge->m_handle);
  752. Handle* handle1 = getHandle(pNext->m_handle);
  753. m_pairCache->addOverlappingPair(handle0,handle1);
  754. if (m_userPairCallback)
  755. m_userPairCallback->addOverlappingPair(handle0,handle1);
  756. }
  757. // update edge reference in other handle
  758. pHandleNext->m_minEdges[axis]--;
  759. }
  760. else
  761. pHandleNext->m_maxEdges[axis]--;
  762. pHandleEdge->m_maxEdges[axis]++;
  763. // swap the edges
  764. Edge swap = *pEdge;
  765. *pEdge = *pNext;
  766. *pNext = swap;
  767. // increment
  768. pEdge++;
  769. pNext++;
  770. }
  771. }
  772. ////////////////////////////////////////////////////////////////////
  773. /// The btAxisSweep3 is an efficient implementation of the 3d axis sweep and prune broadphase.
  774. /// It uses arrays rather then lists for storage of the 3 axis. Also it operates using 16 bit integer coordinates instead of floats.
  775. /// For large worlds and many objects, use bt32BitAxisSweep3 or btDbvtBroadphase instead. bt32BitAxisSweep3 has higher precision and allows more then 16384 objects at the cost of more memory and bit of performance.
  776. class btAxisSweep3 : public btAxisSweep3Internal<unsigned short int>
  777. {
  778. public:
  779. btAxisSweep3(const btVector3& worldAabbMin,const btVector3& worldAabbMax, unsigned short int maxHandles = 16384, btOverlappingPairCache* pairCache = 0, bool disableRaycastAccelerator = false);
  780. };
  781. /// The bt32BitAxisSweep3 allows higher precision quantization and more objects compared to the btAxisSweep3 sweep and prune.
  782. /// This comes at the cost of more memory per handle, and a bit slower performance.
  783. /// It uses arrays rather then lists for storage of the 3 axis.
  784. class bt32BitAxisSweep3 : public btAxisSweep3Internal<unsigned int>
  785. {
  786. public:
  787. bt32BitAxisSweep3(const btVector3& worldAabbMin,const btVector3& worldAabbMax, unsigned int maxHandles = 1500000, btOverlappingPairCache* pairCache = 0, bool disableRaycastAccelerator = false);
  788. };
  789. #endif