Point Cloud Library (PCL)  1.5.1
 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 4702 2012-02-23 09:39:33Z gedikli $
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     // Placeholder for the 3x3 covariance matrix at each surface patch
00062     EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix;
00063     // 16-bytes aligned placeholder for the XYZ centroid of a surface patch
00064     Eigen::Vector4f xyz_centroid;
00065 
00066     if (computeMeanAndCovarianceMatrix (cloud, covariance_matrix, xyz_centroid) == 0)
00067     {
00068       plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
00069       curvature = std::numeric_limits<float>::quiet_NaN ();
00070       return;
00071     }
00072 
00073     // Get the plane normal and surface curvature
00074     solvePlaneParameters (covariance_matrix, xyz_centroid, plane_parameters, curvature);
00075   }
00076 
00088   template <typename PointT> inline void
00089   computePointNormal (const pcl::PointCloud<PointT> &cloud, const std::vector<int> &indices,
00090                       Eigen::Vector4f &plane_parameters, float &curvature)
00091   {
00092     // Placeholder for the 3x3 covariance matrix at each surface patch
00093     EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix;
00094     // 16-bytes aligned placeholder for the XYZ centroid of a surface patch
00095     Eigen::Vector4f xyz_centroid;
00096     if (computeMeanAndCovarianceMatrix (cloud, indices, covariance_matrix, xyz_centroid) == 0)
00097     {
00098       plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
00099       curvature = std::numeric_limits<float>::quiet_NaN ();
00100       return;
00101     }
00102     // Get the plane normal and surface curvature
00103     solvePlaneParameters (covariance_matrix, xyz_centroid, plane_parameters, curvature);
00104   }
00105 
00114   template <typename PointT> inline void
00115   flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z,
00116                               Eigen::Vector4f &normal)
00117   {
00118     Eigen::Vector4f vp (vp_x, vp_y, vp_z, 0);
00119     // See if we need to flip any plane normals
00120     vp -= point.getVector4fMap ();
00121     vp[3] = 0;  // enforce the last coordinate
00122 
00123     // Dot product between the (viewpoint - point) and the plane normal
00124     float cos_theta = vp.dot (normal);
00125 
00126     // Flip the plane normal
00127     if (cos_theta < 0)
00128     {
00129       normal *= -1;
00130       normal[3] = 0;  // enforce the last coordinate;
00131       // Hessian form (D = nc . p_plane (centroid here) + p)
00132       normal[3] = -1 * normal.dot (point.getVector4fMap ());
00133     }
00134   }
00135 
00146   template <typename PointT> inline void
00147   flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z,
00148                               float &nx, float &ny, float &nz)
00149   {
00150     // See if we need to flip any plane normals
00151     vp_x -= point.x;
00152     vp_y -= point.y;
00153     vp_z -= point.z;
00154 
00155     // Dot product between the (viewpoint - point) and the plane normal
00156     float cos_theta = (vp_x * nx + vp_y * ny + vp_z * nz);
00157 
00158     // Flip the plane normal
00159     if (cos_theta < 0)
00160     {
00161       nx *= -1;
00162       ny *= -1;
00163       nz *= -1;
00164     }
00165   }
00166 
00176   template <typename PointInT, typename PointOutT>
00177   class NormalEstimation: public Feature<PointInT, PointOutT>
00178   {
00179     public:
00180       using Feature<PointInT, PointOutT>::feature_name_;
00181       using Feature<PointInT, PointOutT>::getClassName;
00182       using Feature<PointInT, PointOutT>::indices_;
00183       using Feature<PointInT, PointOutT>::input_;
00184       using Feature<PointInT, PointOutT>::surface_;
00185       using Feature<PointInT, PointOutT>::k_;
00186       using Feature<PointInT, PointOutT>::search_radius_;
00187       using Feature<PointInT, PointOutT>::search_parameter_;
00188 
00189       typedef typename Feature<PointInT, PointOutT>::PointCloudOut PointCloudOut;
00190 
00192       NormalEstimation () : vpx_ (0), vpy_ (0), vpz_ (0)
00193       {
00194         feature_name_ = "NormalEstimation";
00195       };
00196 
00207       inline void
00208       computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices, Eigen::Vector4f &plane_parameters, float &curvature)
00209       {
00210         if (computeMeanAndCovarianceMatrix (cloud, indices, covariance_matrix_, xyz_centroid_) == 0)
00211         {
00212           plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
00213           curvature = std::numeric_limits<float>::quiet_NaN ();
00214           return;
00215         }
00216 
00217         // Get the plane normal and surface curvature
00218         solvePlaneParameters (covariance_matrix_, xyz_centroid_, plane_parameters, curvature);
00219       }
00220 
00233       inline void
00234       computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices, float &nx, float &ny, float &nz, float &curvature)
00235       {
00236         if (computeMeanAndCovarianceMatrix (cloud, indices, covariance_matrix_, xyz_centroid_) == 0)
00237         {
00238           nx = ny = nz = curvature = std::numeric_limits<float>::quiet_NaN ();
00239           return;
00240         }
00241 
00242         // Get the plane normal and surface curvature
00243         solvePlaneParameters (covariance_matrix_, nx, ny, nz, curvature);
00244       }
00245 
00251       inline void
00252       setViewPoint (float vpx, float vpy, float vpz)
00253       {
00254         vpx_ = vpx;
00255         vpy_ = vpy;
00256         vpz_ = vpz;
00257       }
00258 
00260       inline void
00261       getViewPoint (float &vpx, float &vpy, float &vpz)
00262       {
00263         vpx = vpx_;
00264         vpy = vpy_;
00265         vpz = vpz_;
00266       }
00267 
00268     protected:
00274       void
00275       computeFeature (PointCloudOut &output);
00276 
00279       float vpx_, vpy_, vpz_;
00280 
00282       EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix_;
00283 
00285       Eigen::Vector4f xyz_centroid_;
00286 
00287     private:
00291       void
00292       computeFeatureEigen (pcl::PointCloud<Eigen::MatrixXf> &output) {}
00293    };
00294 
00303   template <typename PointInT>
00304   class NormalEstimation<PointInT, Eigen::MatrixXf>: public NormalEstimation<PointInT, pcl::Normal>
00305   {
00306     public:
00307       using NormalEstimation<PointInT, pcl::Normal>::indices_;
00308       using NormalEstimation<PointInT, pcl::Normal>::input_;
00309       using NormalEstimation<PointInT, pcl::Normal>::surface_;
00310       using NormalEstimation<PointInT, pcl::Normal>::k_;
00311       using NormalEstimation<PointInT, pcl::Normal>::search_parameter_;
00312       using NormalEstimation<PointInT, pcl::Normal>::vpx_;
00313       using NormalEstimation<PointInT, pcl::Normal>::vpy_;
00314       using NormalEstimation<PointInT, pcl::Normal>::vpz_;
00315       using NormalEstimation<PointInT, pcl::Normal>::computePointNormal;
00316       using NormalEstimation<PointInT, pcl::Normal>::compute;
00317 
00318     private:
00324       void
00325       computeFeatureEigen (pcl::PointCloud<Eigen::MatrixXf> &output);
00326 
00330       void
00331       compute (pcl::PointCloud<pcl::Normal> &output) {}
00332   };
00333 }
00334 
00335 #endif  //#ifndef PCL_NORMAL_3D_H_
00336 
00337