openni_viewer.cpp
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: openni_viewer.cpp 4360 2012-02-10 01:01:11Z rusu $
00035  *
00036  */
00037 
00038 #include <boost/thread/thread.hpp>
00039 #define MEASURE_FUNCTION_TIME
00040 #include <pcl/common/time.h> //fps calculations
00041 #include <pcl/io/openni_grabber.h>
00042 #include <pcl/visualization/point_cloud_handlers.h>
00043 #include <pcl/visualization/pcl_visualizer.h>
00044 #include <pcl/visualization/image_viewer.h>
00045 #include <pcl/console/print.h>
00046 #include <pcl/console/parse.h>
00047 #include <pcl/console/time.h>
00048 
00049 #define SHOW_FPS 1
00050 #if SHOW_FPS
00051 #define FPS_CALC(_WHAT_) \
00052 do \
00053 { \
00054     static unsigned count = 0;\
00055     static double last = pcl::getTime ();\
00056     double now = pcl::getTime (); \
00057     ++count; \
00058     if (now - last >= 1.0) \
00059     { \
00060       std::cout << "Average framerate("<< _WHAT_ << "): " << double(count)/double(now - last) << " Hz" <<  std::endl; \
00061       count = 0; \
00062       last = now; \
00063     } \
00064 }while(false)
00065 #else
00066 #define FPS_CALC(_WHAT_) \
00067 do \
00068 { \
00069 }while(false)
00070 #endif
00071 
00072 boost::mutex cld_mutex, img_mutex;
00073 pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr g_cloud;
00074 boost::shared_ptr<openni_wrapper::Image> g_image;
00075 
00076 void
00077 printHelp (int argc, char **argv)
00078 {
00079   using pcl::console::print_error;
00080   using pcl::console::print_info;
00081   print_error ("Syntax is: %s <options>\n", argv[0]);
00082   print_info ("  where options are:\n");
00083   print_info ("                     -dev device_id           = device to be used\n");
00084   print_info ("                                                maybe \"#n\", with n being the number of the device in device list.\n");
00085   print_info ("                                                maybe \"bus@addr\", with bus and addr being the usb bus and address where device is connected.\n");
00086   print_info ("                                                maybe \"serial\", with serial being the serial number of the device.\n");
00087   print_info ("\n");
00088 }
00089 
00090 // Create the PCLVisualizer object
00091 boost::shared_ptr<pcl::visualization::PCLVisualizer> cld;
00092 #if !((VTK_MAJOR_VERSION == 5)&&(VTK_MINOR_VERSION <= 4))
00093 boost::shared_ptr<pcl::visualization::ImageViewer> img;
00094 #endif
00095 
00096 struct EventHelper
00097 {
00098   void 
00099   cloud_cb (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr & cloud)
00100   {
00101     FPS_CALC ("callback");
00102     cld_mutex.lock ();
00103     g_cloud = cloud;
00104     cld_mutex.unlock ();
00105   }
00106 
00107 #if !((VTK_MAJOR_VERSION == 5)&&(VTK_MINOR_VERSION <= 4))
00108   void
00109   image_callback (const boost::shared_ptr<openni_wrapper::Image>& image)
00110   {
00111     FPS_CALC ("image callback");
00112     img_mutex.lock ();
00113     g_image = image;
00114     img_mutex.unlock ();
00115   }
00116 #endif  
00117 };
00118 // Simple callbacks.
00119 void 
00120 keyboard_callback (const pcl::visualization::KeyboardEvent& event, void* cookie)
00121 {
00122   std::string* message = (std::string*)cookie;
00123   cout << (*message) << " :: ";
00124   if (event.getKeyCode())
00125     cout << "the key \'" << event.getKeyCode() << "\' (" << (int)event.getKeyCode() << ") was";
00126   else
00127     cout << "the special key \'" << event.getKeySym() << "\' was";
00128   if (event.keyDown())
00129     cout << " pressed" << endl;
00130   else
00131     cout << " released" << endl;
00132 }
00133 
00134 void 
00135 mouse_callback (const pcl::visualization::MouseEvent& mouse_event, void* cookie)
00136 {
00137   std::string* message = (std::string*) cookie;
00138   if (mouse_event.getType() == pcl::visualization::MouseEvent::MouseButtonPress && mouse_event.getButton() == pcl::visualization::MouseEvent::LeftButton)
00139   {
00140     cout << (*message) << " :: " << mouse_event.getX () << " , " << mouse_event.getY () << endl;
00141   }
00142 }
00143 
00144 /* ---[ */
00145 int
00146 main (int argc, char** argv)
00147 {
00148   if (argc > 1)
00149   {
00150     for (int i = 1; i < argc; i++)
00151     {
00152       if (std::string (argv[i]) == "-h")
00153       {
00154         printHelp (argc, argv);
00155         return (-1);
00156       }
00157     }
00158   }
00159 
00160   EventHelper event_helper;
00161   std::string device_id = "";
00162   pcl::console::parse_argument (argc, argv, "-dev", device_id);
00163 
00164   pcl::Grabber* interface = new pcl::OpenNIGrabber (device_id);
00165 
00166   cld.reset (new pcl::visualization::PCLVisualizer (argc, argv, "OpenNI Viewer"));
00167 
00168   std::string mouseMsg3D ("Mouse coordinates in PCL Visualizer");
00169   std::string keyMsg3D ("Key event for PCL Visualizer");
00170   cld->registerMouseCallback (&mouse_callback, (void*)(&mouseMsg3D));    
00171   cld->registerKeyboardCallback(&keyboard_callback, (void*)(&keyMsg3D));
00172   boost::function<void(const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&) > f = boost::bind (&EventHelper::cloud_cb, &event_helper, _1);
00173   boost::signals2::connection c1 = interface->registerCallback (f);
00174 
00175 #if !((VTK_MAJOR_VERSION == 5)&&(VTK_MINOR_VERSION <= 4))
00176   img.reset (new pcl::visualization::ImageViewer ("OpenNI Viewer"));
00177   // Register callbacks
00178   std::string keyMsg2D ("Key event for image viewer");
00179   std::string mouseMsg2D ("Mouse coordinates in image viewer");
00180   img->registerMouseCallback (&mouse_callback, (void*)(&mouseMsg2D));
00181   img->registerKeyboardCallback(&keyboard_callback, (void*)(&keyMsg2D));
00182   boost::function<void (const boost::shared_ptr<openni_wrapper::Image>&) > image_cb = boost::bind (&EventHelper::image_callback, &event_helper, _1);
00183   boost::signals2::connection image_connection = interface->registerCallback (image_cb);
00184   unsigned char* rgb_data = 0;
00185   unsigned rgb_data_size = 0;
00186 #endif 
00187   
00188   interface->start ();
00189   bool cld_init = false;
00190   // Loop
00191   while (!cld->wasStopped ())
00192   {
00193     // Render and process events in the two interactors
00194     cld->spinOnce ();
00195 #if !((VTK_MAJOR_VERSION == 5)&&(VTK_MINOR_VERSION <= 4))
00196     img->spinOnce ();
00197 #endif
00198     FPS_CALC ("drawing");
00199 
00200     // Add the cloud
00201     if (g_cloud && cld_mutex.try_lock ())
00202     {
00203       if (!cld_init)
00204       {
00205         cld->getRenderWindow ()->SetSize (g_cloud->width, g_cloud->height);
00206         cld->getRenderWindow ()->SetPosition (g_cloud->width, 0);
00207         cld_init = !cld_init;
00208       }
00209 
00210       pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGBA> handler (g_cloud);
00211       if (!cld->updatePointCloud (g_cloud, handler, "OpenNICloud"))
00212       {
00213         cld->addPointCloud (g_cloud, handler, "OpenNICloud");
00214         cld->resetCameraViewpoint ("OpenNICloud");
00215       }
00216       cld_mutex.unlock ();
00217     }
00218     
00219 #if !((VTK_MAJOR_VERSION == 5)&&(VTK_MINOR_VERSION <= 4))
00220     // Add the image
00221     if (g_image && img_mutex.try_lock ())
00222     {
00223       if (g_image->getEncoding() == openni_wrapper::Image::RGB)
00224         img->showRGBImage (g_image->getMetaData ().Data (), 
00225                            g_image->getWidth (), g_image->getHeight ());
00226       else
00227       {
00228         if (rgb_data_size < g_image->getWidth () * g_image->getHeight ())
00229         {
00230           rgb_data_size = g_image->getWidth () * g_image->getHeight ();
00231           rgb_data = new unsigned char [rgb_data_size * 3];
00232         }
00233         g_image->fillRGB (g_image->getWidth (), g_image->getHeight (), rgb_data);
00234         img->showRGBImage (rgb_data, g_image->getWidth (), g_image->getHeight ());
00235       }
00236       img_mutex.unlock ();
00237     }
00238 #endif
00239     boost::this_thread::sleep (boost::posix_time::microseconds (100));
00240   }
00241 
00242   interface->stop ();
00243 }
00244 /* ]--- */


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:16:07