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