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