41 #ifndef PCL_NDT_2D_IMPL_H_
42 #define PCL_NDT_2D_IMPL_H_
44 #include <pcl/registration/eigen.h>
45 #include <pcl/registration/boost.h>
59 template<
unsigned N=3,
typename T=
double>
65 Eigen::Matrix<T, N, 1>
grad;
72 r.
hessian = Eigen::Matrix<T, N, N>::Zero ();
73 r.
grad = Eigen::Matrix<T, N, 1>::Zero ();
99 template <
typename Po
intT>
126 Eigen::Vector2d sx = Eigen::Vector2d::Zero ();
127 Eigen::Matrix2d sxx = Eigen::Matrix2d::Zero ();
131 Eigen::Vector2d p (cloud[*i]. x, cloud[*i]. y);
133 sxx += p * p.transpose ();
140 mean_ = sx /
static_cast<double> (
n_);
142 Eigen::Matrix2d covar = (sxx - 2 * (sx *
mean_.transpose ())) /
static_cast<double> (
n_) +
mean_ *
mean_.transpose ();
144 Eigen::SelfAdjointEigenSolver<Eigen::Matrix2d> solver (covar);
145 if (solver.eigenvalues ()[0] < min_covar_eigvalue_mult * solver.eigenvalues ()[1])
147 PCL_DEBUG (
"[pcl::NormalDist::estimateParams] NDT normal fit: adjusting eigenvalue %f\n", solver.eigenvalues ()[0]);
148 Eigen::Matrix2d l = solver.eigenvalues ().asDiagonal ();
149 Eigen::Matrix2d q = solver.eigenvectors ();
151 l (0,0) = l (1,1) * min_covar_eigvalue_mult;
152 covar = q * l * q.transpose ();
168 test (
const PointT& transformed_pt,
const double& cos_theta,
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 *
double (qt_cvi * q));
180 r.
value = -exp_qt_cvi_q;
182 Eigen::Matrix<double, 2, 3> jacobian;
184 1, 0, -(x * sin_theta + y*cos_theta),
185 0, 1, x * cos_theta - y*sin_theta;
187 for (std::size_t i = 0; i < 3; i++)
188 r.
grad[i] = double (qt_cvi * jacobian.col (i)) * exp_qt_cvi_q;
191 const Eigen::Vector2d d2q_didj (
192 y * sin_theta - x*cos_theta,
193 -(x * sin_theta + y*cos_theta)
196 for (std::size_t i = 0; i < 3; i++)
197 for (std::size_t j = 0; j < 3; j++)
198 r.
hessian (i,j) = -exp_qt_cvi_q * (
199 double (-qt_cvi*jacobian.col (i)) *
double (-qt_cvi*jacobian.col (j)) +
200 (-qt_cvi * ((i==2 && j==2)? d2q_didj : Eigen::Vector2d::Zero ())) +
201 (-jacobian.col (j).transpose () *
covar_inv_ * jacobian.col (i))
220 template <
typename Po
intT>
229 const Eigen::Vector2f& about,
230 const Eigen::Vector2f& extent,
231 const Eigen::Vector2f& step)
238 std::size_t used_points = 0;
239 for (std::size_t i = 0; i < cloud->size (); i++)
246 PCL_DEBUG (
"[pcl::NDTSingleGrid] NDT single grid %dx%d using %d/%d points\n",
cells_[0],
cells_[1], used_points, cloud->size ());
250 for (
int x = 0; x <
cells_[0]; x++)
251 for (
int y = 0; y < cells_[1]; y++)
261 test (
const PointT& transformed_pt,
const double& cos_theta,
const double& sin_theta)
const
267 return n->
test (transformed_pt, cos_theta, sin_theta);
279 Eigen::Vector2f idxf;
280 for (std::size_t i = 0; i < 2; i++)
281 idxf[i] = (p.getVector3fMap ()[i] -
min_[i]) /
step_[i];
282 Eigen::Vector2i idxi = idxf.cast<
int> ();
283 for (std::size_t i = 0; i < 2; i++)
284 if (idxi[i] >=
cells_[i] || idxi[i] < 0)
305 template <
typename Po
intT>
306 class NDT2D:
public boost::noncopyable
320 const Eigen::Vector2f& about,
321 const Eigen::Vector2f& extent,
322 const Eigen::Vector2f& step)
324 Eigen::Vector2f dx (step[0]/2, 0);
325 Eigen::Vector2f dy (0, step[1]/2);
338 test (
const PointT& transformed_pt,
const double& cos_theta,
const double& sin_theta)
const
342 r += single_grid->test (transformed_pt, cos_theta, sin_theta);
360 template<
typename Po
intT>
361 struct NumTraits<
pcl::ndt2d::NormalDist<PointT> >
371 RequireInitialization = 1,
384 template <
typename Po
intSource,
typename Po
intTarget>
void
387 PointCloudSource intm_cloud = output;
392 if (guess != Eigen::Matrix4f::Identity ())
394 transformation_ = guess;
403 Eigen::Matrix4f& transformation = transformation_;
408 const Eigen::Matrix3f initial_rot (transformation.block<3,3> (0,0));
409 const Eigen::Vector3f rot_x (initial_rot*Eigen::Vector3f::UnitX ());
410 const double z_rotation = std::atan2 (rot_x[1], rot_x[0]);
412 Eigen::Vector3d xytheta_transformation (
413 transformation (0,3),
414 transformation (1,3),
420 const double cos_theta = std::cos (xytheta_transformation[2]);
421 const double sin_theta = std::sin (xytheta_transformation[2]);
422 previous_transformation_ = transformation;
425 for (std::size_t i = 0; i < intm_cloud.size (); i++)
426 score += target_ndt.
test (intm_cloud[i], cos_theta, sin_theta);
428 PCL_DEBUG (
"[pcl::NormalDistributionsTransform2D::computeTransformation] NDT score %f (x=%f,y=%f,r=%f)\n",
429 float (score.
value), xytheta_transformation[0], xytheta_transformation[1], xytheta_transformation[2]
432 if (score.
value != 0)
435 Eigen::EigenSolver<Eigen::Matrix3d> solver;
436 solver.compute (score.
hessian,
false);
437 double min_eigenvalue = 0;
438 for (
int i = 0; i <3; i++)
439 if (solver.eigenvalues ()[i].real () < min_eigenvalue)
440 min_eigenvalue = solver.eigenvalues ()[i].real ();
444 if (min_eigenvalue < 0)
446 double lambda = 1.1 * min_eigenvalue - 1;
447 score.
hessian += Eigen::Vector3d (-lambda, -lambda, -lambda).asDiagonal ();
448 solver.compute (score.
hessian,
false);
449 PCL_DEBUG (
"[pcl::NormalDistributionsTransform2D::computeTransformation] adjust hessian: %f: new eigenvalues:%f %f %f\n",
451 solver.eigenvalues ()[0].real (),
452 solver.eigenvalues ()[1].real (),
453 solver.eigenvalues ()[2].real ()
456 assert (solver.eigenvalues ()[0].real () >= 0 &&
457 solver.eigenvalues ()[1].real () >= 0 &&
458 solver.eigenvalues ()[2].real () >= 0);
460 Eigen::Vector3d delta_transformation (-score.
hessian.inverse () * score.
grad);
461 Eigen::Vector3d new_transformation = xytheta_transformation + newton_lambda_.cwiseProduct (delta_transformation);
463 xytheta_transformation = new_transformation;
466 transformation.block<3,3> (0,0).matrix () = Eigen::Matrix3f (Eigen::AngleAxisf (static_cast<float> (xytheta_transformation[2]), Eigen::Vector3f::UnitZ ()));
467 transformation.block<3,1> (0,3).matrix () = Eigen::Vector3f (static_cast<float> (xytheta_transformation[0]), static_cast<float> (xytheta_transformation[1]), 0.0f);
473 PCL_ERROR (
"[pcl::NormalDistributionsTransform2D::computeTransformation] no overlap: try increasing the size or reducing the step of the grid\n");
481 if (update_visualizer_)
482 update_visualizer_ (output, *indices_, *target_, *indices_);
486 Eigen::Matrix4f transformation_delta = transformation.inverse() * previous_transformation_;
487 double cos_angle = 0.5 * (transformation_delta.coeff (0, 0) + transformation_delta.coeff (1, 1) + transformation_delta.coeff (2, 2) - 1);
488 double translation_sqr = transformation_delta.coeff (0, 3) * transformation_delta.coeff (0, 3) +
489 transformation_delta.coeff (1, 3) * transformation_delta.coeff (1, 3) +
490 transformation_delta.coeff (2, 3) * transformation_delta.coeff (2, 3);
492 if (nr_iterations_ >= max_iterations_ ||
493 ((transformation_epsilon_ > 0 && translation_sqr <= transformation_epsilon_) && (transformation_rotation_epsilon_ > 0 && cos_angle >= transformation_rotation_epsilon_)) ||
494 ((transformation_epsilon_ <= 0) && (transformation_rotation_epsilon_ > 0 && cos_angle >= transformation_rotation_epsilon_)) ||
495 ((transformation_epsilon_ > 0 && translation_sqr <= transformation_epsilon_) && (transformation_rotation_epsilon_ <= 0)))
500 final_transformation_ = transformation;
506 #endif // PCL_NDT_2D_IMPL_H_
NDT2D(PointCloudConstPtr cloud, const Eigen::Vector2f &about, const Eigen::Vector2f &extent, const Eigen::Vector2f &step)
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...
ValueAndDerivatives< N, T > & operator+=(ValueAndDerivatives< N, T > const &r)
void estimateParams(const PointCloud &cloud, double min_covar_eigvalue_mult=0.001)
Estimate the normal distribution parameters given the point indices provided.
Eigen::Matrix< T, N, 1 > grad
static Real dummy_precision()
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...
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...
std::vector< std::size_t > pt_indices_
A normal distribution estimation class.
void transformPointCloud(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const Eigen::Transform< Scalar, 3, Eigen::Affine > &transform, bool copy_all_fields)
Apply an affine transform defined by an Eigen Transform.
Build a Normal Distributions Transform of a 2D point cloud.
Eigen::Matrix< NormalDist, Eigen::Dynamic, Eigen::Dynamic > normal_distributions_
PointCloud represents the base class in PCL for storing collections of 3D points. ...
shared_ptr< const PointCloud< PointT > > ConstPtr
static ValueAndDerivatives< N, T > Zero()
void addIdx(std::size_t i)
Store a point index to use later for estimating distribution parameters.
Eigen::Matrix2d covar_inv_
Class to store vector value and first and second derivatives (grad vector and hessian matrix)...
A point structure representing Euclidean xyz coordinates, and the RGB color.
NDTSingleGrid(PointCloudConstPtr cloud, const Eigen::Vector2f &about, const Eigen::Vector2f &extent, const Eigen::Vector2f &step)
Eigen::Matrix< T, N, N > hessian
Build a set of normal distributions modelling a 2D point cloud, and provide the value and derivatives...
std::shared_ptr< SingleGrid > single_grids_[4]