btGImpactQuantizedBvh.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #ifndef GIM_QUANTIZED_SET_H_INCLUDED
  2. #define GIM_QUANTIZED_SET_H_INCLUDED
  3. /*! \file btGImpactQuantizedBvh.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 "btGImpactBvh.h"
  21. #include "btQuantization.h"
  22. ///btQuantizedBvhNode is a compressed aabb node, 16 bytes.
  23. ///Node can be used for leafnode or internal node. Leafnodes can point to 32-bit triangle index (non-negative range).
  24. ATTRIBUTE_ALIGNED16 (struct) BT_QUANTIZED_BVH_NODE
  25. {
  26. //12 bytes
  27. unsigned short int m_quantizedAabbMin[3];
  28. unsigned short int m_quantizedAabbMax[3];
  29. //4 bytes
  30. int m_escapeIndexOrDataIndex;
  31. BT_QUANTIZED_BVH_NODE()
  32. {
  33. m_escapeIndexOrDataIndex = 0;
  34. }
  35. SIMD_FORCE_INLINE bool isLeafNode() const
  36. {
  37. //skipindex is negative (internal node), triangleindex >=0 (leafnode)
  38. return (m_escapeIndexOrDataIndex>=0);
  39. }
  40. SIMD_FORCE_INLINE int getEscapeIndex() const
  41. {
  42. //btAssert(m_escapeIndexOrDataIndex < 0);
  43. return -m_escapeIndexOrDataIndex;
  44. }
  45. SIMD_FORCE_INLINE void setEscapeIndex(int index)
  46. {
  47. m_escapeIndexOrDataIndex = -index;
  48. }
  49. SIMD_FORCE_INLINE int getDataIndex() const
  50. {
  51. //btAssert(m_escapeIndexOrDataIndex >= 0);
  52. return m_escapeIndexOrDataIndex;
  53. }
  54. SIMD_FORCE_INLINE void setDataIndex(int index)
  55. {
  56. m_escapeIndexOrDataIndex = index;
  57. }
  58. SIMD_FORCE_INLINE bool testQuantizedBoxOverlapp(
  59. unsigned short * quantizedMin,unsigned short * quantizedMax) const
  60. {
  61. if(m_quantizedAabbMin[0] > quantizedMax[0] ||
  62. m_quantizedAabbMax[0] < quantizedMin[0] ||
  63. m_quantizedAabbMin[1] > quantizedMax[1] ||
  64. m_quantizedAabbMax[1] < quantizedMin[1] ||
  65. m_quantizedAabbMin[2] > quantizedMax[2] ||
  66. m_quantizedAabbMax[2] < quantizedMin[2])
  67. {
  68. return false;
  69. }
  70. return true;
  71. }
  72. };
  73. class GIM_QUANTIZED_BVH_NODE_ARRAY:public btAlignedObjectArray<BT_QUANTIZED_BVH_NODE>
  74. {
  75. };
  76. //! Basic Box tree structure
  77. class btQuantizedBvhTree
  78. {
  79. protected:
  80. int m_num_nodes;
  81. GIM_QUANTIZED_BVH_NODE_ARRAY m_node_array;
  82. btAABB m_global_bound;
  83. btVector3 m_bvhQuantization;
  84. protected:
  85. void calc_quantization(GIM_BVH_DATA_ARRAY & primitive_boxes, btScalar boundMargin = btScalar(1.0) );
  86. int _sort_and_calc_splitting_index(
  87. GIM_BVH_DATA_ARRAY & primitive_boxes,
  88. int startIndex, int endIndex, int splitAxis);
  89. int _calc_splitting_axis(GIM_BVH_DATA_ARRAY & primitive_boxes, int startIndex, int endIndex);
  90. void _build_sub_tree(GIM_BVH_DATA_ARRAY & primitive_boxes, int startIndex, int endIndex);
  91. public:
  92. btQuantizedBvhTree()
  93. {
  94. m_num_nodes = 0;
  95. }
  96. //! prototype functions for box tree management
  97. //!@{
  98. void build_tree(GIM_BVH_DATA_ARRAY & primitive_boxes);
  99. SIMD_FORCE_INLINE void quantizePoint(
  100. unsigned short * quantizedpoint, const btVector3 & point) const
  101. {
  102. bt_quantize_clamp(quantizedpoint,point,m_global_bound.m_min,m_global_bound.m_max,m_bvhQuantization);
  103. }
  104. SIMD_FORCE_INLINE bool testQuantizedBoxOverlapp(
  105. int node_index,
  106. unsigned short * quantizedMin,unsigned short * quantizedMax) const
  107. {
  108. return m_node_array[node_index].testQuantizedBoxOverlapp(quantizedMin,quantizedMax);
  109. }
  110. SIMD_FORCE_INLINE void clearNodes()
  111. {
  112. m_node_array.clear();
  113. m_num_nodes = 0;
  114. }
  115. //! node count
  116. SIMD_FORCE_INLINE int getNodeCount() const
  117. {
  118. return m_num_nodes;
  119. }
  120. //! tells if the node is a leaf
  121. SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
  122. {
  123. return m_node_array[nodeindex].isLeafNode();
  124. }
  125. SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
  126. {
  127. return m_node_array[nodeindex].getDataIndex();
  128. }
  129. SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB & bound) const
  130. {
  131. bound.m_min = bt_unquantize(
  132. m_node_array[nodeindex].m_quantizedAabbMin,
  133. m_global_bound.m_min,m_bvhQuantization);
  134. bound.m_max = bt_unquantize(
  135. m_node_array[nodeindex].m_quantizedAabbMax,
  136. m_global_bound.m_min,m_bvhQuantization);
  137. }
  138. SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB & bound)
  139. {
  140. bt_quantize_clamp( m_node_array[nodeindex].m_quantizedAabbMin,
  141. bound.m_min,
  142. m_global_bound.m_min,
  143. m_global_bound.m_max,
  144. m_bvhQuantization);
  145. bt_quantize_clamp( m_node_array[nodeindex].m_quantizedAabbMax,
  146. bound.m_max,
  147. m_global_bound.m_min,
  148. m_global_bound.m_max,
  149. m_bvhQuantization);
  150. }
  151. SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
  152. {
  153. return nodeindex+1;
  154. }
  155. SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
  156. {
  157. if(m_node_array[nodeindex+1].isLeafNode()) return nodeindex+2;
  158. return nodeindex+1 + m_node_array[nodeindex+1].getEscapeIndex();
  159. }
  160. SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
  161. {
  162. return m_node_array[nodeindex].getEscapeIndex();
  163. }
  164. SIMD_FORCE_INLINE const BT_QUANTIZED_BVH_NODE * get_node_pointer(int index = 0) const
  165. {
  166. return &m_node_array[index];
  167. }
  168. //!@}
  169. };
  170. //! Structure for containing Boxes
  171. /*!
  172. This class offers an structure for managing a box tree of primitives.
  173. Requires a Primitive prototype (like btPrimitiveManagerBase )
  174. */
  175. class btGImpactQuantizedBvh
  176. {
  177. protected:
  178. btQuantizedBvhTree m_box_tree;
  179. btPrimitiveManagerBase * m_primitive_manager;
  180. protected:
  181. //stackless refit
  182. void refit();
  183. public:
  184. //! this constructor doesn't build the tree. you must call buildSet
  185. btGImpactQuantizedBvh()
  186. {
  187. m_primitive_manager = NULL;
  188. }
  189. //! this constructor doesn't build the tree. you must call buildSet
  190. btGImpactQuantizedBvh(btPrimitiveManagerBase * primitive_manager)
  191. {
  192. m_primitive_manager = primitive_manager;
  193. }
  194. SIMD_FORCE_INLINE btAABB getGlobalBox() const
  195. {
  196. btAABB totalbox;
  197. getNodeBound(0, totalbox);
  198. return totalbox;
  199. }
  200. SIMD_FORCE_INLINE void setPrimitiveManager(btPrimitiveManagerBase * primitive_manager)
  201. {
  202. m_primitive_manager = primitive_manager;
  203. }
  204. SIMD_FORCE_INLINE btPrimitiveManagerBase * getPrimitiveManager() const
  205. {
  206. return m_primitive_manager;
  207. }
  208. //! node manager prototype functions
  209. ///@{
  210. //! this attemps to refit the box set.
  211. SIMD_FORCE_INLINE void update()
  212. {
  213. refit();
  214. }
  215. //! this rebuild the entire set
  216. void buildSet();
  217. //! returns the indices of the primitives in the m_primitive_manager
  218. bool boxQuery(const btAABB & box, btAlignedObjectArray<int> & collided_results) const;
  219. //! returns the indices of the primitives in the m_primitive_manager
  220. SIMD_FORCE_INLINE bool boxQueryTrans(const btAABB & box,
  221. const btTransform & transform, btAlignedObjectArray<int> & collided_results) const
  222. {
  223. btAABB transbox=box;
  224. transbox.appy_transform(transform);
  225. return boxQuery(transbox,collided_results);
  226. }
  227. //! returns the indices of the primitives in the m_primitive_manager
  228. bool rayQuery(
  229. const btVector3 & ray_dir,const btVector3 & ray_origin ,
  230. btAlignedObjectArray<int> & collided_results) const;
  231. //! tells if this set has hierarcht
  232. SIMD_FORCE_INLINE bool hasHierarchy() const
  233. {
  234. return true;
  235. }
  236. //! tells if this set is a trimesh
  237. SIMD_FORCE_INLINE bool isTrimesh() const
  238. {
  239. return m_primitive_manager->is_trimesh();
  240. }
  241. //! node count
  242. SIMD_FORCE_INLINE int getNodeCount() const
  243. {
  244. return m_box_tree.getNodeCount();
  245. }
  246. //! tells if the node is a leaf
  247. SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
  248. {
  249. return m_box_tree.isLeafNode(nodeindex);
  250. }
  251. SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
  252. {
  253. return m_box_tree.getNodeData(nodeindex);
  254. }
  255. SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB & bound) const
  256. {
  257. m_box_tree.getNodeBound(nodeindex, bound);
  258. }
  259. SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB & bound)
  260. {
  261. m_box_tree.setNodeBound(nodeindex, bound);
  262. }
  263. SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
  264. {
  265. return m_box_tree.getLeftNode(nodeindex);
  266. }
  267. SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
  268. {
  269. return m_box_tree.getRightNode(nodeindex);
  270. }
  271. SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
  272. {
  273. return m_box_tree.getEscapeNodeIndex(nodeindex);
  274. }
  275. SIMD_FORCE_INLINE void getNodeTriangle(int nodeindex,btPrimitiveTriangle & triangle) const
  276. {
  277. m_primitive_manager->get_primitive_triangle(getNodeData(nodeindex),triangle);
  278. }
  279. SIMD_FORCE_INLINE const BT_QUANTIZED_BVH_NODE * get_node_pointer(int index = 0) const
  280. {
  281. return m_box_tree.get_node_pointer(index);
  282. }
  283. #ifdef TRI_COLLISION_PROFILING
  284. static float getAverageTreeCollisionTime();
  285. #endif //TRI_COLLISION_PROFILING
  286. static void find_collision(const btGImpactQuantizedBvh * boxset1, const btTransform & trans1,
  287. const btGImpactQuantizedBvh * boxset2, const btTransform & trans2,
  288. btPairSet & collision_pairs);
  289. };
  290. #endif // GIM_BOXPRUNING_H_INCLUDED