9 #ifndef opengl_COctreePointRenderer_H 10 #define opengl_COctreePointRenderer_H 20 namespace global_settings
50 template <
class Derived>
75 return *
static_cast<const Derived*
>(
this);
135 std::numeric_limits<float>::max(),
136 std::numeric_limits<float>::max(),
137 std::numeric_limits<float>::max()),
139 -
std::numeric_limits<float>::max(),
140 -
std::numeric_limits<float>::max(),
141 -
std::numeric_limits<float>::max())
195 switch (my_child_index)
254 throw std::runtime_error(
"my_child_index!=[0,7]");
289 bool corners_are_all_computed =
true,
290 bool trust_me_youre_visible =
false,
291 float approx_area_sqpixels = 0)
const 295 if (!corners_are_all_computed)
297 for (
int i = 0; i < 8; i++)
301 node.getCornerX(i), node.getCornerY(i), node.getCornerZ(i),
302 cr_px[i].
x, cr_px[i].
y, cr_z[i]);
307 std::numeric_limits<float>::max(),
308 std::numeric_limits<float>::max()),
310 -std::numeric_limits<float>::max(),
311 -std::numeric_limits<float>::max());
312 if (!trust_me_youre_visible)
315 for (
int i = 0; i < 8; i++)
323 const bool any_cr_zs_neg =
324 (cr_z[0] < 0 || cr_z[1] < 0 || cr_z[2] < 0 || cr_z[3] < 0 ||
325 cr_z[4] < 0 || cr_z[5] < 0 || cr_z[6] < 0 || cr_z[7] < 0);
326 const bool any_cr_zs_pos =
327 (cr_z[0] > 0 || cr_z[1] > 0 || cr_z[2] > 0 || cr_z[3] > 0 ||
328 cr_z[4] > 0 || cr_z[5] > 0 || cr_z[6] > 0 || cr_z[7] > 0);
329 const bool box_crosses_image_plane = any_cr_zs_pos && any_cr_zs_neg;
334 if (!box_crosses_image_plane &&
336 px_max.
x < 0 || px_max.
y < 0))
343 if (node.all || !node.pts.empty())
349 float render_area_sqpixels =
350 trust_me_youre_visible ? approx_area_sqpixels
351 : std::abs(px_min.x - px_max.
x) *
352 std::abs(px_min.y - px_max.
y);
353 render_area_sqpixels = std::max(1.0f, render_area_sqpixels);
357 TRenderQueueElement(node_idx, render_area_sqpixels));
366 bool children_are_all_visible_for_sure =
true;
368 if (!trust_me_youre_visible)
370 for (
int i = 0; i < 8; i++)
372 if (!(cr_px[i].
x >= 0 && cr_px[i].
y >= 0 &&
376 children_are_all_visible_for_sure =
false;
383 if (children_are_all_visible_for_sure)
390 const float approx_child_area =
391 trust_me_youre_visible
392 ? approx_area_sqpixels / 8.0f
393 : std::abs(px_min.x - px_max.
x) *
394 std::abs(px_min.y - px_max.
y) / 8.0f;
396 for (
int i = 0; i < 8; i++)
398 node.child_id[i], ri, child_cr_px, child_cr_z,
true,
399 true, approx_child_area);
404 #pragma clang diagnostic push // clang complains about unused vars (becase it 406 #pragma clang diagnostic ignored "-Wunused-variable" 412 node.bb_min.x, node.bb_min.y, node.bb_min.z);
414 node.center.x, node.bb_min.y, node.bb_min.z);
416 node.bb_max.x, node.bb_min.y, node.bb_min.z);
418 node.bb_min.x, node.center.y, node.bb_min.z);
420 node.center.x, node.center.y, node.bb_min.z);
422 node.bb_max.x, node.center.y, node.bb_min.z);
424 node.bb_min.x, node.bb_max.y, node.bb_min.z);
426 node.center.x, node.bb_max.y, node.bb_min.z);
428 node.bb_max.x, node.bb_max.y, node.bb_min.z);
431 node.bb_min.x, node.bb_min.y, node.center.z);
433 node.center.x, node.bb_min.y, node.center.z);
435 node.bb_max.x, node.bb_min.y, node.center.z);
437 node.bb_min.x, node.center.y, node.center.z);
439 node.center.x, node.center.y, node.center.z);
441 node.bb_max.x, node.center.y, node.center.z);
443 node.bb_min.x, node.bb_max.y, node.center.z);
445 node.center.x, node.bb_max.y, node.center.z);
447 node.bb_max.x, node.bb_max.y, node.center.z);
450 node.bb_min.x, node.bb_min.y, node.bb_max.z);
452 node.center.x, node.bb_min.y, node.bb_max.z);
454 node.bb_min.x, node.bb_min.y, node.bb_max.z);
456 node.bb_min.x, node.center.y, node.bb_max.z);
458 node.center.x, node.center.y, node.bb_max.z);
460 node.bb_max.x, node.center.y, node.bb_max.z);
462 node.bb_min.x, node.bb_max.y, node.bb_max.z);
464 node.center.x, node.bb_max.y, node.bb_max.z);
466 node.bb_max.x, node.bb_max.y, node.bb_max.z);
469 #define PROJ_SUB_NODE(POSTFIX) \ 470 mrpt::img::TPixelCoordf px_##POSTFIX; \ 471 float depth_##POSTFIX; \ 472 ri.projectPointPixels( \ 473 p_##POSTFIX.x, p_##POSTFIX.y, p_##POSTFIX.z, px_##POSTFIX.x, \ 474 px_##POSTFIX.y, depth_##POSTFIX); 476 #define PROJ_SUB_NODE_ALREADY_DONE(INDEX, POSTFIX) \ 477 const mrpt::img::TPixelCoordf px_##POSTFIX = cr_px[INDEX]; \ 478 float depth_##POSTFIX = cr_z[INDEX]; 515 #define DO_RECURSE_CHILD( \ 516 INDEX, SEQ0, SEQ1, SEQ2, SEQ3, SEQ4, SEQ5, SEQ6, SEQ7) \ 518 mrpt::img::TPixelCoordf child_cr_px[8] = { \ 519 px_##SEQ0, px_##SEQ1, px_##SEQ2, px_##SEQ3, \ 520 px_##SEQ4, px_##SEQ5, px_##SEQ6, px_##SEQ7}; \ 521 float child_cr_z[8] = {depth_##SEQ0, depth_##SEQ1, depth_##SEQ2, \ 522 depth_##SEQ3, depth_##SEQ4, depth_##SEQ5, \ 523 depth_##SEQ6, depth_##SEQ7}; \ 524 this->octree_recursive_render( \ 525 node.child_id[INDEX], ri, child_cr_px, child_cr_z); \ 531 0, Xm_Ym_Zm, X0_Ym_Zm, Xm_Y0_Zm, X0_Y0_Zm, Xm_Ym_Z0,
532 X0_Ym_Z0, Xm_Y0_Z0, X0_Y0_Z0)
534 1, X0_Ym_Zm, Xp_Ym_Zm, X0_Y0_Zm, Xp_Y0_Zm, X0_Ym_Z0,
535 Xp_Ym_Z0, X0_Y0_Z0, Xp_Y0_Z0)
537 2, Xm_Y0_Zm, X0_Y0_Zm, Xm_Yp_Zm, X0_Yp_Zm, Xm_Y0_Z0,
538 X0_Y0_Z0, Xm_Yp_Z0, X0_Yp_Z0)
540 3, X0_Y0_Zm, Xp_Y0_Zm, X0_Yp_Zm, Xp_Yp_Zm, X0_Y0_Z0,
541 Xp_Y0_Z0, X0_Yp_Z0, Xp_Yp_Z0)
543 4, Xm_Ym_Z0, X0_Ym_Z0, Xm_Y0_Z0, X0_Y0_Z0, Xm_Ym_Zp,
544 X0_Ym_Zp, Xm_Y0_Zp, X0_Y0_Zp)
546 5, X0_Ym_Z0, Xp_Ym_Z0, X0_Y0_Z0, Xp_Y0_Z0, X0_Ym_Zp,
547 Xp_Ym_Zp, X0_Y0_Zp, Xp_Y0_Zp)
549 6, Xm_Y0_Z0, X0_Y0_Z0, Xm_Yp_Z0, X0_Yp_Z0, Xm_Y0_Zp,
550 X0_Y0_Zp, Xm_Yp_Zp, X0_Yp_Zp)
552 7, X0_Y0_Z0, Xp_Y0_Z0, X0_Yp_Z0, Xp_Yp_Z0, X0_Y0_Zp,
553 Xp_Y0_Zp, X0_Yp_Zp, Xp_Yp_Zp)
554 #undef DO_RECURSE_CHILD 556 #undef PROJ_SUB_NODE_ALREADY_DONE 559 #pragma clang diagnostic pop 585 const size_t node_id,
const bool all_pts =
false)
588 const size_t N = all_pts ?
octree_derived().size() : node.pts.size();
599 if (has_to_compute_bb)
602 for (
size_t i = 0; i < N; i++)
605 for (
size_t i = 0; i < N; i++)
615 for (
size_t i = 0; i < N; i++)
619 if (has_to_compute_bb) node.update_bb(
p);
622 for (
size_t i = 0; i < N; i++)
627 if (has_to_compute_bb) node.update_bb(
p);
631 node.is_leaf =
false;
632 node.center =
mean * (1.0f / N);
637 for (
int i = 0; i < 8; i++)
638 node.child_id[i] = children_idx_base + i;
641 for (
int i = 0; i < 8; i++)
648 for (
size_t j = 0; j < N; j++)
650 const size_t i = all_pts ? j : node.pts[j];
698 std::vector<size_t> emptyVec;
699 node.pts.swap(emptyVec);
704 for (
int i = 0; i < 8; i++)
731 const bool draw_solid_boxes =
false)
const 738 if (!node.is_leaf)
continue;
740 gl_box->setBoxCorners(
743 gl_box->setColor(lines_color);
744 gl_box->setLineWidth(lines_width);
745 gl_box->setWireframe(!draw_solid_boxes);
754 size_t total_elements = 0;
759 o <<
"Node #" << i <<
": ";
770 o << node.pts.size() <<
" elements; ";
771 total_elements += node.pts.size();
776 o <<
"parent, center=(" << node.center.x <<
"," << node.center.y
777 <<
"," << node.center.z <<
"), children: " << node.child_id[0]
778 <<
"," << node.child_id[1] <<
"," << node.child_id[2] <<
"," 779 << node.child_id[3] <<
"," << node.child_id[4] <<
"," 780 << node.child_id[5] <<
"," << node.child_id[6] <<
"," 781 << node.child_id[7] <<
"; ";
783 o <<
" bb: (" << node.bb_min.x <<
"," << node.bb_min.y <<
"," 784 << node.bb_min.z <<
")-(" << node.bb_max.x <<
"," << node.bb_max.y
785 <<
"," << node.bb_max.z <<
")\n";
787 o <<
"Total elements in all nodes: " << total_elements << std::endl;
void clear()
Clear the list of objects in the scene, deleting objects' memory.
The structure for each octree spatial node.
bool is_leaf
true: it's a leaf and pts has valid indices; false: children is valid.
void update_bb(const mrpt::math::TPoint3Df &p)
update bounding box with a new point:
volatile size_t m_visible_octree_nodes_ongoing
bool all
true: All elements in the reference object; false: only those in pts
A set of object, which are referenced to the coordinates framework established in this object...
size_t octree_get_visible_nodes() const
Return the number of visible octree nodes in the last render event.
float getCornerZ(int i) const
size_t OCTREE_RENDER_MAX_POINTS_PER_NODE()
Default value = 1e5.
volatile size_t m_visible_octree_nodes
COctreePointRenderer()=default
Default ctor.
void octree_get_graphics_boundingboxes(mrpt::opengl::CSetOfObjects &gl_bb, const double lines_width=1, const mrpt::img::TColorf &lines_color=mrpt::img::TColorf(1, 1, 1), const bool draw_solid_boxes=false) const
Returns a graphical representation of all the bounding boxes of the octree (leaf) nodes...
A pair (x,y) of pixel coordinates (subpixel resolution).
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
const Derived & octree_derived() const
void internal_recursive_split(const size_t node_id, const bool all_pts=false)
Template class that implements the data structure and algorithms for Octree-based efficient rendering...
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
TRenderQueueElement(const size_t id, float area_sq)
Lightweight 3D point (float version).
size_t octree_get_node_count() const
Return the number of octree nodes (all of them, including the empty ones)
size_t child_id[8]
[is_leaf=false] The indices in m_octree_nodes of the 8 children.
void OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL(float value)
Default value = 0.01 points/px^2.
mrpt::math::TPoint3Df center
[is_leaf=false] The center of the node, whose coordinates are used to decide between the 8 children n...
Information about the rendering process being issued.
Derived & octree_derived()
void octree_render(const mrpt::opengl::gl_utils::TRenderInfo &ri) const
Render the entire octree recursively.
mrpt::math::TPoint3Df bb_max
void octree_recursive_render(size_t node_idx, const mrpt::opengl::gl_utils::TRenderInfo &ri, mrpt::img::TPixelCoordf cr_px[8], float cr_z[8], bool corners_are_all_computed=true, bool trust_me_youre_visible=false, float approx_area_sqpixels=0) const
Render a given node.
std::vector< TRenderQueueElement > m_render_queue
The list of elements that really are visible and will be rendered.
COctreePointRenderer(const COctreePointRenderer &)
Copy ctor.
bool m_octree_has_to_rebuild_all
#define PROJ_SUB_NODE(POSTFIX)
void octree_mark_as_outdated()
Called from the derived class (or the user) to indicate we have/want to rebuild the entire node tree ...
void octree_debug_dump_tree(std::ostream &o) const
Used for debug only.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::vector< size_t > pts
Point indices in the derived class that fall into this node.
std::deque< TNode > m_octree_nodes
First one [0] is always the root node.
void internal_octree_assure_uptodate()
A RGB color - floats in the range [0,1].
void octree_getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const
double mean(const CONTAINER &v)
Computes the mean value of a vector.
#define DO_RECURSE_CHILD( INDEX, SEQ0, SEQ1, SEQ2, SEQ3, SEQ4, SEQ5, SEQ6, SEQ7)
float getCornerY(int i) const
float render_area_sqpixels
The approximate size of the octree on the screen (squared pixels).
GLsizei const GLfloat * value
void projectPointPixels(float x, float y, float z, float &proj_x_px, float &proj_y_px, float &proj_z_depth) const
Exactly like projectPoint but the (x,y) projected coordinates are given in pixels instead of normaliz...
void setBBFromOrderInParent(const TNode &parent, int my_child_index)
size_t node_id
The node ID to render.
void insert(const CRenderizable::Ptr &newObject)
Insert a new object to the list.
void octree_assure_uptodate() const
Must be called at children class' render() previously to octree_render()
#define PROJ_SUB_NODE_ALREADY_DONE(INDEX, POSTFIX)
float getCornerX(int i) const
static Ptr Create(Args &&... args)
mrpt::math::TPoint3Df bb_min