Point Cloud Library (PCL)  1.10.0-dev
sac_model_stick.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: sac_model_line.h 2326 2011-08-31 07:48:25Z rusu $
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/eigen.h>
46 
47 namespace pcl
48 {
49  /** \brief SampleConsensusModelStick defines a model for 3D stick segmentation.
50  * A stick is a line with a user given minimum/maximum width.
51  * The model coefficients are defined as:
52  * - \b point_on_line.x : the X coordinate of a point on the line
53  * - \b point_on_line.y : the Y coordinate of a point on the line
54  * - \b point_on_line.z : the Z coordinate of a point on the line
55  * - \b line_direction.x : the X coordinate of a line's direction
56  * - \b line_direction.y : the Y coordinate of a line's direction
57  * - \b line_direction.z : the Z coordinate of a line's direction
58  * - \b line_width : the width of the line
59  *
60  * \author Radu B. Rusu
61  * \ingroup sample_consensus
62  */
63  template <typename PointT>
65  {
66  public:
74 
78 
81 
82  /** \brief Constructor for base SampleConsensusModelStick.
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  */
87  bool random = false)
88  : SampleConsensusModel<PointT> (cloud, random)
89  {
90  model_name_ = "SampleConsensusModelStick";
91  sample_size_ = 2;
92  model_size_ = 7;
93  }
94 
95  /** \brief Constructor for base SampleConsensusModelStick.
96  * \param[in] cloud the input point cloud dataset
97  * \param[in] indices a vector of point indices to be used from \a cloud
98  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
99  */
101  const std::vector<int> &indices,
102  bool random = false)
103  : SampleConsensusModel<PointT> (cloud, indices, random)
104  {
105  model_name_ = "SampleConsensusModelStick";
106  sample_size_ = 2;
107  model_size_ = 7;
108  }
109 
110  /** \brief Empty destructor */
112 
113  /** \brief Check whether the given index samples can form a valid stick model, compute the model coefficients from
114  * these samples and store them internally in model_coefficients_. The stick coefficients are represented by a
115  * point and a line direction
116  * \param[in] samples the point indices found as possible good candidates for creating a valid model
117  * \param[out] model_coefficients the resultant model coefficients
118  */
119  bool
120  computeModelCoefficients (const std::vector<int> &samples,
121  Eigen::VectorXf &model_coefficients) const override;
122 
123  /** \brief Compute all squared distances from the cloud data to a given stick model.
124  * \param[in] model_coefficients the coefficients of a stick model that we need to compute distances to
125  * \param[out] distances the resultant estimated squared distances
126  */
127  void
128  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
129  std::vector<double> &distances) const override;
130 
131  /** \brief Select all the points which respect the given model coefficients as inliers.
132  * \param[in] model_coefficients the coefficients of a stick model that we need to compute distances to
133  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
134  * \param[out] inliers the resultant model inliers
135  */
136  void
137  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
138  const double threshold,
139  std::vector<int> &inliers) override;
140 
141  /** \brief Count all the points which respect the given model coefficients as inliers.
142  *
143  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
144  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
145  * \return the resultant number of inliers
146  */
147  std::size_t
148  countWithinDistance (const Eigen::VectorXf &model_coefficients,
149  const double threshold) const override;
150 
151  /** \brief Recompute the stick coefficients using the given inlier set and return them to the user.
152  * @note: these are the coefficients of the stick model after refinement (e.g. after SVD)
153  * \param[in] inliers the data inliers found as supporting the model
154  * \param[in] model_coefficients the initial guess for the model coefficients
155  * \param[out] optimized_coefficients the resultant recomputed coefficients after optimization
156  */
157  void
158  optimizeModelCoefficients (const std::vector<int> &inliers,
159  const Eigen::VectorXf &model_coefficients,
160  Eigen::VectorXf &optimized_coefficients) const override;
161 
162  /** \brief Create a new point cloud with inliers projected onto the stick model.
163  * \param[in] inliers the data inliers that we want to project on the stick model
164  * \param[in] model_coefficients the *normalized* coefficients of a stick model
165  * \param[out] projected_points the resultant projected points
166  * \param[in] copy_data_fields set to true if we need to copy the other data fields
167  */
168  void
169  projectPoints (const std::vector<int> &inliers,
170  const Eigen::VectorXf &model_coefficients,
171  PointCloud &projected_points,
172  bool copy_data_fields = true) const override;
173 
174  /** \brief Verify whether a subset of indices verifies the given stick model coefficients.
175  * \param[in] indices the data indices that need to be tested against the plane model
176  * \param[in] model_coefficients the plane model coefficients
177  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
178  */
179  bool
180  doSamplesVerifyModel (const std::set<int> &indices,
181  const Eigen::VectorXf &model_coefficients,
182  const double threshold) const override;
183 
184  /** \brief Return a unique id for this model (SACMODEL_STICK). */
185  inline pcl::SacModel
186  getModelType () const override { return (SACMODEL_STICK); }
187 
188  protected:
191 
192  /** \brief Check if a sample of indices results in a good sample of points
193  * indices.
194  * \param[in] samples the resultant index samples
195  */
196  bool
197  isSampleGood (const std::vector<int> &samples) const override;
198  };
199 }
200 
201 #ifdef PCL_NO_PRECOMPILE
202 #include <pcl/sample_consensus/impl/sac_model_stick.hpp>
203 #endif
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all squared distances from the cloud data to a given stick model.
pcl::SacModel getModelType() const override
Return a unique id for this model (SACMODEL_STICK).
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, std::vector< int > &inliers) override
Select all the points which respect the given model coefficients as inliers.
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
unsigned int model_size_
The number of coefficients in the model.
Definition: sac_model.h:564
typename PointCloud::Ptr PointCloudPtr
Definition: sac_model.h:73
void projectPoints(const std::vector< int > &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 stick model.
bool isSampleGood(const std::vector< int > &samples) const override
Check if a sample of indices results in a good sample of points indices.
SampleConsensusModel represents the base model class.
Definition: sac_model.h:68
std::string model_name_
The model name.
Definition: sac_model.h:523
bool doSamplesVerifyModel(const std::set< int > &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const override
Verify whether a subset of indices verifies the given stick model coefficients.
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: sac_model.h:72
SampleConsensusModelStick defines a model for 3D stick segmentation.
SampleConsensusModelStick(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModelStick.
bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients) const override
Check whether the given index samples can form a valid stick model, compute the model coefficients fr...
PointCloud represents the base class in PCL for storing collections of 3D points. ...
SacModel
Definition: model_types.h:45
shared_ptr< const SampleConsensusModel< PointT > > ConstPtr
Definition: sac_model.h:77
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.
~SampleConsensusModelStick()
Empty destructor.
void optimizeModelCoefficients(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the stick coefficients using the given inlier set and return them to the user...
A point structure representing Euclidean xyz coordinates, and the RGB color.
shared_ptr< SampleConsensusModel< PointT > > Ptr
Definition: sac_model.h:76
boost::shared_ptr< T > shared_ptr
Alias for boost::shared_ptr.
Definition: pcl_macros.h:90
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition: sac_model.h:561
SampleConsensusModelStick(const PointCloudConstPtr &cloud, const std::vector< int > &indices, bool random=false)
Constructor for base SampleConsensusModelStick.