Point Cloud Library (PCL)  1.11.1-dev
sac_model_plane.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 #include <pcl/sample_consensus/sac_model.h>
44 #include <pcl/sample_consensus/model_types.h>
45 
46 namespace pcl
47 {
48 
49  /** \brief Project a point on a planar model given by a set of normalized coefficients
50  * \param[in] p the input point to project
51  * \param[in] model_coefficients the coefficients of the plane (a, b, c, d) that satisfy ax+by+cz+d=0
52  * \param[out] q the resultant projected point
53  */
54  template <typename Point> inline void
55  projectPoint (const Point &p, const Eigen::Vector4f &model_coefficients, Point &q)
56  {
57  // Calculate the distance from the point to the plane
58  Eigen::Vector4f pp (p.x, p.y, p.z, 1);
59  // use normalized coefficients to calculate the scalar projection
60  float distance_to_plane = pp.dot(model_coefficients);
61 
62 
63  //TODO: Why doesn't getVector4Map work here?
64  //Eigen::Vector4f q_e = q.getVector4fMap ();
65  //q_e = pp - model_coefficients * distance_to_plane;
66 
67  Eigen::Vector4f q_e = pp - distance_to_plane * model_coefficients;
68  q.x = q_e[0];
69  q.y = q_e[1];
70  q.z = q_e[2];
71  }
72 
73  /** \brief Get the distance from a point to a plane (signed) defined by ax+by+cz+d=0
74  * \param p a point
75  * \param a the normalized <i>a</i> coefficient of a plane
76  * \param b the normalized <i>b</i> coefficient of a plane
77  * \param c the normalized <i>c</i> coefficient of a plane
78  * \param d the normalized <i>d</i> coefficient of a plane
79  * \ingroup sample_consensus
80  */
81  template <typename Point> inline double
82  pointToPlaneDistanceSigned (const Point &p, double a, double b, double c, double d)
83  {
84  return (a * p.x + b * p.y + c * p.z + d);
85  }
86 
87  /** \brief Get the distance from a point to a plane (signed) defined by ax+by+cz+d=0
88  * \param p a point
89  * \param plane_coefficients the normalized coefficients (a, b, c, d) of a plane
90  * \ingroup sample_consensus
91  */
92  template <typename Point> inline double
93  pointToPlaneDistanceSigned (const Point &p, const Eigen::Vector4f &plane_coefficients)
94  {
95  return ( plane_coefficients[0] * p.x + plane_coefficients[1] * p.y + plane_coefficients[2] * p.z + plane_coefficients[3] );
96  }
97 
98  /** \brief Get the distance from a point to a plane (unsigned) defined by ax+by+cz+d=0
99  * \param p a point
100  * \param a the normalized <i>a</i> coefficient of a plane
101  * \param b the normalized <i>b</i> coefficient of a plane
102  * \param c the normalized <i>c</i> coefficient of a plane
103  * \param d the normalized <i>d</i> coefficient of a plane
104  * \ingroup sample_consensus
105  */
106  template <typename Point> inline double
107  pointToPlaneDistance (const Point &p, double a, double b, double c, double d)
108  {
109  return (std::abs (pointToPlaneDistanceSigned (p, a, b, c, d)) );
110  }
111 
112  /** \brief Get the distance from a point to a plane (unsigned) defined by ax+by+cz+d=0
113  * \param p a point
114  * \param plane_coefficients the normalized coefficients (a, b, c, d) of a plane
115  * \ingroup sample_consensus
116  */
117  template <typename Point> inline double
118  pointToPlaneDistance (const Point &p, const Eigen::Vector4f &plane_coefficients)
119  {
120  return ( std::abs (pointToPlaneDistanceSigned (p, plane_coefficients)) );
121  }
122 
123  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
124  /** \brief SampleConsensusModelPlane defines a model for 3D plane segmentation.
125  * The model coefficients are defined as:
126  * - \b a : the X coordinate of the plane's normal (normalized)
127  * - \b b : the Y coordinate of the plane's normal (normalized)
128  * - \b c : the Z coordinate of the plane's normal (normalized)
129  * - \b d : the fourth <a href="http://mathworld.wolfram.com/HessianNormalForm.html">Hessian component</a> of the plane's equation
130  *
131  * \author Radu B. Rusu
132  * \ingroup sample_consensus
133  */
134  template <typename PointT>
136  {
137  public:
143 
147 
148  using Ptr = shared_ptr<SampleConsensusModelPlane<PointT> >;
149  using ConstPtr = shared_ptr<const SampleConsensusModelPlane<PointT>>;
150 
151  /** \brief Constructor for base SampleConsensusModelPlane.
152  * \param[in] cloud the input point cloud dataset
153  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
154  */
155  SampleConsensusModelPlane (const PointCloudConstPtr &cloud, bool random = false)
156  : SampleConsensusModel<PointT> (cloud, random)
157  {
158  model_name_ = "SampleConsensusModelPlane";
159  sample_size_ = 3;
160  model_size_ = 4;
161  }
162 
163  /** \brief Constructor for base SampleConsensusModelPlane.
164  * \param[in] cloud the input point cloud dataset
165  * \param[in] indices a vector of point indices to be used from \a cloud
166  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
167  */
169  const Indices &indices,
170  bool random = false)
171  : SampleConsensusModel<PointT> (cloud, indices, random)
172  {
173  model_name_ = "SampleConsensusModelPlane";
174  sample_size_ = 3;
175  model_size_ = 4;
176  }
177 
178  /** \brief Empty destructor */
180 
181  /** \brief Check whether the given index samples can form a valid plane model, compute the model coefficients from
182  * these samples and store them internally in model_coefficients_. The plane coefficients are:
183  * a, b, c, d (ax+by+cz+d=0)
184  * \param[in] samples the point indices found as possible good candidates for creating a valid model
185  * \param[out] model_coefficients the resultant model coefficients
186  */
187  bool
188  computeModelCoefficients (const Indices &samples,
189  Eigen::VectorXf &model_coefficients) const override;
190 
191  /** \brief Compute all distances from the cloud data to a given plane model.
192  * \param[in] model_coefficients the coefficients of a plane model that we need to compute distances to
193  * \param[out] distances the resultant estimated distances
194  */
195  void
196  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
197  std::vector<double> &distances) const override;
198 
199  /** \brief Select all the points which respect the given model coefficients as inliers.
200  * \param[in] model_coefficients the coefficients of a plane model that we need to compute distances to
201  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
202  * \param[out] inliers the resultant model inliers
203  */
204  void
205  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
206  const double threshold,
207  Indices &inliers) override;
208 
209  /** \brief Count all the points which respect the given model coefficients as inliers.
210  *
211  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
212  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
213  * \return the resultant number of inliers
214  */
215  std::size_t
216  countWithinDistance (const Eigen::VectorXf &model_coefficients,
217  const double threshold) const override;
218 
219  /** \brief Recompute the plane coefficients using the given inlier set and return them to the user.
220  * @note: these are the coefficients of the plane model after refinement (e.g. after SVD)
221  * \param[in] inliers the data inliers found as supporting the model
222  * \param[in] model_coefficients the initial guess for the model coefficients
223  * \param[out] optimized_coefficients the resultant recomputed coefficients after non-linear optimization
224  */
225  void
226  optimizeModelCoefficients (const Indices &inliers,
227  const Eigen::VectorXf &model_coefficients,
228  Eigen::VectorXf &optimized_coefficients) const override;
229 
230  /** \brief Create a new point cloud with inliers projected onto the plane model.
231  * \param[in] inliers the data inliers that we want to project on the plane model
232  * \param[in] model_coefficients the *normalized* coefficients of a plane model
233  * \param[out] projected_points the resultant projected points
234  * \param[in] copy_data_fields set to true if we need to copy the other data fields
235  */
236  void
237  projectPoints (const Indices &inliers,
238  const Eigen::VectorXf &model_coefficients,
239  PointCloud &projected_points,
240  bool copy_data_fields = true) const override;
241 
242  /** \brief Verify whether a subset of indices verifies the given plane model coefficients.
243  * \param[in] indices the data indices that need to be tested against the plane model
244  * \param[in] model_coefficients the plane model coefficients
245  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
246  */
247  bool
248  doSamplesVerifyModel (const std::set<index_t> &indices,
249  const Eigen::VectorXf &model_coefficients,
250  const double threshold) const override;
251 
252  /** \brief Return a unique id for this model (SACMODEL_PLANE). */
253  inline pcl::SacModel
254  getModelType () const override { return (SACMODEL_PLANE); }
255 
256  protected:
259 
260  /** This implementation uses no SIMD instructions. It is not intended for normal use.
261  * See countWithinDistance which automatically uses the fastest implementation.
262  */
263  std::size_t
264  countWithinDistanceStandard (const Eigen::VectorXf &model_coefficients,
265  const double threshold,
266  std::size_t i = 0) const;
267 
268 #if defined (__SSE__) && defined (__SSE2__) && defined (__SSE4_1__)
269  /** This implementation uses SSE, SSE2, and SSE4.1 instructions. It is not intended for normal use.
270  * See countWithinDistance which automatically uses the fastest implementation.
271  */
272  std::size_t
273  countWithinDistanceSSE (const Eigen::VectorXf &model_coefficients,
274  const double threshold,
275  std::size_t i = 0) const;
276 #endif
277 
278 #if defined (__AVX__) && defined (__AVX2__)
279  /** This implementation uses AVX and AVX2 instructions. It is not intended for normal use.
280  * See countWithinDistance which automatically uses the fastest implementation.
281  */
282  std::size_t
283  countWithinDistanceAVX (const Eigen::VectorXf &model_coefficients,
284  const double threshold,
285  std::size_t i = 0) const;
286 #endif
287 
288 #ifdef __AVX__
289  inline __m256 dist8 (const std::size_t i, const __m256 &a_vec, const __m256 &b_vec, const __m256 &c_vec, const __m256 &d_vec, const __m256 &abs_help) const;
290 #endif
291 
292 #ifdef __SSE__
293  inline __m128 dist4 (const std::size_t i, const __m128 &a_vec, const __m128 &b_vec, const __m128 &c_vec, const __m128 &d_vec, const __m128 &abs_help) const;
294 #endif
295 
296  private:
297  /** \brief Check if a sample of indices results in a good sample of points
298  * indices.
299  * \param[in] samples the resultant index samples
300  */
301  bool
302  isSampleGood (const Indices &samples) const override;
303  };
304 }
305 
306 #ifdef PCL_NO_PRECOMPILE
307 #include <pcl/sample_consensus/impl/sac_model_plane.hpp>
308 #endif
pcl
Definition: convolution.h:46
pcl::SampleConsensusModelPlane::SampleConsensusModelPlane
SampleConsensusModelPlane(const PointCloudConstPtr &cloud, const Indices &indices, bool random=false)
Constructor for base SampleConsensusModelPlane.
Definition: sac_model_plane.h:168
pcl::SampleConsensusModelPlane::selectWithinDistance
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Select all the points which respect the given model coefficients as inliers.
Definition: sac_model_plane.hpp:176
pcl::SampleConsensusModelPlane::countWithinDistance
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.
Definition: sac_model_plane.hpp:214
pcl::SampleConsensusModelPlane
SampleConsensusModelPlane defines a model for 3D plane segmentation.
Definition: sac_model_plane.h:135
pcl::SampleConsensusModel::sample_size_
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition: sac_model.h:588
pcl::SampleConsensusModel::model_size_
unsigned int model_size_
The number of coefficients in the model.
Definition: sac_model.h:591
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: distances.h:55
pcl::pointToPlaneDistance
double pointToPlaneDistance(const Point &p, double a, double b, double c, double d)
Get the distance from a point to a plane (unsigned) defined by ax+by+cz+d=0.
Definition: sac_model_plane.h:107
pcl::SampleConsensusModelPlane::SampleConsensusModelPlane
SampleConsensusModelPlane(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModelPlane.
Definition: sac_model_plane.h:155
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:628
pcl::SampleConsensusModelPlane::countWithinDistanceStandard
std::size_t countWithinDistanceStandard(const Eigen::VectorXf &model_coefficients, const double threshold, std::size_t i=0) const
This implementation uses no SIMD instructions.
Definition: sac_model_plane.hpp:234
pcl::SampleConsensusModelPlane::projectPoints
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 plane model.
Definition: sac_model_plane.hpp:384
pcl::SampleConsensusModelPlane::optimizeModelCoefficients
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the plane coefficients using the given inlier set and return them to the user.
Definition: sac_model_plane.hpp:335
pcl::SampleConsensusModelPlane::computeModelCoefficients
bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
Check whether the given index samples can form a valid plane model, compute the model coefficients fr...
Definition: sac_model_plane.hpp:70
pcl::SacModel
SacModel
Definition: model_types.h:45
pcl::SampleConsensusModel::ConstPtr
shared_ptr< const SampleConsensusModel< PointT > > ConstPtr
Definition: sac_model.h:78
pcl::SampleConsensusModel::Ptr
shared_ptr< SampleConsensusModel< PointT > > Ptr
Definition: sac_model.h:77
pcl::SampleConsensusModel::model_name_
std::string model_name_
The model name.
Definition: sac_model.h:550
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:131
pcl::SampleConsensusModel::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: sac_model.h:73
pcl::SampleConsensusModelPlane::getDistancesToModel
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the cloud data to a given plane model.
Definition: sac_model_plane.hpp:145
pcl::SampleConsensusModelPlane::doSamplesVerifyModel
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 plane model coefficients.
Definition: sac_model_plane.hpp:470
pcl::SampleConsensusModel::PointCloudPtr
typename PointCloud::Ptr PointCloudPtr
Definition: sac_model.h:74
pcl::SampleConsensusModel
SampleConsensusModel represents the base model class.
Definition: sac_model.h:69
pcl::projectPoint
void projectPoint(const Point &p, const Eigen::Vector4f &model_coefficients, Point &q)
Project a point on a planar model given by a set of normalized coefficients.
Definition: sac_model_plane.h:55
pcl::SampleConsensusModelPlane::getModelType
pcl::SacModel getModelType() const override
Return a unique id for this model (SACMODEL_PLANE).
Definition: sac_model_plane.h:254
pcl::SACMODEL_PLANE
@ SACMODEL_PLANE
Definition: model_types.h:47
pcl::SampleConsensusModelPlane::~SampleConsensusModelPlane
~SampleConsensusModelPlane()
Empty destructor.
Definition: sac_model_plane.h:179
pcl::pointToPlaneDistanceSigned
double pointToPlaneDistanceSigned(const Point &p, double a, double b, double c, double d)
Get the distance from a point to a plane (signed) defined by ax+by+cz+d=0.
Definition: sac_model_plane.h:82