Point Cloud Library (PCL)  1.8.0
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 NormalEstimation estimates local surface properties (surface normals and curvatures)at each
190  * 3D point. If PointOutT is specified as pcl::Normal, the normal is stored in the first 3 components (0-2),
191  * and the curvature is stored in component 3.
192  *
193  * \note The code is stateful as we do not expect this class to be multicore parallelized. Please look at
194  * \ref NormalEstimationOMP for a parallel implementation.
195  * \author Radu B. Rusu
196  * \ingroup features
197  */
198  template <typename PointInT, typename PointOutT>
199  class NormalEstimation: public Feature<PointInT, PointOutT>
200  {
201  public:
202  typedef boost::shared_ptr<NormalEstimation<PointInT, PointOutT> > Ptr;
203  typedef boost::shared_ptr<const NormalEstimation<PointInT, PointOutT> > ConstPtr;
212 
215 
216  /** \brief Empty constructor. */
218  : vpx_ (0)
219  , vpy_ (0)
220  , vpz_ (0)
221  , covariance_matrix_ ()
222  , xyz_centroid_ ()
223  , use_sensor_origin_ (true)
224  {
225  feature_name_ = "NormalEstimation";
226  };
227 
228  /** \brief Empty destructor */
229  virtual ~NormalEstimation () {}
230 
231  /** \brief Compute the Least-Squares plane fit for a given set of points, using their indices,
232  * and return the estimated plane parameters together with the surface curvature.
233  * \param cloud the input point cloud
234  * \param indices the point cloud indices that need to be used
235  * \param plane_parameters the plane parameters as: a, b, c, d (ax + by + cz + d = 0)
236  * \param curvature the estimated surface curvature as a measure of
237  * \f[
238  * \lambda_0 / (\lambda_0 + \lambda_1 + \lambda_2)
239  * \f]
240  */
241  inline bool
242  computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices,
243  Eigen::Vector4f &plane_parameters, float &curvature)
244  {
245  if (indices.size () < 3 ||
247  {
248  plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
249  curvature = std::numeric_limits<float>::quiet_NaN ();
250  return false;
251  }
252 
253  // Get the plane normal and surface curvature
254  solvePlaneParameters (covariance_matrix_, xyz_centroid_, plane_parameters, curvature);
255  return true;
256  }
257 
258  /** \brief Compute the Least-Squares plane fit for a given set of points, using their indices,
259  * and return the estimated plane parameters together with the surface curvature.
260  * \param cloud the input point cloud
261  * \param indices the point cloud indices that need to be used
262  * \param nx the resultant X component of the plane normal
263  * \param ny the resultant Y component of the plane normal
264  * \param nz the resultant Z component of the plane normal
265  * \param curvature the estimated surface curvature as a measure of
266  * \f[
267  * \lambda_0 / (\lambda_0 + \lambda_1 + \lambda_2)
268  * \f]
269  */
270  inline bool
271  computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices,
272  float &nx, float &ny, float &nz, float &curvature)
273  {
274  if (indices.size () < 3 ||
276  {
277  nx = ny = nz = curvature = std::numeric_limits<float>::quiet_NaN ();
278  return false;
279  }
280 
281  // Get the plane normal and surface curvature
282  solvePlaneParameters (covariance_matrix_, nx, ny, nz, curvature);
283  return true;
284  }
285 
286  /** \brief Provide a pointer to the input dataset
287  * \param cloud the const boost shared pointer to a PointCloud message
288  */
289  virtual inline void
290  setInputCloud (const PointCloudConstPtr &cloud)
291  {
292  input_ = cloud;
293  if (use_sensor_origin_)
294  {
295  vpx_ = input_->sensor_origin_.coeff (0);
296  vpy_ = input_->sensor_origin_.coeff (1);
297  vpz_ = input_->sensor_origin_.coeff (2);
298  }
299  }
300 
301  /** \brief Set the viewpoint.
302  * \param vpx the X coordinate of the viewpoint
303  * \param vpy the Y coordinate of the viewpoint
304  * \param vpz the Z coordinate of the viewpoint
305  */
306  inline void
307  setViewPoint (float vpx, float vpy, float vpz)
308  {
309  vpx_ = vpx;
310  vpy_ = vpy;
311  vpz_ = vpz;
312  use_sensor_origin_ = false;
313  }
314 
315  /** \brief Get the viewpoint.
316  * \param [out] vpx x-coordinate of the view point
317  * \param [out] vpy y-coordinate of the view point
318  * \param [out] vpz z-coordinate of the view point
319  * \note this method returns the currently used viewpoint for normal flipping.
320  * If the viewpoint is set manually using the setViewPoint method, this method will return the set view point coordinates.
321  * If an input cloud is set, it will return the sensor origin otherwise it will return the origin (0, 0, 0)
322  */
323  inline void
324  getViewPoint (float &vpx, float &vpy, float &vpz)
325  {
326  vpx = vpx_;
327  vpy = vpy_;
328  vpz = vpz_;
329  }
330 
331  /** \brief sets whether the sensor origin or a user given viewpoint should be used. After this method, the
332  * normal estimation method uses the sensor origin of the input cloud.
333  * to use a user defined view point, use the method setViewPoint
334  */
335  inline void
337  {
338  use_sensor_origin_ = true;
339  if (input_)
340  {
341  vpx_ = input_->sensor_origin_.coeff (0);
342  vpy_ = input_->sensor_origin_.coeff (1);
343  vpz_ = input_->sensor_origin_.coeff (2);
344  }
345  else
346  {
347  vpx_ = 0;
348  vpy_ = 0;
349  vpz_ = 0;
350  }
351  }
352 
353  protected:
354  /** \brief Estimate normals for all points given in <setInputCloud (), setIndices ()> using the surface in
355  * setSearchSurface () and the spatial locator in setSearchMethod ()
356  * \note In situations where not enough neighbors are found, the normal and curvature values are set to NaN.
357  * \param output the resultant point cloud model dataset that contains surface normals and curvatures
358  */
359  void
360  computeFeature (PointCloudOut &output);
361 
362  /** \brief Values describing the viewpoint ("pinhole" camera model assumed). For per point viewpoints, inherit
363  * from NormalEstimation and provide your own computeFeature (). By default, the viewpoint is set to 0,0,0. */
364  float vpx_, vpy_, vpz_;
365 
366  /** \brief Placeholder for the 3x3 covariance matrix at each surface patch. */
368 
369  /** \brief 16-bytes aligned placeholder for the XYZ centroid of a surface patch. */
370  Eigen::Vector4f xyz_centroid_;
371 
372  /** whether the sensor origin of the input cloud or a user given viewpoint should be used.*/
374 
375  public:
376  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
377  };
378 }
379 
380 #ifdef PCL_NO_PRECOMPILE
381 #include <pcl/features/impl/normal_3d.hpp>
382 #endif
383 
384 #endif //#ifndef PCL_NORMAL_3D_H_
385 
boost::shared_ptr< NormalEstimation< PointInT, PointOutT > > Ptr
Definition: normal_3d.h:202
size_t size() const
Definition: point_cloud.h:440
Feature< PointInT, PointOutT >::PointCloudOut PointCloudOut
Definition: normal_3d.h:213
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:242
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:271
void setViewPoint(float vpx, float vpy, float vpz)
Set the viewpoint.
Definition: normal_3d.h:307
struct pcl::PointXYZIEdge EIGEN_ALIGN16
Feature< PointInT, PointOutT >::PointCloudConstPtr PointCloudConstPtr
Definition: normal_3d.h:214
virtual ~NormalEstimation()
Empty destructor.
Definition: normal_3d.h:229
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:490
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:199
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:370
NormalEstimation()
Empty constructor.
Definition: normal_3d.h:217
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: normal_3d.h:290
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:367
boost::shared_ptr< const NormalEstimation< PointInT, PointOutT > > ConstPtr
Definition: normal_3d.h:203
bool use_sensor_origin_
whether the sensor origin of the input cloud or a user given viewpoint should be used.
Definition: normal_3d.h:373
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:324
float vpx_
Values describing the viewpoint ("pinhole" camera model assumed).
Definition: normal_3d.h:364
void useSensorOriginAsViewPoint()
sets whether the sensor origin or a user given viewpoint should be used.
Definition: normal_3d.h:336