38 #ifndef FCL_HIERARCHY_TREE_ARRAY_INL_H
39 #define FCL_HIERARCHY_TREE_ARRAY_INL_H
53 namespace implementation_array
60 root_node = NULL_NODE;
64 for(
size_t i = 0; i < n_nodes_alloc - 1; ++i)
65 nodes[i].
next = i + 1;
66 nodes[n_nodes_alloc - 1].next = NULL_NODE;
70 max_lookahead_level = -1;
71 bu_threshold = bu_threshold_;
72 topdown_level = topdown_level_;
89 init_0(leaves, n_leaves_);
92 init_1(leaves, n_leaves_);
95 init_2(leaves, n_leaves_);
98 init_3(leaves, n_leaves_);
101 init_0(leaves, n_leaves_);
106 template<
typename BV>
111 n_leaves = n_leaves_;
112 root_node = NULL_NODE;
114 std::copy(leaves, leaves + n_leaves, nodes);
117 n_nodes_alloc = 2 * n_leaves;
118 for(
size_t i = n_leaves; i < n_nodes_alloc; ++i)
119 nodes[i].
next = i + 1;
120 nodes[n_nodes_alloc - 1].next = NULL_NODE;
122 size_t* ids =
new size_t[n_leaves];
123 for(
size_t i = 0; i < n_leaves; ++i)
126 root_node = topdown(ids, ids + n_leaves);
130 max_lookahead_level = -1;
134 template<
typename BV>
139 n_leaves = n_leaves_;
140 root_node = NULL_NODE;
142 std::copy(leaves, leaves + n_leaves, nodes);
145 n_nodes_alloc = 2 * n_leaves;
146 for(
size_t i = n_leaves; i < n_nodes_alloc; ++i)
147 nodes[i].
next = i + 1;
148 nodes[n_nodes_alloc - 1].next = NULL_NODE;
153 bound_bv = nodes[0].bv;
154 for(
size_t i = 1; i < n_leaves; ++i)
155 bound_bv += nodes[i].bv;
157 morton_functor<typename BV::S, uint32> coder(bound_bv);
158 for(
size_t i = 0; i < n_leaves; ++i)
159 nodes[i].code = coder(nodes[i].bv.center());
162 size_t* ids =
new size_t[n_leaves];
163 for(
size_t i = 0; i < n_leaves; ++i)
166 const SortByMorton comp{nodes};
167 std::sort(ids, ids + n_leaves, comp);
168 root_node = mortonRecurse_0(ids, ids + n_leaves, (1 << (coder.bits()-1)), coder.bits()-1);
174 max_lookahead_level = -1;
178 template<
typename BV>
183 n_leaves = n_leaves_;
184 root_node = NULL_NODE;
186 std::copy(leaves, leaves + n_leaves, nodes);
189 n_nodes_alloc = 2 * n_leaves;
190 for(
size_t i = n_leaves; i < n_nodes_alloc; ++i)
191 nodes[i].
next = i + 1;
192 nodes[n_nodes_alloc - 1].next = NULL_NODE;
197 bound_bv = nodes[0].bv;
198 for(
size_t i = 1; i < n_leaves; ++i)
199 bound_bv += nodes[i].bv;
201 morton_functor<typename BV::S, uint32> coder(bound_bv);
202 for(
size_t i = 0; i < n_leaves; ++i)
203 nodes[i].code = coder(nodes[i].bv.center());
206 size_t* ids =
new size_t[n_leaves];
207 for(
size_t i = 0; i < n_leaves; ++i)
210 const SortByMorton comp{nodes};
211 std::sort(ids, ids + n_leaves, comp);
212 root_node = mortonRecurse_1(ids, ids + n_leaves, (1 << (coder.bits()-1)), coder.bits()-1);
218 max_lookahead_level = -1;
222 template<
typename BV>
227 n_leaves = n_leaves_;
228 root_node = NULL_NODE;
230 std::copy(leaves, leaves + n_leaves, nodes);
233 n_nodes_alloc = 2 * n_leaves;
234 for(
size_t i = n_leaves; i < n_nodes_alloc; ++i)
235 nodes[i].
next = i + 1;
236 nodes[n_nodes_alloc - 1].next = NULL_NODE;
241 bound_bv = nodes[0].bv;
242 for(
size_t i = 1; i < n_leaves; ++i)
243 bound_bv += nodes[i].bv;
245 morton_functor<typename BV::S, uint32> coder(bound_bv);
246 for(
size_t i = 0; i < n_leaves; ++i)
247 nodes[i].code = coder(nodes[i].bv.center());
250 size_t* ids =
new size_t[n_leaves];
251 for(
size_t i = 0; i < n_leaves; ++i)
254 const SortByMorton comp{nodes};
255 std::sort(ids, ids + n_leaves, comp);
256 root_node = mortonRecurse_2(ids, ids + n_leaves);
262 max_lookahead_level = -1;
266 template<
typename BV>
269 size_t node = createNode(NULL_NODE, bv, data);
270 insertLeaf(root_node, node);
276 template<
typename BV>
285 template<
typename BV>
289 root_node = NULL_NODE;
292 nodes =
new NodeType[n_nodes_alloc];
293 for(
size_t i = 0; i < n_nodes_alloc; ++i)
294 nodes[i].
next = i + 1;
295 nodes[n_nodes_alloc - 1].next = NULL_NODE;
299 max_lookahead_level = -1;
303 template<
typename BV>
306 return (n_nodes == 0);
310 template<
typename BV>
313 size_t root = removeLeaf(leaf);
314 if(root != NULL_NODE)
316 if(lookahead_level > 0)
318 for(
int i = 0; (i < lookahead_level) && (nodes[root].parent != NULL_NODE); ++i)
319 root = nodes[root].parent;
324 insertLeaf(root, leaf);
328 template<
typename BV>
331 if(nodes[leaf].bv.contain(bv))
return false;
337 template<
typename BV>
344 if(nodes[leaf].bv.contain(bv))
return false;
350 template<
typename BV>
355 if(nodes[leaf].bv.contain(bv))
return false;
361 template<
typename BV>
364 if(root_node == NULL_NODE)
return 0;
366 return getMaxHeight(root_node);
370 template<
typename BV>
373 if(root_node == NULL_NODE)
return 0;
376 getMaxDepth(root_node, 0, max_depth);
381 template<
typename BV>
384 if(root_node != NULL_NODE)
388 extractLeaves(root_node, leaves_);
389 root_node = NULL_NODE;
390 std::copy(leaves, leaves + n_leaves, nodes);
393 for(
size_t i = n_leaves; i < n_nodes_alloc; ++i)
394 nodes[i].
next = i + 1;
395 nodes[n_nodes_alloc - 1].next = NULL_NODE;
398 size_t* ids =
new size_t[n_leaves];
399 for(
size_t i = 0; i < n_leaves; ++i)
402 bottomup(ids, ids + n_leaves);
410 template<
typename BV>
413 if(root_node != NULL_NODE)
417 extractLeaves(root_node, leaves_);
418 root_node = NULL_NODE;
419 std::copy(leaves, leaves + n_leaves, nodes);
422 for(
size_t i = n_leaves; i < n_nodes_alloc; ++i)
423 nodes[i].
next = i + 1;
424 nodes[n_nodes_alloc - 1].next = NULL_NODE;
426 size_t* ids =
new size_t[n_leaves];
427 for(
size_t i = 0; i < n_leaves; ++i)
430 root_node = topdown(ids, ids + n_leaves);
436 template<
typename BV>
439 if(iterations < 0) iterations = n_leaves;
440 if((root_node != NULL_NODE) && (iterations > 0))
442 for(
int i = 0; i < iterations; ++i)
444 size_t node = root_node;
445 unsigned int bit = 0;
446 while(!nodes[node].isLeaf())
448 node = nodes[node].children[(opath>>bit)&1];
449 bit = (bit+1)&(
sizeof(
unsigned int) * 8-1);
458 template<
typename BV>
461 if(root_node != NULL_NODE)
462 recurseRefit(root_node);
466 template<
typename BV>
469 if(!nodes[root].isLeaf())
471 extractLeaves(nodes[root].children[0], leaves);
472 extractLeaves(nodes[root].children[1], leaves);
476 *leaves = nodes[root];
482 template<
typename BV>
489 template<
typename BV>
496 template<
typename BV>
503 template<
typename BV>
506 for(
int i = 0; i < depth; ++i)
509 std::cout <<
" (" << n->
bv.min_[0] <<
", " << n->
bv.min_[1] <<
", " << n->
bv.min_[2] <<
"; " << n->
bv.max_[0] <<
", " << n->
bv.max_[1] <<
", " << n->
bv.max_[2] <<
")" << std::endl;
521 template<
typename BV>
524 if(!nodes[node].isLeaf())
526 size_t height1 = getMaxHeight(nodes[node].children[0]);
527 size_t height2 = getMaxHeight(nodes[node].children[1]);
528 return std::max(height1, height2) + 1;
535 template<
typename BV>
538 if(!nodes[node].isLeaf())
540 getMaxDepth(nodes[node].children[0], depth+1, max_depth);
541 getmaxDepth(nodes[node].children[1], depth+1, max_depth);
544 max_depth =
std::max(max_depth, depth);
548 template<
typename BV>
551 size_t* lcur_end = lend;
552 while(lbeg < lcur_end - 1)
554 size_t* min_it1 =
nullptr, *min_it2 =
nullptr;
556 for(
size_t* it1 = lbeg; it1 < lcur_end; ++it1)
558 for(
size_t* it2 = it1 + 1; it2 < lcur_end; ++it2)
560 S cur_size = (nodes[*it1].bv + nodes[*it2].bv).size();
561 if(cur_size < min_size)
570 size_t p = createNode(NULL_NODE, nodes[*min_it1].bv, nodes[*min_it2].bv,
nullptr);
571 nodes[p].children[0] = *min_it1;
572 nodes[p].children[1] = *min_it2;
573 nodes[*min_it1].parent = p;
574 nodes[*min_it2].parent = p;
576 size_t tmp = *min_it2;
578 *min_it2 = *lcur_end;
584 template<
typename BV>
587 switch(topdown_level)
590 return topdown_0(lbeg, lend);
593 return topdown_1(lbeg, lend);
596 return topdown_0(lbeg, lend);
601 template<
typename BV>
604 int num_leaves = lend - lbeg;
607 if(num_leaves > bu_threshold)
609 BV vol = nodes[*lbeg].bv;
610 for(
size_t* i = lbeg + 1; i < lend; ++i)
614 S extent[3] = {vol.width(), vol.height(), vol.depth()};
615 if(extent[1] > extent[0]) best_axis = 1;
616 if(extent[2] > extent[best_axis]) best_axis = 2;
618 nodeBaseLess<BV> comp(nodes, best_axis);
619 size_t* lcenter = lbeg + num_leaves / 2;
620 std::nth_element(lbeg, lcenter, lend, comp);
622 size_t node = createNode(NULL_NODE, vol,
nullptr);
623 nodes[node].children[0] = topdown_0(lbeg, lcenter);
624 nodes[node].children[1] = topdown_0(lcenter, lend);
625 nodes[nodes[node].children[0]].parent = node;
626 nodes[nodes[node].children[1]].parent = node;
631 bottomup(lbeg, lend);
639 template<
typename BV>
642 int num_leaves = lend - lbeg;
645 if(num_leaves > bu_threshold)
647 Vector3<S> split_p = nodes[*lbeg].bv.center();
648 BV vol = nodes[*lbeg].bv;
649 for(
size_t* i = lbeg + 1; i < lend; ++i)
651 split_p += nodes[*i].bv.center();
654 split_p /= (
S)(num_leaves);
656 int bestmidp = num_leaves;
657 int splitcount[3][2] = {{0,0}, {0,0}, {0,0}};
658 for(
size_t* i = lbeg; i < lend; ++i)
660 Vector3<S> x = nodes[*i].bv.center() - split_p;
661 for(
size_t j = 0; j < 3; ++j)
662 ++splitcount[j][x[j] > 0 ? 1 : 0];
665 for(
size_t i = 0; i < 3; ++i)
667 if((splitcount[i][0] > 0) && (splitcount[i][1] > 0))
669 int midp = std::abs(splitcount[i][0] - splitcount[i][1]);
678 if(best_axis < 0) best_axis = 0;
680 S split_value = split_p[best_axis];
681 size_t* lcenter = lbeg;
682 for(
size_t* i = lbeg; i < lend; ++i)
684 if(nodes[*i].bv.center()[best_axis] < split_value)
693 size_t node = createNode(NULL_NODE, vol,
nullptr);
694 nodes[node].children[0] = topdown_1(lbeg, lcenter);
695 nodes[node].children[1] = topdown_1(lcenter, lend);
696 nodes[nodes[node].children[0]].parent = node;
697 nodes[nodes[node].children[1]].parent = node;
702 bottomup(lbeg, lend);
710 template<
typename BV>
713 int num_leaves = lend - lbeg;
718 const SortByMorton comp{nodes, split};
719 size_t* lcenter = std::lower_bound(lbeg, lend, NULL_NODE, comp);
723 uint32 split2 = split | (1 << (bits - 1));
724 return mortonRecurse_0(lbeg, lend, split2, bits - 1);
726 else if(lcenter == lend)
728 uint32 split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
729 return mortonRecurse_0(lbeg, lend, split1, bits - 1);
733 uint32 split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
734 uint32 split2 = split | (1 << (bits - 1));
736 size_t child1 = mortonRecurse_0(lbeg, lcenter, split1, bits - 1);
737 size_t child2 = mortonRecurse_0(lcenter, lend, split2, bits - 1);
738 size_t node = createNode(NULL_NODE,
nullptr);
739 nodes[node].children[0] = child1;
740 nodes[node].children[1] = child2;
741 nodes[child1].parent = node;
742 nodes[child2].parent = node;
748 size_t node = topdown(lbeg, lend);
757 template<
typename BV>
760 int num_leaves = lend - lbeg;
765 const SortByMorton comp{nodes, split};
766 size_t* lcenter = std::lower_bound(lbeg, lend, NULL_NODE, comp);
770 uint32 split2 = split | (1 << (bits - 1));
771 return mortonRecurse_1(lbeg, lend, split2, bits - 1);
773 else if(lcenter == lend)
775 uint32 split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
776 return mortonRecurse_1(lbeg, lend, split1, bits - 1);
780 uint32 split1 = (split & (~(1 << bits))) | (1 << (bits - 1));
781 uint32 split2 = split | (1 << (bits - 1));
783 size_t child1 = mortonRecurse_1(lbeg, lcenter, split1, bits - 1);
784 size_t child2 = mortonRecurse_1(lcenter, lend, split2, bits - 1);
785 size_t node = createNode(NULL_NODE,
nullptr);
786 nodes[node].children[0] = child1;
787 nodes[node].children[1] = child2;
788 nodes[child1].parent = node;
789 nodes[child2].parent = node;
795 size_t child1 = mortonRecurse_1(lbeg, lbeg + num_leaves / 2, 0, bits - 1);
796 size_t child2 = mortonRecurse_1(lbeg + num_leaves / 2, lend, 0, bits - 1);
797 size_t node = createNode(NULL_NODE,
nullptr);
798 nodes[node].children[0] = child1;
799 nodes[node].children[1] = child2;
800 nodes[child1].parent = node;
801 nodes[child2].parent = node;
810 template<
typename BV>
813 int num_leaves = lend - lbeg;
816 size_t child1 = mortonRecurse_2(lbeg, lbeg + num_leaves / 2);
817 size_t child2 = mortonRecurse_2(lbeg + num_leaves / 2, lend);
818 size_t node = createNode(NULL_NODE,
nullptr);
819 nodes[node].children[0] = child1;
820 nodes[node].children[1] = child2;
821 nodes[child1].parent = node;
822 nodes[child2].parent = node;
830 template<
typename BV>
833 if(root_node == NULL_NODE)
836 nodes[leaf].parent = NULL_NODE;
840 if(!nodes[root].isLeaf())
844 root = nodes[root].children[
select(leaf, nodes[root].children[0], nodes[root].children[1], nodes)];
846 while(!nodes[root].isLeaf());
849 size_t prev = nodes[root].parent;
850 size_t node = createNode(
prev, nodes[leaf].bv, nodes[root].bv,
nullptr);
851 if(
prev != NULL_NODE)
853 nodes[
prev].children[indexOf(root)] = node;
854 nodes[node].children[0] = root; nodes[root].parent = node;
855 nodes[node].children[1] = leaf; nodes[leaf].parent = node;
858 if(!nodes[
prev].bv.contain(nodes[node].bv))
859 nodes[
prev].bv = nodes[nodes[
prev].children[0]].bv + nodes[nodes[
prev].children[1]].bv;
863 }
while (NULL_NODE != (
prev = nodes[node].parent));
867 nodes[node].children[0] = root; nodes[root].parent = node;
868 nodes[node].children[1] = leaf; nodes[leaf].parent = node;
875 template<
typename BV>
878 if(leaf == root_node)
880 root_node = NULL_NODE;
885 size_t parent = nodes[leaf].parent;
886 size_t prev = nodes[parent].parent;
887 size_t sibling = nodes[parent].children[1-indexOf(leaf)];
889 if(
prev != NULL_NODE)
891 nodes[
prev].children[indexOf(parent)] = sibling;
892 nodes[sibling].parent =
prev;
894 while(
prev != NULL_NODE)
896 BV new_bv = nodes[nodes[
prev].children[0]].bv + nodes[nodes[
prev].children[1]].bv;
897 if(!new_bv.equal(nodes[
prev].bv))
899 nodes[
prev].bv = new_bv;
905 return (
prev != NULL_NODE) ?
prev : root_node;
910 nodes[sibling].parent = NULL_NODE;
918 template<
typename BV>
921 size_t root = removeLeaf(leaf);
922 if(root != NULL_NODE)
924 if(max_lookahead_level >= 0)
926 for(
int i = 0; (i < max_lookahead_level) && (nodes[root].parent != NULL_NODE); ++i)
927 root = nodes[root].parent;
931 insertLeaf(root, leaf);
936 template<
typename BV>
939 return (nodes[nodes[node].parent].children[1] == node);
943 template<
typename BV>
946 if(freelist == NULL_NODE)
950 nodes =
new NodeType[n_nodes_alloc];
951 std::copy(old_nodes, old_nodes + n_nodes, nodes);
954 for(
size_t i = n_nodes; i < n_nodes_alloc - 1; ++i)
955 nodes[i].
next = i + 1;
956 nodes[n_nodes_alloc - 1].next = NULL_NODE;
960 size_t node_id = freelist;
961 freelist = nodes[node_id].next;
962 nodes[node_id].parent = NULL_NODE;
963 nodes[node_id].children[0] = NULL_NODE;
964 nodes[node_id].children[1] = NULL_NODE;
970 template<
typename BV>
976 size_t node = allocateNode();
977 nodes[node].parent = parent;
978 nodes[node].data = data;
979 nodes[node].bv = bv1 + bv2;
984 template<
typename BV>
989 size_t node = allocateNode();
990 nodes[node].parent = parent;
991 nodes[node].data = data;
997 template<
typename BV>
1001 size_t node = allocateNode();
1002 nodes[node].parent = parent;
1003 nodes[node].data = data;
1008 template<
typename BV>
1011 nodes[node].next = freelist;
1017 template<
typename BV>
1020 if(!nodes[node].isLeaf())
1022 recurseRefit(nodes[node].children[0]);
1023 recurseRefit(nodes[node].children[1]);
1024 nodes[node].bv = nodes[nodes[node].children[0]].bv + nodes[nodes[node].children[1]].bv;
1031 template<
typename BV>
1034 if((!nodes[root].isLeaf()) && depth)
1036 fetchLeaves(nodes[root].children[0], leaves, depth-1);
1037 fetchLeaves(nodes[root].children[1], leaves, depth-1);
1042 *leaves = nodes[root];
1048 template<
typename BV>
1050 : nodes(nodes_), d(d_)
1056 template<
typename BV>
1059 if(nodes[i].bv.center()[d] < nodes[j].bv.center()[d])
1066 template <
typename S,
typename BV>
1091 template<
typename BV>
1098 template<
typename BV>
1105 template <
typename S>
1110 const AABB<S>& bv = nodes[query].bv;
1111 const AABB<S>& bv1 = nodes[node1].bv;
1112 const AABB<S>& bv2 = nodes[node2].bv;
1116 S d1 = fabs(v1[0]) + fabs(v1[1]) + fabs(v1[2]);
1117 S d2 = fabs(v2[0]) + fabs(v2[1]) + fabs(v2[2]);
1118 return (d1 < d2) ? 0 : 1;
1124 const AABB<S>& bv1 = nodes[node1].bv;
1125 const AABB<S>& bv2 = nodes[node2].bv;
1129 S d1 = fabs(v1[0]) + fabs(v1[1]) + fabs(v1[2]);
1130 S d2 = fabs(v2[0]) + fabs(v2[1]) + fabs(v2[2]);
1131 return (d1 < d2) ? 0 : 1;