Point Cloud Library (PCL)  1.14.1-dev
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 #pragma once
39 
40 #include <pcl/sample_consensus/sac_model.h>
41 #include <pcl/sample_consensus/model_types.h>
42 
43 namespace pcl
44 {
45  /** \brief SampleConsensusModelCircle3D defines a model for 3D circle segmentation.
46  *
47  * The model coefficients are defined as:
48  * - \b center.x : the X coordinate of the circle's center
49  * - \b center.y : the Y coordinate of the circle's center
50  * - \b center.z : the Z coordinate of the circle's center
51  * - \b radius : the circle's radius
52  * - \b normal.x : the X coordinate of the normal's direction
53  * - \b normal.y : the Y coordinate of the normal's direction
54  * - \b normal.z : the Z coordinate of the normal's direction
55  *
56  * \author Raoul Hoffmann, Karol Hausman, Radu B. Rusu
57  * \ingroup sample_consensus
58  */
59  template <typename PointT>
61  {
62  public:
69 
73 
74  using Ptr = shared_ptr<SampleConsensusModelCircle3D<PointT> >;
75  using ConstPtr = shared_ptr<const SampleConsensusModelCircle3D<PointT> >;
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 Indices &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 */
106  ~SampleConsensusModelCircle3D () override = default;
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 Indices &samples,
135  Eigen::VectorXf &model_coefficients) const override;
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 override;
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  Indices &inliers) override;
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  std::size_t
162  countWithinDistance (const Eigen::VectorXf &model_coefficients,
163  const double threshold) const override;
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 Indices &inliers,
173  const Eigen::VectorXf &model_coefficients,
174  Eigen::VectorXf &optimized_coefficients) const override;
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 Indices &inliers,
184  const Eigen::VectorXf &model_coefficients,
185  PointCloud &projected_points,
186  bool copy_data_fields = true) const override;
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<index_t> &indices,
195  const Eigen::VectorXf &model_coefficients,
196  const double threshold) const override;
197 
198  /** \brief Return a unique id for this model (SACMODEL_CIRCLE3D). */
199  inline pcl::SacModel
200  getModelType () const override { 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  bool
210  isModelValid (const Eigen::VectorXf &model_coefficients) const override;
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 Indices &samples) const override;
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 Indices& 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  // Same for all points, so define outside of loop:
237  // C : Circle Center
238  const Eigen::Vector3d C (x[0], x[1], x[2]);
239  // N : Circle (Plane) Normal
240  const Eigen::Vector3d N (x[4], x[5], x[6]);
241  // r : Radius
242  const double r = x[3];
243  for (int i = 0; i < values (); ++i)
244  {
245  // what i have:
246  // P : Sample Point
247  Eigen::Vector3d P =
248  (*model_->input_)[indices_[i]].getVector3fMap().template cast<double>();
249 
250  Eigen::Vector3d helperVectorPC = P - C;
251  // 1.1. get line parameter
252  //float lambda = (helperVectorPC.dot(N)) / N.squaredNorm() ;
253  double lambda = (-(helperVectorPC.dot (N))) / N.dot (N);
254  // Projected Point on plane
255  Eigen::Vector3d P_proj = P + lambda * N;
256  Eigen::Vector3d helperVectorP_projC = P_proj - C;
257 
258  // K : Point on Circle
259  Eigen::Vector3d K = C + r * helperVectorP_projC.normalized ();
260  Eigen::Vector3d distanceVector = P - K;
261 
262  fvec[i] = distanceVector.norm ();
263  }
264  return (0);
265  }
266 
268  const Indices &indices_;
269  };
270  };
271 }
272 
273 #ifdef PCL_NO_PRECOMPILE
274 #include <pcl/sample_consensus/impl/sac_model_circle3d.hpp>
275 #endif
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
SampleConsensusModelCircle3D defines a model for 3D circle segmentation.
SampleConsensusModelCircle3D(const PointCloudConstPtr &cloud, const Indices &indices, bool random=false)
Constructor for base SampleConsensusModelCircle3D.
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
bool doSamplesVerifyModel(const std::set< index_t > &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const override
Verify whether a subset of indices verifies the given 3d circle model coefficients.
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
SampleConsensusModelCircle3D & operator=(const SampleConsensusModelCircle3D &source)
Copy constructor.
void projectPoints(const Indices &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true) const override
Create a new point cloud with inliers projected onto the 3d circle model.
std::size_t countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const override
Count all the points which respect the given model coefficients as inliers.
SampleConsensusModelCircle3D(const SampleConsensusModelCircle3D &source)
Copy constructor.
pcl::SacModel getModelType() const override
Return a unique id for this model (SACMODEL_CIRCLE3D).
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the cloud data to a given 3D circle model.
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the 3d circle coefficients using the given inlier set and return them to the user.
~SampleConsensusModelCircle3D() override=default
Empty destructor.
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Compute all distances from the cloud data to a given 3D circle model.
bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
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.
SampleConsensusModel represents the base model class.
Definition: sac_model.h:71
shared_ptr< SampleConsensusModel< PointT > > Ptr
Definition: sac_model.h:78
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition: sac_model.h:589
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: sac_model.h:74
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition: sac_model.h:557
std::string model_name_
The model name.
Definition: sac_model.h:551
unsigned int model_size_
The number of coefficients in the model.
Definition: sac_model.h:592
typename PointCloud::Ptr PointCloudPtr
Definition: sac_model.h:75
shared_ptr< const SampleConsensusModel< PointT > > ConstPtr
Definition: sac_model.h:79
@ K
Definition: norms.h:54
SacModel
Definition: model_types.h:46
@ SACMODEL_CIRCLE3D
Definition: model_types.h:50
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
Base functor all the models that need non linear optimization must define their own one and implement...
Definition: sac_model.h:680
A point structure representing Euclidean xyz coordinates, and the RGB color.