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#ifndef BT_MULTI_SAP_BROADPHASE
16#define BT_MULTI_SAP_BROADPHASE
17
18#include "btBroadphaseInterface.h"
19#include "LinearMath/btAlignedObjectArray.h"
20#include "btOverlappingPairCache.h"
21
22
23class btBroadphaseInterface;
24class btSimpleBroadphase;
25
26
27typedef btAlignedObjectArray<btBroadphaseInterface*> btSapBroadphaseArray;
28
29///The btMultiSapBroadphase is a research project, not recommended to use in production. Use btAxisSweep3 or btDbvtBroadphase instead.
30///The btMultiSapBroadphase is a broadphase that contains multiple SAP broadphases.
31///The user can add SAP broadphases that cover the world. A btBroadphaseProxy can be in multiple child broadphases at the same time.
32///A btQuantizedBvh acceleration structures finds overlapping SAPs for each btBroadphaseProxy.
33///See http://www.continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=328
34///and http://www.continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=1329
35class btMultiSapBroadphase :public btBroadphaseInterface
36{
37	btSapBroadphaseArray	m_sapBroadphases;
38
39	btSimpleBroadphase*		m_simpleBroadphase;
40
41	btOverlappingPairCache*	m_overlappingPairs;
42
43	class btQuantizedBvh*			m_optimizedAabbTree;
44
45
46	bool					m_ownsPairCache;
47
48	btOverlapFilterCallback*	m_filterCallback;
49
50	int			m_invalidPair;
51
52	struct	btBridgeProxy
53	{
54		btBroadphaseProxy*		m_childProxy;
55		btBroadphaseInterface*	m_childBroadphase;
56	};
57
58
59public:
60
61	struct	btMultiSapProxy	: public btBroadphaseProxy
62	{
63
64		///array with all the entries that this proxy belongs to
65		btAlignedObjectArray<btBridgeProxy*> m_bridgeProxies;
66		btVector3	m_aabbMin;
67		btVector3	m_aabbMax;
68
69		int	m_shapeType;
70
71/*		void*	m_userPtr;
72		short int	m_collisionFilterGroup;
73		short int	m_collisionFilterMask;
74*/
75		btMultiSapProxy(const btVector3& aabbMin,  const btVector3& aabbMax,int shapeType,void* userPtr, short int collisionFilterGroup,short int collisionFilterMask)
76			:btBroadphaseProxy(aabbMin,aabbMax,userPtr,collisionFilterGroup,collisionFilterMask),
77			m_aabbMin(aabbMin),
78			m_aabbMax(aabbMax),
79			m_shapeType(shapeType)
80		{
81			m_multiSapParentProxy =this;
82		}
83
84
85	};
86
87protected:
88
89
90	btAlignedObjectArray<btMultiSapProxy*> m_multiSapProxies;
91
92public:
93
94	btMultiSapBroadphase(int maxProxies = 16384,btOverlappingPairCache* pairCache=0);
95
96
97	btSapBroadphaseArray&	getBroadphaseArray()
98	{
99		return m_sapBroadphases;
100	}
101
102	const btSapBroadphaseArray&	getBroadphaseArray() const
103	{
104		return m_sapBroadphases;
105	}
106
107	virtual ~btMultiSapBroadphase();
108
109	virtual btBroadphaseProxy*	createProxy(  const btVector3& aabbMin,  const btVector3& aabbMax,int shapeType,void* userPtr, short int collisionFilterGroup,short int collisionFilterMask, btDispatcher* dispatcher,void* multiSapProxy);
110	virtual void	destroyProxy(btBroadphaseProxy* proxy,btDispatcher* dispatcher);
111	virtual void	setAabb(btBroadphaseProxy* proxy,const btVector3& aabbMin,const btVector3& aabbMax, btDispatcher* dispatcher);
112	virtual void	getAabb(btBroadphaseProxy* proxy,btVector3& aabbMin, btVector3& aabbMax ) const;
113
114	virtual void	rayTest(const btVector3& rayFrom,const btVector3& rayTo, btBroadphaseRayCallback& rayCallback,const btVector3& aabbMin=btVector3(0,0,0),const btVector3& aabbMax=btVector3(0,0,0));
115
116	void	addToChildBroadphase(btMultiSapProxy* parentMultiSapProxy, btBroadphaseProxy* childProxy, btBroadphaseInterface*	childBroadphase);
117
118	///calculateOverlappingPairs is optional: incremental algorithms (sweep and prune) might do it during the set aabb
119	virtual void	calculateOverlappingPairs(btDispatcher* dispatcher);
120
121	bool	testAabbOverlap(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1);
122
123	virtual	btOverlappingPairCache*	getOverlappingPairCache()
124	{
125		return m_overlappingPairs;
126	}
127	virtual	const btOverlappingPairCache*	getOverlappingPairCache() const
128	{
129		return m_overlappingPairs;
130	}
131
132	///getAabb returns the axis aligned bounding box in the 'global' coordinate frame
133	///will add some transform later
134	virtual void getBroadphaseAabb(btVector3& aabbMin,btVector3& aabbMax) const
135	{
136		aabbMin.setValue(-BT_LARGE_FLOAT,-BT_LARGE_FLOAT,-BT_LARGE_FLOAT);
137		aabbMax.setValue(BT_LARGE_FLOAT,BT_LARGE_FLOAT,BT_LARGE_FLOAT);
138	}
139
140	void	buildTree(const btVector3& bvhAabbMin,const btVector3& bvhAabbMax);
141
142	virtual void	printStats();
143
144	void quicksort (btBroadphasePairArray& a, int lo, int hi);
145
146	///reset broadphase internal structures, to ensure determinism/reproducability
147	virtual void resetPool(btDispatcher* dispatcher);
148
149};
150
151#endif //BT_MULTI_SAP_BROADPHASE
152