Visual Servoing Platform  version 3.6.1 under development (2024-04-30)
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 
44 vpDisplayPCL::vpDisplayPCL(int posx, int posy, const std::string &window_name)
45  : m_stop(false), m_verbose(false), m_width(640), m_height(480), m_posx(posx), m_posy(posy),
46  m_window_name(window_name), m_viewer(nullptr)
47 { }
48 
57 vpDisplayPCL::vpDisplayPCL(unsigned int width, unsigned int height, int posx, int posy, const std::string &window_name)
58  : m_stop(false), m_verbose(false), m_width(width), m_height(height), m_posx(posx), m_posy(posy),
59  m_window_name(window_name), m_viewer(nullptr)
60 { }
61 
67 vpDisplayPCL::~vpDisplayPCL()
68 {
69  stop();
70 }
71 
77 void vpDisplayPCL::run(std::mutex &pointcloud_mutex, pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud)
78 {
79  pcl::PointCloud<pcl::PointXYZ>::Ptr local_pointcloud(new pcl::PointCloud<pcl::PointXYZ>());
80  m_viewer = pcl::visualization::PCLVisualizer::Ptr(new pcl::visualization::PCLVisualizer());
81  m_viewer->setBackgroundColor(0, 0, 0);
82  m_viewer->initCameraParameters();
83  m_viewer->setPosition(m_posx, m_posy);
84  m_viewer->setCameraPosition(0, 0, -0.25, 0, -1, 0);
85  m_viewer->setSize(m_width, m_height);
86  m_viewer->setWindowName(m_window_name);
87  bool init = true;
88 
89  while (!m_stop) {
90  {
91  std::lock_guard<std::mutex> lock(pointcloud_mutex);
92  local_pointcloud = pointcloud->makeShared();
93  }
94 
95  if (init) {
96  m_viewer->addPointCloud<pcl::PointXYZ>(local_pointcloud, "sample cloud");
97  m_viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
98 
99  init = false;
100  }
101  else {
102  m_viewer->updatePointCloud<pcl::PointXYZ>(local_pointcloud, "sample cloud");
103  }
104 
105  m_viewer->spinOnce(10);
106  }
107 
108  if (m_verbose) {
109  std::cout << "End of point cloud display thread" << std::endl;
110  }
111 }
112 
118 void vpDisplayPCL::run_color(std::mutex &mutex, pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud_color)
119 {
120  pcl::PointCloud<pcl::PointXYZRGB>::Ptr local_pointcloud(new pcl::PointCloud<pcl::PointXYZRGB>());
121  pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(pointcloud_color);
122  m_viewer = pcl::visualization::PCLVisualizer::Ptr(new pcl::visualization::PCLVisualizer());
123  m_viewer->setBackgroundColor(0, 0, 0);
124  m_viewer->initCameraParameters();
125  m_viewer->setPosition(m_posx, m_posy);
126  m_viewer->setCameraPosition(0, 0, -0.25, 0, -1, 0);
127  m_viewer->setSize(m_width, m_height);
128  m_viewer->setWindowName(m_window_name);
129  bool init = true;
130 
131  while (!m_stop) {
132  {
133  std::lock_guard<std::mutex> lock(mutex);
134  local_pointcloud = pointcloud_color->makeShared();
135  }
136 
137  if (init) {
138  m_viewer->addPointCloud<pcl::PointXYZRGB>(local_pointcloud, rgb, "RGB sample cloud");
139  m_viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "RGB sample cloud");
140 
141  init = false;
142  }
143  else {
144  m_viewer->updatePointCloud<pcl::PointXYZRGB>(local_pointcloud, rgb, "RGB sample cloud");
145  }
146 
147  m_viewer->spinOnce(10);
148  }
149 
150  if (m_verbose) {
151  std::cout << "End of point cloud display thread" << std::endl;
152  }
153 }
154 
162 void vpDisplayPCL::startThread(std::mutex &mutex, pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud)
163 {
164  m_thread = std::thread(&vpDisplayPCL::run, this, std::ref(mutex), pointcloud);
165 }
166 
174 void vpDisplayPCL::startThread(std::mutex &mutex, pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud_color)
175 {
176  m_thread = std::thread(&vpDisplayPCL::run_color, this, std::ref(mutex), pointcloud_color);
177 }
178 
188 void vpDisplayPCL::setPosition(int posx, int posy)
189 {
190  m_posx = posx;
191  m_posy = posy;
192 }
193 
202 void vpDisplayPCL::setWindowName(const std::string &window_name)
203 {
204  m_window_name = window_name;
205 }
206 
210 void vpDisplayPCL::stop()
211 {
212  if (!m_stop) {
213  m_stop = true;
214 
215  if (m_thread.joinable()) {
216  m_thread.join();
217  }
218  }
219 }
220 
225 void vpDisplayPCL::setVerbose(bool verbose)
226 {
227  m_verbose = verbose;
228 }
229 
230 #elif !defined(VISP_BUILD_SHARED_LIBS)
231 // Work around to avoid warning: libvisp_gui.a(vpDisplayPCL.cpp.o) has no symbols
232 void dummy_vpDisplayPCL() { };
233 
234 #endif
void init(unsigned int h, unsigned int w, Type value)
Definition: vpImage.h:619