Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
correspondence_estimation.hpp
Go to the documentation of this file.
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  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  *
37  */
38 #ifndef PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_H_
39 #define PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_H_
40 
41 #include <pcl/common/concatenate.h>
42 //#include <pcl/registration/correspondence_estimation.h>
43 
45 template <typename PointSource, typename PointTarget> inline void
47  const PointCloudTargetConstPtr &cloud)
48 {
49  if (cloud->points.empty ())
50  {
51  PCL_ERROR ("[pcl::%s::setInputTarget] Invalid or empty point cloud dataset given!\n", getClassName ().c_str ());
52  return;
53  }
54  target_ = cloud;
55  tree_->setInputCloud (target_);
56 }
57 
59 template <typename PointSource, typename PointTarget> void
61  pcl::Correspondences &correspondences, float max_distance)
62 {
63  typedef typename pcl::traits::fieldList<PointTarget>::type FieldListTarget;
64 
65  if (!initCompute ())
66  return;
67 
68  if (!target_)
69  {
70  PCL_WARN ("[pcl::%s::compute] No input target dataset was given!\n", getClassName ().c_str ());
71  return;
72  }
73 
74  float max_dist_sqr = max_distance * max_distance;
75 
76  correspondences.resize (indices_->size ());
77  std::vector<int> index (1);
78  std::vector<float> distance (1);
80  for (size_t i = 0; i < indices_->size (); ++i)
81  {
82  // Copy the source data to a target PointTarget format so we can search in the tree
83  PointTarget pt;
84  pcl::for_each_type <FieldListTarget> (pcl::NdConcatenateFunctor <PointSource, PointTarget> (
85  input_->points[(*indices_)[i]],
86  pt));
87 
88  //if (tree_->nearestKSearch (input_->points[(*indices_)[i]], 1, index, distance))
89  if (tree_->nearestKSearch (pt, 1, index, distance))
90  {
91  if (distance[0] <= max_dist_sqr)
92  {
93  corr.index_query = static_cast<int> (i);
94  corr.index_match = index[0];
95  corr.distance = distance[0];
96  correspondences[i] = corr;
97  continue;
98  }
99  }
100 // correspondences[i] = pcl::Correspondence(i, -1, std::numeric_limits<float>::max());
101  }
102  deinitCompute ();
103 }
104 
106 template <typename PointSource, typename PointTarget> void
108  pcl::Correspondences &correspondences)
109 {
110  typedef typename pcl::traits::fieldList<PointSource>::type FieldListSource;
111  typedef typename pcl::traits::fieldList<PointTarget>::type FieldListTarget;
113 
114  if (!initCompute ())
115  return;
116 
117  if (!target_)
118  {
119  PCL_WARN ("[pcl::%s::compute] No input target dataset was given!\n", getClassName ().c_str ());
120  return;
121  }
122 
123  // setup tree for reciprocal search
124  pcl::KdTreeFLANN<PointSource> tree_reciprocal;
125  tree_reciprocal.setInputCloud (input_, indices_);
126 
127  correspondences.resize (indices_->size());
128  std::vector<int> index (1);
129  std::vector<float> distance (1);
130  std::vector<int> index_reciprocal (1);
131  std::vector<float> distance_reciprocal (1);
132  pcl::Correspondence corr;
133  unsigned int nr_valid_correspondences = 0;
134 
135  for (size_t i = 0; i < indices_->size (); ++i)
136  {
137  // Copy the source data to a target PointTarget format so we can search in the tree
138  PointTarget pt_src;
139  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointSource, PointTarget> (
140  input_->points[(*indices_)[i]],
141  pt_src));
142 
143  //tree_->nearestKSearch (input_->points[(*indices_)[i]], 1, index, distance);
144  tree_->nearestKSearch (pt_src, 1, index, distance);
145 
146  // Copy the target data to a target PointSource format so we can search in the tree_reciprocal
147  PointSource pt_tgt;
148  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTarget, PointSource> (
149  target_->points[index[0]],
150  pt_tgt));
151  //tree_reciprocal.nearestKSearch (target_->points[index[0]], 1, index_reciprocal, distance_reciprocal);
152  tree_reciprocal.nearestKSearch (pt_tgt, 1, index_reciprocal, distance_reciprocal);
153 
154  if ((*indices_)[i] == index_reciprocal[0])
155  {
156  corr.index_query = (*indices_)[i];
157  corr.index_match = index[0];
158  corr.distance = distance[0];
159  correspondences[nr_valid_correspondences] = corr;
160  ++nr_valid_correspondences;
161  }
162  }
163  correspondences.resize (nr_valid_correspondences);
164 
165  deinitCompute ();
166 }
167 
168 //#define PCL_INSTANTIATE_CorrespondenceEstimation(T,U) template class PCL_EXPORTS pcl::registration::CorrespondenceEstimation<T,U>;
169 
170 #endif /* PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_H_ */