41#ifndef PCL_REGISTRATION_NDT_IMPL_H_
42#define PCL_REGISTRATION_NDT_IMPL_H_
46template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
51 reg_name_ =
"NormalDistributionsTransform";
56 const double gauss_d3 = -std::log(gauss_c2);
57 gauss_d1_ = -std::log(gauss_c1 + gauss_c2) - gauss_d3;
59 -2 * std::log((-std::log(gauss_c1 * std::exp(-0.5) + gauss_c2) - gauss_d3) /
66template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
74 PCL_ERROR(
"[%s::computeTransformation] Voxel grid is not searchable!\n",
82 const double gauss_d3 = -std::log(gauss_c2);
83 gauss_d1_ = -std::log(gauss_c1 + gauss_c2) - gauss_d3;
85 -2 * std::log((-std::log(gauss_c1 * std::exp(-0.5) + gauss_c2) - gauss_d3) /
88 if (guess != Matrix4::Identity()) {
100 Eigen::Transform<Scalar, 3, Eigen::Affine, Eigen::ColMajor> eig_transformation;
104 Eigen::Matrix<double, 6, 1> transform, score_gradient;
105 Vector3 init_translation = eig_transformation.translation();
106 Vector3 init_rotation = eig_transformation.rotation().eulerAngles(0, 1, 2);
107 transform << init_translation.template cast<double>(),
108 init_rotation.template cast<double>();
110 Eigen::Matrix<double, 6, 6> hessian;
122 Eigen::JacobiSVD<Eigen::Matrix<double, 6, 6>> sv(
123 hessian, Eigen::ComputeFullU | Eigen::ComputeFullV);
124#if EIGEN_VERSION_AT_LEAST(3, 4, 0)
125 if (sv.info() != Eigen::ComputationInfo::Success) {
128 PCL_ERROR(
"[%s::computeTransformation] JacobiSVD on hessian failed!\n",
134 Eigen::Matrix<double, 6, 1> delta = sv.solve(-score_gradient);
137 double delta_norm = delta.norm();
139 if (delta_norm == 0 || std::isnan(delta_norm)) {
166 const double cos_angle =
168 const double translation_sqr =
192template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
195 Eigen::Matrix<double, 6, 1>& score_gradient,
196 Eigen::Matrix<double, 6, 6>& hessian,
198 const Eigen::Matrix<double, 6, 1>& transform,
199 bool compute_hessian)
201 score_gradient.setZero();
209 for (std::size_t idx = 0; idx <
input_->size(); idx++) {
211 const auto& x_trans_pt = trans_cloud[idx];
215 std::vector<TargetGridLeafConstPtr> neighborhood;
219 for (
const auto& cell : neighborhood) {
221 const auto& x_pt = (*input_)[idx];
222 const Eigen::Vector3d x = x_pt.getVector3fMap().template cast<double>();
225 const Eigen::Vector3d x_trans =
226 x_trans_pt.getVector3fMap().template cast<double>() - cell->getMean();
229 const Eigen::Matrix3d c_inv = cell->getInverseCov();
243template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
246 const Eigen::Matrix<double, 6, 1>& transform,
bool compute_hessian)
249 const auto calculate_cos_sin = [](
double angle,
double& c,
double& s) {
250 if (std::abs(angle) < 10e-5) {
260 double cx, cy, cz, sx, sy, sz;
261 calculate_cos_sin(transform(3), cx, sx);
262 calculate_cos_sin(transform(4), cy, sy);
263 calculate_cos_sin(transform(5), cz, sz);
269 (-sx * sz + cx * sy * cz), (-sx * cz - cx * sy * sz), (-cx * cy), 1.0);
271 (cx * sz + sx * sy * cz), (cx * cz - sx * sy * sz), (-sx * cy), 1.0);
273 Eigen::Vector4d((-sy * cz), sy * sz, cy, 1.0);
275 Eigen::Vector4d(sx * cy * cz, (-sx * cy * sz), sx * sy, 1.0);
277 Eigen::Vector4d((-cx * cy * cz), cx * cy * sz, (-cx * sy), 1.0);
279 Eigen::Vector4d((-cy * sz), (-cy * cz), 0, 1.0);
281 Eigen::Vector4d((cx * cz - sx * sy * sz), (-cx * sz - sx * sy * cz), 0, 1.0);
283 Eigen::Vector4d((sx * cz + cx * sy * sz), (cx * sy * cz - sx * sz), 0, 1.0);
285 if (compute_hessian) {
290 (-cx * sz - sx * sy * cz), (-cx * cz + sx * sy * sz), sx * cy, 0.0f);
292 (-sx * sz + cx * sy * cz), (-cx * sy * sz - sx * cz), (-cx * cy), 0.0f);
295 Eigen::Vector4d((cx * cy * cz), (-cx * cy * sz), (cx * sy), 0.0f);
297 Eigen::Vector4d((sx * cy * cz), (-sx * cy * sz), (sx * sy), 0.0f);
301 (-sx * cz - cx * sy * sz), (sx * sz - cx * sy * cz), 0, 0.0f);
303 (cx * cz - sx * sy * sz), (-sx * sy * cz - cx * sz), 0, 0.0f);
306 Eigen::Vector4d((-cy * cz), (cy * sz), (-sy), 0.0f);
308 Eigen::Vector4d((-sx * sy * cz), (sx * sy * sz), (sx * cy), 0.0f);
310 Eigen::Vector4d((cx * sy * cz), (-cx * sy * sz), (-cx * cy), 0.0f);
313 Eigen::Vector4d((sy * sz), (sy * cz), 0, 0.0f);
315 Eigen::Vector4d((-sx * cy * sz), (-sx * cy * cz), 0, 0.0f);
317 Eigen::Vector4d((cx * cy * sz), (cx * cy * cz), 0, 0.0f);
320 Eigen::Vector4d((-cy * cz), (cy * sz), 0, 0.0f);
322 (-cx * sz - sx * sy * cz), (-cx * cz + sx * sy * sz), 0, 0.0f);
324 (-sx * sz + cx * sy * cz), (-cx * sy * sz - sx * cz), 0, 0.0f);
328template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
331 const Eigen::Vector3d& x,
bool compute_hessian)
336 Eigen::Matrix<double, 8, 1> point_angular_jacobian =
347 if (compute_hessian) {
348 Eigen::Matrix<double, 15, 1> point_angular_hessian =
352 const Eigen::Vector3d a(0, point_angular_hessian[0], point_angular_hessian[1]);
353 const Eigen::Vector3d b(0, point_angular_hessian[2], point_angular_hessian[3]);
354 const Eigen::Vector3d c(0, point_angular_hessian[4], point_angular_hessian[5]);
355 const Eigen::Vector3d d = point_angular_hessian.block<3, 1>(6, 0);
356 const Eigen::Vector3d e = point_angular_hessian.block<3, 1>(9, 0);
357 const Eigen::Vector3d f = point_angular_hessian.block<3, 1>(12, 0);
374template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
377 Eigen::Matrix<double, 6, 1>& score_gradient,
378 Eigen::Matrix<double, 6, 6>& hessian,
379 const Eigen::Vector3d& x_trans,
380 const Eigen::Matrix3d& c_inv,
381 bool compute_hessian)
const
384 double e_x_cov_x = std::exp(-
gauss_d2_ * x_trans.dot(c_inv * x_trans) / 2);
387 const double score_inc = -
gauss_d1_ * e_x_cov_x;
392 if (e_x_cov_x > 1 || e_x_cov_x < 0 || std::isnan(e_x_cov_x)) {
399 for (
int i = 0; i < 6; i++) {
405 score_gradient(i) += x_trans.dot(cov_dxd_pi) * e_x_cov_x;
407 if (compute_hessian) {
408 for (Eigen::Index j = 0; j < hessian.cols(); j++) {
411 e_x_cov_x * (-
gauss_d2_ * x_trans.dot(cov_dxd_pi) *
422template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
432 for (std::size_t idx = 0; idx <
input_->size(); idx++) {
434 const auto& x_trans_pt = trans_cloud[idx];
438 std::vector<TargetGridLeafConstPtr> neighborhood;
442 for (
const auto& cell : neighborhood) {
444 const auto& x_pt = (*input_)[idx];
445 const Eigen::Vector3d x = x_pt.getVector3fMap().template cast<double>();
448 const Eigen::Vector3d x_trans =
449 x_trans_pt.getVector3fMap().template cast<double>() - cell->getMean();
452 const Eigen::Matrix3d c_inv = cell->getInverseCov();
464template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
467 Eigen::Matrix<double, 6, 6>& hessian,
468 const Eigen::Vector3d& x_trans,
469 const Eigen::Matrix3d& c_inv)
const
476 if (e_x_cov_x > 1 || e_x_cov_x < 0 || std::isnan(e_x_cov_x)) {
483 for (
int i = 0; i < 6; i++) {
488 for (Eigen::Index j = 0; j < hessian.cols(); j++) {
491 e_x_cov_x * (-
gauss_d2_ * x_trans.dot(cov_dxd_pi) *
499template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
522 if (g_t * (a_l - a_t) > 0) {
530 if (g_t * (a_l - a_t) < 0) {
544template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
557 if (a_t == a_l && a_t == a_u) {
562 enum class EndpointsCondition { Case1, Case2, Case3, Case4 };
563 EndpointsCondition condition;
566 condition = EndpointsCondition::Case4;
568 else if (f_t > f_l) {
569 condition = EndpointsCondition::Case1;
571 else if (g_t * g_l < 0) {
572 condition = EndpointsCondition::Case2;
574 else if (std::fabs(g_t) <= std::fabs(g_l)) {
575 condition = EndpointsCondition::Case3;
578 condition = EndpointsCondition::Case4;
582 case EndpointsCondition::Case1: {
585 const double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
586 const double w = std::sqrt(z * z - g_t * g_l);
588 const double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
593 a_l - 0.5 * (a_l - a_t) * g_l / (g_l - (f_l - f_t) / (a_l - a_t));
595 if (std::fabs(a_c - a_l) < std::fabs(a_q - a_l)) {
598 return 0.5 * (a_q + a_c);
601 case EndpointsCondition::Case2: {
604 const double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
605 const double w = std::sqrt(z * z - g_t * g_l);
607 const double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
611 const double a_s = a_l - (a_l - a_t) / (g_l - g_t) * g_l;
613 if (std::fabs(a_c - a_t) >= std::fabs(a_s - a_t)) {
619 case EndpointsCondition::Case3: {
622 const double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
623 const double w = std::sqrt(z * z - g_t * g_l);
624 const double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
628 const double a_s = a_l - (a_l - a_t) / (g_l - g_t) * g_l;
632 if (std::fabs(a_c - a_t) < std::fabs(a_s - a_t)) {
640 return std::min(a_t + 0.66 * (a_u - a_t), a_t_next);
642 return std::max(a_t + 0.66 * (a_u - a_t), a_t_next);
646 case EndpointsCondition::Case4: {
649 const double z = 3 * (f_t - f_u) / (a_t - a_u) - g_t - g_u;
650 const double w = std::sqrt(z * z - g_t * g_u);
652 return a_u + (a_t - a_u) * (w - g_u - z) / (g_t - g_u + 2 * w);
657template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
660 const Eigen::Matrix<double, 6, 1>& x,
661 Eigen::Matrix<double, 6, 1>& step_dir,
666 Eigen::Matrix<double, 6, 1>& score_gradient,
667 Eigen::Matrix<double, 6, 6>& hessian,
671 const double phi_0 = -score;
673 double d_phi_0 = -(score_gradient.dot(step_dir));
687 constexpr int max_step_iterations = 10;
688 int step_iterations = 0;
691 constexpr double mu = 1.e-4;
693 constexpr double nu = 0.9;
696 double a_l = 0, a_u = 0;
708 bool interval_converged = (step_max - step_min) < 0, open_interval =
true;
710 double a_t = step_init;
711 a_t = std::min(a_t, step_max);
712 a_t = std::max(a_t, step_min);
714 Eigen::Matrix<double, 6, 1> x_t = x + step_dir * a_t;
729 double phi_t = -score;
731 double d_phi_t = -(score_gradient.dot(step_dir));
741 while (!interval_converged && step_iterations < max_step_iterations &&
743 d_phi_t > -nu * d_phi_0 )) {
752 a_t = std::min(a_t, step_max);
753 a_t = std::max(a_t, step_min);
755 x_t = x + step_dir * a_t;
770 d_phi_t = -(score_gradient.dot(step_dir));
778 if (open_interval && (psi_t <= 0 && d_psi_t >= 0)) {
779 open_interval =
false;
782 f_l += phi_0 - mu * d_phi_0 * a_l;
786 f_u += phi_0 - mu * d_phi_0 * a_u;
808 if (step_iterations) {
PointCloudConstPtr input_
Matrix4 final_transformation_
std::function< UpdateVisualizerCallbackSignature > update_visualizer_
Matrix4 previous_transformation_
double transformation_rotation_epsilon_
double transformation_epsilon_
PointCloudTargetConstPtr target_
const std::string & getClassName() const
void transformPointCloud(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const Eigen::Matrix< Scalar, 4, 4 > &transform, bool copy_all_fields)
Apply a rigid transform defined by a 4x4 matrix.
IndicesAllocator<> Indices
Type used for indices in PCL.