00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
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
00099
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
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
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
00215 bool tryLoad(const double &cx, const double &cy, const double &cz, LazyGrid<PointT> *&grid);
00216
00218
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
00287 void initializeGrids();
00288
00289
00290 public:
00291 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00292 pcl::PointCloud<PointT> conflictPoints;
00293
00294
00295 };
00296
00297 }
00298
00299 #include <ndt_map/impl/ndt_map_hmt.hpp>
00300
00301 #endif