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/pcl_macros.h>
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 (const int &normal_index : normal_indices)
210  {
211  const PointNT& cur_pt = normal_cloud[normal_index];
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  using Ptr = boost::shared_ptr<NormalEstimation<PointInT, PointOutT> >;
246  using ConstPtr = boost::shared_ptr<const NormalEstimation<PointInT, PointOutT> >;
255 
258 
259  /** \brief Empty constructor. */
261  : vpx_ (0)
262  , vpy_ (0)
263  , vpz_ (0)
264  , use_sensor_origin_ (true)
265  {
266  feature_name_ = "NormalEstimation";
267  };
268 
269  /** \brief Empty destructor */
271 
272  /** \brief Compute the Least-Squares plane fit for a given set of points, using their indices,
273  * and return the estimated plane parameters together with the surface curvature.
274  * \param cloud the input point cloud
275  * \param indices the point cloud indices that need to be used
276  * \param plane_parameters the plane parameters as: a, b, c, d (ax + by + cz + d = 0)
277  * \param curvature the estimated surface curvature as a measure of
278  * \f[
279  * \lambda_0 / (\lambda_0 + \lambda_1 + \lambda_2)
280  * \f]
281  */
282  inline bool
283  computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices,
284  Eigen::Vector4f &plane_parameters, float &curvature)
285  {
286  if (indices.size () < 3 ||
288  {
289  plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
290  curvature = std::numeric_limits<float>::quiet_NaN ();
291  return false;
292  }
293 
294  // Get the plane normal and surface curvature
295  solvePlaneParameters (covariance_matrix_, xyz_centroid_, plane_parameters, curvature);
296  return true;
297  }
298 
299  /** \brief Compute the Least-Squares plane fit for a given set of points, using their indices,
300  * and return the estimated plane parameters together with the surface curvature.
301  * \param cloud the input point cloud
302  * \param indices the point cloud indices that need to be used
303  * \param nx the resultant X component of the plane normal
304  * \param ny the resultant Y component of the plane normal
305  * \param nz the resultant Z component of the plane normal
306  * \param curvature the estimated surface curvature as a measure of
307  * \f[
308  * \lambda_0 / (\lambda_0 + \lambda_1 + \lambda_2)
309  * \f]
310  */
311  inline bool
312  computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices,
313  float &nx, float &ny, float &nz, float &curvature)
314  {
315  if (indices.size () < 3 ||
317  {
318  nx = ny = nz = curvature = std::numeric_limits<float>::quiet_NaN ();
319  return false;
320  }
321 
322  // Get the plane normal and surface curvature
323  solvePlaneParameters (covariance_matrix_, nx, ny, nz, curvature);
324  return true;
325  }
326 
327  /** \brief Provide a pointer to the input dataset
328  * \param cloud the const boost shared pointer to a PointCloud message
329  */
330  inline void
331  setInputCloud (const PointCloudConstPtr &cloud) override
332  {
333  input_ = cloud;
334  if (use_sensor_origin_)
335  {
336  vpx_ = input_->sensor_origin_.coeff (0);
337  vpy_ = input_->sensor_origin_.coeff (1);
338  vpz_ = input_->sensor_origin_.coeff (2);
339  }
340  }
341 
342  /** \brief Set the viewpoint.
343  * \param vpx the X coordinate of the viewpoint
344  * \param vpy the Y coordinate of the viewpoint
345  * \param vpz the Z coordinate of the viewpoint
346  */
347  inline void
348  setViewPoint (float vpx, float vpy, float vpz)
349  {
350  vpx_ = vpx;
351  vpy_ = vpy;
352  vpz_ = vpz;
353  use_sensor_origin_ = false;
354  }
355 
356  /** \brief Get the viewpoint.
357  * \param [out] vpx x-coordinate of the view point
358  * \param [out] vpy y-coordinate of the view point
359  * \param [out] vpz z-coordinate of the view point
360  * \note this method returns the currently used viewpoint for normal flipping.
361  * If the viewpoint is set manually using the setViewPoint method, this method will return the set view point coordinates.
362  * If an input cloud is set, it will return the sensor origin otherwise it will return the origin (0, 0, 0)
363  */
364  inline void
365  getViewPoint (float &vpx, float &vpy, float &vpz)
366  {
367  vpx = vpx_;
368  vpy = vpy_;
369  vpz = vpz_;
370  }
371 
372  /** \brief sets whether the sensor origin or a user given viewpoint should be used. After this method, the
373  * normal estimation method uses the sensor origin of the input cloud.
374  * to use a user defined view point, use the method setViewPoint
375  */
376  inline void
378  {
379  use_sensor_origin_ = true;
380  if (input_)
381  {
382  vpx_ = input_->sensor_origin_.coeff (0);
383  vpy_ = input_->sensor_origin_.coeff (1);
384  vpz_ = input_->sensor_origin_.coeff (2);
385  }
386  else
387  {
388  vpx_ = 0;
389  vpy_ = 0;
390  vpz_ = 0;
391  }
392  }
393 
394  protected:
395  /** \brief Estimate normals for all points given in <setInputCloud (), setIndices ()> using the surface in
396  * setSearchSurface () and the spatial locator in setSearchMethod ()
397  * \note In situations where not enough neighbors are found, the normal and curvature values are set to NaN.
398  * \param output the resultant point cloud model dataset that contains surface normals and curvatures
399  */
400  void
401  computeFeature (PointCloudOut &output) override;
402 
403  /** \brief Values describing the viewpoint ("pinhole" camera model assumed). For per point viewpoints, inherit
404  * from NormalEstimation and provide your own computeFeature (). By default, the viewpoint is set to 0,0,0. */
405  float vpx_, vpy_, vpz_;
406 
407  /** \brief Placeholder for the 3x3 covariance matrix at each surface patch. */
408  EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix_;
409 
410  /** \brief 16-bytes aligned placeholder for the XYZ centroid of a surface patch. */
411  Eigen::Vector4f xyz_centroid_;
412 
413  /** whether the sensor origin of the input cloud or a user given viewpoint should be used.*/
415 
416  public:
418  };
419 }
420 
421 #ifdef PCL_NO_PRECOMPILE
422 #include <pcl/features/impl/normal_3d.hpp>
423 #endif
boost::shared_ptr< const Feature< PointInT, PointNT > > ConstPtr
Definition: feature.h:114
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
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:283
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:312
void setViewPoint(float vpx, float vpy, float vpz)
Set the viewpoint.
Definition: normal_3d.h:348
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:483
void computeFeature(PointCloudOut &output) override
Estimate normals for all points given in <setInputCloud (), setIndices ()> using the surface in setSe...
Definition: normal_3d.hpp:48
std::string feature_name_
The feature name.
Definition: feature.h:222
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
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
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: pcl_macros.h:344
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
~NormalEstimation()
Empty destructor.
Definition: normal_3d.h:270
Eigen::Vector4f xyz_centroid_
16-bytes aligned placeholder for the XYZ centroid of a surface patch.
Definition: normal_3d.h:411
NormalEstimation()
Empty constructor.
Definition: normal_3d.h:260
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:408
bool use_sensor_origin_
whether the sensor origin of the input cloud or a user given viewpoint should be used.
Definition: normal_3d.h:414
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:151
Feature represents the base feature class.
Definition: feature.h:105
A point structure representing Euclidean xyz coordinates, and the RGB color.
void setInputCloud(const PointCloudConstPtr &cloud) override
Provide a pointer to the input dataset.
Definition: normal_3d.h:331
void getViewPoint(float &vpx, float &vpy, float &vpz)
Get the viewpoint.
Definition: normal_3d.h:365
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
boost::shared_ptr< Feature< PointInT, PointNT > > Ptr
Definition: feature.h:113
Defines all the PCL and non-PCL macros used.
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74
Define methods for centroid estimation and covariance matrix calculus.
float vpx_
Values describing the viewpoint ("pinhole" camera model assumed).
Definition: normal_3d.h:405
void useSensorOriginAsViewPoint()
sets whether the sensor origin or a user given viewpoint should be used.
Definition: normal_3d.h:377
size_t size() const
Definition: point_cloud.h:461