1 //Bullet Continuous Collision Detection and Physics Library
2 //Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
7 // Copyright (c) 2006 Simon Hobbs
9 // 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.
11 // 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:
13 // 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.
15 // 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
17 // 3. This notice may not be removed or altered from any source distribution.
19 #ifndef BT_AXIS_SWEEP_3_INTERNAL_H
20 #define BT_AXIS_SWEEP_3_INTERNAL_H
22 #include "LinearMath/btVector3.h"
23 #include "btOverlappingPairCache.h"
24 #include "btBroadphaseInterface.h"
25 #include "btBroadphaseProxy.h"
26 #include "btOverlappingPairCallback.h"
27 #include "btDbvtBroadphase.h"
29 //#define DEBUG_BROADPHASE 1
30 #define USE_OVERLAP_TEST_ON_REMOVES 1
32 /// The internal templace class btAxisSweep3Internal implements the sweep and prune broadphase.
33 /// It uses quantized integers to represent the begin and end points for each of the 3 axis.
34 /// Dont use this class directly, use btAxisSweep3 or bt32BitAxisSweep3 instead.
35 template <typename BP_FP_INT_TYPE>
36 class btAxisSweep3Internal : public btBroadphaseInterface
39 BP_FP_INT_TYPE m_bpHandleMask;
40 BP_FP_INT_TYPE m_handleSentinel;
43 BT_DECLARE_ALIGNED_ALLOCATOR();
48 BP_FP_INT_TYPE m_pos; // low bit is min/max
49 BP_FP_INT_TYPE m_handle;
51 BP_FP_INT_TYPE IsMax() const { return static_cast<BP_FP_INT_TYPE>(m_pos & 1); }
55 class Handle : public btBroadphaseProxy
58 BT_DECLARE_ALIGNED_ALLOCATOR();
60 // indexes into the edge arrays
61 BP_FP_INT_TYPE m_minEdges[3], m_maxEdges[3]; // 6 * 2 = 12
62 // BP_FP_INT_TYPE m_uniqueId;
63 btBroadphaseProxy* m_dbvtProxy; //for faster raycast
64 //void* m_pOwner; this is now in btBroadphaseProxy.m_clientObject
66 SIMD_FORCE_INLINE void SetNextFree(BP_FP_INT_TYPE next) { m_minEdges[0] = next; }
67 SIMD_FORCE_INLINE BP_FP_INT_TYPE GetNextFree() const { return m_minEdges[0]; }
68 }; // 24 bytes + 24 for Edge structures = 44 bytes total per entry
71 btVector3 m_worldAabbMin; // overall system bounds
72 btVector3 m_worldAabbMax; // overall system bounds
74 btVector3 m_quantize; // scaling factor for quantization
76 BP_FP_INT_TYPE m_numHandles; // number of active handles
77 BP_FP_INT_TYPE m_maxHandles; // max number of handles
78 Handle* m_pHandles; // handles pool
80 BP_FP_INT_TYPE m_firstFreeHandle; // free handles list
82 Edge* m_pEdges[3]; // edge arrays for the 3 axes (each array has m_maxHandles * 2 + 2 sentinel entries)
83 void* m_pEdgesRawPtr[3];
85 btOverlappingPairCache* m_pairCache;
87 ///btOverlappingPairCallback is an additional optional user callback for adding/removing overlapping pairs, similar interface to btOverlappingPairCache.
88 btOverlappingPairCallback* m_userPairCallback;
94 ///additional dynamic aabb structure, used to accelerate ray cast queries.
95 ///can be disabled using a optional argument in the constructor
96 btDbvtBroadphase* m_raycastAccelerator;
97 btOverlappingPairCache* m_nullPairCache;
99 // allocation/deallocation
100 BP_FP_INT_TYPE allocHandle();
101 void freeHandle(BP_FP_INT_TYPE handle);
103 bool testOverlap2D(const Handle* pHandleA, const Handle* pHandleB, int axis0, int axis1);
105 #ifdef DEBUG_BROADPHASE
106 void debugPrintAxis(int axis, bool checkCardinality = true);
107 #endif //DEBUG_BROADPHASE
109 //Overlap* AddOverlap(BP_FP_INT_TYPE handleA, BP_FP_INT_TYPE handleB);
110 //void RemoveOverlap(BP_FP_INT_TYPE handleA, BP_FP_INT_TYPE handleB);
112 void sortMinDown(int axis, BP_FP_INT_TYPE edge, btDispatcher* dispatcher, bool updateOverlaps);
113 void sortMinUp(int axis, BP_FP_INT_TYPE edge, btDispatcher* dispatcher, bool updateOverlaps);
114 void sortMaxDown(int axis, BP_FP_INT_TYPE edge, btDispatcher* dispatcher, bool updateOverlaps);
115 void sortMaxUp(int axis, BP_FP_INT_TYPE edge, btDispatcher* dispatcher, bool updateOverlaps);
118 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);
120 virtual ~btAxisSweep3Internal();
122 BP_FP_INT_TYPE getNumHandles() const
127 virtual void calculateOverlappingPairs(btDispatcher* dispatcher);
129 BP_FP_INT_TYPE addHandle(const btVector3& aabbMin, const btVector3& aabbMax, void* pOwner, int collisionFilterGroup, int collisionFilterMask, btDispatcher* dispatcher);
130 void removeHandle(BP_FP_INT_TYPE handle, btDispatcher* dispatcher);
131 void updateHandle(BP_FP_INT_TYPE handle, const btVector3& aabbMin, const btVector3& aabbMax, btDispatcher* dispatcher);
132 SIMD_FORCE_INLINE Handle* getHandle(BP_FP_INT_TYPE index) const { return m_pHandles + index; }
134 virtual void resetPool(btDispatcher* dispatcher);
136 void processAllOverlappingPairs(btOverlapCallback* callback);
138 //Broadphase Interface
139 virtual btBroadphaseProxy* createProxy(const btVector3& aabbMin, const btVector3& aabbMax, int shapeType, void* userPtr, int collisionFilterGroup, int collisionFilterMask, btDispatcher* dispatcher);
140 virtual void destroyProxy(btBroadphaseProxy* proxy, btDispatcher* dispatcher);
141 virtual void setAabb(btBroadphaseProxy* proxy, const btVector3& aabbMin, const btVector3& aabbMax, btDispatcher* dispatcher);
142 virtual void getAabb(btBroadphaseProxy* proxy, btVector3& aabbMin, btVector3& aabbMax) const;
144 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));
145 virtual void aabbTest(const btVector3& aabbMin, const btVector3& aabbMax, btBroadphaseAabbCallback& callback);
147 void quantize(BP_FP_INT_TYPE* out, const btVector3& point, int isMax) const;
148 ///unQuantize should be conservative: aabbMin/aabbMax should be larger then 'getAabb' result
149 void unQuantize(btBroadphaseProxy* proxy, btVector3& aabbMin, btVector3& aabbMax) const;
151 bool testAabbOverlap(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1);
153 btOverlappingPairCache* getOverlappingPairCache()
157 const btOverlappingPairCache* getOverlappingPairCache() const
162 void setOverlappingPairUserCallback(btOverlappingPairCallback* pairCallback)
164 m_userPairCallback = pairCallback;
166 const btOverlappingPairCallback* getOverlappingPairUserCallback() const
168 return m_userPairCallback;
171 ///getAabb returns the axis aligned bounding box in the 'global' coordinate frame
172 ///will add some transform later
173 virtual void getBroadphaseAabb(btVector3& aabbMin, btVector3& aabbMax) const
175 aabbMin = m_worldAabbMin;
176 aabbMax = m_worldAabbMax;
179 virtual void printStats()
181 /* printf("btAxisSweep3.h\n");
182 printf("numHandles = %d, maxHandles = %d\n",m_numHandles,m_maxHandles);
183 printf("aabbMin=%f,%f,%f,aabbMax=%f,%f,%f\n",m_worldAabbMin.getX(),m_worldAabbMin.getY(),m_worldAabbMin.getZ(),
184 m_worldAabbMax.getX(),m_worldAabbMax.getY(),m_worldAabbMax.getZ());
189 ////////////////////////////////////////////////////////////////////
191 #ifdef DEBUG_BROADPHASE
194 template <typename BP_FP_INT_TYPE>
195 void btAxisSweep3<BP_FP_INT_TYPE>::debugPrintAxis(int axis, bool checkCardinality)
197 int numEdges = m_pHandles[0].m_maxEdges[axis];
198 printf("SAP Axis %d, numEdges=%d\n", axis, numEdges);
201 for (i = 0; i < numEdges + 1; i++)
203 Edge* pEdge = m_pEdges[axis] + i;
204 Handle* pHandlePrev = getHandle(pEdge->m_handle);
205 int handleIndex = pEdge->IsMax() ? pHandlePrev->m_maxEdges[axis] : pHandlePrev->m_minEdges[axis];
207 beginOrEnd = pEdge->IsMax() ? 'E' : 'B';
208 printf(" [%c,h=%d,p=%x,i=%d]\n", beginOrEnd, pEdge->m_handle, pEdge->m_pos, handleIndex);
211 if (checkCardinality)
212 btAssert(numEdges == m_numHandles * 2 + 1);
214 #endif //DEBUG_BROADPHASE
216 template <typename BP_FP_INT_TYPE>
217 btBroadphaseProxy* btAxisSweep3Internal<BP_FP_INT_TYPE>::createProxy(const btVector3& aabbMin, const btVector3& aabbMax, int shapeType, void* userPtr, int collisionFilterGroup, int collisionFilterMask, btDispatcher* dispatcher)
220 BP_FP_INT_TYPE handleId = addHandle(aabbMin, aabbMax, userPtr, collisionFilterGroup, collisionFilterMask, dispatcher);
222 Handle* handle = getHandle(handleId);
224 if (m_raycastAccelerator)
226 btBroadphaseProxy* rayProxy = m_raycastAccelerator->createProxy(aabbMin, aabbMax, shapeType, userPtr, collisionFilterGroup, collisionFilterMask, dispatcher);
227 handle->m_dbvtProxy = rayProxy;
232 template <typename BP_FP_INT_TYPE>
233 void btAxisSweep3Internal<BP_FP_INT_TYPE>::destroyProxy(btBroadphaseProxy* proxy, btDispatcher* dispatcher)
235 Handle* handle = static_cast<Handle*>(proxy);
236 if (m_raycastAccelerator)
237 m_raycastAccelerator->destroyProxy(handle->m_dbvtProxy, dispatcher);
238 removeHandle(static_cast<BP_FP_INT_TYPE>(handle->m_uniqueId), dispatcher);
241 template <typename BP_FP_INT_TYPE>
242 void btAxisSweep3Internal<BP_FP_INT_TYPE>::setAabb(btBroadphaseProxy* proxy, const btVector3& aabbMin, const btVector3& aabbMax, btDispatcher* dispatcher)
244 Handle* handle = static_cast<Handle*>(proxy);
245 handle->m_aabbMin = aabbMin;
246 handle->m_aabbMax = aabbMax;
247 updateHandle(static_cast<BP_FP_INT_TYPE>(handle->m_uniqueId), aabbMin, aabbMax, dispatcher);
248 if (m_raycastAccelerator)
249 m_raycastAccelerator->setAabb(handle->m_dbvtProxy, aabbMin, aabbMax, dispatcher);
252 template <typename BP_FP_INT_TYPE>
253 void btAxisSweep3Internal<BP_FP_INT_TYPE>::rayTest(const btVector3& rayFrom, const btVector3& rayTo, btBroadphaseRayCallback& rayCallback, const btVector3& aabbMin, const btVector3& aabbMax)
255 if (m_raycastAccelerator)
257 m_raycastAccelerator->rayTest(rayFrom, rayTo, rayCallback, aabbMin, aabbMax);
262 BP_FP_INT_TYPE axis = 0;
264 for (BP_FP_INT_TYPE i = 1; i < m_numHandles * 2 + 1; i++)
266 if (m_pEdges[axis][i].IsMax())
268 rayCallback.process(getHandle(m_pEdges[axis][i].m_handle));
274 template <typename BP_FP_INT_TYPE>
275 void btAxisSweep3Internal<BP_FP_INT_TYPE>::aabbTest(const btVector3& aabbMin, const btVector3& aabbMax, btBroadphaseAabbCallback& callback)
277 if (m_raycastAccelerator)
279 m_raycastAccelerator->aabbTest(aabbMin, aabbMax, callback);
284 BP_FP_INT_TYPE axis = 0;
286 for (BP_FP_INT_TYPE i = 1; i < m_numHandles * 2 + 1; i++)
288 if (m_pEdges[axis][i].IsMax())
290 Handle* handle = getHandle(m_pEdges[axis][i].m_handle);
291 if (TestAabbAgainstAabb2(aabbMin, aabbMax, handle->m_aabbMin, handle->m_aabbMax))
293 callback.process(handle);
300 template <typename BP_FP_INT_TYPE>
301 void btAxisSweep3Internal<BP_FP_INT_TYPE>::getAabb(btBroadphaseProxy* proxy, btVector3& aabbMin, btVector3& aabbMax) const
303 Handle* pHandle = static_cast<Handle*>(proxy);
304 aabbMin = pHandle->m_aabbMin;
305 aabbMax = pHandle->m_aabbMax;
308 template <typename BP_FP_INT_TYPE>
309 void btAxisSweep3Internal<BP_FP_INT_TYPE>::unQuantize(btBroadphaseProxy* proxy, btVector3& aabbMin, btVector3& aabbMax) const
311 Handle* pHandle = static_cast<Handle*>(proxy);
313 unsigned short vecInMin[3];
314 unsigned short vecInMax[3];
316 vecInMin[0] = m_pEdges[0][pHandle->m_minEdges[0]].m_pos;
317 vecInMax[0] = m_pEdges[0][pHandle->m_maxEdges[0]].m_pos + 1;
318 vecInMin[1] = m_pEdges[1][pHandle->m_minEdges[1]].m_pos;
319 vecInMax[1] = m_pEdges[1][pHandle->m_maxEdges[1]].m_pos + 1;
320 vecInMin[2] = m_pEdges[2][pHandle->m_minEdges[2]].m_pos;
321 vecInMax[2] = m_pEdges[2][pHandle->m_maxEdges[2]].m_pos + 1;
323 aabbMin.setValue((btScalar)(vecInMin[0]) / (m_quantize.getX()), (btScalar)(vecInMin[1]) / (m_quantize.getY()), (btScalar)(vecInMin[2]) / (m_quantize.getZ()));
324 aabbMin += m_worldAabbMin;
326 aabbMax.setValue((btScalar)(vecInMax[0]) / (m_quantize.getX()), (btScalar)(vecInMax[1]) / (m_quantize.getY()), (btScalar)(vecInMax[2]) / (m_quantize.getZ()));
327 aabbMax += m_worldAabbMin;
330 template <typename BP_FP_INT_TYPE>
331 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)
332 : m_bpHandleMask(handleMask),
333 m_handleSentinel(handleSentinel),
334 m_pairCache(pairCache),
335 m_userPairCallback(0),
336 m_ownsPairCache(false),
338 m_raycastAccelerator(0)
340 BP_FP_INT_TYPE maxHandles = static_cast<BP_FP_INT_TYPE>(userMaxHandles + 1); //need to add one sentinel handle
344 void* ptr = btAlignedAlloc(sizeof(btHashedOverlappingPairCache), 16);
345 m_pairCache = new (ptr) btHashedOverlappingPairCache();
346 m_ownsPairCache = true;
349 if (!disableRaycastAccelerator)
351 m_nullPairCache = new (btAlignedAlloc(sizeof(btNullPairCache), 16)) btNullPairCache();
352 m_raycastAccelerator = new (btAlignedAlloc(sizeof(btDbvtBroadphase), 16)) btDbvtBroadphase(m_nullPairCache); //m_pairCache);
353 m_raycastAccelerator->m_deferedcollide = true; //don't add/remove pairs
356 //btAssert(bounds.HasVolume());
359 m_worldAabbMin = worldAabbMin;
360 m_worldAabbMax = worldAabbMax;
362 btVector3 aabbSize = m_worldAabbMax - m_worldAabbMin;
364 BP_FP_INT_TYPE maxInt = m_handleSentinel;
366 m_quantize = btVector3(btScalar(maxInt), btScalar(maxInt), btScalar(maxInt)) / aabbSize;
368 // allocate handles buffer, using btAlignedAlloc, and put all handles on free list
369 m_pHandles = new Handle[maxHandles];
371 m_maxHandles = maxHandles;
374 // handle 0 is reserved as the null index, and is also used as the sentinel
375 m_firstFreeHandle = 1;
377 for (BP_FP_INT_TYPE i = m_firstFreeHandle; i < maxHandles; i++)
378 m_pHandles[i].SetNextFree(static_cast<BP_FP_INT_TYPE>(i + 1));
379 m_pHandles[maxHandles - 1].SetNextFree(0);
383 // allocate edge buffers
384 for (int i = 0; i < 3; i++)
386 m_pEdgesRawPtr[i] = btAlignedAlloc(sizeof(Edge) * maxHandles * 2, 16);
387 m_pEdges[i] = new (m_pEdgesRawPtr[i]) Edge[maxHandles * 2];
390 //removed overlap management
392 // make boundary sentinels
394 m_pHandles[0].m_clientObject = 0;
396 for (int axis = 0; axis < 3; axis++)
398 m_pHandles[0].m_minEdges[axis] = 0;
399 m_pHandles[0].m_maxEdges[axis] = 1;
401 m_pEdges[axis][0].m_pos = 0;
402 m_pEdges[axis][0].m_handle = 0;
403 m_pEdges[axis][1].m_pos = m_handleSentinel;
404 m_pEdges[axis][1].m_handle = 0;
405 #ifdef DEBUG_BROADPHASE
406 debugPrintAxis(axis);
407 #endif //DEBUG_BROADPHASE
411 template <typename BP_FP_INT_TYPE>
412 btAxisSweep3Internal<BP_FP_INT_TYPE>::~btAxisSweep3Internal()
414 if (m_raycastAccelerator)
416 m_nullPairCache->~btOverlappingPairCache();
417 btAlignedFree(m_nullPairCache);
418 m_raycastAccelerator->~btDbvtBroadphase();
419 btAlignedFree(m_raycastAccelerator);
422 for (int i = 2; i >= 0; i--)
424 btAlignedFree(m_pEdgesRawPtr[i]);
430 m_pairCache->~btOverlappingPairCache();
431 btAlignedFree(m_pairCache);
435 template <typename BP_FP_INT_TYPE>
436 void btAxisSweep3Internal<BP_FP_INT_TYPE>::quantize(BP_FP_INT_TYPE* out, const btVector3& point, int isMax) const
438 #ifdef OLD_CLAMPING_METHOD
439 ///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]
440 ///see http://code.google.com/p/bullet/issues/detail?id=87
441 btVector3 clampedPoint(point);
442 clampedPoint.setMax(m_worldAabbMin);
443 clampedPoint.setMin(m_worldAabbMax);
444 btVector3 v = (clampedPoint - m_worldAabbMin) * m_quantize;
445 out[0] = (BP_FP_INT_TYPE)(((BP_FP_INT_TYPE)v.getX() & m_bpHandleMask) | isMax);
446 out[1] = (BP_FP_INT_TYPE)(((BP_FP_INT_TYPE)v.getY() & m_bpHandleMask) | isMax);
447 out[2] = (BP_FP_INT_TYPE)(((BP_FP_INT_TYPE)v.getZ() & m_bpHandleMask) | isMax);
449 btVector3 v = (point - m_worldAabbMin) * m_quantize;
450 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);
451 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);
452 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);
453 #endif //OLD_CLAMPING_METHOD
456 template <typename BP_FP_INT_TYPE>
457 BP_FP_INT_TYPE btAxisSweep3Internal<BP_FP_INT_TYPE>::allocHandle()
459 btAssert(m_firstFreeHandle);
461 BP_FP_INT_TYPE handle = m_firstFreeHandle;
462 m_firstFreeHandle = getHandle(handle)->GetNextFree();
468 template <typename BP_FP_INT_TYPE>
469 void btAxisSweep3Internal<BP_FP_INT_TYPE>::freeHandle(BP_FP_INT_TYPE handle)
471 btAssert(handle > 0 && handle < m_maxHandles);
473 getHandle(handle)->SetNextFree(m_firstFreeHandle);
474 m_firstFreeHandle = handle;
479 template <typename BP_FP_INT_TYPE>
480 BP_FP_INT_TYPE btAxisSweep3Internal<BP_FP_INT_TYPE>::addHandle(const btVector3& aabbMin, const btVector3& aabbMax, void* pOwner, int collisionFilterGroup, int collisionFilterMask, btDispatcher* dispatcher)
482 // quantize the bounds
483 BP_FP_INT_TYPE min[3], max[3];
484 quantize(min, aabbMin, 0);
485 quantize(max, aabbMax, 1);
488 BP_FP_INT_TYPE handle = allocHandle();
490 Handle* pHandle = getHandle(handle);
492 pHandle->m_uniqueId = static_cast<int>(handle);
493 //pHandle->m_pOverlaps = 0;
494 pHandle->m_clientObject = pOwner;
495 pHandle->m_collisionFilterGroup = collisionFilterGroup;
496 pHandle->m_collisionFilterMask = collisionFilterMask;
498 // compute current limit of edge arrays
499 BP_FP_INT_TYPE limit = static_cast<BP_FP_INT_TYPE>(m_numHandles * 2);
501 // insert new edges just inside the max boundary edge
502 for (BP_FP_INT_TYPE axis = 0; axis < 3; axis++)
504 m_pHandles[0].m_maxEdges[axis] += 2;
506 m_pEdges[axis][limit + 1] = m_pEdges[axis][limit - 1];
508 m_pEdges[axis][limit - 1].m_pos = min[axis];
509 m_pEdges[axis][limit - 1].m_handle = handle;
511 m_pEdges[axis][limit].m_pos = max[axis];
512 m_pEdges[axis][limit].m_handle = handle;
514 pHandle->m_minEdges[axis] = static_cast<BP_FP_INT_TYPE>(limit - 1);
515 pHandle->m_maxEdges[axis] = limit;
518 // now sort the new edges to their correct position
519 sortMinDown(0, pHandle->m_minEdges[0], dispatcher, false);
520 sortMaxDown(0, pHandle->m_maxEdges[0], dispatcher, false);
521 sortMinDown(1, pHandle->m_minEdges[1], dispatcher, false);
522 sortMaxDown(1, pHandle->m_maxEdges[1], dispatcher, false);
523 sortMinDown(2, pHandle->m_minEdges[2], dispatcher, true);
524 sortMaxDown(2, pHandle->m_maxEdges[2], dispatcher, true);
529 template <typename BP_FP_INT_TYPE>
530 void btAxisSweep3Internal<BP_FP_INT_TYPE>::removeHandle(BP_FP_INT_TYPE handle, btDispatcher* dispatcher)
532 Handle* pHandle = getHandle(handle);
534 //explicitly remove the pairs containing the proxy
535 //we could do it also in the sortMinUp (passing true)
536 ///@todo: compare performance
537 if (!m_pairCache->hasDeferredRemoval())
539 m_pairCache->removeOverlappingPairsContainingProxy(pHandle, dispatcher);
542 // compute current limit of edge arrays
543 int limit = static_cast<int>(m_numHandles * 2);
547 for (axis = 0; axis < 3; axis++)
549 m_pHandles[0].m_maxEdges[axis] -= 2;
552 // remove the edges by sorting them up to the end of the list
553 for (axis = 0; axis < 3; axis++)
555 Edge* pEdges = m_pEdges[axis];
556 BP_FP_INT_TYPE max = pHandle->m_maxEdges[axis];
557 pEdges[max].m_pos = m_handleSentinel;
559 sortMaxUp(axis, max, dispatcher, false);
561 BP_FP_INT_TYPE i = pHandle->m_minEdges[axis];
562 pEdges[i].m_pos = m_handleSentinel;
564 sortMinUp(axis, i, dispatcher, false);
566 pEdges[limit - 1].m_handle = 0;
567 pEdges[limit - 1].m_pos = m_handleSentinel;
569 #ifdef DEBUG_BROADPHASE
570 debugPrintAxis(axis, false);
571 #endif //DEBUG_BROADPHASE
578 template <typename BP_FP_INT_TYPE>
579 void btAxisSweep3Internal<BP_FP_INT_TYPE>::resetPool(btDispatcher* /*dispatcher*/)
581 if (m_numHandles == 0)
583 m_firstFreeHandle = 1;
585 for (BP_FP_INT_TYPE i = m_firstFreeHandle; i < m_maxHandles; i++)
586 m_pHandles[i].SetNextFree(static_cast<BP_FP_INT_TYPE>(i + 1));
587 m_pHandles[m_maxHandles - 1].SetNextFree(0);
594 template <typename BP_FP_INT_TYPE>
595 void btAxisSweep3Internal<BP_FP_INT_TYPE>::calculateOverlappingPairs(btDispatcher* dispatcher)
597 if (m_pairCache->hasDeferredRemoval())
599 btBroadphasePairArray& overlappingPairArray = m_pairCache->getOverlappingPairArray();
601 //perform a sort, to find duplicates and to sort 'invalid' pairs to the end
602 overlappingPairArray.quickSort(btBroadphasePairSortPredicate());
604 overlappingPairArray.resize(overlappingPairArray.size() - m_invalidPair);
609 btBroadphasePair previousPair;
610 previousPair.m_pProxy0 = 0;
611 previousPair.m_pProxy1 = 0;
612 previousPair.m_algorithm = 0;
614 for (i = 0; i < overlappingPairArray.size(); i++)
616 btBroadphasePair& pair = overlappingPairArray[i];
618 bool isDuplicate = (pair == previousPair);
622 bool needsRemoval = false;
626 ///important to use an AABB test that is consistent with the broadphase
627 bool hasOverlap = testAabbOverlap(pair.m_pProxy0, pair.m_pProxy1);
631 needsRemoval = false; //callback->processOverlap(pair);
642 //should have no algorithm
643 btAssert(!pair.m_algorithm);
648 m_pairCache->cleanOverlappingPair(pair, dispatcher);
650 // m_overlappingPairArray.swap(i,m_overlappingPairArray.size()-1);
651 // m_overlappingPairArray.pop_back();
658 ///if you don't like to skip the invalid pairs in the array, execute following code:
659 #define CLEAN_INVALID_PAIRS 1
660 #ifdef CLEAN_INVALID_PAIRS
662 //perform a sort, to sort 'invalid' pairs to the end
663 overlappingPairArray.quickSort(btBroadphasePairSortPredicate());
665 overlappingPairArray.resize(overlappingPairArray.size() - m_invalidPair);
667 #endif //CLEAN_INVALID_PAIRS
669 //printf("overlappingPairArray.size()=%d\n",overlappingPairArray.size());
673 template <typename BP_FP_INT_TYPE>
674 bool btAxisSweep3Internal<BP_FP_INT_TYPE>::testAabbOverlap(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1)
676 const Handle* pHandleA = static_cast<Handle*>(proxy0);
677 const Handle* pHandleB = static_cast<Handle*>(proxy1);
679 //optimization 1: check the array index (memory address), instead of the m_pos
681 for (int axis = 0; axis < 3; axis++)
683 if (pHandleA->m_maxEdges[axis] < pHandleB->m_minEdges[axis] ||
684 pHandleB->m_maxEdges[axis] < pHandleA->m_minEdges[axis])
692 template <typename BP_FP_INT_TYPE>
693 bool btAxisSweep3Internal<BP_FP_INT_TYPE>::testOverlap2D(const Handle* pHandleA, const Handle* pHandleB, int axis0, int axis1)
695 //optimization 1: check the array index (memory address), instead of the m_pos
697 if (pHandleA->m_maxEdges[axis0] < pHandleB->m_minEdges[axis0] ||
698 pHandleB->m_maxEdges[axis0] < pHandleA->m_minEdges[axis0] ||
699 pHandleA->m_maxEdges[axis1] < pHandleB->m_minEdges[axis1] ||
700 pHandleB->m_maxEdges[axis1] < pHandleA->m_minEdges[axis1])
707 template <typename BP_FP_INT_TYPE>
708 void btAxisSweep3Internal<BP_FP_INT_TYPE>::updateHandle(BP_FP_INT_TYPE handle, const btVector3& aabbMin, const btVector3& aabbMax, btDispatcher* dispatcher)
710 // btAssert(bounds.IsFinite());
711 //btAssert(bounds.HasVolume());
713 Handle* pHandle = getHandle(handle);
715 // quantize the new bounds
716 BP_FP_INT_TYPE min[3], max[3];
717 quantize(min, aabbMin, 0);
718 quantize(max, aabbMax, 1);
720 // update changed edges
721 for (int axis = 0; axis < 3; axis++)
723 BP_FP_INT_TYPE emin = pHandle->m_minEdges[axis];
724 BP_FP_INT_TYPE emax = pHandle->m_maxEdges[axis];
726 int dmin = (int)min[axis] - (int)m_pEdges[axis][emin].m_pos;
727 int dmax = (int)max[axis] - (int)m_pEdges[axis][emax].m_pos;
729 m_pEdges[axis][emin].m_pos = min[axis];
730 m_pEdges[axis][emax].m_pos = max[axis];
732 // expand (only adds overlaps)
734 sortMinDown(axis, emin, dispatcher, true);
737 sortMaxUp(axis, emax, dispatcher, true);
739 // shrink (only removes overlaps)
741 sortMinUp(axis, emin, dispatcher, true);
744 sortMaxDown(axis, emax, dispatcher, true);
746 #ifdef DEBUG_BROADPHASE
747 debugPrintAxis(axis);
748 #endif //DEBUG_BROADPHASE
752 // sorting a min edge downwards can only ever *add* overlaps
753 template <typename BP_FP_INT_TYPE>
754 void btAxisSweep3Internal<BP_FP_INT_TYPE>::sortMinDown(int axis, BP_FP_INT_TYPE edge, btDispatcher* /* dispatcher */, bool updateOverlaps)
756 Edge* pEdge = m_pEdges[axis] + edge;
757 Edge* pPrev = pEdge - 1;
758 Handle* pHandleEdge = getHandle(pEdge->m_handle);
760 while (pEdge->m_pos < pPrev->m_pos)
762 Handle* pHandlePrev = getHandle(pPrev->m_handle);
766 // if previous edge is a maximum check the bounds and add an overlap if necessary
767 const int axis1 = (1 << axis) & 3;
768 const int axis2 = (1 << axis1) & 3;
769 if (updateOverlaps && testOverlap2D(pHandleEdge, pHandlePrev, axis1, axis2))
771 m_pairCache->addOverlappingPair(pHandleEdge, pHandlePrev);
772 if (m_userPairCallback)
773 m_userPairCallback->addOverlappingPair(pHandleEdge, pHandlePrev);
775 //AddOverlap(pEdge->m_handle, pPrev->m_handle);
778 // update edge reference in other handle
779 pHandlePrev->m_maxEdges[axis]++;
782 pHandlePrev->m_minEdges[axis]++;
784 pHandleEdge->m_minEdges[axis]--;
796 #ifdef DEBUG_BROADPHASE
797 debugPrintAxis(axis);
798 #endif //DEBUG_BROADPHASE
801 // sorting a min edge upwards can only ever *remove* overlaps
802 template <typename BP_FP_INT_TYPE>
803 void btAxisSweep3Internal<BP_FP_INT_TYPE>::sortMinUp(int axis, BP_FP_INT_TYPE edge, btDispatcher* dispatcher, bool updateOverlaps)
805 Edge* pEdge = m_pEdges[axis] + edge;
806 Edge* pNext = pEdge + 1;
807 Handle* pHandleEdge = getHandle(pEdge->m_handle);
809 while (pNext->m_handle && (pEdge->m_pos >= pNext->m_pos))
811 Handle* pHandleNext = getHandle(pNext->m_handle);
815 Handle* handle0 = getHandle(pEdge->m_handle);
816 Handle* handle1 = getHandle(pNext->m_handle);
817 const int axis1 = (1 << axis) & 3;
818 const int axis2 = (1 << axis1) & 3;
820 // if next edge is maximum remove any overlap between the two handles
822 #ifdef USE_OVERLAP_TEST_ON_REMOVES
823 && testOverlap2D(handle0, handle1, axis1, axis2)
824 #endif //USE_OVERLAP_TEST_ON_REMOVES
827 m_pairCache->removeOverlappingPair(handle0, handle1, dispatcher);
828 if (m_userPairCallback)
829 m_userPairCallback->removeOverlappingPair(handle0, handle1, dispatcher);
832 // update edge reference in other handle
833 pHandleNext->m_maxEdges[axis]--;
836 pHandleNext->m_minEdges[axis]--;
838 pHandleEdge->m_minEdges[axis]++;
851 // sorting a max edge downwards can only ever *remove* overlaps
852 template <typename BP_FP_INT_TYPE>
853 void btAxisSweep3Internal<BP_FP_INT_TYPE>::sortMaxDown(int axis, BP_FP_INT_TYPE edge, btDispatcher* dispatcher, bool updateOverlaps)
855 Edge* pEdge = m_pEdges[axis] + edge;
856 Edge* pPrev = pEdge - 1;
857 Handle* pHandleEdge = getHandle(pEdge->m_handle);
859 while (pEdge->m_pos < pPrev->m_pos)
861 Handle* pHandlePrev = getHandle(pPrev->m_handle);
865 // if previous edge was a minimum remove any overlap between the two handles
866 Handle* handle0 = getHandle(pEdge->m_handle);
867 Handle* handle1 = getHandle(pPrev->m_handle);
868 const int axis1 = (1 << axis) & 3;
869 const int axis2 = (1 << axis1) & 3;
872 #ifdef USE_OVERLAP_TEST_ON_REMOVES
873 && testOverlap2D(handle0, handle1, axis1, axis2)
874 #endif //USE_OVERLAP_TEST_ON_REMOVES
877 //this is done during the overlappingpairarray iteration/narrowphase collision
879 m_pairCache->removeOverlappingPair(handle0, handle1, dispatcher);
880 if (m_userPairCallback)
881 m_userPairCallback->removeOverlappingPair(handle0, handle1, dispatcher);
884 // update edge reference in other handle
885 pHandlePrev->m_minEdges[axis]++;
889 pHandlePrev->m_maxEdges[axis]++;
891 pHandleEdge->m_maxEdges[axis]--;
903 #ifdef DEBUG_BROADPHASE
904 debugPrintAxis(axis);
905 #endif //DEBUG_BROADPHASE
908 // sorting a max edge upwards can only ever *add* overlaps
909 template <typename BP_FP_INT_TYPE>
910 void btAxisSweep3Internal<BP_FP_INT_TYPE>::sortMaxUp(int axis, BP_FP_INT_TYPE edge, btDispatcher* /* dispatcher */, bool updateOverlaps)
912 Edge* pEdge = m_pEdges[axis] + edge;
913 Edge* pNext = pEdge + 1;
914 Handle* pHandleEdge = getHandle(pEdge->m_handle);
916 while (pNext->m_handle && (pEdge->m_pos >= pNext->m_pos))
918 Handle* pHandleNext = getHandle(pNext->m_handle);
920 const int axis1 = (1 << axis) & 3;
921 const int axis2 = (1 << axis1) & 3;
925 // if next edge is a minimum check the bounds and add an overlap if necessary
926 if (updateOverlaps && testOverlap2D(pHandleEdge, pHandleNext, axis1, axis2))
928 Handle* handle0 = getHandle(pEdge->m_handle);
929 Handle* handle1 = getHandle(pNext->m_handle);
930 m_pairCache->addOverlappingPair(handle0, handle1);
931 if (m_userPairCallback)
932 m_userPairCallback->addOverlappingPair(handle0, handle1);
935 // update edge reference in other handle
936 pHandleNext->m_minEdges[axis]--;
939 pHandleNext->m_maxEdges[axis]--;
941 pHandleEdge->m_maxEdges[axis]++;