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
00039
00042 template <typename PointSource, typename PointTarget> void
00043 pcl::IterativeClosestPoint<PointSource, PointTarget>::computeTransformation (PointCloudSource &output)
00044 {
00045
00046 std::vector<int> nn_indices (1);
00047 std::vector<float> nn_dists (1);
00048
00049
00050 PointCloudTarget input_corresp;
00051 input_corresp.points.resize (indices_->size ());
00052
00053 nr_iterations_ = 0;
00054 converged_ = false;
00055 double dist_threshold = corr_dist_threshold_ * corr_dist_threshold_;
00056
00057 while (!converged_)
00058 {
00059
00060 previous_transformation_ = transformation_;
00061
00062 int cnt = 0;
00063 std::vector<int> source_indices (indices_->size ());
00064 std::vector<int> target_indices (indices_->size ());
00065
00066
00067 for (size_t idx = 0; idx < indices_->size (); ++idx)
00068 {
00069 if (!searchForNeighbors (output, idx, nn_indices, nn_dists))
00070 {
00071 ROS_ERROR ("[pcl::%s::computeTransformation] Unable to find a nearest neighbor in the target dataset for point %d in the source!", getClassName ().c_str (), (*indices_)[idx]);
00072 return;
00073 }
00074
00075
00076 if (nn_dists[0] < dist_threshold)
00077 {
00078 source_indices[cnt] = idx;
00079 target_indices[cnt] = nn_indices[0];
00080 cnt++;
00081 }
00082 }
00083
00084 source_indices.resize (cnt); target_indices.resize (cnt);
00085
00086 std::vector<int> source_indices_good;
00087 std::vector<int> target_indices_good;
00088 {
00089
00090
00091 typedef typename SampleConsensusModelRegistration<PointSource>::Ptr SampleConsensusModelRegistrationPtr;
00092 SampleConsensusModelRegistrationPtr model;
00093 model.reset (new SampleConsensusModelRegistration<PointSource> (output.makeShared (), source_indices));
00094
00095 model->setInputTarget (target_, target_indices);
00096
00097 RandomSampleConsensus<PointSource> sac (model, inlier_threshold_);
00098 sac.setMaxIterations (1000);
00099
00100
00101 if (!sac.computeModel ())
00102 {
00103 source_indices_good = source_indices;
00104 target_indices_good = target_indices;
00105 }
00106 else
00107 {
00108 std::vector<int> inliers;
00109
00110 sac.getInliers (inliers);
00111 source_indices_good.resize (inliers.size ());
00112 target_indices_good.resize (inliers.size ());
00113
00114 for (size_t i = 0; i < inliers.size (); ++i)
00115 {
00116 source_indices_good[i] = source_indices[inliers[i]];
00117 target_indices_good[i] = target_indices[inliers[i]];
00118 }
00119 }
00120 }
00121
00122
00123 cnt = (int)source_indices_good.size ();
00124 if (cnt < min_number_correspondences_)
00125 {
00126 ROS_ERROR ("[pcl::%s::computeTransformation] Not enough correspondences found. Relax your threshold parameters.", getClassName ().c_str ());
00127 converged_ = false;
00128 return;
00129 }
00130
00131 ROS_DEBUG ("[pcl::%s::computeTransformation] Number of correspondences %d [%f%%] out of %zu points [100.0%%], RANSAC rejected: %zu [%f%%].", getClassName ().c_str (), cnt, (cnt * 100.0) / indices_->size (), indices_->size (), source_indices.size () - cnt, (source_indices.size () - cnt) * 100.0 / source_indices.size ());
00132
00133
00134 estimateRigidTransformationSVD (output, source_indices_good, *target_, target_indices_good, transformation_);
00135
00136
00137 transformPointCloud (output, output, transformation_);
00138
00139
00140 final_transformation_ = transformation_ * final_transformation_;
00141
00142 nr_iterations_++;
00143
00144 if (nr_iterations_ >= max_iterations_ ||
00145 fabs ((transformation_ - previous_transformation_).sum ()) < transformation_epsilon_)
00146 {
00147 converged_ = true;
00148 ROS_DEBUG ("[pcl::%s::computeTransformation] Convergence reached. Number of iterations: %d out of %d. Transformation difference: %f",
00149 getClassName ().c_str (), nr_iterations_, max_iterations_, fabs ((transformation_ - previous_transformation_).sum ()));
00150 }
00151 }
00152 }
00153