Point Cloud Library (PCL)  1.14.0-dev
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 #pragma once
42 
43 #include <pcl/sample_consensus/sac_model.h>
44 #include <pcl/sample_consensus/model_types.h>
45 #include <pcl/common/distances.h>
46 #include <pcl/pcl_exports.h>
47 
48 namespace pcl
49 {
50  namespace internal {
51  PCL_EXPORTS int optimizeModelCoefficientsCylinder (Eigen::VectorXf& coeff, const Eigen::ArrayXf& pts_x, const Eigen::ArrayXf& pts_y, const Eigen::ArrayXf& pts_z);
52  } // namespace internal
53 
54  /** \brief @b SampleConsensusModelCylinder defines a model for 3D cylinder segmentation.
55  * The model coefficients are defined as:
56  * - \b point_on_axis.x : the X coordinate of a point located on the cylinder axis
57  * - \b point_on_axis.y : the Y coordinate of a point located on the cylinder axis
58  * - \b point_on_axis.z : the Z coordinate of a point located on the cylinder axis
59  * - \b axis_direction.x : the X coordinate of the cylinder's axis direction
60  * - \b axis_direction.y : the Y coordinate of the cylinder's axis direction
61  * - \b axis_direction.z : the Z coordinate of the cylinder's axis direction
62  * - \b radius : the cylinder's radius
63  *
64  * \author Radu Bogdan Rusu
65  * \ingroup sample_consensus
66  */
67  template <typename PointT, typename PointNT>
69  {
70  public:
79 
83 
84  using Ptr = shared_ptr<SampleConsensusModelCylinder<PointT, PointNT> >;
85  using ConstPtr = shared_ptr<const SampleConsensusModelCylinder<PointT, PointNT>>;
86 
87  /** \brief Constructor for base SampleConsensusModelCylinder.
88  * \param[in] cloud the input point cloud dataset
89  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
90  */
91  SampleConsensusModelCylinder (const PointCloudConstPtr &cloud, bool random = false)
92  : SampleConsensusModel<PointT> (cloud, random)
94  , axis_ (Eigen::Vector3f::Zero ())
95  , eps_angle_ (0)
96  {
97  model_name_ = "SampleConsensusModelCylinder";
98  sample_size_ = 2;
99  model_size_ = 7;
100  }
101 
102  /** \brief Constructor for base SampleConsensusModelCylinder.
103  * \param[in] cloud the input point cloud dataset
104  * \param[in] indices a vector of point indices to be used from \a cloud
105  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
106  */
108  const Indices &indices,
109  bool random = false)
110  : SampleConsensusModel<PointT> (cloud, indices, random)
112  , axis_ (Eigen::Vector3f::Zero ())
113  , eps_angle_ (0)
114  {
115  model_name_ = "SampleConsensusModelCylinder";
116  sample_size_ = 2;
117  model_size_ = 7;
118  }
119 
120  /** \brief Copy constructor.
121  * \param[in] source the model to copy into this
122  */
126  axis_ (Eigen::Vector3f::Zero ()),
127  eps_angle_ (0)
128  {
129  *this = source;
130  model_name_ = "SampleConsensusModelCylinder";
131  }
132 
133  /** \brief Empty destructor */
134  ~SampleConsensusModelCylinder () override = default;
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  return (*this);
147  }
148 
149  /** \brief Set the angle epsilon (delta) threshold.
150  * \param[in] ea the maximum allowed difference between the cylinder axis and the given axis.
151  */
152  inline void
153  setEpsAngle (const double ea) { eps_angle_ = ea; }
154 
155  /** \brief Get the angle epsilon (delta) threshold. */
156  inline double
157  getEpsAngle () const { return (eps_angle_); }
158 
159  /** \brief Set the axis along which we need to search for a cylinder direction.
160  * \param[in] ax the axis along which we need to search for a cylinder direction
161  */
162  inline void
163  setAxis (const Eigen::Vector3f &ax) { axis_ = ax; }
164 
165  /** \brief Get the axis along which we need to search for a cylinder direction. */
166  inline Eigen::Vector3f
167  getAxis () const { return (axis_); }
168 
169  /** \brief Check whether the given index samples can form a valid cylinder model, compute the model coefficients
170  * from these samples and store them in model_coefficients. The cylinder coefficients are: point_on_axis,
171  * axis_direction, cylinder_radius_R
172  * \param[in] samples the point indices found as possible good candidates for creating a valid model
173  * \param[out] model_coefficients the resultant model coefficients
174  */
175  bool
176  computeModelCoefficients (const Indices &samples,
177  Eigen::VectorXf &model_coefficients) const override;
178 
179  /** \brief Compute all distances from the cloud data to a given cylinder model.
180  * \param[in] model_coefficients the coefficients of a cylinder model that we need to compute distances to
181  * \param[out] distances the resultant estimated distances
182  */
183  void
184  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
185  std::vector<double> &distances) const override;
186 
187  /** \brief Select all the points which respect the given model coefficients as inliers.
188  * \param[in] model_coefficients the coefficients of a cylinder model that we need to compute distances to
189  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
190  * \param[out] inliers the resultant model inliers
191  */
192  void
193  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
194  const double threshold,
195  Indices &inliers) override;
196 
197  /** \brief Count all the points which respect the given model coefficients as inliers.
198  *
199  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
200  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
201  * \return the resultant number of inliers
202  */
203  std::size_t
204  countWithinDistance (const Eigen::VectorXf &model_coefficients,
205  const double threshold) const override;
206 
207  /** \brief Recompute the cylinder coefficients using the given inlier set and return them to the user.
208  * @note: these are the coefficients of the cylinder model after refinement (e.g. after SVD)
209  * \param[in] inliers the data inliers found as supporting the model
210  * \param[in] model_coefficients the initial guess for the optimization
211  * \param[out] optimized_coefficients the resultant recomputed coefficients after non-linear optimization
212  */
213  void
214  optimizeModelCoefficients (const Indices &inliers,
215  const Eigen::VectorXf &model_coefficients,
216  Eigen::VectorXf &optimized_coefficients) const override;
217 
218 
219  /** \brief Create a new point cloud with inliers projected onto the cylinder model.
220  * \param[in] inliers the data inliers that we want to project on the cylinder model
221  * \param[in] model_coefficients the coefficients of a cylinder model
222  * \param[out] projected_points the resultant projected points
223  * \param[in] copy_data_fields set to true if we need to copy the other data fields
224  */
225  void
226  projectPoints (const Indices &inliers,
227  const Eigen::VectorXf &model_coefficients,
228  PointCloud &projected_points,
229  bool copy_data_fields = true) const override;
230 
231  /** \brief Verify whether a subset of indices verifies the given cylinder model coefficients.
232  * \param[in] indices the data indices that need to be tested against the cylinder model
233  * \param[in] model_coefficients the cylinder model coefficients
234  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
235  */
236  bool
237  doSamplesVerifyModel (const std::set<index_t> &indices,
238  const Eigen::VectorXf &model_coefficients,
239  const double threshold) const override;
240 
241  /** \brief Return a unique id for this model (SACMODEL_CYLINDER). */
242  inline pcl::SacModel
243  getModelType () const override { return (SACMODEL_CYLINDER); }
244 
245  protected:
248 
249  /** \brief Get the distance from a point to a line (represented by a point and a direction)
250  * \param[in] pt a point
251  * \param[in] model_coefficients the line coefficients (a point on the line, line direction)
252  */
253  double
254  pointToLineDistance (const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients) const;
255 
256  /** \brief Project a point onto a line given by a point and a direction vector
257  * \param[in] pt the input point to project
258  * \param[in] line_pt the point on the line (make sure that line_pt[3] = 0 as there are no internal checks!)
259  * \param[in] line_dir the direction of the line (make sure that line_dir[3] = 0 as there are no internal checks!)
260  * \param[out] pt_proj the resultant projected point
261  */
262  inline void
263  projectPointToLine (const Eigen::Vector4f &pt,
264  const Eigen::Vector4f &line_pt,
265  const Eigen::Vector4f &line_dir,
266  Eigen::Vector4f &pt_proj) const
267  {
268  float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
269  // Calculate the projection of the point on the line
270  pt_proj = line_pt + k * line_dir;
271  }
272 
273  /** \brief Project a point onto a cylinder given by its model coefficients (point_on_axis, axis_direction,
274  * cylinder_radius_R)
275  * \param[in] pt the input point to project
276  * \param[in] model_coefficients the coefficients of the cylinder (point_on_axis, axis_direction, cylinder_radius_R)
277  * \param[out] pt_proj the resultant projected point
278  */
279  void
280  projectPointToCylinder (const Eigen::Vector4f &pt,
281  const Eigen::VectorXf &model_coefficients,
282  Eigen::Vector4f &pt_proj) const;
283 
284  /** \brief Check whether a model is valid given the user constraints.
285  * \param[in] model_coefficients the set of model coefficients
286  */
287  bool
288  isModelValid (const Eigen::VectorXf &model_coefficients) const override;
289 
290  /** \brief Check if a sample of indices results in a good sample of points
291  * indices. Pure virtual.
292  * \param[in] samples the resultant index samples
293  */
294  bool
295  isSampleGood (const Indices &samples) const override;
296 
297  private:
298  /** \brief The axis along which we need to search for a cylinder direction. */
299  Eigen::Vector3f axis_;
300 
301  /** \brief The maximum allowed difference between the cylinder direction and the given axis. */
302  double eps_angle_;
303  };
304 }
305 
306 #ifdef PCL_NO_PRECOMPILE
307 #include <pcl/sample_consensus/impl/sac_model_cylinder.hpp>
308 #endif
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
SampleConsensusModelCylinder defines a model for 3D cylinder segmentation.
SampleConsensusModelCylinder(const PointCloudConstPtr &cloud, const Indices &indices, bool random=false)
Constructor for base SampleConsensusModelCylinder.
pcl::SacModel getModelType() const override
Return a unique id for this model (SACMODEL_CYLINDER).
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the cloud data to a given cylinder model.
SampleConsensusModelCylinder(const SampleConsensusModelCylinder &source)
Copy constructor.
void projectPoints(const Indices &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true) const override
Create a new point cloud with inliers projected onto the cylinder model.
double getEpsAngle() const
Get the angle epsilon (delta) threshold.
void projectPointToLine(const Eigen::Vector4f &pt, const Eigen::Vector4f &line_pt, const Eigen::Vector4f &line_dir, Eigen::Vector4f &pt_proj) const
Project a point onto a line given by a point and a direction vector.
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the cylinder coefficients using the given inlier set and return them to the user.
std::size_t countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const override
Count all the points which respect the given model coefficients as inliers.
SampleConsensusModelCylinder & operator=(const SampleConsensusModelCylinder &source)
Copy constructor.
void projectPointToCylinder(const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients, Eigen::Vector4f &pt_proj) const
Project a point onto a cylinder given by its model coefficients (point_on_axis, axis_direction,...
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Select all the points which respect the given model coefficients as inliers.
SampleConsensusModelCylinder(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModelCylinder.
void setEpsAngle(const double ea)
Set the angle epsilon (delta) threshold.
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
void setAxis(const Eigen::Vector3f &ax)
Set the axis along which we need to search for a cylinder direction.
~SampleConsensusModelCylinder() override=default
Empty destructor.
Eigen::Vector3f getAxis() const
Get the axis along which we need to search for a cylinder direction.
bool doSamplesVerifyModel(const std::set< index_t > &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const override
Verify whether a subset of indices verifies the given cylinder model coefficients.
double pointToLineDistance(const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients) const
Get the distance from a point to a line (represented by a point and a direction)
bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
Check whether the given index samples can form a valid cylinder model, compute the model coefficients...
SampleConsensusModelFromNormals represents the base model class for models that require the use of su...
Definition: sac_model.h:613
SampleConsensusModel represents the base model class.
Definition: sac_model.h:71
shared_ptr< SampleConsensusModel< PointT > > Ptr
Definition: sac_model.h:78
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition: sac_model.h:589
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: sac_model.h:74
std::string model_name_
The model name.
Definition: sac_model.h:551
unsigned int model_size_
The number of coefficients in the model.
Definition: sac_model.h:592
typename PointCloud::Ptr PointCloudPtr
Definition: sac_model.h:75
shared_ptr< const SampleConsensusModel< PointT > > ConstPtr
Definition: sac_model.h:79
Define standard C methods to do distance calculations.
Definition: bfgs.h:10
PCL_EXPORTS int optimizeModelCoefficientsCylinder(Eigen::VectorXf &coeff, const Eigen::ArrayXf &pts_x, const Eigen::ArrayXf &pts_y, const Eigen::ArrayXf &pts_z)
SacModel
Definition: model_types.h:46
@ SACMODEL_CYLINDER
Definition: model_types.h:52
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
#define PCL_EXPORTS
Definition: pcl_macros.h:323
A point structure representing Euclidean xyz coordinates, and the RGB color.