| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
#include "bvh_tree.h" |
| 54 |
|
| 55 |
#include "core/math/geometry_3d.h" |
| 56 |
#include "core/os/mutex.h" |
| 57 |
|
| 58 |
#define BVHTREE_CLASS BVH_Tree<T, NUM_TREES, 2, MAX_ITEMS, USER_PAIR_TEST_FUNCTION, USER_CULL_TEST_FUNCTION, USE_PAIRS, BOUNDS, POINT> |
| 59 |
#define BVH_LOCKED_FUNCTION BVHLockedFunction _lock_guard(&_mutex, BVH_THREAD_SAFE &&_thread_safe); |
| 60 |
|
| 61 |
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> |
| 62 |
class BVH_Manager { |
| 63 |
public: |
| 64 |
|
| 65 |
|
| 66 |
typedef void *(*PairCallback)(void *, uint32_t, T *, int, uint32_t, T *, int); |
| 67 |
typedef void (*UnpairCallback)(void *, uint32_t, T *, int, uint32_t, T *, int, void *); |
| 68 |
typedef void *(*CheckPairCallback)(void *, uint32_t, T *, int, uint32_t, T *, int, void *); |
| 69 |
|
| 70 |
|
| 71 |
void params_set_thread_safe(bool p_enable) { |
| 72 |
_thread_safe = p_enable; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
void params_set_node_expansion(real_t p_value) { |
| 78 |
BVH_LOCKED_FUNCTION |
| 79 |
if (p_value >= 0.0) { |
| 80 |
tree._node_expansion = p_value; |
| 81 |
tree._auto_node_expansion = false; |
| 82 |
} else { |
| 83 |
tree._auto_node_expansion = true; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
void params_set_pairing_expansion(real_t p_value) { |
| 88 |
BVH_LOCKED_FUNCTION |
| 89 |
tree.params_set_pairing_expansion(p_value); |
| 90 |
} |
| 91 |
|
| 92 |
void set_pair_callback(PairCallback p_callback, void *p_userdata) { |
| 93 |
BVH_LOCKED_FUNCTION |
| 94 |
pair_callback = p_callback; |
| 95 |
pair_callback_userdata = p_userdata; |
| 96 |
} |
| 97 |
void set_unpair_callback(UnpairCallback p_callback, void *p_userdata) { |
| 98 |
BVH_LOCKED_FUNCTION |
| 99 |
unpair_callback = p_callback; |
| 100 |
unpair_callback_userdata = p_userdata; |
| 101 |
} |
| 102 |
void set_check_pair_callback(CheckPairCallback p_callback, void *p_userdata) { |
| 103 |
BVH_LOCKED_FUNCTION |
| 104 |
check_pair_callback = p_callback; |
| 105 |
check_pair_callback_userdata = p_userdata; |
| 106 |
} |
| 107 |
|
| 108 |
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) { |
| 109 |
BVH_LOCKED_FUNCTION |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
if (USE_PAIRS) { |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
BVHHandle h = tree.item_add(p_userdata, p_active, p_aabb, p_subindex, p_tree_id, p_tree_collision_mask); |
| 118 |
|
| 119 |
if (USE_PAIRS) { |
| 120 |
|
| 121 |
BOUNDS &expanded_aabb = tree._pairs[h.id()].expanded_aabb; |
| 122 |
expanded_aabb = p_aabb; |
| 123 |
expanded_aabb.grow_by(tree._pairing_expansion); |
| 124 |
|
| 125 |
|
| 126 |
if (p_active) { |
| 127 |
_add_changed_item(h, p_aabb, false); |
| 128 |
_check_for_collisions(true); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return h; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
void move(uint32_t p_handle, const BOUNDS &p_aabb) { |
| 139 |
BVHHandle h; |
| 140 |
h.set(p_handle); |
| 141 |
move(h, p_aabb); |
| 142 |
} |
| 143 |
|
| 144 |
void recheck_pairs(uint32_t p_handle) { |
| 145 |
BVHHandle h; |
| 146 |
h.set(p_handle); |
| 147 |
recheck_pairs(h); |
| 148 |
} |
| 149 |
|
| 150 |
void erase(uint32_t p_handle) { |
| 151 |
BVHHandle h; |
| 152 |
h.set(p_handle); |
| 153 |
erase(h); |
| 154 |
} |
| 155 |
|
| 156 |
void force_collision_check(uint32_t p_handle) { |
| 157 |
BVHHandle h; |
| 158 |
h.set(p_handle); |
| 159 |
force_collision_check(h); |
| 160 |
} |
| 161 |
|
| 162 |
bool activate(uint32_t p_handle, const BOUNDS &p_aabb, bool p_delay_collision_check = false) { |
| 163 |
BVHHandle h; |
| 164 |
h.set(p_handle); |
| 165 |
return activate(h, p_aabb, p_delay_collision_check); |
| 166 |
} |
| 167 |
|
| 168 |
bool deactivate(uint32_t p_handle) { |
| 169 |
BVHHandle h; |
| 170 |
h.set(p_handle); |
| 171 |
return deactivate(h); |
| 172 |
} |
| 173 |
|
| 174 |
void set_tree(uint32_t p_handle, uint32_t p_tree_id, uint32_t p_tree_collision_mask, bool p_force_collision_check = true) { |
| 175 |
BVHHandle h; |
| 176 |
h.set(p_handle); |
| 177 |
set_tree(h, p_tree_id, p_tree_collision_mask, p_force_collision_check); |
| 178 |
} |
| 179 |
|
| 180 |
uint32_t get_tree_id(uint32_t p_handle) const { |
| 181 |
BVHHandle h; |
| 182 |
h.set(p_handle); |
| 183 |
return item_get_tree_id(h); |
| 184 |
} |
| 185 |
int get_subindex(uint32_t p_handle) const { |
| 186 |
BVHHandle h; |
| 187 |
h.set(p_handle); |
| 188 |
return item_get_subindex(h); |
| 189 |
} |
| 190 |
|
| 191 |
T *get(uint32_t p_handle) const { |
| 192 |
BVHHandle h; |
| 193 |
h.set(p_handle); |
| 194 |
return item_get_userdata(h); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
void move(BVHHandle p_handle, const BOUNDS &p_aabb) { |
| 200 |
DEV_ASSERT(!p_handle.is_invalid()); |
| 201 |
BVH_LOCKED_FUNCTION |
| 202 |
if (tree.item_move(p_handle, p_aabb)) { |
| 203 |
if (USE_PAIRS) { |
| 204 |
_add_changed_item(p_handle, p_aabb); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void recheck_pairs(BVHHandle p_handle) { |
| 210 |
DEV_ASSERT(!p_handle.is_invalid()); |
| 211 |
force_collision_check(p_handle); |
| 212 |
} |
| 213 |
|
| 214 |
void erase(BVHHandle p_handle) { |
| 215 |
DEV_ASSERT(!p_handle.is_invalid()); |
| 216 |
BVH_LOCKED_FUNCTION |
| 217 |
|
| 218 |
|
| 219 |
if (USE_PAIRS) { |
| 220 |
_remove_changed_item(p_handle); |
| 221 |
} |
| 222 |
|
| 223 |
tree.item_remove(p_handle); |
| 224 |
|
| 225 |
_check_for_collisions(true); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
void force_collision_check(BVHHandle p_handle) { |
| 232 |
DEV_ASSERT(!p_handle.is_invalid()); |
| 233 |
BVH_LOCKED_FUNCTION |
| 234 |
if (USE_PAIRS) { |
| 235 |
|
| 236 |
BOUNDS aabb; |
| 237 |
item_get_AABB(p_handle, aabb); |
| 238 |
|
| 239 |
|
| 240 |
_add_changed_item(p_handle, aabb, false); |
| 241 |
|
| 242 |
|
| 243 |
_check_for_collisions(true); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
bool activate(BVHHandle p_handle, const BOUNDS &p_aabb, bool p_delay_collision_check = false) { |
| 251 |
DEV_ASSERT(!p_handle.is_invalid()); |
| 252 |
BVH_LOCKED_FUNCTION |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
if (tree.item_activate(p_handle, p_aabb)) { |
| 257 |
if (USE_PAIRS) { |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
if (!p_delay_collision_check) { |
| 263 |
_add_changed_item(p_handle, p_aabb, false); |
| 264 |
|
| 265 |
|
| 266 |
_check_for_collisions(true); |
| 267 |
} |
| 268 |
} |
| 269 |
return true; |
| 270 |
} |
| 271 |
|
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
bool deactivate(BVHHandle p_handle) { |
| 276 |
DEV_ASSERT(!p_handle.is_invalid()); |
| 277 |
BVH_LOCKED_FUNCTION |
| 278 |
|
| 279 |
if (tree.item_deactivate(p_handle)) { |
| 280 |
|
| 281 |
|
| 282 |
if (USE_PAIRS) { |
| 283 |
_remove_changed_item(p_handle); |
| 284 |
|
| 285 |
|
| 286 |
_check_for_collisions(true); |
| 287 |
} |
| 288 |
return true; |
| 289 |
} |
| 290 |
|
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
bool get_active(BVHHandle p_handle) { |
| 295 |
DEV_ASSERT(!p_handle.is_invalid()); |
| 296 |
BVH_LOCKED_FUNCTION |
| 297 |
return tree.item_get_active(p_handle); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
void update() { |
| 302 |
BVH_LOCKED_FUNCTION |
| 303 |
tree.update(); |
| 304 |
_check_for_collisions(); |
| 305 |
#ifdef BVH_INTEGRITY_CHECKS |
| 306 |
tree._integrity_check_all(); |
| 307 |
#endif |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
void update_collisions() { |
| 312 |
BVH_LOCKED_FUNCTION |
| 313 |
_check_for_collisions(); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
void set_tree(const BVHHandle &p_handle, uint32_t p_tree_id, uint32_t p_tree_collision_mask, bool p_force_collision_check = true) { |
| 318 |
DEV_ASSERT(!p_handle.is_invalid()); |
| 319 |
BVH_LOCKED_FUNCTION |
| 320 |
|
| 321 |
bool state_changed = tree.item_set_tree(p_handle, p_tree_id, p_tree_collision_mask); |
| 322 |
|
| 323 |
if (USE_PAIRS) { |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
if ((p_force_collision_check || state_changed) && tree.item_get_active(p_handle)) { |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
BOUNDS aabb; |
| 333 |
item_get_AABB(p_handle, aabb); |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
_add_changed_item(p_handle, aabb, false); |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
_check_for_collisions(true); |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
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) { |
| 350 |
BVH_LOCKED_FUNCTION |
| 351 |
typename BVHTREE_CLASS::CullParams params; |
| 352 |
|
| 353 |
params.result_count_overall = 0; |
| 354 |
params.result_max = p_result_max; |
| 355 |
params.result_array = p_result_array; |
| 356 |
params.subindex_array = p_subindex_array; |
| 357 |
params.tree_collision_mask = p_tree_collision_mask; |
| 358 |
params.abb.from(p_aabb); |
| 359 |
params.tester = p_tester; |
| 360 |
|
| 361 |
tree.cull_aabb(params); |
| 362 |
|
| 363 |
return params.result_count_overall; |
| 364 |
} |
| 365 |
|
| 366 |
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) { |
| 367 |
BVH_LOCKED_FUNCTION |
| 368 |
typename BVHTREE_CLASS::CullParams params; |
| 369 |
|
| 370 |
params.result_count_overall = 0; |
| 371 |
params.result_max = p_result_max; |
| 372 |
params.result_array = p_result_array; |
| 373 |
params.subindex_array = p_subindex_array; |
| 374 |
params.tester = p_tester; |
| 375 |
params.tree_collision_mask = p_tree_collision_mask; |
| 376 |
|
| 377 |
params.segment.from = p_from; |
| 378 |
params.segment.to = p_to; |
| 379 |
|
| 380 |
tree.cull_segment(params); |
| 381 |
|
| 382 |
return params.result_count_overall; |
| 383 |
} |
| 384 |
|
| 385 |
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) { |
| 386 |
BVH_LOCKED_FUNCTION |
| 387 |
typename BVHTREE_CLASS::CullParams params; |
| 388 |
|
| 389 |
params.result_count_overall = 0; |
| 390 |
params.result_max = p_result_max; |
| 391 |
params.result_array = p_result_array; |
| 392 |
params.subindex_array = p_subindex_array; |
| 393 |
params.tester = p_tester; |
| 394 |
params.tree_collision_mask = p_tree_collision_mask; |
| 395 |
|
| 396 |
params.point = p_point; |
| 397 |
|
| 398 |
tree.cull_point(params); |
| 399 |
return params.result_count_overall; |
| 400 |
} |
| 401 |
|
| 402 |
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) { |
| 403 |
BVH_LOCKED_FUNCTION |
| 404 |
if (!p_convex.size()) { |
| 405 |
return 0; |
| 406 |
} |
| 407 |
|
| 408 |
Vector<Vector3> convex_points = Geometry3D::compute_convex_mesh_points(&p_convex[0], p_convex.size()); |
| 409 |
if (convex_points.is_empty()) { |
| 410 |
return 0; |
| 411 |
} |
| 412 |
|
| 413 |
typename BVHTREE_CLASS::CullParams params; |
| 414 |
params.result_count_overall = 0; |
| 415 |
params.result_max = p_result_max; |
| 416 |
params.result_array = p_result_array; |
| 417 |
params.subindex_array = nullptr; |
| 418 |
params.tester = p_tester; |
| 419 |
params.tree_collision_mask = p_tree_collision_mask; |
| 420 |
|
| 421 |
params.hull.planes = &p_convex[0]; |
| 422 |
params.hull.num_planes = p_convex.size(); |
| 423 |
params.hull.points = &convex_points[0]; |
| 424 |
params.hull.num_points = convex_points.size(); |
| 425 |
|
| 426 |
tree.cull_convex(params); |
| 427 |
|
| 428 |
return params.result_count_overall; |
| 429 |
} |
| 430 |
|
| 431 |
private: |
| 432 |
|
| 433 |
void _check_for_collisions(bool p_full_check = false) { |
| 434 |
if (!changed_items.size()) { |
| 435 |
|
| 436 |
return; |
| 437 |
} |
| 438 |
|
| 439 |
typename BVHTREE_CLASS::CullParams params; |
| 440 |
|
| 441 |
params.result_count_overall = 0; |
| 442 |
params.result_max = INT_MAX; |
| 443 |
params.result_array = nullptr; |
| 444 |
params.subindex_array = nullptr; |
| 445 |
|
| 446 |
for (const BVHHandle &h : changed_items) { |
| 447 |
|
| 448 |
const BOUNDS &expanded_aabb = tree._pairs[h.id()].expanded_aabb; |
| 449 |
BVHABB_CLASS abb; |
| 450 |
abb.from(expanded_aabb); |
| 451 |
|
| 452 |
tree.item_fill_cullparams(h, params); |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
_find_leavers(h, abb, p_full_check); |
| 457 |
|
| 458 |
uint32_t changed_item_ref_id = h.id(); |
| 459 |
|
| 460 |
params.abb = abb; |
| 461 |
|
| 462 |
params.result_count_overall = 0; |
| 463 |
tree.cull_aabb(params, false); |
| 464 |
|
| 465 |
for (const uint32_t ref_id : tree._cull_hits) { |
| 466 |
|
| 467 |
if (ref_id == changed_item_ref_id) { |
| 468 |
continue; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
BVHHandle h_collidee; |
| 473 |
h_collidee.set_id(ref_id); |
| 474 |
|
| 475 |
|
| 476 |
_collide(h, h_collidee); |
| 477 |
} |
| 478 |
} |
| 479 |
_reset(); |
| 480 |
} |
| 481 |
|
| 482 |
public: |
| 483 |
void item_get_AABB(BVHHandle p_handle, BOUNDS &r_aabb) { |
| 484 |
DEV_ASSERT(!p_handle.is_invalid()); |
| 485 |
BVHABB_CLASS abb; |
| 486 |
tree.item_get_ABB(p_handle, abb); |
| 487 |
abb.to(r_aabb); |
| 488 |
} |
| 489 |
|
| 490 |
private: |
| 491 |
|
| 492 |
uint32_t item_get_tree_id(BVHHandle p_handle) const { return _get_extra(p_handle).tree_id; } |
| 493 |
T *item_get_userdata(BVHHandle p_handle) const { return _get_extra(p_handle).userdata; } |
| 494 |
int item_get_subindex(BVHHandle p_handle) const { return _get_extra(p_handle).subindex; } |
| 495 |
|
| 496 |
void _unpair(BVHHandle p_from, BVHHandle p_to) { |
| 497 |
tree._handle_sort(p_from, p_to); |
| 498 |
|
| 499 |
typename BVHTREE_CLASS::ItemExtra &exa = tree._extra[p_from.id()]; |
| 500 |
typename BVHTREE_CLASS::ItemExtra &exb = tree._extra[p_to.id()]; |
| 501 |
|
| 502 |
|
| 503 |
if ((exa.userdata == exb.userdata) && exa.userdata) { |
| 504 |
return; |
| 505 |
} |
| 506 |
|
| 507 |
typename BVHTREE_CLASS::ItemPairs &pairs_from = tree._pairs[p_from.id()]; |
| 508 |
typename BVHTREE_CLASS::ItemPairs &pairs_to = tree._pairs[p_to.id()]; |
| 509 |
|
| 510 |
void *ud_from = pairs_from.remove_pair_to(p_to); |
| 511 |
pairs_to.remove_pair_to(p_from); |
| 512 |
|
| 513 |
#ifdef BVH_VERBOSE_PAIRING |
| 514 |
print_line("_unpair " + itos(p_from.id()) + " from " + itos(p_to.id())); |
| 515 |
#endif |
| 516 |
|
| 517 |
|
| 518 |
if (unpair_callback) { |
| 519 |
unpair_callback(pair_callback_userdata, p_from, exa.userdata, exa.subindex, p_to, exb.userdata, exb.subindex, ud_from); |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
void *_recheck_pair(BVHHandle p_from, BVHHandle p_to, void *p_pair_data) { |
| 524 |
tree._handle_sort(p_from, p_to); |
| 525 |
|
| 526 |
typename BVHTREE_CLASS::ItemExtra &exa = tree._extra[p_from.id()]; |
| 527 |
typename BVHTREE_CLASS::ItemExtra &exb = tree._extra[p_to.id()]; |
| 528 |
|
| 529 |
|
| 530 |
if ((exa.userdata == exb.userdata) && exa.userdata) { |
| 531 |
return p_pair_data; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
if (check_pair_callback) { |
| 536 |
return check_pair_callback(check_pair_callback_userdata, p_from, exa.userdata, exa.subindex, p_to, exb.userdata, exb.subindex, p_pair_data); |
| 537 |
} |
| 538 |
|
| 539 |
return p_pair_data; |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
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) { |
| 544 |
BVHABB_CLASS abb_to; |
| 545 |
tree.item_get_ABB(p_to, abb_to); |
| 546 |
|
| 547 |
|
| 548 |
if (p_abb_from.intersects(abb_to)) { |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
if (!p_full_check) { |
| 553 |
return false; |
| 554 |
} |
| 555 |
const typename BVHTREE_CLASS::ItemExtra &exa = _get_extra(p_from); |
| 556 |
const typename BVHTREE_CLASS::ItemExtra &exb = _get_extra(p_to); |
| 557 |
|
| 558 |
|
| 559 |
if (exa.are_item_trees_compatible(exb)) { |
| 560 |
bool pair_allowed = USER_PAIR_TEST_FUNCTION::user_pair_check(exa.userdata, exb.userdata); |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
if (pair_allowed) { |
| 565 |
return false; |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
_unpair(p_from, p_to); |
| 571 |
return true; |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
void _find_leavers(BVHHandle p_handle, const BVHABB_CLASS &expanded_abb_from, bool p_full_check) { |
| 577 |
typename BVHTREE_CLASS::ItemPairs &p_from = tree._pairs[p_handle.id()]; |
| 578 |
|
| 579 |
BVHABB_CLASS abb_from = expanded_abb_from; |
| 580 |
|
| 581 |
|
| 582 |
for (unsigned int n = 0; n < p_from.extended_pairs.size(); n++) { |
| 583 |
BVHHandle h_to = p_from.extended_pairs[n].handle; |
| 584 |
if (_find_leavers_process_pair(p_from, abb_from, p_handle, h_to, p_full_check)) { |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
n--; |
| 589 |
} |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
void _collide(BVHHandle p_ha, BVHHandle p_hb) { |
| 596 |
|
| 597 |
tree._handle_sort(p_ha, p_hb); |
| 598 |
|
| 599 |
const typename BVHTREE_CLASS::ItemExtra &exa = _get_extra(p_ha); |
| 600 |
const typename BVHTREE_CLASS::ItemExtra &exb = _get_extra(p_hb); |
| 601 |
|
| 602 |
|
| 603 |
if (!USER_PAIR_TEST_FUNCTION::user_pair_check(exa.userdata, exb.userdata)) { |
| 604 |
return; |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
if ((exa.userdata == exb.userdata) && exa.userdata) { |
| 609 |
return; |
| 610 |
} |
| 611 |
|
| 612 |
typename BVHTREE_CLASS::ItemPairs &p_from = tree._pairs[p_ha.id()]; |
| 613 |
typename BVHTREE_CLASS::ItemPairs &p_to = tree._pairs[p_hb.id()]; |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
if (p_from.num_pairs <= p_to.num_pairs) { |
| 618 |
if (p_from.contains_pair_to(p_hb)) { |
| 619 |
return; |
| 620 |
} |
| 621 |
} else { |
| 622 |
if (p_to.contains_pair_to(p_ha)) { |
| 623 |
return; |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
void *callback_userdata = nullptr; |
| 629 |
|
| 630 |
#ifdef BVH_VERBOSE_PAIRING |
| 631 |
print_line("_pair " + itos(p_ha.id()) + " to " + itos(p_hb.id())); |
| 632 |
#endif |
| 633 |
|
| 634 |
if (pair_callback) { |
| 635 |
callback_userdata = pair_callback(pair_callback_userdata, p_ha, exa.userdata, exa.subindex, p_hb, exb.userdata, exb.subindex); |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
p_from.add_pair_to(p_hb, callback_userdata); |
| 640 |
p_to.add_pair_to(p_ha, callback_userdata); |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
void _remove_pairs_containing(BVHHandle p_handle) { |
| 645 |
typename BVHTREE_CLASS::ItemPairs &p_from = tree._pairs[p_handle.id()]; |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
while (p_from.extended_pairs.size()) { |
| 650 |
BVHHandle h_to = p_from.extended_pairs[0].handle; |
| 651 |
_unpair(p_handle, h_to); |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
void _recheck_pairs(BVHHandle p_handle) { |
| 657 |
typename BVHTREE_CLASS::ItemPairs &from = tree._pairs[p_handle.id()]; |
| 658 |
|
| 659 |
|
| 660 |
for (unsigned int n = 0; n < from.extended_pairs.size(); n++) { |
| 661 |
typename BVHTREE_CLASS::ItemPairs::Link &pair = from.extended_pairs[n]; |
| 662 |
BVHHandle h_to = pair.handle; |
| 663 |
void *new_pair_data = _recheck_pair(p_handle, h_to, pair.userdata); |
| 664 |
|
| 665 |
if (new_pair_data != pair.userdata) { |
| 666 |
pair.userdata = new_pair_data; |
| 667 |
|
| 668 |
|
| 669 |
typename BVHTREE_CLASS::ItemPairs &to = tree._pairs[h_to.id()]; |
| 670 |
for (unsigned int to_index = 0; to_index < to.extended_pairs.size(); to_index++) { |
| 671 |
typename BVHTREE_CLASS::ItemPairs::Link &to_pair = to.extended_pairs[to_index]; |
| 672 |
if (to_pair.handle == p_handle) { |
| 673 |
to_pair.userdata = new_pair_data; |
| 674 |
break; |
| 675 |
} |
| 676 |
} |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
private: |
| 682 |
const typename BVHTREE_CLASS::ItemExtra &_get_extra(BVHHandle p_handle) const { |
| 683 |
return tree._extra[p_handle.id()]; |
| 684 |
} |
| 685 |
const typename BVHTREE_CLASS::ItemRef &_get_ref(BVHHandle p_handle) const { |
| 686 |
return tree._refs[p_handle.id()]; |
| 687 |
} |
| 688 |
|
| 689 |
void _reset() { |
| 690 |
changed_items.clear(); |
| 691 |
_tick++; |
| 692 |
} |
| 693 |
|
| 694 |
void _add_changed_item(BVHHandle p_handle, const BOUNDS &aabb, bool p_check_aabb = true) { |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
#ifdef BVH_EXPAND_LEAF_AABBS |
| 699 |
|
| 700 |
BOUNDS &expanded_aabb = tree._pairs[p_handle.id()].expanded_aabb; |
| 701 |
item_get_AABB(p_handle, expanded_aabb); |
| 702 |
#else |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
BOUNDS &expanded_aabb = tree._pairs[p_handle.id()].expanded_aabb; |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
if (p_check_aabb && tree.expanded_aabb_encloses_not_shrink(expanded_aabb, aabb)) { |
| 712 |
return; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
expanded_aabb = aabb; |
| 718 |
expanded_aabb.grow_by(tree._pairing_expansion); |
| 719 |
#endif |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
uint32_t &last_updated_tick = tree._extra[p_handle.id()].last_updated_tick; |
| 724 |
|
| 725 |
if (last_updated_tick == _tick) { |
| 726 |
return; |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
last_updated_tick = _tick; |
| 731 |
|
| 732 |
|
| 733 |
changed_items.push_back(p_handle); |
| 734 |
} |
| 735 |
|
| 736 |
void _remove_changed_item(BVHHandle p_handle) { |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
_remove_pairs_containing(p_handle); |
| 743 |
|
| 744 |
|
| 745 |
for (int n = 0; n < (int)changed_items.size(); n++) { |
| 746 |
if (changed_items[n] == p_handle) { |
| 747 |
changed_items.remove_at_unordered(n); |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
n--; |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
tree._extra[p_handle.id()].last_updated_tick = 0; |
| 759 |
} |
| 760 |
|
| 761 |
PairCallback pair_callback = nullptr; |
| 762 |
UnpairCallback unpair_callback = nullptr; |
| 763 |
CheckPairCallback check_pair_callback = nullptr; |
| 764 |
void *pair_callback_userdata = nullptr; |
| 765 |
void *unpair_callback_userdata = nullptr; |
| 766 |
void *check_pair_callback_userdata = nullptr; |
| 767 |
|
| 768 |
BVHTREE_CLASS tree; |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
LocalVector<BVHHandle> changed_items; |
| 773 |
uint32_t _tick = 1; |
| 774 |
|
| 775 |
class BVHLockedFunction { |
| 776 |
public: |
| 777 |
BVHLockedFunction(Mutex *p_mutex, bool p_thread_safe) { |
| 778 |
|
| 779 |
if (p_thread_safe) { |
| 780 |
_mutex = p_mutex; |
| 781 |
_mutex->lock(); |
| 782 |
|
| 783 |
} else { |
| 784 |
_mutex = nullptr; |
| 785 |
} |
| 786 |
} |
| 787 |
~BVHLockedFunction() { |
| 788 |
|
| 789 |
if (_mutex) { |
| 790 |
_mutex->unlock(); |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
private: |
| 795 |
Mutex *_mutex = nullptr; |
| 796 |
}; |
| 797 |
|
| 798 |
Mutex _mutex; |
| 799 |
|
| 800 |
|
| 801 |
bool _thread_safe = BVH_THREAD_SAFE; |
| 802 |
}; |
| 803 |
|
| 804 |
#undef BVHTREE_CLASS |