41#ifndef PCL_NDT_2D_IMPL_H_
42#define PCL_NDT_2D_IMPL_H_
44#include <boost/core/noncopyable.hpp>
46#include <Eigen/Eigenvalues>
58template <
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();
97template <
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.noalias() += 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.noalias() = q * l * q.transpose();
166 test(
const PointT& transformed_pt,
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)));
218template <
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++)
264 test(
const PointT& transformed_pt,
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)
310template <
typename Po
intT>
311class 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);
330 single_grids_[0].reset(
new SingleGrid(cloud, about, extent, step));
331 single_grids_[1].reset(
new SingleGrid(cloud, about + dx, extent, step));
332 single_grids_[2].reset(
new SingleGrid(cloud, about + dy, extent, step));
333 single_grids_[3].reset(
new SingleGrid(cloud, about + dx + dy, extent, step));
344 test(
const PointT& transformed_pt,
345 const double& cos_theta,
346 const double& sin_theta)
const
350 r += single_grid->test(transformed_pt, cos_theta, sin_theta);
366template <
typename Po
intT>
367struct NumTraits<
pcl::ndt2d::NormalDist<PointT>> {
390template <
typename Po
intSource,
typename Po
intTarget>
393 PointCloudSource& output,
const Eigen::Matrix4f& guess)
395 PointCloudSource intm_cloud = output;
400 if (guess != Eigen::Matrix4f::Identity()) {
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);
422 const double cos_theta = std::cos(xytheta_transformation[2]);
423 const double sin_theta = std::sin(xytheta_transformation[2]);
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::SelfAdjointEigenSolver<Eigen::Matrix3d> solver(score.
hessian,
441 Eigen::EigenvaluesOnly);
442 double min_eigenvalue = 0;
443 for (
int i = 0; i < 3; i++)
444 if (solver.eigenvalues()[i] < min_eigenvalue)
445 min_eigenvalue = solver.eigenvalues()[i];
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, Eigen::EigenvaluesOnly);
453 PCL_DEBUG(
"[pcl::NormalDistributionsTransform2D::computeTransformation] adjust "
454 "hessian: %f: new eigenvalues:%f %f %f\n",
456 solver.eigenvalues()[0],
457 solver.eigenvalues()[1],
458 solver.eigenvalues()[2]);
460 assert(solver.eigenvalues()[0] >= 0 && solver.eigenvalues()[1] >= 0 &&
461 solver.eigenvalues()[2] >= 0);
463 Eigen::Vector3d delta_transformation(-score.
hessian.inverse() * score.
grad);
464 Eigen::Vector3d new_transformation =
465 xytheta_transformation +
newton_lambda_.cwiseProduct(delta_transformation);
467 xytheta_transformation = new_transformation;
470 transformation.block<3, 3>(0, 0).matrix() = Eigen::Matrix3f(Eigen::AngleAxisf(
471 static_cast<float>(xytheta_transformation[2]), Eigen::Vector3f::UnitZ()));
472 transformation.block<3, 1>(0, 3).matrix() =
473 Eigen::Vector3f(
static_cast<float>(xytheta_transformation[0]),
474 static_cast<float>(xytheta_transformation[1]),
480 PCL_ERROR(
"[pcl::NormalDistributionsTransform2D::computeTransformation] no "
481 "overlap: try increasing the size or reducing the step of the grid\n");
495 Eigen::Matrix4f transformation_delta =
498 0.5 * (transformation_delta.coeff(0, 0) + transformation_delta.coeff(1, 1) +
499 transformation_delta.coeff(2, 2) - 1);
500 double translation_sqr =
501 transformation_delta.coeff(0, 3) * transformation_delta.coeff(0, 3) +
502 transformation_delta.coeff(1, 3) * transformation_delta.coeff(1, 3) +
503 transformation_delta.coeff(2, 3) * transformation_delta.coeff(2, 3);
PointCloud represents the base class in PCL for storing collections of 3D points.
shared_ptr< const PointCloud< PointT > > ConstPtr
Matrix4 final_transformation_
The final transformation matrix estimated by the registration method after N iterations.
std::function< UpdateVisualizerCallbackSignature > update_visualizer_
Callback function to update intermediate source point cloud position during it's registration to the ...
Matrix4 transformation_
The transformation matrix estimated by the registration method.
int nr_iterations_
The number of iterations the internal optimization ran for (used internally).
Matrix4 previous_transformation_
The previous transformation matrix estimated by the registration method (used internally).
bool converged_
Holds internal convergence state, given user parameters.
int max_iterations_
The maximum number of iterations the internal optimization should run for.
double transformation_rotation_epsilon_
The maximum rotation difference between two consecutive transformations in order to consider converge...
double transformation_epsilon_
The maximum difference between two consecutive transformations in order to consider convergence (user...
PointCloudTargetConstPtr target_
The input point cloud dataset target.
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.
NDTSingleGrid(PointCloudConstPtr cloud, const Eigen::Vector2f &about, const Eigen::Vector2f &extent, const Eigen::Vector2f &step)
Eigen::Matrix< NormalDist, Eigen::Dynamic, Eigen::Dynamic > normal_distributions_
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...
A normal distribution estimation class.
std::vector< std::size_t > pt_indices_
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.
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...
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()
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