| 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 |
#include "core/math/aabb.h" |
| 42 |
#include "core/math/bvh_abb.h" |
| 43 |
#include "core/math/vector3.h" |
| 44 |
#include "core/templates/local_vector.h" |
| 45 |
#include "core/templates/pooled_list.h" |
| 46 |
|
| 47 |
#include <climits> |
| 48 |
|
| 49 |
#define BVHABB_CLASS BVH_ABB<BOUNDS, POINT> |
| 50 |
|
| 51 |
|
| 52 |
#define BVH_EXPAND_LEAF_AABBS |
| 53 |
|
| 54 |
|
| 55 |
#ifdef DEV_ENABLED |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
#endif |
| 65 |
|
| 66 |
|
| 67 |
#ifdef BVH_CHECKS |
| 68 |
#define BVH_ASSERT(a) CRASH_COND((a) == false) |
| 69 |
#else |
| 70 |
#define BVH_ASSERT(a) |
| 71 |
#endif |
| 72 |
|
| 73 |
#ifdef BVH_VERBOSE |
| 74 |
#define VERBOSE_PRINT print_line |
| 75 |
#else |
| 76 |
#define VERBOSE_PRINT(a) |
| 77 |
#endif |
| 78 |
|
| 79 |
|
| 80 |
struct BVHCommon { |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
static const uint32_t INVALID = (0xffffffff); |
| 85 |
static const uint32_t INACTIVE = (0xfffffffe); |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
struct BVHHandle { |
| 92 |
|
| 93 |
operator uint32_t() const { return _data; } |
| 94 |
void set(uint32_t p_value) { _data = p_value; } |
| 95 |
|
| 96 |
uint32_t _data; |
| 97 |
|
| 98 |
void set_invalid() { _data = BVHCommon::INVALID; } |
| 99 |
bool is_invalid() const { return _data == BVHCommon::INVALID; } |
| 100 |
uint32_t id() const { return _data; } |
| 101 |
void set_id(uint32_t p_id) { _data = p_id; } |
| 102 |
|
| 103 |
bool operator==(const BVHHandle &p_h) const { return _data == p_h._data; } |
| 104 |
bool operator!=(const BVHHandle &p_h) const { return (*this == p_h) == false; } |
| 105 |
}; |
| 106 |
|
| 107 |
|
| 108 |
template <typename T> |
| 109 |
class BVH_IterativeInfo { |
| 110 |
public: |
| 111 |
constexpr static const size_t ALLOCA_STACK_SIZE = 128; |
| 112 |
|
| 113 |
int32_t depth = 1; |
| 114 |
int32_t threshold = ALLOCA_STACK_SIZE - 2; |
| 115 |
T *stack; |
| 116 |
|
| 117 |
|
| 118 |
LocalVector<T> aux_stack; |
| 119 |
int32_t get_alloca_stacksize() const { return ALLOCA_STACK_SIZE * sizeof(T); } |
| 120 |
|
| 121 |
T *get_first() const { |
| 122 |
return &stack[0]; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
bool pop(T &r_value) { |
| 127 |
if (!depth) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
depth--; |
| 132 |
r_value = stack[depth]; |
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
T *request() { |
| 138 |
if (depth > threshold) { |
| 139 |
if (aux_stack.is_empty()) { |
| 140 |
aux_stack.resize(ALLOCA_STACK_SIZE * 2); |
| 141 |
memcpy(aux_stack.ptr(), stack, get_alloca_stacksize()); |
| 142 |
} else { |
| 143 |
aux_stack.resize(aux_stack.size() * 2); |
| 144 |
} |
| 145 |
stack = aux_stack.ptr(); |
| 146 |
threshold = aux_stack.size() - 2; |
| 147 |
} |
| 148 |
return &stack[depth++]; |
| 149 |
} |
| 150 |
}; |
| 151 |
|
| 152 |
template <typename T> |
| 153 |
class BVH_DummyPairTestFunction { |
| 154 |
public: |
| 155 |
static bool user_collision_check(T *p_a, T *p_b) { |
| 156 |
|
| 157 |
return true; |
| 158 |
} |
| 159 |
}; |
| 160 |
|
| 161 |
template <typename T> |
| 162 |
class BVH_DummyCullTestFunction { |
| 163 |
public: |
| 164 |
static bool user_cull_check(T *p_a, T *p_b) { |
| 165 |
|
| 166 |
return true; |
| 167 |
} |
| 168 |
}; |
| 169 |
|
| 170 |
template <typename T, int NUM_TREES, int MAX_CHILDREN, int MAX_ITEMS, typename USER_PAIR_TEST_FUNCTION = BVH_DummyPairTestFunction<T>, typename USER_CULL_TEST_FUNCTION = BVH_DummyCullTestFunction<T>, bool USE_PAIRS = false, typename BOUNDS = AABB, typename POINT = Vector3> |
| 171 |
class BVH_Tree { |
| 172 |
friend class BVH; |
| 173 |
|
| 174 |
#include "bvh_pair.inc" |
| 175 |
#include "bvh_structs.inc" |
| 176 |
|
| 177 |
public: |
| 178 |
BVH_Tree() { |
| 179 |
for (int n = 0; n < NUM_TREES; n++) { |
| 180 |
_root_node_id[n] = BVHCommon::INVALID; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
uint32_t dummy_leaf_id; |
| 186 |
_leaves.request(dummy_leaf_id); |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
params_set_pairing_expansion(0.1); |
| 192 |
} |
| 193 |
|
| 194 |
private: |
| 195 |
bool node_add_child(uint32_t p_node_id, uint32_t p_child_node_id) { |
| 196 |
TNode &tnode = _nodes[p_node_id]; |
| 197 |
if (tnode.is_full_of_children()) { |
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
tnode.children[tnode.num_children] = p_child_node_id; |
| 202 |
tnode.num_children += 1; |
| 203 |
|
| 204 |
|
| 205 |
TNode &tnode_child = _nodes[p_child_node_id]; |
| 206 |
tnode_child.parent_id = p_node_id; |
| 207 |
|
| 208 |
return true; |
| 209 |
} |
| 210 |
|
| 211 |
void node_replace_child(uint32_t p_parent_id, uint32_t p_old_child_id, uint32_t p_new_child_id) { |
| 212 |
TNode &parent = _nodes[p_parent_id]; |
| 213 |
BVH_ASSERT(!parent.is_leaf()); |
| 214 |
|
| 215 |
int child_num = parent.find_child(p_old_child_id); |
| 216 |
BVH_ASSERT(child_num != -1); |
| 217 |
parent.children[child_num] = p_new_child_id; |
| 218 |
|
| 219 |
TNode &new_child = _nodes[p_new_child_id]; |
| 220 |
new_child.parent_id = p_parent_id; |
| 221 |
} |
| 222 |
|
| 223 |
void node_remove_child(uint32_t p_parent_id, uint32_t p_child_id, uint32_t p_tree_id, bool p_prevent_sibling = false) { |
| 224 |
TNode &parent = _nodes[p_parent_id]; |
| 225 |
BVH_ASSERT(!parent.is_leaf()); |
| 226 |
|
| 227 |
int child_num = parent.find_child(p_child_id); |
| 228 |
BVH_ASSERT(child_num != -1); |
| 229 |
|
| 230 |
parent.remove_child_internal(child_num); |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
uint32_t sibling_id = 0; |
| 235 |
bool sibling_present = false; |
| 236 |
|
| 237 |
|
| 238 |
if (parent.num_children > 1) { |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
if (parent.num_children == 1) { |
| 244 |
|
| 245 |
sibling_id = parent.children[0]; |
| 246 |
sibling_present = true; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
uint32_t grandparent_id = parent.parent_id; |
| 253 |
|
| 254 |
|
| 255 |
if (grandparent_id == BVHCommon::INVALID) { |
| 256 |
if (sibling_present) { |
| 257 |
|
| 258 |
change_root_node(sibling_id, p_tree_id); |
| 259 |
|
| 260 |
|
| 261 |
node_free_node_and_leaf(p_parent_id); |
| 262 |
} |
| 263 |
|
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
if (sibling_present) { |
| 268 |
node_replace_child(grandparent_id, p_parent_id, sibling_id); |
| 269 |
} else { |
| 270 |
node_remove_child(grandparent_id, p_parent_id, p_tree_id, true); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
node_free_node_and_leaf(p_parent_id); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
void node_free_node_and_leaf(uint32_t p_node_id) { |
| 280 |
TNode &node = _nodes[p_node_id]; |
| 281 |
if (node.is_leaf()) { |
| 282 |
int leaf_id = node.get_leaf_id(); |
| 283 |
_leaves.free(leaf_id); |
| 284 |
} |
| 285 |
|
| 286 |
_nodes.free(p_node_id); |
| 287 |
} |
| 288 |
|
| 289 |
void change_root_node(uint32_t p_new_root_id, uint32_t p_tree_id) { |
| 290 |
_root_node_id[p_tree_id] = p_new_root_id; |
| 291 |
TNode &root = _nodes[p_new_root_id]; |
| 292 |
|
| 293 |
|
| 294 |
root.parent_id = BVHCommon::INVALID; |
| 295 |
} |
| 296 |
|
| 297 |
void node_make_leaf(uint32_t p_node_id) { |
| 298 |
uint32_t child_leaf_id; |
| 299 |
TLeaf *child_leaf = _leaves.request(child_leaf_id); |
| 300 |
child_leaf->clear(); |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
BVH_ASSERT(child_leaf_id != 0); |
| 305 |
|
| 306 |
TNode &node = _nodes[p_node_id]; |
| 307 |
node.neg_leaf_id = -(int)child_leaf_id; |
| 308 |
} |
| 309 |
|
| 310 |
void node_remove_item(uint32_t p_ref_id, uint32_t p_tree_id, BVHABB_CLASS *r_old_aabb = nullptr) { |
| 311 |
|
| 312 |
ItemRef &ref = _refs[p_ref_id]; |
| 313 |
uint32_t owner_node_id = ref.tnode_id; |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
if (owner_node_id == BVHCommon::INVALID) { |
| 318 |
return; |
| 319 |
} |
| 320 |
|
| 321 |
TNode &tnode = _nodes[owner_node_id]; |
| 322 |
CRASH_COND(!tnode.is_leaf()); |
| 323 |
|
| 324 |
TLeaf &leaf = _node_get_leaf(tnode); |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
const BVHABB_CLASS &old_aabb = leaf.get_aabb(ref.item_id); |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
BVHABB_CLASS node_bound = tnode.aabb; |
| 337 |
node_bound.expand(-_node_expansion - 0.001f); |
| 338 |
bool refit = true; |
| 339 |
|
| 340 |
if (node_bound.is_other_within(old_aabb)) { |
| 341 |
refit = false; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
if (r_old_aabb) { |
| 346 |
*r_old_aabb = old_aabb; |
| 347 |
} |
| 348 |
|
| 349 |
leaf.remove_item_unordered(ref.item_id); |
| 350 |
|
| 351 |
if (leaf.num_items) { |
| 352 |
|
| 353 |
uint32_t swapped_ref_id = leaf.get_item_ref_id(ref.item_id); |
| 354 |
|
| 355 |
ItemRef &swapped_ref = _refs[swapped_ref_id]; |
| 356 |
|
| 357 |
swapped_ref.item_id = ref.item_id; |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
if (refit) { |
| 363 |
leaf.set_dirty(true); |
| 364 |
} |
| 365 |
} else { |
| 366 |
|
| 367 |
|
| 368 |
if (tnode.parent_id != BVHCommon::INVALID) { |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
uint32_t parent_id = tnode.parent_id; |
| 373 |
|
| 374 |
node_remove_child(parent_id, owner_node_id, p_tree_id); |
| 375 |
refit_upward(parent_id); |
| 376 |
|
| 377 |
|
| 378 |
node_free_node_and_leaf(owner_node_id); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
} |
| 383 |
|
| 384 |
ref.tnode_id = BVHCommon::INVALID; |
| 385 |
ref.item_id = BVHCommon::INVALID; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
bool _node_add_item(uint32_t p_node_id, uint32_t p_ref_id, const BVHABB_CLASS &p_aabb) { |
| 391 |
ItemRef &ref = _refs[p_ref_id]; |
| 392 |
ref.tnode_id = p_node_id; |
| 393 |
|
| 394 |
TNode &node = _nodes[p_node_id]; |
| 395 |
BVH_ASSERT(node.is_leaf()); |
| 396 |
TLeaf &leaf = _node_get_leaf(node); |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
bool needs_refit = true; |
| 402 |
|
| 403 |
|
| 404 |
BVHABB_CLASS expanded = p_aabb; |
| 405 |
expanded.expand(_node_expansion); |
| 406 |
|
| 407 |
|
| 408 |
if (leaf.num_items) { |
| 409 |
if (node.aabb.is_other_within(expanded)) { |
| 410 |
|
| 411 |
needs_refit = false; |
| 412 |
} else { |
| 413 |
node.aabb.merge(expanded); |
| 414 |
} |
| 415 |
} else { |
| 416 |
|
| 417 |
node.aabb = expanded; |
| 418 |
} |
| 419 |
|
| 420 |
ref.item_id = leaf.request_item(); |
| 421 |
BVH_ASSERT(ref.item_id != BVHCommon::INVALID); |
| 422 |
|
| 423 |
|
| 424 |
leaf.get_aabb(ref.item_id) = p_aabb; |
| 425 |
|
| 426 |
|
| 427 |
leaf.get_item_ref_id(ref.item_id) = p_ref_id; |
| 428 |
|
| 429 |
return needs_refit; |
| 430 |
} |
| 431 |
|
| 432 |
uint32_t _node_create_another_child(uint32_t p_node_id, const BVHABB_CLASS &p_aabb) { |
| 433 |
uint32_t child_node_id; |
| 434 |
TNode *child_node = _nodes.request(child_node_id); |
| 435 |
child_node->clear(); |
| 436 |
|
| 437 |
|
| 438 |
child_node->aabb = p_aabb; |
| 439 |
|
| 440 |
node_add_child(p_node_id, child_node_id); |
| 441 |
|
| 442 |
return child_node_id; |
| 443 |
} |
| 444 |
|
| 445 |
#include "bvh_cull.inc" |
| 446 |
#include "bvh_debug.inc" |
| 447 |
#include "bvh_integrity.inc" |
| 448 |
#include "bvh_logic.inc" |
| 449 |
#include "bvh_misc.inc" |
| 450 |
#include "bvh_public.inc" |
| 451 |
#include "bvh_refit.inc" |
| 452 |
#include "bvh_split.inc" |
| 453 |
}; |
| 454 |
|
| 455 |
#undef VERBOSE_PRINT |