Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sac.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  * $Id: sac.h 6144 2012-07-04 22:06:28Z rusu $
37  *
38  */
39 
40 #ifndef PCL_SAMPLE_CONSENSUS_H_
41 #define PCL_SAMPLE_CONSENSUS_H_
42 
44 #include <boost/random.hpp>
45 #include <ctime>
46 #include <set>
47 
48 namespace pcl
49 {
54  template <typename T>
56  {
57  typedef typename SampleConsensusModel<T>::Ptr SampleConsensusModelPtr;
58 
59  private:
62 
63  public:
64  typedef boost::shared_ptr<SampleConsensus> Ptr;
65  typedef boost::shared_ptr<const SampleConsensus> ConstPtr;
66 
71  SampleConsensus (const SampleConsensusModelPtr &model, bool random = false) :
72  sac_model_ (model),
73  model_ (),
74  inliers_ (),
75  model_coefficients_ (),
76  probability_ (0.99),
77  iterations_ (0),
78  threshold_ (std::numeric_limits<double>::max()),
79  max_iterations_ (1000),
80  rng_alg_ (),
81  rng_ (new boost::uniform_01<boost::mt19937> (rng_alg_))
82  {
83  // Create a random number generator object
84  if (random)
85  rng_->base ().seed (static_cast<unsigned> (std::time(0)));
86  else
87  rng_->base ().seed (12345u);
88  };
89 
95  SampleConsensus (const SampleConsensusModelPtr &model, double threshold, bool random = false) :
96  sac_model_ (model),
97  model_ (),
98  inliers_ (),
99  model_coefficients_ (),
100  probability_ (0.99),
101  iterations_ (0),
102  threshold_ (threshold),
103  max_iterations_ (1000),
104  rng_alg_ (),
105  rng_ (new boost::uniform_01<boost::mt19937> (rng_alg_))
106  {
107  // Create a random number generator object
108  if (random)
109  rng_->base ().seed (static_cast<unsigned> (std::time(0)));
110  else
111  rng_->base ().seed (12345u);
112  };
113 
115  virtual ~SampleConsensus () {};
116 
120  inline void
121  setDistanceThreshold (double threshold) { threshold_ = threshold; }
122 
124  inline double
125  getDistanceThreshold () { return (threshold_); }
126 
130  inline void
131  setMaxIterations (int max_iterations) { max_iterations_ = max_iterations; }
132 
134  inline int
135  getMaxIterations () { return (max_iterations_); }
136 
141  inline void
142  setProbability (double probability) { probability_ = probability; }
143 
145  inline double
146  getProbability () { return (probability_); }
147 
149  virtual bool
150  computeModel (int debug_verbosity_level = 0) = 0;
151 
157  inline void
158  getRandomSamples (const boost::shared_ptr <std::vector<int> > &indices,
159  size_t nr_samples,
160  std::set<int> &indices_subset)
161  {
162  indices_subset.clear ();
163  while (indices_subset.size () < nr_samples)
164  //indices_subset.insert ((*indices)[(int) (indices->size () * (rand () / (RAND_MAX + 1.0)))]);
165  indices_subset.insert ((*indices)[static_cast<int> (static_cast<double>(indices->size ()) * rnd ())]);
166  }
167 
171  inline void
172  getModel (std::vector<int> &model) { model = model_; }
173 
177  inline void
178  getInliers (std::vector<int> &inliers) { inliers = inliers_; }
179 
183  inline void
184  getModelCoefficients (Eigen::VectorXf &model_coefficients) { model_coefficients = model_coefficients_; }
185 
186  protected:
188  SampleConsensusModelPtr sac_model_;
189 
191  std::vector<int> model_;
192 
194  std::vector<int> inliers_;
195 
197  Eigen::VectorXf model_coefficients_;
198 
200  double probability_;
201 
203  int iterations_;
204 
206  double threshold_;
207 
209  int max_iterations_;
210 
212  boost::mt19937 rng_alg_;
213 
215  boost::shared_ptr<boost::uniform_01<boost::mt19937> > rng_;
216 
218  inline double
219  rnd ()
220  {
221  return ((*rng_) ());
222  }
223  };
224 }
225 
226 #endif //#ifndef PCL_SAMPLE_CONSENSUS_H_