bvh_refit.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. void _debug_node_verify_bound(uint32_t p_node_id) {
  2. TNode &node = _nodes[p_node_id];
  3. BVHABB_CLASS abb_before = node.aabb;
  4. node_update_aabb(node);
  5. BVHABB_CLASS abb_after = node.aabb;
  6. CRASH_COND(abb_before != abb_after);
  7. }
  8. void node_update_aabb(TNode &tnode) {
  9. tnode.aabb.set_to_max_opposite_extents();
  10. tnode.height = 0;
  11. if (!tnode.is_leaf()) {
  12. for (int n = 0; n < tnode.num_children; n++) {
  13. uint32_t child_node_id = tnode.children[n];
  14. // merge with child aabb
  15. const TNode &tchild = _nodes[child_node_id];
  16. tnode.aabb.merge(tchild.aabb);
  17. // do heights at the same time
  18. if (tchild.height > tnode.height) {
  19. tnode.height = tchild.height;
  20. }
  21. }
  22. // the height of a non leaf is always 1 bigger than the biggest child
  23. tnode.height++;
  24. #ifdef BVH_CHECKS
  25. if (!tnode.num_children) {
  26. // the 'blank' aabb will screw up parent aabbs
  27. WARN_PRINT("BVH_Tree::TNode no children, AABB is undefined");
  28. }
  29. #endif
  30. } else {
  31. // leaf
  32. const TLeaf &leaf = _node_get_leaf(tnode);
  33. for (int n = 0; n < leaf.num_items; n++) {
  34. tnode.aabb.merge(leaf.get_aabb(n));
  35. }
  36. // now the leaf items are unexpanded, we expand only in the node AABB
  37. tnode.aabb.expand(_node_expansion);
  38. #ifdef BVH_CHECKS
  39. if (!leaf.num_items) {
  40. // the 'blank' aabb will screw up parent aabbs
  41. WARN_PRINT("BVH_Tree::TLeaf no items, AABB is undefined");
  42. }
  43. #endif
  44. }
  45. }
  46. void refit_all(int p_tree_id) {
  47. refit_downward(_root_node_id[p_tree_id]);
  48. }
  49. void refit_upward(uint32_t p_node_id) {
  50. while (p_node_id != BVHCommon::INVALID) {
  51. TNode &tnode = _nodes[p_node_id];
  52. node_update_aabb(tnode);
  53. p_node_id = tnode.parent_id;
  54. }
  55. }
  56. void refit_upward_and_balance(uint32_t p_node_id, uint32_t p_tree_id) {
  57. while (p_node_id != BVHCommon::INVALID) {
  58. uint32_t before = p_node_id;
  59. p_node_id = _logic_balance(p_node_id, p_tree_id);
  60. if (before != p_node_id) {
  61. VERBOSE_PRINT("REBALANCED!");
  62. }
  63. TNode &tnode = _nodes[p_node_id];
  64. // update overall aabb from the children
  65. node_update_aabb(tnode);
  66. p_node_id = tnode.parent_id;
  67. }
  68. }
  69. void refit_downward(uint32_t p_node_id) {
  70. TNode &tnode = _nodes[p_node_id];
  71. // do children first
  72. if (!tnode.is_leaf()) {
  73. for (int n = 0; n < tnode.num_children; n++) {
  74. refit_downward(tnode.children[n]);
  75. }
  76. }
  77. node_update_aabb(tnode);
  78. }
  79. // go down to the leaves, then refit upward
  80. void refit_branch(uint32_t p_node_id) {
  81. // our function parameters to keep on a stack
  82. struct RefitParams {
  83. uint32_t node_id;
  84. };
  85. // most of the iterative functionality is contained in this helper class
  86. BVH_IterativeInfo<RefitParams> ii;
  87. // alloca must allocate the stack from this function, it cannot be allocated in the
  88. // helper class
  89. ii.stack = (RefitParams *)alloca(ii.get_alloca_stacksize());
  90. // seed the stack
  91. ii.get_first()->node_id = p_node_id;
  92. RefitParams rp;
  93. // while there are still more nodes on the stack
  94. while (ii.pop(rp)) {
  95. TNode &tnode = _nodes[rp.node_id];
  96. // do children first
  97. if (!tnode.is_leaf()) {
  98. for (int n = 0; n < tnode.num_children; n++) {
  99. uint32_t child_id = tnode.children[n];
  100. // add to the stack
  101. RefitParams *child = ii.request();
  102. child->node_id = child_id;
  103. }
  104. } else {
  105. // leaf .. only refit upward if dirty
  106. TLeaf &leaf = _node_get_leaf(tnode);
  107. if (leaf.is_dirty()) {
  108. leaf.set_dirty(false);
  109. refit_upward(p_node_id);
  110. }
  111. }
  112. } // while more nodes to pop
  113. }