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_ ()
54 , j_ang_a_ (), j_ang_b_ (), j_ang_c_ (), j_ang_d_ (), j_ang_e_ (), j_ang_f_ (), j_ang_g_ (), j_ang_h_ ()
55 , h_ang_a2_ (), h_ang_a3_ (), h_ang_b2_ (), h_ang_b3_ (), h_ang_c2_ (), h_ang_c3_ (), h_ang_d1_ (), h_ang_d2_ ()
56 , h_ang_d3_ (), h_ang_e1_ (), h_ang_e2_ (), h_ang_e3_ (), h_ang_f1_ (), h_ang_f2_ (), h_ang_f3_ ()
60 reg_name_ =
"NormalDistributionsTransform";
62 double gauss_c1, gauss_c2, gauss_d3;
67 gauss_d3 = -log (gauss_c2);
68 gauss_d1_ = -log ( gauss_c1 + gauss_c2 ) - gauss_d3;
69 gauss_d2_ = -2 * log ((-log ( gauss_c1 * exp ( -0.5 ) + gauss_c2 ) - gauss_d3) /
gauss_d1_);
76 template<
typename Po
intSource,
typename Po
intTarget>
void
82 double gauss_c1, gauss_c2, gauss_d3;
85 gauss_c1 = 10 * (1 - outlier_ratio_);
86 gauss_c2 = outlier_ratio_ / pow (resolution_, 3);
87 gauss_d3 = -log (gauss_c2);
88 gauss_d1_ = -log ( gauss_c1 + gauss_c2 ) - gauss_d3;
89 gauss_d2_ = -2 * log ((-log ( gauss_c1 * exp ( -0.5 ) + gauss_c2 ) - gauss_d3) / gauss_d1_);
91 if (guess != Eigen::Matrix4f::Identity ())
94 final_transformation_ = guess;
100 point_gradient_.setZero ();
101 point_gradient_.block<3, 3>(0, 0).setIdentity ();
102 point_hessian_.setZero ();
104 Eigen::Transform<float, 3, Eigen::Affine, Eigen::ColMajor> eig_transformation;
105 eig_transformation.matrix () = final_transformation_;
108 Eigen::Matrix<double, 6, 1> p, delta_p, score_gradient;
109 Eigen::Vector3f init_translation = eig_transformation.translation ();
110 Eigen::Vector3f init_rotation = eig_transformation.rotation ().eulerAngles (0, 1, 2);
111 p << init_translation (0), init_translation (1), init_translation (2),
112 init_rotation (0), init_rotation (1), init_rotation (2);
114 Eigen::Matrix<double, 6, 6> hessian;
120 score = computeDerivatives (score_gradient, hessian, output, p);
125 previous_transformation_ = transformation_;
128 Eigen::JacobiSVD<Eigen::Matrix<double, 6, 6> > sv (hessian, Eigen::ComputeFullU | Eigen::ComputeFullV);
130 delta_p = sv.solve (-score_gradient);
133 delta_p_norm = delta_p.norm ();
135 if (delta_p_norm == 0 || delta_p_norm != delta_p_norm)
137 trans_probability_ = score /
static_cast<double> (input_->points.size ());
138 converged_ = delta_p_norm == delta_p_norm;
142 delta_p.normalize ();
143 delta_p_norm = computeStepLengthMT (p, delta_p, delta_p_norm, step_size_, transformation_epsilon_ / 2, score, score_gradient, hessian, output);
144 delta_p *= delta_p_norm;
147 transformation_ = (Eigen::Translation<float, 3> (
static_cast<float> (delta_p (0)), static_cast<float> (delta_p (1)), static_cast<float> (delta_p (2))) *
148 Eigen::AngleAxis<float> (
static_cast<float> (delta_p (3)), Eigen::Vector3f::UnitX ()) *
149 Eigen::AngleAxis<float> (static_cast<float> (delta_p (4)), Eigen::Vector3f::UnitY ()) *
150 Eigen::AngleAxis<float> (static_cast<float> (delta_p (5)), Eigen::Vector3f::UnitZ ())).matrix ();
156 if (update_visualizer_ != 0)
157 update_visualizer_ (output, std::vector<int>(), *target_, std::vector<int>() );
159 if (nr_iterations_ > max_iterations_ ||
160 (nr_iterations_ && (std::fabs (delta_p_norm) < transformation_epsilon_)))
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 (
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 (fabs (p (3)) < 10e-5)
248 if (fabs (p (4)) < 10e-5)
260 if (fabs (p (5)) < 10e-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 = 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 || e_x_cov_x != 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 (
int 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 (
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_ * exp (-gauss_d2_ * x_trans.dot (c_inv * x_trans) / 2);
456 if (e_x_cov_x > 1 || e_x_cov_x < 0 || e_x_cov_x != 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 (
int 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)
494 if (g_t * (a_l - a_t) > 0)
503 if (g_t * (a_l - a_t) < 0)
520 template<
typename Po
intSource,
typename Po
intTarget>
double
522 double a_u,
double f_u,
double g_u,
523 double a_t,
double f_t,
double g_t)
530 double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
531 double w = std::sqrt (z * z - g_t * g_l);
533 double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
537 double a_q = a_l - 0.5 * (a_l - a_t) * g_l / (g_l - (f_l - f_t) / (a_l - a_t));
539 if (std::fabs (a_c - a_l) < std::fabs (a_q - a_l))
542 return (0.5 * (a_q + a_c));
550 double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
551 double w = std::sqrt (z * z - g_t * g_l);
553 double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
557 double a_s = a_l - (a_l - a_t) / (g_l - g_t) * g_l;
559 if (std::fabs (a_c - a_t) >= std::fabs (a_s - a_t))
566 if (std::fabs (g_t) <= std::fabs (g_l))
570 double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
571 double w = std::sqrt (z * z - g_t * g_l);
572 double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
576 double a_s = a_l - (a_l - a_t) / (g_l - g_t) * g_l;
580 if (std::fabs (a_c - a_t) < std::fabs (a_s - a_t))
586 return (std::min (a_t + 0.66 * (a_u - a_t), a_t_next));
588 return (std::max (a_t + 0.66 * (a_u - a_t), a_t_next));
595 double z = 3 * (f_t - f_u) / (a_t - a_u) - g_t - g_u;
596 double w = std::sqrt (z * z - g_t * g_u);
598 return (a_u + (a_t - a_u) * (w - g_u - z) / (g_t - g_u + 2 * w));
603 template<
typename Po
intSource,
typename Po
intTarget>
double
605 double step_min,
double &score, Eigen::Matrix<double, 6, 1> &score_gradient, Eigen::Matrix<double, 6, 6> &hessian,
609 double phi_0 = -score;
611 double d_phi_0 = -(score_gradient.dot (step_dir));
613 Eigen::Matrix<double, 6, 1> x_t;
631 int max_step_iterations = 10;
632 int step_iterations = 0;
640 double a_l = 0, a_u = 0;
643 double f_l = auxilaryFunction_PsiMT (a_l, phi_0, phi_0, d_phi_0, mu);
644 double g_l = auxilaryFunction_dPsiMT (d_phi_0, d_phi_0, mu);
646 double f_u = auxilaryFunction_PsiMT (a_u, phi_0, phi_0, d_phi_0, mu);
647 double g_u = auxilaryFunction_dPsiMT (d_phi_0, d_phi_0, mu);
650 bool interval_converged = (step_max - step_min) > 0, open_interval =
true;
652 double a_t = step_init;
653 a_t = std::min (a_t, step_max);
654 a_t = std::max (a_t, step_min);
656 x_t = x + step_dir * a_t;
658 final_transformation_ = (Eigen::Translation<float, 3>(
static_cast<float> (x_t (0)), static_cast<float> (x_t (1)), static_cast<float> (x_t (2))) *
659 Eigen::AngleAxis<float> (
static_cast<float> (x_t (3)), Eigen::Vector3f::UnitX ()) *
660 Eigen::AngleAxis<float> (static_cast<float> (x_t (4)), Eigen::Vector3f::UnitY ()) *
661 Eigen::AngleAxis<float> (static_cast<float> (x_t (5)), Eigen::Vector3f::UnitZ ())).matrix ();
668 score = computeDerivatives (score_gradient, hessian, trans_cloud, x_t,
true);
671 double phi_t = -score;
673 double d_phi_t = -(score_gradient.dot (step_dir));
676 double psi_t = auxilaryFunction_PsiMT (a_t, phi_t, phi_0, d_phi_0, mu);
678 double d_psi_t = auxilaryFunction_dPsiMT (d_phi_t, d_phi_0, mu);
681 while (!interval_converged && step_iterations < max_step_iterations && !(psi_t <= 0 && d_phi_t <= -nu * d_phi_0 ))
686 a_t = trialValueSelectionMT (a_l, f_l, g_l,
688 a_t, psi_t, d_psi_t);
692 a_t = trialValueSelectionMT (a_l, f_l, g_l,
694 a_t, phi_t, d_phi_t);
697 a_t = std::min (a_t, step_max);
698 a_t = std::max (a_t, step_min);
700 x_t = x + step_dir * a_t;
702 final_transformation_ = (Eigen::Translation<float, 3> (
static_cast<float> (x_t (0)), static_cast<float> (x_t (1)), static_cast<float> (x_t (2))) *
703 Eigen::AngleAxis<float> (
static_cast<float> (x_t (3)), Eigen::Vector3f::UnitX ()) *
704 Eigen::AngleAxis<float> (static_cast<float> (x_t (4)), Eigen::Vector3f::UnitY ()) *
705 Eigen::AngleAxis<float> (static_cast<float> (x_t (5)), Eigen::Vector3f::UnitZ ())).matrix ();
712 score = computeDerivatives (score_gradient, hessian, trans_cloud, x_t,
false);
717 d_phi_t = -(score_gradient.dot (step_dir));
720 psi_t = auxilaryFunction_PsiMT (a_t, phi_t, phi_0, d_phi_0, mu);
722 d_psi_t = auxilaryFunction_dPsiMT (d_phi_t, d_phi_0, mu);
725 if (open_interval && (psi_t <= 0 && d_psi_t >= 0))
727 open_interval =
false;
730 f_l = f_l + phi_0 - mu * d_phi_0 * a_l;
731 g_l = g_l + mu * d_phi_0;
734 f_u = f_u + phi_0 - mu * d_phi_0 * a_u;
735 g_u = g_u + mu * d_phi_0;
741 interval_converged = updateIntervalMT (a_l, f_l, g_l,
743 a_t, psi_t, d_psi_t);
748 interval_converged = updateIntervalMT (a_l, f_l, g_l,
750 a_t, phi_t, d_phi_t);
760 computeHessian (hessian, trans_cloud, x_t);
765 #endif // PCL_REGISTRATION_NDT_IMPL_H_
void transformPointCloud(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const Eigen::Transform< Scalar, 3, Eigen::Affine > &transform)
Apply an affine transform defined by an Eigen Transform.
int max_iterations_
The maximum number of iterations the internal optimization should run for.
double transformation_epsilon_
The maximum difference between two consecutive transformations in order to consider convergence (user...
std::string reg_name_
The registration method name.