ndt_map_hmt.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, AASS Research Center, Orebro University.
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 AASS Research Center 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  */
00035 #ifndef NDT_MAP_HMT_HH
00036 #define NDT_MAP_HMT_HH
00037 
00038 #include <ndt_map/ndt_map.h>
00039 #include <ndt_map/spatial_index.h>
00040 #include <ndt_map/ndt_cell.h>
00041 #include <ndt_map/depth_camera.h>
00042 #include <ndt_map/lazy_grid.h>
00043 
00044 #include <cstdlib>
00045 #include <cv.h>
00046 #include <sys/stat.h>
00047 #include <sys/dir.h>
00048 
00049 #include <pcl/point_cloud.h>
00050 #include <pcl/point_types.h>
00051 
00052 namespace lslgeneric
00053 {
00054 
00064 template <typename PointT>
00065 class NDTMapHMT : public NDTMap<PointT>
00066 {
00067 public:
00068 
00075     NDTMapHMT(double resolution_, float cenx, float ceny, float cenz, float sizex, float sizey, float sizez, double max_range, std::string directory, bool _saveOnDelete=false)
00076     {
00077         max_range_ = max_range;
00078         grids_init=false;
00079         my_directory = directory;
00080         resolution = resolution_;
00081 
00082         //check if we can stat my_directory and try to create it if we can't
00083         DIR *mdir = opendir(my_directory.c_str());
00084         if(mdir == NULL) {
00085             std::cerr<<"Error accessing map directory "<<my_directory<<" will attempt to recover:\n";
00086             int res = mkdir(my_directory.c_str(),S_IRWXU);
00087             if (res<0) {
00088                 std::cerr<<"CRITICAL! FAILED to create directory\n";
00089                 return;
00090             }
00091         } else {
00092             closedir(mdir);
00093         }
00094 
00095         LazyGrid<PointT> *lz = new LazyGrid<PointT>(resolution);
00096         this->index_ = lz;
00097 
00098         //this is used to prevent memory de-allocation of the *si
00099         //si was allocated outside the NDT class and should be deallocated outside
00100         this->isFirstLoad_=true;
00101 
00102         NDTCell<PointT> *ptCell = new NDTCell<PointT>();
00103         this->index_->setCellType(ptCell);
00104         delete ptCell;
00105         this->index_->setCenter(cenx,ceny,cenz);
00106         this->index_->setSize(sizex,sizey,sizez);
00107         this->map_sizex = sizex;
00108         this->map_sizey = sizey;
00109         this->map_sizez = sizez;
00110         this->is3D=true;
00111         lz->initializeAll();
00112         this->guess_size_ = false;
00113         this->saveOnDelete = _saveOnDelete;
00114 
00115         //create the grid of LazyGrids and set their centers
00116         initializeGrids();
00117     }
00118     NDTMapHMT(const NDTMapHMT<PointT>& other)
00119     {
00120         if(other.index_ != NULL)
00121         {
00122             this->index_ = other.index_->copy();
00123             isFirstLoad_ = false;
00124             this->max_range_ = other.max_range_;
00125             this->grids_init = false;
00126             this->saveOnDelete = other.saveOnDelete;
00127             //TODO copy all LazyGrids
00128         }
00129     }
00133     virtual ~NDTMapHMT()
00134     {
00135         if(saveOnDelete) {
00136             this->writeTo();
00137         }
00138         for(int i=0; i<3; ++i) {
00139             for(int j=0; j<3; ++j) {
00140                 if(grid_[i][j]!=NULL) {
00141                     delete grid_[i][j];
00142                 }
00143             }
00144         }
00145     }
00155     virtual void loadPointCloud(const pcl::PointCloud<PointT> &pc, double range_limit = -1);
00167     virtual void addPointCloud(const Eigen::Vector3d &origin, const pcl::PointCloud<PointT> &pc, double classifierTh=0.06, 
00168                                 double maxz = 100.0, double sensor_noise = 0.25, double occupancy_limit = 255);
00169 
00185     virtual void addPointCloudMeanUpdate(const Eigen::Vector3d &origin, 
00186             const pcl::PointCloud<PointT> &pc, 
00187             const Eigen::Vector3d &localmapsize,
00188             unsigned int maxnumpoints = 1e9, float occupancy_limit=255 ,double maxz = 100.0, double sensor_noise = 0.25);
00189 
00190 
00199     virtual void addDistributionToCell(const Eigen::Matrix3d &ucov,const Eigen::Vector3d &umean, unsigned int numpointsindistribution, 
00200                                                                                                                         float r=0, float g=0,float b=0, unsigned int maxnumpoints=1e9, float max_occupancy=1024);
00201 
00207     virtual void computeNDTCells(int cellupdatemode = CELL_UPDATE_MODE_SAMPLE_VARIANCE, unsigned int maxnumpoints = 1e9, float occupancy_limit=255, Eigen::Vector3d origin = Eigen::Vector3d(0,0,0), double sensor_noise=0.1);
00208 
00212     int writeTo();
00213     int loadFrom();
00214     //cehck if there is a grid at this center and try to load it
00215     bool tryLoad(const double &cx, const double &cy, const double &cz, LazyGrid<PointT> *&grid);
00216 
00218     //computes the likelihood of a single observation
00219     virtual double getLikelihoodForPoint(PointT pt);
00220 
00222     virtual bool getCellAtPoint(const PointT &refPoint, NDTCell<PointT> *&cell);
00223 
00224     virtual bool getCentroid(double &cx, double &cy, double &cz)
00225     {
00226         LazyGrid<PointT> *lz = grid_[1][1];
00227         if(lz == NULL) return false;
00228         lz->getCenter(cx, cy, cz);
00229         return true;
00230     }
00235     virtual bool getCellForPoint(const PointT &refPoint, NDTCell<PointT> *&cell, bool checkForGaussian=true) const;
00240     virtual std::vector<NDTCell<PointT>*> getCellsForPoint(const PointT pt, int n_neighbours, bool checkForGaussian=true) const;
00244     virtual std::vector<NDTCell<PointT>*> getInitializedCellsForPoint(const PointT pt) const;
00245 
00249     virtual std::vector<NDTCell<PointT>*> pseudoTransformNDT(Eigen::Transform<double,3,Eigen::Affine,Eigen::ColMajor> T);
00250 
00259     virtual std::vector<lslgeneric::NDTCell<PointT>*> getAllCells() const;
00264     virtual std::vector<lslgeneric::NDTCell<PointT>*> getAllInitializedCells();
00265 
00266     int numberOfActiveCells();
00267     void setInsertPosition(const Eigen::Vector3d &newPos);
00268     bool tryLoadPosition(const Eigen::Vector3d &newPos);
00269 protected:
00270     bool is3D;
00271     bool saveOnDelete;
00272     SpatialIndex<PointT> *index_;
00273     bool isFirstLoad_;
00274     float map_sizex;
00275     float map_sizey;
00276     float map_sizez;
00277     float centerx,centery,centerz;
00278     bool grids_init;
00279     Eigen::Vector3d last_insert;
00280     std::string my_directory;
00281     double resolution;
00282     std::set<NDTCell<PointT>*> update_set;
00283 
00284     double max_range_;
00285     lslgeneric::LazyGrid<PointT>* grid_[3][3];
00286     //helper functions
00287     void initializeGrids();
00288     
00289 
00290 public:
00291     EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00292     pcl::PointCloud<PointT> conflictPoints; 
00293                 
00294 
00295 };
00296 
00297 } // end namespace
00298 
00299 #include <ndt_map/impl/ndt_map_hmt.hpp>
00300 
00301 #endif


ndt_map
Author(s): Todor Stoyanov, Jari Saarinen, Henrik Andreasson
autogenerated on Mon Oct 6 2014 03:18:54