range_image_visualization.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 
00003 #include <boost/thread/thread.hpp>
00004 
00005 #include <pcl/common/common_headers.h>
00006 #include <pcl/range_image/range_image.h>
00007 #include <pcl/io/pcd_io.h>
00008 #include <pcl/visualization/range_image_visualizer.h>
00009 #include <pcl/visualization/pcl_visualizer.h>
00010 #include <pcl/console/parse.h>
00011 
00012 typedef pcl::PointXYZ PointType;
00013 
00014 // --------------------
00015 // -----Parameters-----
00016 // --------------------
00017 float angular_resolution_x = 0.5f,
00018       angular_resolution_y = angular_resolution_x;
00019 pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;
00020 bool live_update = false;
00021 
00022 // --------------
00023 // -----Help-----
00024 // --------------
00025 void 
00026 printUsage (const char* progName)
00027 {
00028   std::cout << "\n\nUsage: "<<progName<<" [options] <scene.pcd>\n\n"
00029             << "Options:\n"
00030             << "-------------------------------------------\n"
00031             << "-rx <float>  angular resolution in degrees (default "<<angular_resolution_x<<")\n"
00032             << "-ry <float>  angular resolution in degrees (default "<<angular_resolution_y<<")\n"
00033             << "-c <int>     coordinate frame (default "<< (int)coordinate_frame<<")\n"
00034             << "-l           live update - update the range image according to the selected view in the 3D viewer.\n"
00035             << "-h           this help\n"
00036             << "\n\n";
00037 }
00038 
00039 void 
00040 setViewerPose (pcl::visualization::PCLVisualizer& viewer, const Eigen::Affine3f& viewer_pose)
00041 {
00042   Eigen::Vector3f pos_vector = viewer_pose * Eigen::Vector3f(0, 0, 0);
00043   Eigen::Vector3f look_at_vector = viewer_pose.rotation () * Eigen::Vector3f(0, 0, 1) + pos_vector;
00044   Eigen::Vector3f up_vector = viewer_pose.rotation () * Eigen::Vector3f(0, -1, 0);
00045   viewer.setCameraPosition (pos_vector[0], pos_vector[1], pos_vector[2],
00046                             look_at_vector[0], look_at_vector[1], look_at_vector[2],
00047                             up_vector[0], up_vector[1], up_vector[2]);
00048 }
00049 
00050 // --------------
00051 // -----Main-----
00052 // --------------
00053 int 
00054 main (int argc, char** argv)
00055 {
00056   // --------------------------------------
00057   // -----Parse Command Line Arguments-----
00058   // --------------------------------------
00059   if (pcl::console::find_argument (argc, argv, "-h") >= 0)
00060   {
00061     printUsage (argv[0]);
00062     return 0;
00063   }
00064   if (pcl::console::find_argument (argc, argv, "-l") >= 0)
00065   {
00066     live_update = true;
00067     std::cout << "Live update is on.\n";
00068   }
00069   if (pcl::console::parse (argc, argv, "-rx", angular_resolution_x) >= 0)
00070     std::cout << "Setting angular resolution in x-direction to "<<angular_resolution_x<<"deg.\n";
00071   if (pcl::console::parse (argc, argv, "-ry", angular_resolution_y) >= 0)
00072     std::cout << "Setting angular resolution in y-direction to "<<angular_resolution_y<<"deg.\n";
00073   int tmp_coordinate_frame;
00074   if (pcl::console::parse (argc, argv, "-c", tmp_coordinate_frame) >= 0)
00075   {
00076     coordinate_frame = pcl::RangeImage::CoordinateFrame (tmp_coordinate_frame);
00077     std::cout << "Using coordinate frame "<< (int)coordinate_frame<<".\n";
00078   }
00079   angular_resolution_x = pcl::deg2rad (angular_resolution_x);
00080   angular_resolution_y = pcl::deg2rad (angular_resolution_y);
00081   
00082   // ------------------------------------------------------------------
00083   // -----Read pcd file or create example point cloud if not given-----
00084   // ------------------------------------------------------------------
00085   pcl::PointCloud<PointType>::Ptr point_cloud_ptr (new pcl::PointCloud<PointType>);
00086   pcl::PointCloud<PointType>& point_cloud = *point_cloud_ptr;
00087   Eigen::Affine3f scene_sensor_pose (Eigen::Affine3f::Identity ());
00088   std::vector<int> pcd_filename_indices = pcl::console::parse_file_extension_argument (argc, argv, "pcd");
00089   if (!pcd_filename_indices.empty ())
00090   {
00091     std::string filename = argv[pcd_filename_indices[0]];
00092     if (pcl::io::loadPCDFile (filename, point_cloud) == -1)
00093     {
00094       std::cout << "Was not able to open file \""<<filename<<"\".\n";
00095       printUsage (argv[0]);
00096       return 0;
00097     }
00098     scene_sensor_pose = Eigen::Affine3f (Eigen::Translation3f (point_cloud.sensor_origin_[0],
00099                                                              point_cloud.sensor_origin_[1],
00100                                                              point_cloud.sensor_origin_[2])) *
00101                         Eigen::Affine3f (point_cloud.sensor_orientation_);
00102   }
00103   else
00104   {
00105     std::cout << "\nNo *.pcd file given => Genarating example point cloud.\n\n";
00106     for (float x=-0.5f; x<=0.5f; x+=0.01f)
00107     {
00108       for (float y=-0.5f; y<=0.5f; y+=0.01f)
00109       {
00110         PointType point;  point.x = x;  point.y = y;  point.z = 2.0f - y;
00111         point_cloud.points.push_back (point);
00112       }
00113     }
00114     point_cloud.width = (int) point_cloud.points.size ();  point_cloud.height = 1;
00115   }
00116   
00117   // -----------------------------------------------
00118   // -----Create RangeImage from the PointCloud-----
00119   // -----------------------------------------------
00120   float noise_level = 0.0;
00121   float min_range = 0.0f;
00122   int border_size = 1;
00123   boost::shared_ptr<pcl::RangeImage> range_image_ptr(new pcl::RangeImage);
00124   pcl::RangeImage& range_image = *range_image_ptr;   
00125   range_image.createFromPointCloud (point_cloud, angular_resolution_x, angular_resolution_y,
00126                                     pcl::deg2rad (360.0f), pcl::deg2rad (180.0f),
00127                                     scene_sensor_pose, coordinate_frame, noise_level, min_range, border_size);
00128   
00129   // --------------------------------------------
00130   // -----Open 3D viewer and add point cloud-----
00131   // --------------------------------------------
00132   pcl::visualization::PCLVisualizer viewer ("3D Viewer");
00133   viewer.setBackgroundColor (1, 1, 1);
00134   pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> range_image_color_handler (range_image_ptr, 0, 0, 0);
00135   viewer.addPointCloud (range_image_ptr, range_image_color_handler, "range image");
00136   viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "range image");
00137   //viewer.addCoordinateSystem (1.0f);
00138   //PointCloudColorHandlerCustom<PointType> point_cloud_color_handler (point_cloud_ptr, 150, 150, 150);
00139   //viewer.addPointCloud (point_cloud_ptr, point_cloud_color_handler, "original point cloud");
00140   viewer.initCameraParameters ();
00141   setViewerPose(viewer, range_image.getTransformationToWorldSystem ());
00142   
00143   // --------------------------
00144   // -----Show range image-----
00145   // --------------------------
00146   pcl::visualization::RangeImageVisualizer range_image_widget ("Range image");
00147   range_image_widget.showRangeImage (range_image);
00148   
00149   //--------------------
00150   // -----Main loop-----
00151   //--------------------
00152   while (!viewer.wasStopped ())
00153   {
00154     range_image_widget.spinOnce ();
00155     viewer.spinOnce ();
00156     pcl_sleep (0.01);
00157     
00158     if (live_update)
00159     {
00160       scene_sensor_pose = viewer.getViewerPose();
00161       range_image.createFromPointCloud (point_cloud, angular_resolution_x, angular_resolution_y,
00162                                         pcl::deg2rad (360.0f), pcl::deg2rad (180.0f),
00163                                         scene_sensor_pose, pcl::RangeImage::LASER_FRAME, noise_level, min_range, border_size);
00164       range_image_widget.showRangeImage (range_image);
00165     }
00166   }
00167 }


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:32:01