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