Point Cloud Library (PCL)  1.13.1-dev
registration_visualizer.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Id$
35  *
36  */
37 
38 #pragma once
39 
40 // PCL
41 #include <pcl/registration/registration.h>
42 #include <pcl/visualization/pcl_visualizer.h>
43 
44 #include <mutex>
45 #include <thread>
46 
47 namespace pcl
48 {
49  /** \brief @b RegistrationVisualizer represents the base class for rendering
50  * the intermediate positions occupied by the source point cloud during it's registration
51  * to the target point cloud. A registration algorithm is considered as input and
52  * it's convergence is rendered.
53  * \author Gheorghe Lisca
54  * \ingroup visualization
55  */
56  template<typename PointSource, typename PointTarget, typename Scalar = float>
58  {
59 
60  public:
61  /** \brief Empty constructor. */
63  update_visualizer_ (),
64  first_update_flag_ (false),
65  cloud_source_ (),
66  cloud_target_ (),
67  cloud_intermediate_ (),
68  maximum_displayed_correspondences_ (0)
69  {}
70 
72  {
73  stopDisplay();
74  }
75 
76  /** \brief Set the registration algorithm whose intermediate steps will be rendered.
77  * The method creates the local callback function pcl::RegistrationVisualizer::update_visualizer_ and
78  * binds it to the local buffers update function pcl::RegistrationVisualizer::updateIntermediateCloud().
79  * The local callback function pcl::RegistrationVisualizer::update_visualizer_ is then linked to
80  * the pcl::Registration::update_visualizer_ callback function.
81  * \param registration represents the registration method whose intermediate steps will be rendered.
82  */
83  bool
85  {
86  // Update the name of the registration method to be displayed
87  registration_method_name_ = registration.getClassName();
88 
89  // Create the local callback function and bind it to the local function responsible for updating
90  // the local buffers
91  update_visualizer_ = [this] (const pcl::PointCloud<PointSource>& cloud_src, const pcl::Indices& indices_src,
92  const pcl::PointCloud<PointTarget>& cloud_tgt, const pcl::Indices& indices_tgt)
93  {
94  updateIntermediateCloud (cloud_src, indices_src, cloud_tgt, indices_tgt);
95  };
96 
97  // Flag that no visualizer update was done. It indicates to visualizer update function to copy
98  // the registration input source and the target point clouds in the next call.
99  visualizer_updating_mutex_.lock ();
100 
101  first_update_flag_ = false;
102 
103  visualizer_updating_mutex_.unlock ();
104 
105  // Register the local callback function to the registration algorithm callback function
106  registration.registerVisualizationCallback (this->update_visualizer_);
107 
108  return true;
109  }
110 
111  /** \brief Start the viewer thread
112  */
113  void
114  startDisplay ();
115 
116  /** \brief Stop the viewer thread
117  */
118  void
119  stopDisplay ();
120 
121  /** \brief Updates visualizer local buffers cloud_intermediate, cloud_intermediate_indices, cloud_target_indices with
122  * the newest registration intermediate results.
123  * \param cloud_src represents the initial source point cloud
124  * \param indices_src represents the indices of the intermediate source points used for the estimation of rigid transformation
125  * \param cloud_tgt represents the target point cloud
126  * \param indices_tgt represents the indices of the target points used for the estimation of rigid transformation
127  */
128  void
129  updateIntermediateCloud (const pcl::PointCloud<PointSource> &cloud_src, const pcl::Indices &indices_src,
130  const pcl::PointCloud<PointTarget> &cloud_tgt, const pcl::Indices &indices_tgt);
131 
132  /** \brief Set maximum number of correspondence lines which will be rendered. */
133  inline void
134  setMaximumDisplayedCorrespondences (const int maximum_displayed_correspondences)
135  {
136  // This method is usually called form other thread than visualizer thread
137  // therefore same visualizer_updating_mutex_ will be used
138 
139  // Lock maximum_displayed_correspondences_
140  visualizer_updating_mutex_.lock ();
141 
142  // Update maximum_displayed_correspondences_
143  maximum_displayed_correspondences_ = maximum_displayed_correspondences;
144 
145  // Unlock maximum_displayed_correspondences_
146  visualizer_updating_mutex_.unlock();
147  }
148 
149  /** \brief Return maximum number of correspondence lines which are rendered. */
150  inline std::size_t
152  {
153  return maximum_displayed_correspondences_;
154  }
155 
156  private:
157  /** \brief Initialize and run the visualization loop. This function will run in the internal thread viewer_thread_ */
158  void
159  runDisplay ();
160 
161  /** \brief Return the string obtained by concatenating a root_name and an id */
162  inline std::string
163  getIndexedName (std::string &root_name, std::size_t &id)
164  {
165  return root_name + std::to_string(id);
166  }
167 
168  /** \brief The registration viewer. */
170 
171  /** \brief The thread running the runDisplay() function. */
172  std::thread viewer_thread_;
173 
174  /** \brief The name of the registration method whose intermediate results are rendered. */
175  std::string registration_method_name_;
176 
177  /** \brief Callback function linked to pcl::Registration::update_visualizer_ */
178  std::function<void
179  (const pcl::PointCloud<PointSource> &cloud_src, const pcl::Indices &indices_src, const pcl::PointCloud<
180  PointTarget> &cloud_tgt, const pcl::Indices &indices_tgt)> update_visualizer_;
181 
182  /** \brief Updates source and target point clouds only for the first update call. */
183  bool first_update_flag_;
184 
185  /** \brief The local buffer for source point cloud. */
186  pcl::PointCloud<PointSource> cloud_source_;
187 
188  /** \brief The local buffer for target point cloud. */
189  pcl::PointCloud<PointTarget> cloud_target_;
190 
191  /** \brief The mutex used for the synchronization of updating and rendering of the local buffers. */
192  std::mutex visualizer_updating_mutex_;
193 
194  /** \brief The local buffer for intermediate point cloud obtained during registration process. */
195  pcl::PointCloud<PointSource> cloud_intermediate_;
196 
197  /** \brief The indices of intermediate points used for computation of rigid transformation. */
198  pcl::Indices cloud_intermediate_indices_;
199 
200  /** \brief The indices of target points used for computation of rigid transformation. */
201  pcl::Indices cloud_target_indices_;
202 
203  /** \brief The maximum number of displayed correspondences. */
204  std::size_t maximum_displayed_correspondences_;
205 
206  };
207 }
208 
209 #include <pcl/visualization/impl/registration_visualizer.hpp>
Registration represents the base registration class for general purpose, ICP-like methods.
Definition: registration.h:57
const std::string & getClassName() const
Abstract class get name method.
Definition: registration.h:497
bool registerVisualizationCallback(std::function< UpdateVisualizerCallbackSignature > &visualizerCallback)
Register the user callback function which will be called from registration thread in order to update ...
Definition: registration.h:444
RegistrationVisualizer represents the base class for rendering the intermediate positions occupied by...
void setMaximumDisplayedCorrespondences(const int maximum_displayed_correspondences)
Set maximum number of correspondence lines which will be rendered.
RegistrationVisualizer()
Empty constructor.
void stopDisplay()
Stop the viewer thread.
bool setRegistration(pcl::Registration< PointSource, PointTarget, Scalar > &registration)
Set the registration algorithm whose intermediate steps will be rendered.
std::size_t getMaximumDisplayedCorrespondences()
Return maximum number of correspondence lines which are rendered.
void startDisplay()
Start the viewer thread.
void updateIntermediateCloud(const pcl::PointCloud< PointSource > &cloud_src, const pcl::Indices &indices_src, const pcl::PointCloud< PointTarget > &cloud_tgt, const pcl::Indices &indices_tgt)
Updates visualizer local buffers cloud_intermediate, cloud_intermediate_indices, cloud_target_indices...
shared_ptr< PCLVisualizer > Ptr
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133