Point Cloud Library (PCL)  1.12.1-dev
ndt.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #ifndef PCL_REGISTRATION_NDT_IMPL_H_
42 #define PCL_REGISTRATION_NDT_IMPL_H_
43 
44 namespace pcl {
45 
46 template <typename PointSource, typename PointTarget, typename Scalar>
49 : target_cells_()
50 , resolution_(1.0f)
51 , step_size_(0.1)
52 , outlier_ratio_(0.55)
53 , gauss_d1_()
54 , gauss_d2_()
55 , trans_likelihood_()
56 {
57  reg_name_ = "NormalDistributionsTransform";
58 
59  // Initializes the gaussian fitting parameters (eq. 6.8) [Magnusson 2009]
60  const double gauss_c1 = 10.0 * (1 - outlier_ratio_);
61  const double gauss_c2 = outlier_ratio_ / pow(resolution_, 3);
62  const double gauss_d3 = -std::log(gauss_c2);
63  gauss_d1_ = -std::log(gauss_c1 + gauss_c2) - gauss_d3;
64  gauss_d2_ =
65  -2 * std::log((-std::log(gauss_c1 * std::exp(-0.5) + gauss_c2) - gauss_d3) /
66  gauss_d1_);
67 
69  max_iterations_ = 35;
70 }
71 
72 template <typename PointSource, typename PointTarget, typename Scalar>
73 void
75  PointCloudSource& output, const Matrix4& guess)
76 {
77  nr_iterations_ = 0;
78  converged_ = false;
79 
80  // Initializes the gaussian fitting parameters (eq. 6.8) [Magnusson 2009]
81  const double gauss_c1 = 10 * (1 - outlier_ratio_);
82  const double gauss_c2 = outlier_ratio_ / pow(resolution_, 3);
83  const double gauss_d3 = -std::log(gauss_c2);
84  gauss_d1_ = -std::log(gauss_c1 + gauss_c2) - gauss_d3;
85  gauss_d2_ =
86  -2 * std::log((-std::log(gauss_c1 * std::exp(-0.5) + gauss_c2) - gauss_d3) /
87  gauss_d1_);
88 
89  if (guess != Matrix4::Identity()) {
90  // Initialise final transformation to the guessed one
91  final_transformation_ = guess;
92  // Apply guessed transformation prior to search for neighbours
93  transformPointCloud(output, output, guess);
94  }
95 
96  // Initialize Point Gradient and Hessian
97  point_jacobian_.setZero();
98  point_jacobian_.block<3, 3>(0, 0).setIdentity();
99  point_hessian_.setZero();
100 
101  Eigen::Transform<Scalar, 3, Eigen::Affine, Eigen::ColMajor> eig_transformation;
102  eig_transformation.matrix() = final_transformation_;
103 
104  // Convert initial guess matrix to 6 element transformation vector
105  Eigen::Matrix<double, 6, 1> transform, score_gradient;
106  Vector3 init_translation = eig_transformation.translation();
107  Vector3 init_rotation = eig_transformation.rotation().eulerAngles(0, 1, 2);
108  transform << init_translation.template cast<double>(),
109  init_rotation.template cast<double>();
110 
111  Eigen::Matrix<double, 6, 6> hessian;
112 
113  // Calculate derivates of initial transform vector, subsequent derivative calculations
114  // are done in the step length determination.
115  double score = computeDerivatives(score_gradient, hessian, output, transform);
116 
117  while (!converged_) {
118  // Store previous transformation
119  previous_transformation_ = transformation_;
120 
121  // Solve for decent direction using newton method, line 23 in Algorithm 2 [Magnusson
122  // 2009]
123  Eigen::JacobiSVD<Eigen::Matrix<double, 6, 6>> sv(
124  hessian, Eigen::ComputeFullU | Eigen::ComputeFullV);
125  // Negative for maximization as opposed to minimization
126  Eigen::Matrix<double, 6, 1> delta = sv.solve(-score_gradient);
127 
128  // Calculate step length with guaranteed sufficient decrease [More, Thuente 1994]
129  double delta_norm = delta.norm();
130 
131  if (delta_norm == 0 || std::isnan(delta_norm)) {
132  trans_likelihood_ = score / static_cast<double>(input_->size());
133  converged_ = delta_norm == 0;
134  return;
135  }
136 
137  delta /= delta_norm;
138  delta_norm = computeStepLengthMT(transform,
139  delta,
140  delta_norm,
141  step_size_,
142  transformation_epsilon_ / 2,
143  score,
144  score_gradient,
145  hessian,
146  output);
147  delta *= delta_norm;
148 
149  // Convert delta into matrix form
150  convertTransform(delta, transformation_);
151 
152  transform += delta;
153 
154  // Update Visualizer (untested)
155  if (update_visualizer_)
156  update_visualizer_(output, pcl::Indices(), *target_, pcl::Indices());
157 
158  const double cos_angle =
159  0.5 * (transformation_.template block<3, 3>(0, 0).trace() - 1);
160  const double translation_sqr =
161  transformation_.template block<3, 1>(0, 3).squaredNorm();
162 
163  nr_iterations_++;
164 
165  if (nr_iterations_ >= max_iterations_ ||
166  ((transformation_epsilon_ > 0 && translation_sqr <= transformation_epsilon_) &&
167  (transformation_rotation_epsilon_ > 0 &&
168  cos_angle >= transformation_rotation_epsilon_)) ||
169  ((transformation_epsilon_ <= 0) &&
170  (transformation_rotation_epsilon_ > 0 &&
171  cos_angle >= transformation_rotation_epsilon_)) ||
172  ((transformation_epsilon_ > 0 && translation_sqr <= transformation_epsilon_) &&
173  (transformation_rotation_epsilon_ <= 0))) {
174  converged_ = true;
175  }
176  }
177 
178  // Store transformation likelihood. The relative differences within each scan
179  // registration are accurate but the normalization constants need to be modified for
180  // it to be globally accurate
181  trans_likelihood_ = score / static_cast<double>(input_->size());
182 }
183 
184 template <typename PointSource, typename PointTarget, typename Scalar>
185 double
187  Eigen::Matrix<double, 6, 1>& score_gradient,
188  Eigen::Matrix<double, 6, 6>& hessian,
189  const PointCloudSource& trans_cloud,
190  const Eigen::Matrix<double, 6, 1>& transform,
191  bool compute_hessian)
192 {
193  score_gradient.setZero();
194  hessian.setZero();
195  double score = 0;
196 
197  // Precompute Angular Derivatives (eq. 6.19 and 6.21)[Magnusson 2009]
198  computeAngleDerivatives(transform);
199 
200  // Update gradient and hessian for each point, line 17 in Algorithm 2 [Magnusson 2009]
201  for (std::size_t idx = 0; idx < input_->size(); idx++) {
202  // Transformed Point
203  const auto& x_trans_pt = trans_cloud[idx];
204 
205  // Find neighbors (Radius search has been experimentally faster than direct neighbor
206  // checking.
207  std::vector<TargetGridLeafConstPtr> neighborhood;
208  std::vector<float> distances;
209  target_cells_.radiusSearch(x_trans_pt, resolution_, neighborhood, distances);
210 
211  for (const auto& cell : neighborhood) {
212  // Original Point
213  const auto& x_pt = (*input_)[idx];
214  const Eigen::Vector3d x = x_pt.getVector3fMap().template cast<double>();
215 
216  // Denorm point, x_k' in Equations 6.12 and 6.13 [Magnusson 2009]
217  const Eigen::Vector3d x_trans =
218  x_trans_pt.getVector3fMap().template cast<double>() - cell->getMean();
219  // Inverse Covariance of Occupied Voxel
220  // Uses precomputed covariance for speed.
221  const Eigen::Matrix3d c_inv = cell->getInverseCov();
222 
223  // Compute derivative of transform function w.r.t. transform vector, J_E and H_E
224  // in Equations 6.18 and 6.20 [Magnusson 2009]
225  computePointDerivatives(x);
226  // Update score, gradient and hessian, lines 19-21 in Algorithm 2, according to
227  // Equations 6.10, 6.12 and 6.13, respectively [Magnusson 2009]
228  score +=
229  updateDerivatives(score_gradient, hessian, x_trans, c_inv, compute_hessian);
230  }
231  }
232  return score;
233 }
234 
235 template <typename PointSource, typename PointTarget, typename Scalar>
236 void
238  const Eigen::Matrix<double, 6, 1>& transform, bool compute_hessian)
239 {
240  // Simplified math for near 0 angles
241  const auto calculate_cos_sin = [](double angle, double& c, double& s) {
242  if (std::abs(angle) < 10e-5) {
243  c = 1.0;
244  s = 0.0;
245  }
246  else {
247  c = std::cos(angle);
248  s = std::sin(angle);
249  }
250  };
251 
252  double cx, cy, cz, sx, sy, sz;
253  calculate_cos_sin(transform(3), cx, sx);
254  calculate_cos_sin(transform(4), cy, sy);
255  calculate_cos_sin(transform(5), cz, sz);
256 
257  // Precomputed angular gradient components. Letters correspond to Equation 6.19
258  // [Magnusson 2009]
259  angular_jacobian_.setZero();
260  angular_jacobian_.row(0).noalias() = Eigen::Vector4d(
261  (-sx * sz + cx * sy * cz), (-sx * cz - cx * sy * sz), (-cx * cy), 1.0); // a
262  angular_jacobian_.row(1).noalias() = Eigen::Vector4d(
263  (cx * sz + sx * sy * cz), (cx * cz - sx * sy * sz), (-sx * cy), 1.0); // b
264  angular_jacobian_.row(2).noalias() =
265  Eigen::Vector4d((-sy * cz), sy * sz, cy, 1.0); // c
266  angular_jacobian_.row(3).noalias() =
267  Eigen::Vector4d(sx * cy * cz, (-sx * cy * sz), sx * sy, 1.0); // d
268  angular_jacobian_.row(4).noalias() =
269  Eigen::Vector4d((-cx * cy * cz), cx * cy * sz, (-cx * sy), 1.0); // e
270  angular_jacobian_.row(5).noalias() =
271  Eigen::Vector4d((-cy * sz), (-cy * cz), 0, 1.0); // f
272  angular_jacobian_.row(6).noalias() =
273  Eigen::Vector4d((cx * cz - sx * sy * sz), (-cx * sz - sx * sy * cz), 0, 1.0); // g
274  angular_jacobian_.row(7).noalias() =
275  Eigen::Vector4d((sx * cz + cx * sy * sz), (cx * sy * cz - sx * sz), 0, 1.0); // h
276 
277  if (compute_hessian) {
278  // Precomputed angular hessian components. Letters correspond to Equation 6.21 and
279  // numbers correspond to row index [Magnusson 2009]
280  angular_hessian_.setZero();
281  angular_hessian_.row(0).noalias() = Eigen::Vector4d(
282  (-cx * sz - sx * sy * cz), (-cx * cz + sx * sy * sz), sx * cy, 0.0f); // a2
283  angular_hessian_.row(1).noalias() = Eigen::Vector4d(
284  (-sx * sz + cx * sy * cz), (-cx * sy * sz - sx * cz), (-cx * cy), 0.0f); // a3
285 
286  angular_hessian_.row(2).noalias() =
287  Eigen::Vector4d((cx * cy * cz), (-cx * cy * sz), (cx * sy), 0.0f); // b2
288  angular_hessian_.row(3).noalias() =
289  Eigen::Vector4d((sx * cy * cz), (-sx * cy * sz), (sx * sy), 0.0f); // b3
290 
291  // The sign of 'sx * sz' in c2 is incorrect in the thesis, and is fixed here.
292  angular_hessian_.row(4).noalias() = Eigen::Vector4d(
293  (-sx * cz - cx * sy * sz), (sx * sz - cx * sy * cz), 0, 0.0f); // c2
294  angular_hessian_.row(5).noalias() = Eigen::Vector4d(
295  (cx * cz - sx * sy * sz), (-sx * sy * cz - cx * sz), 0, 0.0f); // c3
296 
297  angular_hessian_.row(6).noalias() =
298  Eigen::Vector4d((-cy * cz), (cy * sz), (-sy), 0.0f); // d1
299  angular_hessian_.row(7).noalias() =
300  Eigen::Vector4d((-sx * sy * cz), (sx * sy * sz), (sx * cy), 0.0f); // d2
301  angular_hessian_.row(8).noalias() =
302  Eigen::Vector4d((cx * sy * cz), (-cx * sy * sz), (-cx * cy), 0.0f); // d3
303 
304  angular_hessian_.row(9).noalias() =
305  Eigen::Vector4d((sy * sz), (sy * cz), 0, 0.0f); // e1
306  angular_hessian_.row(10).noalias() =
307  Eigen::Vector4d((-sx * cy * sz), (-sx * cy * cz), 0, 0.0f); // e2
308  angular_hessian_.row(11).noalias() =
309  Eigen::Vector4d((cx * cy * sz), (cx * cy * cz), 0, 0.0f); // e3
310 
311  angular_hessian_.row(12).noalias() =
312  Eigen::Vector4d((-cy * cz), (cy * sz), 0, 0.0f); // f1
313  angular_hessian_.row(13).noalias() = Eigen::Vector4d(
314  (-cx * sz - sx * sy * cz), (-cx * cz + sx * sy * sz), 0, 0.0f); // f2
315  angular_hessian_.row(14).noalias() = Eigen::Vector4d(
316  (-sx * sz + cx * sy * cz), (-cx * sy * sz - sx * cz), 0, 0.0f); // f3
317  }
318 }
319 
320 template <typename PointSource, typename PointTarget, typename Scalar>
321 void
323  const Eigen::Vector3d& x, bool compute_hessian)
324 {
325  // Calculate first derivative of Transformation Equation 6.17 w.r.t. transform vector.
326  // Derivative w.r.t. ith element of transform vector corresponds to column i,
327  // Equation 6.18 and 6.19 [Magnusson 2009]
328  Eigen::Matrix<double, 8, 1> point_angular_jacobian =
329  angular_jacobian_ * Eigen::Vector4d(x[0], x[1], x[2], 0.0);
330  point_jacobian_(1, 3) = point_angular_jacobian[0];
331  point_jacobian_(2, 3) = point_angular_jacobian[1];
332  point_jacobian_(0, 4) = point_angular_jacobian[2];
333  point_jacobian_(1, 4) = point_angular_jacobian[3];
334  point_jacobian_(2, 4) = point_angular_jacobian[4];
335  point_jacobian_(0, 5) = point_angular_jacobian[5];
336  point_jacobian_(1, 5) = point_angular_jacobian[6];
337  point_jacobian_(2, 5) = point_angular_jacobian[7];
338 
339  if (compute_hessian) {
340  Eigen::Matrix<double, 15, 1> point_angular_hessian =
341  angular_hessian_ * Eigen::Vector4d(x[0], x[1], x[2], 0.0);
342 
343  // Vectors from Equation 6.21 [Magnusson 2009]
344  const Eigen::Vector3d a(0, point_angular_hessian[0], point_angular_hessian[1]);
345  const Eigen::Vector3d b(0, point_angular_hessian[2], point_angular_hessian[3]);
346  const Eigen::Vector3d c(0, point_angular_hessian[4], point_angular_hessian[5]);
347  const Eigen::Vector3d d = point_angular_hessian.block<3, 1>(6, 0);
348  const Eigen::Vector3d e = point_angular_hessian.block<3, 1>(9, 0);
349  const Eigen::Vector3d f = point_angular_hessian.block<3, 1>(12, 0);
350 
351  // Calculate second derivative of Transformation Equation 6.17 w.r.t. transform
352  // vector. Derivative w.r.t. ith and jth elements of transform vector corresponds to
353  // the 3x1 block matrix starting at (3i,j), Equation 6.20 and 6.21 [Magnusson 2009]
354  point_hessian_.block<3, 1>(9, 3) = a;
355  point_hessian_.block<3, 1>(12, 3) = b;
356  point_hessian_.block<3, 1>(15, 3) = c;
357  point_hessian_.block<3, 1>(9, 4) = b;
358  point_hessian_.block<3, 1>(12, 4) = d;
359  point_hessian_.block<3, 1>(15, 4) = e;
360  point_hessian_.block<3, 1>(9, 5) = c;
361  point_hessian_.block<3, 1>(12, 5) = e;
362  point_hessian_.block<3, 1>(15, 5) = f;
363  }
364 }
365 
366 template <typename PointSource, typename PointTarget, typename Scalar>
367 double
369  Eigen::Matrix<double, 6, 1>& score_gradient,
370  Eigen::Matrix<double, 6, 6>& hessian,
371  const Eigen::Vector3d& x_trans,
372  const Eigen::Matrix3d& c_inv,
373  bool compute_hessian) const
374 {
375  // e^(-d_2/2 * (x_k - mu_k)^T Sigma_k^-1 (x_k - mu_k)) Equation 6.9 [Magnusson 2009]
376  double e_x_cov_x = std::exp(-gauss_d2_ * x_trans.dot(c_inv * x_trans) / 2);
377  // Calculate likelihood of transformed points existence, Equation 6.9 [Magnusson
378  // 2009]
379  const double score_inc = -gauss_d1_ * e_x_cov_x;
380 
381  e_x_cov_x = gauss_d2_ * e_x_cov_x;
382 
383  // Error checking for invalid values.
384  if (e_x_cov_x > 1 || e_x_cov_x < 0 || std::isnan(e_x_cov_x)) {
385  return 0;
386  }
387 
388  // Reusable portion of Equation 6.12 and 6.13 [Magnusson 2009]
389  e_x_cov_x *= gauss_d1_;
390 
391  for (int i = 0; i < 6; i++) {
392  // Sigma_k^-1 d(T(x,p))/dpi, Reusable portion of Equation 6.12 and 6.13 [Magnusson
393  // 2009]
394  const Eigen::Vector3d cov_dxd_pi = c_inv * point_jacobian_.col(i);
395 
396  // Update gradient, Equation 6.12 [Magnusson 2009]
397  score_gradient(i) += x_trans.dot(cov_dxd_pi) * e_x_cov_x;
398 
399  if (compute_hessian) {
400  for (Eigen::Index j = 0; j < hessian.cols(); j++) {
401  // Update hessian, Equation 6.13 [Magnusson 2009]
402  hessian(i, j) +=
403  e_x_cov_x * (-gauss_d2_ * x_trans.dot(cov_dxd_pi) *
404  x_trans.dot(c_inv * point_jacobian_.col(j)) +
405  x_trans.dot(c_inv * point_hessian_.block<3, 1>(3 * i, j)) +
406  point_jacobian_.col(j).dot(cov_dxd_pi));
407  }
408  }
409  }
410 
411  return score_inc;
412 }
413 
414 template <typename PointSource, typename PointTarget, typename Scalar>
415 void
417  Eigen::Matrix<double, 6, 6>& hessian, const PointCloudSource& trans_cloud)
418 {
419  hessian.setZero();
420 
421  // Precompute Angular Derivatives unnecessary because only used after regular
422  // derivative calculation Update hessian for each point, line 17 in Algorithm 2
423  // [Magnusson 2009]
424  for (std::size_t idx = 0; idx < input_->size(); idx++) {
425  // Transformed Point
426  const auto& x_trans_pt = trans_cloud[idx];
427 
428  // Find neighbors (Radius search has been experimentally faster than direct neighbor
429  // checking.
430  std::vector<TargetGridLeafConstPtr> neighborhood;
431  std::vector<float> distances;
432  target_cells_.radiusSearch(x_trans_pt, resolution_, neighborhood, distances);
433 
434  for (const auto& cell : neighborhood) {
435  // Original Point
436  const auto& x_pt = (*input_)[idx];
437  const Eigen::Vector3d x = x_pt.getVector3fMap().template cast<double>();
438 
439  // Denorm point, x_k' in Equations 6.12 and 6.13 [Magnusson 2009]
440  const Eigen::Vector3d x_trans =
441  x_trans_pt.getVector3fMap().template cast<double>() - cell->getMean();
442  // Inverse Covariance of Occupied Voxel
443  // Uses precomputed covariance for speed.
444  const Eigen::Matrix3d c_inv = cell->getInverseCov();
445 
446  // Compute derivative of transform function w.r.t. transform vector, J_E and H_E
447  // in Equations 6.18 and 6.20 [Magnusson 2009]
448  computePointDerivatives(x);
449  // Update hessian, lines 21 in Algorithm 2, according to Equations 6.10, 6.12
450  // and 6.13, respectively [Magnusson 2009]
451  updateHessian(hessian, x_trans, c_inv);
452  }
453  }
454 }
455 
456 template <typename PointSource, typename PointTarget, typename Scalar>
457 void
459  Eigen::Matrix<double, 6, 6>& hessian,
460  const Eigen::Vector3d& x_trans,
461  const Eigen::Matrix3d& c_inv) const
462 {
463  // e^(-d_2/2 * (x_k - mu_k)^T Sigma_k^-1 (x_k - mu_k)) Equation 6.9 [Magnusson 2009]
464  double e_x_cov_x =
465  gauss_d2_ * std::exp(-gauss_d2_ * x_trans.dot(c_inv * x_trans) / 2);
466 
467  // Error checking for invalid values.
468  if (e_x_cov_x > 1 || e_x_cov_x < 0 || std::isnan(e_x_cov_x)) {
469  return;
470  }
471 
472  // Reusable portion of Equation 6.12 and 6.13 [Magnusson 2009]
473  e_x_cov_x *= gauss_d1_;
474 
475  for (int i = 0; i < 6; i++) {
476  // Sigma_k^-1 d(T(x,p))/dpi, Reusable portion of Equation 6.12 and 6.13 [Magnusson
477  // 2009]
478  const Eigen::Vector3d cov_dxd_pi = c_inv * point_jacobian_.col(i);
479 
480  for (Eigen::Index j = 0; j < hessian.cols(); j++) {
481  // Update hessian, Equation 6.13 [Magnusson 2009]
482  hessian(i, j) +=
483  e_x_cov_x * (-gauss_d2_ * x_trans.dot(cov_dxd_pi) *
484  x_trans.dot(c_inv * point_jacobian_.col(j)) +
485  x_trans.dot(c_inv * point_hessian_.block<3, 1>(3 * i, j)) +
486  point_jacobian_.col(j).dot(cov_dxd_pi));
487  }
488  }
489 }
490 
491 template <typename PointSource, typename PointTarget, typename Scalar>
492 bool
494  double& a_l,
495  double& f_l,
496  double& g_l,
497  double& a_u,
498  double& f_u,
499  double& g_u,
500  double a_t,
501  double f_t,
502  double g_t) const
503 {
504  // Case U1 in Update Algorithm and Case a in Modified Update Algorithm [More, Thuente
505  // 1994]
506  if (f_t > f_l) {
507  a_u = a_t;
508  f_u = f_t;
509  g_u = g_t;
510  return false;
511  }
512  // Case U2 in Update Algorithm and Case b in Modified Update Algorithm [More, Thuente
513  // 1994]
514  if (g_t * (a_l - a_t) > 0) {
515  a_l = a_t;
516  f_l = f_t;
517  g_l = g_t;
518  return false;
519  }
520  // Case U3 in Update Algorithm and Case c in Modified Update Algorithm [More, Thuente
521  // 1994]
522  if (g_t * (a_l - a_t) < 0) {
523  a_u = a_l;
524  f_u = f_l;
525  g_u = g_l;
526 
527  a_l = a_t;
528  f_l = f_t;
529  g_l = g_t;
530  return false;
531  }
532  // Interval Converged
533  return true;
534 }
535 
536 template <typename PointSource, typename PointTarget, typename Scalar>
537 double
539  double a_l,
540  double f_l,
541  double g_l,
542  double a_u,
543  double f_u,
544  double g_u,
545  double a_t,
546  double f_t,
547  double g_t) const
548 {
549  if (a_t == a_l && a_t == a_u) {
550  return a_t;
551  }
552 
553  // Endpoints condition check [More, Thuente 1994], p.299 - 300
554  enum class EndpointsCondition { Case1, Case2, Case3, Case4 };
555  EndpointsCondition condition;
556 
557  if (a_t == a_l) {
558  condition = EndpointsCondition::Case4;
559  }
560  else if (f_t > f_l) {
561  condition = EndpointsCondition::Case1;
562  }
563  else if (g_t * g_l < 0) {
564  condition = EndpointsCondition::Case2;
565  }
566  else if (std::fabs(g_t) <= std::fabs(g_l)) {
567  condition = EndpointsCondition::Case3;
568  }
569  else {
570  condition = EndpointsCondition::Case4;
571  }
572 
573  switch (condition) {
574  case EndpointsCondition::Case1: {
575  // Calculate the minimizer of the cubic that interpolates f_l, f_t, g_l and g_t
576  // Equation 2.4.52 [Sun, Yuan 2006]
577  const double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
578  const double w = std::sqrt(z * z - g_t * g_l);
579  // Equation 2.4.56 [Sun, Yuan 2006]
580  const double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
581 
582  // Calculate the minimizer of the quadratic that interpolates f_l, f_t and g_l
583  // Equation 2.4.2 [Sun, Yuan 2006]
584  const double a_q =
585  a_l - 0.5 * (a_l - a_t) * g_l / (g_l - (f_l - f_t) / (a_l - a_t));
586 
587  if (std::fabs(a_c - a_l) < std::fabs(a_q - a_l)) {
588  return a_c;
589  }
590  return 0.5 * (a_q + a_c);
591  }
592 
593  case EndpointsCondition::Case2: {
594  // Calculate the minimizer of the cubic that interpolates f_l, f_t, g_l and g_t
595  // Equation 2.4.52 [Sun, Yuan 2006]
596  const double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
597  const double w = std::sqrt(z * z - g_t * g_l);
598  // Equation 2.4.56 [Sun, Yuan 2006]
599  const double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
600 
601  // Calculate the minimizer of the quadratic that interpolates f_l, g_l and g_t
602  // Equation 2.4.5 [Sun, Yuan 2006]
603  const double a_s = a_l - (a_l - a_t) / (g_l - g_t) * g_l;
604 
605  if (std::fabs(a_c - a_t) >= std::fabs(a_s - a_t)) {
606  return a_c;
607  }
608  return a_s;
609  }
610 
611  case EndpointsCondition::Case3: {
612  // Calculate the minimizer of the cubic that interpolates f_l, f_t, g_l and g_t
613  // Equation 2.4.52 [Sun, Yuan 2006]
614  const double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
615  const double w = std::sqrt(z * z - g_t * g_l);
616  const double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
617 
618  // Calculate the minimizer of the quadratic that interpolates g_l and g_t
619  // Equation 2.4.5 [Sun, Yuan 2006]
620  const double a_s = a_l - (a_l - a_t) / (g_l - g_t) * g_l;
621 
622  double a_t_next;
623 
624  if (std::fabs(a_c - a_t) < std::fabs(a_s - a_t)) {
625  a_t_next = a_c;
626  }
627  else {
628  a_t_next = a_s;
629  }
630 
631  if (a_t > a_l) {
632  return std::min(a_t + 0.66 * (a_u - a_t), a_t_next);
633  }
634  return std::max(a_t + 0.66 * (a_u - a_t), a_t_next);
635  }
636 
637  default:
638  case EndpointsCondition::Case4: {
639  // Calculate the minimizer of the cubic that interpolates f_u, f_t, g_u and g_t
640  // Equation 2.4.52 [Sun, Yuan 2006]
641  const double z = 3 * (f_t - f_u) / (a_t - a_u) - g_t - g_u;
642  const double w = std::sqrt(z * z - g_t * g_u);
643  // Equation 2.4.56 [Sun, Yuan 2006]
644  return a_u + (a_t - a_u) * (w - g_u - z) / (g_t - g_u + 2 * w);
645  }
646  }
647 }
648 
649 template <typename PointSource, typename PointTarget, typename Scalar>
650 double
652  const Eigen::Matrix<double, 6, 1>& x,
653  Eigen::Matrix<double, 6, 1>& step_dir,
654  double step_init,
655  double step_max,
656  double step_min,
657  double& score,
658  Eigen::Matrix<double, 6, 1>& score_gradient,
659  Eigen::Matrix<double, 6, 6>& hessian,
660  PointCloudSource& trans_cloud)
661 {
662  // Set the value of phi(0), Equation 1.3 [More, Thuente 1994]
663  const double phi_0 = -score;
664  // Set the value of phi'(0), Equation 1.3 [More, Thuente 1994]
665  double d_phi_0 = -(score_gradient.dot(step_dir));
666 
667  if (d_phi_0 >= 0) {
668  // Not a decent direction
669  if (d_phi_0 == 0) {
670  return 0;
671  }
672  // Reverse step direction and calculate optimal step.
673  d_phi_0 *= -1;
674  step_dir *= -1;
675  }
676 
677  // The Search Algorithm for T(mu) [More, Thuente 1994]
678 
679  const int max_step_iterations = 10;
680  int step_iterations = 0;
681 
682  // Sufficient decreace constant, Equation 1.1 [More, Thuete 1994]
683  const double mu = 1.e-4;
684  // Curvature condition constant, Equation 1.2 [More, Thuete 1994]
685  const double nu = 0.9;
686 
687  // Initial endpoints of Interval I,
688  double a_l = 0, a_u = 0;
689 
690  // Auxiliary function psi is used until I is determined ot be a closed interval,
691  // Equation 2.1 [More, Thuente 1994]
692  double f_l = auxilaryFunction_PsiMT(a_l, phi_0, phi_0, d_phi_0, mu);
693  double g_l = auxilaryFunction_dPsiMT(d_phi_0, d_phi_0, mu);
694 
695  double f_u = auxilaryFunction_PsiMT(a_u, phi_0, phi_0, d_phi_0, mu);
696  double g_u = auxilaryFunction_dPsiMT(d_phi_0, d_phi_0, mu);
697 
698  // Check used to allow More-Thuente step length calculation to be skipped by making
699  // step_min == step_max
700  bool interval_converged = (step_max - step_min) < 0, open_interval = true;
701 
702  double a_t = step_init;
703  a_t = std::min(a_t, step_max);
704  a_t = std::max(a_t, step_min);
705 
706  Eigen::Matrix<double, 6, 1> x_t = x + step_dir * a_t;
707 
708  // Convert x_t into matrix form
709  convertTransform(x_t, final_transformation_);
710 
711  // New transformed point cloud
712  transformPointCloud(*input_, trans_cloud, final_transformation_);
713 
714  // Updates score, gradient and hessian. Hessian calculation is unnecessary but
715  // testing showed that most step calculations use the initial step suggestion and
716  // recalculation the reusable portions of the hessian would intail more computation
717  // time.
718  score = computeDerivatives(score_gradient, hessian, trans_cloud, x_t, true);
719 
720  // Calculate phi(alpha_t)
721  double phi_t = -score;
722  // Calculate phi'(alpha_t)
723  double d_phi_t = -(score_gradient.dot(step_dir));
724 
725  // Calculate psi(alpha_t)
726  double psi_t = auxilaryFunction_PsiMT(a_t, phi_t, phi_0, d_phi_0, mu);
727  // Calculate psi'(alpha_t)
728  double d_psi_t = auxilaryFunction_dPsiMT(d_phi_t, d_phi_0, mu);
729 
730  // Iterate until max number of iterations, interval convergance or a value satisfies
731  // the sufficient decrease, Equation 1.1, and curvature condition, Equation 1.2 [More,
732  // Thuente 1994]
733  while (!interval_converged && step_iterations < max_step_iterations &&
734  !(psi_t <= 0 /*Sufficient Decrease*/ &&
735  d_phi_t <= -nu * d_phi_0 /*Curvature Condition*/)) {
736  // Use auxiliary function if interval I is not closed
737  if (open_interval) {
738  a_t = trialValueSelectionMT(a_l, f_l, g_l, a_u, f_u, g_u, a_t, psi_t, d_psi_t);
739  }
740  else {
741  a_t = trialValueSelectionMT(a_l, f_l, g_l, a_u, f_u, g_u, a_t, phi_t, d_phi_t);
742  }
743 
744  a_t = std::min(a_t, step_max);
745  a_t = std::max(a_t, step_min);
746 
747  x_t = x + step_dir * a_t;
748 
749  // Convert x_t into matrix form
750  convertTransform(x_t, final_transformation_);
751 
752  // New transformed point cloud
753  // Done on final cloud to prevent wasted computation
754  transformPointCloud(*input_, trans_cloud, final_transformation_);
755 
756  // Updates score, gradient. Values stored to prevent wasted computation.
757  score = computeDerivatives(score_gradient, hessian, trans_cloud, x_t, false);
758 
759  // Calculate phi(alpha_t+)
760  phi_t = -score;
761  // Calculate phi'(alpha_t+)
762  d_phi_t = -(score_gradient.dot(step_dir));
763 
764  // Calculate psi(alpha_t+)
765  psi_t = auxilaryFunction_PsiMT(a_t, phi_t, phi_0, d_phi_0, mu);
766  // Calculate psi'(alpha_t+)
767  d_psi_t = auxilaryFunction_dPsiMT(d_phi_t, d_phi_0, mu);
768 
769  // Check if I is now a closed interval
770  if (open_interval && (psi_t <= 0 && d_psi_t >= 0)) {
771  open_interval = false;
772 
773  // Converts f_l and g_l from psi to phi
774  f_l += phi_0 - mu * d_phi_0 * a_l;
775  g_l += mu * d_phi_0;
776 
777  // Converts f_u and g_u from psi to phi
778  f_u += phi_0 - mu * d_phi_0 * a_u;
779  g_u += mu * d_phi_0;
780  }
781 
782  if (open_interval) {
783  // Update interval end points using Updating Algorithm [More, Thuente 1994]
784  interval_converged =
785  updateIntervalMT(a_l, f_l, g_l, a_u, f_u, g_u, a_t, psi_t, d_psi_t);
786  }
787  else {
788  // Update interval end points using Modified Updating Algorithm [More, Thuente
789  // 1994]
790  interval_converged =
791  updateIntervalMT(a_l, f_l, g_l, a_u, f_u, g_u, a_t, phi_t, d_phi_t);
792  }
793 
794  step_iterations++;
795  }
796 
797  // If inner loop was run then hessian needs to be calculated.
798  // Hessian is unnecessary for step length determination but gradients are required
799  // so derivative and transform data is stored for the next iteration.
800  if (step_iterations) {
801  computeHessian(hessian, trans_cloud);
802  }
803 
804  return a_t;
805 }
806 
807 } // namespace pcl
808 
809 #endif // PCL_REGISTRATION_NDT_IMPL_H_
void computePointDerivatives(const Eigen::Vector3d &x, bool compute_hessian=true)
Compute point derivatives.
Definition: ndt.hpp:322
virtual void computeTransformation(PointCloudSource &output)
Estimate the transformation and returns the transformed source (input) as output.
Definition: ndt.h:262
double updateDerivatives(Eigen::Matrix< double, 6, 1 > &score_gradient, Eigen::Matrix< double, 6, 6 > &hessian, const Eigen::Vector3d &x_trans, const Eigen::Matrix3d &c_inv, bool compute_hessian=true) const
Compute individual point contributions to derivatives of likelihood function w.r.t.
Definition: ndt.hpp:368
double computeDerivatives(Eigen::Matrix< double, 6, 1 > &score_gradient, Eigen::Matrix< double, 6, 6 > &hessian, const PointCloudSource &trans_cloud, const Eigen::Matrix< double, 6, 1 > &transform, bool compute_hessian=true)
Compute derivatives of likelihood function w.r.t.
Definition: ndt.hpp:186
void computeAngleDerivatives(const Eigen::Matrix< double, 6, 1 > &transform, bool compute_hessian=true)
Precompute angular components of derivatives.
Definition: ndt.hpp:237
typename Registration< PointSource, PointTarget, Scalar >::PointCloudSource PointCloudSource
Definition: ndt.h:69
NormalDistributionsTransform()
Constructor.
Definition: ndt.hpp:48
bool updateIntervalMT(double &a_l, double &f_l, double &g_l, double &a_u, double &f_u, double &g_u, double a_t, double f_t, double g_t) const
Update interval of possible step lengths for More-Thuente method, in More-Thuente (1994)
Definition: ndt.hpp:493
void computeHessian(Eigen::Matrix< double, 6, 6 > &hessian, const PointCloudSource &trans_cloud)
Compute hessian of likelihood function w.r.t.
Definition: ndt.hpp:416
typename Eigen::Matrix< Scalar, 3, 1 > Vector3
Definition: ndt.h:96
float resolution_
The side length of voxels.
Definition: ndt.h:547
typename Registration< PointSource, PointTarget, Scalar >::Matrix4 Matrix4
Definition: ndt.h:97
double outlier_ratio_
The ratio of outliers of points w.r.t.
Definition: ndt.h:554
void updateHessian(Eigen::Matrix< double, 6, 6 > &hessian, const Eigen::Vector3d &x_trans, const Eigen::Matrix3d &c_inv) const
Compute individual point contributions to hessian of likelihood function w.r.t.
Definition: ndt.hpp:458
double gauss_d1_
The normalization constants used fit the point distribution to a normal distribution,...
Definition: ndt.h:558
double trialValueSelectionMT(double a_l, double f_l, double g_l, double a_u, double f_u, double g_u, double a_t, double f_t, double g_t) const
Select new trial value for More-Thuente method.
Definition: ndt.hpp:538
double computeStepLengthMT(const Eigen::Matrix< double, 6, 1 > &transform, Eigen::Matrix< double, 6, 1 > &step_dir, double step_init, double step_max, double step_min, double &score, Eigen::Matrix< double, 6, 1 > &score_gradient, Eigen::Matrix< double, 6, 6 > &hessian, PointCloudSource &trans_cloud)
Compute line search step length and update transform and likelihood derivatives using More-Thuente me...
Definition: ndt.hpp:651
std::string reg_name_
The registration method name.
Definition: registration.h:560
int max_iterations_
The maximum number of iterations the internal optimization should run for.
Definition: registration.h:575
double transformation_epsilon_
The maximum difference between two consecutive transformations in order to consider convergence (user...
Definition: registration.h:597
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.
Definition: transforms.hpp:221
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133