41 #ifndef PCL_REGISTRATION_NDT_IMPL_H_
42 #define PCL_REGISTRATION_NDT_IMPL_H_
45 template<
typename Po
intSource,
typename Po
intTarget>
50 , outlier_ratio_ (0.55)
53 , trans_probability_ ()
55 reg_name_ =
"NormalDistributionsTransform";
57 double gauss_c1, gauss_c2, gauss_d3;
62 gauss_d3 = -std::log (gauss_c2);
63 gauss_d1_ = -std::log ( gauss_c1 + gauss_c2 ) - gauss_d3;
64 gauss_d2_ = -2 * std::log ((-std::log ( gauss_c1 * std::exp ( -0.5 ) + gauss_c2 ) - gauss_d3) /
gauss_d1_);
71 template<
typename Po
intSource,
typename Po
intTarget>
void
77 double gauss_c1, gauss_c2, gauss_d3;
80 gauss_c1 = 10 * (1 - outlier_ratio_);
81 gauss_c2 = outlier_ratio_ / pow (resolution_, 3);
82 gauss_d3 = -std::log (gauss_c2);
83 gauss_d1_ = -std::log ( gauss_c1 + gauss_c2 ) - gauss_d3;
84 gauss_d2_ = -2 * std::log ((-std::log ( gauss_c1 * std::exp ( -0.5 ) + gauss_c2 ) - gauss_d3) / gauss_d1_);
86 if (guess != Eigen::Matrix4f::Identity ())
89 final_transformation_ = guess;
95 point_gradient_.setZero ();
96 point_gradient_.block<3, 3>(0, 0).setIdentity ();
97 point_hessian_.setZero ();
99 Eigen::Transform<float, 3, Eigen::Affine, Eigen::ColMajor> eig_transformation;
100 eig_transformation.matrix () = final_transformation_;
103 Eigen::Matrix<double, 6, 1> p, delta_p, score_gradient;
104 Eigen::Vector3f init_translation = eig_transformation.translation ();
105 Eigen::Vector3f init_rotation = eig_transformation.rotation ().eulerAngles (0, 1, 2);
106 p << init_translation (0), init_translation (1), init_translation (2),
107 init_rotation (0), init_rotation (1), init_rotation (2);
109 Eigen::Matrix<double, 6, 6> hessian;
115 score = computeDerivatives (score_gradient, hessian, output, p);
120 previous_transformation_ = transformation_;
123 Eigen::JacobiSVD<Eigen::Matrix<double, 6, 6> > sv (hessian, Eigen::ComputeFullU | Eigen::ComputeFullV);
125 delta_p = sv.solve (-score_gradient);
128 delta_p_norm = delta_p.norm ();
130 if (delta_p_norm == 0 || std::isnan(delta_p_norm))
132 trans_probability_ = score / static_cast<double> (input_->points.size ());
133 converged_ = delta_p_norm == delta_p_norm;
137 delta_p.normalize ();
138 delta_p_norm = computeStepLengthMT (p, delta_p, delta_p_norm, step_size_, transformation_epsilon_ / 2, score, score_gradient, hessian, output);
139 delta_p *= delta_p_norm;
142 transformation_ = (Eigen::Translation<float, 3> (static_cast<float> (delta_p (0)), static_cast<float> (delta_p (1)), static_cast<float> (delta_p (2))) *
143 Eigen::AngleAxis<float> (static_cast<float> (delta_p (3)), Eigen::Vector3f::UnitX ()) *
144 Eigen::AngleAxis<float> (static_cast<float> (delta_p (4)), Eigen::Vector3f::UnitY ()) *
145 Eigen::AngleAxis<float> (static_cast<float> (delta_p (5)), Eigen::Vector3f::UnitZ ())).matrix ();
151 if (update_visualizer_)
152 update_visualizer_ (output, std::vector<int>(), *target_, std::vector<int>() );
154 double cos_angle = 0.5 * (transformation_.coeff (0, 0) + transformation_.coeff (1, 1) + transformation_.coeff (2, 2) - 1);
155 double translation_sqr = transformation_.coeff (0, 3) * transformation_.coeff (0, 3) +
156 transformation_.coeff (1, 3) * transformation_.coeff (1, 3) +
157 transformation_.coeff (2, 3) * transformation_.coeff (2, 3);
161 if (nr_iterations_ >= max_iterations_ ||
162 ((transformation_epsilon_ > 0 && translation_sqr <= transformation_epsilon_) && (transformation_rotation_epsilon_ > 0 && cos_angle >= transformation_rotation_epsilon_)) ||
163 ((transformation_epsilon_ <= 0) && (transformation_rotation_epsilon_ > 0 && cos_angle >= transformation_rotation_epsilon_)) ||
164 ((transformation_epsilon_ > 0 && translation_sqr <= transformation_epsilon_) && (transformation_rotation_epsilon_ <= 0)))
172 trans_probability_ = score / static_cast<double> (input_->points.size ());
176 template<
typename Po
intSource,
typename Po
intTarget>
double
178 Eigen::Matrix<double, 6, 6> &hessian,
180 Eigen::Matrix<double, 6, 1> &p,
181 bool compute_hessian)
184 PointSource x_pt, x_trans_pt;
186 Eigen::Vector3d x, x_trans;
190 Eigen::Matrix3d c_inv;
192 score_gradient.setZero ();
197 computeAngleDerivatives (p);
200 for (std::size_t idx = 0; idx < input_->points.size (); idx++)
202 x_trans_pt = trans_cloud.points[idx];
205 std::vector<TargetGridLeafConstPtr> neighborhood;
206 std::vector<float> distances;
207 target_cells_.radiusSearch (x_trans_pt, resolution_, neighborhood, distances);
209 for (
typename std::vector<TargetGridLeafConstPtr>::iterator neighborhood_it = neighborhood.begin (); neighborhood_it != neighborhood.end (); neighborhood_it++)
211 cell = *neighborhood_it;
212 x_pt = input_->points[idx];
213 x = Eigen::Vector3d (x_pt.x, x_pt.y, x_pt.z);
215 x_trans = Eigen::Vector3d (x_trans_pt.x, x_trans_pt.y, x_trans_pt.z);
218 x_trans -= cell->getMean ();
220 c_inv = cell->getInverseCov ();
223 computePointDerivatives (x);
225 score += updateDerivatives (score_gradient, hessian, x_trans, c_inv, compute_hessian);
233 template<
typename Po
intSource,
typename Po
intTarget>
void
237 double cx, cy, cz, sx, sy, sz;
238 if (std::abs (p (3)) < 10e-5)
246 cx = std::cos (p (3));
249 if (std::abs (p (4)) < 10e-5)
257 cy = std::cos (p (4));
261 if (std::abs (p (5)) < 10e-5)
269 cz = std::cos (p (5));
274 j_ang_a_ << (-sx * sz + cx * sy * cz), (-sx * cz - cx * sy * sz), (-cx * cy);
275 j_ang_b_ << (cx * sz + sx * sy * cz), (cx * cz - sx * sy * sz), (-sx * cy);
276 j_ang_c_ << (-sy * cz), sy * sz, cy;
277 j_ang_d_ << sx * cy * cz, (-sx * cy * sz), sx * sy;
278 j_ang_e_ << (-cx * cy * cz), cx * cy * sz, (-cx * sy);
279 j_ang_f_ << (-cy * sz), (-cy * cz), 0;
280 j_ang_g_ << (cx * cz - sx * sy * sz), (-cx * sz - sx * sy * cz), 0;
281 j_ang_h_ << (sx * cz + cx * sy * sz), (cx * sy * cz - sx * sz), 0;
286 h_ang_a2_ << (-cx * sz - sx * sy * cz), (-cx * cz + sx * sy * sz), sx * cy;
287 h_ang_a3_ << (-sx * sz + cx * sy * cz), (-cx * sy * sz - sx * cz), (-cx * cy);
289 h_ang_b2_ << (cx * cy * cz), (-cx * cy * sz), (cx * sy);
290 h_ang_b3_ << (sx * cy * cz), (-sx * cy * sz), (sx * sy);
292 h_ang_c2_ << (-sx * cz - cx * sy * sz), (sx * sz - cx * sy * cz), 0;
293 h_ang_c3_ << (cx * cz - sx * sy * sz), (-sx * sy * cz - cx * sz), 0;
295 h_ang_d1_ << (-cy * cz), (cy * sz), (sy);
296 h_ang_d2_ << (-sx * sy * cz), (sx * sy * sz), (sx * cy);
297 h_ang_d3_ << (cx * sy * cz), (-cx * sy * sz), (-cx * cy);
299 h_ang_e1_ << (sy * sz), (sy * cz), 0;
300 h_ang_e2_ << (-sx * cy * sz), (-sx * cy * cz), 0;
301 h_ang_e3_ << (cx * cy * sz), (cx * cy * cz), 0;
303 h_ang_f1_ << (-cy * cz), (cy * sz), 0;
304 h_ang_f2_ << (-cx * sz - sx * sy * cz), (-cx * cz + sx * sy * sz), 0;
305 h_ang_f3_ << (-sx * sz + cx * sy * cz), (-cx * sy * sz - sx * cz), 0;
310 template<
typename Po
intSource,
typename Po
intTarget>
void
315 point_gradient_ (1, 3) = x.dot (j_ang_a_);
316 point_gradient_ (2, 3) = x.dot (j_ang_b_);
317 point_gradient_ (0, 4) = x.dot (j_ang_c_);
318 point_gradient_ (1, 4) = x.dot (j_ang_d_);
319 point_gradient_ (2, 4) = x.dot (j_ang_e_);
320 point_gradient_ (0, 5) = x.dot (j_ang_f_);
321 point_gradient_ (1, 5) = x.dot (j_ang_g_);
322 point_gradient_ (2, 5) = x.dot (j_ang_h_);
327 Eigen::Vector3d a, b, c, d, e, f;
329 a << 0, x.dot (h_ang_a2_), x.dot (h_ang_a3_);
330 b << 0, x.dot (h_ang_b2_), x.dot (h_ang_b3_);
331 c << 0, x.dot (h_ang_c2_), x.dot (h_ang_c3_);
332 d << x.dot (h_ang_d1_), x.dot (h_ang_d2_), x.dot (h_ang_d3_);
333 e << x.dot (h_ang_e1_), x.dot (h_ang_e2_), x.dot (h_ang_e3_);
334 f << x.dot (h_ang_f1_), x.dot (h_ang_f2_), x.dot (h_ang_f3_);
338 point_hessian_.block<3, 1>(9, 3) = a;
339 point_hessian_.block<3, 1>(12, 3) = b;
340 point_hessian_.block<3, 1>(15, 3) = c;
341 point_hessian_.block<3, 1>(9, 4) = b;
342 point_hessian_.block<3, 1>(12, 4) = d;
343 point_hessian_.block<3, 1>(15, 4) = e;
344 point_hessian_.block<3, 1>(9, 5) = c;
345 point_hessian_.block<3, 1>(12, 5) = e;
346 point_hessian_.block<3, 1>(15, 5) = f;
351 template<
typename Po
intSource,
typename Po
intTarget>
double
353 Eigen::Matrix<double, 6, 6> &hessian,
354 Eigen::Vector3d &x_trans, Eigen::Matrix3d &c_inv,
355 bool compute_hessian)
357 Eigen::Vector3d cov_dxd_pi;
359 double e_x_cov_x = std::exp (-gauss_d2_ * x_trans.dot (c_inv * x_trans) / 2);
361 double score_inc = -gauss_d1_ * e_x_cov_x;
363 e_x_cov_x = gauss_d2_ * e_x_cov_x;
366 if (e_x_cov_x > 1 || e_x_cov_x < 0 || std::isnan(e_x_cov_x))
370 e_x_cov_x *= gauss_d1_;
373 for (
int i = 0; i < 6; i++)
376 cov_dxd_pi = c_inv * point_gradient_.col (i);
379 score_gradient (i) += x_trans.dot (cov_dxd_pi) * e_x_cov_x;
383 for (Eigen::Index j = 0; j < hessian.cols (); j++)
386 hessian (i, j) += e_x_cov_x * (-gauss_d2_ * x_trans.dot (cov_dxd_pi) * x_trans.dot (c_inv * point_gradient_.col (j)) +
387 x_trans.dot (c_inv * point_hessian_.block<3, 1>(3 * i, j)) +
388 point_gradient_.col (j).dot (cov_dxd_pi) );
397 template<
typename Po
intSource,
typename Po
intTarget>
void
402 PointSource x_pt, x_trans_pt;
404 Eigen::Vector3d x, x_trans;
408 Eigen::Matrix3d c_inv;
415 for (std::size_t idx = 0; idx < input_->points.size (); idx++)
417 x_trans_pt = trans_cloud.points[idx];
420 std::vector<TargetGridLeafConstPtr> neighborhood;
421 std::vector<float> distances;
422 target_cells_.radiusSearch (x_trans_pt, resolution_, neighborhood, distances);
424 for (
typename std::vector<TargetGridLeafConstPtr>::iterator neighborhood_it = neighborhood.begin (); neighborhood_it != neighborhood.end (); neighborhood_it++)
426 cell = *neighborhood_it;
429 x_pt = input_->points[idx];
430 x = Eigen::Vector3d (x_pt.x, x_pt.y, x_pt.z);
432 x_trans = Eigen::Vector3d (x_trans_pt.x, x_trans_pt.y, x_trans_pt.z);
435 x_trans -= cell->getMean ();
437 c_inv = cell->getInverseCov ();
440 computePointDerivatives (x);
442 updateHessian (hessian, x_trans, c_inv);
449 template<
typename Po
intSource,
typename Po
intTarget>
void
452 Eigen::Vector3d cov_dxd_pi;
454 double e_x_cov_x = gauss_d2_ * std::exp (-gauss_d2_ * x_trans.dot (c_inv * x_trans) / 2);
457 if (e_x_cov_x > 1 || e_x_cov_x < 0 || std::isnan(e_x_cov_x))
461 e_x_cov_x *= gauss_d1_;
463 for (
int i = 0; i < 6; i++)
466 cov_dxd_pi = c_inv * point_gradient_.col (i);
468 for (Eigen::Index j = 0; j < hessian.cols (); j++)
471 hessian (i, j) += e_x_cov_x * (-gauss_d2_ * x_trans.dot (cov_dxd_pi) * x_trans.dot (c_inv * point_gradient_.col (j)) +
472 x_trans.dot (c_inv * point_hessian_.block<3, 1>(3 * i, j)) +
473 point_gradient_.col (j).dot (cov_dxd_pi) );
480 template<
typename Po
intSource,
typename Po
intTarget>
bool
482 double &a_u,
double &f_u,
double &g_u,
483 double a_t,
double f_t,
double g_t)
494 if (g_t * (a_l - a_t) > 0)
502 if (g_t * (a_l - a_t) < 0)
518 template<
typename Po
intSource,
typename Po
intTarget>
double
520 double a_u,
double f_u,
double g_u,
521 double a_t,
double f_t,
double g_t)
528 double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
529 double w = std::sqrt (z * z - g_t * g_l);
531 double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
535 double a_q = a_l - 0.5 * (a_l - a_t) * g_l / (g_l - (f_l - f_t) / (a_l - a_t));
537 if (std::fabs (a_c - a_l) < std::fabs (a_q - a_l))
539 return (0.5 * (a_q + a_c));
546 double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
547 double w = std::sqrt (z * z - g_t * g_l);
549 double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
553 double a_s = a_l - (a_l - a_t) / (g_l - g_t) * g_l;
555 if (std::fabs (a_c - a_t) >= std::fabs (a_s - a_t))
560 if (std::fabs (g_t) <= std::fabs (g_l))
564 double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
565 double w = std::sqrt (z * z - g_t * g_l);
566 double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
570 double a_s = a_l - (a_l - a_t) / (g_l - g_t) * g_l;
574 if (std::fabs (a_c - a_t) < std::fabs (a_s - a_t))
580 return (std::min (a_t + 0.66 * (a_u - a_t), a_t_next));
581 return (std::max (a_t + 0.66 * (a_u - a_t), a_t_next));
586 double z = 3 * (f_t - f_u) / (a_t - a_u) - g_t - g_u;
587 double w = std::sqrt (z * z - g_t * g_u);
589 return (a_u + (a_t - a_u) * (w - g_u - z) / (g_t - g_u + 2 * w));
593 template<
typename Po
intSource,
typename Po
intTarget>
double
595 double step_min,
double &score, Eigen::Matrix<double, 6, 1> &score_gradient, Eigen::Matrix<double, 6, 6> &hessian,
599 double phi_0 = -score;
601 double d_phi_0 = -(score_gradient.dot (step_dir));
603 Eigen::Matrix<double, 6, 1> x_t;
618 int max_step_iterations = 10;
619 int step_iterations = 0;
627 double a_l = 0, a_u = 0;
630 double f_l = auxilaryFunction_PsiMT (a_l, phi_0, phi_0, d_phi_0, mu);
631 double g_l = auxilaryFunction_dPsiMT (d_phi_0, d_phi_0, mu);
633 double f_u = auxilaryFunction_PsiMT (a_u, phi_0, phi_0, d_phi_0, mu);
634 double g_u = auxilaryFunction_dPsiMT (d_phi_0, d_phi_0, mu);
637 bool interval_converged = (step_max - step_min) > 0, open_interval =
true;
639 double a_t = step_init;
640 a_t = std::min (a_t, step_max);
641 a_t = std::max (a_t, step_min);
643 x_t = x + step_dir * a_t;
645 final_transformation_ = (Eigen::Translation<float, 3>(static_cast<float> (x_t (0)), static_cast<float> (x_t (1)), static_cast<float> (x_t (2))) *
646 Eigen::AngleAxis<float> (static_cast<float> (x_t (3)), Eigen::Vector3f::UnitX ()) *
647 Eigen::AngleAxis<float> (static_cast<float> (x_t (4)), Eigen::Vector3f::UnitY ()) *
648 Eigen::AngleAxis<float> (static_cast<float> (x_t (5)), Eigen::Vector3f::UnitZ ())).matrix ();
655 score = computeDerivatives (score_gradient, hessian, trans_cloud, x_t,
true);
658 double phi_t = -score;
660 double d_phi_t = -(score_gradient.dot (step_dir));
663 double psi_t = auxilaryFunction_PsiMT (a_t, phi_t, phi_0, d_phi_0, mu);
665 double d_psi_t = auxilaryFunction_dPsiMT (d_phi_t, d_phi_0, mu);
668 while (!interval_converged && step_iterations < max_step_iterations && !(psi_t <= 0 && d_phi_t <= -nu * d_phi_0 ))
673 a_t = trialValueSelectionMT (a_l, f_l, g_l,
675 a_t, psi_t, d_psi_t);
679 a_t = trialValueSelectionMT (a_l, f_l, g_l,
681 a_t, phi_t, d_phi_t);
684 a_t = std::min (a_t, step_max);
685 a_t = std::max (a_t, step_min);
687 x_t = x + step_dir * a_t;
689 final_transformation_ = (Eigen::Translation<float, 3> (static_cast<float> (x_t (0)), static_cast<float> (x_t (1)), static_cast<float> (x_t (2))) *
690 Eigen::AngleAxis<float> (static_cast<float> (x_t (3)), Eigen::Vector3f::UnitX ()) *
691 Eigen::AngleAxis<float> (static_cast<float> (x_t (4)), Eigen::Vector3f::UnitY ()) *
692 Eigen::AngleAxis<float> (static_cast<float> (x_t (5)), Eigen::Vector3f::UnitZ ())).matrix ();
699 score = computeDerivatives (score_gradient, hessian, trans_cloud, x_t,
false);
704 d_phi_t = -(score_gradient.dot (step_dir));
707 psi_t = auxilaryFunction_PsiMT (a_t, phi_t, phi_0, d_phi_0, mu);
709 d_psi_t = auxilaryFunction_dPsiMT (d_phi_t, d_phi_0, mu);
712 if (open_interval && (psi_t <= 0 && d_psi_t >= 0))
714 open_interval =
false;
717 f_l += phi_0 - mu * d_phi_0 * a_l;
721 f_u += phi_0 - mu * d_phi_0 * a_u;
728 interval_converged = updateIntervalMT (a_l, f_l, g_l,
730 a_t, psi_t, d_psi_t);
735 interval_converged = updateIntervalMT (a_l, f_l, g_l,
737 a_t, phi_t, d_phi_t);
747 computeHessian (hessian, trans_cloud, x_t);
752 #endif // PCL_REGISTRATION_NDT_IMPL_H_