Point Cloud Library (PCL) 1.15.1-dev
Loading...
Searching...
No Matches
registration.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#pragma once
42
43#include <pcl/search/auto.h>
44
45namespace pcl {
46
47template <typename PointSource, typename PointTarget, typename Scalar>
48inline void
50 const PointCloudSourceConstPtr& cloud)
51{
52 if (cloud->points.empty()) {
53 PCL_ERROR("[pcl::%s::setInputSource] Invalid or empty point cloud dataset given!\n",
54 getClassName().c_str());
55 return;
56 }
57 source_cloud_updated_ = true;
59}
60
61template <typename PointSource, typename PointTarget, typename Scalar>
62inline void
64 const PointCloudTargetConstPtr& cloud)
65{
66 if (cloud->points.empty()) {
67 PCL_ERROR("[pcl::%s::setInputTarget] Invalid or empty point cloud dataset given!\n",
68 getClassName().c_str());
69 return;
70 }
71 target_ = cloud;
72 target_cloud_updated_ = true;
73}
74
75template <typename PointSource, typename PointTarget, typename Scalar>
76bool
78{
79 if (!target_) {
80 PCL_ERROR("[pcl::registration::%s::compute] No input target dataset was given!\n",
81 getClassName().c_str());
82 return (false);
83 }
84
85 // Only update target kd-tree if a new target cloud was set
86 if (target_cloud_updated_ && !force_no_recompute_) {
87 if (point_representation_) {
88 if (!tree_ || !tree_->setPointRepresentation(point_representation_) ||
89 !tree_->setInputCloud(target_))
90 tree_.reset(pcl::search::autoSelectMethod<PointTarget>(
91 target_,
93 point_representation_,
94 false,
96 }
97 else if (!tree_ || !tree_->setInputCloud(target_))
98 tree_.reset(pcl::search::autoSelectMethod<PointTarget>(
100 target_cloud_updated_ = false;
101 }
102
103 // Update the correspondence estimation
104 if (correspondence_estimation_) {
105 correspondence_estimation_->setSearchMethodTarget(tree_, force_no_recompute_);
106 correspondence_estimation_->setSearchMethodSource(tree_reciprocal_,
107 force_no_recompute_reciprocal_);
108 }
109
110 // Note: we /cannot/ update the search method on all correspondence rejectors, because
111 // we know nothing about them. If they should be cached, they must be cached
112 // individually.
113
115}
116
117template <typename PointSource, typename PointTarget, typename Scalar>
118bool
120{
121 if (!input_) {
122 PCL_ERROR("[pcl::registration::%s::compute] No input source dataset was given!\n",
123 getClassName().c_str());
124 return (false);
125 }
126
127 if (source_cloud_updated_ && !force_no_recompute_reciprocal_) {
128 if (!tree_reciprocal_ || !tree_reciprocal_->setInputCloud(input_))
129 tree_reciprocal_.reset(pcl::search::autoSelectMethod<PointSource>(input_, false));
130 source_cloud_updated_ = false;
131 }
132 return (true);
133}
134
135template <typename PointSource, typename PointTarget, typename Scalar>
136inline double
138 const std::vector<float>& distances_a, const std::vector<float>& distances_b)
139{
140 unsigned int nr_elem =
141 static_cast<unsigned int>(std::min(distances_a.size(), distances_b.size()));
142 Eigen::VectorXf map_a = Eigen::VectorXf::Map(distances_a.data(), nr_elem);
143 Eigen::VectorXf map_b = Eigen::VectorXf::Map(distances_b.data(), nr_elem);
144 return (static_cast<double>((map_a - map_b).sum()) / static_cast<double>(nr_elem));
145}
146
147template <typename PointSource, typename PointTarget, typename Scalar>
148inline double
150 bool use_indices)
151{
152 double fitness_score = 0.0;
153
154 // Transform the input dataset using the final transformation
155 PointCloudSource input_transformed;
156 if (use_indices && indices_ && indices_->size() != input_->size())
157 transformPointCloud(*input_, *indices_, input_transformed, final_transformation_);
158 else
159 transformPointCloud(*input_, input_transformed, final_transformation_);
160
161 pcl::Indices nn_indices(1);
162 std::vector<float> nn_dists(1);
163
164 // For each point in the source dataset
165 int nr = 0;
166 for (const auto& point : input_transformed) {
167 if (!input_->is_dense && !pcl::isXYZFinite(point))
168 continue;
169 // Find its nearest neighbor in the target
170 tree_->nearestKSearchT(point, 1, nn_indices, nn_dists);
171
172 // Deal with occlusions (incomplete targets)
173 if (nn_dists[0] <= max_range) {
174 // Add to the fitness score
175 fitness_score += nn_dists[0];
176 nr++;
177 }
178 }
179
180 if (nr > 0)
181 return (fitness_score / nr);
182 return (std::numeric_limits<double>::max());
183}
185template <typename PointSource, typename PointTarget, typename Scalar>
186inline void
188{
189 align(output, Matrix4::Identity());
190}
191
192template <typename PointSource, typename PointTarget, typename Scalar>
193inline void
195 const Matrix4& guess)
196{
197 if (!initCompute())
198 return;
199
200 // Resize the output dataset
201 output.resize(indices_->size());
202 // Copy the header
203 output.header = input_->header;
204 // Check if the output will be computed for all points or only a subset
205 if (indices_->size() != input_->size()) {
206 output.width = indices_->size();
207 output.height = 1;
208 }
209 else {
210 output.width = static_cast<std::uint32_t>(input_->width);
211 output.height = input_->height;
212 }
213 output.is_dense = input_->is_dense;
214
215 // Copy the point data to output
216 for (std::size_t i = 0; i < indices_->size(); ++i)
217 output[i] = (*input_)[(*indices_)[i]];
218
219 // Perform the actual transformation computation
220 converged_ = false;
221 final_transformation_ = transformation_ = previous_transformation_ =
222 Matrix4::Identity();
223
224 // Right before we estimate the transformation, we set all the point.data[3] values to
225 // 1 to aid the rigid transformation
226 for (std::size_t i = 0; i < indices_->size(); ++i)
227 output[i].data[3] = 1.0;
228
229 computeTransformation(output, guess);
230
231 deinitCompute();
232}
233
234} // namespace pcl
PCL base class.
Definition pcl_base.h:70
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition pcl_base.hpp:65
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
void resize(std::size_t count)
Resizes the container to contain count elements.
std::uint32_t width
The point cloud width (if organized as an image-structure).
pcl::PCLHeader header
The point cloud header.
std::uint32_t height
The point cloud height (if organized as an image-structure).
bool initCompute()
Internal computation initialization.
virtual void setInputSource(const PointCloudSourceConstPtr &cloud)
Provide a pointer to the input source (e.g., the point cloud that we want to align to the target)
typename PointCloudSource::ConstPtr PointCloudSourceConstPtr
virtual void setInputTarget(const PointCloudTargetConstPtr &cloud)
Provide a pointer to the input target (e.g., the point cloud that we want to align the input source t...
typename PointCloudTarget::ConstPtr PointCloudTargetConstPtr
Eigen::Matrix< Scalar, 4, 4 > Matrix4
void align(PointCloudSource &output)
Call the registration algorithm which estimates the transformation and returns the transformed source...
double getFitnessScore(double max_range=std::numeric_limits< double >::max(), bool use_indices=false)
Obtain the Euclidean fitness score (e.g., mean of squared distances from the source to the target)
bool initComputeReciprocal()
Internal computation when reciprocal lookup is needed.
void transformPointCloud(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const Eigen::Matrix< Scalar, 4, 4 > &transform, bool copy_all_fields)
Apply a rigid transform defined by a 4x4 matrix.
@ one_knn_search
The search method will mainly be used for nearestKSearch where k is 1.
shared_ptr< const Indices > IndicesConstPtr
Definition pcl_base.h:59
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133
constexpr bool isXYZFinite(const PointT &) noexcept