Point Cloud Library (PCL)  1.3.1
registration_visualizer.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * $Id: registration_visualizer.h 2011-07-23 01:06:01Z georgeLisca $
00035  *
00036  */
00037 
00038 #ifndef PCL_REGISTRATION_VISUALIZER_H_
00039 #define PCL_REGISTRATION_VISUALIZER_H_
00040 
00041 // boost
00042 #include <boost/thread.hpp>
00043 #include <boost/function.hpp>
00044 #include <boost/bind.hpp>
00045 
00046 // PCL
00047 #include <pcl/registration/registration.h>
00048 #include <pcl/visualization/pcl_visualizer.h>
00049 
00050 namespace pcl
00051 {
00059   template<typename PointSource, typename PointTarget>
00060     class RegistrationVisualizer
00061     {
00062 
00063     public:
00065       RegistrationVisualizer () : maximum_displayed_correspondences_ (0)
00066       {  }
00067 
00075       bool
00076       setRegistration (pcl::Registration<PointSource, PointTarget> &registration)
00077       {
00078         // Update the name of the registration method to be desplayed
00079         registration_method_name_ = registration.getClassName();
00080 
00081         // Create the local callback function and bind it to the local function resposable for updating
00082         // the local buffers
00083         update_visualizer_ = boost::bind (&RegistrationVisualizer<PointSource, PointTarget>::updateIntermediateCloud,
00084                                           this, _1, _2, _3, _4);
00085 
00086         // Register the local callback function to the registration algorithm callback function
00087         registration.registerVisualizationCallback (this->update_visualizer_);
00088 
00089         // Flag that no visualizer update was done. It indicates to visualizer update function to copy
00090         // the registration input source and the target point clouds in the next call.
00091         visualizer_updating_mutex_.lock ();
00092 
00093         first_update_flag_ = false;
00094 
00095         visualizer_updating_mutex_.unlock ();
00096 
00097         return true;
00098       }
00099 
00102       void
00103       startDisplay ();
00104 
00107       void
00108       stopDisplay ();
00109 
00117       void
00118       updateIntermediateCloud (const pcl::PointCloud<PointSource> &cloud_src, const std::vector<int> &indices_src,
00119                                const pcl::PointCloud<PointTarget> &cloud_tgt, const std::vector<int> &indices_tgt);
00120 
00122       inline void
00123       setMaximumDisplayedCorrespondences (const int maximum_displayed_correspondences)
00124       {
00125         // This method is usualy called form other thread than visualizer thread
00126         // therefore same visualizer_updating_mutex_ will be used
00127 
00128         // Lock maximum_displayed_correspondences_
00129         visualizer_updating_mutex_.lock ();
00130 
00131         // Update maximum_displayed_correspondences_
00132         maximum_displayed_correspondences_ = maximum_displayed_correspondences;
00133 
00134         // Unlock maximum_displayed_correspondences_
00135         visualizer_updating_mutex_.unlock();
00136       }
00137 
00139       inline size_t
00140       getMaximumDisplayedCorrespondences()
00141       {
00142         return maximum_displayed_correspondences_;
00143       }
00144 
00145     private:
00147       void
00148       runDisplay ();
00149 
00151       inline std::string
00152       getIndexedName (std::string &root_name, size_t &id)
00153       {
00154         std::stringstream id_stream_;
00155         id_stream_ << id;
00156         std::string indexed_name_ = root_name + id_stream_.str ();
00157         return indexed_name_;
00158       }
00159 
00161       boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer_;
00162 
00164       boost::thread viewer_thread_;
00165 
00167       std::string registration_method_name_;
00168 
00170       boost::function<void
00171       (const pcl::PointCloud<PointSource> &cloud_src, const std::vector<int> &indices_src, const pcl::PointCloud<
00172           PointTarget> &cloud_tgt, const std::vector<int> &indices_tgt)> update_visualizer_;
00173 
00175       bool first_update_flag_;
00176 
00178       pcl::PointCloud<PointSource> cloud_source_;
00179 
00181       pcl::PointCloud<PointTarget> cloud_target_;
00182 
00184       boost::mutex visualizer_updating_mutex_;
00185 
00187       pcl::PointCloud<PointSource> cloud_intermediate_;
00188 
00190       std::vector<int> cloud_intermediate_indices_;
00191 
00193       std::vector<int> cloud_target_indices_;
00194 
00196       size_t maximum_displayed_correspondences_;
00197 
00198     };
00199 }
00200 
00201 #include "pcl/visualization/impl/registration_visualizer.hpp"
00202 
00203 #endif  //#ifndef PCL_REGISTRATION_VISUALIZER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines