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