normal_refinement.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the copyright holder(s) nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * $Id$
00035  *
00036  */
00037 
00038 #ifndef PCL_FILTERS_NORMAL_REFINEMENT_H_
00039 #define PCL_FILTERS_NORMAL_REFINEMENT_H_
00040 
00041 #include <pcl/filters/filter.h>
00042 
00043 namespace pcl
00044 {
00054   template <typename NormalT> inline std::vector<float>
00055   assignNormalWeights (const PointCloud<NormalT>&,
00056                        int,
00057                        const std::vector<int>& k_indices,
00058                        const std::vector<float>& k_sqr_distances)
00059   {
00060     // Check inputs
00061     if (k_indices.size () != k_sqr_distances.size ())
00062       PCL_ERROR("[pcl::assignNormalWeights] inequal size of neighbor indices and distances!\n");
00063     
00064     // TODO: For now we use uniform weights
00065     return (std::vector<float> (k_indices.size (), 1.0f));
00066   }
00067   
00080   template <typename NormalT> inline bool
00081   refineNormal (const PointCloud<NormalT>& cloud,
00082                 int index,
00083                 const std::vector<int>& k_indices,
00084                 const std::vector<float>& k_sqr_distances,
00085                 NormalT& point)
00086   {
00087     // Start by zeroing result
00088     point.normal_x = 0.0f;
00089     point.normal_y = 0.0f;
00090     point.normal_z = 0.0f;
00091     
00092     // Check inputs
00093     if (k_indices.size () != k_sqr_distances.size ())
00094     {
00095       PCL_ERROR("[pcl::refineNormal] inequal size of neighbor indices and distances!\n");
00096       return (false);
00097     }
00098     
00099     // Get weights
00100     const std::vector<float> weights = assignNormalWeights (cloud, index, k_indices, k_sqr_distances);
00101     
00102     // Loop over all neighbors and accumulate sum of normal components
00103     float nx = 0.0f;
00104     float ny = 0.0f;
00105     float nz = 0.0f;
00106     for (unsigned int i = 0; i < k_indices.size (); ++i) {
00107       // Neighbor
00108       const NormalT& pointi = cloud[k_indices[i]];
00109       
00110       // Accumulate if not NaN
00111       if (pcl_isfinite (pointi.normal_x) && pcl_isfinite (pointi.normal_y) && pcl_isfinite (pointi.normal_z))
00112       {
00113         const float& weighti = weights[i];
00114         nx += weighti * pointi.normal_x;
00115         ny += weighti * pointi.normal_y;
00116         nz += weighti * pointi.normal_z;
00117       }
00118     }
00119     
00120     // Normalize if norm valid and non-zero
00121     const float norm = sqrtf (nx * nx + ny * ny + nz * nz);
00122     if (pcl_isfinite (norm) && norm > std::numeric_limits<float>::epsilon ())
00123     {
00124       point.normal_x = nx / norm;
00125       point.normal_y = ny / norm;
00126       point.normal_z = nz / norm;
00127 
00128       return (true);
00129     }
00130 
00131     return (false);
00132   }
00133   
00187   template<typename NormalT>
00188   class NormalRefinement : public Filter<NormalT>
00189   {
00190     using Filter<NormalT>::input_;
00191     using Filter<NormalT>::filter_name_;
00192     using Filter<NormalT>::getClassName;
00193 
00194     typedef typename Filter<NormalT>::PointCloud PointCloud;
00195     typedef typename PointCloud::Ptr PointCloudPtr;
00196     typedef typename PointCloud::ConstPtr PointCloudConstPtr;
00197 
00198     public:
00201       NormalRefinement () :
00202         Filter<NormalT>::Filter ()
00203       {
00204         filter_name_ = "NormalRefinement";
00205         setMaxIterations (15);
00206         setConvergenceThreshold (0.00001f);
00207       }
00208       
00213       NormalRefinement (const std::vector< std::vector<int> >& k_indices, const std::vector< std::vector<float> >& k_sqr_distances) :
00214         Filter<NormalT>::Filter ()
00215       {
00216         filter_name_ = "NormalRefinement";
00217         setCorrespondences (k_indices, k_sqr_distances);
00218         setMaxIterations (15);
00219         setConvergenceThreshold (0.00001f);
00220       }
00221       
00226       inline void
00227       setCorrespondences (const std::vector< std::vector<int> >& k_indices, const std::vector< std::vector<float> >& k_sqr_distances)
00228       {
00229         k_indices_ = k_indices;
00230         k_sqr_distances_ = k_sqr_distances;
00231       }
00232       
00237       inline void
00238       getCorrespondences (std::vector< std::vector<int> >& k_indices, std::vector< std::vector<float> >& k_sqr_distances)
00239       {
00240         k_indices.assign (k_indices_.begin (), k_indices_.end ());
00241         k_sqr_distances.assign (k_sqr_distances_.begin (), k_sqr_distances_.end ());
00242       }
00243       
00247       inline void
00248       setMaxIterations (unsigned int max_iterations)
00249       {
00250         max_iterations_ = max_iterations;
00251       }
00252       
00256       inline unsigned int
00257       getMaxIterations ()
00258       {
00259         return max_iterations_;
00260       }
00261       
00265       inline void
00266       setConvergenceThreshold (float convergence_threshold)
00267       {
00268         convergence_threshold_ = convergence_threshold;
00269       }
00270 
00274       inline float
00275       getConvergenceThreshold ()
00276       {
00277         return convergence_threshold_;
00278       }
00279 
00280     protected:
00284       void
00285       applyFilter (PointCloud &output);
00286       
00287     private:
00289       std::vector< std::vector<int> > k_indices_;
00290       
00292       std::vector< std::vector<float> > k_sqr_distances_;
00293       
00295       unsigned int max_iterations_;
00296       
00298       float convergence_threshold_;
00299   };
00300 }
00301 
00302 #ifdef PCL_NO_PRECOMPILE
00303 #include <pcl/filters/impl/normal_refinement.hpp>
00304 #else
00305 #define PCL_INSTANTIATE_NormalRefinement(T) template class PCL_EXPORTS pcl::NormalRefinement<T>;
00306 #endif
00307 
00308 #endif 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:25:58