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;
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 ())
104 Eigen::Transform<float, 3, Eigen::Affine, Eigen::ColMajor> eig_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;
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)
142 delta_p.normalize ();
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 ();
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 ();
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;
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 ();
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);
299 h_ang_e2_ << (-sx * cy * sz), (-sx * cy * cz), 0;
300 h_ang_e3_ << (cx * cy * sz), (cx * cy * cz), 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 326 Eigen::Vector3d a, b, c, d, e, 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;
365 if (e_x_cov_x > 1 || e_x_cov_x < 0 || e_x_cov_x != e_x_cov_x)
372 for (
int i = 0; i < 6; 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)) +
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;
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 ();
448 template<
typename Po
intSource,
typename Po
intTarget>
void 451 Eigen::Vector3d cov_dxd_pi;
456 if (e_x_cov_x > 1 || e_x_cov_x < 0 || e_x_cov_x != e_x_cov_x)
462 for (
int i = 0; i < 6; 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)) +
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;
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 ();
671 double phi_t = -score;
673 double d_phi_t = -(score_gradient.dot (step_dir));
681 while (!interval_converged && step_iterations < max_step_iterations && !(psi_t <= 0 && d_phi_t <= -nu * d_phi_0 ))
688 a_t, psi_t, d_psi_t);
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 ();
717 d_phi_t = -(score_gradient.dot (step_dir));
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;
743 a_t, psi_t, d_psi_t);
750 a_t, phi_t, d_phi_t);
765 #endif // PCL_REGISTRATION_NDT_IMPL_H_
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).
Matrix4 transformation_
The transformation matrix estimated by the registration method.
int max_iterations_
The maximum number of iterations the internal optimization should run for.
Matrix4 final_transformation_
The final transformation matrix estimated by the registration method after N iterations.
PointCloudTargetConstPtr target_
The input point cloud dataset target.
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=true)
Apply an affine transform defined by an Eigen Transform.
bool converged_
Holds internal convergence state, given user parameters.
double transformation_epsilon_
The maximum difference between two consecutive transformations in order to consider convergence (user...
std::string reg_name_
The registration method name.
boost::function< void(const pcl::PointCloud< PointSource > &cloud_src, const std::vector< int > &indices_src, const pcl::PointCloud< PointTarget > &cloud_tgt, const std::vector< int > &indices_tgt)> update_visualizer_
Callback function to update intermediate source point cloud position during it's registration to the ...
PointCloudConstPtr input_
The input point cloud dataset.
int radiusSearch(const PointT &point, double radius, std::vector< LeafConstPtr > &k_leaves, std::vector< float > &k_sqr_distances, unsigned int max_nn=0)
Search for all the nearest occupied voxels of the query point in a given radius.