2
0

OPC_SweepAndPrune.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /*
  3. * OPCODE - Optimized Collision Detection
  4. * Copyright (C) 2001 Pierre Terdiman
  5. * Homepage: http://www.codercorner.com/Opcode.htm
  6. */
  7. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. /**
  10. * Contains an implementation of the sweep-and-prune algorithm (moved from Z-Collide)
  11. * \file OPC_SweepAndPrune.h
  12. * \author Pierre Terdiman
  13. * \date January, 29, 2000
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. // Include Guard
  18. #ifndef __OPC_SWEEPANDPRUNE_H__
  19. #define __OPC_SWEEPANDPRUNE_H__
  20. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21. /**
  22. * User-callback, called by OPCODE for each colliding pairs.
  23. * \param id0 [in] id of colliding object
  24. * \param id1 [in] id of colliding object
  25. * \param user_data [in] user-defined data
  26. * \return TRUE to continue enumeration
  27. */
  28. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  29. typedef BOOL (*PairCallback) (udword id0, udword id1, void* user_data);
  30. class SAP_Element;
  31. class SAP_EndPoint;
  32. class SAP_Box;
  33. class OPCODE_API SAP_PairData
  34. {
  35. public:
  36. SAP_PairData();
  37. ~SAP_PairData();
  38. bool Init(udword nb_objects);
  39. void AddPair(udword id1, udword id2);
  40. void RemovePair(udword id1, udword id2);
  41. void DumpPairs(Pairs& pairs) const;
  42. void DumpPairs(PairCallback callback, void* user_data) const;
  43. private:
  44. udword mNbElements; //!< Total number of elements in the pool
  45. udword mNbUsedElements; //!< Number of used elements
  46. SAP_Element* mElementPool; //!< Array of mNbElements elements
  47. SAP_Element* mFirstFree; //!< First free element in the pool
  48. udword mNbObjects; //!< Max number of objects we can handle
  49. SAP_Element** mArray; //!< Pointers to pool
  50. // Internal methods
  51. SAP_Element* GetFreeElem(udword id, SAP_Element* next, udword* remap=null);
  52. inline_ void FreeElem(SAP_Element* elem);
  53. void Release();
  54. };
  55. class OPCODE_API SweepAndPrune
  56. {
  57. public:
  58. SweepAndPrune();
  59. ~SweepAndPrune();
  60. bool Init(udword nb_objects, const AABB** boxes);
  61. bool UpdateObject(udword i, const AABB& box);
  62. void GetPairs(Pairs& pairs) const;
  63. void GetPairs(PairCallback callback, void* user_data) const;
  64. private:
  65. SAP_PairData mPairs;
  66. udword mNbObjects;
  67. SAP_Box* mBoxes;
  68. SAP_EndPoint* mList[3];
  69. // Internal methods
  70. bool CheckListsIntegrity();
  71. };
  72. #endif //__OPC_SWEEPANDPRUNE_H__