00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <ros/ros.h>
00041 #include <pcl/point_types.h>
00042 #include <pcl/point_cloud.h>
00043 #include <pcl/point_representation.h>
00044
00045 #include <pcl/io/pcd_io.h>
00046 #include <pcl/filters/voxel_grid.h>
00047 #include <pcl/features/normal_3d.h>
00048 #include <pcl/registration/icp.h>
00049 #include <pcl/registration/icp_nl.h>
00050 #include <pcl/registration/transforms.h>
00051
00052 #include "pcl_visualization/pcl_visualizer.h"
00053
00054 using namespace std;
00055 using pcl_visualization::PointCloudColorHandlerGenericField;
00056 using pcl_visualization::PointCloudColorHandlerCustom;
00057
00058 typedef pcl::PointXYZ PointT;
00059 typedef pcl::PointCloud<PointT> PointCloud;
00060 typedef pcl::PointNormal PointNormalT;
00061 typedef pcl::PointCloud<PointNormalT> PointCloudWithNormals;
00062
00063
00064 pcl_visualization::PCLVisualizer *p;
00065 int vp_1, vp_2;
00066
00067 struct PCD
00068 {
00069 PointCloud cloud;
00070 std::string f_name;
00071 };
00072
00073 struct PCDComparator
00074 {
00075 bool operator () (const PCD& p1, const PCD& p2)
00076 {
00077 return (p1.f_name < p2.f_name);
00078 }
00079 };
00080
00081
00082 class MyPointRepresentation : public pcl::PointRepresentation <PointNormalT>
00083 {
00084 using pcl::PointRepresentation<PointNormalT>::nr_dimensions_;
00085 public:
00086 MyPointRepresentation ()
00087 {
00088
00089 nr_dimensions_ = 4;
00090 }
00091
00092
00093 virtual void copyToFloatArray (const PointNormalT &p, float * out) const
00094 {
00095
00096 out[0] = p.x;
00097 out[1] = p.y;
00098 out[2] = p.z;
00099 out[3] = p.curvature;
00100 }
00101 };
00102
00104
00109 void
00110 loadData (int argc, char **argv, std::vector<PCD> &models)
00111 {
00112 std::string extension (".pcd");
00113
00114 for (int i = 1; i < argc; i++)
00115 {
00116 string fname = string (argv[i]);
00117
00118 if (fname.size () <= extension.size ())
00119 continue;
00120
00121 transform (fname.begin (), fname.end (), fname.begin (), (int(*)(int))tolower);
00122 if (fname.compare (fname.size () - extension.size (), extension.size (), extension) == 0)
00123 {
00124
00125 PCD m;
00126 m.f_name = argv[i];
00127 pcl::io::loadPCDFile (argv[i], m.cloud);
00128 models.push_back (m);
00129 }
00130 }
00131
00132
00133 }
00134
00136
00141 void
00142 pairAlign (const PointCloud &cloud_src, const PointCloud &cloud_tgt, PointCloud &output, bool downsample = false)
00143 {
00144
00145
00146
00147 PointCloud src, tgt;
00148 pcl::VoxelGrid<PointT> grid;
00149 if (downsample)
00150 {
00151 grid.setLeafSize (0.001, 0.001, 0.001);
00152 grid.setFilterFieldName ("z");
00153 grid.setFilterLimits (-1, 5.0);
00154
00155 grid.setInputCloud (boost::make_shared<const PointCloud> (cloud_src));
00156 grid.filter (src);
00157
00158 grid.setInputCloud (boost::make_shared<const PointCloud> (cloud_tgt));
00159 grid.filter (tgt);
00160 }
00161 else
00162 {
00163 src = cloud_src;
00164 tgt = cloud_tgt;
00165 }
00166
00167
00168
00169 PointCloudWithNormals points_with_normals_src, points_with_normals_tgt;
00170
00171 pcl::NormalEstimation<PointT, PointNormalT> norm_est;
00172 norm_est.setSearchMethod (boost::make_shared<pcl::KdTreeFLANN<PointT> > ());
00173 norm_est.setKSearch (30);
00174
00175 norm_est.setInputCloud (boost::make_shared<const PointCloud> (src));
00176 norm_est.compute (points_with_normals_src);
00177 pcl::copyPointCloud (src, points_with_normals_src);
00178
00179 norm_est.setInputCloud (boost::make_shared<const PointCloud> (tgt));
00180 norm_est.compute (points_with_normals_tgt);
00181 pcl::copyPointCloud (tgt, points_with_normals_tgt);
00182
00183
00184
00185 MyPointRepresentation point_representation;
00186
00187 float alpha[4] = {1.0, 1.0, 1.0, 1.0};
00188 point_representation.setRescaleValues (alpha);
00189
00190
00191
00192
00193 pcl::IterativeClosestPointNonLinear<PointNormalT, PointNormalT> reg;
00194 reg.setTransformationEpsilon (1e-6);
00195
00196
00197 reg.setMaxCorrespondenceDistance (0.1);
00198
00199 reg.setPointRepresentation (boost::make_shared<const MyPointRepresentation> (point_representation));
00200
00201 reg.setInputCloud (boost::make_shared<const PointCloudWithNormals> (points_with_normals_src));
00202 reg.setInputTarget (boost::make_shared<const PointCloudWithNormals> (points_with_normals_tgt));
00203
00204 PointCloudWithNormals unused;
00205
00206
00207
00208 PointCloudColorHandlerGenericField<PointNormalT> tgt_color_handler (points_with_normals_tgt, "curvature");
00209 if (!tgt_color_handler.isCapable ())
00210 ROS_WARN ("Cannot create curvature color handler!");
00211 p->addPointCloud (points_with_normals_tgt, tgt_color_handler, "target", vp_2);
00212 PointCloudColorHandlerGenericField<PointNormalT> src_color_handler (points_with_normals_src, "curvature");
00213
00214
00215
00216 Eigen::Matrix4f Ti = Eigen::Matrix4f::Identity (), prev;
00217 PointCloudWithNormals reg_result = points_with_normals_src;
00218 reg.setMaximumIterations (2);
00219 for (int i = 0; i < 300; ++i)
00220 {
00221
00222 points_with_normals_src = reg_result;
00223 p->addPointCloud (points_with_normals_src, src_color_handler, "source", vp_2);
00224
00225
00226 reg.setInputCloud (boost::make_shared<const PointCloudWithNormals> (points_with_normals_src));
00227 reg.align (reg_result);
00228
00229
00230
00231 Ti = reg.getFinalTransformation () * Ti;
00232
00233
00234
00235
00236
00237
00238
00239
00240 if (fabs ((reg.getLastIncrementalTransformation () - prev).sum ()) < reg.getTransformationEpsilon ())
00241 reg.setMaxCorrespondenceDistance (reg.getMaxCorrespondenceDistance () - 0.001);
00242
00243 prev = reg.getLastIncrementalTransformation ();
00244 p->spinOnce (); p->removePointCloud ("source");
00245 }
00246 p->removePointCloud ("target");
00247
00248
00249
00250
00251
00252
00253
00254 pcl::transformPointCloud (cloud_src, output, Ti);
00255
00256 PointCloudColorHandlerCustom<PointT> cloud_tgt_h (cloud_tgt, 0, 255, 0);
00257 PointCloudColorHandlerCustom<PointT> cloud_src_h (output, 255, 0, 0);
00258 p->addPointCloud (cloud_tgt, cloud_tgt_h, "target", vp_2); p->addPointCloud (output, cloud_src_h, "source", vp_2); p->spin (); p->removePointCloud ("source"); p->removePointCloud ("target");
00259
00260 output += cloud_tgt;
00261 }
00262
00263
00264 int
00265 main (int argc, char** argv)
00266 {
00267 vector<PCD> data;
00268 loadData (argc, argv, data);
00269 if (data.empty ())
00270 {
00271 ROS_ERROR ("Syntax is: %s <source.pcd> <target.pcd> [*]", argv[0]);
00272 ROS_ERROR ("[*] - multiple files can be added. The registration results of (i, i+1) will be registered against (i+2), etc");
00273 ROS_INFO ("Example: %s `rospack find pcl`/test/bun0.pcd `rospack find pcl`/test/bun4.pcd", argv[0]);
00274 return (-1);
00275 }
00276 ROS_INFO ("Loaded %d datasets.", (int)data.size ());
00277
00278
00279
00280 p = new pcl_visualization::PCLVisualizer (argc, argv, "Pairwise Incremental Registration example");
00281 p->createViewPort (0.0, 0, 0.5, 1.0, vp_1);
00282 p->createViewPort (0.5, 0, 1.0, 1.0, vp_2);
00283
00284 PointCloud result = data[0].cloud;
00285 for (size_t i = 1; i < data.size (); ++i)
00286 {
00287
00288 PointCloudColorHandlerCustom<PointT> tgt_h (data[i].cloud, 0, 255, 0); PointCloudColorHandlerCustom<PointT> src_h (result, 255, 0, 0);
00289 p->addPointCloud (data[i].cloud, tgt_h, "vp1_target", vp_1); p->addPointCloud (result, src_h, "vp1_source", vp_1);
00290 ROS_INFO ("Press q to begin the registration.");
00291 p->spin ();
00292
00293 PointCloud temp;
00294 ROS_INFO ("Aligning %s (%d) with %s (%d).", data[i-1].f_name.c_str (), (int)result.points.size (), data[i].f_name.c_str (), (int)data[i].cloud.points.size ());
00295 pairAlign (result, data[i].cloud, temp);
00296 result = temp;
00297
00298 std::stringstream ss;
00299 ss << i << ".pcd";
00300 pcl::io::savePCDFile (ss.str (), result, true);
00301
00302
00303 p->removePointCloud ("vp1_target"); p->removePointCloud ("vp1_source");
00304 }
00305 }
00306