34#include <visp3/core/vpConfig.h>
36#if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_VISUALIZATION) && defined(VISP_HAVE_THREADS)
38#include <visp3/gui/vpDisplayPCL.h>
42#ifndef DOXYGEN_SHOULD_SKIP_THIS
43unsigned int vpDisplayPCL::PointCloudHandling::s_nb = 0;
55 : m_stop(false), m_thread_running(false), m_verbose(false), m_width(640), m_height(480), m_posx(posx), m_posy(posy),
56 m_window_name(window_name), m_viewer(nullptr)
68 : m_stop(false), m_thread_running(false), m_verbose(false), m_width(width), m_height(height), m_posx(posx), m_posy(posy),
69 m_window_name(window_name), m_viewer(nullptr)
85void vpDisplayPCL::createViewer()
87 m_viewer = pcl::visualization::PCLVisualizer::Ptr(
new pcl::visualization::PCLVisualizer(m_window_name));
88 m_viewer->setBackgroundColor(0, 0, 0);
89 m_viewer->initCameraParameters();
90 m_viewer->setPosition(m_posx, m_posy);
91 m_viewer->setCameraPosition(0, 0, -0.25, 0, -1, 0);
92 m_viewer->setSize(m_width, m_height);
100void vpDisplayPCL::insertLegend(
const size_t &
id)
102 std::string text = this->mv_xyz_pcl[id].second.m_name;
103 unsigned int posU = 10;
104 unsigned int size = 16;
105 unsigned int posV = 10 +
id * size;
106 double rRatio = this->mv_xyz_pcl[id].second.m_color.R / 255.0;
107 double gRatio = this->mv_xyz_pcl[id].second.m_color.G / 255.0;
108 double bRatio = this->mv_xyz_pcl[id].second.m_color.B / 255.0;
110 this->m_viewer->addText(text, posU, posV, rRatio, gRatio, bRatio);
126 if (m_thread_running) {
131 size_t nb_pcls = mv_xyz_pcl.size();
132 for (
size_t id = 0;
id < nb_pcls; ++id) {
133 mv_xyz_pcl[id].second.m_do_init =
true;
136 nb_pcls = mv_colored_pcl.size();
137 for (
size_t id = 0;
id < nb_pcls; ++id) {
138 mv_colored_pcl[id].second.m_do_init =
true;
146 size_t nb_pcls = mv_xyz_pcl.size();
147 for (
size_t id = 0;
id < nb_pcls; ++id) {
148 if (mv_xyz_pcl[
id].second.m_do_init) {
149 m_viewer->addPointCloud<pcl::PointXYZ>(mv_xyz_pcl[id].second.m_pcl, mv_xyz_handlers[id], mv_xyz_pcl[id].second.m_name);
150 m_viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, mv_xyz_pcl[
id].second.m_name);
152 mv_xyz_pcl[id].second.m_do_init =
false;
155 m_viewer->updatePointCloud<pcl::PointXYZ>(mv_xyz_pcl[id].second.m_pcl, mv_xyz_handlers[id], mv_xyz_pcl[id].second.m_name);
159 nb_pcls = mv_colored_pcl.size();
160 for (
size_t id = 0;
id < nb_pcls; ++id) {
161 if (mv_colored_pcl[
id].second.m_do_init) {
162 m_viewer->addPointCloud<pcl::PointXYZRGB>(mv_colored_pcl[id].second.m_pcl, mv_color_handlers[id], mv_colored_pcl[id].second.m_name);
163 m_viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, mv_colored_pcl[
id].second.m_name);
165 mv_colored_pcl[id].second.m_do_init =
false;
168 m_viewer->updatePointCloud<pcl::PointXYZRGB>(mv_colored_pcl[id].second.m_pcl, mv_color_handlers[id], mv_colored_pcl[id].second.m_name);
176 m_viewer->spinOnce(10);
183void vpDisplayPCL::run()
196 std::lock_guard<std::mutex> lock(m_mutex_vector);
197 nb_pcls = mv_xyz_pcl.size();
199 for (
size_t id = 0;
id < nb_pcls; ++id) {
200 std::lock_guard<std::mutex> lock(mv_xyz_pcl[
id].first);
201 mv_xyz_pcl[id].second.m_do_init =
true;
205 std::lock_guard<std::mutex> lock(m_mutex_vector);
206 nb_pcls = mv_colored_pcl.size();
209 for (
size_t id = 0;
id < nb_pcls; ++id) {
210 std::lock_guard<std::mutex> lock(mv_colored_pcl[
id].first);
211 mv_colored_pcl[id].second.m_do_init =
true;
216 pcl::PointCloud<pcl::PointXYZ>::Ptr local_pointcloud(
new pcl::PointCloud<pcl::PointXYZ>());
221 std::lock_guard<std::mutex> lock(m_mutex_vector);
222 nb_pcls = mv_xyz_pcl.size();
224 for (
size_t id = 0;
id < nb_pcls; ++id) {
226 std::lock_guard<std::mutex> lock(mv_xyz_pcl[
id].first);
227 local_pointcloud = mv_xyz_pcl[id].second.m_pcl->makeShared();
230 if (mv_xyz_pcl[
id].second.m_do_init) {
231 m_viewer->addPointCloud<pcl::PointXYZ>(local_pointcloud, mv_xyz_handlers[id], mv_xyz_pcl[id].second.m_name);
232 m_viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, mv_xyz_pcl[
id].second.m_name);
234 mv_xyz_pcl[id].second.m_do_init =
false;
237 m_viewer->updatePointCloud<pcl::PointXYZ>(local_pointcloud, mv_xyz_handlers[id], mv_xyz_pcl[id].second.m_name);
241 m_viewer->spinOnce(10);
245 std::cout <<
"End of point cloud display thread" << std::endl;
255void vpDisplayPCL::runColor()
268 std::lock_guard<std::mutex> lock(m_mutex_vector);
269 nb_pcls = mv_xyz_pcl.size();
271 for (
size_t id = 0;
id < nb_pcls; ++id) {
272 std::lock_guard<std::mutex> lock(mv_xyz_pcl[
id].first);
273 mv_xyz_pcl[id].second.m_do_init =
true;
277 std::lock_guard<std::mutex> lock(m_mutex_vector);
278 nb_pcls = mv_colored_pcl.size();
281 for (
size_t id = 0;
id < nb_pcls; ++id) {
282 std::lock_guard<std::mutex> lock(mv_colored_pcl[
id].first);
283 mv_colored_pcl[id].second.m_do_init =
true;
288 pcl::PointCloud<pcl::PointXYZRGB>::Ptr local_pointcloud(
new pcl::PointCloud<pcl::PointXYZRGB>());
292 std::lock_guard<std::mutex> lock(m_mutex_vector);
293 nb_pcls = mv_colored_pcl.size();
295 for (
size_t id = 0;
id < nb_pcls; ++id) {
297 std::lock_guard<std::mutex> lock(mv_colored_pcl[
id].first);
298 local_pointcloud = mv_colored_pcl[id].second.m_pcl->makeShared();
301 if (mv_colored_pcl[
id].second.m_do_init) {
302 std::lock_guard<std::mutex> lock(mv_colored_pcl[
id].first);
303 m_viewer->addPointCloud<pcl::PointXYZRGB>(local_pointcloud, mv_color_handlers[id], mv_colored_pcl[id].second.m_name);
304 m_viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, mv_colored_pcl[
id].second.m_name);
306 mv_colored_pcl[id].second.m_do_init =
false;
309 std::lock_guard<std::mutex> lock(mv_colored_pcl[
id].first);
310 m_viewer->updatePointCloud<pcl::PointXYZRGB>(local_pointcloud, mv_color_handlers[id], mv_colored_pcl[id].second.m_name);
314 m_viewer->spinOnce(10);
318 std::cout <<
"End of point cloud display thread" << std::endl;
334 if (!m_thread_running) {
337 m_thread = std::thread(&vpDisplayPCL::runColor,
this);
340 m_thread = std::thread(&vpDisplayPCL::run,
this);
342 m_thread_running =
true;
359 if (!m_thread_running) {
361 mv_xyz_pcl.emplace_back(std::pair<std::mutex &, XYZPointCloudHandling>(std::ref(mutex), XYZPointCloudHandling(pointcloud, name, color)));
362 mv_xyz_handlers.push_back(pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(pointcloud, color.R, color.G, color.B));
363 m_thread = std::thread(&vpDisplayPCL::run,
this);
364 m_thread_running =
true;
380 if (!m_thread_running) {
382 mv_colored_pcl.emplace_back(std::pair<std::mutex &, ColoredPointCloudHandling>(std::ref(mutex), ColoredPointCloudHandling(pointcloud_color)));
383 mv_color_handlers.push_back(pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB>(pointcloud_color));
384 m_thread = std::thread(&vpDisplayPCL::runColor,
this);
385 m_thread_running =
true;
402 std::lock_guard lg(m_mutex_vector);
403 mv_xyz_pcl.emplace_back(std::pair<std::mutex &, XYZPointCloudHandling>(std::ref(mutex), XYZPointCloudHandling(pointcloud, name, color)));
404 mv_xyz_handlers.push_back(pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(pointcloud, color.R, color.G, color.B));
415 std::lock_guard lg(m_mutex_vector);
416 mv_colored_pcl.emplace_back(std::pair<std::mutex &, ColoredPointCloudHandling>(std::ref(mutex), ColoredPointCloudHandling(pointcloud)));
417 mv_color_handlers.push_back(pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB>(pointcloud));
445 m_window_name = window_name;
453 if (m_thread_running) {
457 if (m_thread.joinable()) {
461 m_thread_running =
false;
474#elif !defined(VISP_BUILD_SHARED_LIBS)
476void dummy_vpDisplayPCL() { }
Class to define RGB colors available for display functionalities.
void setPosition(int posx, int posy)
void display(const bool &blocking=false)
Monothread display method.
void setWindowName(const std::string &window_name)
vpDisplayPCL(int posx=0, int posy=0, const std::string &window_name="")
void startThread(const bool &colorThread=false)
void setVerbose(bool verbose)
void addPointCloud(std::mutex &mutex, pcl::PointCloud< pcl::PointXYZ >::Ptr pointcloud, const std::string &name="", const vpColor &color=vpColor::red)
Insert a point cloud to display.
error that can be emitted by ViSP classes.