Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sac_segmentation.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  * $Id: sac_segmentation.h 6155 2012-07-04 23:10:00Z aichim $
37  *
38  */
39 
40 #ifndef PCL_SEGMENTATION_SAC_SEGMENTATION_H_
41 #define PCL_SEGMENTATION_SAC_SEGMENTATION_H_
42 
43 #include <pcl/pcl_base.h>
44 #include <pcl/PointIndices.h>
45 #include <pcl/ModelCoefficients.h>
46 
47 // Sample Consensus methods
50 // Sample Consensus models
53 
54 #include <pcl/search/search.h>
55 
56 namespace pcl
57 {
64  template <typename PointT>
65  class SACSegmentation : public PCLBase<PointT>
66  {
69 
70  public:
73 
75  typedef typename PointCloud::Ptr PointCloudPtr;
78 
81 
83  SACSegmentation () : model_ (), sac_ (), model_type_ (-1), method_type_ (0),
84  threshold_ (0), optimize_coefficients_ (true),
85  radius_min_ (-std::numeric_limits<double>::max()), radius_max_ (std::numeric_limits<double>::max()), samples_radius_ (0.0), eps_angle_ (0.0),
86  axis_ (Eigen::Vector3f::Zero ()), max_iterations_ (50), probability_ (0.99)
87  {
88  //srand ((unsigned)time (0)); // set a random seed
89  }
90 
92  virtual ~SACSegmentation () { /*srv_.reset ();*/ };
93 
97  inline void
98  setModelType (int model) { model_type_ = model; }
99 
101  inline int
102  getModelType () const { return (model_type_); }
103 
105  inline SampleConsensusPtr
106  getMethod () const { return (sac_); }
107 
110  getModel () const { return (model_); }
111 
115  inline void
116  setMethodType (int method) { method_type_ = method; }
117 
119  inline int
120  getMethodType () const { return (method_type_); }
121 
125  inline void
126  setDistanceThreshold (double threshold) { threshold_ = threshold; }
127 
129  inline double
130  getDistanceThreshold () const { return (threshold_); }
131 
135  inline void
136  setMaxIterations (int max_iterations) { max_iterations_ = max_iterations; }
137 
139  inline int
140  getMaxIterations () const { return (max_iterations_); }
141 
145  inline void
146  setProbability (double probability) { probability_ = probability; }
147 
149  inline double
150  getProbability () const { return (probability_); }
151 
155  inline void
156  setOptimizeCoefficients (bool optimize) { optimize_coefficients_ = optimize; }
157 
159  inline bool
160  getOptimizeCoefficients () const { return (optimize_coefficients_); }
161 
167  inline void
168  setRadiusLimits (const double &min_radius, const double &max_radius)
169  {
170  radius_min_ = min_radius;
171  radius_max_ = max_radius;
172  }
173 
178  inline void
179  getRadiusLimits (double &min_radius, double &max_radius)
180  {
181  min_radius = radius_min_;
182  max_radius = radius_max_;
183  }
184 
188  inline void
189  setSamplesMaxDist (const double &radius, SearchPtr search)
190  {
191  samples_radius_ = radius;
192  samples_radius_search_ = search;
193  }
194 
199  inline void
200  getSamplesMaxDist (double &radius)
201  {
202  radius = samples_radius_;
203  }
204 
208  inline void
209  setAxis (const Eigen::Vector3f &ax) { axis_ = ax; }
210 
212  inline Eigen::Vector3f
213  getAxis () const { return (axis_); }
214 
218  inline void
219  setEpsAngle (double ea) { eps_angle_ = ea; }
220 
222  inline double
223  getEpsAngle () const { return (eps_angle_); }
224 
229  virtual void
230  segment (PointIndices &inliers, ModelCoefficients &model_coefficients);
231 
232  protected:
236  virtual bool
237  initSACModel (const int model_type);
238 
242  virtual void
243  initSAC (const int method_type);
244 
247 
249  SampleConsensusPtr sac_;
250 
252  int model_type_;
253 
255  int method_type_;
256 
258  double threshold_;
259 
261  bool optimize_coefficients_;
262 
264  double radius_min_, radius_max_;
265 
267  double samples_radius_;
268 
270  SearchPtr samples_radius_search_;
271 
273  double eps_angle_;
274 
276  Eigen::Vector3f axis_;
277 
279  int max_iterations_;
280 
282  double probability_;
283 
285  virtual std::string
286  getClassName () const { return ("SACSegmentation"); }
287  };
288 
293  template <typename PointT, typename PointNT>
295  {
302 
303  public:
306 
308  typedef typename PointCloud::Ptr PointCloudPtr;
310 
314 
318 
321  normals_ (),
322  distance_weight_ (0.1),
323  distance_from_origin_ (0),
324  min_angle_ (),
325  max_angle_ ()
326  {};
327 
332  inline void
333  setInputNormals (const PointCloudNConstPtr &normals) { normals_ = normals; }
334 
336  inline PointCloudNConstPtr
337  getInputNormals () const { return (normals_); }
338 
343  inline void
344  setNormalDistanceWeight (double distance_weight) { distance_weight_ = distance_weight; }
345 
348  inline double
349  getNormalDistanceWeight () const { return (distance_weight_); }
350 
354  inline void
355  setMinMaxOpeningAngle (const double &min_angle, const double &max_angle)
356  {
357  min_angle_ = min_angle;
358  max_angle_ = max_angle;
359  }
360 
362  inline void
363  getMinMaxOpeningAngle (double &min_angle, double &max_angle)
364  {
365  min_angle = min_angle_;
366  max_angle = max_angle_;
367  }
368 
372  inline void
373  setDistanceFromOrigin (const double d) { distance_from_origin_ = d; }
374 
376  inline double
377  getDistanceFromOrigin () const { return (distance_from_origin_); }
378 
379  protected:
381  PointCloudNConstPtr normals_;
382 
386  double distance_weight_;
387 
389  double distance_from_origin_;
390 
392  double min_angle_;
393  double max_angle_;
394 
398  virtual bool
399  initSACModel (const int model_type);
400 
402  virtual std::string
403  getClassName () const { return ("SACSegmentationFromNormals"); }
404  };
405 }
406 
407 #endif //#ifndef PCL_SEGMENTATION_SAC_SEGMENTATION_H_