btGImpactQuantizedBvh.cpp 13 KB

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