Point Cloud Library (PCL)  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
normal_3d.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2011, Willow Garage, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  * $Id: normal_3d.h 3755 2011-12-31 23:45:30Z rusu $
00037  *
00038  */
00039 
00040 #ifndef PCL_NORMAL_3D_H_
00041 #define PCL_NORMAL_3D_H_
00042 
00043 #include <pcl/features/feature.h>
00044 
00045 namespace pcl
00046 {
00057   template <typename PointT> inline void 
00058   computePointNormal (const pcl::PointCloud<PointT> &cloud, 
00059                       Eigen::Vector4f &plane_parameters, float &curvature)
00060   {
00061     if (cloud.points.empty ())
00062     {
00063       plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
00064       curvature = std::numeric_limits<float>::quiet_NaN ();
00065       return;
00066     }
00067     // Placeholder for the 3x3 covariance matrix at each surface patch
00068     EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix;
00069     // 16-bytes aligned placeholder for the XYZ centroid of a surface patch
00070     Eigen::Vector4f xyz_centroid;
00071 
00072     // Estimate the XYZ centroid
00073     compute3DCentroid (cloud, xyz_centroid);
00074 
00075     // Compute the 3x3 covariance matrix
00076     computeCovarianceMatrix (cloud, xyz_centroid, covariance_matrix);
00077 
00078     // Get the plane normal and surface curvature
00079     solvePlaneParameters (covariance_matrix, xyz_centroid, plane_parameters, curvature);
00080   }
00081 
00093   template <typename PointT> inline void 
00094   computePointNormal (const pcl::PointCloud<PointT> &cloud, const std::vector<int> &indices, 
00095                       Eigen::Vector4f &plane_parameters, float &curvature)
00096   {
00097     if (indices.empty ())
00098     {
00099       plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
00100       curvature = std::numeric_limits<float>::quiet_NaN ();
00101       return;
00102     }
00103     // Placeholder for the 3x3 covariance matrix at each surface patch
00104     EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix;
00105     // 16-bytes aligned placeholder for the XYZ centroid of a surface patch
00106     Eigen::Vector4f xyz_centroid;
00107 
00108     // Estimate the XYZ centroid
00109     compute3DCentroid (cloud, indices, xyz_centroid);
00110 
00111     // Compute the 3x3 covariance matrix
00112     computeCovarianceMatrix (cloud, indices, xyz_centroid, covariance_matrix);
00113 
00114     // Get the plane normal and surface curvature
00115     solvePlaneParameters (covariance_matrix, xyz_centroid, plane_parameters, curvature);
00116   }
00117 
00126   template <typename PointT> inline void 
00127   flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z, 
00128                               Eigen::Vector4f &normal)
00129   {
00130     Eigen::Vector4f vp (vp_x, vp_y, vp_z, 0);
00131     // See if we need to flip any plane normals
00132     vp -= point.getVector4fMap ();
00133     vp[3] = 0;  // enforce the last coordinate
00134 
00135     // Dot product between the (viewpoint - point) and the plane normal
00136     float cos_theta = vp.dot (normal);
00137 
00138     // Flip the plane normal
00139     if (cos_theta < 0)
00140     {
00141       normal *= -1;
00142       normal[3] = 0;  // enforce the last coordinate;
00143       // Hessian form (D = nc . p_plane (centroid here) + p)
00144       normal[3] = -1 * normal.dot (point.getVector4fMap ());
00145     }
00146   }
00147 
00158   template <typename PointT> inline void 
00159   flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z, 
00160                               float &nx, float &ny, float &nz)
00161   {
00162     // See if we need to flip any plane normals
00163     vp_x -= point.x;
00164     vp_y -= point.y;
00165     vp_z -= point.z;
00166 
00167     // Dot product between the (viewpoint - point) and the plane normal
00168     float cos_theta = (vp_x * nx + vp_y * ny + vp_z * nz);
00169 
00170     // Flip the plane normal
00171     if (cos_theta < 0)
00172     {
00173       nx *= -1;
00174       ny *= -1;
00175       nz *= -1;
00176     }
00177   }
00178 
00187   template <typename PointInT, typename PointOutT>
00188   class NormalEstimation: public Feature<PointInT, PointOutT>
00189   {
00190     public:
00191       using Feature<PointInT, PointOutT>::feature_name_;
00192       using Feature<PointInT, PointOutT>::getClassName;
00193       using Feature<PointInT, PointOutT>::indices_;
00194       using Feature<PointInT, PointOutT>::input_;
00195       using Feature<PointInT, PointOutT>::surface_;
00196       using Feature<PointInT, PointOutT>::k_;
00197       using Feature<PointInT, PointOutT>::search_radius_;
00198       using Feature<PointInT, PointOutT>::search_parameter_;
00199 
00200       typedef typename Feature<PointInT, PointOutT>::PointCloudOut PointCloudOut;
00201 
00203       NormalEstimation () : vpx_ (0), vpy_ (0), vpz_ (0) 
00204       {
00205         feature_name_ = "NormalEstimation";
00206       };
00207 
00218       inline void 
00219       computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices, Eigen::Vector4f &plane_parameters, float &curvature)
00220       {
00221         if (indices.empty ())
00222         {
00223           plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
00224           curvature = std::numeric_limits<float>::quiet_NaN ();
00225           return;
00226         }
00227         // Estimate the XYZ centroid
00228         compute3DCentroid (cloud, indices, xyz_centroid_);
00229 
00230         // Compute the 3x3 covariance matrix
00231         computeCovarianceMatrix (cloud, indices, xyz_centroid_, covariance_matrix_);
00232 
00233         // Get the plane normal and surface curvature
00234         solvePlaneParameters (covariance_matrix_, xyz_centroid_, plane_parameters, curvature);
00235       }
00236 
00249       inline void 
00250       computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices, float &nx, float &ny, float &nz, float &curvature)
00251       {
00252         if (indices.empty ())
00253         {
00254           nx = ny = nz = curvature = std::numeric_limits<float>::quiet_NaN ();
00255           return;
00256         }
00257         // Estimate the XYZ centroid
00258         compute3DCentroid (cloud, indices, xyz_centroid_);
00259 
00260         // Compute the 3x3 covariance matrix
00261         computeCovarianceMatrix (cloud, indices, xyz_centroid_, covariance_matrix_);
00262 
00263         // Get the plane normal and surface curvature
00264         solvePlaneParameters (covariance_matrix_, nx, ny, nz, curvature);
00265       }
00266 
00272       inline void
00273       setViewPoint (float vpx, float vpy, float vpz)
00274       {
00275         vpx_ = vpx;
00276         vpy_ = vpy;
00277         vpz_ = vpz;
00278       }
00279 
00281       inline void
00282       getViewPoint (float &vpx, float &vpy, float &vpz)
00283       {
00284         vpx = vpx_;
00285         vpy = vpy_;
00286         vpz = vpz_;
00287       }
00288 
00289     protected:
00295       void 
00296       computeFeature (PointCloudOut &output);
00297 
00300       float vpx_, vpy_, vpz_;
00301 
00303       EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix_;
00304 
00306       Eigen::Vector4f xyz_centroid_;
00307 
00308     private:
00312       void 
00313       computeFeature (pcl::PointCloud<Eigen::MatrixXf> &output) {}
00314    };
00315 
00324   template <typename PointInT>
00325   class NormalEstimation<PointInT, Eigen::MatrixXf>: public NormalEstimation<PointInT, pcl::Normal>
00326   {
00327     public:
00328       using NormalEstimation<PointInT, pcl::Normal>::indices_;
00329       using NormalEstimation<PointInT, pcl::Normal>::input_;
00330       using NormalEstimation<PointInT, pcl::Normal>::surface_;
00331       using NormalEstimation<PointInT, pcl::Normal>::k_;
00332       using NormalEstimation<PointInT, pcl::Normal>::search_parameter_;
00333       using NormalEstimation<PointInT, pcl::Normal>::vpx_;
00334       using NormalEstimation<PointInT, pcl::Normal>::vpy_;
00335       using NormalEstimation<PointInT, pcl::Normal>::vpz_;
00336       using NormalEstimation<PointInT, pcl::Normal>::computePointNormal;
00337       using NormalEstimation<PointInT, pcl::Normal>::compute;
00338 
00339     private:
00345       void 
00346       computeFeature (pcl::PointCloud<Eigen::MatrixXf> &output);
00347 
00351       void 
00352       compute (pcl::PointCloud<pcl::Normal> &output) {}
00353   };
00354 }
00355 
00356 #endif  //#ifndef PCL_NORMAL_3D_H_
00357 
00358 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines