Point Cloud Library (PCL)  1.7.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 void
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;
74  }
75 
76  // Get the plane normal and surface curvature
77  solvePlaneParameters (covariance_matrix, xyz_centroid, plane_parameters, curvature);
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 void
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;
105  }
106  // Get the plane normal and surface curvature
107  solvePlaneParameters (covariance_matrix, xyz_centroid, plane_parameters, curvature);
108  }
109 
110  /** \brief Flip (in place) the estimated normal of a point towards a given viewpoint
111  * \param point a given point
112  * \param vp_x the X coordinate of the viewpoint
113  * \param vp_y the X coordinate of the viewpoint
114  * \param vp_z the X coordinate of the viewpoint
115  * \param normal the plane normal to be flipped
116  * \ingroup features
117  */
118  template <typename PointT, typename Scalar> inline void
119  flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z,
120  Eigen::Matrix<Scalar, 4, 1>& normal)
121  {
122  Eigen::Matrix <Scalar, 4, 1> vp (vp_x - point.x, vp_y - point.y, vp_z - point.z, 0);
123 
124  // Dot product between the (viewpoint - point) and the plane normal
125  float cos_theta = vp.dot (normal);
126 
127  // Flip the plane normal
128  if (cos_theta < 0)
129  {
130  normal *= -1;
131  normal[3] = 0.0f;
132  // Hessian form (D = nc . p_plane (centroid here) + p)
133  normal[3] = -1 * normal.dot (point.getVector4fMap ());
134  }
135  }
136 
137  /** \brief Flip (in place) the estimated normal of a point towards a given viewpoint
138  * \param point a given point
139  * \param vp_x the X coordinate of the viewpoint
140  * \param vp_y the X coordinate of the viewpoint
141  * \param vp_z the X coordinate of the viewpoint
142  * \param normal the plane normal to be flipped
143  * \ingroup features
144  */
145  template <typename PointT, typename Scalar> inline void
146  flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z,
147  Eigen::Matrix<Scalar, 3, 1>& normal)
148  {
149  Eigen::Matrix <Scalar, 3, 1> vp (vp_x - point.x, vp_y - point.y, vp_z - point.z);
150 
151  // Flip the plane normal
152  if (vp.dot (normal) < 0)
153  normal *= -1;
154  }
155 
156  /** \brief Flip (in place) the estimated normal of a point towards a given viewpoint
157  * \param point a given point
158  * \param vp_x the X coordinate of the viewpoint
159  * \param vp_y the X coordinate of the viewpoint
160  * \param vp_z the X coordinate of the viewpoint
161  * \param nx the resultant X component of the plane normal
162  * \param ny the resultant Y component of the plane normal
163  * \param nz the resultant Z component of the plane normal
164  * \ingroup features
165  */
166  template <typename PointT> inline void
167  flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z,
168  float &nx, float &ny, float &nz)
169  {
170  // See if we need to flip any plane normals
171  vp_x -= point.x;
172  vp_y -= point.y;
173  vp_z -= point.z;
174 
175  // Dot product between the (viewpoint - point) and the plane normal
176  float cos_theta = (vp_x * nx + vp_y * ny + vp_z * nz);
177 
178  // Flip the plane normal
179  if (cos_theta < 0)
180  {
181  nx *= -1;
182  ny *= -1;
183  nz *= -1;
184  }
185  }
186 
187  /** \brief NormalEstimation estimates local surface properties (surface normals and curvatures)at each
188  * 3D point. If PointOutT is specified as pcl::Normal, the normal is stored in the first 3 components (0-2),
189  * and the curvature is stored in component 3.
190  *
191  * \note The code is stateful as we do not expect this class to be multicore parallelized. Please look at
192  * \ref NormalEstimationOMP for a parallel implementation.
193  * \author Radu B. Rusu
194  * \ingroup features
195  */
196  template <typename PointInT, typename PointOutT>
197  class NormalEstimation: public Feature<PointInT, PointOutT>
198  {
199  public:
200  typedef boost::shared_ptr<NormalEstimation<PointInT, PointOutT> > Ptr;
201  typedef boost::shared_ptr<const NormalEstimation<PointInT, PointOutT> > ConstPtr;
210 
213 
214  /** \brief Empty constructor. */
216  : vpx_ (0)
217  , vpy_ (0)
218  , vpz_ (0)
219  , covariance_matrix_ ()
220  , xyz_centroid_ ()
221  , use_sensor_origin_ (true)
222  {
223  feature_name_ = "NormalEstimation";
224  };
225 
226  /** \brief Empty destructor */
227  virtual ~NormalEstimation () {}
228 
229  /** \brief Compute the Least-Squares plane fit for a given set of points, using their indices,
230  * and return the estimated plane parameters together with the surface curvature.
231  * \param cloud the input point cloud
232  * \param indices the point cloud indices that need to be used
233  * \param plane_parameters the plane parameters as: a, b, c, d (ax + by + cz + d = 0)
234  * \param curvature the estimated surface curvature as a measure of
235  * \f[
236  * \lambda_0 / (\lambda_0 + \lambda_1 + \lambda_2)
237  * \f]
238  */
239  inline void
240  computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices,
241  Eigen::Vector4f &plane_parameters, float &curvature)
242  {
243  if (indices.size () < 3 ||
245  {
246  plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
247  curvature = std::numeric_limits<float>::quiet_NaN ();
248  return;
249  }
250 
251  // Get the plane normal and surface curvature
252  solvePlaneParameters (covariance_matrix_, xyz_centroid_, plane_parameters, curvature);
253  }
254 
255  /** \brief Compute the Least-Squares plane fit for a given set of points, using their indices,
256  * and return the estimated plane parameters together with the surface curvature.
257  * \param cloud the input point cloud
258  * \param indices the point cloud indices that need to be used
259  * \param nx the resultant X component of the plane normal
260  * \param ny the resultant Y component of the plane normal
261  * \param nz the resultant Z component of the plane normal
262  * \param curvature the estimated surface curvature as a measure of
263  * \f[
264  * \lambda_0 / (\lambda_0 + \lambda_1 + \lambda_2)
265  * \f]
266  */
267  inline void
268  computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices,
269  float &nx, float &ny, float &nz, float &curvature)
270  {
271  if (indices.size () < 3 ||
273  {
274  nx = ny = nz = curvature = std::numeric_limits<float>::quiet_NaN ();
275  return;
276  }
277 
278  // Get the plane normal and surface curvature
279  solvePlaneParameters (covariance_matrix_, nx, ny, nz, curvature);
280  }
281 
282  /** \brief Provide a pointer to the input dataset
283  * \param cloud the const boost shared pointer to a PointCloud message
284  */
285  virtual inline void
287  {
288  input_ = cloud;
289  if (use_sensor_origin_)
290  {
291  vpx_ = input_->sensor_origin_.coeff (0);
292  vpy_ = input_->sensor_origin_.coeff (1);
293  vpz_ = input_->sensor_origin_.coeff (2);
294  }
295  }
296 
297  /** \brief Set the viewpoint.
298  * \param vpx the X coordinate of the viewpoint
299  * \param vpy the Y coordinate of the viewpoint
300  * \param vpz the Z coordinate of the viewpoint
301  */
302  inline void
303  setViewPoint (float vpx, float vpy, float vpz)
304  {
305  vpx_ = vpx;
306  vpy_ = vpy;
307  vpz_ = vpz;
308  use_sensor_origin_ = false;
309  }
310 
311  /** \brief Get the viewpoint.
312  * \param [out] vpx x-coordinate of the view point
313  * \param [out] vpy y-coordinate of the view point
314  * \param [out] vpz z-coordinate of the view point
315  * \note this method returns the currently used viewpoint for normal flipping.
316  * If the viewpoint is set manually using the setViewPoint method, this method will return the set view point coordinates.
317  * If an input cloud is set, it will return the sensor origin otherwise it will return the origin (0, 0, 0)
318  */
319  inline void
320  getViewPoint (float &vpx, float &vpy, float &vpz)
321  {
322  vpx = vpx_;
323  vpy = vpy_;
324  vpz = vpz_;
325  }
326 
327  /** \brief sets whether the sensor origin or a user given viewpoint should be used. After this method, the
328  * normal estimation method uses the sensor origin of the input cloud.
329  * to use a user defined view point, use the method setViewPoint
330  */
331  inline void
333  {
334  use_sensor_origin_ = true;
335  if (input_)
336  {
337  vpx_ = input_->sensor_origin_.coeff (0);
338  vpy_ = input_->sensor_origin_.coeff (1);
339  vpz_ = input_->sensor_origin_.coeff (2);
340  }
341  else
342  {
343  vpx_ = 0;
344  vpy_ = 0;
345  vpz_ = 0;
346  }
347  }
348 
349  protected:
350  /** \brief Estimate normals for all points given in <setInputCloud (), setIndices ()> using the surface in
351  * setSearchSurface () and the spatial locator in setSearchMethod ()
352  * \note In situations where not enough neighbors are found, the normal and curvature values are set to -1.
353  * \param output the resultant point cloud model dataset that contains surface normals and curvatures
354  */
355  void
356  computeFeature (PointCloudOut &output);
357 
358  /** \brief Values describing the viewpoint ("pinhole" camera model assumed). For per point viewpoints, inherit
359  * from NormalEstimation and provide your own computeFeature (). By default, the viewpoint is set to 0,0,0. */
360  float vpx_, vpy_, vpz_;
361 
362  /** \brief Placeholder for the 3x3 covariance matrix at each surface patch. */
364 
365  /** \brief 16-bytes aligned placeholder for the XYZ centroid of a surface patch. */
366  Eigen::Vector4f xyz_centroid_;
367 
368  /** whether the sensor origin of the input cloud or a user given viewpoint should be used.*/
370 
371  public:
372  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
373  };
374 }
375 
376 #ifdef PCL_NO_PRECOMPILE
377 #include <pcl/features/impl/normal_3d.hpp>
378 #endif
379 
380 #endif //#ifndef PCL_NORMAL_3D_H_
381