Bullet Collision Detection & Physics Library: btUnionFind.h Source File

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btUnionFind.h
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
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 BT_UNION_FIND_H
17#define BT_UNION_FIND_H
18
19#include "LinearMath/btAlignedObjectArray.h"
20
21#define USE_PATH_COMPRESSION 1
22
24#define STATIC_SIMULATION_ISLAND_OPTIMIZATION 1
25
26 struct btElement
27{
28 int m_id;
29 int m_sz;
30};
31
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{
37private:
38 btAlignedObjectArray<btElement> m_elements;
39
40public:
41 btUnionFind();
42 ~btUnionFind();
43
44 //this is a special operation, destroying the content of btUnionFind.
45 //it sorts the elements, based on island id, in order to make it easy to iterate over islands
46 void sortIslands();
47
48 void reset(int N);
49
50 SIMD_FORCE_INLINE int getNumElements() const
51 {
52 return int(m_elements.size());
53 }
54 SIMD_FORCE_INLINE bool isRoot(int x) const
55 {
56 return (x == m_elements[x].m_id);
57 }
58
59 btElement& getElement(int index)
60 {
61 return m_elements[index];
62 }
63 const btElement& getElement(int index) const
64 {
65 return m_elements[index];
66 }
67
68 void allocate(int N);
69 void Free();
70
71 int find(int p, int q)
72 {
73 return (find(p) == find(q));
74 }
75
76 void unite(int p, int q)
77 {
78 int i = find(p), j = find(q);
79 if (i == j)
80 return;
81
82#ifndef USE_PATH_COMPRESSION
83 //weighted quick union, this keeps the 'trees' balanced, and keeps performance of unite O( log(n) )
84 if (m_elements[i].m_sz < m_elements[j].m_sz)
85 {
86 m_elements[i].m_id = j;
87 m_elements[j].m_sz += m_elements[i].m_sz;
88 }
89 else
90 {
91 m_elements[j].m_id = i;
92 m_elements[i].m_sz += m_elements[j].m_sz;
93 }
94#else
95 m_elements[i].m_id = j;
96 m_elements[j].m_sz += m_elements[i].m_sz;
97#endif //USE_PATH_COMPRESSION
98 }
99
100 int find(int x)
101 {
102 //btAssert(x < m_N);
103 //btAssert(x >= 0);
104
105 while (x != m_elements[x].m_id)
106 {
107 //not really a reason not to use path compression, and it flattens the trees/improves find performance dramatically
108
109#ifdef USE_PATH_COMPRESSION
110 const btElement* elementPtr = &m_elements[m_elements[x].m_id];
111 m_elements[x].m_id = elementPtr->m_id;
112 x = elementPtr->m_id;
113#else //
114 x = m_elements[x].m_id;
115#endif
116 //btAssert(x < m_N);
117 //btAssert(x >= 0);
118 }
119 return x;
120 }
121};
122
123#endif //BT_UNION_FIND_H
btAlignedObjectArray.h
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition btScalar.h:98
btAlignedObjectArray
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
Definition btAlignedObjectArray.h:46
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition btAlignedObjectArray.h:142
btUnionFind
UnionFind calculates connected subsets.
Definition btUnionFind.h:36
btUnionFind::reset
void reset(int N)
Definition btUnionFind.cpp:36
btUnionFind::find
int find(int x)
Definition btUnionFind.h:100
btUnionFind::btUnionFind
btUnionFind()
Definition btUnionFind.cpp:23
btUnionFind::getNumElements
int getNumElements() const
Definition btUnionFind.h:50
btUnionFind::Free
void Free()
Definition btUnionFind.cpp:31
btUnionFind::unite
void unite(int p, int q)
Definition btUnionFind.h:76
btUnionFind::allocate
void allocate(int N)
Definition btUnionFind.cpp:27
btUnionFind::getElement
btElement & getElement(int index)
Definition btUnionFind.h:59
btUnionFind::~btUnionFind
~btUnionFind()
Definition btUnionFind.cpp:18
btUnionFind::sortIslands
void sortIslands()
this is a special operation, destroying the content of btUnionFind.
Definition btUnionFind.cpp:58
btUnionFind::isRoot
bool isRoot(int x) const
Definition btUnionFind.h:54
btUnionFind::m_elements
btAlignedObjectArray< btElement > m_elements
Definition btUnionFind.h:38
btUnionFind::find
int find(int p, int q)
Definition btUnionFind.h:71
btUnionFind::getElement
const btElement & getElement(int index) const
Definition btUnionFind.h:63
btElement
Definition btUnionFind.h:27
btElement::m_id
int m_id
Definition btUnionFind.h:28
btElement::m_sz
int m_sz
Definition btUnionFind.h:29