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;
114 score = computeDerivatives (score_gradient, hessian, output, p);
119 previous_transformation_ = transformation_;
122 Eigen::JacobiSVD<Eigen::Matrix<double, 6, 6> > sv (hessian, Eigen::ComputeFullU | Eigen::ComputeFullV);
124 delta_p = sv.solve (-score_gradient);
127 double delta_p_norm = delta_p.norm ();
129 if (delta_p_norm == 0 || std::isnan(delta_p_norm))
131 trans_probability_ = score / static_cast<double> (input_->points.size ());
132 converged_ = delta_p_norm == delta_p_norm;
136 delta_p.normalize ();
137 delta_p_norm = computeStepLengthMT (p, delta_p, delta_p_norm, step_size_, transformation_epsilon_ / 2, score, score_gradient, hessian, output);
138 delta_p *= delta_p_norm;
141 transformation_ = (Eigen::Translation<float, 3> (static_cast<float> (delta_p (0)), static_cast<float> (delta_p (1)), static_cast<float> (delta_p (2))) *
142 Eigen::AngleAxis<float> (static_cast<float> (delta_p (3)), Eigen::Vector3f::UnitX ()) *
143 Eigen::AngleAxis<float> (static_cast<float> (delta_p (4)), Eigen::Vector3f::UnitY ()) *
144 Eigen::AngleAxis<float> (static_cast<float> (delta_p (5)), Eigen::Vector3f::UnitZ ())).matrix ();
150 if (update_visualizer_)
151 update_visualizer_ (output, std::vector<int>(), *target_, std::vector<int>() );
153 double cos_angle = 0.5 * (transformation_.coeff (0, 0) + transformation_.coeff (1, 1) + transformation_.coeff (2, 2) - 1);
154 double translation_sqr = transformation_.coeff (0, 3) * transformation_.coeff (0, 3) +
155 transformation_.coeff (1, 3) * transformation_.coeff (1, 3) +
156 transformation_.coeff (2, 3) * transformation_.coeff (2, 3);
160 if (nr_iterations_ >= max_iterations_ ||
161 ((transformation_epsilon_ > 0 && translation_sqr <= transformation_epsilon_) && (transformation_rotation_epsilon_ > 0 && cos_angle >= transformation_rotation_epsilon_)) ||
162 ((transformation_epsilon_ <= 0) && (transformation_rotation_epsilon_ > 0 && cos_angle >= transformation_rotation_epsilon_)) ||
163 ((transformation_epsilon_ > 0 && translation_sqr <= transformation_epsilon_) && (transformation_rotation_epsilon_ <= 0)))
171 trans_probability_ = score / static_cast<double> (input_->points.size ());
175 template<
typename Po
intSource,
typename Po
intTarget>
double
177 Eigen::Matrix<double, 6, 6> &hessian,
179 Eigen::Matrix<double, 6, 1> &p,
180 bool compute_hessian)
183 PointSource x_pt, x_trans_pt;
185 Eigen::Vector3d x, x_trans;
189 Eigen::Matrix3d c_inv;
191 score_gradient.setZero ();
196 computeAngleDerivatives (p);
199 for (std::size_t idx = 0; idx < input_->points.size (); idx++)
201 x_trans_pt = trans_cloud.points[idx];
204 std::vector<TargetGridLeafConstPtr> neighborhood;
205 std::vector<float> distances;
206 target_cells_.radiusSearch (x_trans_pt, resolution_, neighborhood, distances);
208 for (
typename std::vector<TargetGridLeafConstPtr>::iterator neighborhood_it = neighborhood.begin (); neighborhood_it != neighborhood.end (); ++neighborhood_it)
210 cell = *neighborhood_it;
211 x_pt = input_->points[idx];
212 x = Eigen::Vector3d (x_pt.x, x_pt.y, x_pt.z);
214 x_trans = Eigen::Vector3d (x_trans_pt.x, x_trans_pt.y, x_trans_pt.z);
217 x_trans -= cell->getMean ();
219 c_inv = cell->getInverseCov ();
222 computePointDerivatives (x);
224 score += updateDerivatives (score_gradient, hessian, x_trans, c_inv, compute_hessian);
232 template<
typename Po
intSource,
typename Po
intTarget>
void
236 double cx, cy, cz, sx, sy, sz;
237 if (std::abs (p (3)) < 10e-5)
245 cx = std::cos (p (3));
248 if (std::abs (p (4)) < 10e-5)
256 cy = std::cos (p (4));
260 if (std::abs (p (5)) < 10e-5)
268 cz = std::cos (p (5));
273 j_ang_a_ << (-sx * sz + cx * sy * cz), (-sx * cz - cx * sy * sz), (-cx * cy);
274 j_ang_b_ << (cx * sz + sx * sy * cz), (cx * cz - sx * sy * sz), (-sx * cy);
275 j_ang_c_ << (-sy * cz), sy * sz, cy;
276 j_ang_d_ << sx * cy * cz, (-sx * cy * sz), sx * sy;
277 j_ang_e_ << (-cx * cy * cz), cx * cy * sz, (-cx * sy);
278 j_ang_f_ << (-cy * sz), (-cy * cz), 0;
279 j_ang_g_ << (cx * cz - sx * sy * sz), (-cx * sz - sx * sy * cz), 0;
280 j_ang_h_ << (sx * cz + cx * sy * sz), (cx * sy * cz - sx * sz), 0;
285 h_ang_a2_ << (-cx * sz - sx * sy * cz), (-cx * cz + sx * sy * sz), sx * cy;
286 h_ang_a3_ << (-sx * sz + cx * sy * cz), (-cx * sy * sz - sx * cz), (-cx * cy);
288 h_ang_b2_ << (cx * cy * cz), (-cx * cy * sz), (cx * sy);
289 h_ang_b3_ << (sx * cy * cz), (-sx * cy * sz), (sx * sy);
291 h_ang_c2_ << (-sx * cz - cx * sy * sz), (sx * sz - cx * sy * cz), 0;
292 h_ang_c3_ << (cx * cz - sx * sy * sz), (-sx * sy * cz - cx * sz), 0;
294 h_ang_d1_ << (-cy * cz), (cy * sz), (sy);
295 h_ang_d2_ << (-sx * sy * cz), (sx * sy * sz), (sx * cy);
296 h_ang_d3_ << (cx * sy * cz), (-cx * sy * sz), (-cx * cy);
298 h_ang_e1_ << (sy * sz), (sy * cz), 0;
299 h_ang_e2_ << (-sx * cy * sz), (-sx * cy * cz), 0;
300 h_ang_e3_ << (cx * cy * sz), (cx * cy * cz), 0;
302 h_ang_f1_ << (-cy * cz), (cy * sz), 0;
303 h_ang_f2_ << (-cx * sz - sx * sy * cz), (-cx * cz + sx * sy * sz), 0;
304 h_ang_f3_ << (-sx * sz + cx * sy * cz), (-cx * sy * sz - sx * cz), 0;
309 template<
typename Po
intSource,
typename Po
intTarget>
void
314 point_gradient_ (1, 3) = x.dot (j_ang_a_);
315 point_gradient_ (2, 3) = x.dot (j_ang_b_);
316 point_gradient_ (0, 4) = x.dot (j_ang_c_);
317 point_gradient_ (1, 4) = x.dot (j_ang_d_);
318 point_gradient_ (2, 4) = x.dot (j_ang_e_);
319 point_gradient_ (0, 5) = x.dot (j_ang_f_);
320 point_gradient_ (1, 5) = x.dot (j_ang_g_);
321 point_gradient_ (2, 5) = x.dot (j_ang_h_);
326 Eigen::Vector3d a, b, c, d, e, f;
328 a << 0, x.dot (h_ang_a2_), x.dot (h_ang_a3_);
329 b << 0, x.dot (h_ang_b2_), x.dot (h_ang_b3_);
330 c << 0, x.dot (h_ang_c2_), x.dot (h_ang_c3_);
331 d << x.dot (h_ang_d1_), x.dot (h_ang_d2_), x.dot (h_ang_d3_);
332 e << x.dot (h_ang_e1_), x.dot (h_ang_e2_), x.dot (h_ang_e3_);
333 f << x.dot (h_ang_f1_), x.dot (h_ang_f2_), x.dot (h_ang_f3_);
337 point_hessian_.block<3, 1>(9, 3) = a;
338 point_hessian_.block<3, 1>(12, 3) = b;
339 point_hessian_.block<3, 1>(15, 3) = c;
340 point_hessian_.block<3, 1>(9, 4) = b;
341 point_hessian_.block<3, 1>(12, 4) = d;
342 point_hessian_.block<3, 1>(15, 4) = e;
343 point_hessian_.block<3, 1>(9, 5) = c;
344 point_hessian_.block<3, 1>(12, 5) = e;
345 point_hessian_.block<3, 1>(15, 5) = f;
350 template<
typename Po
intSource,
typename Po
intTarget>
double
352 Eigen::Matrix<double, 6, 6> &hessian,
353 Eigen::Vector3d &x_trans, Eigen::Matrix3d &c_inv,
354 bool compute_hessian)
356 Eigen::Vector3d cov_dxd_pi;
358 double e_x_cov_x = std::exp (-gauss_d2_ * x_trans.dot (c_inv * x_trans) / 2);
360 double score_inc = -gauss_d1_ * e_x_cov_x;
362 e_x_cov_x = gauss_d2_ * e_x_cov_x;
365 if (e_x_cov_x > 1 || e_x_cov_x < 0 || std::isnan(e_x_cov_x))
369 e_x_cov_x *= gauss_d1_;
372 for (
int i = 0; i < 6; i++)
375 cov_dxd_pi = c_inv * point_gradient_.col (i);
378 score_gradient (i) += x_trans.dot (cov_dxd_pi) * e_x_cov_x;
382 for (Eigen::Index j = 0; j < hessian.cols (); j++)
385 hessian (i, j) += e_x_cov_x * (-gauss_d2_ * x_trans.dot (cov_dxd_pi) * x_trans.dot (c_inv * point_gradient_.col (j)) +
386 x_trans.dot (c_inv * point_hessian_.block<3, 1>(3 * i, j)) +
387 point_gradient_.col (j).dot (cov_dxd_pi) );
396 template<
typename Po
intSource,
typename Po
intTarget>
void
401 PointSource x_pt, x_trans_pt;
403 Eigen::Vector3d x, x_trans;
407 Eigen::Matrix3d c_inv;
414 for (std::size_t idx = 0; idx < input_->points.size (); idx++)
416 x_trans_pt = trans_cloud.points[idx];
419 std::vector<TargetGridLeafConstPtr> neighborhood;
420 std::vector<float> distances;
421 target_cells_.radiusSearch (x_trans_pt, resolution_, neighborhood, distances);
423 for (
typename std::vector<TargetGridLeafConstPtr>::iterator neighborhood_it = neighborhood.begin (); neighborhood_it != neighborhood.end (); ++neighborhood_it)
425 cell = *neighborhood_it;
428 x_pt = input_->points[idx];
429 x = Eigen::Vector3d (x_pt.x, x_pt.y, x_pt.z);
431 x_trans = Eigen::Vector3d (x_trans_pt.x, x_trans_pt.y, x_trans_pt.z);
434 x_trans -= cell->getMean ();
436 c_inv = cell->getInverseCov ();
439 computePointDerivatives (x);
441 updateHessian (hessian, x_trans, c_inv);
448 template<
typename Po
intSource,
typename Po
intTarget>
void
451 Eigen::Vector3d cov_dxd_pi;
453 double e_x_cov_x = gauss_d2_ * std::exp (-gauss_d2_ * x_trans.dot (c_inv * x_trans) / 2);
456 if (e_x_cov_x > 1 || e_x_cov_x < 0 || std::isnan(e_x_cov_x))
460 e_x_cov_x *= gauss_d1_;
462 for (
int i = 0; i < 6; i++)
465 cov_dxd_pi = c_inv * point_gradient_.col (i);
467 for (Eigen::Index j = 0; j < hessian.cols (); j++)
470 hessian (i, j) += e_x_cov_x * (-gauss_d2_ * x_trans.dot (cov_dxd_pi) * x_trans.dot (c_inv * point_gradient_.col (j)) +
471 x_trans.dot (c_inv * point_hessian_.block<3, 1>(3 * i, j)) +
472 point_gradient_.col (j).dot (cov_dxd_pi) );
479 template<
typename Po
intSource,
typename Po
intTarget>
bool
481 double &a_u,
double &f_u,
double &g_u,
482 double a_t,
double f_t,
double g_t)
493 if (g_t * (a_l - a_t) > 0)
501 if (g_t * (a_l - a_t) < 0)
517 template<
typename Po
intSource,
typename Po
intTarget>
double
519 double a_u,
double f_u,
double g_u,
520 double a_t,
double f_t,
double g_t)
527 double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
528 double w = std::sqrt (z * z - g_t * g_l);
530 double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
534 double a_q = a_l - 0.5 * (a_l - a_t) * g_l / (g_l - (f_l - f_t) / (a_l - a_t));
536 if (std::fabs (a_c - a_l) < std::fabs (a_q - a_l))
538 return (0.5 * (a_q + a_c));
545 double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
546 double w = std::sqrt (z * z - g_t * g_l);
548 double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
552 double a_s = a_l - (a_l - a_t) / (g_l - g_t) * g_l;
554 if (std::fabs (a_c - a_t) >= std::fabs (a_s - a_t))
559 if (std::fabs (g_t) <= std::fabs (g_l))
563 double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
564 double w = std::sqrt (z * z - g_t * g_l);
565 double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
569 double a_s = a_l - (a_l - a_t) / (g_l - g_t) * g_l;
573 if (std::fabs (a_c - a_t) < std::fabs (a_s - a_t))
579 return (std::min (a_t + 0.66 * (a_u - a_t), a_t_next));
580 return (std::max (a_t + 0.66 * (a_u - a_t), a_t_next));
585 double z = 3 * (f_t - f_u) / (a_t - a_u) - g_t - g_u;
586 double w = std::sqrt (z * z - g_t * g_u);
588 return (a_u + (a_t - a_u) * (w - g_u - z) / (g_t - g_u + 2 * w));
592 template<
typename Po
intSource,
typename Po
intTarget>
double
594 double step_min,
double &score, Eigen::Matrix<double, 6, 1> &score_gradient, Eigen::Matrix<double, 6, 6> &hessian,
598 double phi_0 = -score;
600 double d_phi_0 = -(score_gradient.dot (step_dir));
602 Eigen::Matrix<double, 6, 1> x_t;
617 int max_step_iterations = 10;
618 int step_iterations = 0;
626 double a_l = 0, a_u = 0;
629 double f_l = auxilaryFunction_PsiMT (a_l, phi_0, phi_0, d_phi_0, mu);
630 double g_l = auxilaryFunction_dPsiMT (d_phi_0, d_phi_0, mu);
632 double f_u = auxilaryFunction_PsiMT (a_u, phi_0, phi_0, d_phi_0, mu);
633 double g_u = auxilaryFunction_dPsiMT (d_phi_0, d_phi_0, mu);
636 bool interval_converged = (step_max - step_min) > 0, open_interval =
true;
638 double a_t = step_init;
639 a_t = std::min (a_t, step_max);
640 a_t = std::max (a_t, step_min);
642 x_t = x + step_dir * a_t;
644 final_transformation_ = (Eigen::Translation<float, 3>(static_cast<float> (x_t (0)), static_cast<float> (x_t (1)), static_cast<float> (x_t (2))) *
645 Eigen::AngleAxis<float> (static_cast<float> (x_t (3)), Eigen::Vector3f::UnitX ()) *
646 Eigen::AngleAxis<float> (static_cast<float> (x_t (4)), Eigen::Vector3f::UnitY ()) *
647 Eigen::AngleAxis<float> (static_cast<float> (x_t (5)), Eigen::Vector3f::UnitZ ())).matrix ();
654 score = computeDerivatives (score_gradient, hessian, trans_cloud, x_t,
true);
657 double phi_t = -score;
659 double d_phi_t = -(score_gradient.dot (step_dir));
662 double psi_t = auxilaryFunction_PsiMT (a_t, phi_t, phi_0, d_phi_0, mu);
664 double d_psi_t = auxilaryFunction_dPsiMT (d_phi_t, d_phi_0, mu);
667 while (!interval_converged && step_iterations < max_step_iterations && !(psi_t <= 0 && d_phi_t <= -nu * d_phi_0 ))
672 a_t = trialValueSelectionMT (a_l, f_l, g_l,
674 a_t, psi_t, d_psi_t);
678 a_t = trialValueSelectionMT (a_l, f_l, g_l,
680 a_t, phi_t, d_phi_t);
683 a_t = std::min (a_t, step_max);
684 a_t = std::max (a_t, step_min);
686 x_t = x + step_dir * a_t;
688 final_transformation_ = (Eigen::Translation<float, 3> (static_cast<float> (x_t (0)), static_cast<float> (x_t (1)), static_cast<float> (x_t (2))) *
689 Eigen::AngleAxis<float> (static_cast<float> (x_t (3)), Eigen::Vector3f::UnitX ()) *
690 Eigen::AngleAxis<float> (static_cast<float> (x_t (4)), Eigen::Vector3f::UnitY ()) *
691 Eigen::AngleAxis<float> (static_cast<float> (x_t (5)), Eigen::Vector3f::UnitZ ())).matrix ();
698 score = computeDerivatives (score_gradient, hessian, trans_cloud, x_t,
false);
703 d_phi_t = -(score_gradient.dot (step_dir));
706 psi_t = auxilaryFunction_PsiMT (a_t, phi_t, phi_0, d_phi_0, mu);
708 d_psi_t = auxilaryFunction_dPsiMT (d_phi_t, d_phi_0, mu);
711 if (open_interval && (psi_t <= 0 && d_psi_t >= 0))
713 open_interval =
false;
716 f_l += phi_0 - mu * d_phi_0 * a_l;
720 f_u += phi_0 - mu * d_phi_0 * a_u;
727 interval_converged = updateIntervalMT (a_l, f_l, g_l,
729 a_t, psi_t, d_psi_t);
734 interval_converged = updateIntervalMT (a_l, f_l, g_l,
736 a_t, phi_t, d_phi_t);
746 computeHessian (hessian, trans_cloud, x_t);
751 #endif // PCL_REGISTRATION_NDT_IMPL_H_