2#include <muda/ext/geo/lbvh/bvh.h>
3#include <muda/ext/geo/lbvh/predicator.h>
7template <
typename Real,
typename Objects,
typename AABBGetter,
typename MortonCodeCalculator,
typename OutputBackInserter>
8MUDA_HOST uint32_t query(
const BVH<Real, Objects, AABBGetter, MortonCodeCalculator>& tree,
9 const query_overlap<Real> q,
10 OutputBackInserter outiter)
noexcept
12 using bvh_type = BVH<Real, Objects, AABBGetter, MortonCodeCalculator>;
13 using index_type =
typename bvh_type::index_type;
14 using aabb_type =
typename bvh_type::aabb_type;
15 using node_type =
typename bvh_type::node_type;
17 std::vector<std::size_t> stack;
21 uint32_t num_found = 0;
24 const index_type node = stack.back();
26 const index_type L_idx = tree.host_nodes()[node].left_idx;
27 const index_type R_idx = tree.host_nodes()[node].right_idx;
29 if(intersects(q.target, tree.host_aabbs()[L_idx]))
31 const auto obj_idx = tree.host_nodes()[L_idx].object_idx;
32 if(obj_idx != 0xFFFFFFFF)
39 stack.push_back(L_idx);
42 if(intersects(q.target, tree.host_aabbs()[R_idx]))
44 const auto obj_idx = tree.host_nodes()[R_idx].object_idx;
45 if(obj_idx != 0xFFFFFFFF)
52 stack.push_back(R_idx);
55 }
while(!stack.empty());
59template <
typename Real,
typename Objects,
typename AABBGetter,
typename MortonCodeCalculator,
typename DistanceCalculator>
60MUDA_HOST std::pair<uint32_t, Real> query(
const BVH<Real, Objects, AABBGetter, MortonCodeCalculator>& tree,
61 const query_nearest<Real>& q,
62 DistanceCalculator calc_dist)
noexcept
64 using bvh_type = BVH<Real, Objects, AABBGetter, MortonCodeCalculator>;
65 using real_type =
typename bvh_type::real_type;
66 using index_type =
typename bvh_type::index_type;
67 using aabb_type =
typename bvh_type::aabb_type;
68 using node_type =
typename bvh_type::node_type;
76 std::vector<std::pair<index_type, real_type>> stack = {
77 {0, mindist(tree.host_aabbs()[0], q.target)}};
80 uint32_t nearest = 0xFFFFFFFF;
81 real_type current_nearest_dist = infinity<real_type>();
84 const auto node = stack.back();
86 if(node.second > current_nearest_dist)
92 const index_type L_idx = tree.host_nodes()[node.first].left_idx;
93 const index_type R_idx = tree.host_nodes()[node.first].right_idx;
95 const aabb_type& L_box = tree.host_aabbs()[L_idx];
96 const aabb_type& R_box = tree.host_aabbs()[R_idx];
98 const real_type L_mindist = mindist(L_box, q.target);
99 const real_type R_mindist = mindist(R_box, q.target);
101 const real_type L_minmaxdist = minmaxdist(L_box, q.target);
102 const real_type R_minmaxdist = minmaxdist(R_box, q.target);
106 if(L_mindist <= R_minmaxdist)
108 const auto obj_idx = tree.host_nodes()[L_idx].object_idx;
109 if(obj_idx != 0xFFFFFFFF)
111 const real_type dist = calc_dist(q.target, tree.objects_host()[obj_idx]);
112 if(dist <= current_nearest_dist)
114 current_nearest_dist = dist;
120 stack.emplace_back(L_idx, L_mindist);
123 if(R_mindist <= L_minmaxdist)
125 const auto obj_idx = tree.host_nodes()[R_idx].object_idx;
126 if(obj_idx != 0xFFFFFFFF)
128 const real_type dist = calc_dist(q.target, tree.objects_host()[obj_idx]);
129 if(dist <= current_nearest_dist)
131 current_nearest_dist = dist;
137 stack.emplace_back(R_idx, R_mindist);
140 }
while(!stack.empty());
141 return std::make_pair(nearest, current_nearest_dist);