79 if (target_cells_.getCentroids()->empty()) {
80 PCL_ERROR(
"[%s::computeTransformation] Voxel grid is not searchable!\n",
81 getClassName().c_str());
86 const double gauss_c1 = 10 * (1 - outlier_ratio_);
87 const double gauss_c2 = outlier_ratio_ / pow(resolution_, 3);
88 const double gauss_d3 = -std::log(gauss_c2);
89 gauss_d1_ = -std::log(gauss_c1 + gauss_c2) - gauss_d3;
91 -2 * std::log((-std::log(gauss_c1 * std::exp(-0.5) + gauss_c2) - gauss_d3) /
94 if (guess != Matrix4::Identity()) {
96 final_transformation_ = guess;
102 point_jacobian_.setZero();
103 point_jacobian_.block<3, 3>(0, 0).setIdentity();
104 point_hessian_.setZero();
106 Eigen::Transform<Scalar, 3, Eigen::Affine, Eigen::ColMajor> eig_transformation;
107 eig_transformation.matrix() = final_transformation_;
110 Eigen::Matrix<double, 6, 1> transform, score_gradient;
111 Vector3 init_translation = eig_transformation.translation();
112 Vector3 init_rotation = eig_transformation.rotation().eulerAngles(0, 1, 2);
113 transform << init_translation.template cast<double>(),
114 init_rotation.template cast<double>();
116 Eigen::Matrix<double, 6, 6> hessian;
120 double score = computeDerivatives(score_gradient, hessian, output, transform);
122 while (!converged_) {
124 previous_transformation_ = transformation_;
128 Eigen::JacobiSVD<Eigen::Matrix<double, 6, 6>> sv(
129 hessian, Eigen::ComputeFullU | Eigen::ComputeFullV);
131 Eigen::Matrix<double, 6, 1> delta = sv.solve(-score_gradient);
134 double delta_norm = delta.norm();
136 if (delta_norm == 0 || std::isnan(delta_norm)) {
137 trans_likelihood_ = score /
static_cast<double>(input_->size());
138 converged_ = delta_norm == 0;
143 delta_norm = computeStepLengthMT(transform,
147 transformation_epsilon_ / 2,
155 convertTransform(delta, transformation_);
160 if (update_visualizer_)
163 const double cos_angle =
164 0.5 * (transformation_.template block<3, 3>(0, 0).trace() - 1);
165 const double translation_sqr =
166 transformation_.template block<3, 1>(0, 3).squaredNorm();
170 if (nr_iterations_ >= max_iterations_ ||
171 ((transformation_epsilon_ > 0 && translation_sqr <= transformation_epsilon_) &&
172 (transformation_rotation_epsilon_ > 0 &&
173 cos_angle >= transformation_rotation_epsilon_)) ||
174 ((transformation_epsilon_ <= 0) &&
175 (transformation_rotation_epsilon_ > 0 &&
176 cos_angle >= transformation_rotation_epsilon_)) ||
177 ((transformation_epsilon_ > 0 && translation_sqr <= transformation_epsilon_) &&
178 (transformation_rotation_epsilon_ <= 0))) {
186 trans_likelihood_ = score /
static_cast<double>(input_->size());
192 Eigen::Matrix<double, 6, 1>& score_gradient,
193 Eigen::Matrix<double, 6, 6>& hessian,
195 const Eigen::Matrix<double, 6, 1>& transform,
196 bool compute_hessian)
198 score_gradient.setZero();
203 computeAngleDerivatives(transform);
206 for (std::size_t idx = 0; idx < input_->size(); idx++) {
208 const auto& x_trans_pt = trans_cloud[idx];
212 std::vector<TargetGridLeafConstPtr> neighborhood;
213 std::vector<float> distances;
214 target_cells_.radiusSearch(x_trans_pt, resolution_, neighborhood, distances);
216 for (
const auto& cell : neighborhood) {
218 const auto& x_pt = (*input_)[idx];
219 const Eigen::Vector3d x = x_pt.getVector3fMap().template cast<double>();
222 const Eigen::Vector3d x_trans =
223 x_trans_pt.getVector3fMap().template cast<double>() - cell->getMean();
226 const Eigen::Matrix3d c_inv = cell->getInverseCov();
230 computePointDerivatives(x);
234 updateDerivatives(score_gradient, hessian, x_trans, c_inv, compute_hessian);
243 const Eigen::Matrix<double, 6, 1>& transform,
bool compute_hessian)
246 const auto calculate_cos_sin = [](
double angle,
double& c,
double& s) {
247 if (std::abs(angle) < 10e-5) {
257 double cx, cy, cz, sx, sy, sz;
258 calculate_cos_sin(transform(3), cx, sx);
259 calculate_cos_sin(transform(4), cy, sy);
260 calculate_cos_sin(transform(5), cz, sz);
264 angular_jacobian_.setZero();
265 angular_jacobian_.row(0).noalias() = Eigen::Vector4d(
266 (-sx * sz + cx * sy * cz), (-sx * cz - cx * sy * sz), (-cx * cy), 1.0);
267 angular_jacobian_.row(1).noalias() = Eigen::Vector4d(
268 (cx * sz + sx * sy * cz), (cx * cz - sx * sy * sz), (-sx * cy), 1.0);
269 angular_jacobian_.row(2).noalias() =
270 Eigen::Vector4d((-sy * cz), sy * sz, cy, 1.0);
271 angular_jacobian_.row(3).noalias() =
272 Eigen::Vector4d(sx * cy * cz, (-sx * cy * sz), sx * sy, 1.0);
273 angular_jacobian_.row(4).noalias() =
274 Eigen::Vector4d((-cx * cy * cz), cx * cy * sz, (-cx * sy), 1.0);
275 angular_jacobian_.row(5).noalias() =
276 Eigen::Vector4d((-cy * sz), (-cy * cz), 0, 1.0);
277 angular_jacobian_.row(6).noalias() =
278 Eigen::Vector4d((cx * cz - sx * sy * sz), (-cx * sz - sx * sy * cz), 0, 1.0);
279 angular_jacobian_.row(7).noalias() =
280 Eigen::Vector4d((sx * cz + cx * sy * sz), (cx * sy * cz - sx * sz), 0, 1.0);
282 if (compute_hessian) {
285 angular_hessian_.setZero();
286 angular_hessian_.row(0).noalias() = Eigen::Vector4d(
287 (-cx * sz - sx * sy * cz), (-cx * cz + sx * sy * sz), sx * cy, 0.0f);
288 angular_hessian_.row(1).noalias() = Eigen::Vector4d(
289 (-sx * sz + cx * sy * cz), (-cx * sy * sz - sx * cz), (-cx * cy), 0.0f);
291 angular_hessian_.row(2).noalias() =
292 Eigen::Vector4d((cx * cy * cz), (-cx * cy * sz), (cx * sy), 0.0f);
293 angular_hessian_.row(3).noalias() =
294 Eigen::Vector4d((sx * cy * cz), (-sx * cy * sz), (sx * sy), 0.0f);
297 angular_hessian_.row(4).noalias() = Eigen::Vector4d(
298 (-sx * cz - cx * sy * sz), (sx * sz - cx * sy * cz), 0, 0.0f);
299 angular_hessian_.row(5).noalias() = Eigen::Vector4d(
300 (cx * cz - sx * sy * sz), (-sx * sy * cz - cx * sz), 0, 0.0f);
302 angular_hessian_.row(6).noalias() =
303 Eigen::Vector4d((-cy * cz), (cy * sz), (-sy), 0.0f);
304 angular_hessian_.row(7).noalias() =
305 Eigen::Vector4d((-sx * sy * cz), (sx * sy * sz), (sx * cy), 0.0f);
306 angular_hessian_.row(8).noalias() =
307 Eigen::Vector4d((cx * sy * cz), (-cx * sy * sz), (-cx * cy), 0.0f);
309 angular_hessian_.row(9).noalias() =
310 Eigen::Vector4d((sy * sz), (sy * cz), 0, 0.0f);
311 angular_hessian_.row(10).noalias() =
312 Eigen::Vector4d((-sx * cy * sz), (-sx * cy * cz), 0, 0.0f);
313 angular_hessian_.row(11).noalias() =
314 Eigen::Vector4d((cx * cy * sz), (cx * cy * cz), 0, 0.0f);
316 angular_hessian_.row(12).noalias() =
317 Eigen::Vector4d((-cy * cz), (cy * sz), 0, 0.0f);
318 angular_hessian_.row(13).noalias() = Eigen::Vector4d(
319 (-cx * sz - sx * sy * cz), (-cx * cz + sx * sy * sz), 0, 0.0f);
320 angular_hessian_.row(14).noalias() = Eigen::Vector4d(
321 (-sx * sz + cx * sy * cz), (-cx * sy * sz - sx * cz), 0, 0.0f);
328 const Eigen::Vector3d& x,
bool compute_hessian)
333 Eigen::Matrix<double, 8, 1> point_angular_jacobian =
334 angular_jacobian_ * Eigen::Vector4d(x[0], x[1], x[2], 0.0);
335 point_jacobian_(1, 3) = point_angular_jacobian[0];
336 point_jacobian_(2, 3) = point_angular_jacobian[1];
337 point_jacobian_(0, 4) = point_angular_jacobian[2];
338 point_jacobian_(1, 4) = point_angular_jacobian[3];
339 point_jacobian_(2, 4) = point_angular_jacobian[4];
340 point_jacobian_(0, 5) = point_angular_jacobian[5];
341 point_jacobian_(1, 5) = point_angular_jacobian[6];
342 point_jacobian_(2, 5) = point_angular_jacobian[7];
344 if (compute_hessian) {
345 Eigen::Matrix<double, 15, 1> point_angular_hessian =
346 angular_hessian_ * Eigen::Vector4d(x[0], x[1], x[2], 0.0);
349 const Eigen::Vector3d a(0, point_angular_hessian[0], point_angular_hessian[1]);
350 const Eigen::Vector3d b(0, point_angular_hessian[2], point_angular_hessian[3]);
351 const Eigen::Vector3d c(0, point_angular_hessian[4], point_angular_hessian[5]);
352 const Eigen::Vector3d d = point_angular_hessian.block<3, 1>(6, 0);
353 const Eigen::Vector3d e = point_angular_hessian.block<3, 1>(9, 0);
354 const Eigen::Vector3d f = point_angular_hessian.block<3, 1>(12, 0);
359 point_hessian_.block<3, 1>(9, 3) = a;
360 point_hessian_.block<3, 1>(12, 3) = b;
361 point_hessian_.block<3, 1>(15, 3) = c;
362 point_hessian_.block<3, 1>(9, 4) = b;
363 point_hessian_.block<3, 1>(12, 4) = d;
364 point_hessian_.block<3, 1>(15, 4) = e;
365 point_hessian_.block<3, 1>(9, 5) = c;
366 point_hessian_.block<3, 1>(12, 5) = e;
367 point_hessian_.block<3, 1>(15, 5) = f;
374 Eigen::Matrix<double, 6, 1>& score_gradient,
375 Eigen::Matrix<double, 6, 6>& hessian,
376 const Eigen::Vector3d& x_trans,
377 const Eigen::Matrix3d& c_inv,
378 bool compute_hessian)
const
381 double e_x_cov_x = std::exp(-gauss_d2_ * x_trans.dot(c_inv * x_trans) / 2);
384 const double score_inc = -gauss_d1_ * e_x_cov_x;
386 e_x_cov_x = gauss_d2_ * e_x_cov_x;
389 if (e_x_cov_x > 1 || e_x_cov_x < 0 || std::isnan(e_x_cov_x)) {
394 e_x_cov_x *= gauss_d1_;
396 for (
int i = 0; i < 6; i++) {
399 const Eigen::Vector3d cov_dxd_pi = c_inv * point_jacobian_.col(i);
402 score_gradient(i) += x_trans.dot(cov_dxd_pi) * e_x_cov_x;
404 if (compute_hessian) {
405 for (Eigen::Index j = 0; j < hessian.cols(); j++) {
408 e_x_cov_x * (-gauss_d2_ * x_trans.dot(cov_dxd_pi) *
409 x_trans.dot(c_inv * point_jacobian_.col(j)) +
410 x_trans.dot(c_inv * point_hessian_.block<3, 1>(3 * i, j)) +
411 point_jacobian_.col(j).dot(cov_dxd_pi));
554 if (a_t == a_l && a_t == a_u) {
559 enum class EndpointsCondition { Case1, Case2, Case3, Case4 };
560 EndpointsCondition condition;
563 condition = EndpointsCondition::Case4;
565 else if (f_t > f_l) {
566 condition = EndpointsCondition::Case1;
568 else if (g_t * g_l < 0) {
569 condition = EndpointsCondition::Case2;
571 else if (std::fabs(g_t) <= std::fabs(g_l)) {
572 condition = EndpointsCondition::Case3;
575 condition = EndpointsCondition::Case4;
579 case EndpointsCondition::Case1: {
582 const double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
583 const double w = std::sqrt(z * z - g_t * g_l);
585 const double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
590 a_l - 0.5 * (a_l - a_t) * g_l / (g_l - (f_l - f_t) / (a_l - a_t));
592 if (std::fabs(a_c - a_l) < std::fabs(a_q - a_l)) {
595 return 0.5 * (a_q + a_c);
598 case EndpointsCondition::Case2: {
601 const double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
602 const double w = std::sqrt(z * z - g_t * g_l);
604 const double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
608 const double a_s = a_l - (a_l - a_t) / (g_l - g_t) * g_l;
610 if (std::fabs(a_c - a_t) >= std::fabs(a_s - a_t)) {
616 case EndpointsCondition::Case3: {
619 const double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
620 const double w = std::sqrt(z * z - g_t * g_l);
621 const double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
625 const double a_s = a_l - (a_l - a_t) / (g_l - g_t) * g_l;
629 if (std::fabs(a_c - a_t) < std::fabs(a_s - a_t)) {
637 return std::min(a_t + 0.66 * (a_u - a_t), a_t_next);
639 return std::max(a_t + 0.66 * (a_u - a_t), a_t_next);
643 case EndpointsCondition::Case4: {
646 const double z = 3 * (f_t - f_u) / (a_t - a_u) - g_t - g_u;
647 const double w = std::sqrt(z * z - g_t * g_u);
649 return a_u + (a_t - a_u) * (w - g_u - z) / (g_t - g_u + 2 * w);
657 const Eigen::Matrix<double, 6, 1>& x,
658 Eigen::Matrix<double, 6, 1>& step_dir,
663 Eigen::Matrix<double, 6, 1>& score_gradient,
664 Eigen::Matrix<double, 6, 6>& hessian,
668 const double phi_0 = -score;
670 double d_phi_0 = -(score_gradient.dot(step_dir));
684 constexpr int max_step_iterations = 10;
685 int step_iterations = 0;
688 constexpr double mu = 1.e-4;
690 constexpr double nu = 0.9;
693 double a_l = 0, a_u = 0;
697 double f_l = auxilaryFunction_PsiMT(a_l, phi_0, phi_0, d_phi_0, mu);
698 double g_l = auxilaryFunction_dPsiMT(d_phi_0, d_phi_0, mu);
700 double f_u = auxilaryFunction_PsiMT(a_u, phi_0, phi_0, d_phi_0, mu);
701 double g_u = auxilaryFunction_dPsiMT(d_phi_0, d_phi_0, mu);
705 bool interval_converged = (step_max - step_min) < 0, open_interval =
true;
707 double a_t = step_init;
708 a_t = std::min(a_t, step_max);
709 a_t = std::max(a_t, step_min);
711 Eigen::Matrix<double, 6, 1> x_t = x + step_dir * a_t;
714 convertTransform(x_t, final_transformation_);
723 score = computeDerivatives(score_gradient, hessian, trans_cloud, x_t,
true);
726 double phi_t = -score;
728 double d_phi_t = -(score_gradient.dot(step_dir));
731 double psi_t = auxilaryFunction_PsiMT(a_t, phi_t, phi_0, d_phi_0, mu);
733 double d_psi_t = auxilaryFunction_dPsiMT(d_phi_t, d_phi_0, mu);
738 while (!interval_converged && step_iterations < max_step_iterations &&
740 d_phi_t <= -nu * d_phi_0 )) {
743 a_t = trialValueSelectionMT(a_l, f_l, g_l, a_u, f_u, g_u, a_t, psi_t, d_psi_t);
746 a_t = trialValueSelectionMT(a_l, f_l, g_l, a_u, f_u, g_u, a_t, phi_t, d_phi_t);
749 a_t = std::min(a_t, step_max);
750 a_t = std::max(a_t, step_min);
752 x_t = x + step_dir * a_t;
755 convertTransform(x_t, final_transformation_);
762 score = computeDerivatives(score_gradient, hessian, trans_cloud, x_t,
false);
767 d_phi_t = -(score_gradient.dot(step_dir));
770 psi_t = auxilaryFunction_PsiMT(a_t, phi_t, phi_0, d_phi_0, mu);
772 d_psi_t = auxilaryFunction_dPsiMT(d_phi_t, d_phi_0, mu);
775 if (open_interval && (psi_t <= 0 && d_psi_t >= 0)) {
776 open_interval =
false;
779 f_l += phi_0 - mu * d_phi_0 * a_l;
783 f_u += phi_0 - mu * d_phi_0 * a_u;
790 updateIntervalMT(a_l, f_l, g_l, a_u, f_u, g_u, a_t, psi_t, d_psi_t);
796 updateIntervalMT(a_l, f_l, g_l, a_u, f_u, g_u, a_t, phi_t, d_phi_t);
805 if (step_iterations) {
806 computeHessian(hessian, trans_cloud);