#pragma once
#include "bvh_tree.h"
#include "core/math/geometry_3d.h"
#include "core/os/mutex.h"
#define BVHTREE_CLASS BVH_Tree<T, NUM_TREES, 2, MAX_ITEMS, USER_PAIR_TEST_FUNCTION, USER_CULL_TEST_FUNCTION, USE_PAIRS, BOUNDS, POINT>
#define BVH_LOCKED_FUNCTION BVHLockedFunction _lock_guard(&_mutex, BVH_THREAD_SAFE &&_thread_safe);
template <typename T, int NUM_TREES = 1, bool USE_PAIRS = false, int MAX_ITEMS = 32, typename USER_PAIR_TEST_FUNCTION = BVH_DummyPairTestFunction<T>, typename USER_CULL_TEST_FUNCTION = BVH_DummyCullTestFunction<T>, typename BOUNDS = AABB, typename POINT = Vector3, bool BVH_THREAD_SAFE = true>
class BVH_Manager {
public:
typedef void *(*PairCallback)(void *, uint32_t, T *, int, uint32_t, T *, int);
typedef void (*UnpairCallback)(void *, uint32_t, T *, int, uint32_t, T *, int, void *);
typedef void *(*CheckPairCallback)(void *, uint32_t, T *, int, uint32_t, T *, int, void *);
void params_set_thread_safe(bool p_enable) {
_thread_safe = p_enable;
}
void params_set_node_expansion(real_t p_value) {
BVH_LOCKED_FUNCTION
if (p_value >= 0.0) {
tree._node_expansion = p_value;
tree._auto_node_expansion = false;
} else {
tree._auto_node_expansion = true;
}
}
void params_set_pairing_expansion(real_t p_value) {
BVH_LOCKED_FUNCTION
tree.params_set_pairing_expansion(p_value);
}
void set_pair_callback(PairCallback p_callback, void *p_userdata) {
BVH_LOCKED_FUNCTION
pair_callback = p_callback;
pair_callback_userdata = p_userdata;
}
void set_unpair_callback(UnpairCallback p_callback, void *p_userdata) {
BVH_LOCKED_FUNCTION
unpair_callback = p_callback;
unpair_callback_userdata = p_userdata;
}
void set_check_pair_callback(CheckPairCallback p_callback, void *p_userdata) {
BVH_LOCKED_FUNCTION
check_pair_callback = p_callback;
check_pair_callback_userdata = p_userdata;
}
BVHHandle create(T *p_userdata, bool p_active = true, uint32_t p_tree_id = 0, uint32_t p_tree_collision_mask = 1, const BOUNDS &p_aabb = BOUNDS(), int p_subindex = 0) {
BVH_LOCKED_FUNCTION
if (USE_PAIRS) {
}
BVHHandle h = tree.item_add(p_userdata, p_active, p_aabb, p_subindex, p_tree_id, p_tree_collision_mask);
if (USE_PAIRS) {
BOUNDS &expanded_aabb = tree._pairs[h.id()].expanded_aabb;
expanded_aabb = p_aabb;
expanded_aabb.grow_by(tree._pairing_expansion);
if (p_active) {
_add_changed_item(h, p_aabb, false);
_check_for_collisions(true);
}
}
return h;
}
void move(uint32_t p_handle, const BOUNDS &p_aabb) {
BVHHandle h;
h.set(p_handle);
move(h, p_aabb);
}
void recheck_pairs(uint32_t p_handle) {
BVHHandle h;
h.set(p_handle);
recheck_pairs(h);
}
void erase(uint32_t p_handle) {
BVHHandle h;
h.set(p_handle);
erase(h);
}
void force_collision_check(uint32_t p_handle) {
BVHHandle h;
h.set(p_handle);
force_collision_check(h);
}
bool activate(uint32_t p_handle, const BOUNDS &p_aabb, bool p_delay_collision_check = false) {
BVHHandle h;
h.set(p_handle);
return activate(h, p_aabb, p_delay_collision_check);
}
bool deactivate(uint32_t p_handle) {
BVHHandle h;
h.set(p_handle);
return deactivate(h);
}
void set_tree(uint32_t p_handle, uint32_t p_tree_id, uint32_t p_tree_collision_mask, bool p_force_collision_check = true) {
BVHHandle h;
h.set(p_handle);
set_tree(h, p_tree_id, p_tree_collision_mask, p_force_collision_check);
}
uint32_t get_tree_id(uint32_t p_handle) const {
BVHHandle h;
h.set(p_handle);
return item_get_tree_id(h);
}
int get_subindex(uint32_t p_handle) const {
BVHHandle h;
h.set(p_handle);
return item_get_subindex(h);
}
T *get(uint32_t p_handle) const {
BVHHandle h;
h.set(p_handle);
return item_get_userdata(h);
}
void move(BVHHandle p_handle, const BOUNDS &p_aabb) {
DEV_ASSERT(!p_handle.is_invalid());
BVH_LOCKED_FUNCTION
if (tree.item_move(p_handle, p_aabb)) {
if (USE_PAIRS) {
_add_changed_item(p_handle, p_aabb);
}
}
}
void recheck_pairs(BVHHandle p_handle) {
DEV_ASSERT(!p_handle.is_invalid());
force_collision_check(p_handle);
}
void erase(BVHHandle p_handle) {
DEV_ASSERT(!p_handle.is_invalid());
BVH_LOCKED_FUNCTION
if (USE_PAIRS) {
_remove_changed_item(p_handle);
}
tree.item_remove(p_handle);
_check_for_collisions(true);
}
void force_collision_check(BVHHandle p_handle) {
DEV_ASSERT(!p_handle.is_invalid());
BVH_LOCKED_FUNCTION
if (USE_PAIRS) {
BOUNDS aabb;
item_get_AABB(p_handle, aabb);
_add_changed_item(p_handle, aabb, false);
_check_for_collisions(true);
}
}
bool activate(BVHHandle p_handle, const BOUNDS &p_aabb, bool p_delay_collision_check = false) {
DEV_ASSERT(!p_handle.is_invalid());
BVH_LOCKED_FUNCTION
if (tree.item_activate(p_handle, p_aabb)) {
if (USE_PAIRS) {
if (!p_delay_collision_check) {
_add_changed_item(p_handle, p_aabb, false);
_check_for_collisions(true);
}
}
return true;
}
return false;
}
bool deactivate(BVHHandle p_handle) {
DEV_ASSERT(!p_handle.is_invalid());
BVH_LOCKED_FUNCTION
if (tree.item_deactivate(p_handle)) {
if (USE_PAIRS) {
_remove_changed_item(p_handle);
_check_for_collisions(true);
}
return true;
}
return false;
}
bool get_active(BVHHandle p_handle) {
DEV_ASSERT(!p_handle.is_invalid());
BVH_LOCKED_FUNCTION
return tree.item_get_active(p_handle);
}
void update() {
BVH_LOCKED_FUNCTION
tree.update();
_check_for_collisions();
#ifdef BVH_INTEGRITY_CHECKS
tree._integrity_check_all();
#endif
}
void update_collisions() {
BVH_LOCKED_FUNCTION
_check_for_collisions();
}
void set_tree(const BVHHandle &p_handle, uint32_t p_tree_id, uint32_t p_tree_collision_mask, bool p_force_collision_check = true) {
DEV_ASSERT(!p_handle.is_invalid());
BVH_LOCKED_FUNCTION
bool state_changed = tree.item_set_tree(p_handle, p_tree_id, p_tree_collision_mask);
if (USE_PAIRS) {
if ((p_force_collision_check || state_changed) && tree.item_get_active(p_handle)) {
BOUNDS aabb;
item_get_AABB(p_handle, aabb);
_add_changed_item(p_handle, aabb, false);
_check_for_collisions(true);
}
}
}
int cull_aabb(const BOUNDS &p_aabb, T **p_result_array, int p_result_max, const T *p_tester, uint32_t p_tree_collision_mask = 0xFFFFFFFF, int *p_subindex_array = nullptr) {
BVH_LOCKED_FUNCTION
typename BVHTREE_CLASS::CullParams params;
params.result_count_overall = 0;
params.result_max = p_result_max;
params.result_array = p_result_array;
params.subindex_array = p_subindex_array;
params.tree_collision_mask = p_tree_collision_mask;
params.abb.from(p_aabb);
params.tester = p_tester;
tree.cull_aabb(params);
return params.result_count_overall;
}
int cull_segment(const POINT &p_from, const POINT &p_to, T **p_result_array, int p_result_max, const T *p_tester, uint32_t p_tree_collision_mask = 0xFFFFFFFF, int *p_subindex_array = nullptr) {
BVH_LOCKED_FUNCTION
typename BVHTREE_CLASS::CullParams params;
params.result_count_overall = 0;
params.result_max = p_result_max;
params.result_array = p_result_array;
params.subindex_array = p_subindex_array;
params.tester = p_tester;
params.tree_collision_mask = p_tree_collision_mask;
params.segment.from = p_from;
params.segment.to = p_to;
tree.cull_segment(params);
return params.result_count_overall;
}
int cull_point(const POINT &p_point, T **p_result_array, int p_result_max, const T *p_tester, uint32_t p_tree_collision_mask = 0xFFFFFFFF, int *p_subindex_array = nullptr) {
BVH_LOCKED_FUNCTION
typename BVHTREE_CLASS::CullParams params;
params.result_count_overall = 0;
params.result_max = p_result_max;
params.result_array = p_result_array;
params.subindex_array = p_subindex_array;
params.tester = p_tester;
params.tree_collision_mask = p_tree_collision_mask;
params.point = p_point;
tree.cull_point(params);
return params.result_count_overall;
}
int cull_convex(const Vector<Plane> &p_convex, T **p_result_array, int p_result_max, const T *p_tester, uint32_t p_tree_collision_mask = 0xFFFFFFFF) {
BVH_LOCKED_FUNCTION
if (!p_convex.size()) {
return 0;
}
Vector<Vector3> convex_points = Geometry3D::compute_convex_mesh_points(&p_convex[0], p_convex.size());
if (convex_points.is_empty()) {
return 0;
}
typename BVHTREE_CLASS::CullParams params;
params.result_count_overall = 0;
params.result_max = p_result_max;
params.result_array = p_result_array;
params.subindex_array = nullptr;
params.tester = p_tester;
params.tree_collision_mask = p_tree_collision_mask;
params.hull.planes = &p_convex[0];
params.hull.num_planes = p_convex.size();
params.hull.points = &convex_points[0];
params.hull.num_points = convex_points.size();
tree.cull_convex(params);
return params.result_count_overall;
}
private:
void _check_for_collisions(bool p_full_check = false) {
if (!changed_items.size()) {
return;
}
typename BVHTREE_CLASS::CullParams params;
params.result_count_overall = 0;
params.result_max = INT_MAX;
params.result_array = nullptr;
params.subindex_array = nullptr;
for (const BVHHandle &h : changed_items) {
const BOUNDS &expanded_aabb = tree._pairs[h.id()].expanded_aabb;
BVHABB_CLASS abb;
abb.from(expanded_aabb);
tree.item_fill_cullparams(h, params);
_find_leavers(h, abb, p_full_check);
uint32_t changed_item_ref_id = h.id();
params.abb = abb;
params.result_count_overall = 0;
tree.cull_aabb(params, false);
for (const uint32_t ref_id : tree._cull_hits) {
if (ref_id == changed_item_ref_id) {
continue;
}
BVHHandle h_collidee;
h_collidee.set_id(ref_id);
_collide(h, h_collidee);
}
}
_reset();
}
public:
void item_get_AABB(BVHHandle p_handle, BOUNDS &r_aabb) {
DEV_ASSERT(!p_handle.is_invalid());
BVHABB_CLASS abb;
tree.item_get_ABB(p_handle, abb);
abb.to(r_aabb);
}
private:
uint32_t item_get_tree_id(BVHHandle p_handle) const { return _get_extra(p_handle).tree_id; }
T *item_get_userdata(BVHHandle p_handle) const { return _get_extra(p_handle).userdata; }
int item_get_subindex(BVHHandle p_handle) const { return _get_extra(p_handle).subindex; }
void _unpair(BVHHandle p_from, BVHHandle p_to) {
tree._handle_sort(p_from, p_to);
typename BVHTREE_CLASS::ItemExtra &exa = tree._extra[p_from.id()];
typename BVHTREE_CLASS::ItemExtra &exb = tree._extra[p_to.id()];
if ((exa.userdata == exb.userdata) && exa.userdata) {
return;
}
typename BVHTREE_CLASS::ItemPairs &pairs_from = tree._pairs[p_from.id()];
typename BVHTREE_CLASS::ItemPairs &pairs_to = tree._pairs[p_to.id()];
void *ud_from = pairs_from.remove_pair_to(p_to);
pairs_to.remove_pair_to(p_from);
#ifdef BVH_VERBOSE_PAIRING
print_line("_unpair " + itos(p_from.id()) + " from " + itos(p_to.id()));
#endif
if (unpair_callback) {
unpair_callback(pair_callback_userdata, p_from, exa.userdata, exa.subindex, p_to, exb.userdata, exb.subindex, ud_from);
}
}
void *_recheck_pair(BVHHandle p_from, BVHHandle p_to, void *p_pair_data) {
tree._handle_sort(p_from, p_to);
typename BVHTREE_CLASS::ItemExtra &exa = tree._extra[p_from.id()];
typename BVHTREE_CLASS::ItemExtra &exb = tree._extra[p_to.id()];
if ((exa.userdata == exb.userdata) && exa.userdata) {
return p_pair_data;
}
if (check_pair_callback) {
return check_pair_callback(check_pair_callback_userdata, p_from, exa.userdata, exa.subindex, p_to, exb.userdata, exb.subindex, p_pair_data);
}
return p_pair_data;
}
bool _find_leavers_process_pair(typename BVHTREE_CLASS::ItemPairs &p_pairs_from, const BVHABB_CLASS &p_abb_from, BVHHandle p_from, BVHHandle p_to, bool p_full_check) {
BVHABB_CLASS abb_to;
tree.item_get_ABB(p_to, abb_to);
if (p_abb_from.intersects(abb_to)) {
if (!p_full_check) {
return false;
}
const typename BVHTREE_CLASS::ItemExtra &exa = _get_extra(p_from);
const typename BVHTREE_CLASS::ItemExtra &exb = _get_extra(p_to);
if (exa.are_item_trees_compatible(exb)) {
bool pair_allowed = USER_PAIR_TEST_FUNCTION::user_pair_check(exa.userdata, exb.userdata);
if (pair_allowed) {
return false;
}
}
}
_unpair(p_from, p_to);
return true;
}
void _find_leavers(BVHHandle p_handle, const BVHABB_CLASS &expanded_abb_from, bool p_full_check) {
typename BVHTREE_CLASS::ItemPairs &p_from = tree._pairs[p_handle.id()];
BVHABB_CLASS abb_from = expanded_abb_from;
for (unsigned int n = 0; n < p_from.extended_pairs.size(); n++) {
BVHHandle h_to = p_from.extended_pairs[n].handle;
if (_find_leavers_process_pair(p_from, abb_from, p_handle, h_to, p_full_check)) {
n--;
}
}
}
void _collide(BVHHandle p_ha, BVHHandle p_hb) {
tree._handle_sort(p_ha, p_hb);
const typename BVHTREE_CLASS::ItemExtra &exa = _get_extra(p_ha);
const typename BVHTREE_CLASS::ItemExtra &exb = _get_extra(p_hb);
if (!USER_PAIR_TEST_FUNCTION::user_pair_check(exa.userdata, exb.userdata)) {
return;
}
if ((exa.userdata == exb.userdata) && exa.userdata) {
return;
}
typename BVHTREE_CLASS::ItemPairs &p_from = tree._pairs[p_ha.id()];
typename BVHTREE_CLASS::ItemPairs &p_to = tree._pairs[p_hb.id()];
if (p_from.num_pairs <= p_to.num_pairs) {
if (p_from.contains_pair_to(p_hb)) {
return;
}
} else {
if (p_to.contains_pair_to(p_ha)) {
return;
}
}
void *callback_userdata = nullptr;
#ifdef BVH_VERBOSE_PAIRING
print_line("_pair " + itos(p_ha.id()) + " to " + itos(p_hb.id()));
#endif
if (pair_callback) {
callback_userdata = pair_callback(pair_callback_userdata, p_ha, exa.userdata, exa.subindex, p_hb, exb.userdata, exb.subindex);
}
p_from.add_pair_to(p_hb, callback_userdata);
p_to.add_pair_to(p_ha, callback_userdata);
}
void _remove_pairs_containing(BVHHandle p_handle) {
typename BVHTREE_CLASS::ItemPairs &p_from = tree._pairs[p_handle.id()];
while (p_from.extended_pairs.size()) {
BVHHandle h_to = p_from.extended_pairs[0].handle;
_unpair(p_handle, h_to);
}
}
void _recheck_pairs(BVHHandle p_handle) {
typename BVHTREE_CLASS::ItemPairs &from = tree._pairs[p_handle.id()];
for (unsigned int n = 0; n < from.extended_pairs.size(); n++) {
typename BVHTREE_CLASS::ItemPairs::Link &pair = from.extended_pairs[n];
BVHHandle h_to = pair.handle;
void *new_pair_data = _recheck_pair(p_handle, h_to, pair.userdata);
if (new_pair_data != pair.userdata) {
pair.userdata = new_pair_data;
typename BVHTREE_CLASS::ItemPairs &to = tree._pairs[h_to.id()];
for (unsigned int to_index = 0; to_index < to.extended_pairs.size(); to_index++) {
typename BVHTREE_CLASS::ItemPairs::Link &to_pair = to.extended_pairs[to_index];
if (to_pair.handle == p_handle) {
to_pair.userdata = new_pair_data;
break;
}
}
}
}
}
private:
const typename BVHTREE_CLASS::ItemExtra &_get_extra(BVHHandle p_handle) const {
return tree._extra[p_handle.id()];
}
const typename BVHTREE_CLASS::ItemRef &_get_ref(BVHHandle p_handle) const {
return tree._refs[p_handle.id()];
}
void _reset() {
changed_items.clear();
_tick++;
}
void _add_changed_item(BVHHandle p_handle, const BOUNDS &aabb, bool p_check_aabb = true) {
#ifdef BVH_EXPAND_LEAF_AABBS
BOUNDS &expanded_aabb = tree._pairs[p_handle.id()].expanded_aabb;
item_get_AABB(p_handle, expanded_aabb);
#else
BOUNDS &expanded_aabb = tree._pairs[p_handle.id()].expanded_aabb;
if (p_check_aabb && tree.expanded_aabb_encloses_not_shrink(expanded_aabb, aabb)) {
return;
}
expanded_aabb = aabb;
expanded_aabb.grow_by(tree._pairing_expansion);
#endif
uint32_t &last_updated_tick = tree._extra[p_handle.id()].last_updated_tick;
if (last_updated_tick == _tick) {
return;
}
last_updated_tick = _tick;
changed_items.push_back(p_handle);
}
void _remove_changed_item(BVHHandle p_handle) {
_remove_pairs_containing(p_handle);
for (int n = 0; n < (int)changed_items.size(); n++) {
if (changed_items[n] == p_handle) {
changed_items.remove_at_unordered(n);
n--;
}
}
tree._extra[p_handle.id()].last_updated_tick = 0;
}
PairCallback pair_callback = nullptr;
UnpairCallback unpair_callback = nullptr;
CheckPairCallback check_pair_callback = nullptr;
void *pair_callback_userdata = nullptr;
void *unpair_callback_userdata = nullptr;
void *check_pair_callback_userdata = nullptr;
BVHTREE_CLASS tree;
LocalVector<BVHHandle> changed_items;
uint32_t _tick = 1;
class BVHLockedFunction {
public:
BVHLockedFunction(Mutex *p_mutex, bool p_thread_safe) {
if (p_thread_safe) {
_mutex = p_mutex;
_mutex->lock();
} else {
_mutex = nullptr;
}
}
~BVHLockedFunction() {
if (_mutex) {
_mutex->unlock();
}
}
private:
Mutex *_mutex = nullptr;
};
Mutex _mutex;
bool _thread_safe = BVH_THREAD_SAFE;
public:
BVH_Manager() {}
};
#undef BVHTREE_CLASS