lsq_registration.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /************************************************************
  2. *
  3. * Copyright (c) 2021, University of California, Los Angeles
  4. *
  5. * Authors: Kenny J. Chen, Brett T. Lopez
  6. * Contact: kennyjchen@ucla.edu, btlopez@ucla.edu
  7. *
  8. ***********************************************************/
  9. /***********************************************************************
  10. * BSD 3-Clause License
  11. *
  12. * Copyright (c) 2020, SMRT-AIST
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright notice, this
  19. * list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. *
  25. * 3. Neither the name of the copyright holder nor the names of its
  26. * contributors may be used to endorse or promote products derived from
  27. * this software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  30. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  32. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  33. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  35. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  37. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *************************************************************************/
  40. #ifndef NANO_GICP_LSQ_REGISTRATION_HPP
  41. #define NANO_GICP_LSQ_REGISTRATION_HPP
  42. #include <Eigen/Core>
  43. #include <Eigen/Geometry>
  44. #include <pcl/point_types.h>
  45. #include <pcl/point_cloud.h>
  46. #include <pcl/registration/registration.h>
  47. namespace nano_gicp {
  48. enum class LSQ_OPTIMIZER_TYPE { GaussNewton, LevenbergMarquardt };
  49. template<typename PointSource, typename PointTarget>
  50. class LsqRegistration : public pcl::Registration<PointSource, PointTarget, float> {
  51. public:
  52. using Scalar = float;
  53. using Matrix4 = typename pcl::Registration<PointSource, PointTarget, Scalar>::Matrix4;
  54. using PointCloudSource = typename pcl::Registration<PointSource, PointTarget, Scalar>::PointCloudSource;
  55. using PointCloudSourcePtr = typename PointCloudSource::Ptr;
  56. using PointCloudSourceConstPtr = typename PointCloudSource::ConstPtr;
  57. using PointCloudTarget = typename pcl::Registration<PointSource, PointTarget, Scalar>::PointCloudTarget;
  58. using PointCloudTargetPtr = typename PointCloudTarget::Ptr;
  59. using PointCloudTargetConstPtr = typename PointCloudTarget::ConstPtr;
  60. protected:
  61. using pcl::Registration<PointSource, PointTarget, Scalar>::input_;
  62. using pcl::Registration<PointSource, PointTarget, Scalar>::nr_iterations_;
  63. using pcl::Registration<PointSource, PointTarget, Scalar>::max_iterations_;
  64. using pcl::Registration<PointSource, PointTarget, Scalar>::final_transformation_;
  65. using pcl::Registration<PointSource, PointTarget, Scalar>::transformation_epsilon_;
  66. using pcl::Registration<PointSource, PointTarget, Scalar>::converged_;
  67. public:
  68. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  69. LsqRegistration();
  70. virtual ~LsqRegistration();
  71. void setRotationEpsilon(double eps);
  72. void setInitialLambdaFactor(double init_lambda_factor);
  73. void setDebugPrint(bool lm_debug_print);
  74. const Eigen::Matrix<double, 6, 6>& getFinalHessian() const;
  75. virtual void swapSourceAndTarget() {}
  76. virtual void clearSource() {}
  77. virtual void clearTarget() {}
  78. protected:
  79. virtual void computeTransformation(PointCloudSource& output, const Matrix4& guess) override;
  80. bool is_converged(const Eigen::Isometry3d& delta) const;
  81. virtual double linearize(const Eigen::Isometry3d& trans, Eigen::Matrix<double, 6, 6>* H = nullptr, Eigen::Matrix<double, 6, 1>* b = nullptr) = 0;
  82. virtual double compute_error(const Eigen::Isometry3d& trans) = 0;
  83. bool step_optimize(Eigen::Isometry3d& x0, Eigen::Isometry3d& delta);
  84. bool step_gn(Eigen::Isometry3d& x0, Eigen::Isometry3d& delta);
  85. bool step_lm(Eigen::Isometry3d& x0, Eigen::Isometry3d& delta);
  86. protected:
  87. double rotation_epsilon_;
  88. LSQ_OPTIMIZER_TYPE lsq_optimizer_type_;
  89. int lm_max_iterations_;
  90. double lm_init_lambda_factor_;
  91. double lm_lambda_;
  92. bool lm_debug_print_;
  93. Eigen::Matrix<double, 6, 6> final_hessian_;
  94. };
  95. } // namespace nano_gicp
  96. #endif