Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
correspondence_rejection_features.h
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_CORRESPONDENCE_REJECTION_FEATURES_H_
39 #define PCL_REGISTRATION_CORRESPONDENCE_REJECTION_FEATURES_H_
40 
41 #include <boost/function.hpp>
42 #include <boost/unordered_map.hpp>
44 #include <pcl/point_cloud.h>
46 
47 namespace pcl
48 {
49  namespace registration
50  {
61  {
62  using CorrespondenceRejector::input_correspondences_;
63  using CorrespondenceRejector::rejection_name_;
64  using CorrespondenceRejector::getClassName;
65 
66  public:
68  CorrespondenceRejectorFeatures () : max_distance_ (std::numeric_limits<float>::max ())
69  {
70  rejection_name_ = "CorrespondenceRejectorFeatures";
71  }
72 
77  void
78  getRemainingCorrespondences (const pcl::Correspondences& original_correspondences,
79  pcl::Correspondences& remaining_correspondences);
80 
85  template <typename FeatureT> inline void
86  setSourceFeature (const typename pcl::PointCloud<FeatureT>::ConstPtr &source_feature,
87  const std::string &key);
88 
92  template <typename FeatureT> inline typename pcl::PointCloud<FeatureT>::ConstPtr
93  getSourceFeature (const std::string &key);
94 
99  template <typename FeatureT> inline void
100  setTargetFeature (const typename pcl::PointCloud<FeatureT>::ConstPtr &target_feature,
101  const std::string &key);
102 
106  template <typename FeatureT> inline typename pcl::PointCloud<FeatureT>::ConstPtr
107  getTargetFeature (const std::string &key);
108 
115  template <typename FeatureT> inline void
116  setDistanceThreshold (double thresh, const std::string &key);
117 
121  inline bool
122  hasValidFeatures ();
123 
128  template <typename FeatureT> inline void
130  const std::string &key);
131 
132  protected:
133 
137  inline void
138  applyRejection (pcl::Correspondences &correspondences)
139  {
140  getRemainingCorrespondences (*input_correspondences_, correspondences);
141  }
142 
146  float max_distance_;
147 
148  class FeatureContainerInterface
149  {
150  public:
151  virtual bool isValid () = 0;
152  virtual double getCorrespondenceScore (int index) = 0;
153  virtual bool isCorrespondenceValid (int index) = 0;
154  };
155 
156  typedef boost::unordered_map<std::string, boost::shared_ptr<FeatureContainerInterface> > FeaturesMap;
157 
159  FeaturesMap features_map_;
160 
167  template <typename FeatureT>
168  class FeatureContainer : public pcl::registration::CorrespondenceRejectorFeatures::FeatureContainerInterface
169  {
170  public:
171  typedef typename pcl::PointCloud<FeatureT>::ConstPtr FeatureCloudConstPtr;
172  typedef boost::function<int (const pcl::PointCloud<FeatureT> &, int, std::vector<int> &,
173  std::vector<float> &)> SearchMethod;
174 
175  typedef typename pcl::PointRepresentation<FeatureT>::ConstPtr PointRepresentationConstPtr;
176 
177  FeatureContainer () : thresh_(std::numeric_limits<double>::max ()), feature_representation_()
178  {
179  }
180 
181  inline void
182  setSourceFeature (const FeatureCloudConstPtr &source_features)
183  {
184  source_features_ = source_features;
185  }
186 
187  inline FeatureCloudConstPtr
189  {
190  return (source_features_);
191  }
192 
193  inline void
194  setTargetFeature (const FeatureCloudConstPtr &target_features)
195  {
196  target_features_ = target_features;
197  }
198 
199  inline FeatureCloudConstPtr
201  {
202  return (target_features_);
203  }
204 
205  inline void
206  setDistanceThreshold (double thresh)
207  {
208  thresh_ = thresh;
209  }
210 
211  virtual inline bool
212  isValid ()
213  {
214  if (!source_features_ || !target_features_)
215  return (false);
216  else
217  return (source_features_->points.size () > 0 &&
218  target_features_->points.size () > 0);
219  }
220 
224  inline void
225  setFeatureRepresentation (const PointRepresentationConstPtr &fr)
226  {
227  feature_representation_ = fr;
228  }
229 
234  virtual inline double
235  getCorrespondenceScore (int index)
236  {
237  // If no feature representation was given, reset to the default implementation for FeatureT
238  if (!feature_representation_)
239  feature_representation_.reset (new DefaultFeatureRepresentation<FeatureT>);
240 
241  // Get the source and the target feature from the list
242  const FeatureT &feat_src = source_features_->points[index];
243  const FeatureT &feat_tgt = target_features_->points[index];
244 
245  // Check if the representations are valid
246  if (!feature_representation_->isValid (feat_src) || !feature_representation_->isValid (feat_tgt))
247  {
248  PCL_ERROR ("[pcl::registration::CorrespondenceRejectorFeatures::getCorrespondenceScore] Invalid feature representation given!\n");
249  return (std::numeric_limits<double>::max ());
250  }
251 
252  // Set the internal feature point representation of choice
253  Eigen::VectorXf feat_src_ptr = Eigen::VectorXf::Zero (feature_representation_->getNumberOfDimensions ());
254  feature_representation_->vectorize (FeatureT (feat_src), feat_src_ptr);
255  Eigen::VectorXf feat_tgt_ptr = Eigen::VectorXf::Zero (feature_representation_->getNumberOfDimensions ());
256  feature_representation_->vectorize (FeatureT (feat_tgt), feat_tgt_ptr);
257 
258  // Compute the L2 norm
259  return ((feat_src_ptr - feat_tgt_ptr).squaredNorm ());
260  }
261 
267  virtual inline bool
268  isCorrespondenceValid (int index)
269  {
270  if (getCorrespondenceScore (index) < thresh_ * thresh_)
271  return (true);
272  else
273  return (false);
274  }
275 
276  private:
277  FeatureCloudConstPtr source_features_, target_features_;
278  SearchMethod search_method_;
279 
281  double thresh_;
282 
284  PointRepresentationConstPtr feature_representation_;
285  };
286  };
287  }
288 }
289 
291 
292 #endif /* PCL_REGISTRATION_CORRESPONDENCE_REJECTION_FEATURES_H_ */