Point Cloud Library (PCL)  1.14.1-dev
sac_model_circle.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 #pragma once
42 
43 #ifdef __SSE__
44 #include <xmmintrin.h> // for __m128
45 #endif // ifdef __SSE__
46 #ifdef __AVX__
47 #include <immintrin.h> // for __m256
48 #endif // ifdef __AVX__
49 
50 #include <pcl/sample_consensus/sac_model.h>
51 #include <pcl/sample_consensus/model_types.h>
52 
53 namespace pcl
54 {
55  /** \brief SampleConsensusModelCircle2D defines a model for 2D circle segmentation on the X-Y plane.
56  *
57  * The model coefficients are defined as:
58  * - \b center.x : the X coordinate of the circle's center
59  * - \b center.y : the Y coordinate of the circle's center
60  * - \b radius : the circle's radius
61  *
62  * \author Radu B. Rusu
63  * \ingroup sample_consensus
64  */
65  template <typename PointT>
67  {
68  public:
75 
79 
80  using Ptr = shared_ptr<SampleConsensusModelCircle2D<PointT> >;
81  using ConstPtr = shared_ptr<const SampleConsensusModelCircle2D<PointT>>;
82 
83  /** \brief Constructor for base SampleConsensusModelCircle2D.
84  * \param[in] cloud the input point cloud dataset
85  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
86  */
87  SampleConsensusModelCircle2D (const PointCloudConstPtr &cloud, bool random = false)
88  : SampleConsensusModel<PointT> (cloud, random)
89  {
90  model_name_ = "SampleConsensusModelCircle2D";
91  sample_size_ = 3;
92  model_size_ = 3;
93  }
94 
95  /** \brief Constructor for base SampleConsensusModelCircle2D.
96  * \param[in] cloud the input point cloud dataset
97  * \param[in] indices a vector of point indices to be used from \a cloud
98  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
99  */
101  const Indices &indices,
102  bool random = false)
103  : SampleConsensusModel<PointT> (cloud, indices, random)
104  {
105  model_name_ = "SampleConsensusModelCircle2D";
106  sample_size_ = 3;
107  model_size_ = 3;
108  }
109 
110  /** \brief Copy constructor.
111  * \param[in] source the model to copy into this
112  */
115  {
116  *this = source;
117  model_name_ = "SampleConsensusModelCircle2D";
118  }
119 
120  /** \brief Empty destructor */
121  ~SampleConsensusModelCircle2D () override = default;
122 
123  /** \brief Copy constructor.
124  * \param[in] source the model to copy into this
125  */
128  {
130  return (*this);
131  }
132 
133  /** \brief Check whether the given index samples can form a valid 2D circle model, compute the model coefficients
134  * from these samples and store them in model_coefficients. The circle coefficients are: x, y, R.
135  * \param[in] samples the point indices found as possible good candidates for creating a valid model
136  * \param[out] model_coefficients the resultant model coefficients
137  */
138  bool
139  computeModelCoefficients (const Indices &samples,
140  Eigen::VectorXf &model_coefficients) const override;
141 
142  /** \brief Compute all distances from the cloud data to a given 2D circle model.
143  * \param[in] model_coefficients the coefficients of a 2D circle model that we need to compute distances to
144  * \param[out] distances the resultant estimated distances
145  */
146  void
147  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
148  std::vector<double> &distances) const override;
149 
150  /** \brief Compute all distances from the cloud data to a given 2D circle model.
151  * \param[in] model_coefficients the coefficients of a 2D circle model that we need to compute distances to
152  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
153  * \param[out] inliers the resultant model inliers
154  */
155  void
156  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
157  const double threshold,
158  Indices &inliers) override;
159 
160  /** \brief Count all the points which respect the given model coefficients as inliers.
161  *
162  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
163  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
164  * \return the resultant number of inliers
165  */
166  std::size_t
167  countWithinDistance (const Eigen::VectorXf &model_coefficients,
168  const double threshold) const override;
169 
170  /** \brief Recompute the 2d circle coefficients using the given inlier set and return them to the user.
171  * @note: these are the coefficients of the 2d circle model after refinement (e.g. after SVD)
172  * \param[in] inliers the data inliers found as supporting the model
173  * \param[in] model_coefficients the initial guess for the optimization
174  * \param[out] optimized_coefficients the resultant recomputed coefficients after non-linear optimization
175  */
176  void
177  optimizeModelCoefficients (const Indices &inliers,
178  const Eigen::VectorXf &model_coefficients,
179  Eigen::VectorXf &optimized_coefficients) const override;
180 
181  /** \brief Create a new point cloud with inliers projected onto the 2d circle model.
182  * \param[in] inliers the data inliers that we want to project on the 2d circle model
183  * \param[in] model_coefficients the coefficients of a 2d circle model
184  * \param[out] projected_points the resultant projected points
185  * \param[in] copy_data_fields set to true if we need to copy the other data fields
186  */
187  void
188  projectPoints (const Indices &inliers,
189  const Eigen::VectorXf &model_coefficients,
190  PointCloud &projected_points,
191  bool copy_data_fields = true) const override;
192 
193  /** \brief Verify whether a subset of indices verifies the given 2d circle model coefficients.
194  * \param[in] indices the data indices that need to be tested against the 2d circle model
195  * \param[in] model_coefficients the 2d circle model coefficients
196  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
197  */
198  bool
199  doSamplesVerifyModel (const std::set<index_t> &indices,
200  const Eigen::VectorXf &model_coefficients,
201  const double threshold) const override;
202 
203  /** \brief Return a unique id for this model (SACMODEL_CIRCLE2D). */
204  inline pcl::SacModel
205  getModelType () const override { return (SACMODEL_CIRCLE2D); }
206 
207  protected:
210 
211  /** \brief Check whether a model is valid given the user constraints.
212  * \param[in] model_coefficients the set of model coefficients
213  */
214  bool
215  isModelValid (const Eigen::VectorXf &model_coefficients) const override;
216 
217  /** \brief Check if a sample of indices results in a good sample of points indices.
218  * \param[in] samples the resultant index samples
219  */
220  bool
221  isSampleGood(const Indices &samples) const override;
222 
223  /** This implementation uses no SIMD instructions. It is not intended for normal use.
224  * See countWithinDistance which automatically uses the fastest implementation.
225  */
226  std::size_t
227  countWithinDistanceStandard (const Eigen::VectorXf &model_coefficients,
228  const double threshold,
229  std::size_t i = 0) const;
230 
231 #if defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
232  /** This implementation uses SSE, SSE2, and SSE4.1 instructions. It is not intended for normal use.
233  * See countWithinDistance which automatically uses the fastest implementation.
234  */
235  std::size_t
236  countWithinDistanceSSE (const Eigen::VectorXf &model_coefficients,
237  const double threshold,
238  std::size_t i = 0) const;
239 #endif
240 
241 #if defined (__AVX__) && defined (__AVX2__)
242  /** This implementation uses AVX and AVX2 instructions. It is not intended for normal use.
243  * See countWithinDistance which automatically uses the fastest implementation.
244  */
245  std::size_t
246  countWithinDistanceAVX (const Eigen::VectorXf &model_coefficients,
247  const double threshold,
248  std::size_t i = 0) const;
249 #endif
250 
251  private:
252  /** \brief Functor for the optimization function */
253  struct OptimizationFunctor : pcl::Functor<float>
254  {
255  /** \brief Functor constructor
256  * \param[in] indices the indices of data points to evaluate
257  * \param[in] estimator pointer to the estimator object
258  */
259  OptimizationFunctor (const pcl::SampleConsensusModelCircle2D<PointT> *model, const Indices& indices) :
260  pcl::Functor<float> (indices.size ()), model_ (model), indices_ (indices) {}
261 
262  /** Cost function to be minimized
263  * \param[in] x the variables array
264  * \param[out] fvec the resultant functions evaluations
265  * \return 0
266  */
267  int
268  operator() (const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const
269  {
270  for (int i = 0; i < values (); ++i)
271  {
272  // Compute the difference between the center of the circle and the datapoint X_i
273  float xt = (*model_->input_)[indices_[i]].x - x[0];
274  float yt = (*model_->input_)[indices_[i]].y - x[1];
275 
276  // g = sqrt ((x-a)^2 + (y-b)^2) - R
277  fvec[i] = std::sqrt (xt * xt + yt * yt) - x[2];
278  }
279  return (0);
280  }
281 
283  const Indices &indices_;
284  };
285 
286 #ifdef __AVX__
287  inline __m256 sqr_dist8 (const std::size_t i, const __m256 a_vec, const __m256 b_vec) const;
288 #endif
289 
290 #ifdef __SSE__
291  inline __m128 sqr_dist4 (const std::size_t i, const __m128 a_vec, const __m128 b_vec) const;
292 #endif
293  };
294 }
295 
296 #ifdef PCL_NO_PRECOMPILE
297 #include <pcl/sample_consensus/impl/sac_model_circle.hpp>
298 #endif
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
SampleConsensusModelCircle2D defines a model for 2D circle segmentation on the X-Y plane.
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.
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...
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 2d circle model.
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Compute all distances from the cloud data to a given 2D circle model.
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the cloud data to a given 2D circle model.
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the 2d circle coefficients using the given inlier set and return them to the user.
pcl::SacModel getModelType() const override
Return a unique id for this model (SACMODEL_CIRCLE2D).
SampleConsensusModelCircle2D(const SampleConsensusModelCircle2D &source)
Copy constructor.
std::size_t countWithinDistanceStandard(const Eigen::VectorXf &model_coefficients, const double threshold, std::size_t i=0) const
This implementation uses no SIMD instructions.
~SampleConsensusModelCircle2D() override=default
Empty destructor.
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
SampleConsensusModelCircle2D(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModelCircle2D.
SampleConsensusModelCircle2D & operator=(const SampleConsensusModelCircle2D &source)
Copy constructor.
SampleConsensusModelCircle2D(const PointCloudConstPtr &cloud, const Indices &indices, bool random=false)
Constructor for base SampleConsensusModelCircle2D.
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 2d circle model coefficients.
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
SacModel
Definition: model_types.h:46
@ SACMODEL_CIRCLE2D
Definition: model_types.h:49
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.