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