Point Cloud Library (PCL)  1.11.1
correspondence_estimation.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_IMPL_CORRESPONDENCE_ESTIMATION_H_
42 #define PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_H_
43 
44 #include <pcl/common/io.h>
45 #include <pcl/common/copy_point.h>
46 
47 
48 namespace pcl
49 {
50 
51 namespace registration
52 {
53 
54 template <typename PointSource, typename PointTarget, typename Scalar> void
56  const PointCloudTargetConstPtr &cloud)
57 {
58  if (cloud->points.empty ())
59  {
60  PCL_ERROR ("[pcl::registration::%s::setInputTarget] Invalid or empty point cloud dataset given!\n", getClassName ().c_str ());
61  return;
62  }
63  target_ = cloud;
64 
65  // Set the internal point representation of choice
66  if (point_representation_)
67  tree_->setPointRepresentation (point_representation_);
68 
69  target_cloud_updated_ = true;
70 }
71 
72 
73 template <typename PointSource, typename PointTarget, typename Scalar> bool
75 {
76  if (!target_)
77  {
78  PCL_ERROR ("[pcl::registration::%s::compute] No input target dataset was given!\n", getClassName ().c_str ());
79  return (false);
80  }
81 
82  // Only update target kd-tree if a new target cloud was set
83  if (target_cloud_updated_ && !force_no_recompute_)
84  {
85  // If the target indices have been given via setIndicesTarget
86  if (target_indices_)
87  tree_->setInputCloud (target_, target_indices_);
88  else
89  tree_->setInputCloud (target_);
90 
91  target_cloud_updated_ = false;
92  }
93 
95 }
96 
97 
98 template <typename PointSource, typename PointTarget, typename Scalar> bool
100 {
101  // Only update source kd-tree if a new target cloud was set
102  if (source_cloud_updated_ && !force_no_recompute_reciprocal_)
103  {
104  if (point_representation_)
105  tree_reciprocal_->setPointRepresentation (point_representation_);
106  // If the target indices have been given via setIndicesTarget
107  if (indices_)
108  tree_reciprocal_->setInputCloud (getInputSource(), getIndicesSource());
109  else
110  tree_reciprocal_->setInputCloud (getInputSource());
111 
112  source_cloud_updated_ = false;
113  }
114 
115  return (true);
116 }
117 
118 
119 template <typename PointSource, typename PointTarget, typename Scalar> void
121  pcl::Correspondences &correspondences, double max_distance)
122 {
123  if (!initCompute ())
124  return;
125 
126  double max_dist_sqr = max_distance * max_distance;
127 
128  correspondences.resize (indices_->size ());
129 
130  std::vector<int> index (1);
131  std::vector<float> distance (1);
132  pcl::Correspondence corr;
133  unsigned int nr_valid_correspondences = 0;
134 
135  // Check if the template types are the same. If true, avoid a copy.
136  // Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT macro!
137  if (isSamePointType<PointSource, PointTarget> ())
138  {
139  // Iterate over the input set of source indices
140  for (std::vector<int>::const_iterator idx = indices_->begin (); idx != indices_->end (); ++idx)
141  {
142  tree_->nearestKSearch ((*input_)[*idx], 1, index, distance);
143  if (distance[0] > max_dist_sqr)
144  continue;
145 
146  corr.index_query = *idx;
147  corr.index_match = index[0];
148  corr.distance = distance[0];
149  correspondences[nr_valid_correspondences++] = corr;
150  }
151  }
152  else
153  {
154  PointTarget pt;
155 
156  // Iterate over the input set of source indices
157  for (std::vector<int>::const_iterator idx = indices_->begin (); idx != indices_->end (); ++idx)
158  {
159  // Copy the source data to a target PointTarget format so we can search in the tree
160  copyPoint ((*input_)[*idx], pt);
161 
162  tree_->nearestKSearch (pt, 1, index, distance);
163  if (distance[0] > max_dist_sqr)
164  continue;
165 
166  corr.index_query = *idx;
167  corr.index_match = index[0];
168  corr.distance = distance[0];
169  correspondences[nr_valid_correspondences++] = corr;
170  }
171  }
172  correspondences.resize (nr_valid_correspondences);
173  deinitCompute ();
174 }
175 
176 
177 template <typename PointSource, typename PointTarget, typename Scalar> void
179  pcl::Correspondences &correspondences, double max_distance)
180 {
181  if (!initCompute ())
182  return;
183 
184  // setup tree for reciprocal search
185  // Set the internal point representation of choice
186  if (!initComputeReciprocal())
187  return;
188  double max_dist_sqr = max_distance * max_distance;
189 
190  correspondences.resize (indices_->size());
191  std::vector<int> index (1);
192  std::vector<float> distance (1);
193  std::vector<int> index_reciprocal (1);
194  std::vector<float> distance_reciprocal (1);
195  pcl::Correspondence corr;
196  unsigned int nr_valid_correspondences = 0;
197  int target_idx = 0;
198 
199  // Check if the template types are the same. If true, avoid a copy.
200  // Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT macro!
201  if (isSamePointType<PointSource, PointTarget> ())
202  {
203  // Iterate over the input set of source indices
204  for (std::vector<int>::const_iterator idx = indices_->begin (); idx != indices_->end (); ++idx)
205  {
206  tree_->nearestKSearch ((*input_)[*idx], 1, index, distance);
207  if (distance[0] > max_dist_sqr)
208  continue;
209 
210  target_idx = index[0];
211 
212  tree_reciprocal_->nearestKSearch ((*target_)[target_idx], 1, index_reciprocal, distance_reciprocal);
213  if (distance_reciprocal[0] > max_dist_sqr || *idx != index_reciprocal[0])
214  continue;
215 
216  corr.index_query = *idx;
217  corr.index_match = index[0];
218  corr.distance = distance[0];
219  correspondences[nr_valid_correspondences++] = corr;
220  }
221  }
222  else
223  {
224  PointTarget pt_src;
225  PointSource pt_tgt;
226 
227  // Iterate over the input set of source indices
228  for (std::vector<int>::const_iterator idx = indices_->begin (); idx != indices_->end (); ++idx)
229  {
230  // Copy the source data to a target PointTarget format so we can search in the tree
231  copyPoint ((*input_)[*idx], pt_src);
232 
233  tree_->nearestKSearch (pt_src, 1, index, distance);
234  if (distance[0] > max_dist_sqr)
235  continue;
236 
237  target_idx = index[0];
238 
239  // Copy the target data to a target PointSource format so we can search in the tree_reciprocal
240  copyPoint ((*target_)[target_idx], pt_tgt);
241 
242  tree_reciprocal_->nearestKSearch (pt_tgt, 1, index_reciprocal, distance_reciprocal);
243  if (distance_reciprocal[0] > max_dist_sqr || *idx != index_reciprocal[0])
244  continue;
245 
246  corr.index_query = *idx;
247  corr.index_match = index[0];
248  corr.distance = distance[0];
249  correspondences[nr_valid_correspondences++] = corr;
250  }
251  }
252  correspondences.resize (nr_valid_correspondences);
253  deinitCompute ();
254 }
255 
256 } // namespace registration
257 } // namespace pcl
258 
259 //#define PCL_INSTANTIATE_CorrespondenceEstimation(T,U) template class PCL_EXPORTS pcl::registration::CorrespondenceEstimation<T,U>;
260 
261 #endif /* PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_H_ */
262 
pcl
Definition: convolution.h:46
pcl::geometry::distance
float distance(const PointT &p1, const PointT &p2)
Definition: geometry.h:60
pcl::Correspondence::distance
float distance
Definition: correspondence.h:70
pcl::registration::CorrespondenceEstimationBase::setInputTarget
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...
Definition: correspondence_estimation.hpp:55
pcl::registration::CorrespondenceEstimationBase::initCompute
bool initCompute()
Internal computation initialization.
Definition: correspondence_estimation.hpp:74
pcl::PCLBase
PCL base class.
Definition: pcl_base.h:73
pcl::registration::CorrespondenceEstimationBase::PointCloudTargetConstPtr
typename PointCloudTarget::ConstPtr PointCloudTargetConstPtr
Definition: correspondence_estimation.h:87
pcl::registration::CorrespondenceEstimationBase::initComputeReciprocal
bool initComputeReciprocal()
Internal computation initialization for reciprocal correspondences.
Definition: correspondence_estimation.hpp:99
pcl::copyPoint
void copyPoint(const PointInT &point_in, PointOutT &point_out)
Copy the fields of a source point into a target point.
Definition: copy_point.hpp:137
pcl::registration::CorrespondenceEstimation::determineCorrespondences
void determineCorrespondences(pcl::Correspondences &correspondences, double max_distance=std::numeric_limits< double >::max()) override
Determine the correspondences between input and target cloud.
Definition: correspondence_estimation.hpp:120
pcl::Correspondence::index_match
index_t index_match
Index of the matching (target) point.
Definition: correspondence.h:66
pcl::Correspondences
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
Definition: correspondence.h:90
pcl::Correspondence::index_query
index_t index_query
Index of the query (source) point.
Definition: correspondence.h:64
pcl::registration::CorrespondenceEstimation::determineReciprocalCorrespondences
void determineReciprocalCorrespondences(pcl::Correspondences &correspondences, double max_distance=std::numeric_limits< double >::max()) override
Determine the reciprocal correspondences between input and target cloud.
Definition: correspondence_estimation.hpp:178
pcl::Correspondence
Correspondence represents a match between two entities (e.g., points, descriptors,...
Definition: correspondence.h:62