41 #ifndef PCL_REGISTRATION_IMPL_ICP_HPP_
42 #define PCL_REGISTRATION_IMPL_ICP_HPP_
44 #include <pcl/correspondence.h>
49 template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
54 Eigen::Vector4f pt(0.0f, 0.0f, 0.0f, 1.0f), pt_t;
55 Eigen::Matrix4f tr = transform.template cast<float>();
59 if (source_has_normals_) {
60 Eigen::Vector3f nt, nt_t;
61 Eigen::Matrix3f rot = tr.block<3, 3>(0, 0);
63 for (std::size_t i = 0; i < input.size(); ++i) {
64 const auto* data_in =
reinterpret_cast<const std::uint8_t*
>(&input[i]);
65 auto* data_out =
reinterpret_cast<std::uint8_t*
>(&output[i]);
66 memcpy(&pt[0], data_in + x_idx_offset_,
sizeof(
float));
67 memcpy(&pt[1], data_in + y_idx_offset_,
sizeof(
float));
68 memcpy(&pt[2], data_in + z_idx_offset_,
sizeof(
float));
70 if (!std::isfinite(pt[0]) || !std::isfinite(pt[1]) || !std::isfinite(pt[2]))
75 memcpy(data_out + x_idx_offset_, &pt_t[0],
sizeof(
float));
76 memcpy(data_out + y_idx_offset_, &pt_t[1],
sizeof(
float));
77 memcpy(data_out + z_idx_offset_, &pt_t[2],
sizeof(
float));
79 memcpy(&nt[0], data_in + nx_idx_offset_,
sizeof(
float));
80 memcpy(&nt[1], data_in + ny_idx_offset_,
sizeof(
float));
81 memcpy(&nt[2], data_in + nz_idx_offset_,
sizeof(
float));
83 if (!std::isfinite(nt[0]) || !std::isfinite(nt[1]) || !std::isfinite(nt[2]))
88 memcpy(data_out + nx_idx_offset_, &nt_t[0],
sizeof(
float));
89 memcpy(data_out + ny_idx_offset_, &nt_t[1],
sizeof(
float));
90 memcpy(data_out + nz_idx_offset_, &nt_t[2],
sizeof(
float));
94 for (std::size_t i = 0; i < input.size(); ++i) {
95 const auto* data_in =
reinterpret_cast<const std::uint8_t*
>(&input[i]);
96 auto* data_out =
reinterpret_cast<std::uint8_t*
>(&output[i]);
97 memcpy(&pt[0], data_in + x_idx_offset_,
sizeof(
float));
98 memcpy(&pt[1], data_in + y_idx_offset_,
sizeof(
float));
99 memcpy(&pt[2], data_in + z_idx_offset_,
sizeof(
float));
101 if (!std::isfinite(pt[0]) || !std::isfinite(pt[1]) || !std::isfinite(pt[2]))
106 memcpy(data_out + x_idx_offset_, &pt_t[0],
sizeof(
float));
107 memcpy(data_out + y_idx_offset_, &pt_t[1],
sizeof(
float));
108 memcpy(data_out + z_idx_offset_, &pt_t[2],
sizeof(
float));
113 template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
125 final_transformation_ = guess;
128 if (guess != Matrix4::Identity()) {
129 input_transformed->resize(input_->size());
131 transformCloud(*input_, *input_transformed, guess);
134 *input_transformed = *input_;
136 transformation_ = Matrix4::Identity();
139 determineRequiredBlobData();
141 if (need_target_blob_)
145 correspondence_estimation_->setInputTarget(target_);
146 if (correspondence_estimation_->requiresTargetNormals())
147 correspondence_estimation_->setTargetNormals(target_blob);
149 for (std::size_t i = 0; i < correspondence_rejectors_.size(); ++i) {
151 if (rej->requiresTargetPoints())
152 rej->setTargetPoints(target_blob);
153 if (rej->requiresTargetNormals() && target_has_normals_)
154 rej->setTargetNormals(target_blob);
157 convergence_criteria_->setMaximumIterations(max_iterations_);
158 convergence_criteria_->setRelativeMSE(euclidean_fitness_epsilon_);
159 convergence_criteria_->setTranslationThreshold(transformation_epsilon_);
160 if (transformation_rotation_epsilon_ > 0)
161 convergence_criteria_->setRotationThreshold(transformation_rotation_epsilon_);
167 if (need_source_blob_) {
172 previous_transformation_ = transformation_;
175 correspondence_estimation_->setInputSource(input_transformed);
176 if (correspondence_estimation_->requiresSourceNormals())
177 correspondence_estimation_->setSourceNormals(input_transformed_blob);
179 if (use_reciprocal_correspondence_)
180 correspondence_estimation_->determineReciprocalCorrespondences(
181 *correspondences_, corr_dist_threshold_);
183 correspondence_estimation_->determineCorrespondences(*correspondences_,
184 corr_dist_threshold_);
188 for (std::size_t i = 0; i < correspondence_rejectors_.size(); ++i) {
190 PCL_DEBUG(
"Applying a correspondence rejector method: %s.\n",
191 rej->getClassName().c_str());
192 if (rej->requiresSourcePoints())
193 rej->setSourcePoints(input_transformed_blob);
194 if (rej->requiresSourceNormals() && source_has_normals_)
195 rej->setSourceNormals(input_transformed_blob);
196 rej->setInputCorrespondences(temp_correspondences);
197 rej->getCorrespondences(*correspondences_);
199 if (i < correspondence_rejectors_.size() - 1)
200 *temp_correspondences = *correspondences_;
204 if (correspondences_->size() < min_number_correspondences_) {
205 PCL_ERROR(
"[pcl::%s::computeTransformation] Not enough correspondences found. "
206 "Relax your threshold parameters.\n",
207 getClassName().c_str());
208 convergence_criteria_->setConvergenceState(
210 Scalar>::CONVERGENCE_CRITERIA_NO_CORRESPONDENCES);
216 transformation_estimation_->estimateRigidTransformation(
217 *input_transformed, *target_, *correspondences_, transformation_);
220 transformCloud(*input_transformed, *input_transformed, transformation_);
223 final_transformation_ = transformation_ * final_transformation_;
228 if (update_visualizer_ !=
nullptr) {
231 source_indices_good.emplace_back(corr.index_query);
232 target_indices_good.emplace_back(corr.index_match);
235 *input_transformed, source_indices_good, *target_, target_indices_good);
238 converged_ =
static_cast<bool>((*convergence_criteria_));
239 }
while (convergence_criteria_->getConvergenceState() ==
241 Scalar>::CONVERGENCE_CRITERIA_NOT_CONVERGED);
244 PCL_DEBUG(
"Transformation "
245 "is:\n\t%5f\t%5f\t%5f\t%5f\n\t%5f\t%5f\t%5f\t%5f\n\t%5f\t%5f\t%5f\t%5f\n\t%"
246 "5f\t%5f\t%5f\t%5f\n",
247 final_transformation_(0, 0),
248 final_transformation_(0, 1),
249 final_transformation_(0, 2),
250 final_transformation_(0, 3),
251 final_transformation_(1, 0),
252 final_transformation_(1, 1),
253 final_transformation_(1, 2),
254 final_transformation_(1, 3),
255 final_transformation_(2, 0),
256 final_transformation_(2, 1),
257 final_transformation_(2, 2),
258 final_transformation_(2, 3),
259 final_transformation_(3, 0),
260 final_transformation_(3, 1),
261 final_transformation_(3, 2),
262 final_transformation_(3, 3));
267 transformCloud(*input_, output, final_transformation_);
270 template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
274 need_source_blob_ =
false;
275 need_target_blob_ =
false;
277 need_source_blob_ |= correspondence_estimation_->requiresSourceNormals();
278 need_target_blob_ |= correspondence_estimation_->requiresTargetNormals();
280 if (correspondence_estimation_->requiresSourceNormals() && !source_has_normals_) {
281 PCL_WARN(
"[pcl::%s::determineRequiredBlobData] Estimator expects source normals, "
282 "but we can't provide them.\n",
283 getClassName().c_str());
285 if (correspondence_estimation_->requiresTargetNormals() && !target_has_normals_) {
286 PCL_WARN(
"[pcl::%s::determineRequiredBlobData] Estimator expects target normals, "
287 "but we can't provide them.\n",
288 getClassName().c_str());
291 for (std::size_t i = 0; i < correspondence_rejectors_.size(); i++) {
293 need_source_blob_ |= rej->requiresSourcePoints();
294 need_source_blob_ |= rej->requiresSourceNormals();
295 need_target_blob_ |= rej->requiresTargetPoints();
296 need_target_blob_ |= rej->requiresTargetNormals();
297 if (rej->requiresSourceNormals() && !source_has_normals_) {
298 PCL_WARN(
"[pcl::%s::determineRequiredBlobData] Rejector %s expects source "
299 "normals, but we can't provide them.\n",
300 getClassName().c_str(),
301 rej->getClassName().c_str());
303 if (rej->requiresTargetNormals() && !target_has_normals_) {
304 PCL_WARN(
"[pcl::%s::determineRequiredBlobData] Rejector %s expects target "
305 "normals, but we can't provide them.\n",
306 getClassName().c_str(),
307 rej->getClassName().c_str());
312 template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
typename Registration< PointSource, PointTarget, Scalar >::Matrix4 Matrix4
typename Registration< PointSource, PointTarget, Scalar >::PointCloudSource PointCloudSource
virtual void transformCloud(const PointCloudSource &input, PointCloudSource &output, const Matrix4 &transform)
Apply a rigid transform to a given dataset.
virtual void determineRequiredBlobData()
Looks at the Estimators and Rejectors and determines whether their blob-setter methods need to be cal...
void computeTransformation(PointCloudSource &output, const Matrix4 &guess) override
Rigid transformation computation method with initial guess.
typename PointCloudSource::Ptr PointCloudSourcePtr
typename IterativeClosestPoint< PointSource, PointTarget, Scalar >::PointCloudSource PointCloudSource
void transformCloud(const PointCloudSource &input, PointCloudSource &output, const Matrix4 &transform) override
Apply a rigid transform to a given dataset.
typename IterativeClosestPoint< PointSource, PointTarget, Scalar >::Matrix4 Matrix4
shared_ptr< CorrespondenceRejector > Ptr
DefaultConvergenceCriteria represents an instantiation of ConvergenceCriteria, and implements the fol...
void transformPointCloudWithNormals(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const Eigen::Matrix< Scalar, 4, 4 > &transform, bool copy_all_fields)
Transform a point cloud and rotate its normals using an Eigen transform.
shared_ptr< Correspondences > CorrespondencesPtr
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
void toPCLPointCloud2(const pcl::PointCloud< PointT > &cloud, pcl::PCLPointCloud2 &msg, bool padding)
Convert a pcl::PointCloud<T> object to a PCLPointCloud2 binary data blob.
IndicesAllocator<> Indices
Type used for indices in PCL.
Correspondence represents a match between two entities (e.g., points, descriptors,...
shared_ptr< ::pcl::PCLPointCloud2 > Ptr