Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sac_model_cylinder.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_model_cylinder.h 6144 2012-07-04 22:06:28Z rusu $
37  *
38  */
39 
40 #ifndef PCL_SAMPLE_CONSENSUS_MODEL_CYLINDER_H_
41 #define PCL_SAMPLE_CONSENSUS_MODEL_CYLINDER_H_
42 
45 #include <pcl/common/common.h>
46 #include <pcl/common/distances.h>
47 
48 namespace pcl
49 {
63  template <typename PointT, typename PointNT>
65  {
72 
73  public:
77 
78  typedef boost::shared_ptr<SampleConsensusModelCylinder> Ptr;
79 
84  SampleConsensusModel<PointT> (cloud),
85  axis_ (Eigen::Vector3f::Zero ()),
86  eps_angle_ (0),
87  tmp_inliers_ ()
88  {
89  }
90 
95  SampleConsensusModelCylinder (const PointCloudConstPtr &cloud, const std::vector<int> &indices) :
96  SampleConsensusModel<PointT> (cloud, indices),
97  axis_ (Eigen::Vector3f::Zero ()),
98  eps_angle_ (0),
99  tmp_inliers_ ()
100  {
101  }
102 
108  axis_ (Eigen::Vector3f::Zero ()),
109  eps_angle_ (0),
110  tmp_inliers_ ()
111  {
112  *this = source;
113  }
114 
120  {
122  axis_ = source.axis_;
123  eps_angle_ = source.eps_angle_;
124  tmp_inliers_ = source.tmp_inliers_;
125  return (*this);
126  }
127 
131  inline void
132  setEpsAngle (const double ea) { eps_angle_ = ea; }
133 
135  inline double
136  getEpsAngle () { return (eps_angle_); }
137 
141  inline void
142  setAxis (const Eigen::Vector3f &ax) { axis_ = ax; }
143 
145  inline Eigen::Vector3f
146  getAxis () { return (axis_); }
147 
154  bool
155  computeModelCoefficients (const std::vector<int> &samples,
156  Eigen::VectorXf &model_coefficients);
157 
162  void
163  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
164  std::vector<double> &distances);
165 
171  void
172  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
173  const double threshold,
174  std::vector<int> &inliers);
175 
182  virtual int
183  countWithinDistance (const Eigen::VectorXf &model_coefficients,
184  const double threshold);
185 
192  void
193  optimizeModelCoefficients (const std::vector<int> &inliers,
194  const Eigen::VectorXf &model_coefficients,
195  Eigen::VectorXf &optimized_coefficients);
196 
197 
204  void
205  projectPoints (const std::vector<int> &inliers,
206  const Eigen::VectorXf &model_coefficients,
207  PointCloud &projected_points,
208  bool copy_data_fields = true);
209 
215  bool
216  doSamplesVerifyModel (const std::set<int> &indices,
217  const Eigen::VectorXf &model_coefficients,
218  const double threshold);
219 
221  inline pcl::SacModel
222  getModelType () const { return (SACMODEL_CYLINDER); }
223 
224  protected:
229  double
230  pointToLineDistance (const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients);
231 
238  inline void
239  projectPointToLine (const Eigen::Vector4f &pt,
240  const Eigen::Vector4f &line_pt,
241  const Eigen::Vector4f &line_dir,
242  Eigen::Vector4f &pt_proj)
243  {
244  float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
245  // Calculate the projection of the point on the line
246  pt_proj = line_pt + k * line_dir;
247  }
248 
255  void
256  projectPointToCylinder (const Eigen::Vector4f &pt,
257  const Eigen::VectorXf &model_coefficients,
258  Eigen::Vector4f &pt_proj);
259 
261  std::string
262  getName () const { return ("SampleConsensusModelCylinder"); }
263 
264  protected:
268  bool
269  isModelValid (const Eigen::VectorXf &model_coefficients);
270 
275  bool
276  isSampleGood (const std::vector<int> &samples) const;
277 
278  private:
280  Eigen::Vector3f axis_;
281 
283  double eps_angle_;
284 
286  const std::vector<int> *tmp_inliers_;
287 
288 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
289 #pragma GCC diagnostic ignored "-Weffc++"
290 #endif
291 
292  struct OptimizationFunctor : pcl::Functor<float>
293  {
299  OptimizationFunctor (int m_data_points, pcl::SampleConsensusModelCylinder<PointT, PointNT> *model) :
300  pcl::Functor<float> (m_data_points), model_ (model) {}
301 
307  int
308  operator() (const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const
309  {
310  Eigen::Vector4f line_pt (x[0], x[1], x[2], 0);
311  Eigen::Vector4f line_dir (x[3], x[4], x[5], 0);
312 
313  for (int i = 0; i < values (); ++i)
314  {
315  // dist = f - r
316  Eigen::Vector4f pt (model_->input_->points[(*model_->tmp_inliers_)[i]].x,
317  model_->input_->points[(*model_->tmp_inliers_)[i]].y,
318  model_->input_->points[(*model_->tmp_inliers_)[i]].z, 0);
319 
320  fvec[i] = static_cast<float> (pcl::sqrPointToLineDistance (pt, line_pt, line_dir) - x[6]*x[6]);
321  }
322  return (0);
323  }
324 
326  };
327 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
328 #pragma GCC diagnostic warning "-Weffc++"
329 #endif
330  };
331 }
332 
333 #endif //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_CYLINDER_H_