btGImpactQuantizedBvh.h 7.9 KB

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