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 <gtest/gtest.h>
00041
00042 #include <pcl/point_types.h>
00043 #include <pcl/io/pcd_io.h>
00044 #include "pcl/features/normal_3d.h"
00045 #include "pcl/features/fpfh.h"
00046 #include "pcl/registration/registration.h"
00047 #include "pcl/registration/icp.h"
00048 #include "pcl/registration/icp_nl.h"
00049 #include "pcl/registration/ia_ransac.h"
00050
00051
00052 using namespace pcl;
00053 using namespace pcl::io;
00054 using namespace std;
00055
00056 PointCloud<PointXYZ> cloud_source, cloud_target, cloud_reg;
00057
00058 template <typename PointSource, typename PointTarget>
00059 class RegistrationWrapper : public Registration<PointSource, PointTarget>
00060 {
00061 public:
00062 void computeTransformation (pcl::PointCloud<PointSource> &foo) { }
00063
00064 bool hasValidFeaturesTest ()
00065 {
00066 return (this->hasValidFeatures ());
00067 }
00068 void findFeatureCorrespondencesTest (int index, std::vector<int> &correspondence_indices)
00069 {
00070 this->findFeatureCorrespondences (index, correspondence_indices);
00071 }
00072 };
00073
00075 TEST (PCL, findFeatureCorrespondences)
00076 {
00077 typedef Histogram<2> FeatureT;
00078 typedef PointCloud<FeatureT> FeatureCloud;
00079 typedef FeatureCloud::ConstPtr FeatureCloudConstPtr;
00080
00081 RegistrationWrapper <PointXYZ, PointXYZ> reg;
00082
00083 FeatureCloud feature0, feature1, feature2, feature3;
00084 feature0.height = feature1.height = feature2.height = feature2.height = 1;
00085 feature0.is_dense = feature1.is_dense = feature2.is_dense = feature2.is_dense = true;
00086
00087 for (float x = -5.0; x <= 5.0; x += 0.2)
00088 {
00089 for (float y = -5.0; y <= 5.0; y += 0.2)
00090 {
00091 FeatureT f;
00092 f.histogram[0] = x;
00093 f.histogram[1] = y;
00094 feature0.points.push_back (f);
00095
00096 f.histogram[0] = x;
00097 f.histogram[1] = y - 2.5;
00098 feature1.points.push_back (f);
00099
00100 f.histogram[0] = x - 2.0;
00101 f.histogram[1] = y + 1.5;
00102 feature2.points.push_back (f);
00103
00104 f.histogram[0] = x + 2.0;
00105 f.histogram[1] = y + 1.5;
00106 feature3.points.push_back (f);
00107 }
00108 }
00109 feature0.width = feature0.points.size ();
00110 feature1.width = feature1.points.size ();
00111 feature2.width = feature2.points.size ();
00112 feature3.width = feature3.points.size ();
00113
00114 KdTreeFLANN<FeatureT> tree;
00115
00116 int k = 600;
00117
00118 reg.setSourceFeature<FeatureT> (feature0.makeShared (), "feature1");
00119 reg.setTargetFeature<FeatureT> (feature1.makeShared (), "feature1");
00120 reg.setKSearch<FeatureT> (tree.makeShared (), k, "feature1");
00121
00122 reg.setSourceFeature<FeatureT> (feature0.makeShared (), "feature2");
00123 reg.setTargetFeature<FeatureT> (feature2.makeShared (), "feature2");
00124 reg.setKSearch<FeatureT> (tree.makeShared (), k, "feature2");
00125
00126 reg.setSourceFeature<FeatureT> (feature0.makeShared (), "feature3");
00127 reg.setTargetFeature<FeatureT> (feature3.makeShared (), "feature3");
00128 reg.setKSearch<FeatureT> (tree.makeShared (), k, "feature3");
00129
00130 ASSERT_TRUE (reg.hasValidFeaturesTest ());
00131
00132 std::vector<int> indices;
00133 reg.findFeatureCorrespondencesTest (1300, indices);
00134
00135 ASSERT_EQ ((int)indices.size (), 10);
00136 const int correct_values[] = {1197, 1248, 1249, 1299, 1300, 1301, 1302, 1350, 1351, 1401};
00137 for (size_t i = 0; i < indices.size (); ++i)
00138 {
00139 EXPECT_EQ (indices[i], correct_values[i]);
00140 }
00141 }
00142
00144 TEST (PCL, IterativeClosestPoint)
00145 {
00146 IterativeClosestPoint<PointXYZ, PointXYZ> reg;
00147 reg.setInputCloud (cloud_source.makeShared ());
00148 reg.setInputTarget (cloud_target.makeShared ());
00149 reg.setMaximumIterations (50);
00150 reg.setTransformationEpsilon (1e-8);
00151 reg.setMaxCorrespondenceDistance (0.05);
00152
00153
00154 reg.align (cloud_reg);
00155 EXPECT_EQ ((int)cloud_reg.points.size (), (int)cloud_source.points.size ());
00156
00157 Eigen::Matrix4f transformation = reg.getFinalTransformation ();
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 EXPECT_EQ (transformation (3, 0), 0);
00175 EXPECT_EQ (transformation (3, 1), 0);
00176 EXPECT_EQ (transformation (3, 2), 0);
00177 EXPECT_EQ (transformation (3, 3), 1);
00178 }
00179
00181 TEST (PCL, IterativeClosestPointNonLinear)
00182 {
00183 IterativeClosestPointNonLinear<PointXYZ, PointXYZ> reg;
00184 reg.setInputCloud (cloud_source.makeShared ());
00185 reg.setInputTarget (cloud_target.makeShared ());
00186 reg.setMaximumIterations (50);
00187 reg.setTransformationEpsilon (1e-8);
00188
00189
00190 reg.align (cloud_reg);
00191 EXPECT_EQ ((int)cloud_reg.points.size (), (int)cloud_source.points.size ());
00192
00193 Eigen::Matrix4f transformation = reg.getFinalTransformation ();
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 EXPECT_EQ (transformation (3, 0), 0);
00211 EXPECT_EQ (transformation (3, 1), 0);
00212 EXPECT_EQ (transformation (3, 2), 0);
00213 EXPECT_EQ (transformation (3, 3), 1);
00214 }
00215
00217 TEST (PCL, SampleConsensusInitialAlignment)
00218 {
00219
00220 Eigen::Vector3f initial_offset (100, 0, 0);
00221 float angle = M_PI/2;
00222 Eigen::Quaternionf initial_rotation (cos (angle / 2), 0, 0, sin (angle / 2));
00223 PointCloud<PointXYZ> cloud_source_transformed;
00224 transformPointCloud (cloud_source, cloud_source_transformed, initial_offset, initial_rotation);
00225
00226
00227 PointCloud<PointXYZ>::Ptr cloud_source_ptr, cloud_target_ptr;
00228 cloud_source_ptr = cloud_source_transformed.makeShared ();
00229 cloud_target_ptr = cloud_target.makeShared ();
00230
00231
00232 KdTreeFLANN<PointXYZ> tree;
00233
00234 NormalEstimation<PointXYZ, Normal> norm_est;
00235 norm_est.setSearchMethod (boost::make_shared<KdTreeFLANN<PointXYZ> > (tree));
00236 norm_est.setRadiusSearch (0.05);
00237 PointCloud<Normal> normals;
00238
00239 FPFHEstimation<PointXYZ, Normal, FPFHSignature33> fpfh_est;
00240 fpfh_est.setSearchMethod (boost::make_shared<KdTreeFLANN<PointXYZ> > (tree));
00241 fpfh_est.setRadiusSearch (0.05);
00242 PointCloud<FPFHSignature33> features_source, features_target;
00243
00244
00245 norm_est.setInputCloud (cloud_source_ptr);
00246 norm_est.compute (normals);
00247 fpfh_est.setInputCloud (cloud_source_ptr);
00248 fpfh_est.setInputNormals (normals.makeShared ());
00249 fpfh_est.compute (features_source);
00250
00251
00252 norm_est.setInputCloud (cloud_target_ptr);
00253 norm_est.compute (normals);
00254 fpfh_est.setInputCloud (cloud_target_ptr);
00255 fpfh_est.setInputNormals (normals.makeShared ());
00256 fpfh_est.compute (features_target);
00257
00258
00259 SampleConsensusInitialAlignment<PointXYZ, PointXYZ, FPFHSignature33> reg;
00260 reg.setMinSampleDistance (0.05);
00261 reg.setMaxCorrespondenceDistance (0.2);
00262 reg.setMaximumIterations (1000);
00263
00264 reg.setInputCloud (cloud_source_ptr);
00265 reg.setInputTarget (cloud_target_ptr);
00266 reg.setSourceFeatures (features_source.makeShared ());
00267 reg.setTargetFeatures (features_target.makeShared ());
00268
00269
00270 reg.align (cloud_reg);
00271 EXPECT_EQ ((int)cloud_reg.points.size (), (int)cloud_source.points.size ());
00272 EXPECT_EQ (reg.getFitnessScore () < 0.0005, true);
00273 }
00274
00275
00276 int
00277 main (int argc, char** argv)
00278 {
00279
00280 loadPCDFile ("test/bun0.pcd", cloud_source);
00281 loadPCDFile ("test/bun4.pcd", cloud_target);
00282
00283
00284
00285
00286 testing::InitGoogleTest (&argc, argv);
00287 return (RUN_ALL_TESTS ());
00288
00289
00290
00291
00292
00293
00294 }
00295