Point Cloud Library (PCL)  1.8.1-dev
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
normal_3d.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 #ifndef PCL_NORMAL_3D_H_
42 #define PCL_NORMAL_3D_H_
43 
44 #include <pcl/features/feature.h>
45 #include <pcl/common/centroid.h>
46 
47 namespace pcl
48 {
49  /** \brief Compute the Least-Squares plane fit for a given set of points, and return the estimated plane
50  * parameters together with the surface curvature.
51  * \param cloud the input point cloud
52  * \param plane_parameters the plane parameters as: a, b, c, d (ax + by + cz + d = 0)
53  * \param curvature the estimated surface curvature as a measure of
54  * \f[
55  * \lambda_0 / (\lambda_0 + \lambda_1 + \lambda_2)
56  * \f]
57  * \ingroup features
58  */
59  template <typename PointT> inline bool
61  Eigen::Vector4f &plane_parameters, float &curvature)
62  {
63  // Placeholder for the 3x3 covariance matrix at each surface patch
64  EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix;
65  // 16-bytes aligned placeholder for the XYZ centroid of a surface patch
66  Eigen::Vector4f xyz_centroid;
67 
68  if (cloud.size () < 3 ||
69  computeMeanAndCovarianceMatrix (cloud, covariance_matrix, xyz_centroid) == 0)
70  {
71  plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
72  curvature = std::numeric_limits<float>::quiet_NaN ();
73  return false;
74  }
75 
76  // Get the plane normal and surface curvature
77  solvePlaneParameters (covariance_matrix, xyz_centroid, plane_parameters, curvature);
78  return true;
79  }
80 
81  /** \brief Compute the Least-Squares plane fit for a given set of points, using their indices,
82  * and return the estimated plane parameters together with the surface curvature.
83  * \param cloud the input point cloud
84  * \param indices the point cloud indices that need to be used
85  * \param plane_parameters the plane parameters as: a, b, c, d (ax + by + cz + d = 0)
86  * \param curvature the estimated surface curvature as a measure of
87  * \f[
88  * \lambda_0 / (\lambda_0 + \lambda_1 + \lambda_2)
89  * \f]
90  * \ingroup features
91  */
92  template <typename PointT> inline bool
93  computePointNormal (const pcl::PointCloud<PointT> &cloud, const std::vector<int> &indices,
94  Eigen::Vector4f &plane_parameters, float &curvature)
95  {
96  // Placeholder for the 3x3 covariance matrix at each surface patch
97  EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix;
98  // 16-bytes aligned placeholder for the XYZ centroid of a surface patch
99  Eigen::Vector4f xyz_centroid;
100  if (indices.size () < 3 ||
101  computeMeanAndCovarianceMatrix (cloud, indices, covariance_matrix, xyz_centroid) == 0)
102  {
103  plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
104  curvature = std::numeric_limits<float>::quiet_NaN ();
105  return false;
106  }
107  // Get the plane normal and surface curvature
108  solvePlaneParameters (covariance_matrix, xyz_centroid, plane_parameters, curvature);
109  return true;
110  }
111 
112  /** \brief Flip (in place) the estimated normal of a point towards a given viewpoint
113  * \param point a given point
114  * \param vp_x the X coordinate of the viewpoint
115  * \param vp_y the X coordinate of the viewpoint
116  * \param vp_z the X coordinate of the viewpoint
117  * \param normal the plane normal to be flipped
118  * \ingroup features
119  */
120  template <typename PointT, typename Scalar> inline void
121  flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z,
122  Eigen::Matrix<Scalar, 4, 1>& normal)
123  {
124  Eigen::Matrix <Scalar, 4, 1> vp (vp_x - point.x, vp_y - point.y, vp_z - point.z, 0);
125 
126  // Dot product between the (viewpoint - point) and the plane normal
127  float cos_theta = vp.dot (normal);
128 
129  // Flip the plane normal
130  if (cos_theta < 0)
131  {
132  normal *= -1;
133  normal[3] = 0.0f;
134  // Hessian form (D = nc . p_plane (centroid here) + p)
135  normal[3] = -1 * normal.dot (point.getVector4fMap ());
136  }
137  }
138 
139  /** \brief Flip (in place) the estimated normal of a point towards a given viewpoint
140  * \param point a given point
141  * \param vp_x the X coordinate of the viewpoint
142  * \param vp_y the X coordinate of the viewpoint
143  * \param vp_z the X coordinate of the viewpoint
144  * \param normal the plane normal to be flipped
145  * \ingroup features
146  */
147  template <typename PointT, typename Scalar> inline void
148  flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z,
149  Eigen::Matrix<Scalar, 3, 1>& normal)
150  {
151  Eigen::Matrix <Scalar, 3, 1> vp (vp_x - point.x, vp_y - point.y, vp_z - point.z);
152 
153  // Flip the plane normal
154  if (vp.dot (normal) < 0)
155  normal *= -1;
156  }
157 
158  /** \brief Flip (in place) the estimated normal of a point towards a given viewpoint
159  * \param point a given point
160  * \param vp_x the X coordinate of the viewpoint
161  * \param vp_y the X coordinate of the viewpoint
162  * \param vp_z the X coordinate of the viewpoint
163  * \param nx the resultant X component of the plane normal
164  * \param ny the resultant Y component of the plane normal
165  * \param nz the resultant Z component of the plane normal
166  * \ingroup features
167  */
168  template <typename PointT> inline void
169  flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z,
170  float &nx, float &ny, float &nz)
171  {
172  // See if we need to flip any plane normals
173  vp_x -= point.x;
174  vp_y -= point.y;
175  vp_z -= point.z;
176 
177  // Dot product between the (viewpoint - point) and the plane normal
178  float cos_theta = (vp_x * nx + vp_y * ny + vp_z * nz);
179 
180  // Flip the plane normal
181  if (cos_theta < 0)
182  {
183  nx *= -1;
184  ny *= -1;
185  nz *= -1;
186  }
187  }
188 
189  /** \brief Flip (in place) normal to get the same sign of the mean of the normals specified by normal_indices.
190  *
191  * The method is described in:
192  * A. Petrelli, L. Di Stefano, "A repeatable and efficient canonical reference for surface matching", 3DimPVT, 2012
193  * A. Petrelli, L. Di Stefano, "On the repeatability of the local reference frame for partial shape matching", 13th International Conference on Computer Vision (ICCV), 2011
194  *
195  * Normals should be unit vectors. Otherwise the resulting mean would be weighted by the normal norms.
196  * \param[in] normal_cloud Cloud of normals used to compute the mean
197  * \param[in] normal_indices Indices of normals used to compute the mean
198  * \param[in] normal input Normal to flip. Normal is modified by the function.
199  * \return false if normal_indices does not contain any valid normal.
200  * \ingroup features
201  */
202  template<typename PointNT> inline bool
204  std::vector<int> const &normal_indices,
205  Eigen::Vector3f &normal)
206  {
207  Eigen::Vector3f normal_mean = Eigen::Vector3f::Zero ();
208 
209  for (size_t i = 0; i < normal_indices.size (); ++i)
210  {
211  const PointNT& cur_pt = normal_cloud[normal_indices[i]];
212 
213  if (pcl::isFinite (cur_pt))
214  {
215  normal_mean += cur_pt.getNormalVector3fMap ();
216  }
217  }
218 
219  if (normal_mean.isZero ())
220  return false;
221 
222  normal_mean.normalize ();
223 
224  if (normal.dot (normal_mean) < 0)
225  {
226  normal = -normal;
227  }
228 
229  return true;
230  }
231 
232  /** \brief NormalEstimation estimates local surface properties (surface normals and curvatures)at each
233  * 3D point. If PointOutT is specified as pcl::Normal, the normal is stored in the first 3 components (0-2),
234  * and the curvature is stored in component 3.
235  *
236  * \note The code is stateful as we do not expect this class to be multicore parallelized. Please look at
237  * \ref NormalEstimationOMP for a parallel implementation.
238  * \author Radu B. Rusu
239  * \ingroup features
240  */
241  template <typename PointInT, typename PointOutT>
242  class NormalEstimation: public Feature<PointInT, PointOutT>
243  {
244  public:
245  typedef boost::shared_ptr<NormalEstimation<PointInT, PointOutT> > Ptr;
246  typedef boost::shared_ptr<const NormalEstimation<PointInT, PointOutT> > ConstPtr;
255 
258 
259  /** \brief Empty constructor. */
261  : vpx_ (0)
262  , vpy_ (0)
263  , vpz_ (0)
264  , covariance_matrix_ ()
265  , xyz_centroid_ ()
266  , use_sensor_origin_ (true)
267  {
268  feature_name_ = "NormalEstimation";
269  };
270 
271  /** \brief Empty destructor */
272  virtual ~NormalEstimation () {}
273 
274  /** \brief Compute the Least-Squares plane fit for a given set of points, using their indices,
275  * and return the estimated plane parameters together with the surface curvature.
276  * \param cloud the input point cloud
277  * \param indices the point cloud indices that need to be used
278  * \param plane_parameters the plane parameters as: a, b, c, d (ax + by + cz + d = 0)
279  * \param curvature the estimated surface curvature as a measure of
280  * \f[
281  * \lambda_0 / (\lambda_0 + \lambda_1 + \lambda_2)
282  * \f]
283  */
284  inline bool
285  computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices,
286  Eigen::Vector4f &plane_parameters, float &curvature)
287  {
288  if (indices.size () < 3 ||
290  {
291  plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
292  curvature = std::numeric_limits<float>::quiet_NaN ();
293  return false;
294  }
295 
296  // Get the plane normal and surface curvature
297  solvePlaneParameters (covariance_matrix_, xyz_centroid_, plane_parameters, curvature);
298  return true;
299  }
300 
301  /** \brief Compute the Least-Squares plane fit for a given set of points, using their indices,
302  * and return the estimated plane parameters together with the surface curvature.
303  * \param cloud the input point cloud
304  * \param indices the point cloud indices that need to be used
305  * \param nx the resultant X component of the plane normal
306  * \param ny the resultant Y component of the plane normal
307  * \param nz the resultant Z component of the plane normal
308  * \param curvature the estimated surface curvature as a measure of
309  * \f[
310  * \lambda_0 / (\lambda_0 + \lambda_1 + \lambda_2)
311  * \f]
312  */
313  inline bool
314  computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices,
315  float &nx, float &ny, float &nz, float &curvature)
316  {
317  if (indices.size () < 3 ||
319  {
320  nx = ny = nz = curvature = std::numeric_limits<float>::quiet_NaN ();
321  return false;
322  }
323 
324  // Get the plane normal and surface curvature
325  solvePlaneParameters (covariance_matrix_, nx, ny, nz, curvature);
326  return true;
327  }
328 
329  /** \brief Provide a pointer to the input dataset
330  * \param cloud the const boost shared pointer to a PointCloud message
331  */
332  virtual inline void
334  {
335  input_ = cloud;
336  if (use_sensor_origin_)
337  {
338  vpx_ = input_->sensor_origin_.coeff (0);
339  vpy_ = input_->sensor_origin_.coeff (1);
340  vpz_ = input_->sensor_origin_.coeff (2);
341  }
342  }
343 
344  /** \brief Set the viewpoint.
345  * \param vpx the X coordinate of the viewpoint
346  * \param vpy the Y coordinate of the viewpoint
347  * \param vpz the Z coordinate of the viewpoint
348  */
349  inline void
350  setViewPoint (float vpx, float vpy, float vpz)
351  {
352  vpx_ = vpx;
353  vpy_ = vpy;
354  vpz_ = vpz;
355  use_sensor_origin_ = false;
356  }
357 
358  /** \brief Get the viewpoint.
359  * \param [out] vpx x-coordinate of the view point
360  * \param [out] vpy y-coordinate of the view point
361  * \param [out] vpz z-coordinate of the view point
362  * \note this method returns the currently used viewpoint for normal flipping.
363  * If the viewpoint is set manually using the setViewPoint method, this method will return the set view point coordinates.
364  * If an input cloud is set, it will return the sensor origin otherwise it will return the origin (0, 0, 0)
365  */
366  inline void
367  getViewPoint (float &vpx, float &vpy, float &vpz)
368  {
369  vpx = vpx_;
370  vpy = vpy_;
371  vpz = vpz_;
372  }
373 
374  /** \brief sets whether the sensor origin or a user given viewpoint should be used. After this method, the
375  * normal estimation method uses the sensor origin of the input cloud.
376  * to use a user defined view point, use the method setViewPoint
377  */
378  inline void
380  {
381  use_sensor_origin_ = true;
382  if (input_)
383  {
384  vpx_ = input_->sensor_origin_.coeff (0);
385  vpy_ = input_->sensor_origin_.coeff (1);
386  vpz_ = input_->sensor_origin_.coeff (2);
387  }
388  else
389  {
390  vpx_ = 0;
391  vpy_ = 0;
392  vpz_ = 0;
393  }
394  }
395 
396  protected:
397  /** \brief Estimate normals for all points given in <setInputCloud (), setIndices ()> using the surface in
398  * setSearchSurface () and the spatial locator in setSearchMethod ()
399  * \note In situations where not enough neighbors are found, the normal and curvature values are set to NaN.
400  * \param output the resultant point cloud model dataset that contains surface normals and curvatures
401  */
402  void
403  computeFeature (PointCloudOut &output);
404 
405  /** \brief Values describing the viewpoint ("pinhole" camera model assumed). For per point viewpoints, inherit
406  * from NormalEstimation and provide your own computeFeature (). By default, the viewpoint is set to 0,0,0. */
407  float vpx_, vpy_, vpz_;
408 
409  /** \brief Placeholder for the 3x3 covariance matrix at each surface patch. */
411 
412  /** \brief 16-bytes aligned placeholder for the XYZ centroid of a surface patch. */
413  Eigen::Vector4f xyz_centroid_;
414 
415  /** whether the sensor origin of the input cloud or a user given viewpoint should be used.*/
417 
418  public:
419  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
420  };
421 }
422 
423 #ifdef PCL_NO_PRECOMPILE
424 #include <pcl/features/impl/normal_3d.hpp>
425 #endif
426 
427 #endif //#ifndef PCL_NORMAL_3D_H_
428 
boost::shared_ptr< NormalEstimation< PointInT, PointOutT > > Ptr
Definition: normal_3d.h:245
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Definition: point_tests.h:54
Feature< PointInT, PointOutT >::PointCloudOut PointCloudOut
Definition: normal_3d.h:256
PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:73
bool computePointNormal(const pcl::PointCloud< PointInT > &cloud, const std::vector< int > &indices, Eigen::Vector4f &plane_parameters, float &curvature)
Compute the Least-Squares plane fit for a given set of points, using their indices, and return the estimated plane parameters together with the surface curvature.
Definition: normal_3d.h:285
bool computePointNormal(const pcl::PointCloud< PointInT > &cloud, const std::vector< int > &indices, float &nx, float &ny, float &nz, float &curvature)
Compute the Least-Squares plane fit for a given set of points, using their indices, and return the estimated plane parameters together with the surface curvature.
Definition: normal_3d.h:314
void setViewPoint(float vpx, float vpy, float vpz)
Set the viewpoint.
Definition: normal_3d.h:350
struct pcl::PointXYZIEdge EIGEN_ALIGN16
Feature< PointInT, PointOutT >::PointCloudConstPtr PointCloudConstPtr
Definition: normal_3d.h:257
virtual ~NormalEstimation()
Empty destructor.
Definition: normal_3d.h:272
unsigned int computeMeanAndCovarianceMatrix(const pcl::PointCloud< PointT > &cloud, Eigen::Matrix< Scalar, 3, 3 > &covariance_matrix, Eigen::Matrix< Scalar, 4, 1 > &centroid)
Compute the normalized 3x3 covariance matrix and the centroid of a given set of points in a single lo...
Definition: centroid.hpp:489
std::string feature_name_
The feature name.
Definition: feature.h:222
void solvePlaneParameters(const Eigen::Matrix3f &covariance_matrix, const Eigen::Vector4f &point, Eigen::Vector4f &plane_parameters, float &curvature)
Solve the eigenvalues and eigenvectors of a given 3x3 covariance matrix, and estimate the least-squar...
Definition: feature.hpp:48
NormalEstimation estimates local surface properties (surface normals and curvatures)at each 3D point...
Definition: normal_3d.h:242
bool computePointNormal(const pcl::PointCloud< PointT > &cloud, Eigen::Vector4f &plane_parameters, float &curvature)
Compute the Least-Squares plane fit for a given set of points, and return the estimated plane paramet...
Definition: normal_3d.h:60
void flipNormalTowardsViewpoint(const PointT &point, float vp_x, float vp_y, float vp_z, Eigen::Matrix< Scalar, 4, 1 > &normal)
Flip (in place) the estimated normal of a point towards a given viewpoint.
Definition: normal_3d.h:121
Eigen::Vector4f xyz_centroid_
16-bytes aligned placeholder for the XYZ centroid of a surface patch.
Definition: normal_3d.h:413
NormalEstimation()
Empty constructor.
Definition: normal_3d.h:260
size_t size() const
Definition: point_cloud.h:448
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: normal_3d.h:333
EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix_
Placeholder for the 3x3 covariance matrix at each surface patch.
Definition: normal_3d.h:410
boost::shared_ptr< const NormalEstimation< PointInT, PointOutT > > ConstPtr
Definition: normal_3d.h:246
bool use_sensor_origin_
whether the sensor origin of the input cloud or a user given viewpoint should be used.
Definition: normal_3d.h:416
void computeFeature(PointCloudOut &output)
Estimate normals for all points given in <setInputCloud (), setIndices ()> using the surface in setSe...
Definition: normal_3d.hpp:48
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:150
Feature represents the base feature class.
Definition: feature.h:105
A point structure representing Euclidean xyz coordinates, and the RGB color.
void getViewPoint(float &vpx, float &vpy, float &vpz)
Get the viewpoint.
Definition: normal_3d.h:367
bool flipNormalTowardsNormalsMean(pcl::PointCloud< PointNT > const &normal_cloud, std::vector< int > const &normal_indices, Eigen::Vector3f &normal)
Flip (in place) normal to get the same sign of the mean of the normals specified by normal_indices...
Definition: normal_3d.h:203
float vpx_
Values describing the viewpoint ("pinhole" camera model assumed).
Definition: normal_3d.h:407
void useSensorOriginAsViewPoint()
sets whether the sensor origin or a user given viewpoint should be used.
Definition: normal_3d.h:379