Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/bullet/BulletCollision/CollisionDispatch/btUnionFind.h @ 1963

Last change on this file since 1963 was 1963, checked in by rgrieder, 16 years ago

Added Bullet physics engine.

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