124 template <
typename U>
125 using auto_const_t =
typename Base::template auto_const_t<U>;
127 template <
typename Real_,
typename Object_,
typename AABBGetter,
typename MortonCodeCalculator>
131 using real_type = Real;
134 using index_type = std::uint32_t;
135 using object_type = Object;
144 MUDA_GENERIC
void operator()(uint32_t obj_idx)
const noexcept {}
148 const uint32_t num_objects,
149 auto_const_t<node_type>* nodes,
150 auto_const_t<aabb_type>* aabbs,
151 auto_const_t<object_type>* objects)
152 : m_num_nodes(num_nodes)
153 , m_num_objects(num_objects)
158 MUDA_KERNEL_ASSERT(m_nodes && m_aabbs && m_objects,
159 "BVHViewerBase[%s:%s]: nullptr is passed,"
170 MUDA_GENERIC
auto as_const() const noexcept
172 return ConstViewer{m_num_nodes, m_num_objects, m_nodes, m_aabbs, m_objects};
175 MUDA_GENERIC
operator ConstViewer() const noexcept
180 MUDA_GENERIC
auto num_nodes() const noexcept {
return m_num_nodes; }
181 MUDA_GENERIC
auto num_objects() const noexcept {
return m_num_objects; }
187 template <
typename F, u
int32_t StackNum = 64>
188 MUDA_GENERIC uint32_t query(
const query_overlap<real_type>& q,
189 F callback = DefaultQueryCallback{})
const noexcept
191 index_type stack[StackNum];
192 index_type* stack_ptr = stack;
193 index_type* stack_end = stack + StackNum;
196 uint32_t num_found = 0;
199 const index_type node = *--stack_ptr;
200 const index_type L_idx = m_nodes[node].left_idx;
201 const index_type R_idx = m_nodes[node].right_idx;
203 if(intersects(q.target, m_aabbs[L_idx]))
205 const auto obj_idx = m_nodes[L_idx].object_idx;
206 if(obj_idx != 0xFFFFFFFF)
208 if constexpr(!std::is_same_v<F, DefaultQueryCallback>)
216 *stack_ptr++ = L_idx;
219 if(intersects(q.target, m_aabbs[R_idx]))
221 const auto obj_idx = m_nodes[R_idx].object_idx;
222 if(obj_idx != 0xFFFFFFFF)
224 if constexpr(!std::is_same_v<F, DefaultQueryCallback>)
232 *stack_ptr++ = R_idx;
235 MUDA_KERNEL_ASSERT(stack_ptr < stack_end,
236 "LBVHQuery[%s:%s]: stack overflow, try use a larger StackNum.",
238 this->kernel_name());
239 }
while(stack < stack_ptr);
249 template <
typename FDistanceCalculator, u
int32_t StackNum = 64>
250 MUDA_GENERIC thrust::pair<uint32_t, real_type> query(
251 const query_nearest<real_type>& q, FDistanceCalculator calc_dist)
const noexcept
254 thrust::pair<index_type, real_type> stack[StackNum];
255 thrust::pair<index_type, real_type>* stack_ptr = stack;
256 thrust::pair<index_type, real_type>* stack_end = stack + StackNum;
258 *stack_ptr++ = thrust::make_pair(0, mindist(m_aabbs[0], q.target));
260 uint32_t nearest = 0xFFFFFFFF;
261 real_type dist_to_nearest_object = infinity<real_type>();
264 const auto node = *--stack_ptr;
265 if(node.second > dist_to_nearest_object)
271 const index_type L_idx = m_nodes[node.first].left_idx;
272 const index_type R_idx = m_nodes[node.first].right_idx;
274 const aabb_type& L_box = m_aabbs[L_idx];
275 const aabb_type& R_box = m_aabbs[R_idx];
277 const real_type L_mindist = mindist(L_box, q.target);
278 const real_type R_mindist = mindist(R_box, q.target);
280 const real_type L_minmaxdist = minmaxdist(L_box, q.target);
281 const real_type R_minmaxdist = minmaxdist(R_box, q.target);
285 if(L_mindist <= R_minmaxdist)
287 const auto obj_idx = m_nodes[L_idx].object_idx;
288 if(obj_idx != 0xFFFFFFFF)
290 const real_type dist = calc_dist(q.target, m_objects[obj_idx]);
291 if(dist <= dist_to_nearest_object)
293 dist_to_nearest_object = dist;
299 *stack_ptr++ = thrust::make_pair(L_idx, L_mindist);
302 if(R_mindist <= L_minmaxdist)
304 const auto obj_idx = m_nodes[R_idx].object_idx;
305 if(obj_idx != 0xFFFFFFFF)
307 const real_type dist = calc_dist(q.target, m_objects[obj_idx]);
308 if(dist <= dist_to_nearest_object)
310 dist_to_nearest_object = dist;
316 *stack_ptr++ = thrust::make_pair(R_idx, R_mindist);
319 MUDA_KERNEL_ASSERT(stack_ptr < stack_end,
320 "LBVHQuery[%s:%s]: stack overflow, try use a larger StackNum.",
322 this->kernel_name());
323 }
while(stack < stack_ptr);
324 return thrust::make_pair(nearest, dist_to_nearest_object);
327 MUDA_GENERIC auto_const_t<object_type>& object(
const uint32_t idx)
noexcept
330 return m_objects[idx];
333 MUDA_GENERIC
const object_type& object(
const uint32_t idx)
const noexcept
336 return m_objects[idx];
340 uint32_t m_num_nodes;
341 uint32_t m_num_objects;
343 auto_const_t<node_type>* m_nodes;
344 auto_const_t<aabb_type>* m_aabbs;
345 auto_const_t<object_type>* m_objects;
347 MUDA_INLINE MUDA_GENERIC
void check_index(
const uint32_t idx)
const noexcept
349 MUDA_KERNEL_ASSERT(idx < m_num_objects,
350 "BVHViewer[%s:%s]: index out of range, idx=%u, num_objects=%u",