btGImpactBvh.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*! \file gim_box_set.h
  2. \author Francisco Leon Najera
  3. */
  4. /*
  5. This source file is part of GIMPACT Library.
  6. For the latest info, see http://gimpact.sourceforge.net/
  7. Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
  8. email: projectileman@yahoo.com
  9. This software is provided 'as-is', without any express or implied warranty.
  10. In no event will the authors be held liable for any damages arising from the use of this software.
  11. Permission is granted to anyone to use this software for any purpose,
  12. including commercial applications, and to alter it and redistribute it freely,
  13. subject to the following restrictions:
  14. 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.
  15. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "btGImpactBvh.h"
  19. #include "LinearMath/btQuickprof.h"
  20. #ifdef TRI_COLLISION_PROFILING
  21. btClock g_tree_clock;
  22. float g_accum_tree_collision_time = 0;
  23. int g_count_traversing = 0;
  24. void bt_begin_gim02_tree_time()
  25. {
  26. g_tree_clock.reset();
  27. }
  28. void bt_end_gim02_tree_time()
  29. {
  30. g_accum_tree_collision_time += g_tree_clock.getTimeMicroseconds();
  31. g_count_traversing++;
  32. }
  33. //! Gets the average time in miliseconds of tree collisions
  34. float btGImpactBvh::getAverageTreeCollisionTime()
  35. {
  36. if(g_count_traversing == 0) return 0;
  37. float avgtime = g_accum_tree_collision_time;
  38. avgtime /= (float)g_count_traversing;
  39. g_accum_tree_collision_time = 0;
  40. g_count_traversing = 0;
  41. return avgtime;
  42. // float avgtime = g_count_traversing;
  43. // g_count_traversing = 0;
  44. // return avgtime;
  45. }
  46. #endif //TRI_COLLISION_PROFILING
  47. /////////////////////// btBvhTree /////////////////////////////////
  48. int btBvhTree::_calc_splitting_axis(
  49. GIM_BVH_DATA_ARRAY & primitive_boxes, int startIndex, int endIndex)
  50. {
  51. int i;
  52. btVector3 means(btScalar(0.),btScalar(0.),btScalar(0.));
  53. btVector3 variance(btScalar(0.),btScalar(0.),btScalar(0.));
  54. int numIndices = endIndex-startIndex;
  55. for (i=startIndex;i<endIndex;i++)
  56. {
  57. btVector3 center = btScalar(0.5)*(primitive_boxes[i].m_bound.m_max +
  58. primitive_boxes[i].m_bound.m_min);
  59. means+=center;
  60. }
  61. means *= (btScalar(1.)/(btScalar)numIndices);
  62. for (i=startIndex;i<endIndex;i++)
  63. {
  64. btVector3 center = btScalar(0.5)*(primitive_boxes[i].m_bound.m_max +
  65. primitive_boxes[i].m_bound.m_min);
  66. btVector3 diff2 = center-means;
  67. diff2 = diff2 * diff2;
  68. variance += diff2;
  69. }
  70. variance *= (btScalar(1.)/ ((btScalar)numIndices-1) );
  71. return variance.maxAxis();
  72. }
  73. int btBvhTree::_sort_and_calc_splitting_index(
  74. GIM_BVH_DATA_ARRAY & primitive_boxes, int startIndex,
  75. int endIndex, int splitAxis)
  76. {
  77. int i;
  78. int splitIndex =startIndex;
  79. int numIndices = endIndex - startIndex;
  80. // average of centers
  81. btScalar splitValue = 0.0f;
  82. btVector3 means(btScalar(0.),btScalar(0.),btScalar(0.));
  83. for (i=startIndex;i<endIndex;i++)
  84. {
  85. btVector3 center = btScalar(0.5)*(primitive_boxes[i].m_bound.m_max +
  86. primitive_boxes[i].m_bound.m_min);
  87. means+=center;
  88. }
  89. means *= (btScalar(1.)/(btScalar)numIndices);
  90. splitValue = means[splitAxis];
  91. //sort leafNodes so all values larger then splitValue comes first, and smaller values start from 'splitIndex'.
  92. for (i=startIndex;i<endIndex;i++)
  93. {
  94. btVector3 center = btScalar(0.5)*(primitive_boxes[i].m_bound.m_max +
  95. primitive_boxes[i].m_bound.m_min);
  96. if (center[splitAxis] > splitValue)
  97. {
  98. //swap
  99. primitive_boxes.swap(i,splitIndex);
  100. //swapLeafNodes(i,splitIndex);
  101. splitIndex++;
  102. }
  103. }
  104. //if the splitIndex causes unbalanced trees, fix this by using the center in between startIndex and endIndex
  105. //otherwise the tree-building might fail due to stack-overflows in certain cases.
  106. //unbalanced1 is unsafe: it can cause stack overflows
  107. //bool unbalanced1 = ((splitIndex==startIndex) || (splitIndex == (endIndex-1)));
  108. //unbalanced2 should work too: always use center (perfect balanced trees)
  109. //bool unbalanced2 = true;
  110. //this should be safe too:
  111. int rangeBalancedIndices = numIndices/3;
  112. bool unbalanced = ((splitIndex<=(startIndex+rangeBalancedIndices)) || (splitIndex >=(endIndex-1-rangeBalancedIndices)));
  113. if (unbalanced)
  114. {
  115. splitIndex = startIndex+ (numIndices>>1);
  116. }
  117. btAssert(!((splitIndex==startIndex) || (splitIndex == (endIndex))));
  118. return splitIndex;
  119. }
  120. void btBvhTree::_build_sub_tree(GIM_BVH_DATA_ARRAY & primitive_boxes, int startIndex, int endIndex)
  121. {
  122. int curIndex = m_num_nodes;
  123. m_num_nodes++;
  124. btAssert((endIndex-startIndex)>0);
  125. if ((endIndex-startIndex)==1)
  126. {
  127. //We have a leaf node
  128. setNodeBound(curIndex,primitive_boxes[startIndex].m_bound);
  129. m_node_array[curIndex].setDataIndex(primitive_boxes[startIndex].m_data);
  130. return;
  131. }
  132. //calculate Best Splitting Axis and where to split it. Sort the incoming 'leafNodes' array within range 'startIndex/endIndex'.
  133. //split axis
  134. int splitIndex = _calc_splitting_axis(primitive_boxes,startIndex,endIndex);
  135. splitIndex = _sort_and_calc_splitting_index(
  136. primitive_boxes,startIndex,endIndex,
  137. splitIndex//split axis
  138. );
  139. //calc this node bounding box
  140. btAABB node_bound;
  141. node_bound.invalidate();
  142. for (int i=startIndex;i<endIndex;i++)
  143. {
  144. node_bound.merge(primitive_boxes[i].m_bound);
  145. }
  146. setNodeBound(curIndex,node_bound);
  147. //build left branch
  148. _build_sub_tree(primitive_boxes, startIndex, splitIndex );
  149. //build right branch
  150. _build_sub_tree(primitive_boxes, splitIndex ,endIndex);
  151. m_node_array[curIndex].setEscapeIndex(m_num_nodes - curIndex);
  152. }
  153. //! stackless build tree
  154. void btBvhTree::build_tree(
  155. GIM_BVH_DATA_ARRAY & primitive_boxes)
  156. {
  157. // initialize node count to 0
  158. m_num_nodes = 0;
  159. // allocate nodes
  160. m_node_array.resize(primitive_boxes.size()*2);
  161. _build_sub_tree(primitive_boxes, 0, primitive_boxes.size());
  162. }
  163. ////////////////////////////////////class btGImpactBvh
  164. void btGImpactBvh::refit()
  165. {
  166. int nodecount = getNodeCount();
  167. while(nodecount--)
  168. {
  169. if(isLeafNode(nodecount))
  170. {
  171. btAABB leafbox;
  172. m_primitive_manager->get_primitive_box(getNodeData(nodecount),leafbox);
  173. setNodeBound(nodecount,leafbox);
  174. }
  175. else
  176. {
  177. //const GIM_BVH_TREE_NODE * nodepointer = get_node_pointer(nodecount);
  178. //get left bound
  179. btAABB bound;
  180. bound.invalidate();
  181. btAABB temp_box;
  182. int child_node = getLeftNode(nodecount);
  183. if(child_node)
  184. {
  185. getNodeBound(child_node,temp_box);
  186. bound.merge(temp_box);
  187. }
  188. child_node = getRightNode(nodecount);
  189. if(child_node)
  190. {
  191. getNodeBound(child_node,temp_box);
  192. bound.merge(temp_box);
  193. }
  194. setNodeBound(nodecount,bound);
  195. }
  196. }
  197. }
  198. //! this rebuild the entire set
  199. void btGImpactBvh::buildSet()
  200. {
  201. //obtain primitive boxes
  202. GIM_BVH_DATA_ARRAY primitive_boxes;
  203. primitive_boxes.resize(m_primitive_manager->get_primitive_count());
  204. for (int i = 0;i<primitive_boxes.size() ;i++ )
  205. {
  206. m_primitive_manager->get_primitive_box(i,primitive_boxes[i].m_bound);
  207. primitive_boxes[i].m_data = i;
  208. }
  209. m_box_tree.build_tree(primitive_boxes);
  210. }
  211. //! returns the indices of the primitives in the m_primitive_manager
  212. bool btGImpactBvh::boxQuery(const btAABB & box, btAlignedObjectArray<int> & collided_results) const
  213. {
  214. int curIndex = 0;
  215. int numNodes = getNodeCount();
  216. while (curIndex < numNodes)
  217. {
  218. btAABB bound;
  219. getNodeBound(curIndex,bound);
  220. //catch bugs in tree data
  221. bool aabbOverlap = bound.has_collision(box);
  222. bool isleafnode = isLeafNode(curIndex);
  223. if (isleafnode && aabbOverlap)
  224. {
  225. collided_results.push_back(getNodeData(curIndex));
  226. }
  227. if (aabbOverlap || isleafnode)
  228. {
  229. //next subnode
  230. curIndex++;
  231. }
  232. else
  233. {
  234. //skip node
  235. curIndex+= getEscapeNodeIndex(curIndex);
  236. }
  237. }
  238. if(collided_results.size()>0) return true;
  239. return false;
  240. }
  241. //! returns the indices of the primitives in the m_primitive_manager
  242. bool btGImpactBvh::rayQuery(
  243. const btVector3 & ray_dir,const btVector3 & ray_origin ,
  244. btAlignedObjectArray<int> & collided_results) const
  245. {
  246. int curIndex = 0;
  247. int numNodes = getNodeCount();
  248. while (curIndex < numNodes)
  249. {
  250. btAABB bound;
  251. getNodeBound(curIndex,bound);
  252. //catch bugs in tree data
  253. bool aabbOverlap = bound.collide_ray(ray_origin,ray_dir);
  254. bool isleafnode = isLeafNode(curIndex);
  255. if (isleafnode && aabbOverlap)
  256. {
  257. collided_results.push_back(getNodeData( curIndex));
  258. }
  259. if (aabbOverlap || isleafnode)
  260. {
  261. //next subnode
  262. curIndex++;
  263. }
  264. else
  265. {
  266. //skip node
  267. curIndex+= getEscapeNodeIndex(curIndex);
  268. }
  269. }
  270. if(collided_results.size()>0) return true;
  271. return false;
  272. }
  273. SIMD_FORCE_INLINE bool _node_collision(
  274. btGImpactBvh * boxset0, btGImpactBvh * boxset1,
  275. const BT_BOX_BOX_TRANSFORM_CACHE & trans_cache_1to0,
  276. int node0 ,int node1, bool complete_primitive_tests)
  277. {
  278. btAABB box0;
  279. boxset0->getNodeBound(node0,box0);
  280. btAABB box1;
  281. boxset1->getNodeBound(node1,box1);
  282. return box0.overlapping_trans_cache(box1,trans_cache_1to0,complete_primitive_tests );
  283. // box1.appy_transform_trans_cache(trans_cache_1to0);
  284. // return box0.has_collision(box1);
  285. }
  286. //stackless recursive collision routine
  287. static void _find_collision_pairs_recursive(
  288. btGImpactBvh * boxset0, btGImpactBvh * boxset1,
  289. btPairSet * collision_pairs,
  290. const BT_BOX_BOX_TRANSFORM_CACHE & trans_cache_1to0,
  291. int node0, int node1, bool complete_primitive_tests)
  292. {
  293. if( _node_collision(
  294. boxset0,boxset1,trans_cache_1to0,
  295. node0,node1,complete_primitive_tests) ==false) return;//avoid colliding internal nodes
  296. if(boxset0->isLeafNode(node0))
  297. {
  298. if(boxset1->isLeafNode(node1))
  299. {
  300. // collision result
  301. collision_pairs->push_pair(
  302. boxset0->getNodeData(node0),boxset1->getNodeData(node1));
  303. return;
  304. }
  305. else
  306. {
  307. //collide left recursive
  308. _find_collision_pairs_recursive(
  309. boxset0,boxset1,
  310. collision_pairs,trans_cache_1to0,
  311. node0,boxset1->getLeftNode(node1),false);
  312. //collide right recursive
  313. _find_collision_pairs_recursive(
  314. boxset0,boxset1,
  315. collision_pairs,trans_cache_1to0,
  316. node0,boxset1->getRightNode(node1),false);
  317. }
  318. }
  319. else
  320. {
  321. if(boxset1->isLeafNode(node1))
  322. {
  323. //collide left recursive
  324. _find_collision_pairs_recursive(
  325. boxset0,boxset1,
  326. collision_pairs,trans_cache_1to0,
  327. boxset0->getLeftNode(node0),node1,false);
  328. //collide right recursive
  329. _find_collision_pairs_recursive(
  330. boxset0,boxset1,
  331. collision_pairs,trans_cache_1to0,
  332. boxset0->getRightNode(node0),node1,false);
  333. }
  334. else
  335. {
  336. //collide left0 left1
  337. _find_collision_pairs_recursive(
  338. boxset0,boxset1,
  339. collision_pairs,trans_cache_1to0,
  340. boxset0->getLeftNode(node0),boxset1->getLeftNode(node1),false);
  341. //collide left0 right1
  342. _find_collision_pairs_recursive(
  343. boxset0,boxset1,
  344. collision_pairs,trans_cache_1to0,
  345. boxset0->getLeftNode(node0),boxset1->getRightNode(node1),false);
  346. //collide right0 left1
  347. _find_collision_pairs_recursive(
  348. boxset0,boxset1,
  349. collision_pairs,trans_cache_1to0,
  350. boxset0->getRightNode(node0),boxset1->getLeftNode(node1),false);
  351. //collide right0 right1
  352. _find_collision_pairs_recursive(
  353. boxset0,boxset1,
  354. collision_pairs,trans_cache_1to0,
  355. boxset0->getRightNode(node0),boxset1->getRightNode(node1),false);
  356. }// else if node1 is not a leaf
  357. }// else if node0 is not a leaf
  358. }
  359. void btGImpactBvh::find_collision(btGImpactBvh * boxset0, const btTransform & trans0,
  360. btGImpactBvh * boxset1, const btTransform & trans1,
  361. btPairSet & collision_pairs)
  362. {
  363. if(boxset0->getNodeCount()==0 || boxset1->getNodeCount()==0 ) return;
  364. BT_BOX_BOX_TRANSFORM_CACHE trans_cache_1to0;
  365. trans_cache_1to0.calc_from_homogenic(trans0,trans1);
  366. #ifdef TRI_COLLISION_PROFILING
  367. bt_begin_gim02_tree_time();
  368. #endif //TRI_COLLISION_PROFILING
  369. _find_collision_pairs_recursive(
  370. boxset0,boxset1,
  371. &collision_pairs,trans_cache_1to0,0,0,true);
  372. #ifdef TRI_COLLISION_PROFILING
  373. bt_end_gim02_tree_time();
  374. #endif //TRI_COLLISION_PROFILING
  375. }