38 #ifndef COAL_HIERARCHY_TREE_INL_H
39 #define COAL_HIERARCHY_TREE_INL_H
48 template <
typename BV>
53 max_lookahead_level = -1;
55 bu_threshold = bu_threshold_;
56 topdown_level = topdown_level_;
60 template <
typename BV>
66 template <
typename BV>
87 template <
typename BV>
90 Node* leaf = createNode(
nullptr, bv,
data);
91 insertLeaf(root_node, leaf);
97 template <
typename BV>
105 template <
typename BV>
107 if (root_node) recurseDeleteNode(root_node);
111 max_lookahead_level = -1;
116 template <
typename BV>
118 return (
nullptr == root_node);
122 template <
typename BV>
135 if (lookahead_level > 0) {
137 for (
int i = 0; (i < lookahead_level) && sub_root->
parent; ++i)
138 sub_root = sub_root->
parent;
142 sub_root = root_node;
146 insertLeaf(sub_root, leaf);
150 template <
typename BV>
152 if (leaf->
bv.contain(bv))
return false;
158 template <
typename S,
typename BV>
163 if (leaf->
bv.contain(bv))
return false;
171 if (leaf->
bv.contain(bv))
return false;
178 template <
typename BV>
185 template <
typename BV>
191 template <
typename BV>
193 if (!root_node)
return 0;
194 return getMaxHeight(root_node);
198 template <
typename BV>
200 if (!root_node)
return 0;
203 getMaxDepth(root_node, 0, max_depth);
208 template <
typename BV>
211 std::vector<Node*> leaves;
212 leaves.reserve(n_leaves);
213 fetchLeaves(root_node, leaves);
214 bottomup(leaves.begin(), leaves.end());
215 root_node = leaves[0];
220 template <
typename BV>
223 std::vector<Node*> leaves;
224 leaves.reserve(n_leaves);
225 fetchLeaves(root_node, leaves);
226 root_node = topdown(leaves.begin(), leaves.end());
231 template <
typename BV>
233 if (iterations < 0) iterations = (int)n_leaves;
234 if (root_node && (iterations > 0)) {
235 for (
int i = 0; i < iterations; ++i) {
236 Node* node = root_node;
237 unsigned int bit = 0;
239 node = sort(node, root_node)->
children[(opath >> bit) & 1];
240 bit = (bit + 1) & (
sizeof(
unsigned int) * 8 - 1);
249 template <
typename BV>
251 if (root_node) recurseRefit(root_node);
255 template <
typename BV>
257 std::vector<Node*>& leaves)
const {
259 extractLeaves(root->
children[0], leaves);
260 extractLeaves(root->
children[1], leaves);
262 leaves.push_back(root);
266 template <
typename BV>
272 template <
typename BV>
278 template <
typename BV>
284 template <
typename BV>
286 for (
int i = 0; i < depth; ++i) std::cout <<
" ";
287 std::cout <<
" (" << root->
bv.min_[0] <<
", " << root->
bv.min_[1] <<
", "
288 << root->
bv.min_[2] <<
"; " << root->
bv.max_[0] <<
", "
289 << root->
bv.max_[1] <<
", " << root->
bv.max_[2] <<
")" << std::endl;
298 template <
typename BV>
302 while (lbeg < lcur_end - 1) {
305 CoalScalar min_size = (std::numeric_limits<CoalScalar>::max)();
308 CoalScalar cur_size = ((*it1)->bv + (*it2)->bv).size();
309 if (cur_size < min_size) {
317 Node* n[2] = {*min_it1, *min_it2};
318 Node* p = createNode(
nullptr, n[0]->bv, n[1]->bv,
nullptr);
324 Node* tmp = *min_it2;
326 *min_it2 = *lcur_end;
332 template <
typename BV>
335 switch (topdown_level) {
337 return topdown_0(lbeg, lend);
340 return topdown_1(lbeg, lend);
343 return topdown_0(lbeg, lend);
348 template <
typename BV>
351 size_t height1 = getMaxHeight(node->
children[0]);
352 size_t height2 = getMaxHeight(node->
children[1]);
353 return std::max(height1, height2) + 1;
359 template <
typename BV>
361 size_t& max_depth)
const {
363 getMaxDepth(node->
children[0], depth + 1, max_depth);
364 getMaxDepth(node->
children[1], depth + 1, max_depth);
366 max_depth = std::max(max_depth, depth);
370 template <
typename BV>
373 long num_leaves = lend - lbeg;
374 if (num_leaves > 1) {
375 if (num_leaves > bu_threshold) {
376 BV vol = (*lbeg)->
bv;
380 CoalScalar extent[3] = {vol.width(), vol.height(), vol.depth()};
381 if (extent[1] > extent[0]) best_axis = 1;
382 if (extent[2] > extent[best_axis]) best_axis = 2;
386 std::nth_element(lbeg, lcenter, lend,
387 std::bind(&nodeBaseLess<BV>, std::placeholders::_1,
388 std::placeholders::_2, std::ref(best_axis)));
390 Node* node = createNode(
nullptr, vol,
nullptr);
391 node->
children[0] = topdown_0(lbeg, lcenter);
392 node->
children[1] = topdown_0(lcenter, lend);
397 bottomup(lbeg, lend);
405 template <
typename BV>
408 long num_leaves = lend - lbeg;
409 if (num_leaves > 1) {
410 if (num_leaves > bu_threshold) {
411 Vec3s split_p = (*lbeg)->bv.center();
412 BV vol = (*lbeg)->bv;
414 for (it = lbeg + 1; it < lend; ++it) {
415 split_p += (*it)->bv.center();
418 split_p /=
static_cast<CoalScalar>(num_leaves);
420 long bestmidp = num_leaves;
421 int splitcount[3][2] = {{0, 0}, {0, 0}, {0, 0}};
422 for (it = lbeg; it < lend; ++it) {
423 Vec3s x = (*it)->bv.center() - split_p;
424 for (
int j = 0; j < 3; ++j) ++splitcount[j][x[j] > 0 ? 1 : 0];
427 for (
int i = 0; i < 3; ++i) {
428 if ((splitcount[i][0] > 0) && (splitcount[i][1] > 0)) {
429 long midp = std::abs(splitcount[i][0] - splitcount[i][1]);
430 if (midp < bestmidp) {
437 if (best_axis < 0) best_axis = 0;
441 for (it = lbeg; it < lend; ++it) {
442 if ((*it)->bv.center()[best_axis] < split_value) {
450 Node* node = createNode(
nullptr, vol,
nullptr);
451 node->
children[0] = topdown_1(lbeg, lcenter);
452 node->
children[1] = topdown_1(lcenter, lend);
457 bottomup(lbeg, lend);
465 template <
typename BV>
468 root_node = topdown(leaves.begin(), leaves.end());
469 n_leaves = leaves.size();
470 max_lookahead_level = -1;
475 template <
typename BV>
480 if (leaves.size() > 0) bound_bv = leaves[0]->bv;
481 for (
size_t i = 1; i < leaves.size(); ++i) bound_bv += leaves[i]->bv;
483 morton_functor<CoalScalar, uint32_t> coder(bound_bv);
484 for (
size_t i = 0; i < leaves.size(); ++i)
485 leaves[i]->code = coder(leaves[i]->bv.center());
487 std::sort(leaves.begin(), leaves.end(), SortByMorton());
489 root_node = mortonRecurse_0(leaves.begin(), leaves.end(),
490 (1 << (coder.bits() - 1)), coder.bits() - 1);
493 n_leaves = leaves.size();
494 max_lookahead_level = -1;
499 template <
typename BV>
504 if (leaves.size() > 0) bound_bv = leaves[0]->bv;
505 for (
size_t i = 1; i < leaves.size(); ++i) bound_bv += leaves[i]->bv;
507 morton_functor<CoalScalar, uint32_t> coder(bound_bv);
508 for (
size_t i = 0; i < leaves.size(); ++i)
509 leaves[i]->code = coder(leaves[i]->bv.center());
511 std::sort(leaves.begin(), leaves.end(), SortByMorton());
513 root_node = mortonRecurse_1(leaves.begin(), leaves.end(),
514 (1 << (coder.bits() - 1)), coder.bits() - 1);
517 n_leaves = leaves.size();
518 max_lookahead_level = -1;
523 template <
typename BV>
528 if (leaves.size() > 0) bound_bv = leaves[0]->bv;
529 for (
size_t i = 1; i < leaves.size(); ++i) bound_bv += leaves[i]->bv;
531 morton_functor<CoalScalar, uint32_t> coder(bound_bv);
532 for (
size_t i = 0; i < leaves.size(); ++i)
533 leaves[i]->code = coder(leaves[i]->bv.center());
535 std::sort(leaves.begin(), leaves.end(), SortByMorton());
537 root_node = mortonRecurse_2(leaves.begin(), leaves.end());
540 n_leaves = leaves.size();
541 max_lookahead_level = -1;
546 template <
typename BV>
549 const uint32_t& split,
int bits) {
550 long num_leaves = lend - lbeg;
551 if (num_leaves > 1) {
556 std::lower_bound(lbeg, lend, &dummy, SortByMorton());
558 if (lcenter == lbeg) {
559 uint32_t split2 = split | (1 << (bits - 1));
560 return mortonRecurse_0(lbeg, lend, split2, bits - 1);
561 }
else if (lcenter == lend) {
562 uint32_t split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
563 return mortonRecurse_0(lbeg, lend, split1, bits - 1);
565 uint32_t split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
566 uint32_t split2 = split | (1 << (bits - 1));
568 Node* child1 = mortonRecurse_0(lbeg, lcenter, split1, bits - 1);
569 Node* child2 = mortonRecurse_0(lcenter, lend, split2, bits - 1);
570 Node* node = createNode(
nullptr,
nullptr);
578 Node* node = topdown(lbeg, lend);
586 template <
typename BV>
589 const uint32_t& split,
int bits) {
590 long num_leaves = lend - lbeg;
591 if (num_leaves > 1) {
596 std::lower_bound(lbeg, lend, &dummy, SortByMorton());
598 if (lcenter == lbeg) {
599 uint32_t split2 = split | (1 << (bits - 1));
600 return mortonRecurse_1(lbeg, lend, split2, bits - 1);
601 }
else if (lcenter == lend) {
602 uint32_t split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
603 return mortonRecurse_1(lbeg, lend, split1, bits - 1);
605 uint32_t split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
606 uint32_t split2 = split | (1 << (bits - 1));
608 Node* child1 = mortonRecurse_1(lbeg, lcenter, split1, bits - 1);
609 Node* child2 = mortonRecurse_1(lcenter, lend, split2, bits - 1);
610 Node* node = createNode(
nullptr,
nullptr);
618 Node* child1 = mortonRecurse_1(lbeg, lbeg + num_leaves / 2, 0, bits - 1);
619 Node* child2 = mortonRecurse_1(lbeg + num_leaves / 2, lend, 0, bits - 1);
620 Node* node = createNode(
nullptr,
nullptr);
632 template <
typename BV>
635 long num_leaves = lend - lbeg;
636 if (num_leaves > 1) {
637 Node* child1 = mortonRecurse_2(lbeg, lbeg + num_leaves / 2);
638 Node* child2 = mortonRecurse_2(lbeg + num_leaves / 2, lend);
639 Node* node = createNode(
nullptr,
nullptr);
650 template <
typename BV>
652 Node* root = removeLeaf(leaf);
654 if (max_lookahead_level >= 0) {
655 for (
int i = 0; (i < max_lookahead_level) && root->
parent; ++i)
662 insertLeaf(root, leaf);
666 template <
typename BV>
670 size_t i = indexOf(n);
675 q->children[indexOf(p)] = n;
687 std::swap(p->
bv, n->
bv);
694 template <
typename BV>
714 Node* sibling = sub_root;
715 while (!sibling->
isLeaf()) {
722 Node* node = createNode(prev, leaf->
bv, sibling->
bv,
nullptr);
736 prev->
children[indexOf(sibling)] = node;
746 if (!prev->
bv.contain(node->
bv))
751 }
while (
nullptr != (prev = node->
parent));
775 template <
typename BV>
782 if (leaf == root_node) {
807 prev->
children[indexOf(parent)] = sibling;
813 if (!(new_bv == prev->
bv)) {
820 return prev ? prev : root_node;
833 sibling->
parent =
nullptr;
840 template <
typename BV>
843 if ((!root->
isLeaf()) && depth) {
844 fetchLeaves(root->
children[0], leaves, depth - 1);
845 fetchLeaves(root->
children[1], leaves, depth - 1);
848 leaves.push_back(root);
853 template <
typename BV>
856 return (node->
parent->children[1] == node);
860 template <
typename BV>
864 Node* node = createNode(parent,
data);
870 template <
typename BV>
875 Node* node = createNode(parent,
data);
876 node->
bv = bv1 + bv2;
881 template <
typename BV>
884 Node* node =
nullptr;
897 template <
typename BV>
899 if (free_node != node) {
906 template <
typename BV>
909 recurseDeleteNode(node->
children[0]);
910 recurseDeleteNode(node->
children[1]);
913 if (node == root_node) root_node =
nullptr;
918 template <
typename BV>
929 template <
typename BV>
931 if (
a->bv.center()[
d] <
b->bv.center()[
d])
return true;
936 template <
typename S,
typename BV>
951 template <
typename BV>
958 template <
typename BV>
965 template <
typename S>
971 const AABB& bv1 = node1.
bv;
972 const AABB& bv2 = node2.
bv;
976 CoalScalar d1 = fabs(v1[0]) + fabs(v1[1]) + fabs(v1[2]);
977 CoalScalar d2 = fabs(v2[0]) + fabs(v2[1]) + fabs(v2[2]);
978 return (
d1 < d2) ? 0 : 1;
983 const AABB& bv = query;
984 const AABB& bv1 = node1.
bv;
985 const AABB& bv2 = node2.
bv;
989 CoalScalar d1 = fabs(v1[0]) + fabs(v1[1]) + fabs(v1[2]);
990 CoalScalar d2 = fabs(v2[0]) + fabs(v2[1]) + fabs(v2[2]);
991 return (
d1 < d2) ? 0 : 1;