nano_gicp.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_NANO_GICP_HPP
  41. #define NANO_GICP_NANO_GICP_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. #include <nano_gicp/lsq_registration.hpp>
  48. #include <nano_gicp/gicp/gicp_settings.hpp>
  49. #include <nano_gicp/nanoflann.hpp>
  50. namespace nano_gicp {
  51. template<typename PointSource, typename PointTarget>
  52. class NanoGICP : public LsqRegistration<PointSource, PointTarget> {
  53. public:
  54. using Scalar = float;
  55. using Matrix4 = typename pcl::Registration<PointSource, PointTarget, Scalar>::Matrix4;
  56. using PointCloudSource = typename pcl::Registration<PointSource, PointTarget, Scalar>::PointCloudSource;
  57. using PointCloudSourcePtr = typename PointCloudSource::Ptr;
  58. using PointCloudSourceConstPtr = typename PointCloudSource::ConstPtr;
  59. using PointCloudTarget = typename pcl::Registration<PointSource, PointTarget, Scalar>::PointCloudTarget;
  60. using PointCloudTargetPtr = typename PointCloudTarget::Ptr;
  61. using PointCloudTargetConstPtr = typename PointCloudTarget::ConstPtr;
  62. protected:
  63. using pcl::Registration<PointSource, PointTarget, Scalar>::reg_name_;
  64. using pcl::Registration<PointSource, PointTarget, Scalar>::input_;
  65. using pcl::Registration<PointSource, PointTarget, Scalar>::target_;
  66. using pcl::Registration<PointSource, PointTarget, Scalar>::corr_dist_threshold_;
  67. public:
  68. NanoGICP();
  69. virtual ~NanoGICP() override;
  70. void setNumThreads(int n);
  71. void setCorrespondenceRandomness(int k);
  72. void setRegularizationMethod(RegularizationMethod method);
  73. virtual void swapSourceAndTarget() override;
  74. virtual void clearSource() override;
  75. virtual void clearTarget() override;
  76. virtual void setInputSource(const PointCloudSourceConstPtr& cloud) override;
  77. virtual void setSourceCovariances(const std::vector<Eigen::Matrix4d, Eigen::aligned_allocator<Eigen::Matrix4d>>& covs);
  78. virtual void setInputTarget(const PointCloudTargetConstPtr& cloud) override;
  79. virtual void setTargetCovariances(const std::vector<Eigen::Matrix4d, Eigen::aligned_allocator<Eigen::Matrix4d>>& covs);
  80. virtual void registerInputSource(const PointCloudSourceConstPtr& cloud);
  81. const std::vector<Eigen::Matrix4d, Eigen::aligned_allocator<Eigen::Matrix4d>>& getSourceCovariances() const {
  82. return source_covs_;
  83. }
  84. const std::vector<Eigen::Matrix4d, Eigen::aligned_allocator<Eigen::Matrix4d>>& getTargetCovariances() const {
  85. return target_covs_;
  86. }
  87. protected:
  88. virtual void computeTransformation(PointCloudSource& output, const Matrix4& guess) override;
  89. virtual void update_correspondences(const Eigen::Isometry3d& trans);
  90. virtual double linearize(const Eigen::Isometry3d& trans, Eigen::Matrix<double, 6, 6>* H, Eigen::Matrix<double, 6, 1>* b) override;
  91. virtual double compute_error(const Eigen::Isometry3d& trans) override;
  92. template<typename PointT>
  93. bool calculate_covariances(const typename pcl::PointCloud<PointT>::ConstPtr& cloud, nanoflann::KdTreeFLANN<PointT>& kdtree, std::vector<Eigen::Matrix4d, Eigen::aligned_allocator<Eigen::Matrix4d>>& covariances);
  94. public:
  95. std::shared_ptr<nanoflann::KdTreeFLANN<PointSource>> source_kdtree_;
  96. std::shared_ptr<nanoflann::KdTreeFLANN<PointTarget>> target_kdtree_;
  97. std::vector<Eigen::Matrix4d, Eigen::aligned_allocator<Eigen::Matrix4d>> source_covs_;
  98. std::vector<Eigen::Matrix4d, Eigen::aligned_allocator<Eigen::Matrix4d>> target_covs_;
  99. protected:
  100. int num_threads_;
  101. int k_correspondences_;
  102. RegularizationMethod regularization_method_;
  103. std::vector<Eigen::Matrix4d, Eigen::aligned_allocator<Eigen::Matrix4d>> mahalanobis_;
  104. std::vector<int> correspondences_;
  105. std::vector<float> sq_distances_;
  106. };
  107. } // namespace nano_gicp
  108. #endif