btGImpactBvh.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #ifndef GIM_BOX_SET_H_INCLUDED
  2. #define GIM_BOX_SET_H_INCLUDED
  3. /*! \file gim_box_set.h
  4. \author Francisco Leon Najera
  5. */
  6. /*
  7. This source file is part of GIMPACT Library.
  8. For the latest info, see http://gimpact.sourceforge.net/
  9. Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
  10. email: projectileman@yahoo.com
  11. This software is provided 'as-is', without any express or implied warranty.
  12. In no event will the authors be held liable for any damages arising from the use of this software.
  13. Permission is granted to anyone to use this software for any purpose,
  14. including commercial applications, and to alter it and redistribute it freely,
  15. subject to the following restrictions:
  16. 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.
  17. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  18. 3. This notice may not be removed or altered from any source distribution.
  19. */
  20. #include "bullet/LinearMath/btAlignedObjectArray.h"
  21. #include "btBoxCollision.h"
  22. #include "btTriangleShapeEx.h"
  23. //! Overlapping pair
  24. struct GIM_PAIR
  25. {
  26. int m_index1;
  27. int m_index2;
  28. GIM_PAIR()
  29. {}
  30. GIM_PAIR(const GIM_PAIR & p)
  31. {
  32. m_index1 = p.m_index1;
  33. m_index2 = p.m_index2;
  34. }
  35. GIM_PAIR(int index1, int index2)
  36. {
  37. m_index1 = index1;
  38. m_index2 = index2;
  39. }
  40. };
  41. //! A pairset array
  42. class btPairSet: public btAlignedObjectArray<GIM_PAIR>
  43. {
  44. public:
  45. btPairSet()
  46. {
  47. reserve(32);
  48. }
  49. inline void push_pair(int index1,int index2)
  50. {
  51. push_back(GIM_PAIR(index1,index2));
  52. }
  53. inline void push_pair_inv(int index1,int index2)
  54. {
  55. push_back(GIM_PAIR(index2,index1));
  56. }
  57. };
  58. ///GIM_BVH_DATA is an internal GIMPACT collision structure to contain axis aligned bounding box
  59. struct GIM_BVH_DATA
  60. {
  61. btAABB m_bound;
  62. int m_data;
  63. };
  64. //! Node Structure for trees
  65. class GIM_BVH_TREE_NODE
  66. {
  67. public:
  68. btAABB m_bound;
  69. protected:
  70. int m_escapeIndexOrDataIndex;
  71. public:
  72. GIM_BVH_TREE_NODE()
  73. {
  74. m_escapeIndexOrDataIndex = 0;
  75. }
  76. SIMD_FORCE_INLINE bool isLeafNode() const
  77. {
  78. //skipindex is negative (internal node), triangleindex >=0 (leafnode)
  79. return (m_escapeIndexOrDataIndex>=0);
  80. }
  81. SIMD_FORCE_INLINE int getEscapeIndex() const
  82. {
  83. //btAssert(m_escapeIndexOrDataIndex < 0);
  84. return -m_escapeIndexOrDataIndex;
  85. }
  86. SIMD_FORCE_INLINE void setEscapeIndex(int index)
  87. {
  88. m_escapeIndexOrDataIndex = -index;
  89. }
  90. SIMD_FORCE_INLINE int getDataIndex() const
  91. {
  92. //btAssert(m_escapeIndexOrDataIndex >= 0);
  93. return m_escapeIndexOrDataIndex;
  94. }
  95. SIMD_FORCE_INLINE void setDataIndex(int index)
  96. {
  97. m_escapeIndexOrDataIndex = index;
  98. }
  99. };
  100. class GIM_BVH_DATA_ARRAY:public btAlignedObjectArray<GIM_BVH_DATA>
  101. {
  102. };
  103. class GIM_BVH_TREE_NODE_ARRAY:public btAlignedObjectArray<GIM_BVH_TREE_NODE>
  104. {
  105. };
  106. //! Basic Box tree structure
  107. class btBvhTree
  108. {
  109. protected:
  110. int m_num_nodes;
  111. GIM_BVH_TREE_NODE_ARRAY m_node_array;
  112. protected:
  113. int _sort_and_calc_splitting_index(
  114. GIM_BVH_DATA_ARRAY & primitive_boxes,
  115. int startIndex, int endIndex, int splitAxis);
  116. int _calc_splitting_axis(GIM_BVH_DATA_ARRAY & primitive_boxes, int startIndex, int endIndex);
  117. void _build_sub_tree(GIM_BVH_DATA_ARRAY & primitive_boxes, int startIndex, int endIndex);
  118. public:
  119. btBvhTree()
  120. {
  121. m_num_nodes = 0;
  122. }
  123. //! prototype functions for box tree management
  124. //!@{
  125. void build_tree(GIM_BVH_DATA_ARRAY & primitive_boxes);
  126. SIMD_FORCE_INLINE void clearNodes()
  127. {
  128. m_node_array.clear();
  129. m_num_nodes = 0;
  130. }
  131. //! node count
  132. SIMD_FORCE_INLINE int getNodeCount() const
  133. {
  134. return m_num_nodes;
  135. }
  136. //! tells if the node is a leaf
  137. SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
  138. {
  139. return m_node_array[nodeindex].isLeafNode();
  140. }
  141. SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
  142. {
  143. return m_node_array[nodeindex].getDataIndex();
  144. }
  145. SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB & bound) const
  146. {
  147. bound = m_node_array[nodeindex].m_bound;
  148. }
  149. SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB & bound)
  150. {
  151. m_node_array[nodeindex].m_bound = bound;
  152. }
  153. SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
  154. {
  155. return nodeindex+1;
  156. }
  157. SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
  158. {
  159. if(m_node_array[nodeindex+1].isLeafNode()) return nodeindex+2;
  160. return nodeindex+1 + m_node_array[nodeindex+1].getEscapeIndex();
  161. }
  162. SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
  163. {
  164. return m_node_array[nodeindex].getEscapeIndex();
  165. }
  166. SIMD_FORCE_INLINE const GIM_BVH_TREE_NODE * get_node_pointer(int index = 0) const
  167. {
  168. return &m_node_array[index];
  169. }
  170. //!@}
  171. };
  172. //! Prototype Base class for primitive classification
  173. /*!
  174. This class is a wrapper for primitive collections.
  175. This tells relevant info for the Bounding Box set classes, which take care of space classification.
  176. This class can manage Compound shapes and trimeshes, and if it is managing trimesh then the Hierarchy Bounding Box classes will take advantage of primitive Vs Box overlapping tests for getting optimal results and less Per Box compairisons.
  177. */
  178. class btPrimitiveManagerBase
  179. {
  180. public:
  181. virtual ~btPrimitiveManagerBase() {}
  182. //! determines if this manager consist on only triangles, which special case will be optimized
  183. virtual bool is_trimesh() const = 0;
  184. virtual int get_primitive_count() const = 0;
  185. virtual void get_primitive_box(int prim_index ,btAABB & primbox) const = 0;
  186. //! retrieves only the points of the triangle, and the collision margin
  187. virtual void get_primitive_triangle(int prim_index,btPrimitiveTriangle & triangle) const= 0;
  188. };
  189. //! Structure for containing Boxes
  190. /*!
  191. This class offers an structure for managing a box tree of primitives.
  192. Requires a Primitive prototype (like btPrimitiveManagerBase )
  193. */
  194. class btGImpactBvh
  195. {
  196. protected:
  197. btBvhTree m_box_tree;
  198. btPrimitiveManagerBase * m_primitive_manager;
  199. protected:
  200. //stackless refit
  201. void refit();
  202. public:
  203. //! this constructor doesn't build the tree. you must call buildSet
  204. btGImpactBvh()
  205. {
  206. m_primitive_manager = NULL;
  207. }
  208. //! this constructor doesn't build the tree. you must call buildSet
  209. btGImpactBvh(btPrimitiveManagerBase * primitive_manager)
  210. {
  211. m_primitive_manager = primitive_manager;
  212. }
  213. SIMD_FORCE_INLINE btAABB getGlobalBox() const
  214. {
  215. btAABB totalbox;
  216. getNodeBound(0, totalbox);
  217. return totalbox;
  218. }
  219. SIMD_FORCE_INLINE void setPrimitiveManager(btPrimitiveManagerBase * primitive_manager)
  220. {
  221. m_primitive_manager = primitive_manager;
  222. }
  223. SIMD_FORCE_INLINE btPrimitiveManagerBase * getPrimitiveManager() const
  224. {
  225. return m_primitive_manager;
  226. }
  227. //! node manager prototype functions
  228. ///@{
  229. //! this attemps to refit the box set.
  230. SIMD_FORCE_INLINE void update()
  231. {
  232. refit();
  233. }
  234. //! this rebuild the entire set
  235. void buildSet();
  236. //! returns the indices of the primitives in the m_primitive_manager
  237. bool boxQuery(const btAABB & box, btAlignedObjectArray<int> & collided_results) const;
  238. //! returns the indices of the primitives in the m_primitive_manager
  239. SIMD_FORCE_INLINE bool boxQueryTrans(const btAABB & box,
  240. const btTransform & transform, btAlignedObjectArray<int> & collided_results) const
  241. {
  242. btAABB transbox=box;
  243. transbox.appy_transform(transform);
  244. return boxQuery(transbox,collided_results);
  245. }
  246. //! returns the indices of the primitives in the m_primitive_manager
  247. bool rayQuery(
  248. const btVector3 & ray_dir,const btVector3 & ray_origin ,
  249. btAlignedObjectArray<int> & collided_results) const;
  250. //! tells if this set has hierarcht
  251. SIMD_FORCE_INLINE bool hasHierarchy() const
  252. {
  253. return true;
  254. }
  255. //! tells if this set is a trimesh
  256. SIMD_FORCE_INLINE bool isTrimesh() const
  257. {
  258. return m_primitive_manager->is_trimesh();
  259. }
  260. //! node count
  261. SIMD_FORCE_INLINE int getNodeCount() const
  262. {
  263. return m_box_tree.getNodeCount();
  264. }
  265. //! tells if the node is a leaf
  266. SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
  267. {
  268. return m_box_tree.isLeafNode(nodeindex);
  269. }
  270. SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
  271. {
  272. return m_box_tree.getNodeData(nodeindex);
  273. }
  274. SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB & bound) const
  275. {
  276. m_box_tree.getNodeBound(nodeindex, bound);
  277. }
  278. SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB & bound)
  279. {
  280. m_box_tree.setNodeBound(nodeindex, bound);
  281. }
  282. SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
  283. {
  284. return m_box_tree.getLeftNode(nodeindex);
  285. }
  286. SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
  287. {
  288. return m_box_tree.getRightNode(nodeindex);
  289. }
  290. SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
  291. {
  292. return m_box_tree.getEscapeNodeIndex(nodeindex);
  293. }
  294. SIMD_FORCE_INLINE void getNodeTriangle(int nodeindex,btPrimitiveTriangle & triangle) const
  295. {
  296. m_primitive_manager->get_primitive_triangle(getNodeData(nodeindex),triangle);
  297. }
  298. SIMD_FORCE_INLINE const GIM_BVH_TREE_NODE * get_node_pointer(int index = 0) const
  299. {
  300. return m_box_tree.get_node_pointer(index);
  301. }
  302. #ifdef TRI_COLLISION_PROFILING
  303. static float getAverageTreeCollisionTime();
  304. #endif //TRI_COLLISION_PROFILING
  305. static void find_collision(btGImpactBvh * boxset1, const btTransform & trans1,
  306. btGImpactBvh * boxset2, const btTransform & trans2,
  307. btPairSet & collision_pairs);
  308. };
  309. #endif // GIM_BOXPRUNING_H_INCLUDED