Point Cloud Library (PCL)  1.7.0
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:
68 
72 
73  typedef boost::shared_ptr<SampleConsensusModelCircle3D<PointT> > Ptr;
74  typedef boost::shared_ptr<const SampleConsensusModelCircle3D<PointT> > ConstPtr;
75 
76  /** \brief Constructor for base SampleConsensusModelCircle3D.
77  * \param[in] cloud the input point cloud dataset
78  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
79  */
81  bool random = false)
82  : SampleConsensusModel<PointT> (cloud, random) {};
83 
84  /** \brief Constructor for base SampleConsensusModelCircle3D.
85  * \param[in] cloud the input point cloud dataset
86  * \param[in] indices a vector of point indices to be used from \a cloud
87  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
88  */
90  const std::vector<int> &indices,
91  bool random = false)
92  : SampleConsensusModel<PointT> (cloud, indices, random) {};
93 
94  /** \brief Empty destructor */
96 
97  /** \brief Copy constructor.
98  * \param[in] source the model to copy into this
99  */
101  SampleConsensusModel<PointT> (), tmp_inliers_ ()
102  {
103  *this = source;
104  }
105 
106  /** \brief Copy constructor.
107  * \param[in] source the model to copy into this
108  */
111  {
113  tmp_inliers_ = source.tmp_inliers_;
114  return (*this);
115  }
116 
117  /** \brief Check whether the given index samples can form a valid 2D circle model, compute the model coefficients
118  * from these samples and store them in model_coefficients. The circle coefficients are: x, y, R.
119  * \param[in] samples the point indices found as possible good candidates for creating a valid model
120  * \param[out] model_coefficients the resultant model coefficients
121  */
122  bool
123  computeModelCoefficients (const std::vector<int> &samples,
124  Eigen::VectorXf &model_coefficients);
125 
126  /** \brief Compute all distances from the cloud data to a given 3D circle model.
127  * \param[in] model_coefficients the coefficients of a 2D circle model that we need to compute distances to
128  * \param[out] distances the resultant estimated distances
129  */
130  void
131  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
132  std::vector<double> &distances);
133 
134  /** \brief Compute all distances from the cloud data to a given 3D circle model.
135  * \param[in] model_coefficients the coefficients of a 3D circle model that we need to compute distances to
136  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
137  * \param[out] inliers the resultant model inliers
138  */
139  void
140  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
141  const double threshold,
142  std::vector<int> &inliers);
143 
144  /** \brief Count all the points which respect the given model coefficients as inliers.
145  *
146  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
147  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
148  * \return the resultant number of inliers
149  */
150  virtual int
151  countWithinDistance (const Eigen::VectorXf &model_coefficients,
152  const double threshold);
153 
154  /** \brief Recompute the 3d circle coefficients using the given inlier set and return them to the user.
155  * @note: these are the coefficients of the 3d circle model after refinement (eg. after SVD)
156  * \param[in] inliers the data inliers found as supporting the model
157  * \param[in] model_coefficients the initial guess for the optimization
158  * \param[out] optimized_coefficients the resultant recomputed coefficients after non-linear optimization
159  */
160  void
161  optimizeModelCoefficients (const std::vector<int> &inliers,
162  const Eigen::VectorXf &model_coefficients,
163  Eigen::VectorXf &optimized_coefficients);
164 
165  /** \brief Create a new point cloud with inliers projected onto the 3d circle model.
166  * \param[in] inliers the data inliers that we want to project on the 3d circle model
167  * \param[in] model_coefficients the coefficients of a 3d circle model
168  * \param[out] projected_points the resultant projected points
169  * \param[in] copy_data_fields set to true if we need to copy the other data fields
170  */
171  void
172  projectPoints (const std::vector<int> &inliers,
173  const Eigen::VectorXf &model_coefficients,
174  PointCloud &projected_points,
175  bool copy_data_fields = true);
176 
177  /** \brief Verify whether a subset of indices verifies the given 3d circle model coefficients.
178  * \param[in] indices the data indices that need to be tested against the 3d circle model
179  * \param[in] model_coefficients the 3d circle model coefficients
180  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
181  */
182  bool
183  doSamplesVerifyModel (const std::set<int> &indices,
184  const Eigen::VectorXf &model_coefficients,
185  const double threshold);
186 
187  /** \brief Return an unique id for this model (SACMODEL_CIRCLE3D). */
188  inline pcl::SacModel
189  getModelType () const { return (SACMODEL_CIRCLE3D); }
190 
191  protected:
192  /** \brief Check whether a model is valid given the user constraints.
193  * \param[in] model_coefficients the set of model coefficients
194  */
195  bool
196  isModelValid (const Eigen::VectorXf &model_coefficients);
197 
198  /** \brief Check if a sample of indices results in a good sample of points indices.
199  * \param[in] samples the resultant index samples
200  */
201  bool
202  isSampleGood(const std::vector<int> &samples) const;
203 
204  private:
205  /** \brief Temporary pointer to a list of given indices for optimizeModelCoefficients () */
206  const std::vector<int> *tmp_inliers_;
207 
208  /** \brief Functor for the optimization function */
209  struct OptimizationFunctor : pcl::Functor<double>
210  {
211  /** Functor constructor
212  * \param[in] m_data_points the number of functions
213  * \param[in] estimator pointer to the estimator object
214  * \param[in] distance distance computation function pointer
215  */
216  OptimizationFunctor (int m_data_points, pcl::SampleConsensusModelCircle3D<PointT> *model) :
217  pcl::Functor<double> (m_data_points), model_ (model) {}
218 
219  /** Cost function to be minimized
220  * \param[in] x the variables array
221  * \param[out] fvec the resultant functions evaluations
222  * \return 0
223  */
224  int operator() (const Eigen::VectorXd &x, Eigen::VectorXd &fvec) const
225  {
226  for (int i = 0; i < values (); ++i)
227  {
228  // what i have:
229  // P : Sample Point
230  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);
231  // C : Circle Center
232  Eigen::Vector3d C (x[0], x[1], x[2]);
233  // N : Circle (Plane) Normal
234  Eigen::Vector3d N (x[4], x[5], x[6]);
235  // r : Radius
236  double r = x[3];
237 
238  Eigen::Vector3d helperVectorPC = P - C;
239  // 1.1. get line parameter
240  //float lambda = (helperVectorPC.dot(N)) / N.squaredNorm() ;
241  double lambda = (-(helperVectorPC.dot (N))) / N.dot (N);
242  // Projected Point on plane
243  Eigen::Vector3d P_proj = P + lambda * N;
244  Eigen::Vector3d helperVectorP_projC = P_proj - C;
245 
246  // K : Point on Circle
247  Eigen::Vector3d K = C + r * helperVectorP_projC.normalized ();
248  Eigen::Vector3d distanceVector = P - K;
249 
250  fvec[i] = distanceVector.norm ();
251  }
252  return (0);
253  }
254 
256  };
257  };
258 }
259 
260 #endif //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_CIRCLE3D_H_