5 #include <pcl/impl/pcl_base.hpp>
6 #include <pcl/search/impl/search.hpp>
7 #include <pcl/search/impl/kdtree.hpp>
8 #include <pcl/search/impl/flann_search.hpp>
22 template <
typename Po
intSource,
typename Po
intTarget>
24 reg_name_ =
"RegistrationPCL";
27 k_correspondences_ = 20;
28 corr_dist_threshold_ = 1000.0;
29 rotation_epsilon_ = 2e-3;
30 transformation_epsilon_ = 5e-4;
32 voxel_resolution_ = 1.0;
34 registration_type_ =
"GICP";
37 template <
typename Po
intSource,
typename Po
intTarget>
40 template <
typename Po
intSource,
typename Po
intTarget>
42 if (input_ == cloud) {
46 pcl::Registration<PointSource, PointTarget, Scalar>::setInputSource(cloud);
47 source_tree_ = std::make_shared<KdTree<pcl::PointCloud<PointSource>>>(input_,
KdTreeBuilderOMP(num_threads_));
49 source_voxelmap_.reset();
52 template <
typename Po
intSource,
typename Po
intTarget>
54 if (target_ == cloud) {
58 pcl::Registration<PointSource, PointTarget, Scalar>::setInputTarget(cloud);
59 target_tree_ = std::make_shared<KdTree<pcl::PointCloud<PointTarget>>>(target_,
KdTreeBuilderOMP(num_threads_));
61 target_voxelmap_.reset();
64 template <
typename Po
intSource,
typename Po
intTarget>
66 if (input_ ==
nullptr) {
67 PCL_ERROR(
"[RegistrationPCL::setSourceCovariances] Target cloud is not set\n");
71 if (covs.size() != input_->size()) {
72 PCL_ERROR(
"[RegistrationPCL::setSourceCovariances] Invalid number of covariances: %lu\n", covs.size());
79 template <
typename Po
intSource,
typename Po
intTarget>
81 if (target_ ==
nullptr) {
82 PCL_ERROR(
"[RegistrationPCL::setTargetCovariances] Target cloud is not set\n");
86 if (covs.size() != target_->size()) {
87 PCL_ERROR(
"[RegistrationPCL::setTargetCovariances] Invalid number of covariances: %lu\n", covs.size());
94 template <
typename Po
intSource,
typename Po
intTarget>
96 if (source_covs_.empty()) {
97 PCL_WARN(
"[RegistrationPCL::getSourceCovariances] Covariances are not estimated\n");
103 template <
typename Po
intSource,
typename Po
intTarget>
105 if (target_covs_.empty()) {
106 PCL_WARN(
"[RegistrationPCL::getTargetCovariances] Covariances are not estimated\n");
112 template <
typename Po
intSource,
typename Po
intTarget>
114 input_.swap(target_);
115 source_tree_.swap(target_tree_);
116 source_covs_.swap(target_covs_);
117 source_voxelmap_.swap(target_voxelmap_);
120 template <
typename Po
intSource,
typename Po
intTarget>
123 source_tree_.reset();
124 source_covs_.clear();
125 source_voxelmap_.reset();
128 template <
typename Po
intSource,
typename Po
intTarget>
131 target_tree_.reset();
132 target_covs_.clear();
133 target_voxelmap_.reset();
136 template <
typename Po
intSource,
typename Po
intTarget>
139 PCL_ERROR(
"[RegistrationPCL::setNumThreads] Invalid number of threads: %d\n", n);
146 template <
typename Po
intSource,
typename Po
intTarget>
148 setNumNeighborsForCovariance(k);
151 template <
typename Po
intSource,
typename Po
intTarget>
154 PCL_ERROR(
"[RegistrationPCL::setNumNeighborsForCovariance] Invalid number of neighbors: %d\n", k);
157 k_correspondences_ = k;
160 template <
typename Po
intSource,
typename Po
intTarget>
162 if (voxel_resolution_ <= 0) {
163 PCL_ERROR(
"[RegistrationPCL::setVoxelResolution] Invalid voxel resolution: %f\n", r);
167 voxel_resolution_ = r;
170 template <
typename Po
intSource,
typename Po
intTarget>
172 rotation_epsilon_ = eps;
175 template <
typename Po
intSource,
typename Po
intTarget>
177 if (type ==
"GICP") {
178 registration_type_ = type;
179 }
else if (type ==
"VGICP") {
180 registration_type_ = type;
182 PCL_ERROR(
"[RegistrationPCL::setRegistrationType] Invalid registration type: %s\n", type.c_str());
186 template <
typename Po
intSource,
typename Po
intTarget>
191 template <
typename Po
intSource,
typename Po
intTarget>
196 template <
typename Po
intSource,
typename Po
intTarget>
201 template <
typename Po
intSource,
typename Po
intTarget>
203 if (output.points.data() == input_->points.data() || output.points.data() == target_->points.data()) {
204 throw std::invalid_argument(
"FastGICP: destination cloud cannot be identical to source or target");
210 if (source_covs_.size() != input_->size()) {
213 if (target_covs_.size() != target_->size()) {
220 registration.
reduction.num_threads = num_threads_;
221 registration.
rejector.max_dist_sq = corr_dist_threshold_ * corr_dist_threshold_;
222 registration.
optimizer.verbose = verbose_;
223 registration.
optimizer.max_iterations = max_iterations_;
225 if (registration_type_ ==
"GICP") {
226 result_ = registration.
align(target_proxy, source_proxy, *target_tree_, Eigen::Isometry3d(guess.template cast<double>()));
227 }
else if (registration_type_ ==
"VGICP") {
228 if (!target_voxelmap_) {
229 target_voxelmap_ = std::make_shared<GaussianVoxelMap>(voxel_resolution_);
230 target_voxelmap_->insert(target_proxy);
232 if (!source_voxelmap_) {
233 source_voxelmap_ = std::make_shared<GaussianVoxelMap>(voxel_resolution_);
234 source_voxelmap_->insert(source_proxy);
237 result_ = registration.
align(*target_voxelmap_, source_proxy, *target_voxelmap_, Eigen::Isometry3d(guess.template cast<double>()));
239 PCL_ERROR(
"[RegistrationPCL::computeTransformation] Invalid registration type: %s\n", registration_type_.c_str());
243 converged_ = result_.converged;
244 final_transformation_ = result_.T_target_source.matrix().template cast<float>();
245 pcl::transformPointCloud(*input_, output, final_transformation_);
void setVoxelResolution(double r)
Set the voxel resolution for VGICP.
Definition: pcl_registration_impl.hpp:161
RegistrationPCL()
Definition: pcl_registration_impl.hpp:23
void setVerbosity(bool verbose)
Set the verbosity flag.
Definition: pcl_registration_impl.hpp:187
typename PointCloudSource::ConstPtr PointCloudSourceConstPtr
Definition: pcl_registration.hpp:22
typename pcl::Registration< PointSource, PointTarget, Scalar >::Matrix4 Matrix4
Definition: pcl_registration.hpp:18
const std::vector< Eigen::Matrix4d > & getTargetCovariances() const
Get target point covariances.
Definition: pcl_registration_impl.hpp:104
virtual ~RegistrationPCL()
Definition: pcl_registration_impl.hpp:38
const std::vector< Eigen::Matrix4d > & getSourceCovariances() const
Get source point covariances.
Definition: pcl_registration_impl.hpp:95
void setCorrespondenceRandomness(int k)
Set the number of neighbors for covariance estimation.
Definition: pcl_registration_impl.hpp:147
void setNumThreads(int n)
Set the number of threads to use.
Definition: pcl_registration_impl.hpp:137
void setSourceCovariances(const std::vector< Eigen::Matrix4d > &covs)
Set source point covariances.
Definition: pcl_registration_impl.hpp:65
typename pcl::Registration< PointSource, PointTarget, Scalar >::PointCloudSource PointCloudSource
Definition: pcl_registration.hpp:20
const Eigen::Matrix< double, 6, 6 > & getFinalHessian() const
Get the final Hessian matrix ([rx, ry, rz, tx, ty, tz]).
Definition: pcl_registration_impl.hpp:192
virtual void computeTransformation(PointCloudSource &output, const Matrix4 &guess) override
Definition: pcl_registration_impl.hpp:202
const RegistrationResult & getRegistrationResult() const
Get the detailed registration result.
Definition: pcl_registration_impl.hpp:197
void clearSource()
Clear source point cloud.
Definition: pcl_registration_impl.hpp:121
void clearTarget()
Clear target point cloud.
Definition: pcl_registration_impl.hpp:129
void setTargetCovariances(const std::vector< Eigen::Matrix4d > &covs)
Set target point covariances.
Definition: pcl_registration_impl.hpp:80
typename PointCloudTarget::ConstPtr PointCloudTargetConstPtr
Definition: pcl_registration.hpp:26
void swapSourceAndTarget()
Swap source and target point clouds and their augmented data (KdTrees, covariances,...
Definition: pcl_registration_impl.hpp:113
void setRegistrationType(const std::string &type)
Set registration type ("GICP" or "VGICP").
Definition: pcl_registration_impl.hpp:176
void setRotationEpsilon(double eps)
Set rotation epsilon for convergence check.
Definition: pcl_registration_impl.hpp:171
void setInputSource(const PointCloudSourceConstPtr &cloud) override
Set the input source (aligned) point cloud.
Definition: pcl_registration_impl.hpp:41
void setNumNeighborsForCovariance(int k)
Set the number of neighbors for covariance estimation.
Definition: pcl_registration_impl.hpp:152
void setInputTarget(const PointCloudTargetConstPtr &cloud) override
Set the input target (fixed) point cloud.
Definition: pcl_registration_impl.hpp:53
Definition: flat_container.hpp:12
void estimate_covariances_omp(PointCloud &cloud, int num_neighbors=20, int num_threads=4)
Estimate point covariances with OpenMP.
Definition: normal_estimation_omp.hpp:58
Kd-tree builder with OpenMP.
Definition: kdtree_omp.hpp:16
Proxy class to access PCL point cloud with external covariance matrices.
Definition: pcl_proxy.hpp:12
Registration result.
Definition: registration_result.hpp:11
Point cloud registration.
Definition: registration.hpp:23
RegistrationResult align(const TargetPointCloud &target, const SourcePointCloud &source, const TargetTree &target_tree, const Eigen::Isometry3d &init_T=Eigen::Isometry3d::Identity()) const
Align point clouds.
Definition: registration.hpp:33
TerminationCriteria criteria
Termination criteria.
Definition: registration.hpp:48
CorrespondenceRejector rejector
Correspondence rejector.
Definition: registration.hpp:49
Reduction reduction
Reduction.
Definition: registration.hpp:52
Optimizer optimizer
Optimizer.
Definition: registration.hpp:53
double rotation_eps
Translation tolerance.
Definition: termination_criteria.hpp:20
double translation_eps
Rotation tolerance [rad].
Definition: termination_criteria.hpp:19