38 #ifndef FCL_HIERARCHY_TREE_INL_H 39 #define FCL_HIERARCHY_TREE_INL_H 41 #include "fcl/broadphase/detail/hierarchy_tree.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>
138 if(lookahead_level > 0)
140 for(
int i = 0; (i < lookahead_level) && root->
parent; ++i)
146 insertLeaf(root, leaf);
150 template<
typename BV>
153 if(leaf->
bv.contain(bv))
return false;
159 template <
typename S,
typename BV>
169 if(leaf->
bv.contain(bv))
return false;
170 tree.update_(leaf, bv);
180 if(leaf->
bv.contain(bv))
return false;
181 tree.update_(leaf, bv);
187 template<
typename BV>
194 template<
typename BV>
201 template<
typename BV>
206 return getMaxHeight(root_node);
210 template<
typename BV>
213 if(!root_node)
return 0;
216 getMaxDepth(root_node, 0, max_depth);
221 template<
typename BV>
226 std::vector<NodeType*> leaves;
227 leaves.reserve(n_leaves);
228 fetchLeaves(root_node, leaves);
229 bottomup(leaves.begin(), leaves.end());
230 root_node = leaves[0];
235 template<
typename BV>
240 std::vector<NodeType*> leaves;
241 leaves.reserve(n_leaves);
242 fetchLeaves(root_node, leaves);
243 root_node = topdown(leaves.begin(), leaves.end());
248 template<
typename BV>
251 if(iterations < 0) iterations = n_leaves;
252 if(root_node && (iterations > 0))
254 for(
int i = 0; i < iterations; ++i)
257 unsigned int bit = 0;
260 node = sort(node, root_node)->
children[(opath>>bit)&1];
261 bit = (bit+1)&(
sizeof(
unsigned int) * 8-1);
270 template<
typename BV>
274 recurseRefit(root_node);
278 template<
typename BV>
283 extractLeaves(root->
children[0], leaves);
284 extractLeaves(root->
children[1], leaves);
287 leaves.push_back(root);
291 template<
typename BV>
298 template<
typename BV>
305 template<
typename BV>
312 template<
typename BV>
315 for(
int i = 0; i < depth; ++i)
317 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;
329 template<
typename BV>
332 NodeVecIterator lcur_end = lend;
333 while(lbeg < lcur_end - 1)
335 NodeVecIterator min_it1, min_it2;
336 S min_size = std::numeric_limits<S>::max();
337 for(NodeVecIterator it1 = lbeg; it1 < lcur_end; ++it1)
339 for(NodeVecIterator it2 = it1 + 1; it2 < lcur_end; ++it2)
341 S cur_size = ((*it1)->bv + (*it2)->bv).size();
342 if(cur_size < min_size)
351 NodeType* n[2] = {*min_it1, *min_it2};
352 NodeType* p = createNode(
nullptr, n[0]->bv, n[1]->bv,
nullptr);
360 *min_it2 = *lcur_end;
366 template<
typename BV>
369 switch(topdown_level)
372 return topdown_0(lbeg, lend);
375 return topdown_1(lbeg, lend);
378 return topdown_0(lbeg, lend);
383 template<
typename BV>
388 size_t height1 = getMaxHeight(node->
children[0]);
389 size_t height2 = getMaxHeight(node->
children[1]);
390 return std::max(height1, height2) + 1;
397 template<
typename BV>
402 getMaxDepth(node->
children[0], depth+1, max_depth);
403 getMaxDepth(node->
children[1], depth+1, max_depth);
406 max_depth = std::max(max_depth, depth);
410 template<
typename BV>
413 int num_leaves = lend - lbeg;
416 if(num_leaves > bu_threshold)
418 BV vol = (*lbeg)->bv;
419 for(NodeVecIterator it = lbeg + 1; it < lend; ++it)
423 S extent[3] = {vol.width(), vol.height(), vol.depth()};
424 if(extent[1] > extent[0]) best_axis = 1;
425 if(extent[2] > extent[best_axis]) best_axis = 2;
428 NodeVecIterator lcenter = lbeg + num_leaves / 2;
429 std::nth_element(lbeg, lcenter, lend, std::bind(&nodeBaseLess<BV>, std::placeholders::_1, std::placeholders::_2, std::ref(best_axis)));
431 NodeType* node = createNode(
nullptr, vol,
nullptr);
432 node->
children[0] = topdown_0(lbeg, lcenter);
433 node->
children[1] = topdown_0(lcenter, lend);
440 bottomup(lbeg, lend);
448 template<
typename BV>
451 int num_leaves = lend - lbeg;
454 if(num_leaves > bu_threshold)
456 Vector3<S> split_p = (*lbeg)->bv.center();
457 BV vol = (*lbeg)->bv;
459 for(it = lbeg + 1; it < lend; ++it)
461 split_p += (*it)->bv.center();
464 split_p /= (S)(num_leaves);
466 int bestmidp = num_leaves;
467 int splitcount[3][2] = {{0,0}, {0,0}, {0,0}};
468 for(it = lbeg; it < lend; ++it)
470 Vector3<S> x = (*it)->bv.center() - split_p;
471 for(
size_t j = 0; j < 3; ++j)
472 ++splitcount[j][x[j] > 0 ? 1 : 0];
475 for(
size_t i = 0; i < 3; ++i)
477 if((splitcount[i][0] > 0) && (splitcount[i][1] > 0))
479 int midp = std::abs(splitcount[i][0] - splitcount[i][1]);
488 if(best_axis < 0) best_axis = 0;
490 S split_value = split_p[best_axis];
491 NodeVecIterator lcenter = lbeg;
492 for(it = lbeg; it < lend; ++it)
494 if((*it)->bv.center()[best_axis] < split_value)
503 NodeType* node = createNode(
nullptr, vol,
nullptr);
504 node->
children[0] = topdown_1(lbeg, lcenter);
505 node->
children[1] = topdown_1(lcenter, lend);
512 bottomup(lbeg, lend);
520 template<
typename BV>
524 root_node = topdown(leaves.begin(), leaves.end());
525 n_leaves = leaves.size();
526 max_lookahead_level = -1;
531 template<
typename BV>
537 if(leaves.size() > 0)
538 bound_bv = leaves[0]->bv;
539 for(
size_t i = 1; i < leaves.size(); ++i)
540 bound_bv += leaves[i]->bv;
542 morton_functor<typename BV::S, uint32> coder(bound_bv);
543 for(
size_t i = 0; i < leaves.size(); ++i)
544 leaves[i]->code = coder(leaves[i]->bv.center());
546 std::sort(leaves.begin(), leaves.end(), SortByMorton());
548 root_node = mortonRecurse_0(leaves.begin(), leaves.end(), (1 << (coder.bits()-1)), coder.bits()-1);
551 n_leaves = leaves.size();
552 max_lookahead_level = -1;
557 template<
typename BV>
563 if(leaves.size() > 0)
564 bound_bv = leaves[0]->bv;
565 for(
size_t i = 1; i < leaves.size(); ++i)
566 bound_bv += leaves[i]->bv;
568 morton_functor<typename BV::S, uint32> coder(bound_bv);
569 for(
size_t i = 0; i < leaves.size(); ++i)
570 leaves[i]->code = coder(leaves[i]->bv.center());
572 std::sort(leaves.begin(), leaves.end(), SortByMorton());
574 root_node = mortonRecurse_1(leaves.begin(), leaves.end(), (1 << (coder.bits()-1)), coder.bits()-1);
577 n_leaves = leaves.size();
578 max_lookahead_level = -1;
583 template<
typename BV>
589 if(leaves.size() > 0)
590 bound_bv = leaves[0]->bv;
591 for(
size_t i = 1; i < leaves.size(); ++i)
592 bound_bv += leaves[i]->bv;
594 morton_functor<typename BV::S, uint32> coder(bound_bv);
595 for(
size_t i = 0; i < leaves.size(); ++i)
596 leaves[i]->code = coder(leaves[i]->bv.center());
598 std::sort(leaves.begin(), leaves.end(), SortByMorton());
600 root_node = mortonRecurse_2(leaves.begin(), leaves.end());
603 n_leaves = leaves.size();
604 max_lookahead_level = -1;
609 template<
typename BV>
612 int num_leaves = lend - lbeg;
619 NodeVecIterator lcenter = std::lower_bound(lbeg, lend, &dummy, SortByMorton());
623 uint32 split2 = split | (1 << (bits - 1));
624 return mortonRecurse_0(lbeg, lend, split2, bits - 1);
626 else if(lcenter == lend)
628 uint32 split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
629 return mortonRecurse_0(lbeg, lend, split1, bits - 1);
633 uint32 split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
634 uint32 split2 = split | (1 << (bits - 1));
636 NodeType* child1 = mortonRecurse_0(lbeg, lcenter, split1, bits - 1);
637 NodeType* child2 = mortonRecurse_0(lcenter, lend, split2, bits - 1);
638 NodeType* node = createNode(
nullptr,
nullptr);
648 NodeType* node = topdown(lbeg, lend);
657 template<
typename BV>
660 int num_leaves = lend - lbeg;
667 NodeVecIterator lcenter = std::lower_bound(lbeg, lend, &dummy, SortByMorton());
671 uint32 split2 = split | (1 << (bits - 1));
672 return mortonRecurse_1(lbeg, lend, split2, bits - 1);
674 else if(lcenter == lend)
676 uint32 split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
677 return mortonRecurse_1(lbeg, lend, split1, bits - 1);
681 uint32 split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
682 uint32 split2 = split | (1 << (bits - 1));
684 NodeType* child1 = mortonRecurse_1(lbeg, lcenter, split1, bits - 1);
685 NodeType* child2 = mortonRecurse_1(lcenter, lend, split2, bits - 1);
686 NodeType* node = createNode(
nullptr,
nullptr);
696 NodeType* child1 = mortonRecurse_1(lbeg, lbeg + num_leaves / 2, 0, bits - 1);
697 NodeType* child2 = mortonRecurse_1(lbeg + num_leaves / 2, lend, 0, bits - 1);
698 NodeType* node = createNode(
nullptr,
nullptr);
711 template<
typename BV>
714 int num_leaves = lend - lbeg;
717 NodeType* child1 = mortonRecurse_2(lbeg, lbeg + num_leaves / 2);
718 NodeType* child2 = mortonRecurse_2(lbeg + num_leaves / 2, lend);
719 NodeType* node = createNode(
nullptr,
nullptr);
731 template<
typename BV>
737 if(max_lookahead_level >= 0)
739 for(
int i = 0; (i < max_lookahead_level) && root->
parent; ++i)
747 insertLeaf(root, leaf);
751 template<
typename BV>
761 if(q) q->
children[indexOf(p)] = n;
else r = n;
771 std::swap(p->
bv, n->
bv);
778 template<
typename BV>
798 NodeType* node = createNode(prev, leaf->
bv, root->
bv,
nullptr);
801 prev->
children[indexOf(root)] = node;
806 if(!prev->
bv.contain(node->
bv))
811 }
while (
nullptr != (prev = node->
parent));
823 template<
typename BV>
826 if(leaf == root_node)
838 prev->
children[indexOf(parent)] = sibling;
844 if(!new_bv.equal(prev->
bv))
852 return prev ? prev : root_node;
857 sibling->
parent =
nullptr;
865 template<
typename BV>
868 if((!root->
isLeaf()) && depth)
870 fetchLeaves(root->
children[0], leaves, depth-1);
871 fetchLeaves(root->
children[1], leaves, depth-1);
876 leaves.push_back(root);
881 template<
typename BV>
885 return (node->
parent->children[1] == node);
889 template<
typename BV>
894 NodeType* node = createNode(parent, data);
900 template<
typename BV>
906 NodeType* node = createNode(parent, data);
907 node->
bv = bv1 + bv2;
912 template<
typename BV>
930 template<
typename BV>
933 if(free_node != node)
941 template<
typename BV>
946 recurseDeleteNode(node->
children[0]);
947 recurseDeleteNode(node->
children[1]);
950 if(node == root_node) root_node =
nullptr;
955 template<
typename BV>
969 template<
typename BV>
972 if(leaves.size() == 0)
return BV();
973 BV bv = leaves[0]->bv;
974 for(
size_t i = 1; i < leaves.size(); ++i)
983 template<
typename BV>
986 if(lbeg == lend)
return BV();
988 for(NodeVecIterator it = lbeg + 1; it < lend; ++it)
997 template<
typename BV>
1000 if(a->
bv.center()[d] < b->
bv.center()[d])
return true;
1005 template <
typename S,
typename BV>
1008 static std::size_t run(
1016 static std::size_t run(
1026 template<
typename BV>
1036 template<
typename BV>
1046 template <
typename S>
1049 static std::size_t run(
1058 Vector3<S> v1 = v - (bv1.
min_ + bv1.
max_);
1059 Vector3<S> v2 = v - (bv2.
min_ + bv2.
max_);
1060 S d1 = fabs(v1[0]) + fabs(v1[1]) + fabs(v1[2]);
1061 S d2 = fabs(v2[0]) + fabs(v2[1]) + fabs(v2[2]);
1062 return (d1 < d2) ? 0 : 1;
1065 static std::size_t run(
1074 Vector3<S> v1 = v - (bv1.
min_ + bv1.
max_);
1075 Vector3<S> v2 = v - (bv2.
min_ + bv2.
max_);
1076 S d1 = fabs(v1[0]) + fabs(v1[1]) + fabs(v1[2]);
1077 S d2 = fabs(v2[0]) + fabs(v2[1]) + fabs(v2[2]);
1078 return (d1 < d2) ? 0 : 1;
1085 const Vector3<S>& vel,
1089 if(leaf->bv.contain(bv))
return false;
1090 Vector3<S> marginv(margin);
1093 if(vel[0] > 0) bv.
max_[0] += vel[0];
1094 else bv.
min_[0] += vel[0];
1095 if(vel[1] > 0) bv.
max_[1] += vel[1];
1096 else bv.
min_[1] += vel[1];
1097 if(vel[2] > 0) bv.
max_[2] += vel[2];
1098 else bv.
max_[2] += vel[2];
1099 tree.update(leaf, bv);
1107 const Vector3<S>& vel)
1110 if(leaf->bv.contain(bv))
return false;
1111 if(vel[0] > 0) bv.
max_[0] += vel[0];
1112 else bv.
min_[0] += vel[0];
1113 if(vel[1] > 0) bv.
max_[1] += vel[1];
1114 else bv.
min_[1] += vel[1];
1115 if(vel[2] > 0) bv.
max_[2] += vel[2];
1116 else bv.
max_[2] += vel[2];
1117 tree.update(leaf, bv);
dynamic AABB<S> tree node
Definition: node_base.h:51
void clear()
Clear the tree.
Definition: hierarchy_tree-inl.h:113
Definition: hierarchy_tree-inl.h:1006
void extractLeaves(const NodeType *root, std::vector< NodeType * > &leaves) const
extract all the leaves of the tree
Definition: hierarchy_tree-inl.h:279
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
void balanceBottomup()
balance the tree from bottom
Definition: hierarchy_tree-inl.h:222
A class describing the AABB collision structure, which is a box in 3D space determined by two diagona...
Definition: AABB.h:49
NodeBase< BV > * parent
pointer to parent node
Definition: node_base.h:57
size_t getMaxDepth() const
get the max depth of the tree
Definition: hierarchy_tree-inl.h:211
void update(NodeType *leaf, int lookahead_level=-1)
update one leaf node
Definition: hierarchy_tree-inl.h:133
Vector3< S > max_
The max point in the AABB.
Definition: AABB.h:59
size_t getMaxHeight() const
get the max height of the tree
Definition: hierarchy_tree-inl.h:202
NodeBase< BV > * children[2]
for leaf node, children nodes
Definition: node_base.h:68
void balanceIncremental(int iterations)
balance the tree in an incremental way
Definition: hierarchy_tree-inl.h:249
Definition: hierarchy_tree-inl.h:160
NodeType * getRoot() const
get the root of the tree
Definition: hierarchy_tree-inl.h:299
uint32 code
morton code for current BV
Definition: node_base.h:73
void refit()
refit the tree, i.e., when the leaf nodes' bounding volumes change, update the entire tree in a botto...
Definition: hierarchy_tree-inl.h:271
bool empty() const
Whether the tree is empty.
Definition: hierarchy_tree-inl.h:126
void remove(NodeType *leaf)
Remove a leaf node.
Definition: hierarchy_tree-inl.h:104
bool isLeaf() const
whether is a leaf
Definition: node_base-inl.h:57
Class for hierarchy tree structure.
Definition: hierarchy_tree.h:58
Vector3< S > min_
The min point in the AABB.
Definition: AABB.h:56
void init(std::vector< NodeType * > &leaves, int level=0)
Initialize the tree by a set of leaves using algorithm with a given level.
Definition: hierarchy_tree-inl.h:71
BV bv
the bounding volume for the node
Definition: node_base.h:54
size_t size() const
number of leaves in the tree
Definition: hierarchy_tree-inl.h:292
void balanceTopdown()
balance the tree from top
Definition: hierarchy_tree-inl.h:236
NodeType * insert(const BV &bv, void *data)
Insest a node.
Definition: hierarchy_tree-inl.h:94
void print(NodeType *root, int depth)
print the tree in a recursive way
Definition: hierarchy_tree-inl.h:313
HierarchyTree(int bu_threshold_=16, int topdown_level_=0)
Create hierarchy tree with suitable setting. bu_threshold decides the height of tree node to start bo...
Definition: hierarchy_tree-inl.h:51