Point Cloud Library (PCL)  1.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
sac.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2011, Willow Garage, Inc.
00006  *
00007  *  All rights reserved. 
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  * $Id: sac.h 4702 2012-02-23 09:39:33Z gedikli $
00037  *
00038  */
00039 
00040 #ifndef PCL_SAMPLE_CONSENSUS_H_
00041 #define PCL_SAMPLE_CONSENSUS_H_
00042 
00043 #include "pcl/sample_consensus/sac_model.h"
00044 #include <boost/random.hpp>
00045 #include <ctime>
00046 #include <set>
00047 
00048 namespace pcl
00049 {
00054   template <typename T>
00055   class SampleConsensus
00056   {
00057     typedef typename SampleConsensusModel<T>::Ptr SampleConsensusModelPtr;
00058 
00059     private:
00061       SampleConsensus () {};
00062 
00063     public:
00064       typedef boost::shared_ptr<SampleConsensus> Ptr;
00065       typedef boost::shared_ptr<const SampleConsensus> ConstPtr;
00066 
00071       SampleConsensus (const SampleConsensusModelPtr &model, bool random = false) : sac_model_(model), probability_ (0.99),
00072                                                                iterations_ (0), threshold_ (DBL_MAX), max_iterations_ (1000)
00073       {
00074          // Create a random number generator object
00075          rng_.reset (new boost::uniform_01<boost::mt19937> (rng_alg_));
00076          if (random)
00077            rng_->base ().seed (static_cast<unsigned> (std::time(0)));
00078          else
00079            rng_->base ().seed (12345u);
00080       };
00081 
00087       SampleConsensus (const SampleConsensusModelPtr &model, double threshold, bool random = false) : 
00088         sac_model_(model), probability_ (0.99), iterations_ (0), threshold_ (threshold), max_iterations_ (1000)
00089       {
00090          // Create a random number generator object
00091          rng_.reset (new boost::uniform_01<boost::mt19937> (rng_alg_));
00092          if (random)
00093            rng_->base ().seed (static_cast<unsigned> (std::time(0)));
00094          else
00095            rng_->base ().seed (12345u);
00096        };
00097 
00099       virtual ~SampleConsensus () {};
00100 
00104       inline void 
00105       setDistanceThreshold (double threshold)  { threshold_ = threshold; }
00106 
00108       inline double 
00109       getDistanceThreshold () { return (threshold_); }
00110 
00114       inline void 
00115       setMaxIterations (int max_iterations) { max_iterations_ = max_iterations; }
00116 
00118       inline int 
00119       getMaxIterations () { return (max_iterations_); }
00120 
00125       inline void 
00126       setProbability (double probability) { probability_ = probability; }
00127 
00129       inline double 
00130       getProbability () { return (probability_); }
00131 
00133       virtual bool 
00134       computeModel (int debug_verbosity_level = 0) = 0;
00135 
00141       inline void
00142       getRandomSamples (const boost::shared_ptr <std::vector<int> > &indices, 
00143                         size_t nr_samples, 
00144                         std::set<int> &indices_subset)
00145       {
00146         indices_subset.clear ();
00147         while (indices_subset.size () < nr_samples)
00148           //indices_subset.insert ((*indices)[(int) (indices->size () * (rand () / (RAND_MAX + 1.0)))]);
00149           indices_subset.insert ((*indices)[(int) (indices->size () * rnd ())]);
00150       }
00151 
00155       inline void 
00156       getModel (std::vector<int> &model) { model = model_; }
00157 
00161       inline void 
00162       getInliers (std::vector<int> &inliers) { inliers = inliers_; }
00163 
00167       inline void 
00168       getModelCoefficients (Eigen::VectorXf &model_coefficients) { model_coefficients = model_coefficients_; }
00169 
00170     protected:
00172       SampleConsensusModelPtr sac_model_;
00173 
00175       std::vector<int> model_;
00176 
00178       std::vector<int> inliers_;
00179 
00181       Eigen::VectorXf model_coefficients_;
00182 
00184       double probability_;
00185 
00187       int iterations_;
00188       
00190       double threshold_;
00191       
00193       int max_iterations_;
00194 
00196       boost::mt19937 rng_alg_;
00197 
00199       boost::shared_ptr<boost::uniform_01<boost::mt19937> > rng_;
00200 
00202       inline double
00203       rnd ()
00204       {
00205         return ((*rng_) ());
00206       }
00207    };
00208 }
00209 
00210 #endif  //#ifndef PCL_SAMPLE_CONSENSUS_H_