Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option verbose debugging to dynamic BVH. #44650

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions core/math/dynamic_bvh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@

#include "dynamic_bvh.h"

#ifdef DYNAMICBVH_VERBOSE_DEBUG
#include "core/config/engine.h"
#include "core/string/print_string.h"
#endif

void DynamicBVH::_delete_node(Node *p_node) {
memdelete(p_node);
}
Expand Down Expand Up @@ -296,6 +301,9 @@ void DynamicBVH::optimize_bottom_up() {
_bottom_up(&leaves[0], leaves.size());
bvh_root = leaves[0];
}
#ifdef DYNAMICBVH_VERBOSE_DEBUG
debug_print_tree("optimize_bottom_up");
#endif
}

void DynamicBVH::optimize_top_down(int bu_threshold) {
Expand All @@ -304,6 +312,9 @@ void DynamicBVH::optimize_top_down(int bu_threshold) {
_fetch_leaves(bvh_root, leaves);
bvh_root = _top_down(&leaves[0], leaves.size(), bu_threshold);
}
#ifdef DYNAMICBVH_VERBOSE_DEBUG
debug_print_tree("optimize_top_down");
#endif
}

void DynamicBVH::optimize_incremental(int passes) {
Expand All @@ -321,6 +332,9 @@ void DynamicBVH::optimize_incremental(int passes) {
++opath;
} while (--passes);
}
#ifdef DYNAMICBVH_VERBOSE_DEBUG
debug_print_tree("optimize_incremental");
#endif
}

DynamicBVH::ID DynamicBVH::insert(const AABB &p_box, void *p_userdata) {
Expand All @@ -335,6 +349,9 @@ DynamicBVH::ID DynamicBVH::insert(const AABB &p_box, void *p_userdata) {
ID id;
id.node = leaf;

#ifdef DYNAMICBVH_VERBOSE_DEBUG
debug_print_tree("insert");
#endif
return id;
}

Expand Down Expand Up @@ -425,6 +442,50 @@ int DynamicBVH::get_max_depth() const {
}
}

#ifdef DYNAMICBVH_VERBOSE_DEBUG
void DynamicBVH::_debug_print_node(const Node &p_node, int p_depth, String &p_output) {
String sz;
for (int n = 0; n < p_depth; n++) {
sz += "\t";
}

if (p_node.is_leaf()) {
sz += "L [";
} else {
sz += "N [";
}

// volume
sz += String(p_node.volume.min) + ", " + String(p_node.volume.max) + "]";

p_output += sz + "\n";

// children
if (!p_node.is_leaf()) {
if (p_node.childs[0])
_debug_print_node(*p_node.childs[0], p_depth + 1, p_output);

if (p_node.childs[1])
_debug_print_node(*p_node.childs[1], p_depth + 1, p_output);
}
}

void DynamicBVH::debug_print_tree(String p_string) {
if (Engine::get_singleton()->is_editor_hint())
return;

if (p_string != "") {
p_string += "\n";
}

if (bvh_root) {
_debug_print_node(*bvh_root, 0, p_string);
}

print_line(p_string);
}
#endif

DynamicBVH::~DynamicBVH() {
clear();
}
9 changes: 9 additions & 0 deletions core/math/dynamic_bvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ subject to the following restrictions:
///DynamicBVH implementation by Nathanael Presson
// The DynamicBVH class implements a fast dynamic bounding volume tree based on axis aligned bounding boxes (aabb tree).

// #define DYNAMICBVH_VERBOSE_DEBUG

class DynamicBVH {
struct Node;

Expand Down Expand Up @@ -244,6 +246,9 @@ class DynamicBVH {
_FORCE_INLINE_ void _update(Node *leaf, int lookahead = -1);

void _extract_leaves(Node *p_node, List<ID> *r_elements);
#ifdef DYNAMICBVH_VERBOSE_DEBUG
void _debug_print_node(const Node &p_node, int p_depth, String &p_output);
#endif

_FORCE_INLINE_ bool _ray_aabb(const Vector3 &rayFrom, const Vector3 &rayInvDirection, const unsigned int raySign[3], const Vector3 bounds[2], real_t &tmin, real_t lambda_min, real_t lambda_max) {
real_t tmax, tymin, tymax, tzmin, tzmax;
Expand Down Expand Up @@ -288,6 +293,10 @@ class DynamicBVH {
int get_leaf_count() const;
int get_max_depth() const;

#ifdef DYNAMICBVH_VERBOSE_DEBUG
void debug_print_tree(String p_string);
#endif

/* Discouraged, but works as a reference on how it must be used */
struct DefaultQueryResult {
virtual bool operator()(void *p_data) = 0; //return true whether you want to continue the query
Expand Down