40 #ifndef PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_LM_HPP_
41 #define PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_LM_HPP_
43 #include <pcl/registration/warp_point_rigid_6d.h>
45 #include <unsupported/Eigen/NonLinearOptimization>
48 template <
typename Po
intSource,
typename Po
intTarget,
typename MatScalar>
56 template <
typename Po
intSource,
typename Po
intTarget,
typename MatScalar>
61 Matrix4& transformation_matrix)
const
65 if (cloud_src.
size() != cloud_tgt.
size()) {
66 PCL_ERROR(
"[pcl::registration::TransformationEstimationLM::"
67 "estimateRigidTransformation] ");
68 PCL_ERROR(
"Number or points in source (%zu) differs than target (%zu)!\n",
69 static_cast<std::size_t
>(cloud_src.
size()),
70 static_cast<std::size_t
>(cloud_tgt.
size()));
73 if (cloud_src.
size() < 4)
75 PCL_ERROR(
"[pcl::registration::TransformationEstimationLM::"
76 "estimateRigidTransformation] ");
77 PCL_ERROR(
"Need at least 4 points to estimate a transform! Source and target have "
79 static_cast<std::size_t
>(cloud_src.
size()));
83 int n_unknowns = warp_point_->getDimension();
84 VectorX x(n_unknowns);
88 tmp_src_ = &cloud_src;
89 tmp_tgt_ = &cloud_tgt;
91 OptimizationFunctor functor(
static_cast<int>(cloud_src.
size()),
this);
92 Eigen::NumericalDiff<OptimizationFunctor> num_diff(functor);
95 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctor>, MatScalar> lm(
97 int info = lm.minimize(x);
101 "[pcl::registration::TransformationEstimationLM::estimateRigidTransformation]");
102 PCL_DEBUG(
"LM solver finished with exit code %i, having a residual norm of %g. \n",
105 PCL_DEBUG(
"Final solution: [%f", x[0]);
106 for (
int i = 1; i < n_unknowns; ++i)
107 PCL_DEBUG(
" %f", x[i]);
111 warp_point_->setParam(x);
112 transformation_matrix = warp_point_->getTransform();
119 template <
typename Po
intSource,
typename Po
intTarget,
typename MatScalar>
125 Matrix4& transformation_matrix)
const
127 if (indices_src.size() != cloud_tgt.
size()) {
129 "[pcl::registration::TransformationEstimationLM::estimateRigidTransformation] "
130 "Number or points in source (%zu) differs than target (%zu)!\n",
132 static_cast<std::size_t
>(cloud_tgt.
size()));
137 transformation_matrix.setIdentity();
139 const auto nr_correspondences = cloud_tgt.
size();
141 indices_tgt.resize(nr_correspondences);
142 for (std::size_t i = 0; i < nr_correspondences; ++i)
145 estimateRigidTransformation(
146 cloud_src, indices_src, cloud_tgt, indices_tgt, transformation_matrix);
150 template <
typename Po
intSource,
typename Po
intTarget,
typename MatScalar>
157 Matrix4& transformation_matrix)
const
159 if (indices_src.size() != indices_tgt.size()) {
161 "[pcl::registration::TransformationEstimationLM::estimateRigidTransformation] "
162 "Number or points in source (%lu) differs than target (%lu)!\n",
168 if (indices_src.size() < 4)
170 PCL_ERROR(
"[pcl::IterativeClosestPointNonLinear::estimateRigidTransformationLM] ");
171 PCL_ERROR(
"Need at least 4 points to estimate a transform! Source and target have "
177 int n_unknowns = warp_point_->getDimension();
179 x.setConstant(n_unknowns, 0);
182 tmp_src_ = &cloud_src;
183 tmp_tgt_ = &cloud_tgt;
184 tmp_idx_src_ = &indices_src;
185 tmp_idx_tgt_ = &indices_tgt;
187 OptimizationFunctorWithIndices functor(
static_cast<int>(indices_src.size()),
this);
188 Eigen::NumericalDiff<OptimizationFunctorWithIndices> num_diff(functor);
191 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<OptimizationFunctorWithIndices>,
194 int info = lm.minimize(x);
198 "[pcl::registration::TransformationEstimationLM::estimateRigidTransformation] LM "
199 "solver finished with exit code %i, having a residual norm of %g. \n",
202 PCL_DEBUG(
"Final solution: [%f", x[0]);
203 for (
int i = 1; i < n_unknowns; ++i)
204 PCL_DEBUG(
" %f", x[i]);
208 warp_point_->setParam(x);
209 transformation_matrix = warp_point_->getTransform();
213 tmp_idx_src_ = tmp_idx_tgt_ =
nullptr;
217 template <
typename Po
intSource,
typename Po
intTarget,
typename MatScalar>
223 Matrix4& transformation_matrix)
const
225 const auto nr_correspondences = correspondences.size();
228 for (std::size_t i = 0; i < nr_correspondences; ++i) {
229 indices_src[i] = correspondences[i].index_query;
230 indices_tgt[i] = correspondences[i].index_match;
233 estimateRigidTransformation(
234 cloud_src, indices_src, cloud_tgt, indices_tgt, transformation_matrix);
238 template <
typename Po
intSource,
typename Po
intTarget,
typename MatScalar>
247 estimator_->warp_point_->setParam(x);
251 for (
int i = 0; i < values(); ++i) {
252 const PointSource& p_src = src_points[i];
253 const PointTarget& p_tgt = tgt_points[i];
257 estimator_->warp_point_->warpPoint(p_src, p_src_warped);
260 fvec[i] = estimator_->computeDistance(p_src_warped, p_tgt);
266 template <
typename Po
intSource,
typename Po
intTarget,
typename MatScalar>
273 const pcl::Indices& src_indices = *estimator_->tmp_idx_src_;
274 const pcl::Indices& tgt_indices = *estimator_->tmp_idx_tgt_;
277 estimator_->warp_point_->setParam(x);
281 for (
int i = 0; i < values(); ++i) {
282 const PointSource& p_src = src_points[src_indices[i]];
283 const PointTarget& p_tgt = tgt_points[tgt_indices[i]];
287 estimator_->warp_point_->warpPoint(p_src, p_src_warped);
290 fvec[i] = estimator_->computeDistance(p_src_warped, p_tgt);
WarpPointRigid3D enables 6D (3D rotation + 3D translation) transformations for points.
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
IndicesAllocator<> Indices
Type used for indices in PCL.