38 #ifndef FCL_HIERARCHY_TREE_INL_H
39 #define FCL_HIERARCHY_TREE_INL_H
56 max_lookahead_level = -1;
58 bu_threshold = bu_threshold_;
59 topdown_level = topdown_level_;
96 NodeType* leaf = createNode(
nullptr, bv, data);
97 insertLeaf(root_node, leaf);
103 template<
typename BV>
112 template<
typename BV>
116 recurseDeleteNode(root_node);
120 max_lookahead_level = -1;
125 template<
typename BV>
128 return (
nullptr == root_node);
132 template<
typename BV>
147 if(lookahead_level > 0)
150 for(
int i = 0; (i < lookahead_level) && sub_root->
parent; ++i)
151 sub_root = sub_root->
parent;
156 sub_root = root_node;
160 insertLeaf(sub_root, leaf);
164 template<
typename BV>
167 if(leaf->
bv.contain(bv))
return false;
173 template <
typename S,
typename BV>
183 if(leaf->
bv.contain(bv))
return false;
194 if(leaf->
bv.contain(bv))
return false;
201 template<
typename BV>
208 template<
typename BV>
215 template<
typename BV>
220 return getMaxHeight(root_node);
224 template<
typename BV>
227 if(!root_node)
return 0;
230 getMaxDepth(root_node, 0, max_depth);
235 template<
typename BV>
240 std::vector<NodeType*> leaves;
241 leaves.reserve(n_leaves);
242 fetchLeaves(root_node, leaves);
243 bottomup(leaves.begin(), leaves.end());
244 root_node = leaves[0];
249 template<
typename BV>
254 std::vector<NodeType*> leaves;
255 leaves.reserve(n_leaves);
256 fetchLeaves(root_node, leaves);
257 root_node = topdown(leaves.begin(), leaves.end());
262 template<
typename BV>
265 if(iterations < 0) iterations = n_leaves;
266 if(root_node && (iterations > 0))
268 for(
int i = 0; i < iterations; ++i)
271 unsigned int bit = 0;
274 node = sort(node, root_node)->
children[(opath>>bit)&1];
275 bit = (bit+1)&(
sizeof(
unsigned int) * 8-1);
284 template<
typename BV>
288 recurseRefit(root_node);
292 template<
typename BV>
297 extractLeaves(root->
children[0], leaves);
298 extractLeaves(root->
children[1], leaves);
301 leaves.push_back(root);
305 template<
typename BV>
312 template<
typename BV>
319 template<
typename BV>
326 template<
typename BV>
329 for(
int i = 0; i < depth; ++i)
331 std::cout <<
" (" << root->
bv.min_[0] <<
", " << root->
bv.min_[1] <<
", " << root->
bv.min_[2] <<
"; " << root->
bv.max_[0] <<
", " << root->
bv.max_[1] <<
", " << root->
bv.max_[2] <<
")" << std::endl;
343 template<
typename BV>
347 while(lbeg < lcur_end - 1)
355 S cur_size = ((*it1)->bv + (*it2)->bv).size();
356 if(cur_size < min_size)
365 NodeType* n[2] = {*min_it1, *min_it2};
366 NodeType* p = createNode(
nullptr, n[0]->bv, n[1]->bv,
nullptr);
374 *min_it2 = *lcur_end;
380 template<
typename BV>
383 switch(topdown_level)
386 return topdown_0(lbeg, lend);
389 return topdown_1(lbeg, lend);
392 return topdown_0(lbeg, lend);
397 template<
typename BV>
402 size_t height1 = getMaxHeight(node->
children[0]);
403 size_t height2 = getMaxHeight(node->
children[1]);
404 return std::max(height1, height2) + 1;
411 template<
typename BV>
416 getMaxDepth(node->
children[0], depth+1, max_depth);
417 getMaxDepth(node->
children[1], depth+1, max_depth);
420 max_depth =
std::max(max_depth, depth);
424 template<
typename BV>
427 int num_leaves = lend - lbeg;
430 if(num_leaves > bu_threshold)
432 BV vol = (*lbeg)->
bv;
437 S extent[3] = {vol.width(), vol.height(), vol.depth()};
438 if(extent[1] > extent[0]) best_axis = 1;
439 if(extent[2] > extent[best_axis]) best_axis = 2;
443 std::nth_element(lbeg, lcenter, lend, std::bind(&nodeBaseLess<BV>, std::placeholders::_1, std::placeholders::_2, std::ref(best_axis)));
445 NodeType* node = createNode(
nullptr, vol,
nullptr);
446 node->
children[0] = topdown_0(lbeg, lcenter);
447 node->
children[1] = topdown_0(lcenter, lend);
454 bottomup(lbeg, lend);
462 template<
typename BV>
465 int num_leaves = lend - lbeg;
468 if(num_leaves > bu_threshold)
471 BV vol = (*lbeg)->bv;
473 for(it = lbeg + 1; it < lend; ++it)
475 split_p += (*it)->bv.center();
478 split_p /= (
S)(num_leaves);
480 int bestmidp = num_leaves;
481 int splitcount[3][2] = {{0,0}, {0,0}, {0,0}};
482 for(it = lbeg; it < lend; ++it)
485 for(
size_t j = 0; j < 3; ++j)
486 ++splitcount[j][x[j] > 0 ? 1 : 0];
489 for(
size_t i = 0; i < 3; ++i)
491 if((splitcount[i][0] > 0) && (splitcount[i][1] > 0))
493 int midp = std::abs(splitcount[i][0] - splitcount[i][1]);
502 if(best_axis < 0) best_axis = 0;
504 S split_value = split_p[best_axis];
506 for(it = lbeg; it < lend; ++it)
508 if((*it)->bv.center()[best_axis] < split_value)
517 NodeType* node = createNode(
nullptr, vol,
nullptr);
518 node->
children[0] = topdown_1(lbeg, lcenter);
519 node->
children[1] = topdown_1(lcenter, lend);
526 bottomup(lbeg, lend);
534 template<
typename BV>
538 root_node = topdown(leaves.begin(), leaves.end());
539 n_leaves = leaves.size();
540 max_lookahead_level = -1;
545 template<
typename BV>
551 if(leaves.size() > 0)
552 bound_bv = leaves[0]->bv;
553 for(
size_t i = 1; i < leaves.size(); ++i)
554 bound_bv += leaves[i]->bv;
556 morton_functor<typename BV::S, uint32> coder(bound_bv);
557 for(
size_t i = 0; i < leaves.size(); ++i)
558 leaves[i]->code = coder(leaves[i]->bv.center());
560 std::sort(leaves.begin(), leaves.end(), SortByMorton());
562 root_node = mortonRecurse_0(leaves.begin(), leaves.end(), (1 << (coder.bits()-1)), coder.bits()-1);
565 n_leaves = leaves.size();
566 max_lookahead_level = -1;
571 template<
typename BV>
577 if(leaves.size() > 0)
578 bound_bv = leaves[0]->bv;
579 for(
size_t i = 1; i < leaves.size(); ++i)
580 bound_bv += leaves[i]->bv;
582 morton_functor<typename BV::S, uint32> coder(bound_bv);
583 for(
size_t i = 0; i < leaves.size(); ++i)
584 leaves[i]->code = coder(leaves[i]->bv.center());
586 std::sort(leaves.begin(), leaves.end(), SortByMorton());
588 root_node = mortonRecurse_1(leaves.begin(), leaves.end(), (1 << (coder.bits()-1)), coder.bits()-1);
591 n_leaves = leaves.size();
592 max_lookahead_level = -1;
597 template<
typename BV>
603 if(leaves.size() > 0)
604 bound_bv = leaves[0]->bv;
605 for(
size_t i = 1; i < leaves.size(); ++i)
606 bound_bv += leaves[i]->bv;
608 morton_functor<typename BV::S, uint32> coder(bound_bv);
609 for(
size_t i = 0; i < leaves.size(); ++i)
610 leaves[i]->code = coder(leaves[i]->bv.center());
612 std::sort(leaves.begin(), leaves.end(), SortByMorton());
614 root_node = mortonRecurse_2(leaves.begin(), leaves.end());
617 n_leaves = leaves.size();
618 max_lookahead_level = -1;
623 template<
typename BV>
626 int num_leaves = lend - lbeg;
633 NodeVecIterator lcenter = std::lower_bound(lbeg, lend, &dummy, SortByMorton());
637 uint32 split2 = split | (1 << (bits - 1));
638 return mortonRecurse_0(lbeg, lend, split2, bits - 1);
640 else if(lcenter == lend)
642 uint32 split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
643 return mortonRecurse_0(lbeg, lend, split1, bits - 1);
647 uint32 split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
648 uint32 split2 = split | (1 << (bits - 1));
650 NodeType* child1 = mortonRecurse_0(lbeg, lcenter, split1, bits - 1);
651 NodeType* child2 = mortonRecurse_0(lcenter, lend, split2, bits - 1);
652 NodeType* node = createNode(
nullptr,
nullptr);
662 NodeType* node = topdown(lbeg, lend);
671 template<
typename BV>
674 int num_leaves = lend - lbeg;
681 NodeVecIterator lcenter = std::lower_bound(lbeg, lend, &dummy, SortByMorton());
685 uint32 split2 = split | (1 << (bits - 1));
686 return mortonRecurse_1(lbeg, lend, split2, bits - 1);
688 else if(lcenter == lend)
690 uint32 split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
691 return mortonRecurse_1(lbeg, lend, split1, bits - 1);
695 uint32 split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
696 uint32 split2 = split | (1 << (bits - 1));
698 NodeType* child1 = mortonRecurse_1(lbeg, lcenter, split1, bits - 1);
699 NodeType* child2 = mortonRecurse_1(lcenter, lend, split2, bits - 1);
700 NodeType* node = createNode(
nullptr,
nullptr);
710 NodeType* child1 = mortonRecurse_1(lbeg, lbeg + num_leaves / 2, 0, bits - 1);
711 NodeType* child2 = mortonRecurse_1(lbeg + num_leaves / 2, lend, 0, bits - 1);
712 NodeType* node = createNode(
nullptr,
nullptr);
725 template<
typename BV>
728 int num_leaves = lend - lbeg;
731 NodeType* child1 = mortonRecurse_2(lbeg, lbeg + num_leaves / 2);
732 NodeType* child2 = mortonRecurse_2(lbeg + num_leaves / 2, lend);
733 NodeType* node = createNode(
nullptr,
nullptr);
745 template<
typename BV>
751 if(max_lookahead_level >= 0)
753 for(
int i = 0; (i < max_lookahead_level) && root->
parent; ++i)
761 insertLeaf(root, leaf);
765 template<
typename BV>
775 if(q) q->
children[indexOf(p)] = n;
else r = n;
792 template<
typename BV>
837 prev->children[indexOf(sibling)] = node;
846 if(!
prev->bv.contain(node->
bv))
847 prev->bv =
prev->children[0]->bv +
prev->children[1]->bv;
874 template <
typename BV>
881 if(leaf == root_node)
908 prev->children[indexOf(parent)] = sibling;
914 BV new_bv =
prev->children[0]->bv +
prev->children[1]->bv;
915 if(!new_bv.equal(
prev->bv))
938 sibling->
parent =
nullptr;
945 template<
typename BV>
948 if((!root->
isLeaf()) && depth)
950 fetchLeaves(root->
children[0], leaves, depth-1);
951 fetchLeaves(root->
children[1], leaves, depth-1);
956 leaves.push_back(root);
961 template<
typename BV>
965 return (node->
parent->children[1] == node);
969 template<
typename BV>
974 NodeType* node = createNode(parent, data);
980 template<
typename BV>
986 NodeType* node = createNode(parent, data);
987 node->
bv = bv1 + bv2;
992 template<
typename BV>
1010 template<
typename BV>
1013 if(free_node != node)
1021 template<
typename BV>
1026 recurseDeleteNode(node->
children[0]);
1027 recurseDeleteNode(node->
children[1]);
1030 if(node == root_node) root_node =
nullptr;
1035 template<
typename BV>
1049 template<
typename BV>
1052 if(a->
bv.center()[d] < b->
bv.center()[d])
return true;
1057 template <
typename S,
typename BV>
1078 template<
typename BV>
1088 template<
typename BV>
1098 template <
typename S>
1107 const AABB<S>& bv1 = node1.bv;
1108 const AABB<S>& bv2 = node2.bv;
1112 S d1 = fabs(v1[0]) + fabs(v1[1]) + fabs(v1[2]);
1113 S d2 = fabs(v2[0]) + fabs(v2[1]) + fabs(v2[2]);
1114 return (d1 < d2) ? 0 : 1;
1123 const AABB<S>& bv1 = node1.bv;
1124 const AABB<S>& bv2 = node2.bv;
1128 S d1 = fabs(v1[0]) + fabs(v1[1]) + fabs(v1[2]);
1129 S d2 = fabs(v2[0]) + fabs(v2[1]) + fabs(v2[2]);
1130 return (d1 < d2) ? 0 : 1;