Point Cloud Library (PCL)  1.10.0
registration_visualizer.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 
39 #include <thread>
40 
41 //////////////////////////////////////////////////////////////////////////////////////////////
42 template<typename PointSource, typename PointTarget> void
44 {
45  // Create and start the rendering thread. This will open the display window.
46  viewer_thread_ = std::thread (&pcl::RegistrationVisualizer<PointSource, PointTarget>::runDisplay, this);
47 }
48 
49 //////////////////////////////////////////////////////////////////////////////////////////////
50 template<typename PointSource, typename PointTarget> void
52 {
53  // Stop the rendering thread. This will kill the display window.
54  viewer_thread_.~thread ();
55 }
56 
57 //////////////////////////////////////////////////////////////////////////////////////////////
58 template<typename PointSource, typename PointTarget> void
60 {
61  // Open 3D viewer
62  viewer_
64  viewer_->initCameraParameters ();
65 
66  // Create the handlers for the three point clouds buffers: cloud_source_, cloud_target_ and cloud_intermediate_
67  pcl::visualization::PointCloudColorHandlerCustom<PointSource> cloud_source_handler_ (cloud_source_.makeShared (),
68  255, 0, 0);
69  pcl::visualization::PointCloudColorHandlerCustom<PointTarget> cloud_target_handler_ (cloud_target_.makeShared (),
70  0, 0, 255);
71  pcl::visualization::PointCloudColorHandlerCustom<PointSource> cloud_intermediate_handler_ (cloud_intermediate_.makeShared (),
72  255, 255, 0);
73 
74  // Create the view port for displaying initial source and target point clouds
75  int v1 (0);
76  viewer_->createViewPort (0.0, 0.0, 0.5, 1.0, v1);
77  viewer_->setBackgroundColor (0, 0, 0, v1);
78  viewer_->addText ("Initial position of source and target point clouds", 10, 50, "title v1", v1);
79  viewer_->addText ("Blue -> target", 10, 30, 0.0, 0.0, 1.0, "legend target v1", v1);
80  viewer_->addText ("Red -> source", 10, 10, 1.0, 0.0, 0.0, "legend source v1", v1);
81  //
82  viewer_->addPointCloud<PointSource> (cloud_source_.makeShared (), cloud_source_handler_, "cloud source v1", v1);
83  viewer_->addPointCloud<PointTarget> (cloud_target_.makeShared (), cloud_target_handler_, "cloud target v1", v1);
84 
85  // Create the view port for displaying the registration process of source to target point cloud
86  int v2 (0);
87  viewer_->createViewPort (0.5, 0.0, 1.0, 1.0, v2);
88  viewer_->setBackgroundColor (0.1, 0.1, 0.1, v2);
89  std::string registration_port_title_ = "Registration using "+registration_method_name_;
90  viewer_->addText (registration_port_title_, 10, 90, "title v2", v2);
91 
92  viewer_->addText ("Yellow -> intermediate", 10, 50, 1.0, 1.0, 0.0, "legend intermediate v2", v2);
93  viewer_->addText ("Blue -> target", 10, 30, 0.0, 0.0, 1.0, "legend target v2", v2);
94  viewer_->addText ("Red -> source", 10, 10, 1.0, 0.0, 0.0, "legend source v2", v1);
95 
96 // viewer_->addPointCloud<PointSource> (cloud_source_.makeShared (), cloud_source_handler_, "cloud source v2", v2);
97  viewer_->addPointCloud<PointTarget> (cloud_target_.makeShared (), cloud_target_handler_, "cloud target v2", v2);
98  viewer_->addPointCloud<PointSource> (cloud_intermediate_.makeShared (), cloud_intermediate_handler_,
99  "cloud intermediate v2", v2);
100 
101  // Used to remove all old correspondences
102  std::size_t correspondeces_old_size = 0;
103 
104  // Add coordinate system to both ports
105  viewer_->addCoordinateSystem (1.0, "global");
106 
107  // The root name of correspondence lines
108  std::string line_root_ = "line";
109 
110  // Visualization loop
111  while (!viewer_->wasStopped ())
112  {
113  // Lock access to visualizer buffers
114  visualizer_updating_mutex_.lock ();
115 
116  // Updating intermediate point cloud
117  // Remove old point cloud
118  viewer_->removePointCloud ("cloud intermediate v2", v2);
119 
120  // Add the new point cloud
121  viewer_->addPointCloud<PointSource> (cloud_intermediate_.makeShared (), cloud_intermediate_handler_,
122  "cloud intermediate v2", v2);
123 
124  // Updating the correspondece lines
125 
126  std::string line_name_;
127  // Remove the old correspondeces
128  for (std::size_t correspondence_id = 0; correspondence_id < correspondeces_old_size; ++correspondence_id)
129  {
130  // Generate the line name
131  line_name_ = getIndexedName (line_root_, correspondence_id);
132 
133  // Remove the current line according to it's name
134  viewer_->removeShape (line_name_, v2);
135  }
136 
137  // Display the new correspondences lines
138  std::size_t correspondences_new_size = cloud_intermediate_indices_.size ();
139 
140 
141  std::stringstream stream_;
142  stream_ << "Random -> correspondences " << correspondences_new_size;
143  viewer_->removeShape ("correspondences_size", 0);
144  viewer_->addText (stream_.str(), 10, 70, 0.0, 1.0, 0.0, "correspondences_size", v2);
145 
146  // Display entire set of correspondece lines if no maximum displayed correspondences is set
147  if( ( 0 < maximum_displayed_correspondences_ ) &&
148  (maximum_displayed_correspondences_ < correspondences_new_size) )
149  correspondences_new_size = maximum_displayed_correspondences_;
150 
151  // Actualize correspondeces_old_size
152  correspondeces_old_size = correspondences_new_size;
153 
154  // Update new correspondence lines
155  for (std::size_t correspondence_id = 0; correspondence_id < correspondences_new_size; ++correspondence_id)
156  {
157  // Generate random color for current correspondence line
158  double random_red = 255 * rand () / (RAND_MAX + 1.0);
159  double random_green = 255 * rand () / (RAND_MAX + 1.0);
160  double random_blue = 255 * rand () / (RAND_MAX + 1.0);
161 
162  // Generate the name for current line
163  line_name_ = getIndexedName (line_root_, correspondence_id);
164 
165  // Add the new correspondence line.
166  viewer_->addLine (cloud_intermediate_[cloud_intermediate_indices_[correspondence_id]],
167  cloud_target_[cloud_target_indices_[correspondence_id]],
168  random_red, random_green, random_blue,
169  line_name_, v2);
170  }
171 
172  // Unlock access to visualizer buffers
173  visualizer_updating_mutex_.unlock ();
174 
175  // Render visualizer updated buffers
176  viewer_->spinOnce (100);
177  using namespace std::chrono_literals;
178  std::this_thread::sleep_for(100ms);
179  }
180 }
181 
182 //////////////////////////////////////////////////////////////////////////////////////////////
183 template<typename PointSource, typename PointTarget> void
185  const pcl::PointCloud<PointSource> &cloud_src,
186  const std::vector<int> &indices_src,
187  const pcl::PointCloud<PointTarget> &cloud_tgt,
188  const std::vector<int> &indices_tgt)
189 {
190  // Lock local buffers
191  visualizer_updating_mutex_.lock ();
192 
193  // Update source and target point clouds if this is the first callback
194  // Here we are sure that source and target point clouds are initialized
195  if (!first_update_flag_)
196  {
197  first_update_flag_ = true;
198 
199  this->cloud_source_ = cloud_src;
200  this->cloud_target_ = cloud_tgt;
201 
202  this->cloud_intermediate_ = cloud_src;
203  }
204 
205  // Copy the intermediate point cloud and it's associates indices
206  cloud_intermediate_ = cloud_src;
207  cloud_intermediate_indices_ = indices_src;
208 
209  // Copy the intermediate indices associate to the target point cloud
210  cloud_target_indices_ = indices_tgt;
211 
212  // Unlock local buffers
213  visualizer_updating_mutex_.unlock ();
214 }
pcl::RegistrationVisualizer::updateIntermediateCloud
void updateIntermediateCloud(const pcl::PointCloud< PointSource > &cloud_src, const std::vector< int > &indices_src, const pcl::PointCloud< PointTarget > &cloud_tgt, const std::vector< int > &indices_tgt)
Updates visualizer local buffers cloud_intermediate, cloud_intermediate_indices, cloud_target_indices...
Definition: registration_visualizer.hpp:184
pcl::visualization::PointCloudColorHandlerCustom
Handler for predefined user colors.
Definition: point_cloud_color_handlers.h:204
pcl::RegistrationVisualizer::startDisplay
void startDisplay()
Start the viewer thread.
Definition: registration_visualizer.hpp:43
pcl::RegistrationVisualizer::stopDisplay
void stopDisplay()
Stop the viewer thread.
Definition: registration_visualizer.hpp:51
pcl::PointCloud< PointSource >
pcl::visualization::PCLVisualizer::Ptr
shared_ptr< PCLVisualizer > Ptr
Definition: pcl_visualizer.h:89
pcl::RegistrationVisualizer
RegistrationVisualizer represents the base class for rendering the intermediate positions occupied by...
Definition: registration_visualizer.h:57
pcl::visualization::PCLVisualizer
PCL Visualizer main class.
Definition: pcl_visualizer.h:86