range_image_visualization.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 
00003 #include <boost/thread/thread.hpp>
00004 #include <pcl/common/common_headers.h>
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.camera_.pos[0] = pos_vector[0];
00046   viewer.camera_.pos[1] = pos_vector[1];
00047   viewer.camera_.pos[2] = pos_vector[2];
00048   viewer.camera_.focal[0] = look_at_vector[0];
00049   viewer.camera_.focal[1] = look_at_vector[1];
00050   viewer.camera_.focal[2] = look_at_vector[2];
00051   viewer.camera_.view[0] = up_vector[0];
00052   viewer.camera_.view[1] = up_vector[1];
00053   viewer.camera_.view[2] = up_vector[2];
00054   viewer.updateCamera();
00055 }
00056 
00057 // --------------
00058 // -----Main-----
00059 // --------------
00060 int 
00061 main (int argc, char** argv)
00062 {
00063   // --------------------------------------
00064   // -----Parse Command Line Arguments-----
00065   // --------------------------------------
00066   if (pcl::console::find_argument (argc, argv, "-h") >= 0)
00067   {
00068     printUsage (argv[0]);
00069     return 0;
00070   }
00071   if (pcl::console::find_argument (argc, argv, "-l") >= 0)
00072   {
00073     live_update = true;
00074     std::cout << "Live update is on.\n";
00075   }
00076   if (pcl::console::parse (argc, argv, "-rx", angular_resolution_x) >= 0)
00077     std::cout << "Setting angular resolution in x-direction to "<<angular_resolution_x<<"deg.\n";
00078   if (pcl::console::parse (argc, argv, "-ry", angular_resolution_y) >= 0)
00079     std::cout << "Setting angular resolution in y-direction to "<<angular_resolution_y<<"deg.\n";
00080   int tmp_coordinate_frame;
00081   if (pcl::console::parse (argc, argv, "-c", tmp_coordinate_frame) >= 0)
00082   {
00083     coordinate_frame = pcl::RangeImage::CoordinateFrame (tmp_coordinate_frame);
00084     std::cout << "Using coordinate frame "<< (int)coordinate_frame<<".\n";
00085   }
00086   angular_resolution_x = pcl::deg2rad (angular_resolution_x);
00087   angular_resolution_y = pcl::deg2rad (angular_resolution_y);
00088   
00089   // ------------------------------------------------------------------
00090   // -----Read pcd file or create example point cloud if not given-----
00091   // ------------------------------------------------------------------
00092   pcl::PointCloud<PointType>::Ptr point_cloud_ptr (new pcl::PointCloud<PointType>);
00093   pcl::PointCloud<PointType>& point_cloud = *point_cloud_ptr;
00094   Eigen::Affine3f scene_sensor_pose (Eigen::Affine3f::Identity ());
00095   std::vector<int> pcd_filename_indices = pcl::console::parse_file_extension_argument (argc, argv, "pcd");
00096   if (!pcd_filename_indices.empty ())
00097   {
00098     std::string filename = argv[pcd_filename_indices[0]];
00099     if (pcl::io::loadPCDFile (filename, point_cloud) == -1)
00100     {
00101       std::cout << "Was not able to open file \""<<filename<<"\".\n";
00102       printUsage (argv[0]);
00103       return 0;
00104     }
00105     scene_sensor_pose = Eigen::Affine3f (Eigen::Translation3f (point_cloud.sensor_origin_[0],
00106                                                              point_cloud.sensor_origin_[1],
00107                                                              point_cloud.sensor_origin_[2])) *
00108                         Eigen::Affine3f (point_cloud.sensor_orientation_);
00109   }
00110   else
00111   {
00112     std::cout << "\nNo *.pcd file given => Genarating example point cloud.\n\n";
00113     for (float x=-0.5f; x<=0.5f; x+=0.01f)
00114     {
00115       for (float y=-0.5f; y<=0.5f; y+=0.01f)
00116       {
00117         PointType point;  point.x = x;  point.y = y;  point.z = 2.0f - y;
00118         point_cloud.points.push_back (point);
00119       }
00120     }
00121     point_cloud.width = (int) point_cloud.points.size ();  point_cloud.height = 1;
00122   }
00123   
00124   // -----------------------------------------------
00125   // -----Create RangeImage from the PointCloud-----
00126   // -----------------------------------------------
00127   float noise_level = 0.0;
00128   float min_range = 0.0f;
00129   int border_size = 1;
00130   boost::shared_ptr<pcl::RangeImage> range_image_ptr(new pcl::RangeImage);
00131   pcl::RangeImage& range_image = *range_image_ptr;   
00132   range_image.createFromPointCloud (point_cloud, angular_resolution_x, angular_resolution_y,
00133                                     pcl::deg2rad (360.0f), pcl::deg2rad (180.0f),
00134                                     scene_sensor_pose, coordinate_frame, noise_level, min_range, border_size);
00135   
00136   // --------------------------------------------
00137   // -----Open 3D viewer and add point cloud-----
00138   // --------------------------------------------
00139   pcl::visualization::PCLVisualizer viewer ("3D Viewer");
00140   viewer.setBackgroundColor (1, 1, 1);
00141   pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> range_image_color_handler (range_image_ptr, 0, 0, 0);
00142   viewer.addPointCloud (range_image_ptr, range_image_color_handler, "range image");
00143   viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "range image");
00144   //viewer.addCoordinateSystem (1.0f);
00145   //PointCloudColorHandlerCustom<PointType> point_cloud_color_handler (point_cloud_ptr, 150, 150, 150);
00146   //viewer.addPointCloud (point_cloud_ptr, point_cloud_color_handler, "original point cloud");
00147   viewer.initCameraParameters ();
00148   setViewerPose(viewer, range_image.getTransformationToWorldSystem ());
00149   
00150   // --------------------------
00151   // -----Show range image-----
00152   // --------------------------
00153   pcl::visualization::RangeImageVisualizer range_image_widget ("Range image");
00154   range_image_widget.showRangeImage (range_image);
00155   
00156   //--------------------
00157   // -----Main loop-----
00158   //--------------------
00159   while (!viewer.wasStopped ())
00160   {
00161     range_image_widget.spinOnce ();
00162     viewer.spinOnce ();
00163     pcl_sleep (0.01);
00164     
00165     if (live_update)
00166     {
00167       scene_sensor_pose = viewer.getViewerPose();
00168       range_image.createFromPointCloud (point_cloud, angular_resolution_x, angular_resolution_y,
00169                                         pcl::deg2rad (360.0f), pcl::deg2rad (180.0f),
00170                                         scene_sensor_pose, pcl::RangeImage::LASER_FRAME, noise_level, min_range, border_size);
00171       range_image_widget.showRangeImage (range_image);
00172     }
00173   }
00174 }


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:17:37