Point Cloud Library (PCL)  1.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
grid_projection.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 Willow Garage, Inc. 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: grid_projection.h 4702 2012-02-23 09:39:33Z gedikli $
00035  *
00036  */
00037 
00038 #ifndef PCL_SURFACE_GRID_PROJECTION_H_
00039 #define PCL_SURFACE_GRID_PROJECTION_H_
00040 
00041 #include <pcl/surface/reconstruction.h>
00042 #include <boost/dynamic_bitset/dynamic_bitset.hpp>
00043 #include <boost/unordered_map.hpp>
00044 
00045 namespace pcl
00046 {
00048   const int I_SHIFT_EP[12][2] = {
00049     {0, 4}, {1, 5}, {2, 6}, {3, 7}, 
00050     {0, 1}, {1, 2}, {2, 3}, {3, 0},
00051     {4, 5}, {5, 6}, {6, 7}, {7, 4}
00052   };
00053 
00054   const int I_SHIFT_PT[4] = {
00055     0, 4, 5, 7
00056   };
00057 
00058   const int I_SHIFT_EDGE[3][2] = {
00059     {0,1}, {1,3}, {1,2}
00060   };
00061 
00062 
00072   template <typename PointNT>
00073   class GridProjection : public SurfaceReconstruction<PointNT>
00074   {
00075     public:
00076       using SurfaceReconstruction<PointNT>::input_;
00077       using SurfaceReconstruction<PointNT>::tree_;
00078 
00079       typedef typename pcl::PointCloud<PointNT>::Ptr PointCloudPtr;
00080 
00081       typedef typename pcl::KdTree<PointNT> KdTree;
00082       typedef typename pcl::KdTree<PointNT>::Ptr KdTreePtr;
00083 
00085       struct Leaf
00086       {
00087         std::vector<int> data_indices;
00088         Eigen::Vector4f pt_on_surface; 
00089         Eigen::Vector3f vect_at_grid_pt;
00090       };
00091 
00092       typedef boost::unordered_map<int, Leaf, boost::hash<int>, std::equal_to<int>, Eigen::aligned_allocator<int> > HashMap;
00093 
00095       GridProjection ();
00096 
00100       GridProjection (double in_resolution);
00101 
00103       ~GridProjection ();
00104 
00108       inline void 
00109       setResolution (double resolution)
00110       {
00111         leaf_size_ = resolution;
00112       }
00113 
00114       inline double 
00115       getResolution () const
00116       {
00117         return (leaf_size_);
00118       }
00119 
00129       inline void 
00130       setPaddingSize (int padding_size)
00131       {
00132         padding_size_ = padding_size;
00133       }
00134       inline int 
00135       getPaddingSize () const
00136       {
00137         return (padding_size_);
00138       }
00139 
00144       inline void 
00145       setNearestNeighborNum (int k)
00146       {
00147         k_ = k;
00148       }
00149       inline int 
00150       getNearestNeighborNum () const
00151       {
00152         return (k_);
00153       }
00154 
00159       inline void 
00160       setMaxBinarySearchLevel (int max_binary_search_level)
00161       {
00162         max_binary_search_level_ = max_binary_search_level;
00163       }
00164       inline int 
00165       getMaxBinarySearchLevel () const
00166       {
00167         return (max_binary_search_level_);
00168       }
00169 
00171       inline const HashMap& 
00172       getCellHashMap () const
00173       {
00174         return (cell_hash_map_);
00175       }
00176 
00177       inline const std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> >& 
00178       getVectorAtDataPoint () const
00179       {
00180         return (vector_at_data_point_);
00181       }
00182       
00183       inline const std::vector<Eigen::Vector4f, Eigen::aligned_allocator<Eigen::Vector4f> >& 
00184       getSurface () const
00185       {
00186         return (surface_);
00187       }
00188 
00189     protected:
00193       void 
00194       getBoundingBox ();
00195 
00199       bool
00200       reconstructPolygons (std::vector<pcl::Vertices> &polygons);
00201 
00211       void 
00212       performReconstruction (pcl::PolygonMesh &output);
00213 
00224       void 
00225       performReconstruction (pcl::PointCloud<PointNT> &points, 
00226                              std::vector<pcl::Vertices> &polygons);
00227 
00233       void 
00234       scaleInputDataPoint (double scale_factor);
00235 
00241       inline void 
00242       getCellIndex (const Eigen::Vector4f &p, Eigen::Vector3i& index) const
00243       {
00244         for (int i = 0; i < 3; ++i)
00245           index[i] = (p[i] - min_p_(i))/leaf_size_;
00246       }
00247 
00253       inline void
00254       getCellCenterFromIndex (const Eigen::Vector3i &index, Eigen::Vector4f &center) const
00255       {
00256         for (int i = 0; i < 3; ++i)
00257           center[i] = min_p_[i] + index[i] * leaf_size_ + leaf_size_/2;
00258       }
00259 
00264       void 
00265       getVertexFromCellCenter (const Eigen::Vector4f &cell_center, 
00266                                std::vector<Eigen::Vector4f, Eigen::aligned_allocator<Eigen::Vector4f> > &pts) const;
00267 
00272       inline int 
00273       getIndexIn1D (const Eigen::Vector3i &index) const
00274       {
00275         //assert(data_size_ > 0);
00276         return (index[0] * data_size_ * data_size_ + 
00277                 index[1] * data_size_ + index[2]);
00278       }
00279 
00285       inline void 
00286       getIndexIn3D (int index_1d, Eigen::Vector3i& index_3d) const
00287       {
00288         //assert(data_size_ > 0);
00289         index_3d[0] = index_1d / (data_size_ * data_size_);
00290         index_1d -= index_3d[0] * data_size_ * data_size_;
00291         index_3d[1] = index_1d / data_size_;
00292         index_1d -= index_3d[1] * data_size_;
00293         index_3d[2] = index_1d;
00294       }
00295 
00300       void 
00301       fillPad (const Eigen::Vector3i &index);
00302 
00307       void 
00308       getDataPtsUnion (const Eigen::Vector3i &index, std::vector <int> &pt_union_indices);
00309 
00316       void 
00317       createSurfaceForCell (const Eigen::Vector3i &index, std::vector <int> &pt_union_indices);
00318 
00319 
00327       void
00328       getProjection (const Eigen::Vector4f &p, std::vector<int> &pt_union_indices, Eigen::Vector4f &projection);
00329 
00337       void 
00338       getProjectionWithPlaneFit (const Eigen::Vector4f &p, 
00339                                  std::vector<int> &pt_union_indices, 
00340                                  Eigen::Vector4f &projection);
00341 
00342 
00348       void
00349       getVectorAtPoint (const Eigen::Vector4f &p, 
00350                         std::vector <int> &pt_union_indices, Eigen::Vector3f &vo);
00351 
00359       void
00360       getVectorAtPointKNN (const Eigen::Vector4f &p, 
00361                            std::vector<int> &k_indices, 
00362                            std::vector<float> &k_squared_distances,
00363                            Eigen::Vector3f &vo);
00364 
00369       double 
00370       getMagAtPoint (const Eigen::Vector4f &p, const std::vector <int> &pt_union_indices);
00371 
00377       double 
00378       getD1AtPoint (const Eigen::Vector4f &p, const Eigen::Vector3f &vec, 
00379                     const std::vector <int> &pt_union_indices);
00380 
00386       double 
00387       getD2AtPoint (const Eigen::Vector4f &p, const Eigen::Vector3f &vec, 
00388                     const std::vector <int> &pt_union_indices);
00389 
00398       bool 
00399       isIntersected (const std::vector<Eigen::Vector4f, Eigen::aligned_allocator<Eigen::Vector4f> > &end_pts, 
00400                      std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > &vect_at_end_pts, 
00401                      std::vector <int> &pt_union_indices);
00402 
00411       void
00412       findIntersection (int level, 
00413                         const std::vector<Eigen::Vector4f, Eigen::aligned_allocator<Eigen::Vector4f> > &end_pts, 
00414                         const std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > &vect_at_end_pts, 
00415                         const Eigen::Vector4f &start_pt, 
00416                         std::vector<int> &pt_union_indices,
00417                         Eigen::Vector4f &intersection);
00418 
00433       void
00434       storeVectAndSurfacePoint (int index_1d, const Eigen::Vector3i &index_3d, 
00435                                 std::vector<int> &pt_union_indices, const Leaf &cell_data);
00436 
00450       void 
00451       storeVectAndSurfacePointKNN (int index_1d, const Eigen::Vector3i &index_3d, const Leaf &cell_data);
00452 
00453     private:
00455       HashMap cell_hash_map_;
00456 
00458       Eigen::Vector4f min_p_, max_p_;
00459 
00461       double leaf_size_;
00462 
00464       double gaussian_scale_;
00465 
00467       int data_size_;
00468 
00470       int max_binary_search_level_;
00471 
00473       int k_;
00474 
00476       int padding_size_;
00477 
00479       PointCloudPtr data_;
00480 
00482       std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > vector_at_data_point_;
00483       
00485       std::vector<Eigen::Vector4f, Eigen::aligned_allocator<Eigen::Vector4f> > surface_;
00486 
00488       boost::dynamic_bitset<> occupied_cell_list_;
00489 
00491       std::string getClassName () const { return ("GridProjection"); }
00492 
00493     public:
00494       EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00495   };
00496 }
00497 
00498 #endif  // PCL_SURFACE_GRID_PROJECTION_H_
00499