Point Cloud Library (PCL)  1.9.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
sac_model_line.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 #ifndef PCL_SAMPLE_CONSENSUS_MODEL_LINE_H_
42 #define PCL_SAMPLE_CONSENSUS_MODEL_LINE_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 SampleConsensusModelLine defines a model for 3D line segmentation.
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  *
59  * \author Radu B. Rusu
60  * \ingroup sample_consensus
61  */
62  template <typename PointT>
64  {
65  public:
71 
75 
76  typedef boost::shared_ptr<SampleConsensusModelLine> Ptr;
77 
78  /** \brief Constructor for base SampleConsensusModelLine.
79  * \param[in] cloud the input point cloud dataset
80  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
81  */
82  SampleConsensusModelLine (const PointCloudConstPtr &cloud, bool random = false)
83  : SampleConsensusModel<PointT> (cloud, random)
84  {
85  model_name_ = "SampleConsensusModelLine";
86  sample_size_ = 2;
87  model_size_ = 6;
88  }
89 
90  /** \brief Constructor for base SampleConsensusModelLine.
91  * \param[in] cloud the input point cloud dataset
92  * \param[in] indices a vector of point indices to be used from \a cloud
93  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
94  */
95  SampleConsensusModelLine (const PointCloudConstPtr &cloud,
96  const std::vector<int> &indices,
97  bool random = false)
98  : SampleConsensusModel<PointT> (cloud, indices, random)
99  {
100  model_name_ = "SampleConsensusModelLine";
101  sample_size_ = 2;
102  model_size_ = 6;
103  }
104 
105  /** \brief Empty destructor */
107 
108  /** \brief Check whether the given index samples can form a valid line model, compute the model coefficients from
109  * these samples and store them internally in model_coefficients_. The line coefficients are represented by a
110  * point and a line direction
111  * \param[in] samples the point indices found as possible good candidates for creating a valid model
112  * \param[out] model_coefficients the resultant model coefficients
113  */
114  bool
115  computeModelCoefficients (const std::vector<int> &samples,
116  Eigen::VectorXf &model_coefficients) const;
117 
118  /** \brief Compute all squared distances from the cloud data to a given line model.
119  * \param[in] model_coefficients the coefficients of a line model that we need to compute distances to
120  * \param[out] distances the resultant estimated squared distances
121  */
122  void
123  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
124  std::vector<double> &distances) const;
125 
126  /** \brief Select all the points which respect the given model coefficients as inliers.
127  * \param[in] model_coefficients the coefficients of a line model that we need to compute distances to
128  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
129  * \param[out] inliers the resultant model inliers
130  */
131  void
132  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
133  const double threshold,
134  std::vector<int> &inliers);
135 
136  /** \brief Count all the points which respect the given model coefficients as inliers.
137  *
138  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
139  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
140  * \return the resultant number of inliers
141  */
142  virtual int
143  countWithinDistance (const Eigen::VectorXf &model_coefficients,
144  const double threshold) const;
145 
146  /** \brief Recompute the line coefficients using the given inlier set and return them to the user.
147  * @note: these are the coefficients of the line model after refinement (e.g. after SVD)
148  * \param[in] inliers the data inliers found as supporting the model
149  * \param[in] model_coefficients the initial guess for the model coefficients
150  * \param[out] optimized_coefficients the resultant recomputed coefficients after optimization
151  */
152  void
153  optimizeModelCoefficients (const std::vector<int> &inliers,
154  const Eigen::VectorXf &model_coefficients,
155  Eigen::VectorXf &optimized_coefficients) const;
156 
157  /** \brief Create a new point cloud with inliers projected onto the line model.
158  * \param[in] inliers the data inliers that we want to project on the line model
159  * \param[in] model_coefficients the *normalized* coefficients of a line model
160  * \param[out] projected_points the resultant projected points
161  * \param[in] copy_data_fields set to true if we need to copy the other data fields
162  */
163  void
164  projectPoints (const std::vector<int> &inliers,
165  const Eigen::VectorXf &model_coefficients,
166  PointCloud &projected_points,
167  bool copy_data_fields = true) const;
168 
169  /** \brief Verify whether a subset of indices verifies the given line model coefficients.
170  * \param[in] indices the data indices that need to be tested against the line model
171  * \param[in] model_coefficients the line model coefficients
172  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
173  */
174  bool
175  doSamplesVerifyModel (const std::set<int> &indices,
176  const Eigen::VectorXf &model_coefficients,
177  const double threshold) const;
178 
179  /** \brief Return an unique id for this model (SACMODEL_LINE). */
180  inline pcl::SacModel
181  getModelType () const { return (SACMODEL_LINE); }
182 
183  protected:
186 
187  /** \brief Check if a sample of indices results in a good sample of points
188  * indices.
189  * \param[in] samples the resultant index samples
190  */
191  bool
192  isSampleGood (const std::vector<int> &samples) const;
193  };
194 }
195 
196 #ifdef PCL_NO_PRECOMPILE
197 #include <pcl/sample_consensus/impl/sac_model_line.hpp>
198 #endif
199 
200 #endif //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_LINE_H_
SampleConsensusModel< PointT >::PointCloud PointCloud
SampleConsensusModelLine defines a model for 3D line segmentation.
SampleConsensusModel< PointT >::PointCloudPtr PointCloudPtr
SampleConsensusModelLine(const PointCloudConstPtr &cloud, const std::vector< int > &indices, bool random=false)
Constructor for base SampleConsensusModelLine.
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:575
SampleConsensusModel< PointT >::PointCloudConstPtr PointCloudConstPtr
SampleConsensusModelLine(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModelLine.
SampleConsensusModel represents the base model class.
Definition: sac_model.h:66
bool isSampleGood(const std::vector< int > &samples) const
Check if a sample of indices results in a good sample of points indices.
std::string model_name_
The model name.
Definition: sac_model.h:534
pcl::PointCloud< PointT >::Ptr PointCloudPtr
Definition: sac_model.h:71
boost::shared_ptr< SampleConsensusModelLine > Ptr
bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients) const
Check whether the given index samples can form a valid line model, compute the model coefficients fro...
pcl::SacModel getModelType() const
Return an unique id for this model (SACMODEL_LINE).
bool doSamplesVerifyModel(const std::set< int > &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const
Verify whether a subset of indices verifies the given line model coefficients.
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const
Compute all squared distances from the cloud data to a given line model.
SacModel
Definition: model_types.h:46
pcl::PointCloud< PointT >::ConstPtr PointCloudConstPtr
Definition: sac_model.h:70
void optimizeModelCoefficients(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const
Recompute the line coefficients using the given inlier set and return them to the user...
PointCloud represents the base class in PCL for storing collections of 3D points. ...
void projectPoints(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true) const
Create a new point cloud with inliers projected onto the line model.
virtual int countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const
Count all the points which respect the given model coefficients as inliers.
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.
A point structure representing Euclidean xyz coordinates, and the RGB color.
virtual ~SampleConsensusModelLine()
Empty destructor.
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition: sac_model.h:572