Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / Gpu2dDemo / btGpuDemoPairCache.h
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 CUDA_DEMO_PAIR_CACHE_H
17 #define CUDA_DEMO_PAIR_CACHE_H
18
19
20 #include "BulletCollision/BroadphaseCollision/btBroadphaseInterface.h"
21 #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
22 #include "BulletCollision/BroadphaseCollision/btOverlappingPairCallback.h"
23 #include "BulletCollision/BroadphaseCollision/btOverlappingPairCache.h"
24 #include "LinearMath/btAlignedObjectArray.h"
25
26 class btGpuDemoPairCache : public btNullPairCache
27 {
28 public:
29         int             m_maxProxies;
30         int             m_maxNeighbors;
31         int*    m_hNeighbors;
32         int             m_numPairs;
33         int             m_numSmallProxies;
34         int             m_maxSmallProxies;
35
36         btGpuDemoPairCache(int maxProxies, int maxNeighbors, int maxSmallProxies)
37         {
38                 m_maxProxies = maxProxies;
39                 m_maxNeighbors = maxNeighbors;
40                 m_maxSmallProxies = maxSmallProxies;
41                 int sz = maxProxies * maxNeighbors;
42                 m_hNeighbors = new int [sz];
43                 reset();
44         }
45
46         ~btGpuDemoPairCache()
47         {
48                 delete [] m_hNeighbors;
49         }
50         
51         void reset(void)
52         {
53                 int sz = m_maxProxies * m_maxNeighbors;
54                 for(int i = 0; i < sz; i++)
55                 {
56                         m_hNeighbors[i] = -1;
57                 }
58                 m_numPairs = 0;
59         }
60
61         virtual int getNumOverlappingPairs() const
62         {
63                 return  m_numPairs;
64                 //return 0; // to skip btSimulationIslandManager::findUnions()
65         }
66
67         virtual void    processAllOverlappingPairs(btOverlapCallback* callback, btDispatcher* dispatcher);
68
69         virtual btBroadphasePair*       addOverlappingPair(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1)
70         {
71                 int id0 = proxy0->m_uniqueId - 2;
72                 int id1 = proxy1->m_uniqueId - 2;
73                 if(id0 >= m_maxSmallProxies)
74                 {
75                         id0 -= m_maxSmallProxies - m_numSmallProxies;
76                 }
77                 if(id1 >= m_maxSmallProxies)
78                 {
79                         id1 -= m_maxSmallProxies - m_numSmallProxies;
80                 }
81                 if(id0 > id1)
82                 {
83                         int tmp = id0; id0 = id1; id1 = tmp;
84                 }
85                 int offs = id0 * m_maxNeighbors;
86                 int i;
87                 for(i = 0; i < m_maxNeighbors; i++)
88                 {
89                         if(m_hNeighbors[offs + i] < 0)
90                         {
91                                 m_hNeighbors[offs + i] = id1;
92                                 m_numPairs++;
93                                 break;
94                         }
95                 }
96                 // btAssert(i < m_maxNeighbors);
97                 return 0;
98         }
99
100         virtual void*   removeOverlappingPair(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1,btDispatcher* /*dispatcher*/)
101         {
102                 int id0 = proxy0->m_uniqueId - 2;
103                 int id1 = proxy1->m_uniqueId - 2;
104                 if(id0 >= m_maxSmallProxies)
105                 {
106                         id0 -= m_maxSmallProxies - m_numSmallProxies;
107                 }
108                 if(id1 >= m_maxSmallProxies)
109                 {
110                         id1 -= m_maxSmallProxies - m_numSmallProxies;
111                 }
112                 if(id0 > id1)
113                 {
114                         int tmp = id0; id0 = id1; id1 = tmp;
115                 }
116                 int offs = id0 * m_maxNeighbors;
117                 int i;
118                 for(i = 0; i < m_maxNeighbors; i++)
119                 {
120                         if(m_hNeighbors[offs + i] == id1)
121                         {
122                                 m_hNeighbors[offs + i] = -1;
123                                 m_numPairs--;
124                                 break;
125                         }
126                 }
127 //              btAssert(i < m_maxNeighbors);
128                 return 0;
129         }
130 };
131
132 extern void cudaDemoNearCallback(btBroadphasePair& collisionPair, btCollisionDispatcher& dispatcher, const btDispatcherInfo& dispatchInfo);
133
134 #endif //CUDA_DEMO_PAIR_CACHE_H
135
136