| 1 | /* | 
|---|
| 2 | Bullet Continuous Collision Detection and Physics Library | 
|---|
| 3 | Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/ | 
|---|
| 4 |  | 
|---|
| 5 | This software is provided 'as-is', without any express or implied warranty. | 
|---|
| 6 | In no event will the authors be held liable for any damages arising from the use of this software. | 
|---|
| 7 | Permission is granted to anyone to use this software for any purpose, | 
|---|
| 8 | including commercial applications, and to alter it and redistribute it freely, | 
|---|
| 9 | subject to the following restrictions: | 
|---|
| 10 |  | 
|---|
| 11 | 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. | 
|---|
| 12 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. | 
|---|
| 13 | 3. This notice may not be removed or altered from any source distribution. | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #ifndef BT_UNION_FIND_H | 
|---|
| 17 | #define BT_UNION_FIND_H | 
|---|
| 18 |  | 
|---|
| 19 | #include "LinearMath/btAlignedObjectArray.h" | 
|---|
| 20 |  | 
|---|
| 21 | #define USE_PATH_COMPRESSION 1 | 
|---|
| 22 |  | 
|---|
| 23 | ///see for discussion of static island optimizations by Vroonsh here: http://code.google.com/p/bullet/issues/detail?id=406 | 
|---|
| 24 | #define STATIC_SIMULATION_ISLAND_OPTIMIZATION 1 | 
|---|
| 25 |  | 
|---|
| 26 | struct  btElement | 
|---|
| 27 | { | 
|---|
| 28 | int     m_id; | 
|---|
| 29 | int     m_sz; | 
|---|
| 30 | }; | 
|---|
| 31 |  | 
|---|
| 32 | ///UnionFind calculates connected subsets | 
|---|
| 33 | // Implements weighted Quick Union with path compression | 
|---|
| 34 | // optimization: could use short ints instead of ints (halving memory, would limit the number of rigid bodies to 64k, sounds reasonable) | 
|---|
| 35 | class btUnionFind | 
|---|
| 36 | { | 
|---|
| 37 | private: | 
|---|
| 38 | btAlignedObjectArray<btElement> m_elements; | 
|---|
| 39 |  | 
|---|
| 40 | public: | 
|---|
| 41 |  | 
|---|
| 42 | btUnionFind(); | 
|---|
| 43 | ~btUnionFind(); | 
|---|
| 44 |  | 
|---|
| 45 |  | 
|---|
| 46 | //this is a special operation, destroying the content of btUnionFind. | 
|---|
| 47 | //it sorts the elements, based on island id, in order to make it easy to iterate over islands | 
|---|
| 48 | void    sortIslands(); | 
|---|
| 49 |  | 
|---|
| 50 | void  reset(int N); | 
|---|
| 51 |  | 
|---|
| 52 | SIMD_FORCE_INLINE int getNumElements() const | 
|---|
| 53 | { | 
|---|
| 54 | return int(m_elements.size()); | 
|---|
| 55 | } | 
|---|
| 56 | SIMD_FORCE_INLINE bool  isRoot(int x) const | 
|---|
| 57 | { | 
|---|
| 58 | return (x == m_elements[x].m_id); | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | btElement&    getElement(int index) | 
|---|
| 62 | { | 
|---|
| 63 | return m_elements[index]; | 
|---|
| 64 | } | 
|---|
| 65 | const btElement& getElement(int index) const | 
|---|
| 66 | { | 
|---|
| 67 | return m_elements[index]; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | void  allocate(int N); | 
|---|
| 71 | void  Free(); | 
|---|
| 72 |  | 
|---|
| 73 |  | 
|---|
| 74 |  | 
|---|
| 75 |  | 
|---|
| 76 | int find(int p, int q) | 
|---|
| 77 | { | 
|---|
| 78 | return (find(p) == find(q)); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | void unite(int p, int q) | 
|---|
| 82 | { | 
|---|
| 83 | int i = find(p), j = find(q); | 
|---|
| 84 | if (i == j) | 
|---|
| 85 | return; | 
|---|
| 86 |  | 
|---|
| 87 | #ifndef USE_PATH_COMPRESSION | 
|---|
| 88 | //weighted quick union, this keeps the 'trees' balanced, and keeps performance of unite O( log(n) ) | 
|---|
| 89 | if (m_elements[i].m_sz < m_elements[j].m_sz) | 
|---|
| 90 | { | 
|---|
| 91 | m_elements[i].m_id = j; m_elements[j].m_sz += m_elements[i].m_sz; | 
|---|
| 92 | } | 
|---|
| 93 | else | 
|---|
| 94 | { | 
|---|
| 95 | m_elements[j].m_id = i; m_elements[i].m_sz += m_elements[j].m_sz; | 
|---|
| 96 | } | 
|---|
| 97 | #else | 
|---|
| 98 | m_elements[i].m_id = j; m_elements[j].m_sz += m_elements[i].m_sz; | 
|---|
| 99 | #endif //USE_PATH_COMPRESSION | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | int find(int x) | 
|---|
| 103 | { | 
|---|
| 104 | //btAssert(x < m_N); | 
|---|
| 105 | //btAssert(x >= 0); | 
|---|
| 106 |  | 
|---|
| 107 | while (x != m_elements[x].m_id) | 
|---|
| 108 | { | 
|---|
| 109 | //not really a reason not to use path compression, and it flattens the trees/improves find performance dramatically | 
|---|
| 110 |  | 
|---|
| 111 | #ifdef USE_PATH_COMPRESSION | 
|---|
| 112 | const btElement* elementPtr = &m_elements[m_elements[x].m_id]; | 
|---|
| 113 | m_elements[x].m_id = elementPtr->m_id; | 
|---|
| 114 | x = elementPtr->m_id; | 
|---|
| 115 | #else// | 
|---|
| 116 | x = m_elements[x].m_id; | 
|---|
| 117 | #endif | 
|---|
| 118 | //btAssert(x < m_N); | 
|---|
| 119 | //btAssert(x >= 0); | 
|---|
| 120 |  | 
|---|
| 121 | } | 
|---|
| 122 | return x; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 |  | 
|---|
| 126 | }; | 
|---|
| 127 |  | 
|---|
| 128 |  | 
|---|
| 129 | #endif //BT_UNION_FIND_H | 
|---|