1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/* +------------------------------------------------------------------------+
| Mobile Robot Programming Toolkit (MRPT) |
| https://www.mrpt.org/ |
| |
| Copyright (c) 2005-2023, Individual contributors, see AUTHORS file |
| See: https://www.mrpt.org/Authors - All rights reserved. |
| Released under BSD License. See: https://www.mrpt.org/License |
+------------------------------------------------------------------------+ */
#include <mrpt/gui/CDisplayWindow3D.h>
#include <mrpt/img/TColor.h>
#include <mrpt/math/geometry.h>
#include <mrpt/opengl/CPointCloud.h>
#include <mrpt/random.h>
#include <chrono>
#include <iostream>
#include <thread>
using namespace std;
using namespace mrpt;
using namespace mrpt::gui;
using namespace mrpt::opengl;
using namespace mrpt::math;
using namespace mrpt::img;
void insertRandomPoints_uniform(
const size_t N, opengl::CPointCloud::Ptr& gl, const TPoint3D& p_min,
const TPoint3D& p_max)
{
auto& rnd = random::getRandomGenerator();
for (size_t i = 0; i < N; i++)
gl->insertPoint(
rnd.drawUniform<float>(p_min.x, p_max.x),
rnd.drawUniform<float>(p_min.y, p_max.y),
rnd.drawUniform<float>(p_min.z, p_max.z));
}
void insertRandomPoints_screw(
const size_t N, opengl::CPointCloud::Ptr& gl, const TPoint3D& p_start,
const TPoint3D& p_end)
{
TPoint3D d = p_end - p_start;
d *= 1.0 / N;
TPoint3D up(0, 0, 1);
TPoint3D lat;
mrpt::math::crossProduct3D(d, up, lat);
lat *= 1.0 / lat.norm();
TPoint3D p = p_start;
for (size_t i = 0; i < N; i++)
{
const double ang = i * 0.01;
TPoint3D pp = p + up * 30 * cos(ang) + lat * 30 * sin(ang);
const auto ppf = TPoint3Df(pp);
gl->insertPoint(ppf.x, ppf.y, ppf.z);
p += d;
}
}
void insertRandomPoints_gauss(
const size_t N, opengl::CPointCloud::Ptr& gl, const TPoint3D& p_mean,
const TPoint3D& p_stddevs)
{
auto& rnd = random::getRandomGenerator();
for (size_t i = 0; i < N; i++)
gl->insertPoint(
rnd.drawGaussian1D<float>(p_mean.x, p_stddevs.x),
rnd.drawGaussian1D<float>(p_mean.y, p_stddevs.y),
rnd.drawGaussian1D<float>(p_mean.z, p_stddevs.z));
}
// ------------------------------------------------------
// TestOctreeRenderHugePointCloud
// ------------------------------------------------------
void TestOctreeRenderHugePointCloud()
{
// Change this in your program as needed:
// mrpt::global_settings::OCTREE_RENDER_MAX_DENSITY_POINTS_PER_SQPIXEL(0.1f);
CDisplayWindow3D win("Demo of MRPT's octree pointclouds", 640, 480);
COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
// CPointCloud
opengl::CPointCloud::Ptr gl_pointcloud = opengl::CPointCloud::Create();
theScene->insert(gl_pointcloud);
gl_pointcloud->setPointSize(3.0);
gl_pointcloud->enableColorFromZ();
// Set the list of all points:
const double L = 1e3;
cout << "Building point cloud...";
cout.flush();
for (int XX = -10; XX <= 10; XX++)
{
const double off_x = XX * 2 * L;
for (int YY = -10; YY <= 10; YY++)
{
const double off_y = YY * 2 * L;
insertRandomPoints_screw(
1e4, gl_pointcloud, TPoint3D(off_x + 0, off_y + 0, 0),
TPoint3D(off_x + L, off_y + 0, 500));
insertRandomPoints_screw(
1e4, gl_pointcloud, TPoint3D(off_x + L, off_y + 0, 500),
TPoint3D(off_x + L, off_y + L, -500));
insertRandomPoints_screw(
1e4, gl_pointcloud, TPoint3D(off_x + L, off_y + L, -500),
TPoint3D(off_x + 0, off_y + L, 500));
insertRandomPoints_screw(
1e4, gl_pointcloud, TPoint3D(off_x + 0, off_y + L, 500),
TPoint3D(off_x + 0, off_y + 0, 0));
}
}
cout << "Done.\n";
cout.flush();
printf("Point count: %e\n", (double)gl_pointcloud->size());
// Draw the octree bounding boxes:
mrpt::opengl::CSetOfObjects::Ptr gl_bb =
mrpt::opengl::CSetOfObjects::Create();
gl_pointcloud->octree_get_graphics_boundingboxes(*gl_bb);
theScene->insert(gl_bb);
// gl_pointcloud->octree_debug_dump_tree(std::cout);
win.setCameraZoom(600);
{
mrpt::opengl::COpenGLViewport::Ptr view = theScene->getViewport("main");
view->setViewportClipDistances(0.1f, 1e6f);
}
// IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
win.unlockAccess3DScene();
win.repaint();
cout << "Close the window or press any key to end.\n";
bool end = false;
while (win.isOpen() && !end)
{
std::this_thread::sleep_for(5ms);
if (win.keyHit())
{
switch (win.getPushedKey())
{
case 'q': end = true; break;
case 'b': gl_bb->setVisibility(!gl_bb->isVisible()); break;
};
}
// Update the texts on the gl display:
string s = mrpt::format(
"FPS=%5.02f | Rendered points=%.02e/%.02e (%.02f%%) | "
"Visib.oct.nodes: %u/%u",
win.getRenderingFPS(), (double)gl_pointcloud->getActuallyRendered(),
(double)gl_pointcloud->size(),
100 * double(gl_pointcloud->getActuallyRendered()) /
double(gl_pointcloud->size()),
(unsigned int)gl_pointcloud->octree_get_visible_nodes(),
(unsigned int)gl_pointcloud->octree_get_node_count());
win.get3DSceneAndLock();
win.addTextMessage(5, 5, s, 0);
win.addTextMessage(
5, 35, "'b': switch bounding-boxes visible, 'q': quit", 1);
win.unlockAccess3DScene();
win.repaint();
}
}
// ------------------------------------------------------
// MAIN
// ------------------------------------------------------
int main()
{
try
{
TestOctreeRenderHugePointCloud();
std::this_thread::sleep_for(500ms);
return 0;
}
catch (const std::exception& e)
{
std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
return -1;
}
}
|