Point Cloud Library (PCL)  1.8.0
sac_model_cylinder.h
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  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #ifndef PCL_SAMPLE_CONSENSUS_MODEL_CYLINDER_H_
42 #define PCL_SAMPLE_CONSENSUS_MODEL_CYLINDER_H_
43 
44 #include <pcl/sample_consensus/sac_model.h>
45 #include <pcl/sample_consensus/model_types.h>
46 #include <pcl/pcl_macros.h>
47 #include <pcl/common/common.h>
48 #include <pcl/common/distances.h>
49 
50 namespace pcl
51 {
52  /** \brief @b SampleConsensusModelCylinder defines a model for 3D cylinder segmentation.
53  * The model coefficients are defined as:
54  * - \b point_on_axis.x : the X coordinate of a point located on the cylinder axis
55  * - \b point_on_axis.y : the Y coordinate of a point located on the cylinder axis
56  * - \b point_on_axis.z : the Z coordinate of a point located on the cylinder axis
57  * - \b axis_direction.x : the X coordinate of the cylinder's axis direction
58  * - \b axis_direction.y : the Y coordinate of the cylinder's axis direction
59  * - \b axis_direction.z : the Z coordinate of the cylinder's axis direction
60  * - \b radius : the cylinder's radius
61  *
62  * \author Radu Bogdan Rusu
63  * \ingroup sample_consensus
64  */
65  template <typename PointT, typename PointNT>
67  {
68  public:
77 
81 
82  typedef boost::shared_ptr<SampleConsensusModelCylinder> Ptr;
83 
84  /** \brief Constructor for base SampleConsensusModelCylinder.
85  * \param[in] cloud the input point cloud dataset
86  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
87  */
88  SampleConsensusModelCylinder (const PointCloudConstPtr &cloud, bool random = false)
89  : SampleConsensusModel<PointT> (cloud, random)
91  , axis_ (Eigen::Vector3f::Zero ())
92  , eps_angle_ (0)
93  , tmp_inliers_ ()
94  {
95  model_name_ = "SampleConsensusModelCylinder";
96  sample_size_ = 2;
97  model_size_ = 7;
98  }
99 
100  /** \brief Constructor for base SampleConsensusModelCylinder.
101  * \param[in] cloud the input point cloud dataset
102  * \param[in] indices a vector of point indices to be used from \a cloud
103  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
104  */
105  SampleConsensusModelCylinder (const PointCloudConstPtr &cloud,
106  const std::vector<int> &indices,
107  bool random = false)
108  : SampleConsensusModel<PointT> (cloud, indices, random)
110  , axis_ (Eigen::Vector3f::Zero ())
111  , eps_angle_ (0)
112  , tmp_inliers_ ()
113  {
114  model_name_ = "SampleConsensusModelCylinder";
115  sample_size_ = 2;
116  model_size_ = 7;
117  }
118 
119  /** \brief Copy constructor.
120  * \param[in] source the model to copy into this
121  */
125  axis_ (Eigen::Vector3f::Zero ()),
126  eps_angle_ (0),
127  tmp_inliers_ ()
128  {
129  *this = source;
130  model_name_ = "SampleConsensusModelCylinder";
131  }
132 
133  /** \brief Empty destructor */
135 
136  /** \brief Copy constructor.
137  * \param[in] source the model to copy into this
138  */
141  {
144  axis_ = source.axis_;
145  eps_angle_ = source.eps_angle_;
146  tmp_inliers_ = source.tmp_inliers_;
147  return (*this);
148  }
149 
150  /** \brief Set the angle epsilon (delta) threshold.
151  * \param[in] ea the maximum allowed difference between the cylinder axis and the given axis.
152  */
153  inline void
154  setEpsAngle (const double ea) { eps_angle_ = ea; }
155 
156  /** \brief Get the angle epsilon (delta) threshold. */
157  inline double
158  getEpsAngle () { return (eps_angle_); }
159 
160  /** \brief Set the axis along which we need to search for a cylinder direction.
161  * \param[in] ax the axis along which we need to search for a cylinder direction
162  */
163  inline void
164  setAxis (const Eigen::Vector3f &ax) { axis_ = ax; }
165 
166  /** \brief Get the axis along which we need to search for a cylinder direction. */
167  inline Eigen::Vector3f
168  getAxis () { return (axis_); }
169 
170  /** \brief Check whether the given index samples can form a valid cylinder model, compute the model coefficients
171  * from these samples and store them in model_coefficients. The cylinder coefficients are: point_on_axis,
172  * axis_direction, cylinder_radius_R
173  * \param[in] samples the point indices found as possible good candidates for creating a valid model
174  * \param[out] model_coefficients the resultant model coefficients
175  */
176  bool
177  computeModelCoefficients (const std::vector<int> &samples,
178  Eigen::VectorXf &model_coefficients);
179 
180  /** \brief Compute all distances from the cloud data to a given cylinder model.
181  * \param[in] model_coefficients the coefficients of a cylinder model that we need to compute distances to
182  * \param[out] distances the resultant estimated distances
183  */
184  void
185  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
186  std::vector<double> &distances);
187 
188  /** \brief Select all the points which respect the given model coefficients as inliers.
189  * \param[in] model_coefficients the coefficients of a cylinder model that we need to compute distances to
190  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
191  * \param[out] inliers the resultant model inliers
192  */
193  void
194  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
195  const double threshold,
196  std::vector<int> &inliers);
197 
198  /** \brief Count all the points which respect the given model coefficients as inliers.
199  *
200  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
201  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
202  * \return the resultant number of inliers
203  */
204  virtual int
205  countWithinDistance (const Eigen::VectorXf &model_coefficients,
206  const double threshold);
207 
208  /** \brief Recompute the cylinder coefficients using the given inlier set and return them to the user.
209  * @note: these are the coefficients of the cylinder model after refinement (e.g. after SVD)
210  * \param[in] inliers the data inliers found as supporting the model
211  * \param[in] model_coefficients the initial guess for the optimization
212  * \param[out] optimized_coefficients the resultant recomputed coefficients after non-linear optimization
213  */
214  void
215  optimizeModelCoefficients (const std::vector<int> &inliers,
216  const Eigen::VectorXf &model_coefficients,
217  Eigen::VectorXf &optimized_coefficients);
218 
219 
220  /** \brief Create a new point cloud with inliers projected onto the cylinder model.
221  * \param[in] inliers the data inliers that we want to project on the cylinder model
222  * \param[in] model_coefficients the coefficients of a cylinder model
223  * \param[out] projected_points the resultant projected points
224  * \param[in] copy_data_fields set to true if we need to copy the other data fields
225  */
226  void
227  projectPoints (const std::vector<int> &inliers,
228  const Eigen::VectorXf &model_coefficients,
229  PointCloud &projected_points,
230  bool copy_data_fields = true);
231 
232  /** \brief Verify whether a subset of indices verifies the given cylinder model coefficients.
233  * \param[in] indices the data indices that need to be tested against the cylinder model
234  * \param[in] model_coefficients the cylinder model coefficients
235  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
236  */
237  bool
238  doSamplesVerifyModel (const std::set<int> &indices,
239  const Eigen::VectorXf &model_coefficients,
240  const double threshold);
241 
242  /** \brief Return an unique id for this model (SACMODEL_CYLINDER). */
243  inline pcl::SacModel
244  getModelType () const { return (SACMODEL_CYLINDER); }
245 
246  protected:
249 
250  /** \brief Get the distance from a point to a line (represented by a point and a direction)
251  * \param[in] pt a point
252  * \param[in] model_coefficients the line coefficients (a point on the line, line direction)
253  */
254  double
255  pointToLineDistance (const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients);
256 
257  /** \brief Project a point onto a line given by a point and a direction vector
258  * \param[in] pt the input point to project
259  * \param[in] line_pt the point on the line (make sure that line_pt[3] = 0 as there are no internal checks!)
260  * \param[in] line_dir the direction of the line (make sure that line_dir[3] = 0 as there are no internal checks!)
261  * \param[out] pt_proj the resultant projected point
262  */
263  inline void
264  projectPointToLine (const Eigen::Vector4f &pt,
265  const Eigen::Vector4f &line_pt,
266  const Eigen::Vector4f &line_dir,
267  Eigen::Vector4f &pt_proj)
268  {
269  float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
270  // Calculate the projection of the point on the line
271  pt_proj = line_pt + k * line_dir;
272  }
273 
274  /** \brief Project a point onto a cylinder given by its model coefficients (point_on_axis, axis_direction,
275  * cylinder_radius_R)
276  * \param[in] pt the input point to project
277  * \param[in] model_coefficients the coefficients of the cylinder (point_on_axis, axis_direction, cylinder_radius_R)
278  * \param[out] pt_proj the resultant projected point
279  */
280  void
281  projectPointToCylinder (const Eigen::Vector4f &pt,
282  const Eigen::VectorXf &model_coefficients,
283  Eigen::Vector4f &pt_proj);
284 
285  /** \brief Get a string representation of the name of this class. */
286  PCL_DEPRECATED ("[pcl::SampleConsensusModelCylinder::getName] getName is deprecated. Please use getClassName instead.")
287  std::string
288  getName () const { return (model_name_); }
289 
290  protected:
291  /** \brief Check whether a model is valid given the user constraints.
292  * \param[in] model_coefficients the set of model coefficients
293  */
294  virtual bool
295  isModelValid (const Eigen::VectorXf &model_coefficients);
296 
297  /** \brief Check if a sample of indices results in a good sample of points
298  * indices. Pure virtual.
299  * \param[in] samples the resultant index samples
300  */
301  bool
302  isSampleGood (const std::vector<int> &samples) const;
303 
304  private:
305  /** \brief The axis along which we need to search for a plane perpendicular to. */
306  Eigen::Vector3f axis_;
307 
308  /** \brief The maximum allowed difference between the plane normal and the given axis. */
309  double eps_angle_;
310 
311  /** \brief temporary pointer to a list of given indices for optimizeModelCoefficients () */
312  const std::vector<int> *tmp_inliers_;
313 
314 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
315 #pragma GCC diagnostic ignored "-Weffc++"
316 #endif
317  /** \brief Functor for the optimization function */
318  struct OptimizationFunctor : pcl::Functor<float>
319  {
320  /** Functor constructor
321  * \param[in] m_data_points the number of data points to evaluate
322  * \param[in] estimator pointer to the estimator object
323  * \param[in] distance distance computation function pointer
324  */
325  OptimizationFunctor (int m_data_points, pcl::SampleConsensusModelCylinder<PointT, PointNT> *model) :
326  pcl::Functor<float> (m_data_points), model_ (model) {}
327 
328  /** Cost function to be minimized
329  * \param[in] x variables array
330  * \param[out] fvec resultant functions evaluations
331  * \return 0
332  */
333  int
334  operator() (const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const
335  {
336  Eigen::Vector4f line_pt (x[0], x[1], x[2], 0);
337  Eigen::Vector4f line_dir (x[3], x[4], x[5], 0);
338 
339  for (int i = 0; i < values (); ++i)
340  {
341  // dist = f - r
342  Eigen::Vector4f pt (model_->input_->points[(*model_->tmp_inliers_)[i]].x,
343  model_->input_->points[(*model_->tmp_inliers_)[i]].y,
344  model_->input_->points[(*model_->tmp_inliers_)[i]].z, 0);
345 
346  fvec[i] = static_cast<float> (pcl::sqrPointToLineDistance (pt, line_pt, line_dir) - x[6]*x[6]);
347  }
348  return (0);
349  }
350 
352  };
353 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
354 #pragma GCC diagnostic warning "-Weffc++"
355 #endif
356  };
357 }
358 
359 #ifdef PCL_NO_PRECOMPILE
360 #include <pcl/sample_consensus/impl/sac_model_cylinder.hpp>
361 #endif
362 
363 #endif //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_CYLINDER_H_
SampleConsensusModel< PointT >::PointCloud PointCloud
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances)
Compute all distances from the cloud data to a given cylinder model.
SampleConsensusModelCylinder defines a model for 3D cylinder segmentation.
pcl::SacModel getModelType() const
Return an unique id for this model (SACMODEL_CYLINDER).
Eigen::Vector3f getAxis()
Get the axis along which we need to search for a cylinder direction.
SampleConsensusModelCylinder(const PointCloudConstPtr &cloud, const std::vector< int > &indices, bool random=false)
Constructor for base SampleConsensusModelCylinder.
void optimizeModelCoefficients(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
Recompute the cylinder coefficients using the given inlier set and return them to the user...
void setEpsAngle(const double ea)
Set the angle epsilon (delta) threshold.
bool doSamplesVerifyModel(const std::set< int > &indices, const Eigen::VectorXf &model_coefficients, const double threshold)
Verify whether a subset of indices verifies the given cylinder model coefficients.
unsigned int model_size_
The number of coefficients in the model.
Definition: sac_model.h:575
Base functor all the models that need non linear optimization must define their own one and implement...
Definition: sac_model.h:653
SampleConsensusModelCylinder & operator=(const SampleConsensusModelCylinder &source)
Copy constructor.
boost::shared_ptr< SampleConsensusModelCylinder > Ptr
bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients)
Check whether the given index samples can form a valid cylinder model, compute the model coefficients...
Definition: bfgs.h:10
Define standard C methods to do distance calculations.
SampleConsensusModel< PointT >::PointCloudConstPtr PointCloudConstPtr
virtual bool isModelValid(const Eigen::VectorXf &model_coefficients)
Check whether a model is valid given the user constraints.
double pointToLineDistance(const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients)
Get the distance from a point to a line (represented by a point and a direction)
void setAxis(const Eigen::Vector3f &ax)
Set the axis along which we need to search for a cylinder direction.
Define standard C methods and C++ classes that are common to all methods.
virtual int countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold)
Count all the points which respect the given model coefficients as inliers.
SampleConsensusModel represents the base model class.
Definition: sac_model.h:66
double sqrPointToLineDistance(const Eigen::Vector4f &pt, const Eigen::Vector4f &line_pt, const Eigen::Vector4f &line_dir)
Get the square distance from a point to a line (represented by a point and a direction) ...
Definition: distances.h:69
void projectPointToLine(const Eigen::Vector4f &pt, const Eigen::Vector4f &line_pt, const Eigen::Vector4f &line_dir, Eigen::Vector4f &pt_proj)
Project a point onto a line given by a point and a direction vector.
std::string model_name_
The model name.
Definition: sac_model.h:534
pcl::PointCloud< PointT >::Ptr PointCloudPtr
Definition: sac_model.h:71
SampleConsensusModelCylinder(const SampleConsensusModelCylinder &source)
Copy constructor.
bool isSampleGood(const std::vector< int > &samples) const
Check if a sample of indices results in a good sample of points indices.
SampleConsensusModelFromNormals represents the base model class for models that require the use of su...
Definition: sac_model.h:591
PointCloud represents the base class in PCL for storing collections of 3D points. ...
SacModel
Definition: model_types.h:48
pcl::PointCloud< PointT >::ConstPtr PointCloudConstPtr
Definition: sac_model.h:70
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, std::vector< int > &inliers)
Select all the points which respect the given model coefficients as inliers.
void projectPointToCylinder(const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients, Eigen::Vector4f &pt_proj)
Project a point onto a cylinder given by its model coefficients (point_on_axis, axis_direction, cylinder_radius_R)
std::string getName() const
Get a string representation of the name of this class.
A point structure representing Euclidean xyz coordinates, and the RGB color.
virtual ~SampleConsensusModelCylinder()
Empty destructor.
double getEpsAngle()
Get the angle epsilon (delta) threshold.
void projectPoints(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true)
Create a new point cloud with inliers projected onto the cylinder model.
SampleConsensusModel< PointT >::PointCloudPtr PointCloudPtr
SampleConsensusModelCylinder(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModelCylinder.
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition: sac_model.h:572