normal_distributions_transform.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <pcl/io/pcd_io.h>
00003 #include <pcl/point_types.h>
00004 
00005 #include <pcl/registration/ndt.h>
00006 #include <pcl/filters/approximate_voxel_grid.h>
00007 
00008 #include <pcl/visualization/pcl_visualizer.h>
00009 #include <boost/thread/thread.hpp>
00010 
00011 int
00012 main (int argc, char** argv)
00013 {
00014   // Loading first scan of room.
00015   pcl::PointCloud<pcl::PointXYZ>::Ptr target_cloud (new pcl::PointCloud<pcl::PointXYZ>);
00016   if (pcl::io::loadPCDFile<pcl::PointXYZ> ("room_scan1.pcd", *target_cloud) == -1)
00017   {
00018     PCL_ERROR ("Couldn't read file room_scan1.pcd \n");
00019     return (-1);
00020   }
00021   std::cout << "Loaded " << target_cloud->size () << " data points from room_scan1.pcd" << std::endl;
00022 
00023   // Loading second scan of room from new perspective.
00024   pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud (new pcl::PointCloud<pcl::PointXYZ>);
00025   if (pcl::io::loadPCDFile<pcl::PointXYZ> ("room_scan2.pcd", *input_cloud) == -1)
00026   {
00027     PCL_ERROR ("Couldn't read file room_scan2.pcd \n");
00028     return (-1);
00029   }
00030   std::cout << "Loaded " << input_cloud->size () << " data points from room_scan2.pcd" << std::endl;
00031 
00032   // Filtering input scan to roughly 10% of original size to increase speed of registration.
00033   pcl::PointCloud<pcl::PointXYZ>::Ptr filtered_cloud (new pcl::PointCloud<pcl::PointXYZ>);
00034   pcl::ApproximateVoxelGrid<pcl::PointXYZ> approximate_voxel_filter;
00035   approximate_voxel_filter.setLeafSize (0.2, 0.2, 0.2);
00036   approximate_voxel_filter.setInputCloud (input_cloud);
00037   approximate_voxel_filter.filter (*filtered_cloud);
00038   std::cout << "Filtered cloud contains " << filtered_cloud->size ()
00039             << " data points from room_scan2.pcd" << std::endl;
00040 
00041   // Initializing Normal Distributions Transform (NDT).
00042   pcl::NormalDistributionsTransform<pcl::PointXYZ, pcl::PointXYZ> ndt;
00043 
00044   // Setting scale dependent NDT parameters
00045   // Setting minimum transformation difference for termination condition.
00046   ndt.setTransformationEpsilon (0.01);
00047   // Setting maximum step size for More-Thuente line search.
00048   ndt.setStepSize (0.1);
00049   //Setting Resolution of NDT grid structure (VoxelGridCovariance).
00050   ndt.setResolution (1.0);
00051 
00052   // Setting max number of registration iterations.
00053   ndt.setMaximumIterations (35);
00054 
00055   // Setting point cloud to be aligned.
00056   ndt.setInputSource (filtered_cloud);
00057   // Setting point cloud to be aligned to.
00058   ndt.setInputTarget (target_cloud);
00059 
00060   // Set initial alignment estimate found using robot odometry.
00061   Eigen::AngleAxisf init_rotation (0.6931, Eigen::Vector3f::UnitZ ());
00062   Eigen::Translation3f init_translation (1.79387, 0.720047, 0);
00063   Eigen::Matrix4f init_guess = (init_translation * init_rotation).matrix ();
00064 
00065   // Calculating required rigid transform to align the input cloud to the target cloud.
00066   pcl::PointCloud<pcl::PointXYZ>::Ptr output_cloud (new pcl::PointCloud<pcl::PointXYZ>);
00067   ndt.align (*output_cloud, init_guess);
00068 
00069   std::cout << "Normal Distributions Transform has converged:" << ndt.hasConverged ()
00070             << " score: " << ndt.getFitnessScore () << std::endl;
00071 
00072   // Transforming unfiltered, input cloud using found transform.
00073   pcl::transformPointCloud (*input_cloud, *output_cloud, ndt.getFinalTransformation ());
00074 
00075   // Saving transformed input cloud.
00076   pcl::io::savePCDFileASCII ("room_scan2_transformed.pcd", *output_cloud);
00077 
00078   // Initializing point cloud visualizer
00079   boost::shared_ptr<pcl::visualization::PCLVisualizer>
00080   viewer_final (new pcl::visualization::PCLVisualizer ("3D Viewer"));
00081   viewer_final->setBackgroundColor (0, 0, 0);
00082 
00083   // Coloring and visualizing target cloud (red).
00084   pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>
00085   target_color (target_cloud, 255, 0, 0);
00086   viewer_final->addPointCloud<pcl::PointXYZ> (target_cloud, target_color, "target cloud");
00087   viewer_final->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE,
00088                                                   1, "target cloud");
00089 
00090   // Coloring and visualizing transformed input cloud (green).
00091   pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>
00092   output_color (output_cloud, 0, 255, 0);
00093   viewer_final->addPointCloud<pcl::PointXYZ> (output_cloud, output_color, "output cloud");
00094   viewer_final->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE,
00095                                                   1, "output cloud");
00096 
00097   // Starting visualizer
00098   viewer_final->addCoordinateSystem (1.0);
00099   viewer_final->initCameraParameters ();
00100 
00101   // Wait until visualizer window is closed.
00102   while (!viewer_final->wasStopped ())
00103   {
00104     viewer_final->spinOnce (100);
00105     boost::this_thread::sleep (boost::posix_time::microseconds (100000));
00106   }
00107 
00108   return (0);
00109 }


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:25:51