image_grabber_viewer.cpp
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) 2011, Willow Garage, Inc.
00006  *  Copyright (c) 2012-, Open Perception, Inc.
00007  *  
00008  *  All rights reserved.
00009  *
00010  *  Redistribution and use in source and binary forms, with or without
00011  *  modification, are permitted provided that the following conditions
00012  *  are met:
00013  *
00014  *   * Redistributions of source code must retain the above copyright
00015  *     notice, this list of conditions and the following disclaimer.
00016  *   * Redistributions in binary form must reproduce the above
00017  *     copyright notice, this list of conditions and the following
00018  *     disclaimer in the documentation and/or other materials provided
00019  *     with the distribution.
00020  *   * Neither the name of the copyright holder(s) nor the names of its
00021  *     contributors may be used to endorse or promote products derived
00022  *     from this software without specific prior written permission.
00023  *
00024  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00027  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00028  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00029  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00030  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00031  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00034  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  *  POSSIBILITY OF SUCH DAMAGE.
00036  *
00037  *
00038  */
00039 #include <pcl/io/image_grabber.h>
00040 #include <pcl/console/parse.h>
00041 #include <pcl/console/print.h>
00042 #include <pcl/visualization/boost.h>
00043 #include <pcl/visualization/cloud_viewer.h>
00044 #include <pcl/visualization/image_viewer.h>
00045 #include <pcl/io/pcd_io.h>
00046 
00047 #if (PCL_LINEAR_VERSION(VTK_MAJOR_VERSION,VTK_MINOR_VERSION,0)<=PCL_LINEAR_VERSION(5,4,0))
00048   #define DISPLAY_IMAGE
00049 #endif
00050 
00051 using pcl::console::print_error;
00052 using pcl::console::print_info;
00053 using pcl::console::print_value;
00054 
00055 boost::mutex mutex_;
00056 boost::shared_ptr<pcl::ImageGrabber<pcl::PointXYZRGBA> > grabber;
00057 pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr cloud_;
00058 
00059 void
00060 printHelp (int, char **argv)
00061 {
00062   //print_error ("Syntax is: %s <file_name 1..N>.tiff <options>\n", argv[0]);
00063   print_error ("Syntax is: %s <options>\n", argv[0]);
00064   print_info ("  where options are:\n");
00065   print_info ("\t-dir directory_path      = directory path to image or pclzf file(s) to be read from\n");
00066   print_info ("\t-fps frequency           = frames per second\n");
00067   print_info ("\t-pclzf                   = Load pclzf files instead\n");
00068   print_info ("\t-repeat                  = optional parameter that tells wheter the TIFF file(s) should be \"grabbed\" in a endless loop.\n");
00069   print_info ("\n");
00070   print_info ("\t-cam (*)                 = use given camera settings as initial view\n");
00071   print_info (stderr, " (*) [Clipping Range / Focal Point / Position / ViewUp / Distance / Window Size / Window Pos] or use a <filename.cam> that contains the same information.\n");
00072 
00073   print_info ("Additional options:\n");
00074   print_info ("\t-bc col1 col2 col3       = background color\n");
00075   print_info ("\t-ax                      = use custom coordinate system\n");
00076   print_info ("\t-ax_pos pos1 pos2 pos3   = the axes coordinates\n");
00077   print_info ("\t-focal focal_length      = ImageGrabber focal length\n");
00078   // Following ones are not implemented
00079 //  print_info ("\t-fc col1 col2 col3       = foreground color\n");
00080 //  print_info ("\t-pc                      = point size\n");
00081 //  print_info ("\t-opaque                  = point opacity\n");
00082 }
00083 
00084 // Create the PCLVisualizer object
00085 boost::shared_ptr<pcl::visualization::PCLVisualizer> cloud_viewer;
00086 #ifdef DISPLAY_IMAGE
00087 boost::shared_ptr<pcl::visualization::ImageViewer> img_viewer;
00088 #endif
00089 
00090 std::vector<double> fcolor_r, fcolor_b, fcolor_g;
00091 bool fcolorparam = false;
00092 
00093 struct EventHelper
00094 {
00095   void 
00096   cloud_cb (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr & cloud)
00097   {
00098     pcl::uint64_t timestamp;
00099     timestamp = cloud->header.stamp;
00100     if (timestamp > 0)
00101       PCL_INFO ("Acquired cloud with timestamp of %lu\n", timestamp);
00102     if (mutex_.try_lock ())
00103     {
00104       cloud_ = cloud;
00105       mutex_.unlock ();
00106     }
00107   }
00108 };
00109 
00110 void 
00111 keyboard_callback (const pcl::visualization::KeyboardEvent& event, void*)
00112 {
00114   if (event.getKeyCode() == ' ' && grabber)
00115     grabber->trigger ();
00116 }
00117 
00118 void 
00119 mouse_callback (const pcl::visualization::MouseEvent& mouse_event, void* cookie)
00120 {
00121   std::string* message = static_cast<std::string*> (cookie);
00122   if (mouse_event.getType() == pcl::visualization::MouseEvent::MouseButtonPress && mouse_event.getButton() == pcl::visualization::MouseEvent::LeftButton)
00123   {
00124     cout << (*message) << " :: " << mouse_event.getX () << " , " << mouse_event.getY () << endl;
00125   }
00126 }
00127 
00128 /* ---[ */
00129 int
00130 main (int argc, char** argv)
00131 {
00132   srand (unsigned (time (0)));
00133 
00134   if (argc > 1)
00135   {
00136     for (int i = 1; i < argc; i++)
00137     {
00138       if (std::string (argv[i]) == "-h")
00139       {
00140         printHelp (argc, argv);
00141         return (-1);
00142       }
00143     }
00144   }
00145 
00146   // Command line parsing
00147   double bcolor[3] = {0, 0, 0};
00148   pcl::console::parse_3x_arguments (argc, argv, "-bc", bcolor[0], bcolor[1], bcolor[2]);
00149 
00150   fcolorparam = pcl::console::parse_multiple_3x_arguments (argc, argv, "-fc", fcolor_r, fcolor_g, fcolor_b);
00151 
00152   int psize = 0;
00153   pcl::console::parse_argument (argc, argv, "-ps", psize);
00154 
00155   double opaque;
00156   pcl::console::parse_argument (argc, argv, "-opaque", opaque);
00157 
00158   cloud_viewer.reset (new pcl::visualization::PCLVisualizer (argc, argv, "PCD viewer"));
00159 
00160 #ifdef DISPLAY_IMAGE
00161   img_viewer.reset (new pcl::visualization::ImageViewer ("OpenNI Viewer"));
00162 #endif
00163 
00164   //  // Change the cloud rendered point size
00165   //  if (psize > 0)
00166   //    p->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, psize, "OpenNICloud");
00167   //
00168   //  // Change the cloud rendered opacity
00169   //  if (opaque >= 0)
00170   //    p->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_OPACITY, opaque, "OpenNICloud");
00171 
00172   cloud_viewer->setBackgroundColor (bcolor[0], bcolor[1], bcolor[2]);
00173 
00174   // Read axes settings
00175   double axes = 0.0;
00176   pcl::console::parse_argument (argc, argv, "-ax", axes);
00177   if (axes != 0.0 && cloud_viewer)
00178   {
00179     float ax_x = 0.0, ax_y = 0.0, ax_z = 0.0;
00180     pcl::console::parse_3x_arguments (argc, argv, "-ax_pos", ax_x, ax_y, ax_z, false);
00181     // Draw XYZ axes if command-line enabled
00182     cloud_viewer->addCoordinateSystem (axes, ax_x, ax_y, ax_z);
00183   }
00184 
00185   float frames_per_second = 0; // 0 means only if triggered!
00186   pcl::console::parse (argc, argv, "-fps", frames_per_second);
00187   if (frames_per_second < 0)
00188     frames_per_second = 0.0;
00189 
00190 
00191 
00192   bool repeat = (pcl::console::find_argument (argc, argv, "-repeat") != -1);
00193 
00194   bool use_pclzf = (pcl::console::find_argument (argc, argv, "-pclzf") != -1);
00195 
00196   std::cout << "fps: " << frames_per_second << " , repeat: " << repeat << std::endl;
00197   std::string path = "";
00198   pcl::console::parse_argument (argc, argv, "-dir", path);
00199   std::cout << "path: " << path << std::endl;
00200   if (path != "" && boost::filesystem::exists (path))
00201   {
00202     grabber.reset (new pcl::ImageGrabber<pcl::PointXYZRGBA> (path, frames_per_second, repeat, use_pclzf));
00203   }
00204   else
00205   {
00206     std::cout << "No directory was given with the -dir flag." << std::endl;
00207     printHelp (argc, argv);
00208     return (-1);
00209   }
00210   grabber->setDepthImageUnits (float (1E-3));
00211 
00212   // Before manually setting
00213   double fx, fy, cx, cy;
00214   grabber->getCameraIntrinsics (fx, fy, cx, cy);
00215   PCL_INFO ("Factory default intrinsics: %f, %f, %f, %f\n", 
00216       fx, fy, cx, cy);
00217   float focal_length;
00218   if (pcl::console::parse (argc, argv, "-focal", focal_length) != -1)
00219     grabber->setCameraIntrinsics (focal_length, focal_length, 320, 240);
00220   
00221   
00222 
00223   EventHelper h;
00224   boost::function<void(const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&) > f = boost::bind (&EventHelper::cloud_cb, &h, _1);
00225   boost::signals2::connection c1 = grabber->registerCallback (f);
00226 
00227   std::string mouse_msg_3D ("Mouse coordinates in PCL Visualizer");
00228   std::string key_msg_3D ("Key event for PCL Visualizer");
00229 
00230   cloud_viewer->registerMouseCallback (&mouse_callback, static_cast<void*> (&mouse_msg_3D));
00231   cloud_viewer->registerKeyboardCallback(&keyboard_callback, static_cast<void*> (&key_msg_3D));
00232 
00233   std::string mouse_msg_2D ("Mouse coordinates in image viewer");
00234   std::string key_msg_2D ("Key event for image viewer");
00235 
00236 #ifdef DISPLAY_IMAGE
00237   img_viewer->registerMouseCallback (&mouse_callback, static_cast<void*> (&mouse_msg_2D));
00238   img_viewer->registerKeyboardCallback(&keyboard_callback, static_cast<void*> (&key_msg_2D));
00239 #endif
00240 
00241   grabber->start ();
00242 
00243   grabber->getCameraIntrinsics (fx, fy, cx, cy);
00244   PCL_INFO ("Grabber is using intrinsics: %f, %f, %f, %f\n", 
00245       fx, fy, cx, cy);
00246 
00247   while (!cloud_viewer->wasStopped ())
00248   {
00249     cloud_viewer->spinOnce ();
00250 
00251 #ifdef DISPLAY_IMAGE
00252     img_viewer->spinOnce ();
00253 #endif
00254 
00255     if (!cloud_)
00256     {
00257       boost::this_thread::sleep(boost::posix_time::microseconds(10000));
00258       continue;
00259     }
00260     else if (mutex_.try_lock ())
00261     {
00262       pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr temp_cloud;
00263       temp_cloud.swap (cloud_);
00264       mutex_.unlock ();
00265 
00266 #ifdef DISPLAY_IMAGE
00267       img_viewer->showRGBImage (*temp_cloud);
00268 #endif
00269 
00270       if (!cloud_viewer->updatePointCloud (temp_cloud, "PCDCloud"))
00271       {
00272         cloud_viewer->addPointCloud (temp_cloud, "PCDCloud");
00273         cloud_viewer->setCameraPosition (0, 0, 0, 0, 0, 1, 0, -1, 0);
00274       }
00275     }
00276   }
00277 
00278   grabber->stop ();
00279 }
00280 /* ]--- */


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:24:57