btUnionFind.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #ifndef BT_UNION_FIND_H
  14. #define BT_UNION_FIND_H
  15. #include "LinearMath/btAlignedObjectArray.h"
  16. #define USE_PATH_COMPRESSION 1
  17. ///see for discussion of static island optimizations by Vroonsh here: http://code.google.com/p/bullet/issues/detail?id=406
  18. #define STATIC_SIMULATION_ISLAND_OPTIMIZATION 1
  19. struct btElement
  20. {
  21. int m_id;
  22. int m_sz;
  23. };
  24. ///UnionFind calculates connected subsets
  25. // Implements weighted Quick Union with path compression
  26. // optimization: could use short ints instead of ints (halving memory, would limit the number of rigid bodies to 64k, sounds reasonable)
  27. class btUnionFind
  28. {
  29. private:
  30. btAlignedObjectArray<btElement> m_elements;
  31. public:
  32. btUnionFind();
  33. ~btUnionFind();
  34. //this is a special operation, destroying the content of btUnionFind.
  35. //it sorts the elements, based on island id, in order to make it easy to iterate over islands
  36. void sortIslands();
  37. void reset(int N);
  38. SIMD_FORCE_INLINE int getNumElements() const
  39. {
  40. return int(m_elements.size());
  41. }
  42. SIMD_FORCE_INLINE bool isRoot(int x) const
  43. {
  44. return (x == m_elements[x].m_id);
  45. }
  46. btElement& getElement(int index)
  47. {
  48. return m_elements[index];
  49. }
  50. const btElement& getElement(int index) const
  51. {
  52. return m_elements[index];
  53. }
  54. void allocate(int N);
  55. void Free();
  56. int find(int p, int q)
  57. {
  58. return (find(p) == find(q));
  59. }
  60. void unite(int p, int q)
  61. {
  62. int i = find(p), j = find(q);
  63. if (i == j)
  64. return;
  65. #ifndef USE_PATH_COMPRESSION
  66. //weighted quick union, this keeps the 'trees' balanced, and keeps performance of unite O( log(n) )
  67. if (m_elements[i].m_sz < m_elements[j].m_sz)
  68. {
  69. m_elements[i].m_id = j;
  70. m_elements[j].m_sz += m_elements[i].m_sz;
  71. }
  72. else
  73. {
  74. m_elements[j].m_id = i;
  75. m_elements[i].m_sz += m_elements[j].m_sz;
  76. }
  77. #else
  78. m_elements[i].m_id = j;
  79. m_elements[j].m_sz += m_elements[i].m_sz;
  80. #endif //USE_PATH_COMPRESSION
  81. }
  82. int find(int x)
  83. {
  84. //btAssert(x < m_N);
  85. //btAssert(x >= 0);
  86. while (x != m_elements[x].m_id)
  87. {
  88. //not really a reason not to use path compression, and it flattens the trees/improves find performance dramatically
  89. #ifdef USE_PATH_COMPRESSION
  90. const btElement* elementPtr = &m_elements[m_elements[x].m_id];
  91. m_elements[x].m_id = elementPtr->m_id;
  92. x = elementPtr->m_id;
  93. #else //
  94. x = m_elements[x].m_id;
  95. #endif
  96. //btAssert(x < m_N);
  97. //btAssert(x >= 0);
  98. }
  99. return x;
  100. }
  101. };
  102. #endif //BT_UNION_FIND_H