Point Cloud Library (PCL)  1.8.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  */
112  SampleConsensusModel<PointT> (), tmp_inliers_ ()
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  tmp_inliers_ = source.tmp_inliers_;
126  return (*this);
127  }
128 
129  /** \brief Check whether the given index samples can form a valid 2D circle model, compute the model coefficients
130  * from these samples and store them in model_coefficients. The circle coefficients are: x, y, R.
131  * \param[in] samples the point indices found as possible good candidates for creating a valid model
132  * \param[out] model_coefficients the resultant model coefficients
133  */
134  bool
135  computeModelCoefficients (const std::vector<int> &samples,
136  Eigen::VectorXf &model_coefficients);
137 
138  /** \brief Compute all distances from the cloud data to a given 3D circle model.
139  * \param[in] model_coefficients the coefficients of a 2D circle model that we need to compute distances to
140  * \param[out] distances the resultant estimated distances
141  */
142  void
143  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
144  std::vector<double> &distances);
145 
146  /** \brief Compute all distances from the cloud data to a given 3D circle model.
147  * \param[in] model_coefficients the coefficients of a 3D circle model that we need to compute distances to
148  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
149  * \param[out] inliers the resultant model inliers
150  */
151  void
152  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
153  const double threshold,
154  std::vector<int> &inliers);
155 
156  /** \brief Count all the points which respect the given model coefficients as inliers.
157  *
158  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
159  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
160  * \return the resultant number of inliers
161  */
162  virtual int
163  countWithinDistance (const Eigen::VectorXf &model_coefficients,
164  const double threshold);
165 
166  /** \brief Recompute the 3d circle coefficients using the given inlier set and return them to the user.
167  * @note: these are the coefficients of the 3d circle model after refinement (e.g. after SVD)
168  * \param[in] inliers the data inliers found as supporting the model
169  * \param[in] model_coefficients the initial guess for the optimization
170  * \param[out] optimized_coefficients the resultant recomputed coefficients after non-linear optimization
171  */
172  void
173  optimizeModelCoefficients (const std::vector<int> &inliers,
174  const Eigen::VectorXf &model_coefficients,
175  Eigen::VectorXf &optimized_coefficients);
176 
177  /** \brief Create a new point cloud with inliers projected onto the 3d circle model.
178  * \param[in] inliers the data inliers that we want to project on the 3d circle model
179  * \param[in] model_coefficients the coefficients of a 3d circle model
180  * \param[out] projected_points the resultant projected points
181  * \param[in] copy_data_fields set to true if we need to copy the other data fields
182  */
183  void
184  projectPoints (const std::vector<int> &inliers,
185  const Eigen::VectorXf &model_coefficients,
186  PointCloud &projected_points,
187  bool copy_data_fields = true);
188 
189  /** \brief Verify whether a subset of indices verifies the given 3d circle model coefficients.
190  * \param[in] indices the data indices that need to be tested against the 3d circle model
191  * \param[in] model_coefficients the 3d circle model coefficients
192  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
193  */
194  bool
195  doSamplesVerifyModel (const std::set<int> &indices,
196  const Eigen::VectorXf &model_coefficients,
197  const double threshold);
198 
199  /** \brief Return an unique id for this model (SACMODEL_CIRCLE3D). */
200  inline pcl::SacModel
201  getModelType () const { return (SACMODEL_CIRCLE3D); }
202 
203  protected:
206 
207  /** \brief Check whether a model is valid given the user constraints.
208  * \param[in] model_coefficients the set of model coefficients
209  */
210  virtual bool
211  isModelValid (const Eigen::VectorXf &model_coefficients);
212 
213  /** \brief Check if a sample of indices results in a good sample of points indices.
214  * \param[in] samples the resultant index samples
215  */
216  bool
217  isSampleGood(const std::vector<int> &samples) const;
218 
219  private:
220  /** \brief Temporary pointer to a list of given indices for optimizeModelCoefficients () */
221  const std::vector<int> *tmp_inliers_;
222 
223  /** \brief Functor for the optimization function */
224  struct OptimizationFunctor : pcl::Functor<double>
225  {
226  /** Functor constructor
227  * \param[in] m_data_points the number of functions
228  * \param[in] estimator pointer to the estimator object
229  * \param[in] distance distance computation function pointer
230  */
231  OptimizationFunctor (int m_data_points, pcl::SampleConsensusModelCircle3D<PointT> *model) :
232  pcl::Functor<double> (m_data_points), model_ (model) {}
233 
234  /** Cost function to be minimized
235  * \param[in] x the variables array
236  * \param[out] fvec the resultant functions evaluations
237  * \return 0
238  */
239  int operator() (const Eigen::VectorXd &x, Eigen::VectorXd &fvec) const
240  {
241  for (int i = 0; i < values (); ++i)
242  {
243  // what i have:
244  // P : Sample Point
245  Eigen::Vector3d P (model_->input_->points[(*model_->tmp_inliers_)[i]].x, model_->input_->points[(*model_->tmp_inliers_)[i]].y, model_->input_->points[(*model_->tmp_inliers_)[i]].z);
246  // C : Circle Center
247  Eigen::Vector3d C (x[0], x[1], x[2]);
248  // N : Circle (Plane) Normal
249  Eigen::Vector3d N (x[4], x[5], x[6]);
250  // r : Radius
251  double r = x[3];
252 
253  Eigen::Vector3d helperVectorPC = P - C;
254  // 1.1. get line parameter
255  //float lambda = (helperVectorPC.dot(N)) / N.squaredNorm() ;
256  double lambda = (-(helperVectorPC.dot (N))) / N.dot (N);
257  // Projected Point on plane
258  Eigen::Vector3d P_proj = P + lambda * N;
259  Eigen::Vector3d helperVectorP_projC = P_proj - C;
260 
261  // K : Point on Circle
262  Eigen::Vector3d K = C + r * helperVectorP_projC.normalized ();
263  Eigen::Vector3d distanceVector = P - K;
264 
265  fvec[i] = distanceVector.norm ();
266  }
267  return (0);
268  }
269 
271  };
272  };
273 }
274 
275 #ifdef PCL_NO_PRECOMPILE
276 #include <pcl/sample_consensus/impl/sac_model_circle3d.hpp>
277 #endif
278 
279 #endif //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_CIRCLE3D_H_
void optimizeModelCoefficients(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
Recompute the 3d circle coefficients using the given inlier set and return them to the user...
boost::shared_ptr< SampleConsensusModelCircle3D< PointT > > Ptr
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.
SampleConsensusModelCircle3D(const PointCloudConstPtr &cloud, const std::vector< int > &indices, bool random=false)
Constructor for base SampleConsensusModelCircle3D.
unsigned int model_size_
The number of coefficients in the model.
Definition: sac_model.h:575
Base functor all the models that need non linear optimization must define their own one and implement...
Definition: sac_model.h:653
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances)
Compute all distances from the cloud data to a given 3D circle model.
SampleConsensusModel< PointT >::PointCloud PointCloud
SampleConsensusModelCircle3D defines a model for 3D circle segmentation.
virtual int countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold)
Count all the points which respect the given model coefficients as inliers.
SampleConsensusModel represents the base model class.
Definition: sac_model.h:66
pcl::SacModel getModelType() const
Return an unique id for this model (SACMODEL_CIRCLE3D).
std::string model_name_
The model name.
Definition: sac_model.h:534
pcl::PointCloud< PointT >::Ptr PointCloudPtr
Definition: sac_model.h:71
bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients)
Check whether the given index samples can form a valid 2D circle model, compute the model coefficient...
SampleConsensusModelCircle3D(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModelCircle3D.
bool doSamplesVerifyModel(const std::set< int > &indices, const Eigen::VectorXf &model_coefficients, const double threshold)
Verify whether a subset of indices verifies the given 3d circle model coefficients.
SampleConsensusModel< PointT >::PointCloudConstPtr PointCloudConstPtr
boost::shared_ptr< const SampleConsensusModelCircle3D< PointT > > ConstPtr
SampleConsensusModelCircle3D(const SampleConsensusModelCircle3D &source)
Copy constructor.
PointCloud represents the base class in PCL for storing collections of 3D points. ...
SacModel
Definition: model_types.h:48
virtual ~SampleConsensusModelCircle3D()
Empty destructor.
pcl::PointCloud< PointT >::ConstPtr PointCloudConstPtr
Definition: sac_model.h:70
virtual bool isModelValid(const Eigen::VectorXf &model_coefficients)
Check whether a model is valid given the user constraints.
Definition: norms.h:55
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.
SampleConsensusModel< PointT >::PointCloudPtr PointCloudPtr
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 3d circle model.
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition: sac_model.h:572
SampleConsensusModelCircle3D & operator=(const SampleConsensusModelCircle3D &source)
Copy constructor.