Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
vpDisplayPCL.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Display a point cloud using PCL library.
32  */
33 
34 #include <visp3/core/vpConfig.h>
35 
36 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_VISUALIZATION) && defined(VISP_HAVE_THREADS)
37 
38 #include <visp3/gui/vpDisplayPCL.h>
39 
40 BEGIN_VISP_NAMESPACE
45 vpDisplayPCL::vpDisplayPCL(int posx, int posy, const std::string &window_name)
46  : m_stop(false), m_verbose(false), m_width(640), m_height(480), m_posx(posx), m_posy(posy),
47  m_window_name(window_name), m_viewer(nullptr)
48 { }
49 
58 vpDisplayPCL::vpDisplayPCL(unsigned int width, unsigned int height, int posx, int posy, const std::string &window_name)
59  : m_stop(false), m_verbose(false), m_width(width), m_height(height), m_posx(posx), m_posy(posy),
60  m_window_name(window_name), m_viewer(nullptr)
61 { }
62 
69 {
70  stop();
71 }
72 
78 void vpDisplayPCL::run(std::mutex &pointcloud_mutex, pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud)
79 {
80  pcl::PointCloud<pcl::PointXYZ>::Ptr local_pointcloud(new pcl::PointCloud<pcl::PointXYZ>());
81  m_viewer = pcl::visualization::PCLVisualizer::Ptr(new pcl::visualization::PCLVisualizer());
82  m_viewer->setBackgroundColor(0, 0, 0);
83  m_viewer->initCameraParameters();
84  m_viewer->setPosition(m_posx, m_posy);
85  m_viewer->setCameraPosition(0, 0, -0.25, 0, -1, 0);
86  m_viewer->setSize(m_width, m_height);
87  m_viewer->setWindowName(m_window_name);
88  bool init = true;
89 
90  while (!m_stop) {
91  {
92  std::lock_guard<std::mutex> lock(pointcloud_mutex);
93  local_pointcloud = pointcloud->makeShared();
94  }
95 
96  if (init) {
97  m_viewer->addPointCloud<pcl::PointXYZ>(local_pointcloud, "sample cloud");
98  m_viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
99 
100  init = false;
101  }
102  else {
103  m_viewer->updatePointCloud<pcl::PointXYZ>(local_pointcloud, "sample cloud");
104  }
105 
106  m_viewer->spinOnce(10);
107  }
108 
109  if (m_verbose) {
110  std::cout << "End of point cloud display thread" << std::endl;
111  }
112 }
113 
119 void vpDisplayPCL::run_color(std::mutex &mutex, pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud_color)
120 {
121  pcl::PointCloud<pcl::PointXYZRGB>::Ptr local_pointcloud(new pcl::PointCloud<pcl::PointXYZRGB>());
122  pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(pointcloud_color);
123  m_viewer = pcl::visualization::PCLVisualizer::Ptr(new pcl::visualization::PCLVisualizer());
124  m_viewer->setBackgroundColor(0, 0, 0);
125  m_viewer->initCameraParameters();
126  m_viewer->setPosition(m_posx, m_posy);
127  m_viewer->setCameraPosition(0, 0, -0.25, 0, -1, 0);
128  m_viewer->setSize(m_width, m_height);
129  m_viewer->setWindowName(m_window_name);
130  bool init = true;
131 
132  while (!m_stop) {
133  {
134  std::lock_guard<std::mutex> lock(mutex);
135  local_pointcloud = pointcloud_color->makeShared();
136  }
137 
138  if (init) {
139  m_viewer->addPointCloud<pcl::PointXYZRGB>(local_pointcloud, rgb, "RGB sample cloud");
140  m_viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "RGB sample cloud");
141 
142  init = false;
143  }
144  else {
145  m_viewer->updatePointCloud<pcl::PointXYZRGB>(local_pointcloud, rgb, "RGB sample cloud");
146  }
147 
148  m_viewer->spinOnce(10);
149  }
150 
151  if (m_verbose) {
152  std::cout << "End of point cloud display thread" << std::endl;
153  }
154 }
155 
163 void vpDisplayPCL::startThread(std::mutex &mutex, pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud)
164 {
165  m_thread = std::thread(&vpDisplayPCL::run, this, std::ref(mutex), pointcloud);
166 }
167 
175 void vpDisplayPCL::startThread(std::mutex &mutex, pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud_color)
176 {
177  m_thread = std::thread(&vpDisplayPCL::run_color, this, std::ref(mutex), pointcloud_color);
178 }
179 
189 void vpDisplayPCL::setPosition(int posx, int posy)
190 {
191  m_posx = posx;
192  m_posy = posy;
193 }
194 
203 void vpDisplayPCL::setWindowName(const std::string &window_name)
204 {
205  m_window_name = window_name;
206 }
207 
212 {
213  if (!m_stop) {
214  m_stop = true;
215 
216  if (m_thread.joinable()) {
217  m_thread.join();
218  }
219  }
220 }
221 
226 void vpDisplayPCL::setVerbose(bool verbose)
227 {
228  m_verbose = verbose;
229 }
230 END_VISP_NAMESPACE
231 #elif !defined(VISP_BUILD_SHARED_LIBS)
232 // Work around to avoid warning: libvisp_gui.a(vpDisplayPCL.cpp.o) has no symbols
233 void dummy_vpDisplayPCL() { };
234 
235 #endif
void setPosition(int posx, int posy)
void startThread(std::mutex &mutex, pcl::PointCloud< pcl::PointXYZ >::Ptr pointcloud)
void setWindowName(const std::string &window_name)
vpDisplayPCL(int posx=0, int posy=0, const std::string &window_name="")
void setVerbose(bool verbose)