Point Cloud Library (PCL)  1.7.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 #ifndef PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_H_
41 #define PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_H_
42 
43 #include <pcl/common/concatenate.h>
44 #include <pcl/common/io.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  typedef typename pcl::traits::fieldList<PointTarget>::type FieldListTarget;
136  correspondences.resize (indices_->size ());
137 
138  std::vector<int> index (1);
139  std::vector<float> distance (1);
140  pcl::Correspondence corr;
141  unsigned int nr_valid_correspondences = 0;
142 
143  // Check if the template types are the same. If true, avoid a copy.
144  // Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT macro!
145  if (isSamePointType<PointSource, PointTarget> ())
146  {
147  // Iterate over the input set of source indices
148  for (std::vector<int>::const_iterator idx = indices_->begin (); idx != indices_->end (); ++idx)
149  {
150  tree_->nearestKSearch (input_->points[*idx], 1, index, distance);
151  if (distance[0] > max_dist_sqr)
152  continue;
153 
154  corr.index_query = *idx;
155  corr.index_match = index[0];
156  corr.distance = distance[0];
157  correspondences[nr_valid_correspondences++] = corr;
158  }
159  }
160  else
161  {
162  PointTarget pt;
163 
164  // Iterate over the input set of source indices
165  for (std::vector<int>::const_iterator idx = indices_->begin (); idx != indices_->end (); ++idx)
166  {
167  // Copy the source data to a target PointTarget format so we can search in the tree
168  pcl::for_each_type <FieldListTarget> (pcl::NdConcatenateFunctor <PointSource, PointTarget> (
169  input_->points[*idx],
170  pt));
171 
172  tree_->nearestKSearch (pt, 1, index, distance);
173  if (distance[0] > max_dist_sqr)
174  continue;
175 
176  corr.index_query = *idx;
177  corr.index_match = index[0];
178  corr.distance = distance[0];
179  correspondences[nr_valid_correspondences++] = corr;
180  }
181  }
182  correspondences.resize (nr_valid_correspondences);
183  deinitCompute ();
184 }
185 
186 ///////////////////////////////////////////////////////////////////////////////////////////
187 template <typename PointSource, typename PointTarget, typename Scalar> void
189  pcl::Correspondences &correspondences, double max_distance)
190 {
191  if (!initCompute ())
192  return;
193 
194  typedef typename pcl::traits::fieldList<PointSource>::type FieldListSource;
195  typedef typename pcl::traits::fieldList<PointTarget>::type FieldListTarget;
197 
198  // setup tree for reciprocal search
199  // Set the internal point representation of choice
200  if (!initComputeReciprocal())
201  return;
202  double max_dist_sqr = max_distance * max_distance;
203 
204  correspondences.resize (indices_->size());
205  std::vector<int> index (1);
206  std::vector<float> distance (1);
207  std::vector<int> index_reciprocal (1);
208  std::vector<float> distance_reciprocal (1);
209  pcl::Correspondence corr;
210  unsigned int nr_valid_correspondences = 0;
211  int target_idx = 0;
212 
213  // Check if the template types are the same. If true, avoid a copy.
214  // Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT macro!
215  if (isSamePointType<PointSource, PointTarget> ())
216  {
217  // Iterate over the input set of source indices
218  for (std::vector<int>::const_iterator idx = indices_->begin (); idx != indices_->end (); ++idx)
219  {
220  tree_->nearestKSearch (input_->points[*idx], 1, index, distance);
221  if (distance[0] > max_dist_sqr)
222  continue;
223 
224  target_idx = index[0];
225 
226  tree_reciprocal_->nearestKSearch (target_->points[target_idx], 1, index_reciprocal, distance_reciprocal);
227  if (distance_reciprocal[0] > max_dist_sqr || *idx != index_reciprocal[0])
228  continue;
229 
230  corr.index_query = *idx;
231  corr.index_match = index[0];
232  corr.distance = distance[0];
233  correspondences[nr_valid_correspondences++] = corr;
234  }
235  }
236  else
237  {
238  PointTarget pt_src;
239  PointSource pt_tgt;
240 
241  // Iterate over the input set of source indices
242  for (std::vector<int>::const_iterator idx = indices_->begin (); idx != indices_->end (); ++idx)
243  {
244  // Copy the source data to a target PointTarget format so we can search in the tree
245  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointSource, PointTarget> (
246  input_->points[*idx],
247  pt_src));
248 
249  tree_->nearestKSearch (pt_src, 1, index, distance);
250  if (distance[0] > max_dist_sqr)
251  continue;
252 
253  target_idx = index[0];
254 
255  // Copy the target data to a target PointSource format so we can search in the tree_reciprocal
256  pcl::for_each_type<FieldList> (pcl::NdConcatenateFunctor <PointTarget, PointSource> (
257  target_->points[target_idx],
258  pt_tgt));
259 
260  tree_reciprocal_->nearestKSearch (pt_tgt, 1, index_reciprocal, distance_reciprocal);
261  if (distance_reciprocal[0] > max_dist_sqr || *idx != index_reciprocal[0])
262  continue;
263 
264  corr.index_query = *idx;
265  corr.index_match = index[0];
266  corr.distance = distance[0];
267  correspondences[nr_valid_correspondences++] = corr;
268  }
269  }
270  correspondences.resize (nr_valid_correspondences);
271  deinitCompute ();
272 }
273 
274 //#define PCL_INSTANTIATE_CorrespondenceEstimation(T,U) template class PCL_EXPORTS pcl::registration::CorrespondenceEstimation<T,U>;
275 
276 #endif /* PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_H_ */