Point Cloud Library (PCL)  1.11.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 
45 namespace pcl
46 {
47 
48 template<typename PointSource, typename PointTarget>
50  : target_cells_ ()
51  , resolution_ (1.0f)
52  , step_size_ (0.1)
53  , outlier_ratio_ (0.55)
54  , gauss_d1_ ()
55  , gauss_d2_ ()
56  , trans_probability_ ()
57 {
58  reg_name_ = "NormalDistributionsTransform";
59 
60  double gauss_c1, gauss_c2, gauss_d3;
61 
62  // Initializes the gaussian fitting parameters (eq. 6.8) [Magnusson 2009]
63  gauss_c1 = 10.0 * (1 - outlier_ratio_);
64  gauss_c2 = outlier_ratio_ / pow (resolution_, 3);
65  gauss_d3 = -std::log (gauss_c2);
66  gauss_d1_ = -std::log ( gauss_c1 + gauss_c2 ) - gauss_d3;
67  gauss_d2_ = -2 * std::log ((-std::log ( gauss_c1 * std::exp ( -0.5 ) + gauss_c2 ) - gauss_d3) / gauss_d1_);
68 
70  max_iterations_ = 35;
71 }
72 
73 
74 template<typename PointSource, typename PointTarget> void
76 {
77  nr_iterations_ = 0;
78  converged_ = false;
79 
80  double gauss_c1, gauss_c2, gauss_d3;
81 
82  // Initializes the gaussian fitting parameters (eq. 6.8) [Magnusson 2009]
83  gauss_c1 = 10 * (1 - outlier_ratio_);
84  gauss_c2 = outlier_ratio_ / pow (resolution_, 3);
85  gauss_d3 = -std::log (gauss_c2);
86  gauss_d1_ = -std::log ( gauss_c1 + gauss_c2 ) - gauss_d3;
87  gauss_d2_ = -2 * std::log ((-std::log ( gauss_c1 * std::exp ( -0.5 ) + gauss_c2 ) - gauss_d3) / gauss_d1_);
88 
89  if (guess != Eigen::Matrix4f::Identity ())
90  {
91  // Initialise final transformation to the guessed one
92  final_transformation_ = guess;
93  // Apply guessed transformation prior to search for neighbours
94  transformPointCloud (output, output, guess);
95  }
96 
97  // Initialize Point Gradient and Hessian
98  point_gradient_.setZero ();
99  point_gradient_.block<3, 3>(0, 0).setIdentity ();
100  point_hessian_.setZero ();
101 
102  Eigen::Transform<float, 3, Eigen::Affine, Eigen::ColMajor> eig_transformation;
103  eig_transformation.matrix () = final_transformation_;
104 
105  // Convert initial guess matrix to 6 element transformation vector
106  Eigen::Matrix<double, 6, 1> p, delta_p, score_gradient;
107  Eigen::Vector3f init_translation = eig_transformation.translation ();
108  Eigen::Vector3f init_rotation = eig_transformation.rotation ().eulerAngles (0, 1, 2);
109  p << init_translation (0), init_translation (1), init_translation (2),
110  init_rotation (0), init_rotation (1), init_rotation (2);
111 
112  Eigen::Matrix<double, 6, 6> hessian;
113 
114  double score = 0;
115 
116  // Calculate derivates of initial transform vector, subsequent derivative calculations are done in the step length determination.
117  score = computeDerivatives (score_gradient, hessian, output, p);
118 
119  while (!converged_)
120  {
121  // Store previous transformation
122  previous_transformation_ = transformation_;
123 
124  // Solve for decent direction using newton method, line 23 in Algorithm 2 [Magnusson 2009]
125  Eigen::JacobiSVD<Eigen::Matrix<double, 6, 6> > sv (hessian, Eigen::ComputeFullU | Eigen::ComputeFullV);
126  // Negative for maximization as opposed to minimization
127  delta_p = sv.solve (-score_gradient);
128 
129  //Calculate step length with guarnteed sufficient decrease [More, Thuente 1994]
130  double delta_p_norm = delta_p.norm ();
131 
132  if (delta_p_norm == 0 || std::isnan(delta_p_norm))
133  {
134  trans_probability_ = score / static_cast<double> (input_->points.size ());
135  converged_ = delta_p_norm == delta_p_norm;
136  return;
137  }
138 
139  delta_p.normalize ();
140  delta_p_norm = computeStepLengthMT (p, delta_p, delta_p_norm, step_size_, transformation_epsilon_ / 2, score, score_gradient, hessian, output);
141  delta_p *= delta_p_norm;
142 
143 
144  transformation_ = (Eigen::Translation<float, 3> (static_cast<float> (delta_p (0)), static_cast<float> (delta_p (1)), static_cast<float> (delta_p (2))) *
145  Eigen::AngleAxis<float> (static_cast<float> (delta_p (3)), Eigen::Vector3f::UnitX ()) *
146  Eigen::AngleAxis<float> (static_cast<float> (delta_p (4)), Eigen::Vector3f::UnitY ()) *
147  Eigen::AngleAxis<float> (static_cast<float> (delta_p (5)), Eigen::Vector3f::UnitZ ())).matrix ();
148 
149 
150  p += delta_p;
151 
152  // Update Visualizer (untested)
153  if (update_visualizer_)
154  update_visualizer_ (output, std::vector<int>(), *target_, std::vector<int>() );
155 
156  double cos_angle = 0.5 * (transformation_.coeff (0, 0) + transformation_.coeff (1, 1) + transformation_.coeff (2, 2) - 1);
157  double translation_sqr = transformation_.coeff (0, 3) * transformation_.coeff (0, 3) +
158  transformation_.coeff (1, 3) * transformation_.coeff (1, 3) +
159  transformation_.coeff (2, 3) * transformation_.coeff (2, 3);
160 
161  nr_iterations_++;
162 
163  if (nr_iterations_ >= max_iterations_ ||
164  ((transformation_epsilon_ > 0 && translation_sqr <= transformation_epsilon_) && (transformation_rotation_epsilon_ > 0 && cos_angle >= transformation_rotation_epsilon_)) ||
165  ((transformation_epsilon_ <= 0) && (transformation_rotation_epsilon_ > 0 && cos_angle >= transformation_rotation_epsilon_)) ||
166  ((transformation_epsilon_ > 0 && translation_sqr <= transformation_epsilon_) && (transformation_rotation_epsilon_ <= 0)))
167  {
168  converged_ = true;
169  }
170  }
171 
172  // Store transformation probability. The realtive differences within each scan registration are accurate
173  // but the normalization constants need to be modified for it to be globally accurate
174  trans_probability_ = score / static_cast<double> (input_->points.size ());
175 }
176 
177 
178 template<typename PointSource, typename PointTarget> double
180  Eigen::Matrix<double, 6, 6> &hessian,
181  PointCloudSource &trans_cloud,
182  Eigen::Matrix<double, 6, 1> &p,
183  bool compute_hessian)
184 {
185  // Original Point and Transformed Point
186  PointSource x_pt, x_trans_pt;
187  // Original Point and Transformed Point (for math)
188  Eigen::Vector3d x, x_trans;
189  // Occupied Voxel
191  // Inverse Covariance of Occupied Voxel
192  Eigen::Matrix3d c_inv;
193 
194  score_gradient.setZero ();
195  hessian.setZero ();
196  double score = 0;
197 
198  // Precompute Angular Derivatives (eq. 6.19 and 6.21)[Magnusson 2009]
199  computeAngleDerivatives (p);
200 
201  // Update gradient and hessian for each point, line 17 in Algorithm 2 [Magnusson 2009]
202  for (std::size_t idx = 0; idx < input_->points.size (); idx++)
203  {
204  x_trans_pt = trans_cloud.points[idx];
205 
206  // Find nieghbors (Radius search has been experimentally faster than direct neighbor checking.
207  std::vector<TargetGridLeafConstPtr> neighborhood;
208  std::vector<float> distances;
209  target_cells_.radiusSearch (x_trans_pt, resolution_, neighborhood, distances);
210 
211  for (typename std::vector<TargetGridLeafConstPtr>::iterator neighborhood_it = neighborhood.begin (); neighborhood_it != neighborhood.end (); ++neighborhood_it)
212  {
213  cell = *neighborhood_it;
214  x_pt = input_->points[idx];
215  x = Eigen::Vector3d (x_pt.x, x_pt.y, x_pt.z);
216 
217  x_trans = Eigen::Vector3d (x_trans_pt.x, x_trans_pt.y, x_trans_pt.z);
218 
219  // Denorm point, x_k' in Equations 6.12 and 6.13 [Magnusson 2009]
220  x_trans -= cell->getMean ();
221  // Uses precomputed covariance for speed.
222  c_inv = cell->getInverseCov ();
223 
224  // Compute derivative of transform function w.r.t. transform vector, J_E and H_E 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 Equations 6.10, 6.12 and 6.13, respectively [Magnusson 2009]
227  score += updateDerivatives (score_gradient, hessian, x_trans, c_inv, compute_hessian);
228 
229  }
230  }
231  return (score);
232 }
233 
234 
235 template<typename PointSource, typename PointTarget> void
236 NormalDistributionsTransform<PointSource, PointTarget>::computeAngleDerivatives (Eigen::Matrix<double, 6, 1> &p, bool compute_hessian)
237 {
238  // Simplified math for near 0 angles
239  double cx, cy, cz, sx, sy, sz;
240  if (std::abs (p (3)) < 10e-5)
241  {
242  //p(3) = 0;
243  cx = 1.0;
244  sx = 0.0;
245  }
246  else
247  {
248  cx = std::cos (p (3));
249  sx = sin (p (3));
250  }
251  if (std::abs (p (4)) < 10e-5)
252  {
253  //p(4) = 0;
254  cy = 1.0;
255  sy = 0.0;
256  }
257  else
258  {
259  cy = std::cos (p (4));
260  sy = sin (p (4));
261  }
262 
263  if (std::abs (p (5)) < 10e-5)
264  {
265  //p(5) = 0;
266  cz = 1.0;
267  sz = 0.0;
268  }
269  else
270  {
271  cz = std::cos (p (5));
272  sz = sin (p (5));
273  }
274 
275  // Precomputed angular gradiant components. Letters correspond to Equation 6.19 [Magnusson 2009]
276  j_ang_a_ << (-sx * sz + cx * sy * cz), (-sx * cz - cx * sy * sz), (-cx * cy);
277  j_ang_b_ << (cx * sz + sx * sy * cz), (cx * cz - sx * sy * sz), (-sx * cy);
278  j_ang_c_ << (-sy * cz), sy * sz, cy;
279  j_ang_d_ << sx * cy * cz, (-sx * cy * sz), sx * sy;
280  j_ang_e_ << (-cx * cy * cz), cx * cy * sz, (-cx * sy);
281  j_ang_f_ << (-cy * sz), (-cy * cz), 0;
282  j_ang_g_ << (cx * cz - sx * sy * sz), (-cx * sz - sx * sy * cz), 0;
283  j_ang_h_ << (sx * cz + cx * sy * sz), (cx * sy * cz - sx * sz), 0;
284 
285  if (compute_hessian)
286  {
287  // Precomputed angular hessian components. Letters correspond to Equation 6.21 and numbers correspond to row index [Magnusson 2009]
288  h_ang_a2_ << (-cx * sz - sx * sy * cz), (-cx * cz + sx * sy * sz), sx * cy;
289  h_ang_a3_ << (-sx * sz + cx * sy * cz), (-cx * sy * sz - sx * cz), (-cx * cy);
290 
291  h_ang_b2_ << (cx * cy * cz), (-cx * cy * sz), (cx * sy);
292  h_ang_b3_ << (sx * cy * cz), (-sx * cy * sz), (sx * sy);
293 
294  h_ang_c2_ << (-sx * cz - cx * sy * sz), (sx * sz - cx * sy * cz), 0;
295  h_ang_c3_ << (cx * cz - sx * sy * sz), (-sx * sy * cz - cx * sz), 0;
296 
297  h_ang_d1_ << (-cy * cz), (cy * sz), (sy);
298  h_ang_d2_ << (-sx * sy * cz), (sx * sy * sz), (sx * cy);
299  h_ang_d3_ << (cx * sy * cz), (-cx * sy * sz), (-cx * cy);
300 
301  h_ang_e1_ << (sy * sz), (sy * cz), 0;
302  h_ang_e2_ << (-sx * cy * sz), (-sx * cy * cz), 0;
303  h_ang_e3_ << (cx * cy * sz), (cx * cy * cz), 0;
304 
305  h_ang_f1_ << (-cy * cz), (cy * sz), 0;
306  h_ang_f2_ << (-cx * sz - sx * sy * cz), (-cx * cz + sx * sy * sz), 0;
307  h_ang_f3_ << (-sx * sz + cx * sy * cz), (-cx * sy * sz - sx * cz), 0;
308  }
309 }
310 
311 
312 template<typename PointSource, typename PointTarget> void
314 {
315  // Calculate first derivative of Transformation Equation 6.17 w.r.t. transform vector p.
316  // Derivative w.r.t. ith element of transform vector corresponds to column i, Equation 6.18 and 6.19 [Magnusson 2009]
317  point_gradient_ (1, 3) = x.dot (j_ang_a_);
318  point_gradient_ (2, 3) = x.dot (j_ang_b_);
319  point_gradient_ (0, 4) = x.dot (j_ang_c_);
320  point_gradient_ (1, 4) = x.dot (j_ang_d_);
321  point_gradient_ (2, 4) = x.dot (j_ang_e_);
322  point_gradient_ (0, 5) = x.dot (j_ang_f_);
323  point_gradient_ (1, 5) = x.dot (j_ang_g_);
324  point_gradient_ (2, 5) = x.dot (j_ang_h_);
325 
326  if (compute_hessian)
327  {
328  // Vectors from Equation 6.21 [Magnusson 2009]
329  Eigen::Vector3d a, b, c, d, e, f;
330 
331  a << 0, x.dot (h_ang_a2_), x.dot (h_ang_a3_);
332  b << 0, x.dot (h_ang_b2_), x.dot (h_ang_b3_);
333  c << 0, x.dot (h_ang_c2_), x.dot (h_ang_c3_);
334  d << x.dot (h_ang_d1_), x.dot (h_ang_d2_), x.dot (h_ang_d3_);
335  e << x.dot (h_ang_e1_), x.dot (h_ang_e2_), x.dot (h_ang_e3_);
336  f << x.dot (h_ang_f1_), x.dot (h_ang_f2_), x.dot (h_ang_f3_);
337 
338  // Calculate second derivative of Transformation Equation 6.17 w.r.t. transform vector p.
339  // Derivative w.r.t. ith and jth elements of transform vector corresponds to the 3x1 block matrix starting at (3i,j), Equation 6.20 and 6.21 [Magnusson 2009]
340  point_hessian_.block<3, 1>(9, 3) = a;
341  point_hessian_.block<3, 1>(12, 3) = b;
342  point_hessian_.block<3, 1>(15, 3) = c;
343  point_hessian_.block<3, 1>(9, 4) = b;
344  point_hessian_.block<3, 1>(12, 4) = d;
345  point_hessian_.block<3, 1>(15, 4) = e;
346  point_hessian_.block<3, 1>(9, 5) = c;
347  point_hessian_.block<3, 1>(12, 5) = e;
348  point_hessian_.block<3, 1>(15, 5) = f;
349  }
350 }
351 
352 
353 template<typename PointSource, typename PointTarget> double
355  Eigen::Matrix<double, 6, 6> &hessian,
356  Eigen::Vector3d &x_trans, Eigen::Matrix3d &c_inv,
357  bool compute_hessian)
358 {
359  Eigen::Vector3d cov_dxd_pi;
360  // e^(-d_2/2 * (x_k - mu_k)^T Sigma_k^-1 (x_k - mu_k)) Equation 6.9 [Magnusson 2009]
361  double e_x_cov_x = std::exp (-gauss_d2_ * x_trans.dot (c_inv * x_trans) / 2);
362  // Calculate probability of transformed points existence, Equation 6.9 [Magnusson 2009]
363  double score_inc = -gauss_d1_ * e_x_cov_x;
364 
365  e_x_cov_x = gauss_d2_ * e_x_cov_x;
366 
367  // Error checking for invalid values.
368  if (e_x_cov_x > 1 || e_x_cov_x < 0 || std::isnan(e_x_cov_x))
369  return (0);
370 
371  // Reusable portion of Equation 6.12 and 6.13 [Magnusson 2009]
372  e_x_cov_x *= gauss_d1_;
373 
374 
375  for (int i = 0; i < 6; i++)
376  {
377  // Sigma_k^-1 d(T(x,p))/dpi, Reusable portion of Equation 6.12 and 6.13 [Magnusson 2009]
378  cov_dxd_pi = c_inv * point_gradient_.col (i);
379 
380  // Update gradient, Equation 6.12 [Magnusson 2009]
381  score_gradient (i) += x_trans.dot (cov_dxd_pi) * e_x_cov_x;
382 
383  if (compute_hessian)
384  {
385  for (Eigen::Index j = 0; j < hessian.cols (); j++)
386  {
387  // Update hessian, Equation 6.13 [Magnusson 2009]
388  hessian (i, j) += e_x_cov_x * (-gauss_d2_ * x_trans.dot (cov_dxd_pi) * x_trans.dot (c_inv * point_gradient_.col (j)) +
389  x_trans.dot (c_inv * point_hessian_.block<3, 1>(3 * i, j)) +
390  point_gradient_.col (j).dot (cov_dxd_pi) );
391  }
392  }
393  }
394 
395  return (score_inc);
396 }
397 
398 
399 template<typename PointSource, typename PointTarget> void
401  PointCloudSource &trans_cloud, Eigen::Matrix<double, 6, 1> &)
402 {
403  // Original Point and Transformed Point
404  PointSource x_pt, x_trans_pt;
405  // Original Point and Transformed Point (for math)
406  Eigen::Vector3d x, x_trans;
407  // Occupied Voxel
409  // Inverse Covariance of Occupied Voxel
410  Eigen::Matrix3d c_inv;
411 
412  hessian.setZero ();
413 
414  // Precompute Angular Derivatives unessisary because only used after regular derivative calculation
415 
416  // Update hessian for each point, line 17 in Algorithm 2 [Magnusson 2009]
417  for (std::size_t idx = 0; idx < input_->points.size (); idx++)
418  {
419  x_trans_pt = trans_cloud.points[idx];
420 
421  // Find nieghbors (Radius search has been experimentally faster than direct neighbor checking.
422  std::vector<TargetGridLeafConstPtr> neighborhood;
423  std::vector<float> distances;
424  target_cells_.radiusSearch (x_trans_pt, resolution_, neighborhood, distances);
425 
426  for (typename std::vector<TargetGridLeafConstPtr>::iterator neighborhood_it = neighborhood.begin (); neighborhood_it != neighborhood.end (); ++neighborhood_it)
427  {
428  cell = *neighborhood_it;
429 
430  {
431  x_pt = input_->points[idx];
432  x = Eigen::Vector3d (x_pt.x, x_pt.y, x_pt.z);
433 
434  x_trans = Eigen::Vector3d (x_trans_pt.x, x_trans_pt.y, x_trans_pt.z);
435 
436  // Denorm point, x_k' in Equations 6.12 and 6.13 [Magnusson 2009]
437  x_trans -= cell->getMean ();
438  // Uses precomputed covariance for speed.
439  c_inv = cell->getInverseCov ();
440 
441  // Compute derivative of transform function w.r.t. transform vector, J_E and H_E in Equations 6.18 and 6.20 [Magnusson 2009]
442  computePointDerivatives (x);
443  // Update hessian, lines 21 in Algorithm 2, according to Equations 6.10, 6.12 and 6.13, respectively [Magnusson 2009]
444  updateHessian (hessian, x_trans, c_inv);
445  }
446  }
447  }
448 }
449 
450 
451 template<typename PointSource, typename PointTarget> void
452 NormalDistributionsTransform<PointSource, PointTarget>::updateHessian (Eigen::Matrix<double, 6, 6> &hessian, Eigen::Vector3d &x_trans, Eigen::Matrix3d &c_inv)
453 {
454  Eigen::Vector3d cov_dxd_pi;
455  // e^(-d_2/2 * (x_k - mu_k)^T Sigma_k^-1 (x_k - mu_k)) Equation 6.9 [Magnusson 2009]
456  double e_x_cov_x = gauss_d2_ * std::exp (-gauss_d2_ * x_trans.dot (c_inv * x_trans) / 2);
457 
458  // Error checking for invalid values.
459  if (e_x_cov_x > 1 || e_x_cov_x < 0 || std::isnan(e_x_cov_x))
460  return;
461 
462  // Reusable portion of Equation 6.12 and 6.13 [Magnusson 2009]
463  e_x_cov_x *= gauss_d1_;
464 
465  for (int i = 0; i < 6; i++)
466  {
467  // Sigma_k^-1 d(T(x,p))/dpi, Reusable portion of Equation 6.12 and 6.13 [Magnusson 2009]
468  cov_dxd_pi = c_inv * point_gradient_.col (i);
469 
470  for (Eigen::Index j = 0; j < hessian.cols (); j++)
471  {
472  // Update hessian, Equation 6.13 [Magnusson 2009]
473  hessian (i, j) += e_x_cov_x * (-gauss_d2_ * x_trans.dot (cov_dxd_pi) * x_trans.dot (c_inv * point_gradient_.col (j)) +
474  x_trans.dot (c_inv * point_hessian_.block<3, 1>(3 * i, j)) +
475  point_gradient_.col (j).dot (cov_dxd_pi) );
476  }
477  }
478 
479 }
480 
481 
482 template<typename PointSource, typename PointTarget> bool
484  double &a_u, double &f_u, double &g_u,
485  double a_t, double f_t, double g_t)
486 {
487  // Case U1 in Update Algorithm and Case a in Modified Update Algorithm [More, Thuente 1994]
488  if (f_t > f_l)
489  {
490  a_u = a_t;
491  f_u = f_t;
492  g_u = g_t;
493  return (false);
494  }
495  // Case U2 in Update Algorithm and Case b in Modified Update Algorithm [More, Thuente 1994]
496  if (g_t * (a_l - a_t) > 0)
497  {
498  a_l = a_t;
499  f_l = f_t;
500  g_l = g_t;
501  return (false);
502  }
503  // Case U3 in Update Algorithm and Case c in Modified Update Algorithm [More, Thuente 1994]
504  if (g_t * (a_l - a_t) < 0)
505  {
506  a_u = a_l;
507  f_u = f_l;
508  g_u = g_l;
509 
510  a_l = a_t;
511  f_l = f_t;
512  g_l = g_t;
513  return (false);
514  }
515  // Interval Converged
516  return (true);
517 }
518 
519 
520 template<typename PointSource, typename PointTarget> double
522  double a_u, double f_u, double g_u,
523  double a_t, double f_t, double g_t)
524 {
525  // Case 1 in Trial Value Selection [More, Thuente 1994]
526  if (f_t > f_l)
527  {
528  // Calculate the minimizer of the cubic that interpolates f_l, f_t, g_l and g_t
529  // Equation 2.4.52 [Sun, Yuan 2006]
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);
532  // Equation 2.4.56 [Sun, Yuan 2006]
533  double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
534 
535  // Calculate the minimizer of the quadratic that interpolates f_l, f_t and g_l
536  // Equation 2.4.2 [Sun, Yuan 2006]
537  double a_q = a_l - 0.5 * (a_l - a_t) * g_l / (g_l - (f_l - f_t) / (a_l - a_t));
538 
539  if (std::fabs (a_c - a_l) < std::fabs (a_q - a_l))
540  return (a_c);
541  return (0.5 * (a_q + a_c));
542  }
543  // Case 2 in Trial Value Selection [More, Thuente 1994]
544  if (g_t * g_l < 0)
545  {
546  // Calculate the minimizer of the cubic that interpolates f_l, f_t, g_l and g_t
547  // Equation 2.4.52 [Sun, Yuan 2006]
548  double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
549  double w = std::sqrt (z * z - g_t * g_l);
550  // Equation 2.4.56 [Sun, Yuan 2006]
551  double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
552 
553  // Calculate the minimizer of the quadratic that interpolates f_l, g_l and g_t
554  // Equation 2.4.5 [Sun, Yuan 2006]
555  double a_s = a_l - (a_l - a_t) / (g_l - g_t) * g_l;
556 
557  if (std::fabs (a_c - a_t) >= std::fabs (a_s - a_t))
558  return (a_c);
559  return (a_s);
560  }
561  // Case 3 in Trial Value Selection [More, Thuente 1994]
562  if (std::fabs (g_t) <= std::fabs (g_l))
563  {
564  // Calculate the minimizer of the cubic that interpolates f_l, f_t, g_l and g_t
565  // Equation 2.4.52 [Sun, Yuan 2006]
566  double z = 3 * (f_t - f_l) / (a_t - a_l) - g_t - g_l;
567  double w = std::sqrt (z * z - g_t * g_l);
568  double a_c = a_l + (a_t - a_l) * (w - g_l - z) / (g_t - g_l + 2 * w);
569 
570  // Calculate the minimizer of the quadratic that interpolates g_l and g_t
571  // Equation 2.4.5 [Sun, Yuan 2006]
572  double a_s = a_l - (a_l - a_t) / (g_l - g_t) * g_l;
573 
574  double a_t_next;
575 
576  if (std::fabs (a_c - a_t) < std::fabs (a_s - a_t))
577  a_t_next = a_c;
578  else
579  a_t_next = a_s;
580 
581  if (a_t > a_l)
582  return (std::min (a_t + 0.66 * (a_u - a_t), a_t_next));
583  return (std::max (a_t + 0.66 * (a_u - a_t), a_t_next));
584  }
585  // Case 4 in Trial Value Selection [More, Thuente 1994]
586  // Calculate the minimizer of the cubic that interpolates f_u, f_t, g_u and g_t
587  // Equation 2.4.52 [Sun, Yuan 2006]
588  double z = 3 * (f_t - f_u) / (a_t - a_u) - g_t - g_u;
589  double w = std::sqrt (z * z - g_t * g_u);
590  // Equation 2.4.56 [Sun, Yuan 2006]
591  return (a_u + (a_t - a_u) * (w - g_u - z) / (g_t - g_u + 2 * w));
592 }
593 
594 
595 template<typename PointSource, typename PointTarget> double
596 NormalDistributionsTransform<PointSource, PointTarget>::computeStepLengthMT (const Eigen::Matrix<double, 6, 1> &x, Eigen::Matrix<double, 6, 1> &step_dir, double step_init, double step_max,
597  double step_min, double &score, Eigen::Matrix<double, 6, 1> &score_gradient, Eigen::Matrix<double, 6, 6> &hessian,
598  PointCloudSource &trans_cloud)
599 {
600  // Set the value of phi(0), Equation 1.3 [More, Thuente 1994]
601  double phi_0 = -score;
602  // Set the value of phi'(0), Equation 1.3 [More, Thuente 1994]
603  double d_phi_0 = -(score_gradient.dot (step_dir));
604 
605  Eigen::Matrix<double, 6, 1> x_t;
606 
607  if (d_phi_0 >= 0)
608  {
609  // Not a decent direction
610  if (d_phi_0 == 0)
611  return 0;
612  // Reverse step direction and calculate optimal step.
613  d_phi_0 *= -1;
614  step_dir *= -1;
615 
616  }
617 
618  // The Search Algorithm for T(mu) [More, Thuente 1994]
619 
620  int max_step_iterations = 10;
621  int step_iterations = 0;
622 
623  // Sufficient decreace constant, Equation 1.1 [More, Thuete 1994]
624  double mu = 1.e-4;
625  // Curvature condition constant, Equation 1.2 [More, Thuete 1994]
626  double nu = 0.9;
627 
628  // Initial endpoints of Interval I,
629  double a_l = 0, a_u = 0;
630 
631  // Auxiliary function psi is used until I is determined ot be a closed interval, Equation 2.1 [More, Thuente 1994]
632  double f_l = auxilaryFunction_PsiMT (a_l, phi_0, phi_0, d_phi_0, mu);
633  double g_l = auxilaryFunction_dPsiMT (d_phi_0, d_phi_0, mu);
634 
635  double f_u = auxilaryFunction_PsiMT (a_u, phi_0, phi_0, d_phi_0, mu);
636  double g_u = auxilaryFunction_dPsiMT (d_phi_0, d_phi_0, mu);
637 
638  // Check used to allow More-Thuente step length calculation to be skipped by making step_min == step_max
639  bool interval_converged = (step_max - step_min) > 0, open_interval = true;
640 
641  double a_t = step_init;
642  a_t = std::min (a_t, step_max);
643  a_t = std::max (a_t, step_min);
644 
645  x_t = x + step_dir * a_t;
646 
647  final_transformation_ = (Eigen::Translation<float, 3>(static_cast<float> (x_t (0)), static_cast<float> (x_t (1)), static_cast<float> (x_t (2))) *
648  Eigen::AngleAxis<float> (static_cast<float> (x_t (3)), Eigen::Vector3f::UnitX ()) *
649  Eigen::AngleAxis<float> (static_cast<float> (x_t (4)), Eigen::Vector3f::UnitY ()) *
650  Eigen::AngleAxis<float> (static_cast<float> (x_t (5)), Eigen::Vector3f::UnitZ ())).matrix ();
651 
652  // New transformed point cloud
653  transformPointCloud (*input_, trans_cloud, final_transformation_);
654 
655  // Updates score, gradient and hessian. Hessian calculation is unessisary but testing showed that most step calculations use the
656  // initial step suggestion and recalculation the reusable portions of the hessian would intail more computation time.
657  score = computeDerivatives (score_gradient, hessian, trans_cloud, x_t, true);
658 
659  // Calculate phi(alpha_t)
660  double phi_t = -score;
661  // Calculate phi'(alpha_t)
662  double d_phi_t = -(score_gradient.dot (step_dir));
663 
664  // Calculate psi(alpha_t)
665  double psi_t = auxilaryFunction_PsiMT (a_t, phi_t, phi_0, d_phi_0, mu);
666  // Calculate psi'(alpha_t)
667  double d_psi_t = auxilaryFunction_dPsiMT (d_phi_t, d_phi_0, mu);
668 
669  // Iterate until max number of iterations, interval convergance or a value satisfies the sufficient decrease, Equation 1.1, and curvature condition, Equation 1.2 [More, Thuente 1994]
670  while (!interval_converged && step_iterations < max_step_iterations && !(psi_t <= 0 /*Sufficient Decrease*/ && d_phi_t <= -nu * d_phi_0 /*Curvature Condition*/))
671  {
672  // Use auxiliary function if interval I is not closed
673  if (open_interval)
674  {
675  a_t = trialValueSelectionMT (a_l, f_l, g_l,
676  a_u, f_u, g_u,
677  a_t, psi_t, d_psi_t);
678  }
679  else
680  {
681  a_t = trialValueSelectionMT (a_l, f_l, g_l,
682  a_u, f_u, g_u,
683  a_t, phi_t, d_phi_t);
684  }
685 
686  a_t = std::min (a_t, step_max);
687  a_t = std::max (a_t, step_min);
688 
689  x_t = x + step_dir * a_t;
690 
691  final_transformation_ = (Eigen::Translation<float, 3> (static_cast<float> (x_t (0)), static_cast<float> (x_t (1)), static_cast<float> (x_t (2))) *
692  Eigen::AngleAxis<float> (static_cast<float> (x_t (3)), Eigen::Vector3f::UnitX ()) *
693  Eigen::AngleAxis<float> (static_cast<float> (x_t (4)), Eigen::Vector3f::UnitY ()) *
694  Eigen::AngleAxis<float> (static_cast<float> (x_t (5)), Eigen::Vector3f::UnitZ ())).matrix ();
695 
696  // New transformed point cloud
697  // Done on final cloud to prevent wasted computation
698  transformPointCloud (*input_, trans_cloud, final_transformation_);
699 
700  // Updates score, gradient. Values stored to prevent wasted computation.
701  score = computeDerivatives (score_gradient, hessian, trans_cloud, x_t, false);
702 
703  // Calculate phi(alpha_t+)
704  phi_t = -score;
705  // Calculate phi'(alpha_t+)
706  d_phi_t = -(score_gradient.dot (step_dir));
707 
708  // Calculate psi(alpha_t+)
709  psi_t = auxilaryFunction_PsiMT (a_t, phi_t, phi_0, d_phi_0, mu);
710  // Calculate psi'(alpha_t+)
711  d_psi_t = auxilaryFunction_dPsiMT (d_phi_t, d_phi_0, mu);
712 
713  // Check if I is now a closed interval
714  if (open_interval && (psi_t <= 0 && d_psi_t >= 0))
715  {
716  open_interval = false;
717 
718  // Converts f_l and g_l from psi to phi
719  f_l += phi_0 - mu * d_phi_0 * a_l;
720  g_l += mu * d_phi_0;
721 
722  // Converts f_u and g_u from psi to phi
723  f_u += phi_0 - mu * d_phi_0 * a_u;
724  g_u += mu * d_phi_0;
725  }
726 
727  if (open_interval)
728  {
729  // Update interval end points using Updating Algorithm [More, Thuente 1994]
730  interval_converged = updateIntervalMT (a_l, f_l, g_l,
731  a_u, f_u, g_u,
732  a_t, psi_t, d_psi_t);
733  }
734  else
735  {
736  // Update interval end points using Modified Updating Algorithm [More, Thuente 1994]
737  interval_converged = updateIntervalMT (a_l, f_l, g_l,
738  a_u, f_u, g_u,
739  a_t, phi_t, d_phi_t);
740  }
741 
742  step_iterations++;
743  }
744 
745  // If inner loop was run then hessian needs to be calculated.
746  // Hessian is unnessisary for step length determination but gradients are required
747  // so derivative and transform data is stored for the next iteration.
748  if (step_iterations)
749  computeHessian (hessian, trans_cloud, x_t);
750 
751  return (a_t);
752 }
753 
754 } // namespace pcl
755 
756 #endif // PCL_REGISTRATION_NDT_IMPL_H_
757 
double computeDerivatives(Eigen::Matrix< double, 6, 1 > &score_gradient, Eigen::Matrix< double, 6, 6 > &hessian, PointCloudSource &trans_cloud, Eigen::Matrix< double, 6, 1 > &p, bool compute_hessian=true)
Compute derivatives of probability function w.r.t.
Definition: ndt.hpp:179
float resolution_
The side length of voxels.
Definition: ndt.h:425
typename Registration< PointSource, PointTarget >::PointCloudSource PointCloudSource
Definition: ndt.h:68
double updateDerivatives(Eigen::Matrix< double, 6, 1 > &score_gradient, Eigen::Matrix< double, 6, 6 > &hessian, Eigen::Vector3d &x_trans, Eigen::Matrix3d &c_inv, bool compute_hessian=true)
Compute individual point contirbutions to derivatives of probability function w.r.t.
Definition: ndt.hpp:354
typename TargetGrid::LeafConstPtr TargetGridLeafConstPtr
Typename of const pointer to searchable voxel grid leaf.
Definition: ndt.h:86
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)
Select new trial value for More-Thuente method.
Definition: ndt.hpp:521
int max_iterations_
The maximum number of iterations the internal optimization should run for.
Definition: registration.h:504
double computeStepLengthMT(const Eigen::Matrix< double, 6, 1 > &x, 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 probability derivatives using More-Thuente m...
Definition: ndt.hpp:596
void updateHessian(Eigen::Matrix< double, 6, 6 > &hessian, Eigen::Vector3d &x_trans, Eigen::Matrix3d &c_inv)
Compute individual point contirbutions to hessian of probability function w.r.t.
Definition: ndt.hpp:452
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)
Apply an affine transform defined by an Eigen Transform.
Definition: transforms.hpp:221
double outlier_ratio_
The ratio of outliers of points w.r.t.
Definition: ndt.h:431
double gauss_d1_
The normalization constants used fit the point distribution to a normal distribution, Equation 6.8 [Magnusson 2009].
Definition: ndt.h:434
void computeAngleDerivatives(Eigen::Matrix< double, 6, 1 > &p, bool compute_hessian=true)
Precompute anglular components of derivatives.
Definition: ndt.hpp:236
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)
Update interval of possible step lengths for More-Thuente method, in More-Thuente (1994) ...
Definition: ndt.hpp:483
double transformation_epsilon_
The maximum difference between two consecutive transformations in order to consider convergence (user...
Definition: registration.h:524
NormalDistributionsTransform()
Constructor.
Definition: ndt.hpp:49
std::string reg_name_
The registration method name.
Definition: registration.h:490
void computePointDerivatives(Eigen::Vector3d &x, bool compute_hessian=true)
Compute point derivatives.
Definition: ndt.hpp:313
virtual void computeTransformation(PointCloudSource &output)
Estimate the transformation and returns the transformed source (input) as output. ...
Definition: ndt.h:240
void computeHessian(Eigen::Matrix< double, 6, 6 > &hessian, PointCloudSource &trans_cloud, Eigen::Matrix< double, 6, 1 > &p)
Compute hessian of probability function w.r.t.
Definition: ndt.hpp:400