9 #ifndef opengl_COctreePointRenderer_H
10 #define opengl_COctreePointRenderer_H
20 namespace global_settings
50 template <
class Derived>
80 return *
static_cast<const Derived*
>(
this);
141 std::numeric_limits<float>::max(),
142 std::numeric_limits<float>::max(),
143 std::numeric_limits<float>::max()),
145 -std::numeric_limits<float>::max(),
146 -std::numeric_limits<float>::max(),
147 -std::numeric_limits<float>::max()),
201 switch (my_child_index)
260 throw std::runtime_error(
"my_child_index!=[0,7]");
296 bool corners_are_all_computed =
true,
297 bool trust_me_youre_visible =
false,
298 float approx_area_sqpixels = 0)
const
302 if (!corners_are_all_computed)
304 for (
int i = 0; i < 8; i++)
308 node.getCornerX(i), node.getCornerY(i), node.getCornerZ(i),
309 cr_px[i].
x, cr_px[i].
y, cr_z[i]);
314 std::numeric_limits<float>::max(),
315 std::numeric_limits<float>::max()),
317 -std::numeric_limits<float>::max(),
318 -std::numeric_limits<float>::max());
319 if (!trust_me_youre_visible)
322 for (
int i = 0; i < 8; i++)
330 const bool any_cr_zs_neg =
331 (cr_z[0] < 0 || cr_z[1] < 0 || cr_z[2] < 0 || cr_z[3] < 0 ||
332 cr_z[4] < 0 || cr_z[5] < 0 || cr_z[6] < 0 || cr_z[7] < 0);
333 const bool any_cr_zs_pos =
334 (cr_z[0] > 0 || cr_z[1] > 0 || cr_z[2] > 0 || cr_z[3] > 0 ||
335 cr_z[4] > 0 || cr_z[5] > 0 || cr_z[6] > 0 || cr_z[7] > 0);
336 const bool box_crosses_image_plane = any_cr_zs_pos && any_cr_zs_neg;
341 if (!box_crosses_image_plane &&
343 px_max.
x < 0 || px_max.
y < 0))
350 if (node.all || !node.pts.empty())
356 const float render_area_sqpixels =
357 trust_me_youre_visible ? approx_area_sqpixels
358 : std::abs(px_min.x - px_max.
x) *
359 std::abs(px_min.y - px_max.
y);
363 TRenderQueueElement(node_idx, render_area_sqpixels));
372 bool children_are_all_visible_for_sure =
true;
374 if (!trust_me_youre_visible)
376 for (
int i = 0; i < 8; i++)
378 if (!(cr_px[i].
x >= 0 && cr_px[i].
y >= 0 &&
382 children_are_all_visible_for_sure =
false;
389 if (children_are_all_visible_for_sure)
396 const float approx_child_area =
397 trust_me_youre_visible
398 ? approx_area_sqpixels / 8.0f
399 : std::abs(px_min.x - px_max.
x) *
400 std::abs(px_min.y - px_max.
y) / 8.0f;
402 for (
int i = 0; i < 8; i++)
404 node.child_id[i], ri, child_cr_px, child_cr_z,
true,
405 true, approx_child_area);
410 #pragma clang diagnostic push // clang complains about unused vars (becase it
412 #pragma clang diagnostic ignored "-Wunused-variable"
418 node.bb_min.x, node.bb_min.y, node.bb_min.z);
420 node.center.x, node.bb_min.y, node.bb_min.z);
422 node.bb_max.x, node.bb_min.y, node.bb_min.z);
424 node.bb_min.x, node.center.y, node.bb_min.z);
426 node.center.x, node.center.y, node.bb_min.z);
428 node.bb_max.x, node.center.y, node.bb_min.z);
430 node.bb_min.x, node.bb_max.y, node.bb_min.z);
432 node.center.x, node.bb_max.y, node.bb_min.z);
434 node.bb_max.x, node.bb_max.y, node.bb_min.z);
437 node.bb_min.x, node.bb_min.y, node.center.z);
439 node.center.x, node.bb_min.y, node.center.z);
441 node.bb_max.x, node.bb_min.y, node.center.z);
443 node.bb_min.x, node.center.y, node.center.z);
445 node.center.x, node.center.y, node.center.z);
447 node.bb_max.x, node.center.y, node.center.z);
449 node.bb_min.x, node.bb_max.y, node.center.z);
451 node.center.x, node.bb_max.y, node.center.z);
453 node.bb_max.x, node.bb_max.y, node.center.z);
456 node.bb_min.x, node.bb_min.y, node.bb_max.z);
458 node.center.x, node.bb_min.y, node.bb_max.z);
460 node.bb_min.x, node.bb_min.y, node.bb_max.z);
462 node.bb_min.x, node.center.y, node.bb_max.z);
464 node.center.x, node.center.y, node.bb_max.z);
466 node.bb_max.x, node.center.y, node.bb_max.z);
468 node.bb_min.x, node.bb_max.y, node.bb_max.z);
470 node.center.x, node.bb_max.y, node.bb_max.z);
472 node.bb_max.x, node.bb_max.y, node.bb_max.z);
475 #define PROJ_SUB_NODE(POSTFIX) \
476 mrpt::img::TPixelCoordf px_##POSTFIX; \
477 float depth_##POSTFIX; \
478 ri.projectPointPixels( \
479 p_##POSTFIX.x, p_##POSTFIX.y, p_##POSTFIX.z, px_##POSTFIX.x, \
480 px_##POSTFIX.y, depth_##POSTFIX);
482 #define PROJ_SUB_NODE_ALREADY_DONE(INDEX, POSTFIX) \
483 const mrpt::img::TPixelCoordf px_##POSTFIX = cr_px[INDEX]; \
484 float depth_##POSTFIX = cr_z[INDEX];
521 #define DO_RECURSE_CHILD( \
522 INDEX, SEQ0, SEQ1, SEQ2, SEQ3, SEQ4, SEQ5, SEQ6, SEQ7) \
524 mrpt::img::TPixelCoordf child_cr_px[8] = { \
525 px_##SEQ0, px_##SEQ1, px_##SEQ2, px_##SEQ3, \
526 px_##SEQ4, px_##SEQ5, px_##SEQ6, px_##SEQ7}; \
527 float child_cr_z[8] = {depth_##SEQ0, depth_##SEQ1, depth_##SEQ2, \
528 depth_##SEQ3, depth_##SEQ4, depth_##SEQ5, \
529 depth_##SEQ6, depth_##SEQ7}; \
530 this->octree_recursive_render( \
531 node.child_id[INDEX], ri, child_cr_px, child_cr_z); \
537 0, Xm_Ym_Zm, X0_Ym_Zm, Xm_Y0_Zm, X0_Y0_Zm, Xm_Ym_Z0,
538 X0_Ym_Z0, Xm_Y0_Z0, X0_Y0_Z0)
540 1, X0_Ym_Zm, Xp_Ym_Zm, X0_Y0_Zm, Xp_Y0_Zm, X0_Ym_Z0,
541 Xp_Ym_Z0, X0_Y0_Z0, Xp_Y0_Z0)
543 2, Xm_Y0_Zm, X0_Y0_Zm, Xm_Yp_Zm, X0_Yp_Zm, Xm_Y0_Z0,
544 X0_Y0_Z0, Xm_Yp_Z0, X0_Yp_Z0)
546 3, X0_Y0_Zm, Xp_Y0_Zm, X0_Yp_Zm, Xp_Yp_Zm, X0_Y0_Z0,
547 Xp_Y0_Z0, X0_Yp_Z0, Xp_Yp_Z0)
549 4, Xm_Ym_Z0, X0_Ym_Z0, Xm_Y0_Z0, X0_Y0_Z0, Xm_Ym_Zp,
550 X0_Ym_Zp, Xm_Y0_Zp, X0_Y0_Zp)
552 5, X0_Ym_Z0, Xp_Ym_Z0, X0_Y0_Z0, Xp_Y0_Z0, X0_Ym_Zp,
553 Xp_Ym_Zp, X0_Y0_Zp, Xp_Y0_Zp)
555 6, Xm_Y0_Z0, X0_Y0_Z0, Xm_Yp_Z0, X0_Yp_Z0, Xm_Y0_Zp,
556 X0_Y0_Zp, Xm_Yp_Zp, X0_Yp_Zp)
558 7, X0_Y0_Z0, Xp_Y0_Z0, X0_Yp_Z0, Xp_Yp_Z0, X0_Y0_Zp,
559 Xp_Y0_Zp, X0_Yp_Zp, Xp_Yp_Zp)
560 #undef DO_RECURSE_CHILD
562 #undef PROJ_SUB_NODE_ALREADY_DONE
565 #pragma clang diagnostic pop
591 const size_t node_id,
const bool all_pts =
false)
594 const size_t N = all_pts ?
octree_derived().size() : node.pts.size();
605 if (has_to_compute_bb)
608 for (
size_t i = 0; i < N; i++)
611 for (
size_t i = 0; i < N; i++)
621 for (
size_t i = 0; i < N; i++)
625 if (has_to_compute_bb) node.update_bb(
p);
628 for (
size_t i = 0; i < N; i++)
633 if (has_to_compute_bb) node.update_bb(
p);
637 node.is_leaf =
false;
638 node.center =
mean * (1.0f / N);
643 for (
int i = 0; i < 8; i++)
644 node.child_id[i] = children_idx_base + i;
647 for (
int i = 0; i < 8; i++)
654 for (
size_t j = 0; j < N; j++)
656 const size_t i = all_pts ? j : node.pts[j];
704 std::vector<size_t> emptyVec;
705 node.pts.swap(emptyVec);
710 for (
int i = 0; i < 8; i++)
737 const bool draw_solid_boxes =
false)
const
744 if (!node.is_leaf)
continue;
746 mrpt::make_aligned_shared<mrpt::opengl::CBox>();
747 gl_box->setBoxCorners(
750 gl_box->setColor(lines_color);
751 gl_box->setLineWidth(lines_width);
752 gl_box->setWireframe(!draw_solid_boxes);
761 size_t total_elements = 0;
766 o <<
"Node #" << i <<
": ";
777 o << node.pts.size() <<
" elements; ";
778 total_elements += node.pts.size();
783 o <<
"parent, center=(" << node.center.x <<
"," << node.center.y
784 <<
"," << node.center.z <<
"), children: " << node.child_id[0]
785 <<
"," << node.child_id[1] <<
"," << node.child_id[2] <<
","
786 << node.child_id[3] <<
"," << node.child_id[4] <<
","
787 << node.child_id[5] <<
"," << node.child_id[6] <<
","
788 << node.child_id[7] <<
"; ";
790 o <<
" bb: (" << node.bb_min.x <<
"," << node.bb_min.y <<
","
791 << node.bb_min.z <<
")-(" << node.bb_max.x <<
"," << node.bb_max.y
792 <<
"," << node.bb_max.z <<
")\n";
794 o <<
"Total elements in all nodes: " << total_elements << std::endl;