Point Cloud Library (PCL)  1.9.1
sac_model_circle3d.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef PCL_SAMPLE_CONSENSUS_MODEL_CIRCLE3D_H_
39 #define PCL_SAMPLE_CONSENSUS_MODEL_CIRCLE3D_H_
40 
41 #include <pcl/sample_consensus/sac_model.h>
42 #include <pcl/sample_consensus/model_types.h>
43 
44 namespace pcl
45 {
46  /** \brief SampleConsensusModelCircle3D defines a model for 3D circle segmentation.
47  *
48  * The model coefficients are defined as:
49  * - \b center.x : the X coordinate of the circle's center
50  * - \b center.y : the Y coordinate of the circle's center
51  * - \b center.z : the Z coordinate of the circle's center
52  * - \b radius : the circle's radius
53  * - \b normal.x : the X coordinate of the normal's direction
54  * - \b normal.y : the Y coordinate of the normal's direction
55  * - \b normal.z : the Z coordinate of the normal's direction
56  *
57  * \author Raoul Hoffmann, Karol Hausman, Radu B. Rusu
58  * \ingroup sample_consensus
59  */
60  template <typename PointT>
62  {
63  public:
69 
73 
74  typedef boost::shared_ptr<SampleConsensusModelCircle3D<PointT> > Ptr;
75  typedef boost::shared_ptr<const SampleConsensusModelCircle3D<PointT> > ConstPtr;
76 
77  /** \brief Constructor for base SampleConsensusModelCircle3D.
78  * \param[in] cloud the input point cloud dataset
79  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
80  */
82  bool random = false)
83  : SampleConsensusModel<PointT> (cloud, random)
84  {
85  model_name_ = "SampleConsensusModelCircle3D";
86  sample_size_ = 3;
87  model_size_ = 7;
88  }
89 
90  /** \brief Constructor for base SampleConsensusModelCircle3D.
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  */
96  const std::vector<int> &indices,
97  bool random = false)
98  : SampleConsensusModel<PointT> (cloud, indices, random)
99  {
100  model_name_ = "SampleConsensusModelCircle3D";
101  sample_size_ = 3;
102  model_size_ = 7;
103  }
104 
105  /** \brief Empty destructor */
107 
108  /** \brief Copy constructor.
109  * \param[in] source the model to copy into this
110  */
113  {
114  *this = source;
115  model_name_ = "SampleConsensusModelCircle3D";
116  }
117 
118  /** \brief Copy constructor.
119  * \param[in] source the model to copy into this
120  */
123  {
125  return (*this);
126  }
127 
128  /** \brief Check whether the given index samples can form a valid 2D circle model, compute the model coefficients
129  * from these samples and store them in model_coefficients. The circle coefficients are: x, y, R.
130  * \param[in] samples the point indices found as possible good candidates for creating a valid model
131  * \param[out] model_coefficients the resultant model coefficients
132  */
133  bool
134  computeModelCoefficients (const std::vector<int> &samples,
135  Eigen::VectorXf &model_coefficients) const;
136 
137  /** \brief Compute all distances from the cloud data to a given 3D circle model.
138  * \param[in] model_coefficients the coefficients of a 2D circle model that we need to compute distances to
139  * \param[out] distances the resultant estimated distances
140  */
141  void
142  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
143  std::vector<double> &distances) const;
144 
145  /** \brief Compute all distances from the cloud data to a given 3D circle model.
146  * \param[in] model_coefficients the coefficients of a 3D circle model that we need to compute distances to
147  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
148  * \param[out] inliers the resultant model inliers
149  */
150  void
151  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
152  const double threshold,
153  std::vector<int> &inliers);
154 
155  /** \brief Count all the points which respect the given model coefficients as inliers.
156  *
157  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
158  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
159  * \return the resultant number of inliers
160  */
161  virtual int
162  countWithinDistance (const Eigen::VectorXf &model_coefficients,
163  const double threshold) const;
164 
165  /** \brief Recompute the 3d circle coefficients using the given inlier set and return them to the user.
166  * @note: these are the coefficients of the 3d circle model after refinement (e.g. after SVD)
167  * \param[in] inliers the data inliers found as supporting the model
168  * \param[in] model_coefficients the initial guess for the optimization
169  * \param[out] optimized_coefficients the resultant recomputed coefficients after non-linear optimization
170  */
171  void
172  optimizeModelCoefficients (const std::vector<int> &inliers,
173  const Eigen::VectorXf &model_coefficients,
174  Eigen::VectorXf &optimized_coefficients) const;
175 
176  /** \brief Create a new point cloud with inliers projected onto the 3d circle model.
177  * \param[in] inliers the data inliers that we want to project on the 3d circle model
178  * \param[in] model_coefficients the coefficients of a 3d circle model
179  * \param[out] projected_points the resultant projected points
180  * \param[in] copy_data_fields set to true if we need to copy the other data fields
181  */
182  void
183  projectPoints (const std::vector<int> &inliers,
184  const Eigen::VectorXf &model_coefficients,
185  PointCloud &projected_points,
186  bool copy_data_fields = true) const;
187 
188  /** \brief Verify whether a subset of indices verifies the given 3d circle model coefficients.
189  * \param[in] indices the data indices that need to be tested against the 3d circle model
190  * \param[in] model_coefficients the 3d circle model coefficients
191  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
192  */
193  bool
194  doSamplesVerifyModel (const std::set<int> &indices,
195  const Eigen::VectorXf &model_coefficients,
196  const double threshold) const;
197 
198  /** \brief Return an unique id for this model (SACMODEL_CIRCLE3D). */
199  inline pcl::SacModel
200  getModelType () const { return (SACMODEL_CIRCLE3D); }
201 
202  protected:
205 
206  /** \brief Check whether a model is valid given the user constraints.
207  * \param[in] model_coefficients the set of model coefficients
208  */
209  virtual bool
210  isModelValid (const Eigen::VectorXf &model_coefficients) const;
211 
212  /** \brief Check if a sample of indices results in a good sample of points indices.
213  * \param[in] samples the resultant index samples
214  */
215  bool
216  isSampleGood(const std::vector<int> &samples) const;
217 
218  private:
219  /** \brief Functor for the optimization function */
220  struct OptimizationFunctor : pcl::Functor<double>
221  {
222  /** Functor constructor
223  * \param[in] indices the indices of data points to evaluate
224  * \param[in] estimator pointer to the estimator object
225  */
226  OptimizationFunctor (const pcl::SampleConsensusModelCircle3D<PointT> *model, const std::vector<int>& indices) :
227  pcl::Functor<double> (indices.size ()), model_ (model), indices_ (indices) {}
228 
229  /** Cost function to be minimized
230  * \param[in] x the variables array
231  * \param[out] fvec the resultant functions evaluations
232  * \return 0
233  */
234  int operator() (const Eigen::VectorXd &x, Eigen::VectorXd &fvec) const
235  {
236  for (int i = 0; i < values (); ++i)
237  {
238  // what i have:
239  // P : Sample Point
240  Eigen::Vector3d P (model_->input_->points[indices_[i]].x, model_->input_->points[indices_[i]].y, model_->input_->points[indices_[i]].z);
241  // C : Circle Center
242  Eigen::Vector3d C (x[0], x[1], x[2]);
243  // N : Circle (Plane) Normal
244  Eigen::Vector3d N (x[4], x[5], x[6]);
245  // r : Radius
246  double r = x[3];
247 
248  Eigen::Vector3d helperVectorPC = P - C;
249  // 1.1. get line parameter
250  //float lambda = (helperVectorPC.dot(N)) / N.squaredNorm() ;
251  double lambda = (-(helperVectorPC.dot (N))) / N.dot (N);
252  // Projected Point on plane
253  Eigen::Vector3d P_proj = P + lambda * N;
254  Eigen::Vector3d helperVectorP_projC = P_proj - C;
255 
256  // K : Point on Circle
257  Eigen::Vector3d K = C + r * helperVectorP_projC.normalized ();
258  Eigen::Vector3d distanceVector = P - K;
259 
260  fvec[i] = distanceVector.norm ();
261  }
262  return (0);
263  }
264 
266  const std::vector<int> &indices_;
267  };
268  };
269 }
270 
271 #ifdef PCL_NO_PRECOMPILE
272 #include <pcl/sample_consensus/impl/sac_model_circle3d.hpp>
273 #endif
274 
275 #endif //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_CIRCLE3D_H_
pcl::SampleConsensusModel::indices_
boost::shared_ptr< std::vector< int > > indices_
A pointer to the vector of point indices to use.
Definition: sac_model.h:540
pcl
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
pcl::K
@ K
Definition: norms.h:55
pcl::SampleConsensusModel::PointCloudPtr
pcl::PointCloud< PointT >::Ptr PointCloudPtr
Definition: sac_model.h:71
pcl::SACMODEL_CIRCLE3D
@ SACMODEL_CIRCLE3D
Definition: model_types.h:51
pcl::SampleConsensusModelCircle3D::doSamplesVerifyModel
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 3d circle model coefficients.
Definition: sac_model_circle3d.hpp:393
pcl::SampleConsensusModelCircle3D::isModelValid
virtual bool isModelValid(const Eigen::VectorXf &model_coefficients) const
Check whether a model is valid given the user constraints.
Definition: sac_model_circle3d.hpp:438
pcl::SampleConsensusModel::sample_size_
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition: sac_model.h:572
pcl::SampleConsensusModel::model_size_
unsigned int model_size_
The number of coefficients in the model.
Definition: sac_model.h:575
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: projection_matrix.h:53
pcl::SampleConsensusModelCircle3D::projectPoints
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 3d circle model.
Definition: sac_model_circle3d.hpp:291
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:619
pcl::SampleConsensusModelCircle3D::Ptr
boost::shared_ptr< SampleConsensusModelCircle3D< PointT > > Ptr
Definition: sac_model_circle3d.h:74
pcl::SampleConsensusModel::PointCloudConstPtr
pcl::PointCloud< PointT >::ConstPtr PointCloudConstPtr
Definition: sac_model.h:70
pcl::SampleConsensusModelCircle3D::PointCloudConstPtr
SampleConsensusModel< PointT >::PointCloudConstPtr PointCloudConstPtr
Definition: sac_model_circle3d.h:72
pcl::SampleConsensusModelCircle3D::computeModelCoefficients
bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients) const
Check whether the given index samples can form a valid 2D circle model, compute the model coefficient...
Definition: sac_model_circle3d.hpp:68
pcl::Functor
Base functor all the models that need non linear optimization must define their own one and implement...
Definition: sac_model.h:653
pcl::SacModel
SacModel
Definition: model_types.h:46
pcl::SampleConsensusModelCircle3D::getModelType
pcl::SacModel getModelType() const
Return an unique id for this model (SACMODEL_CIRCLE3D).
Definition: sac_model_circle3d.h:200
pcl::SampleConsensusModelCircle3D
SampleConsensusModelCircle3D defines a model for 3D circle segmentation.
Definition: sac_model_circle3d.h:61
pcl::SampleConsensusModelCircle3D::ConstPtr
boost::shared_ptr< const SampleConsensusModelCircle3D< PointT > > ConstPtr
Definition: sac_model_circle3d.h:75
pcl::SampleConsensusModelCircle3D::countWithinDistance
virtual int countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const
Count all the points which respect the given model coefficients as inliers.
Definition: sac_model_circle3d.hpp:214
pcl::SampleConsensusModel::model_name_
std::string model_name_
The model name.
Definition: sac_model.h:534
pcl::SampleConsensusModelCircle3D::SampleConsensusModelCircle3D
SampleConsensusModelCircle3D(const SampleConsensusModelCircle3D &source)
Copy constructor.
Definition: sac_model_circle3d.h:111
pcl::SampleConsensusModelCircle3D::SampleConsensusModelCircle3D
SampleConsensusModelCircle3D(const PointCloudConstPtr &cloud, const std::vector< int > &indices, bool random=false)
Constructor for base SampleConsensusModelCircle3D.
Definition: sac_model_circle3d.h:95
pcl::SampleConsensusModelCircle3D::~SampleConsensusModelCircle3D
virtual ~SampleConsensusModelCircle3D()
Empty destructor.
Definition: sac_model_circle3d.h:106
pcl::SampleConsensusModelCircle3D::operator=
SampleConsensusModelCircle3D & operator=(const SampleConsensusModelCircle3D &source)
Copy constructor.
Definition: sac_model_circle3d.h:122
pcl::SampleConsensusModelCircle3D::SampleConsensusModelCircle3D
SampleConsensusModelCircle3D(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModelCircle3D.
Definition: sac_model_circle3d.h:81
pcl::SampleConsensusModelCircle3D::optimizeModelCoefficients
void optimizeModelCoefficients(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const
Recompute the 3d circle coefficients using the given inlier set and return them to the user.
Definition: sac_model_circle3d.hpp:255
pcl::SampleConsensusModelCircle3D::PointCloud
SampleConsensusModel< PointT >::PointCloud PointCloud
Definition: sac_model_circle3d.h:70
pcl::SampleConsensusModelCircle3D::isSampleGood
bool isSampleGood(const std::vector< int > &samples) const
Check if a sample of indices results in a good sample of points indices.
Definition: sac_model_circle3d.hpp:48
pcl::SampleConsensusModel
SampleConsensusModel represents the base model class.
Definition: sac_model.h:66
pcl::SampleConsensusModelCircle3D::PointCloudPtr
SampleConsensusModel< PointT >::PointCloudPtr PointCloudPtr
Definition: sac_model_circle3d.h:71
pcl::SampleConsensusModelCircle3D::getDistancesToModel
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const
Compute all distances from the cloud data to a given 3D circle model.
Definition: sac_model_circle3d.hpp:118
pcl::SampleConsensusModelCircle3D::selectWithinDistance
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, std::vector< int > &inliers)
Compute all distances from the cloud data to a given 3D circle model.
Definition: sac_model_circle3d.hpp:165