gim_box_set.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of GIMPACT Library.
  4. For the latest info, see http://gimpact.sourceforge.net/
  5. Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
  6. email: projectileman@yahoo.com
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of EITHER:
  9. (1) The GNU Lesser General Public License as published by the Free
  10. Software Foundation; either version 2.1 of the License, or (at
  11. your option) any later version. The text of the GNU Lesser
  12. General Public License is included with this library in the
  13. file GIMPACT-LICENSE-LGPL.TXT.
  14. (2) The BSD-style license that is included with this library in
  15. the file GIMPACT-LICENSE-BSD.TXT.
  16. (3) The zlib/libpng license that is included with this library in
  17. the file GIMPACT-LICENSE-ZLIB.TXT.
  18. This library is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
  21. GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "gim_box_set.h"
  25. GUINT GIM_BOX_TREE::_calc_splitting_axis(
  26. gim_array<GIM_AABB_DATA> & primitive_boxes, GUINT startIndex, GUINT endIndex)
  27. {
  28. GUINT i;
  29. btVector3 means(btScalar(0.),btScalar(0.),btScalar(0.));
  30. btVector3 variance(btScalar(0.),btScalar(0.),btScalar(0.));
  31. GUINT numIndices = endIndex-startIndex;
  32. for (i=startIndex;i<endIndex;i++)
  33. {
  34. btVector3 center = btScalar(0.5)*(primitive_boxes[i].m_bound.m_max +
  35. primitive_boxes[i].m_bound.m_min);
  36. means+=center;
  37. }
  38. means *= (btScalar(1.)/(btScalar)numIndices);
  39. for (i=startIndex;i<endIndex;i++)
  40. {
  41. btVector3 center = btScalar(0.5)*(primitive_boxes[i].m_bound.m_max +
  42. primitive_boxes[i].m_bound.m_min);
  43. btVector3 diff2 = center-means;
  44. diff2 = diff2 * diff2;
  45. variance += diff2;
  46. }
  47. variance *= (btScalar(1.)/ ((btScalar)numIndices-1) );
  48. return variance.maxAxis();
  49. }
  50. GUINT GIM_BOX_TREE::_sort_and_calc_splitting_index(
  51. gim_array<GIM_AABB_DATA> & primitive_boxes, GUINT startIndex,
  52. GUINT endIndex, GUINT splitAxis)
  53. {
  54. GUINT i;
  55. GUINT splitIndex =startIndex;
  56. GUINT numIndices = endIndex - startIndex;
  57. // average of centers
  58. btScalar splitValue = 0.0f;
  59. for (i=startIndex;i<endIndex;i++)
  60. {
  61. splitValue+= 0.5f*(primitive_boxes[i].m_bound.m_max[splitAxis] +
  62. primitive_boxes[i].m_bound.m_min[splitAxis]);
  63. }
  64. splitValue /= (btScalar)numIndices;
  65. //sort leafNodes so all values larger then splitValue comes first, and smaller values start from 'splitIndex'.
  66. for (i=startIndex;i<endIndex;i++)
  67. {
  68. btScalar center = 0.5f*(primitive_boxes[i].m_bound.m_max[splitAxis] +
  69. primitive_boxes[i].m_bound.m_min[splitAxis]);
  70. if (center > splitValue)
  71. {
  72. //swap
  73. primitive_boxes.swap(i,splitIndex);
  74. splitIndex++;
  75. }
  76. }
  77. //if the splitIndex causes unbalanced trees, fix this by using the center in between startIndex and endIndex
  78. //otherwise the tree-building might fail due to stack-overflows in certain cases.
  79. //unbalanced1 is unsafe: it can cause stack overflows
  80. //bool unbalanced1 = ((splitIndex==startIndex) || (splitIndex == (endIndex-1)));
  81. //unbalanced2 should work too: always use center (perfect balanced trees)
  82. //bool unbalanced2 = true;
  83. //this should be safe too:
  84. GUINT rangeBalancedIndices = numIndices/3;
  85. bool unbalanced = ((splitIndex<=(startIndex+rangeBalancedIndices)) || (splitIndex >=(endIndex-1-rangeBalancedIndices)));
  86. if (unbalanced)
  87. {
  88. splitIndex = startIndex+ (numIndices>>1);
  89. }
  90. btAssert(!((splitIndex==startIndex) || (splitIndex == (endIndex))));
  91. return splitIndex;
  92. }
  93. void GIM_BOX_TREE::_build_sub_tree(gim_array<GIM_AABB_DATA> & primitive_boxes, GUINT startIndex, GUINT endIndex)
  94. {
  95. GUINT current_index = m_num_nodes++;
  96. btAssert((endIndex-startIndex)>0);
  97. if((endIndex-startIndex) == 1) //we got a leaf
  98. {
  99. m_node_array[current_index].m_left = 0;
  100. m_node_array[current_index].m_right = 0;
  101. m_node_array[current_index].m_escapeIndex = 0;
  102. m_node_array[current_index].m_bound = primitive_boxes[startIndex].m_bound;
  103. m_node_array[current_index].m_data = primitive_boxes[startIndex].m_data;
  104. return;
  105. }
  106. //configure inner node
  107. GUINT splitIndex;
  108. //calc this node bounding box
  109. m_node_array[current_index].m_bound.invalidate();
  110. for (splitIndex=startIndex;splitIndex<endIndex;splitIndex++)
  111. {
  112. m_node_array[current_index].m_bound.merge(primitive_boxes[splitIndex].m_bound);
  113. }
  114. //calculate Best Splitting Axis and where to split it. Sort the incoming 'leafNodes' array within range 'startIndex/endIndex'.
  115. //split axis
  116. splitIndex = _calc_splitting_axis(primitive_boxes,startIndex,endIndex);
  117. splitIndex = _sort_and_calc_splitting_index(
  118. primitive_boxes,startIndex,endIndex,splitIndex);
  119. //configure this inner node : the left node index
  120. m_node_array[current_index].m_left = m_num_nodes;
  121. //build left child tree
  122. _build_sub_tree(primitive_boxes, startIndex, splitIndex );
  123. //configure this inner node : the right node index
  124. m_node_array[current_index].m_right = m_num_nodes;
  125. //build right child tree
  126. _build_sub_tree(primitive_boxes, splitIndex ,endIndex);
  127. //configure this inner node : the escape index
  128. m_node_array[current_index].m_escapeIndex = m_num_nodes - current_index;
  129. }
  130. //! stackless build tree
  131. void GIM_BOX_TREE::build_tree(
  132. gim_array<GIM_AABB_DATA> & primitive_boxes)
  133. {
  134. // initialize node count to 0
  135. m_num_nodes = 0;
  136. // allocate nodes
  137. m_node_array.resize(primitive_boxes.size()*2);
  138. _build_sub_tree(primitive_boxes, 0, primitive_boxes.size());
  139. }