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