Point Cloud Library (PCL)  1.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
image_viewer.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2011, Willow Garage, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  */
00037 
00038 #ifndef PCL_VISUALIZATION_IMAGE_VISUALIZER_H__
00039 #define PCL_VISUALIZATION_IMAGE_VISUALIZER_H__
00040 
00041 #include <vtkImageViewer.h>
00042 #include <vtkImageViewer2.h>
00043 #include <vtkInteractorStyle.h>
00044 #include <boost/shared_array.hpp>
00045 #include <pcl/pcl_macros.h>
00046 #include <pcl/console/print.h>
00047 #include <vtkRenderWindow.h>
00048 #include <vtkRenderWindowInteractor.h>
00049 #include <vtkSmartPointer.h>
00050 #include <boost/signals2.hpp>
00051 #include <vtkCallbackCommand.h>
00052 #include <pcl/visualization/interactor_style.h>
00053 
00054 namespace pcl
00055 {
00056   namespace visualization
00057   {
00058     typedef Eigen::Array<unsigned char, 3, 1> Vector3ub;
00059     static const Vector3ub green_color(0,255,0);
00060     static const Vector3ub red_color(255,0,0);
00061     static const Vector3ub blue_color(0,0,255);
00062 
00066     class PCL_EXPORTS ImageViewer
00067     {
00068       public:
00072         ImageViewer (const std::string& window_title = "");
00073 
00075         virtual ~ImageViewer ();
00076        
00082         void 
00083         showMonoImage (const unsigned char* data, unsigned width, unsigned height);
00084 
00090         void 
00091         showRGBImage (const unsigned char* data, unsigned width, unsigned height);
00092 
00096         void 
00097         showRGBImage (const pcl::PointCloud<pcl::PointXYZRGB> &data);
00098 
00102         void 
00103         showRGBImage (const pcl::PointCloud<pcl::PointXYZRGBA> &data);
00104 
00113         void 
00114         showFloatImage (const float* data, unsigned int width, unsigned int height, 
00115                         float min_value = std::numeric_limits<float>::min (), 
00116                         float max_value = std::numeric_limits<float>::max (), bool grayscale = false);
00117         
00126         void
00127         showShortImage (const unsigned short* short_image, unsigned int width, unsigned int height, 
00128                         unsigned short min_value = std::numeric_limits<unsigned short>::min (), 
00129                         unsigned short max_value = std::numeric_limits<unsigned short>::max (), bool grayscale = false);
00130 
00136         void 
00137         showAngleImage (const float* data, unsigned width, unsigned height);
00138 
00144         void 
00145         showHalfAngleImage (const float* data, unsigned width, unsigned height);
00146 
00154         void
00155         markPoint (size_t u, size_t v, Vector3ub fg_color, Vector3ub bg_color = red_color, float radius = 2);
00156 
00160         void
00161         setWindowTitle (const std::string& name)
00162         {
00163           strcpy (image_viewer_->GetWindowName (), name.c_str ());
00164         }
00165 
00167         void 
00168         spin ();
00169         
00175         void 
00176         spinOnce (int time = 1, bool force_redraw = false);
00177         
00183         boost::signals2::connection 
00184         registerKeyboardCallback (void (*callback) (const pcl::visualization::KeyboardEvent&, void*), 
00185                                   void* cookie = NULL)
00186         {
00187           return (registerKeyboardCallback (boost::bind (callback, _1, cookie)));
00188         }
00189         
00196         template<typename T> boost::signals2::connection 
00197         registerKeyboardCallback (void (T::*callback) (const pcl::visualization::KeyboardEvent&, void*), 
00198                                   T& instance, void* cookie = NULL)
00199         {
00200           return (registerKeyboardCallback (boost::bind (callback,  boost::ref (instance), _1, cookie)));
00201         }
00202         
00207         boost::signals2::connection 
00208         registerKeyboardCallback (boost::function<void (const pcl::visualization::KeyboardEvent&)> cb);
00209 
00215         boost::signals2::connection 
00216         registerMouseCallback (void (*callback) (const pcl::visualization::MouseEvent&, void*), 
00217                                void* cookie = NULL)
00218         {
00219           return (registerMouseCallback (boost::bind (callback, _1, cookie)));
00220         }
00221         
00228         template<typename T> boost::signals2::connection 
00229         registerMouseCallback (void (T::*callback) (const pcl::visualization::MouseEvent&, void*), 
00230                                T& instance, void* cookie = NULL)
00231         {
00232           return (registerMouseCallback (boost::bind (callback, boost::ref (instance), _1, cookie)));
00233         }
00234 
00239         boost::signals2::connection 
00240         registerMouseCallback (boost::function<void (const pcl::visualization::MouseEvent&)> cb);
00241         
00246         void
00247         setPosition (int x, int y)
00248         {
00249           image_viewer_->SetPosition (x, y);
00250         }
00251       protected: // methods
00253         void 
00254         resetStoppedFlag () { stopped_ = false; }
00255 
00259         void 
00260         emitMouseEvent (unsigned long event_id);
00261         
00265         void 
00266         emitKeyboardEvent (unsigned long event_id);
00267         
00268         // Callbacks used to register for vtk command
00269         static void 
00270         MouseCallback (vtkObject*, unsigned long eid, void* clientdata, void *calldata);
00271         static void 
00272         KeyboardCallback (vtkObject*, unsigned long eid, void* clientdata, void *calldata);
00273         
00274       protected: // types
00275         struct ExitMainLoopTimerCallback : public vtkCommand
00276         {
00277           static ExitMainLoopTimerCallback* New ()
00278           {
00279             return (new ExitMainLoopTimerCallback);
00280           }
00281           virtual void 
00282           Execute (vtkObject* vtkNotUsed (caller), unsigned long event_id, void* call_data)
00283           {
00284             if (event_id != vtkCommand::TimerEvent)
00285               return;
00286             int timer_id = *(int*)call_data;
00287             if (timer_id != right_timer_id)
00288               return;
00289             window->interactor_->TerminateApp ();
00290           }
00291           int right_timer_id;
00292           ImageViewer* window;
00293         };
00294         struct ExitCallback : public vtkCommand
00295         {
00296           static ExitCallback* New ()
00297           {
00298             return (new ExitCallback);
00299           }
00300           virtual void 
00301           Execute (vtkObject* caller, unsigned long event_id, void* call_data)
00302           {
00303             if (event_id != vtkCommand::ExitEvent)
00304               return;
00305             window->stopped_ = true;
00306             window->interactor_->TerminateApp ();
00307           }
00308           ImageViewer* window;
00309         };
00310 
00311     private:
00312         boost::signals2::signal<void (const pcl::visualization::MouseEvent&)> mouse_signal_;
00313         boost::signals2::signal<void (const pcl::visualization::KeyboardEvent&)> keyboard_signal_;
00314         
00315         vtkSmartPointer<vtkRenderWindowInteractor> interactor_;
00316         vtkCallbackCommand* mouse_command_;
00317         vtkCallbackCommand* keyboard_command_;
00318 
00320         vtkSmartPointer<ExitMainLoopTimerCallback> exit_main_loop_timer_callback_;
00321         vtkSmartPointer<ExitCallback> exit_callback_;
00322 
00324         vtkSmartPointer<vtkImageViewer> image_viewer_;
00325    
00327         boost::shared_array<unsigned char> data_;
00329         size_t data_size_;
00330 
00332         bool stopped_;
00333 
00335         int timer_id_;
00336      };
00337   }
00338 }
00339 
00340 #endif  /* __IMAGE_VISUALIZER_H__ */
00341