Point Cloud Library (PCL)  1.14.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  cloud_source_ (),
65  cloud_target_ (),
66  cloud_intermediate_ ()
67  {}
68 
70  {
71  stopDisplay();
72  }
73 
74  /** \brief Set the registration algorithm whose intermediate steps will be rendered.
75  * The method creates the local callback function pcl::RegistrationVisualizer::update_visualizer_ and
76  * binds it to the local buffers update function pcl::RegistrationVisualizer::updateIntermediateCloud().
77  * The local callback function pcl::RegistrationVisualizer::update_visualizer_ is then linked to
78  * the pcl::Registration::update_visualizer_ callback function.
79  * \param registration represents the registration method whose intermediate steps will be rendered.
80  */
81  bool
83  {
84  // Update the name of the registration method to be displayed
85  registration_method_name_ = registration.getClassName();
86 
87  // Create the local callback function and bind it to the local function responsible for updating
88  // the local buffers
89  update_visualizer_ = [this] (const pcl::PointCloud<PointSource>& cloud_src, const pcl::Indices& indices_src,
90  const pcl::PointCloud<PointTarget>& cloud_tgt, const pcl::Indices& indices_tgt)
91  {
92  updateIntermediateCloud (cloud_src, indices_src, cloud_tgt, indices_tgt);
93  };
94 
95  // Flag that no visualizer update was done. It indicates to visualizer update function to copy
96  // the registration input source and the target point clouds in the next call.
97  visualizer_updating_mutex_.lock ();
98 
99  first_update_flag_ = false;
100 
101  visualizer_updating_mutex_.unlock ();
102 
103  // Register the local callback function to the registration algorithm callback function
104  registration.registerVisualizationCallback (this->update_visualizer_);
105 
106  return true;
107  }
108 
109  /** \brief Start the viewer thread
110  */
111  void
112  startDisplay ();
113 
114  /** \brief Stop the viewer thread
115  */
116  void
117  stopDisplay ();
118 
119  /** \brief Updates visualizer local buffers cloud_intermediate, cloud_intermediate_indices, cloud_target_indices with
120  * the newest registration intermediate results.
121  * \param cloud_src represents the initial source point cloud
122  * \param indices_src represents the indices of the intermediate source points used for the estimation of rigid transformation
123  * \param cloud_tgt represents the target point cloud
124  * \param indices_tgt represents the indices of the target points used for the estimation of rigid transformation
125  */
126  void
127  updateIntermediateCloud (const pcl::PointCloud<PointSource> &cloud_src, const pcl::Indices &indices_src,
128  const pcl::PointCloud<PointTarget> &cloud_tgt, const pcl::Indices &indices_tgt);
129 
130  /** \brief Set maximum number of correspondence lines which will be rendered. */
131  inline void
132  setMaximumDisplayedCorrespondences (const int maximum_displayed_correspondences)
133  {
134  // This method is usually called form other thread than visualizer thread
135  // therefore same visualizer_updating_mutex_ will be used
136 
137  // Lock maximum_displayed_correspondences_
138  visualizer_updating_mutex_.lock ();
139 
140  // Update maximum_displayed_correspondences_
141  maximum_displayed_correspondences_ = maximum_displayed_correspondences;
142 
143  // Unlock maximum_displayed_correspondences_
144  visualizer_updating_mutex_.unlock();
145  }
146 
147  /** \brief Return maximum number of correspondence lines which are rendered. */
148  inline std::size_t
150  {
151  return maximum_displayed_correspondences_;
152  }
153 
154  private:
155  /** \brief Initialize and run the visualization loop. This function will run in the internal thread viewer_thread_ */
156  void
157  runDisplay ();
158 
159  /** \brief Return the string obtained by concatenating a root_name and an id */
160  inline std::string
161  getIndexedName (std::string &root_name, std::size_t &id)
162  {
163  return root_name + std::to_string(id);
164  }
165 
166  /** \brief The registration viewer. */
168 
169  /** \brief The thread running the runDisplay() function. */
170  std::thread viewer_thread_;
171 
172  /** \brief The name of the registration method whose intermediate results are rendered. */
173  std::string registration_method_name_;
174 
175  /** \brief Callback function linked to pcl::Registration::update_visualizer_ */
176  std::function<void
177  (const pcl::PointCloud<PointSource> &cloud_src, const pcl::Indices &indices_src, const pcl::PointCloud<
178  PointTarget> &cloud_tgt, const pcl::Indices &indices_tgt)> update_visualizer_;
179 
180  /** \brief Updates source and target point clouds only for the first update call. */
181  bool first_update_flag_{false};
182 
183  /** \brief The local buffer for source point cloud. */
184  pcl::PointCloud<PointSource> cloud_source_;
185 
186  /** \brief The local buffer for target point cloud. */
187  pcl::PointCloud<PointTarget> cloud_target_;
188 
189  /** \brief The mutex used for the synchronization of updating and rendering of the local buffers. */
190  std::mutex visualizer_updating_mutex_;
191 
192  /** \brief The local buffer for intermediate point cloud obtained during registration process. */
193  pcl::PointCloud<PointSource> cloud_intermediate_;
194 
195  /** \brief The indices of intermediate points used for computation of rigid transformation. */
196  pcl::Indices cloud_intermediate_indices_;
197 
198  /** \brief The indices of target points used for computation of rigid transformation. */
199  pcl::Indices cloud_target_indices_;
200 
201  /** \brief The maximum number of displayed correspondences. */
202  std::size_t maximum_displayed_correspondences_{0};
203 
204  };
205 }
206 
207 #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:485
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:432
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