41 #ifndef PCL_NDT_2D_IMPL_H_
42 #define PCL_NDT_2D_IMPL_H_
44 #include <boost/core/noncopyable.hpp>
46 #include <Eigen/Eigenvalues>
58 template <
unsigned N = 3,
typename T =
double>
63 Eigen::Matrix<T, N, 1>
grad;
70 r.
hessian = Eigen::Matrix<T, N, N>::Zero();
71 r.
grad = Eigen::Matrix<T, N, 1>::Zero();
97 template <
typename Po
intT>
121 Eigen::Vector2d sx = Eigen::Vector2d::Zero();
122 Eigen::Matrix2d sxx = Eigen::Matrix2d::Zero();
125 Eigen::Vector2d p(cloud[pt_index].x, cloud[pt_index].y);
127 sxx += p * p.transpose();
133 mean_ = sx /
static_cast<double>(
n_);
135 Eigen::Matrix2d covar =
136 (sxx - 2 * (sx *
mean_.transpose())) /
static_cast<double>(
n_) +
139 Eigen::SelfAdjointEigenSolver<Eigen::Matrix2d> solver(covar);
140 if (solver.eigenvalues()[0] < min_covar_eigvalue_mult * solver.eigenvalues()[1]) {
141 PCL_DEBUG(
"[pcl::NormalDist::estimateParams] NDT normal fit: adjusting "
143 solver.eigenvalues()[0]);
144 Eigen::Matrix2d l = solver.eigenvalues().asDiagonal();
145 Eigen::Matrix2d q = solver.eigenvectors();
147 l(0, 0) = l(1, 1) * min_covar_eigvalue_mult;
148 covar = q * l * q.transpose();
167 const double& cos_theta,
168 const double& sin_theta)
const
174 const double x = transformed_pt.x;
175 const double y = transformed_pt.y;
176 const Eigen::Vector2d p_xy(transformed_pt.x, transformed_pt.y);
177 const Eigen::Vector2d q = p_xy -
mean_;
178 const Eigen::RowVector2d qt_cvi(q.transpose() *
covar_inv_);
179 const double exp_qt_cvi_q = std::exp(-0.5 *
static_cast<double>(qt_cvi * q));
180 r.
value = -exp_qt_cvi_q;
182 Eigen::Matrix<double, 2, 3> jacobian;
183 jacobian << 1, 0, -(x * sin_theta + y * cos_theta), 0, 1,
184 x * cos_theta - y * sin_theta;
186 for (std::size_t i = 0; i < 3; i++)
187 r.
grad[i] =
static_cast<double>(qt_cvi * jacobian.col(i)) * exp_qt_cvi_q;
190 const Eigen::Vector2d d2q_didj(y * sin_theta - x * cos_theta,
191 -(x * sin_theta + y * cos_theta));
193 for (std::size_t i = 0; i < 3; i++)
194 for (std::size_t j = 0; j < 3; j++)
197 (
static_cast<double>(-qt_cvi * jacobian.col(i)) *
198 static_cast<double>(-qt_cvi * jacobian.col(j)) +
199 (-qt_cvi * ((i == 2 && j == 2) ? d2q_didj : Eigen::Vector2d::Zero())) +
200 (-jacobian.col(j).transpose() *
covar_inv_ * jacobian.col(i)));
218 template <
typename Po
intT>
226 const Eigen::Vector2f& about,
227 const Eigen::Vector2f& extent,
228 const Eigen::Vector2f& step)
229 :
min_(about - extent)
236 std::size_t used_points = 0;
237 for (std::size_t i = 0; i < cloud->size(); i++)
243 PCL_DEBUG(
"[pcl::NDTSingleGrid] NDT single grid %dx%d using %d/%d points\n",
251 for (
int x = 0; x <
cells_[0]; x++)
252 for (
int y = 0; y <
cells_[1]; y++)
265 const double& cos_theta,
266 const double& sin_theta)
const
272 return n->
test(transformed_pt, cos_theta, sin_theta);
284 Eigen::Vector2f idxf;
285 for (std::size_t i = 0; i < 2; i++)
286 idxf[i] = (p.getVector3fMap()[i] -
min_[i]) /
step_[i];
287 Eigen::Vector2i idxi = idxf.cast<
int>();
288 for (std::size_t i = 0; i < 2; i++)
289 if (idxi[i] >=
cells_[i] || idxi[i] < 0)
310 template <
typename Po
intT>
311 class NDT2D :
public boost::noncopyable {
324 const Eigen::Vector2f& about,
325 const Eigen::Vector2f& extent,
326 const Eigen::Vector2f& step)
328 Eigen::Vector2f dx(step[0] / 2, 0);
329 Eigen::Vector2f dy(0, step[1] / 2);
345 const double& cos_theta,
346 const double& sin_theta)
const
350 r += single_grid->test(transformed_pt, cos_theta, sin_theta);
366 template <
typename Po
intT>
367 struct NumTraits<
pcl::ndt2d::NormalDist<PointT>> {
379 RequireInitialization = 1,
390 template <
typename Po
intSource,
typename Po
intTarget>
393 PointCloudSource& output,
const Eigen::Matrix4f& guess)
395 PointCloudSource intm_cloud = output;
400 if (guess != Eigen::Matrix4f::Identity()) {
401 transformation_ = guess;
410 Eigen::Matrix4f& transformation = transformation_;
414 const Eigen::Matrix3f initial_rot(transformation.block<3, 3>(0, 0));
415 const Eigen::Vector3f rot_x(initial_rot * Eigen::Vector3f::UnitX());
416 const double z_rotation = std::atan2(rot_x[1], rot_x[0]);
418 Eigen::Vector3d xytheta_transformation(
419 transformation(0, 3), transformation(1, 3), z_rotation);
421 while (!converged_) {
422 const double cos_theta = std::cos(xytheta_transformation[2]);
423 const double sin_theta = std::sin(xytheta_transformation[2]);
424 previous_transformation_ = transformation;
428 for (std::size_t i = 0; i < intm_cloud.size(); i++)
429 score += target_ndt.
test(intm_cloud[i], cos_theta, sin_theta);
431 PCL_DEBUG(
"[pcl::NormalDistributionsTransform2D::computeTransformation] NDT score "
432 "%f (x=%f,y=%f,r=%f)\n",
434 xytheta_transformation[0],
435 xytheta_transformation[1],
436 xytheta_transformation[2]);
438 if (score.
value != 0) {
440 Eigen::EigenSolver<Eigen::Matrix3d> solver;
441 solver.compute(score.
hessian,
false);
442 double min_eigenvalue = 0;
443 for (
int i = 0; i < 3; i++)
444 if (solver.eigenvalues()[i].real() < min_eigenvalue)
445 min_eigenvalue = solver.eigenvalues()[i].real();
449 if (min_eigenvalue < 0) {
450 double lambda = 1.1 * min_eigenvalue - 1;
451 score.
hessian += Eigen::Vector3d(-lambda, -lambda, -lambda).asDiagonal();
452 solver.compute(score.
hessian,
false);
453 PCL_DEBUG(
"[pcl::NormalDistributionsTransform2D::computeTransformation] adjust "
454 "hessian: %f: new eigenvalues:%f %f %f\n",
456 solver.eigenvalues()[0].real(),
457 solver.eigenvalues()[1].real(),
458 solver.eigenvalues()[2].real());
460 assert(solver.eigenvalues()[0].real() >= 0 &&
461 solver.eigenvalues()[1].real() >= 0 &&
462 solver.eigenvalues()[2].real() >= 0);
464 Eigen::Vector3d delta_transformation(-score.
hessian.inverse() * score.
grad);
465 Eigen::Vector3d new_transformation =
466 xytheta_transformation + newton_lambda_.cwiseProduct(delta_transformation);
468 xytheta_transformation = new_transformation;
471 transformation.block<3, 3>(0, 0).matrix() = Eigen::Matrix3f(Eigen::AngleAxisf(
472 static_cast<float>(xytheta_transformation[2]), Eigen::Vector3f::UnitZ()));
473 transformation.block<3, 1>(0, 3).matrix() =
474 Eigen::Vector3f(
static_cast<float>(xytheta_transformation[0]),
475 static_cast<float>(xytheta_transformation[1]),
481 PCL_ERROR(
"[pcl::NormalDistributionsTransform2D::computeTransformation] no "
482 "overlap: try increasing the size or reducing the step of the grid\n");
490 if (update_visualizer_)
491 update_visualizer_(output, *indices_, *target_, *indices_);
496 Eigen::Matrix4f transformation_delta =
497 transformation.inverse() * previous_transformation_;
499 0.5 * (transformation_delta.coeff(0, 0) + transformation_delta.coeff(1, 1) +
500 transformation_delta.coeff(2, 2) - 1);
501 double translation_sqr =
502 transformation_delta.coeff(0, 3) * transformation_delta.coeff(0, 3) +
503 transformation_delta.coeff(1, 3) * transformation_delta.coeff(1, 3) +
504 transformation_delta.coeff(2, 3) * transformation_delta.coeff(2, 3);
506 if (nr_iterations_ >= max_iterations_ ||
507 ((transformation_epsilon_ > 0 && translation_sqr <= transformation_epsilon_) &&
508 (transformation_rotation_epsilon_ > 0 &&
509 cos_angle >= transformation_rotation_epsilon_)) ||
510 ((transformation_epsilon_ <= 0) &&
511 (transformation_rotation_epsilon_ > 0 &&
512 cos_angle >= transformation_rotation_epsilon_)) ||
513 ((transformation_epsilon_ > 0 && translation_sqr <= transformation_epsilon_) &&
514 (transformation_rotation_epsilon_ <= 0))) {
518 final_transformation_ = transformation;
PointCloud represents the base class in PCL for storing collections of 3D points.
shared_ptr< const PointCloud< PointT > > ConstPtr
Build a Normal Distributions Transform of a 2D point cloud.
ValueAndDerivatives< 3, double > test(const PointT &transformed_pt, const double &cos_theta, const double &sin_theta) const
Return the 'score' (denormalised likelihood) and derivatives of score of the point p given this distr...
std::shared_ptr< SingleGrid > single_grids_[4]
NDT2D(PointCloudConstPtr cloud, const Eigen::Vector2f &about, const Eigen::Vector2f &extent, const Eigen::Vector2f &step)
Build a set of normal distributions modelling a 2D point cloud, and provide the value and derivatives...
NormalDist * normalDistForPoint(PointT const &p) const
Return the normal distribution covering the location of point p.
ValueAndDerivatives< 3, double > test(const PointT &transformed_pt, const double &cos_theta, const double &sin_theta) const
Return the 'score' (denormalised likelihood) and derivatives of score of the point p given this distr...
NDTSingleGrid(PointCloudConstPtr cloud, const Eigen::Vector2f &about, const Eigen::Vector2f &extent, const Eigen::Vector2f &step)
Eigen::Matrix< NormalDist, Eigen::Dynamic, Eigen::Dynamic > normal_distributions_
A normal distribution estimation class.
std::vector< std::size_t > pt_indices_
ValueAndDerivatives< 3, double > test(const PointT &transformed_pt, const double &cos_theta, const double &sin_theta) const
Return the 'score' (denormalised likelihood) and derivatives of score of the point p given this distr...
void addIdx(std::size_t i)
Store a point index to use later for estimating distribution parameters.
void estimateParams(const PointCloud &cloud, double min_covar_eigvalue_mult=0.001)
Estimate the normal distribution parameters given the point indices provided.
Eigen::Matrix2d covar_inv_
void transformPointCloud(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const Eigen::Matrix< Scalar, 4, 4 > &transform, bool copy_all_fields)
Apply a rigid transform defined by a 4x4 matrix.
static Real dummy_precision()
A point structure representing Euclidean xyz coordinates, and the RGB color.
Class to store vector value and first and second derivatives (grad vector and hessian matrix),...
Eigen::Matrix< T, N, N > hessian
static ValueAndDerivatives< N, T > Zero()
ValueAndDerivatives< N, T > & operator+=(ValueAndDerivatives< N, T > const &r)
Eigen::Matrix< T, N, 1 > grad