Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ia_ransac.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-2012, 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 IA_RANSAC_H_
39 #define IA_RANSAC_H_
40 
43 
44 namespace pcl
45 {
51  template <typename PointSource, typename PointTarget, typename FeatureT>
52  class SampleConsensusInitialAlignment : public Registration<PointSource, PointTarget>
53  {
54  public:
67 
71 
73 
76 
80 
81 
83  {
84  public:
85  virtual ~ErrorFunctor () {}
86  virtual float operator () (float d) const = 0;
87  };
88 
89  class HuberPenalty : public ErrorFunctor
90  {
91  private:
92  HuberPenalty () {}
93  public:
94  HuberPenalty (float threshold) : threshold_ (threshold) {}
95  virtual float operator () (float e) const
96  {
97  if (e <= threshold_)
98  return (0.5 * e*e);
99  else
100  return (0.5 * threshold_ * (2.0 * fabs (e) - threshold_));
101  }
102  protected:
103  float threshold_;
104  };
105 
107  {
108  private:
109  TruncatedError () {}
110  public:
111  virtual ~TruncatedError () {}
112 
113  TruncatedError (float threshold) : threshold_ (threshold) {}
114  virtual float operator () (float e) const
115  {
116  if (e <= threshold_)
117  return (e / threshold_);
118  else
119  return (1.0);
120  }
121  protected:
122  float threshold_;
123  };
124 
128  input_features_ (), target_features_ (),
129  nr_samples_(3), min_sample_distance_ (0.0f), k_correspondences_ (10),
130  feature_tree_ (new pcl::KdTreeFLANN<FeatureT>),
131  error_functor_ ()
132  {
133  reg_name_ = "SampleConsensusInitialAlignment";
134  max_iterations_ = 1000;
136  };
137 
141  void
142  setSourceFeatures (const FeatureCloudConstPtr &features);
143 
145  inline FeatureCloudConstPtr const
146  getSourceFeatures () { return (input_features_); }
147 
151  void
152  setTargetFeatures (const FeatureCloudConstPtr &features);
153 
155  inline FeatureCloudConstPtr const
156  getTargetFeatures () { return (target_features_); }
157 
161  void
162  setMinSampleDistance (float min_sample_distance) { min_sample_distance_ = min_sample_distance; }
163 
165  float
166  getMinSampleDistance () { return (min_sample_distance_); }
167 
171  void
172  setNumberOfSamples (int nr_samples) { nr_samples_ = nr_samples; }
173 
175  int
176  getNumberOfSamples () { return (nr_samples_); }
177 
182  void
183  setCorrespondenceRandomness (int k) { k_correspondences_ = k; }
184 
186  int
187  getCorrespondenceRandomness () { return (k_correspondences_); }
188 
193  void
194  setErrorFunction (const boost::shared_ptr<ErrorFunctor> & error_functor) { error_functor_ = error_functor; }
195 
199  boost::shared_ptr<ErrorFunctor>
200  getErrorFunction () { return (error_functor_); }
201 
202  protected:
206  inline int
207  getRandomIndex (int n) { return (static_cast<int> (n * (rand () / (RAND_MAX + 1.0)))); };
208 
216  void
217  selectSamples (const PointCloudSource &cloud, int nr_samples, float min_sample_distance,
218  std::vector<int> &sample_indices);
219 
227  void
228  findSimilarFeatures (const FeatureCloud &input_features, const std::vector<int> &sample_indices,
229  std::vector<int> &corresponding_indices);
230 
235  float
236  computeErrorMetric (const PointCloudSource &cloud, float threshold);
237 
241  virtual void
242  computeTransformation (PointCloudSource &output, const Eigen::Matrix4f& guess);
243 
245  FeatureCloudConstPtr input_features_;
246 
248  FeatureCloudConstPtr target_features_;
249 
251  int nr_samples_;
252 
254  float min_sample_distance_;
255 
257  int k_correspondences_;
258 
260  FeatureKdTreePtr feature_tree_;
261 
263  boost::shared_ptr<ErrorFunctor> error_functor_;
264  public:
265  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
266  };
267 }
268 
270 
271 #endif //#ifndef IA_RANSAC_H_