Point Cloud Library (PCL)  1.9.1
sac.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2009, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Id$
35  *
36  */
37 
38 #ifndef PCL_CUDA_SAMPLE_CONSENSUS_H_
39 #define PCL_CUDA_SAMPLE_CONSENSUS_H_
40 
41 #include <pcl/cuda/sample_consensus/sac_model.h>
42 #include <pcl/cuda/point_cloud.h>
43 #include <cfloat>
44 //#include <set>
45 
46 namespace pcl
47 {
48  namespace cuda
49  {
50  template <template <typename> class Storage>
52  {
53  typedef typename SampleConsensusModel<Storage>::Ptr SampleConsensusModelPtr;
54  typedef typename SampleConsensusModel<Storage>::Hypotheses Hypotheses;
55 
56  typedef typename SampleConsensusModel<Storage>::Indices Indices;
57  typedef typename SampleConsensusModel<Storage>::IndicesPtr IndicesPtr;
58  typedef typename SampleConsensusModel<Storage>::IndicesConstPtr IndicesConstPtr;
59 
60  private:
61  /** \brief Constructor for base SAC. */
63 
64  public:
65  typedef typename Storage<float>::type Coefficients;
66  typedef boost::shared_ptr <Coefficients> CoefficientsPtr;
67  typedef boost::shared_ptr <const Coefficients> CoefficientsConstPtr;
68 
69  typedef boost::shared_ptr<SampleConsensus> Ptr;
70  typedef boost::shared_ptr<const SampleConsensus> ConstPtr;
71 
72  /** \brief Constructor for base SAC.
73  * \param model a Sample Consensus model
74  */
75  SampleConsensus (const SampleConsensusModelPtr &model) :
76  sac_model_(model), probability_ (0.99), iterations_ (0), threshold_ (DBL_MAX),
77  max_iterations_ (1000)
78  {};
79 
80  /** \brief Constructor for base SAC.
81  * \param model a Sample Consensus model
82  * \param threshold distance to model threshold
83  */
84  SampleConsensus (const SampleConsensusModelPtr &model, float threshold) :
85  sac_model_(model), probability_ (0.99), iterations_ (0), threshold_ (threshold),
86  max_iterations_ (1000)
87  {};
88 
89  /** \brief Destructor for base SAC. */
90  virtual ~SampleConsensus () {};
91 
92  /** \brief Set the distance to model threshold.
93  * \param threshold distance to model threshold
94  */
95  inline void
96  setDistanceThreshold (float threshold) { threshold_ = threshold; }
97 
98  /** \brief Get the distance to model threshold, as set by the user. */
99  inline float
101 
102  /** \brief Set the maximum number of iterations.
103  * \param max_iterations maximum number of iterations
104  */
105  inline void
106  setMaxIterations (int max_iterations) { max_iterations_ = max_iterations; }
107 
108  /** \brief Get the maximum number of iterations, as set by the user. */
109  inline int
111 
112  /** \brief Set the desired probability of choosing at least one sample free from
113  * outliers.
114  * \param probability the desired probability of choosing at least one sample free
115  * from outliers
116  * \note internally, the probability is set to 99% (0.99) by default.
117  */
118  inline void
119  setProbability (float probability) { probability_ = probability; }
120 
121  /** \brief Obtain the probability of choosing at least one sample free from outliers,
122  * as set by the user.
123  */
124  inline float
125  getProbability () { return (probability_); }
126 
127  /** \brief Compute the actual model. Pure virtual. */
128  virtual bool
129  computeModel (int debug_verbosity_level = 0) = 0;
130 
131  /* \brief Get a set of randomly selected indices.
132  * \param indices the input indices vector
133  * \param nr_samples the desired number of point indices to randomly select
134  * \param indices_subset the resultant output set of randomly selected indices
135  */
136 /* inline void
137  getRandomSamples (const IndicesPtr &indices, size_t nr_samples,
138  std::set<int> &indices_subset)
139  {
140  indices_subset.clear ();
141  while (indices_subset.size () < nr_samples)
142  indices_subset.insert ((*indices)[(int) (indices->size () * (rand () / (RAND_MAX + 1.0)))]);
143  }*/
144 
145  /** \brief Return the best model found so far.
146  * \param model the resultant model
147  */
148  inline void
149  getModel (Indices &model) { model = model_; }
150 
151  /** \brief Return the best set of inliers found so far for this model.
152  */
153  // inline void
154  // getInliers (std::vector<int> &inliers) { inliers = inliers_; }
155  inline IndicesPtr
156  getInliers () { return inliers_; }
157 
158  // inline void
159  // getInliersStencil (Indices &inliers) { inliers = inliers_stencil_; }
160  inline IndicesPtr
162 
163  /** \brief Return the model coefficients of the best model found so far.
164  * \param model_coefficients the resultant model coefficients
165  */
166  inline void
167  getModelCoefficients (Coefficients &model_coefficients)
168  {
169  model_coefficients = model_coefficients_;
170  }
171 
172  protected:
173  /** \brief The underlying data model used (what is it that we attempt to search for). */
174  SampleConsensusModelPtr sac_model_;
175 
176  /** \brief The model found after the last computeModel () as point cloud indices. */
177  Indices model_;
178 
179  /** \brief The indices of the points that were chosen as inliers after the last call. */
180  IndicesPtr inliers_;
181  IndicesPtr inliers_stencil_;
182 
183  /** \brief The coefficients of our model computed directly from the model found. */
184  Coefficients model_coefficients_;
185 
186  /** \brief Desired probability of choosing at least one sample free from outliers. */
188 
189  /** \brief Total number of internal loop iterations that we've done so far. */
191 
192  /** \brief Distance to model threshold. */
193  float threshold_;
194 
195  /** \brief Maximum number of iterations before giving up. */
197  };
198  } // namespace
199 } // namespace
200 
201 #endif //#ifndef PCL_CUDA_SAMPLE_CONSENSUS_H_
int getMaxIterations()
Get the maximum number of iterations, as set by the user.
Definition: sac.h:110
SampleConsensus(const SampleConsensusModelPtr &model, float threshold)
Constructor for base SAC.
Definition: sac.h:84
virtual ~SampleConsensus()
Destructor for base SAC.
Definition: sac.h:90
virtual bool computeModel(int debug_verbosity_level=0)=0
Compute the actual model.
void getModelCoefficients(Coefficients &model_coefficients)
Return the model coefficients of the best model found so far.
Definition: sac.h:167
boost::shared_ptr< const SampleConsensus > ConstPtr
Definition: sac.h:70
float getProbability()
Obtain the probability of choosing at least one sample free from outliers, as set by the user...
Definition: sac.h:125
boost::shared_ptr< typename Storage< int >::type > IndicesPtr
Definition: sac_model.h:99
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
void setDistanceThreshold(float threshold)
Set the distance to model threshold.
Definition: sac.h:96
float probability_
Desired probability of choosing at least one sample free from outliers.
Definition: sac.h:187
SampleConsensus(const SampleConsensusModelPtr &model)
Constructor for base SAC.
Definition: sac.h:75
boost::shared_ptr< const typename Storage< int >::type > IndicesConstPtr
Definition: sac_model.h:100
IndicesPtr getInliers()
Return the best set of inliers found so far for this model.
Definition: sac.h:156
Storage< int >::type Indices
Definition: sac_model.h:98
Coefficients model_coefficients_
The coefficients of our model computed directly from the model found.
Definition: sac.h:184
Storage< float >::type Coefficients
Definition: sac.h:62
int iterations_
Total number of internal loop iterations that we&#39;ve done so far.
Definition: sac.h:190
void setMaxIterations(int max_iterations)
Set the maximum number of iterations.
Definition: sac.h:106
void getModel(Indices &model)
Return the best model found so far.
Definition: sac.h:149
boost::shared_ptr< const Coefficients > CoefficientsConstPtr
Definition: sac.h:67
Storage< float4 >::type Hypotheses
Definition: sac_model.h:106
boost::shared_ptr< SampleConsensusModel > Ptr
Definition: sac_model.h:95
void setProbability(float probability)
Set the desired probability of choosing at least one sample free from outliers.
Definition: sac.h:119
boost::shared_ptr< Coefficients > CoefficientsPtr
Definition: sac.h:66
IndicesPtr inliers_
The indices of the points that were chosen as inliers after the last call.
Definition: sac.h:180
boost::shared_ptr< SampleConsensus > Ptr
Definition: sac.h:69
float getDistanceThreshold()
Get the distance to model threshold, as set by the user.
Definition: sac.h:100
float threshold_
Distance to model threshold.
Definition: sac.h:193
int max_iterations_
Maximum number of iterations before giving up.
Definition: sac.h:196
IndicesPtr inliers_stencil_
Definition: sac.h:181
SampleConsensusModelPtr sac_model_
The underlying data model used (what is it that we attempt to search for).
Definition: sac.h:174
IndicesPtr getInliersStencil()
Definition: sac.h:161
Indices model_
The model found after the last computeModel () as point cloud indices.
Definition: sac.h:177