bvh_logic.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // for slow incremental optimization, we will periodically remove each
  2. // item from the tree and reinsert, to give it a chance to find a better position
  3. void _logic_item_remove_and_reinsert(uint32_t p_ref_id) {
  4. // get the reference
  5. ItemRef &ref = _refs[p_ref_id];
  6. // no need to optimize inactive items
  7. if (!ref.is_active())
  8. return;
  9. // special case of debug draw
  10. if (ref.item_id == BVHCommon::INVALID)
  11. return;
  12. BVH_ASSERT(ref.tnode_id != BVHCommon::INVALID);
  13. // some overlay elaborate way to find out which tree the node is in!
  14. BVHHandle temp_handle;
  15. temp_handle.set_id(p_ref_id);
  16. _current_tree = _handle_get_tree_id(temp_handle);
  17. // remove and reinsert
  18. BVH_ABB abb;
  19. node_remove_item(p_ref_id, &abb);
  20. // we must choose where to add to tree
  21. ref.tnode_id = _logic_choose_item_add_node(_root_node_id[_current_tree], abb);
  22. _node_add_item(ref.tnode_id, p_ref_id, abb);
  23. refit_upward_and_balance(ref.tnode_id);
  24. }
  25. // from randy gaul balance function
  26. BVH_ABB _logic_abb_merge(const BVH_ABB &a, const BVH_ABB &b) {
  27. BVH_ABB c = a;
  28. c.merge(b);
  29. return c;
  30. }
  31. //--------------------------------------------------------------------------------------------------
  32. /**
  33. @file q3DynamicAABBTree.h
  34. @author Randy Gaul
  35. @date 10/10/2014
  36. Copyright (c) 2014 Randy Gaul http://www.randygaul.net
  37. This software is provided 'as-is', without any express or implied
  38. warranty. In no event will the authors be held liable for any damages
  39. arising from the use of this software.
  40. Permission is granted to anyone to use this software for any purpose,
  41. including commercial applications, and to alter it and redistribute it
  42. freely, subject to the following restrictions:
  43. 1. The origin of this software must not be misrepresented; you must not
  44. claim that you wrote the original software. If you use this software
  45. in a product, an acknowledgment in the product documentation would be
  46. appreciated but is not required.
  47. 2. Altered source versions must be plainly marked as such, and must not
  48. be misrepresented as being the original software.
  49. 3. This notice may not be removed or altered from any source distribution.
  50. */
  51. //--------------------------------------------------------------------------------------------------
  52. // This function is based on the 'Balance' function from Randy Gaul's qu3e
  53. // https://github.com/RandyGaul/qu3e
  54. // It is MODIFIED from qu3e version.
  55. // This is the only function used (and _logic_abb_merge helper function).
  56. int32_t _logic_balance(int32_t iA) {
  57. // return iA; // uncomment this to bypass balance
  58. TNode *A = &_nodes[iA];
  59. if (A->is_leaf() || A->height == 1)
  60. return iA;
  61. /* A
  62. / \
  63. B C
  64. / \ / \
  65. D E F G
  66. */
  67. CRASH_COND(A->num_children != 2);
  68. int32_t iB = A->children[0];
  69. int32_t iC = A->children[1];
  70. TNode *B = &_nodes[iB];
  71. TNode *C = &_nodes[iC];
  72. int32_t balance = C->height - B->height;
  73. // C is higher, promote C
  74. if (balance > 1) {
  75. int32_t iF = C->children[0];
  76. int32_t iG = C->children[1];
  77. TNode *F = &_nodes[iF];
  78. TNode *G = &_nodes[iG];
  79. // grandParent point to C
  80. if (A->parent_id != BVHCommon::INVALID) {
  81. if (_nodes[A->parent_id].children[0] == iA)
  82. _nodes[A->parent_id].children[0] = iC;
  83. else
  84. _nodes[A->parent_id].children[1] = iC;
  85. } else {
  86. // check this .. seems dodgy
  87. change_root_node(iC);
  88. }
  89. // Swap A and C
  90. C->children[0] = iA;
  91. C->parent_id = A->parent_id;
  92. A->parent_id = iC;
  93. // Finish rotation
  94. if (F->height > G->height) {
  95. C->children[1] = iF;
  96. A->children[1] = iG;
  97. G->parent_id = iA;
  98. A->aabb = _logic_abb_merge(B->aabb, G->aabb);
  99. C->aabb = _logic_abb_merge(A->aabb, F->aabb);
  100. A->height = 1 + MAX(B->height, G->height);
  101. C->height = 1 + MAX(A->height, F->height);
  102. }
  103. else {
  104. C->children[1] = iG;
  105. A->children[1] = iF;
  106. F->parent_id = iA;
  107. A->aabb = _logic_abb_merge(B->aabb, F->aabb);
  108. C->aabb = _logic_abb_merge(A->aabb, G->aabb);
  109. A->height = 1 + MAX(B->height, F->height);
  110. C->height = 1 + MAX(A->height, G->height);
  111. }
  112. return iC;
  113. }
  114. // B is higher, promote B
  115. else if (balance < -1) {
  116. int32_t iD = B->children[0];
  117. int32_t iE = B->children[1];
  118. TNode *D = &_nodes[iD];
  119. TNode *E = &_nodes[iE];
  120. // grandParent point to B
  121. if (A->parent_id != BVHCommon::INVALID) {
  122. if (_nodes[A->parent_id].children[0] == iA)
  123. _nodes[A->parent_id].children[0] = iB;
  124. else
  125. _nodes[A->parent_id].children[1] = iB;
  126. }
  127. else {
  128. // check this .. seems dodgy
  129. change_root_node(iB);
  130. }
  131. // Swap A and B
  132. B->children[1] = iA;
  133. B->parent_id = A->parent_id;
  134. A->parent_id = iB;
  135. // Finish rotation
  136. if (D->height > E->height) {
  137. B->children[0] = iD;
  138. A->children[0] = iE;
  139. E->parent_id = iA;
  140. A->aabb = _logic_abb_merge(C->aabb, E->aabb);
  141. B->aabb = _logic_abb_merge(A->aabb, D->aabb);
  142. A->height = 1 + MAX(C->height, E->height);
  143. B->height = 1 + MAX(A->height, D->height);
  144. }
  145. else {
  146. B->children[0] = iE;
  147. A->children[0] = iD;
  148. D->parent_id = iA;
  149. A->aabb = _logic_abb_merge(C->aabb, D->aabb);
  150. B->aabb = _logic_abb_merge(A->aabb, E->aabb);
  151. A->height = 1 + MAX(C->height, D->height);
  152. B->height = 1 + MAX(A->height, E->height);
  153. }
  154. return iB;
  155. }
  156. return iA;
  157. }
  158. // either choose an existing node to add item to, or create a new node and return this
  159. uint32_t _logic_choose_item_add_node(uint32_t p_node_id, const BVH_ABB &p_aabb) {
  160. while (true) {
  161. BVH_ASSERT(p_node_id != BVHCommon::INVALID);
  162. TNode &tnode = _nodes[p_node_id];
  163. if (tnode.is_leaf()) {
  164. // if a leaf, and non full, use this to add to
  165. if (!node_is_leaf_full(tnode))
  166. return p_node_id;
  167. // else split the leaf, and use one of the children to add to
  168. return split_leaf(p_node_id, p_aabb);
  169. }
  170. // this should not happen???
  171. // is still happening, need to debug and find circumstances. Is not that serious
  172. // but would be nice to prevent. I think it only happens with the root node.
  173. if (tnode.num_children == 1) {
  174. WARN_PRINT_ONCE("BVH::recursive_choose_item_add_node, node with 1 child, recovering");
  175. p_node_id = tnode.children[0];
  176. } else {
  177. BVH_ASSERT(tnode.num_children == 2);
  178. TNode &childA = _nodes[tnode.children[0]];
  179. TNode &childB = _nodes[tnode.children[1]];
  180. int which = p_aabb.select_by_proximity(childA.aabb, childB.aabb);
  181. p_node_id = tnode.children[which];
  182. }
  183. }
  184. }