Point Cloud Library (PCL) 1.12.0
Loading...
Searching...
No Matches
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>
46
47namespace pcl
48{
49 /** \brief @b SampleConsensusModelCylinder defines a model for 3D cylinder segmentation.
50 * The model coefficients are defined as:
51 * - \b point_on_axis.x : the X coordinate of a point located on the cylinder axis
52 * - \b point_on_axis.y : the Y coordinate of a point located on the cylinder axis
53 * - \b point_on_axis.z : the Z coordinate of a point located on the cylinder axis
54 * - \b axis_direction.x : the X coordinate of the cylinder's axis direction
55 * - \b axis_direction.y : the Y coordinate of the cylinder's axis direction
56 * - \b axis_direction.z : the Z coordinate of the cylinder's axis direction
57 * - \b radius : the cylinder's radius
58 *
59 * \author Radu Bogdan Rusu
60 * \ingroup sample_consensus
61 */
62 template <typename PointT, typename PointNT>
64 {
65 public:
74
78
79 using Ptr = shared_ptr<SampleConsensusModelCylinder<PointT, PointNT> >;
80 using ConstPtr = shared_ptr<const SampleConsensusModelCylinder<PointT, PointNT>>;
81
82 /** \brief Constructor for base SampleConsensusModelCylinder.
83 * \param[in] cloud the input point cloud dataset
84 * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
85 */
86 SampleConsensusModelCylinder (const PointCloudConstPtr &cloud, bool random = false)
87 : SampleConsensusModel<PointT> (cloud, random)
89 , axis_ (Eigen::Vector3f::Zero ())
90 , eps_angle_ (0)
91 {
92 model_name_ = "SampleConsensusModelCylinder";
93 sample_size_ = 2;
94 model_size_ = 7;
95 }
96
97 /** \brief Constructor for base SampleConsensusModelCylinder.
98 * \param[in] cloud the input point cloud dataset
99 * \param[in] indices a vector of point indices to be used from \a cloud
100 * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
101 */
103 const Indices &indices,
104 bool random = false)
105 : SampleConsensusModel<PointT> (cloud, indices, random)
107 , axis_ (Eigen::Vector3f::Zero ())
108 , eps_angle_ (0)
109 {
110 model_name_ = "SampleConsensusModelCylinder";
111 sample_size_ = 2;
112 model_size_ = 7;
113 }
114
115 /** \brief Copy constructor.
116 * \param[in] source the model to copy into this
117 */
121 axis_ (Eigen::Vector3f::Zero ()),
122 eps_angle_ (0)
123 {
124 *this = source;
125 model_name_ = "SampleConsensusModelCylinder";
126 }
127
128 /** \brief Empty destructor */
130
131 /** \brief Copy constructor.
132 * \param[in] source the model to copy into this
133 */
136 {
139 axis_ = source.axis_;
140 eps_angle_ = source.eps_angle_;
141 return (*this);
142 }
143
144 /** \brief Set the angle epsilon (delta) threshold.
145 * \param[in] ea the maximum allowed difference between the cylinder axis and the given axis.
146 */
147 inline void
148 setEpsAngle (const double ea) { eps_angle_ = ea; }
149
150 /** \brief Get the angle epsilon (delta) threshold. */
151 inline double
152 getEpsAngle () const { return (eps_angle_); }
153
154 /** \brief Set the axis along which we need to search for a cylinder direction.
155 * \param[in] ax the axis along which we need to search for a cylinder direction
156 */
157 inline void
158 setAxis (const Eigen::Vector3f &ax) { axis_ = ax; }
159
160 /** \brief Get the axis along which we need to search for a cylinder direction. */
161 inline Eigen::Vector3f
162 getAxis () const { return (axis_); }
163
164 /** \brief Check whether the given index samples can form a valid cylinder model, compute the model coefficients
165 * from these samples and store them in model_coefficients. The cylinder coefficients are: point_on_axis,
166 * axis_direction, cylinder_radius_R
167 * \param[in] samples the point indices found as possible good candidates for creating a valid model
168 * \param[out] model_coefficients the resultant model coefficients
169 */
170 bool
171 computeModelCoefficients (const Indices &samples,
172 Eigen::VectorXf &model_coefficients) const override;
173
174 /** \brief Compute all distances from the cloud data to a given cylinder model.
175 * \param[in] model_coefficients the coefficients of a cylinder model that we need to compute distances to
176 * \param[out] distances the resultant estimated distances
177 */
178 void
179 getDistancesToModel (const Eigen::VectorXf &model_coefficients,
180 std::vector<double> &distances) const override;
181
182 /** \brief Select all the points which respect the given model coefficients as inliers.
183 * \param[in] model_coefficients the coefficients of a cylinder model that we need to compute distances to
184 * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
185 * \param[out] inliers the resultant model inliers
186 */
187 void
188 selectWithinDistance (const Eigen::VectorXf &model_coefficients,
189 const double threshold,
190 Indices &inliers) override;
191
192 /** \brief Count all the points which respect the given model coefficients as inliers.
193 *
194 * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
195 * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
196 * \return the resultant number of inliers
197 */
198 std::size_t
199 countWithinDistance (const Eigen::VectorXf &model_coefficients,
200 const double threshold) const override;
201
202 /** \brief Recompute the cylinder coefficients using the given inlier set and return them to the user.
203 * @note: these are the coefficients of the cylinder model after refinement (e.g. after SVD)
204 * \param[in] inliers the data inliers found as supporting the model
205 * \param[in] model_coefficients the initial guess for the optimization
206 * \param[out] optimized_coefficients the resultant recomputed coefficients after non-linear optimization
207 */
208 void
209 optimizeModelCoefficients (const Indices &inliers,
210 const Eigen::VectorXf &model_coefficients,
211 Eigen::VectorXf &optimized_coefficients) const override;
212
213
214 /** \brief Create a new point cloud with inliers projected onto the cylinder model.
215 * \param[in] inliers the data inliers that we want to project on the cylinder model
216 * \param[in] model_coefficients the coefficients of a cylinder model
217 * \param[out] projected_points the resultant projected points
218 * \param[in] copy_data_fields set to true if we need to copy the other data fields
219 */
220 void
221 projectPoints (const Indices &inliers,
222 const Eigen::VectorXf &model_coefficients,
223 PointCloud &projected_points,
224 bool copy_data_fields = true) const override;
225
226 /** \brief Verify whether a subset of indices verifies the given cylinder model coefficients.
227 * \param[in] indices the data indices that need to be tested against the cylinder model
228 * \param[in] model_coefficients the cylinder model coefficients
229 * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
230 */
231 bool
232 doSamplesVerifyModel (const std::set<index_t> &indices,
233 const Eigen::VectorXf &model_coefficients,
234 const double threshold) const override;
235
236 /** \brief Return a unique id for this model (SACMODEL_CYLINDER). */
237 inline pcl::SacModel
238 getModelType () const override { return (SACMODEL_CYLINDER); }
239
240 protected:
243
244 /** \brief Get the distance from a point to a line (represented by a point and a direction)
245 * \param[in] pt a point
246 * \param[in] model_coefficients the line coefficients (a point on the line, line direction)
247 */
248 double
249 pointToLineDistance (const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients) const;
250
251 /** \brief Project a point onto a line given by a point and a direction vector
252 * \param[in] pt the input point to project
253 * \param[in] line_pt the point on the line (make sure that line_pt[3] = 0 as there are no internal checks!)
254 * \param[in] line_dir the direction of the line (make sure that line_dir[3] = 0 as there are no internal checks!)
255 * \param[out] pt_proj the resultant projected point
256 */
257 inline void
258 projectPointToLine (const Eigen::Vector4f &pt,
259 const Eigen::Vector4f &line_pt,
260 const Eigen::Vector4f &line_dir,
261 Eigen::Vector4f &pt_proj) const
262 {
263 float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
264 // Calculate the projection of the point on the line
265 pt_proj = line_pt + k * line_dir;
266 }
267
268 /** \brief Project a point onto a cylinder given by its model coefficients (point_on_axis, axis_direction,
269 * cylinder_radius_R)
270 * \param[in] pt the input point to project
271 * \param[in] model_coefficients the coefficients of the cylinder (point_on_axis, axis_direction, cylinder_radius_R)
272 * \param[out] pt_proj the resultant projected point
273 */
274 void
275 projectPointToCylinder (const Eigen::Vector4f &pt,
276 const Eigen::VectorXf &model_coefficients,
277 Eigen::Vector4f &pt_proj) const;
278
279 /** \brief Check whether a model is valid given the user constraints.
280 * \param[in] model_coefficients the set of model coefficients
281 */
282 bool
283 isModelValid (const Eigen::VectorXf &model_coefficients) const override;
284
285 /** \brief Check if a sample of indices results in a good sample of points
286 * indices. Pure virtual.
287 * \param[in] samples the resultant index samples
288 */
289 bool
290 isSampleGood (const Indices &samples) const override;
291
292 private:
293 /** \brief The axis along which we need to search for a cylinder direction. */
294 Eigen::Vector3f axis_;
295
296 /** \brief The maximum allowed difference between the cylinder direction and the given axis. */
297 double eps_angle_;
298
299 /** \brief Functor for the optimization function */
300 struct OptimizationFunctor : pcl::Functor<float>
301 {
302 /** Functor constructor
303 * \param[in] indices the indices of data points to evaluate
304 * \param[in] estimator pointer to the estimator object
305 */
306 OptimizationFunctor (const pcl::SampleConsensusModelCylinder<PointT, PointNT> *model, const Indices& indices) :
307 pcl::Functor<float> (indices.size ()), model_ (model), indices_ (indices) {}
308
309 /** Cost function to be minimized
310 * \param[in] x variables array
311 * \param[out] fvec resultant functions evaluations
312 * \return 0
313 */
314 int
315 operator() (const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const
316 {
317 Eigen::Vector4f line_pt (x[0], x[1], x[2], 0);
318 Eigen::Vector4f line_dir (x[3], x[4], x[5], 0);
319
320 for (int i = 0; i < values (); ++i)
321 {
322 // dist = f - r
323 Eigen::Vector4f pt = (*model_->input_)[indices_[i]].getVector4fMap();
324 pt[3] = 0;
325
326 fvec[i] = static_cast<float> (pcl::sqrPointToLineDistance (pt, line_pt, line_dir) - x[6]*x[6]);
327 }
328 return (0);
329 }
330
332 const Indices &indices_;
333 };
334 };
335}
336
337#ifdef PCL_NO_PRECOMPILE
338#include <pcl/sample_consensus/impl/sac_model_cylinder.hpp>
339#endif
PointCloud represents the base class in PCL for storing collections of 3D points.
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.
shared_ptr< const SampleConsensusModelCylinder< PointT, PointNT > > ConstPtr
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.
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.
~SampleConsensusModelCylinder()
Empty destructor.
void setEpsAngle(const double ea)
Set the angle epsilon (delta) threshold.
SampleConsensusModelCylinder & operator=(const SampleConsensusModelCylinder &source)
Copy constructor.
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
typename SampleConsensusModel< PointT >::PointCloud PointCloud
void setAxis(const Eigen::Vector3f &ax)
Set the axis along which we need to search for a cylinder direction.
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)
typename SampleConsensusModel< PointT >::PointCloudConstPtr PointCloudConstPtr
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...
typename SampleConsensusModel< PointT >::PointCloudPtr PointCloudPtr
shared_ptr< SampleConsensusModelCylinder< PointT, PointNT > > Ptr
SampleConsensusModelFromNormals represents the base model class for models that require the use of su...
Definition sac_model.h:612
PointCloudNConstPtr normals_
A pointer to the input dataset that contains the point normals of the XYZ dataset.
Definition sac_model.h:670
double normal_distance_weight_
The relative weight (between 0 and 1) to give to the angular distance (0 to pi/2) between point norma...
Definition sac_model.h:665
SampleConsensusModel represents the base model class.
Definition sac_model.h:70
double radius_min_
The minimum and maximum radius limits for the model.
Definition sac_model.h:564
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition sac_model.h:588
typename PointCloud::ConstPtr PointCloudConstPtr
Definition sac_model.h:73
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition sac_model.h:556
PointCloudConstPtr input_
A boost shared pointer to the point cloud data array.
Definition sac_model.h:553
std::string model_name_
The model name.
Definition sac_model.h:550
unsigned int model_size_
The number of coefficients in the model.
Definition sac_model.h:591
typename PointCloud::Ptr PointCloudPtr
Definition sac_model.h:74
std::vector< double > error_sqr_dists_
A vector holding the distances to the computed model.
Definition sac_model.h:585
Define standard C methods to do distance calculations.
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:75
Definition bfgs.h:10
@ SACMODEL_CYLINDER
Definition model_types.h:52
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133
Base functor all the models that need non linear optimization must define their own one and implement...
Definition sac_model.h:679
int values() const
Get the number of values.
Definition sac_model.h:703
A point structure representing Euclidean xyz coordinates, and the RGB color.