38 #ifndef HPP_FCL_HIERARCHY_TREE_INL_H
39 #define HPP_FCL_HIERARCHY_TREE_INL_H
49 template <
typename BV>
54 max_lookahead_level = -1;
56 bu_threshold = bu_threshold_;
57 topdown_level = topdown_level_;
61 template <
typename BV>
67 template <
typename BV>
88 template <
typename BV>
91 Node* leaf = createNode(
nullptr, bv,
data);
92 insertLeaf(root_node, leaf);
98 template <
typename BV>
106 template <
typename BV>
108 if (root_node) recurseDeleteNode(root_node);
112 max_lookahead_level = -1;
117 template <
typename BV>
119 return (
nullptr == root_node);
123 template <
typename BV>
136 if (lookahead_level > 0) {
138 for (
int i = 0; (i < lookahead_level) && sub_root->
parent; ++i)
139 sub_root = sub_root->
parent;
143 sub_root = root_node;
147 insertLeaf(sub_root, leaf);
151 template <
typename BV>
153 if (leaf->
bv.contain(bv))
return false;
159 template <
typename S,
typename BV>
164 if (leaf->
bv.contain(bv))
return false;
172 if (leaf->
bv.contain(bv))
return false;
179 template <
typename BV>
186 template <
typename BV>
192 template <
typename BV>
194 if (!root_node)
return 0;
195 return getMaxHeight(root_node);
199 template <
typename BV>
201 if (!root_node)
return 0;
204 getMaxDepth(root_node, 0, max_depth);
209 template <
typename BV>
212 std::vector<Node*> leaves;
213 leaves.reserve(n_leaves);
214 fetchLeaves(root_node, leaves);
215 bottomup(leaves.begin(), leaves.end());
216 root_node = leaves[0];
221 template <
typename BV>
224 std::vector<Node*> leaves;
225 leaves.reserve(n_leaves);
226 fetchLeaves(root_node, leaves);
227 root_node = topdown(leaves.begin(), leaves.end());
232 template <
typename BV>
234 if (iterations < 0) iterations = (int)n_leaves;
235 if (root_node && (iterations > 0)) {
236 for (
int i = 0; i < iterations; ++i) {
237 Node* node = root_node;
238 unsigned int bit = 0;
240 node = sort(node, root_node)->
children[(opath >> bit) & 1];
241 bit = (bit + 1) & (
sizeof(
unsigned int) * 8 - 1);
250 template <
typename BV>
252 if (root_node) recurseRefit(root_node);
256 template <
typename BV>
258 std::vector<Node*>& leaves)
const {
260 extractLeaves(root->
children[0], leaves);
261 extractLeaves(root->
children[1], leaves);
263 leaves.push_back(root);
267 template <
typename BV>
273 template <
typename BV>
279 template <
typename BV>
285 template <
typename BV>
287 for (
int i = 0; i < depth; ++i) std::cout <<
" ";
288 std::cout <<
" (" << root->
bv.min_[0] <<
", " << root->
bv.min_[1] <<
", "
289 << root->
bv.min_[2] <<
"; " << root->
bv.max_[0] <<
", "
290 << root->
bv.max_[1] <<
", " << root->
bv.max_[2] <<
")" << std::endl;
299 template <
typename BV>
303 while (lbeg < lcur_end - 1) {
306 FCL_REAL min_size = (std::numeric_limits<FCL_REAL>::max)();
309 FCL_REAL cur_size = ((*it1)->bv + (*it2)->bv).size();
310 if (cur_size < min_size) {
318 Node* n[2] = {*min_it1, *min_it2};
319 Node* p = createNode(
nullptr, n[0]->bv, n[1]->bv,
nullptr);
325 Node* tmp = *min_it2;
327 *min_it2 = *lcur_end;
333 template <
typename BV>
336 switch (topdown_level) {
338 return topdown_0(lbeg, lend);
341 return topdown_1(lbeg, lend);
344 return topdown_0(lbeg, lend);
349 template <
typename BV>
352 size_t height1 = getMaxHeight(node->
children[0]);
353 size_t height2 = getMaxHeight(node->
children[1]);
354 return std::max(height1, height2) + 1;
360 template <
typename BV>
362 size_t& max_depth)
const {
364 getMaxDepth(node->
children[0], depth + 1, max_depth);
365 getMaxDepth(node->
children[1], depth + 1, max_depth);
367 max_depth = std::max(max_depth, depth);
371 template <
typename BV>
374 long num_leaves = lend - lbeg;
375 if (num_leaves > 1) {
376 if (num_leaves > bu_threshold) {
377 BV vol = (*lbeg)->
bv;
381 FCL_REAL extent[3] = {vol.width(), vol.height(), vol.depth()};
382 if (extent[1] > extent[0]) best_axis = 1;
383 if (extent[2] > extent[best_axis]) best_axis = 2;
387 std::nth_element(lbeg, lcenter, lend,
388 std::bind(&nodeBaseLess<BV>, std::placeholders::_1,
389 std::placeholders::_2, std::ref(best_axis)));
391 Node* node = createNode(
nullptr, vol,
nullptr);
392 node->
children[0] = topdown_0(lbeg, lcenter);
393 node->
children[1] = topdown_0(lcenter, lend);
398 bottomup(lbeg, lend);
406 template <
typename BV>
409 long num_leaves = lend - lbeg;
410 if (num_leaves > 1) {
411 if (num_leaves > bu_threshold) {
412 Vec3f split_p = (*lbeg)->bv.center();
413 BV vol = (*lbeg)->bv;
415 for (it = lbeg + 1; it < lend; ++it) {
416 split_p += (*it)->bv.center();
419 split_p /=
static_cast<FCL_REAL>(num_leaves);
421 long bestmidp = num_leaves;
422 int splitcount[3][2] = {{0, 0}, {0, 0}, {0, 0}};
423 for (it = lbeg; it < lend; ++it) {
424 Vec3f x = (*it)->bv.center() - split_p;
425 for (
int j = 0; j < 3; ++j) ++splitcount[j][x[j] > 0 ? 1 : 0];
428 for (
int i = 0; i < 3; ++i) {
429 if ((splitcount[i][0] > 0) && (splitcount[i][1] > 0)) {
430 long midp = std::abs(splitcount[i][0] - splitcount[i][1]);
431 if (midp < bestmidp) {
438 if (best_axis < 0) best_axis = 0;
440 FCL_REAL split_value = split_p[best_axis];
442 for (it = lbeg; it < lend; ++it) {
443 if ((*it)->bv.center()[best_axis] < split_value) {
451 Node* node = createNode(
nullptr, vol,
nullptr);
452 node->
children[0] = topdown_1(lbeg, lcenter);
453 node->
children[1] = topdown_1(lcenter, lend);
458 bottomup(lbeg, lend);
466 template <
typename BV>
469 root_node = topdown(leaves.begin(), leaves.end());
470 n_leaves = leaves.size();
471 max_lookahead_level = -1;
476 template <
typename BV>
481 if (leaves.size() > 0) bound_bv = leaves[0]->bv;
482 for (
size_t i = 1; i < leaves.size(); ++i) bound_bv += leaves[i]->bv;
484 morton_functor<FCL_REAL, uint32_t> coder(bound_bv);
485 for (
size_t i = 0; i < leaves.size(); ++i)
486 leaves[i]->code = coder(leaves[i]->bv.center());
488 std::sort(leaves.begin(), leaves.end(), SortByMorton());
490 root_node = mortonRecurse_0(leaves.begin(), leaves.end(),
491 (1 << (coder.bits() - 1)), coder.bits() - 1);
494 n_leaves = leaves.size();
495 max_lookahead_level = -1;
500 template <
typename BV>
505 if (leaves.size() > 0) bound_bv = leaves[0]->bv;
506 for (
size_t i = 1; i < leaves.size(); ++i) bound_bv += leaves[i]->bv;
508 morton_functor<FCL_REAL, uint32_t> coder(bound_bv);
509 for (
size_t i = 0; i < leaves.size(); ++i)
510 leaves[i]->code = coder(leaves[i]->bv.center());
512 std::sort(leaves.begin(), leaves.end(), SortByMorton());
514 root_node = mortonRecurse_1(leaves.begin(), leaves.end(),
515 (1 << (coder.bits() - 1)), coder.bits() - 1);
518 n_leaves = leaves.size();
519 max_lookahead_level = -1;
524 template <
typename BV>
529 if (leaves.size() > 0) bound_bv = leaves[0]->bv;
530 for (
size_t i = 1; i < leaves.size(); ++i) bound_bv += leaves[i]->bv;
532 morton_functor<FCL_REAL, uint32_t> coder(bound_bv);
533 for (
size_t i = 0; i < leaves.size(); ++i)
534 leaves[i]->code = coder(leaves[i]->bv.center());
536 std::sort(leaves.begin(), leaves.end(), SortByMorton());
538 root_node = mortonRecurse_2(leaves.begin(), leaves.end());
541 n_leaves = leaves.size();
542 max_lookahead_level = -1;
547 template <
typename BV>
550 const uint32_t& split,
int bits) {
551 long num_leaves = lend - lbeg;
552 if (num_leaves > 1) {
557 std::lower_bound(lbeg, lend, &dummy, SortByMorton());
559 if (lcenter == lbeg) {
560 uint32_t split2 = split | (1 << (bits - 1));
561 return mortonRecurse_0(lbeg, lend, split2, bits - 1);
562 }
else if (lcenter == lend) {
563 uint32_t split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
564 return mortonRecurse_0(lbeg, lend, split1, bits - 1);
566 uint32_t split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
567 uint32_t split2 = split | (1 << (bits - 1));
569 Node* child1 = mortonRecurse_0(lbeg, lcenter, split1, bits - 1);
570 Node* child2 = mortonRecurse_0(lcenter, lend, split2, bits - 1);
571 Node* node = createNode(
nullptr,
nullptr);
579 Node* node = topdown(lbeg, lend);
587 template <
typename BV>
590 const uint32_t& split,
int bits) {
591 long num_leaves = lend - lbeg;
592 if (num_leaves > 1) {
597 std::lower_bound(lbeg, lend, &dummy, SortByMorton());
599 if (lcenter == lbeg) {
600 uint32_t split2 = split | (1 << (bits - 1));
601 return mortonRecurse_1(lbeg, lend, split2, bits - 1);
602 }
else if (lcenter == lend) {
603 uint32_t split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
604 return mortonRecurse_1(lbeg, lend, split1, bits - 1);
606 uint32_t split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
607 uint32_t split2 = split | (1 << (bits - 1));
609 Node* child1 = mortonRecurse_1(lbeg, lcenter, split1, bits - 1);
610 Node* child2 = mortonRecurse_1(lcenter, lend, split2, bits - 1);
611 Node* node = createNode(
nullptr,
nullptr);
619 Node* child1 = mortonRecurse_1(lbeg, lbeg + num_leaves / 2, 0, bits - 1);
620 Node* child2 = mortonRecurse_1(lbeg + num_leaves / 2, lend, 0, bits - 1);
621 Node* node = createNode(
nullptr,
nullptr);
633 template <
typename BV>
636 long num_leaves = lend - lbeg;
637 if (num_leaves > 1) {
638 Node* child1 = mortonRecurse_2(lbeg, lbeg + num_leaves / 2);
639 Node* child2 = mortonRecurse_2(lbeg + num_leaves / 2, lend);
640 Node* node = createNode(
nullptr,
nullptr);
651 template <
typename BV>
653 Node* root = removeLeaf(leaf);
655 if (max_lookahead_level >= 0) {
656 for (
int i = 0; (i < max_lookahead_level) && root->
parent; ++i)
663 insertLeaf(root, leaf);
667 template <
typename BV>
671 size_t i = indexOf(n);
676 q->children[indexOf(p)] = n;
688 std::swap(p->
bv, n->
bv);
695 template <
typename BV>
715 Node* sibling = sub_root;
716 while (!sibling->
isLeaf()) {
723 Node* node = createNode(prev, leaf->
bv, sibling->
bv,
nullptr);
737 prev->
children[indexOf(sibling)] = node;
747 if (!prev->
bv.contain(node->
bv))
752 }
while (
nullptr != (prev = node->
parent));
776 template <
typename BV>
783 if (leaf == root_node) {
808 prev->
children[indexOf(parent)] = sibling;
814 if (!(new_bv == prev->
bv)) {
821 return prev ? prev : root_node;
834 sibling->
parent =
nullptr;
841 template <
typename BV>
844 if ((!root->
isLeaf()) && depth) {
845 fetchLeaves(root->
children[0], leaves, depth - 1);
846 fetchLeaves(root->
children[1], leaves, depth - 1);
849 leaves.push_back(root);
854 template <
typename BV>
857 return (node->
parent->children[1] == node);
861 template <
typename BV>
865 Node* node = createNode(parent,
data);
871 template <
typename BV>
876 Node* node = createNode(parent,
data);
877 node->
bv = bv1 + bv2;
882 template <
typename BV>
885 Node* node =
nullptr;
898 template <
typename BV>
900 if (free_node != node) {
907 template <
typename BV>
910 recurseDeleteNode(node->
children[0]);
911 recurseDeleteNode(node->
children[1]);
914 if (node == root_node) root_node =
nullptr;
919 template <
typename BV>
930 template <
typename BV>
932 if (
a->bv.center()[d] <
b->bv.center()[d])
return true;
937 template <
typename S,
typename BV>
952 template <
typename BV>
959 template <
typename BV>
966 template <
typename S>
972 const AABB& bv1 = node1.
bv;
973 const AABB& bv2 = node2.
bv;
977 FCL_REAL d1 = fabs(v1[0]) + fabs(v1[1]) + fabs(v1[2]);
978 FCL_REAL d2 = fabs(v2[0]) + fabs(v2[1]) + fabs(v2[2]);
979 return (
d1 < d2) ? 0 : 1;
984 const AABB& bv = query;
985 const AABB& bv1 = node1.
bv;
986 const AABB& bv2 = node2.
bv;
990 FCL_REAL d1 = fabs(v1[0]) + fabs(v1[1]) + fabs(v1[2]);
991 FCL_REAL d2 = fabs(v2[0]) + fabs(v2[1]) + fabs(v2[2]);
992 return (
d1 < d2) ? 0 : 1;