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