lazy_grid.cpp
Go to the documentation of this file.
00001 #include <cstring>
00002 #include <cstdio>
00003 #include <ndt_map/lazy_grid.h>
00004 
00005 #define JFFERR(x) std::cerr << x << std::endl; return -1;
00006 
00007 namespace lslgeneric
00008 {
00009 
00010 LazyGrid::LazyGrid(double cellSize) 
00011 {
00012     initialized = false;
00013     centerIsSet = false;
00014     sizeIsSet = false;
00015     cellSizeX = cellSizeY = cellSizeZ = cellSize;
00016 
00017 }
00018 
00019 LazyGrid::LazyGrid(LazyGrid *prot)
00020 {
00021 
00022     sizeXmeters = prot->sizeXmeters;
00023     sizeYmeters = prot->sizeYmeters;
00024     sizeZmeters = prot->sizeZmeters;
00025 
00026     cellSizeX = prot->cellSizeX;
00027     cellSizeY = prot->cellSizeY;
00028     cellSizeZ = prot->cellSizeZ;
00029 
00030     sizeX = abs(ceil(sizeXmeters/cellSizeX));
00031     sizeY = abs(ceil(sizeYmeters/cellSizeY));
00032     sizeZ = abs(ceil(sizeZmeters/cellSizeZ));
00033 
00034     centerX = prot->centerX;
00035     centerY = prot->centerY;
00036     centerZ = prot->centerZ;
00037 
00038     protoType = prot->protoType->clone();
00039     initialize();
00040 
00041 }
00042 
00043 LazyGrid::LazyGrid(double _sizeXmeters, double _sizeYmeters, double _sizeZmeters,
00044                            double _cellSizeX, double _cellSizeY, double _cellSizeZ,
00045                            double _centerX, double _centerY, double _centerZ,
00046                            NDTCell *cellPrototype ) 
00047 {
00048 
00049     sizeXmeters = _sizeXmeters;
00050     sizeYmeters = _sizeYmeters;
00051     sizeZmeters = _sizeZmeters;
00052 
00053     cellSizeX = _cellSizeX;
00054     cellSizeY = _cellSizeY;
00055     cellSizeZ = _cellSizeZ;
00056 
00057     sizeX = abs(ceil(sizeXmeters/cellSizeX));
00058     sizeY = abs(ceil(sizeYmeters/cellSizeY));
00059     sizeZ = abs(ceil(sizeZmeters/cellSizeZ));
00060 
00061     centerX = _centerX;
00062     centerY = _centerY;
00063     centerZ = _centerZ;
00064 
00065     protoType = cellPrototype->clone();
00066     initialize();
00067 
00068 
00069 }
00070 
00071 void LazyGrid::setCenter(const double &cx, const double &cy, const double &cz)
00072 {
00073     centerX = cx;
00074     centerY = cy;
00075     centerZ = cz;
00076 
00077     centerIsSet = true;
00078     if(sizeIsSet)
00079     {
00080         initialize();
00081     }
00082 }
00083 
00084 void LazyGrid::setSize(const double &sx, const double &sy, const double &sz)
00085 {
00086 
00087     sizeXmeters = sx;
00088     sizeYmeters = sy;
00089     sizeZmeters = sz;
00090 
00091     sizeX = abs(ceil(sizeXmeters/cellSizeX));
00092     sizeY = abs(ceil(sizeYmeters/cellSizeY));
00093     sizeZ = abs(ceil(sizeZmeters/cellSizeZ));
00094 
00095     sizeIsSet = true;
00096     if(centerIsSet)
00097     {
00098         initialize();
00099     }
00100 }
00101 
00102 void LazyGrid::initializeAll()
00103 {
00104     if(!initialized)
00105     {
00106         this->initialize();
00107     }
00108     pcl::PointXYZ centerCell;
00109     for(int i=0; i<sizeX; i++)
00110     {
00111         for(int j=0; j<sizeY; j++)
00112         {
00113             for(int k=0; k<sizeZ; k++)
00114             {
00115                 dataArray[i][j][k] = new NDTCell();
00116                 dataArray[i][j][k]->setDimensions(cellSizeX,cellSizeY,cellSizeZ);
00117 
00118                 int idcX, idcY, idcZ;
00119                 pcl::PointXYZ center;
00120                 center.x = centerX;
00121                 center.y = centerY;
00122                 center.z = centerZ;
00123                 this->getIndexForPoint(center, idcX,idcY,idcZ);
00124                 centerCell.x = centerX + (i-idcX)*cellSizeX;
00125                 centerCell.y = centerY + (j-idcY)*cellSizeY;
00126                 centerCell.z = centerZ + (k-idcZ)*cellSizeZ;
00127                 dataArray[i][j][k]->setCenter(centerCell);
00128                 activeCells.push_back(dataArray[i][j][k]);
00129             }
00130         }
00131     }
00132 }
00133 
00134 void LazyGrid::initialize()
00135 {
00136 
00137     dataArray = new NDTCell***[sizeX];
00138     for(int i=0; i<sizeX; i++)
00139     {
00140         dataArray[i] = new NDTCell**[sizeY];
00141         for(int j=0; j<sizeY; j++)
00142         {
00143             dataArray[i][j] = new NDTCell*[sizeZ];
00144             //set all cells to NULL
00145             memset(dataArray[i][j],0,sizeZ*sizeof(NDTCell*));
00146         }
00147     }
00148     initialized = true;
00149 }
00150 
00151 LazyGrid::~LazyGrid()
00152 {
00153     if(initialized)
00154     {
00155         //fprintf(stderr,"LAZY GRID DESTRUCTION -- ");
00156         int cnt = 0;
00157         //go through all cells and delete the non-NULL ones
00158         for(unsigned int i=0; i<activeCells.size(); ++i)
00159         {
00160             if(activeCells[i])
00161             {
00162                 delete activeCells[i];
00163                 cnt++;
00164             }
00165         }
00166         for(int i=0; i<sizeX; i++)
00167         {
00168             for(int j=0; j<sizeY; j++)
00169             {
00170                 delete[] dataArray[i][j];
00171             }
00172             delete[] dataArray[i];
00173         }
00174         delete[] dataArray;
00175         if(protoType!=NULL)
00176         {
00177             delete protoType;
00178         }
00179         //fprintf(stderr,"Deleted %d cells and array of (%d x %d)!!!\n",cnt, sizeX, sizeY);
00180     }
00181 }
00182 
00183 NDTCell* LazyGrid::getCellForPoint(const pcl::PointXYZ &point)
00184 {
00185 
00186     int indX,indY,indZ;
00187     this->getIndexForPoint(point,indX,indY,indZ);
00188 
00189     if(indX >= sizeX || indY >= sizeY || indZ >= sizeZ || indX<0 || indY<0 || indZ<0) return NULL;
00190     if(!initialized) return NULL;
00191     if(dataArray == NULL) return NULL;
00192     if(dataArray[indX] == NULL) return NULL;
00193     if(dataArray[indX][indY] == NULL) return NULL;
00194 
00195     //    cout<<"LZ: "<<indX<<" "<<indY<<" "<<indZ<<endl;
00196     return dataArray[indX][indY][indZ];
00197 }
00198 
00199 NDTCell* LazyGrid::addPoint(const pcl::PointXYZ &point_c)
00200 {
00201 
00202     pcl::PointXYZ point = point_c;
00203     if(std::isnan(point.x) ||std::isnan(point.y) ||std::isnan(point.z))
00204     {
00205         return NULL;
00206     }
00207     int indX,indY,indZ;
00208     this->getIndexForPoint(point,indX,indY,indZ);
00209     pcl::PointXYZ centerCell;
00210 
00211     if(indX >= sizeX || indY >= sizeY || indZ >= sizeZ || indX<0 || indY<0 || indZ<0)
00212     {
00213         return NULL;
00214     }
00215 
00216     if(!initialized) return NULL;
00217     if(dataArray == NULL) return NULL;
00218     if(dataArray[indX] == NULL) return NULL;
00219     if(dataArray[indX][indY] == NULL) return NULL;
00220 
00221     if(dataArray[indX][indY][indZ]==NULL)
00222     {
00223         //initialize cell
00224         dataArray[indX][indY][indZ] = protoType->clone();
00225         dataArray[indX][indY][indZ]->setDimensions(cellSizeX,cellSizeY,cellSizeZ);
00226 
00227         int idcX, idcY, idcZ;
00228         pcl::PointXYZ center;
00229         center.x = centerX;
00230         center.y = centerY;
00231         center.z = centerZ;
00232         this->getIndexForPoint(center, idcX,idcY,idcZ);
00233         centerCell.x = centerX + (indX-idcX)*cellSizeX;
00234         centerCell.y = centerY + (indY-idcY)*cellSizeY;
00235         centerCell.z = centerZ + (indZ-idcZ)*cellSizeZ;
00236         dataArray[indX][indY][indZ]->setCenter(centerCell);
00237         /*
00238            cout<<"center: "<<centerX<<" "<<centerY<<" "<<centerZ<<endl;
00239            cout<<"size  : "<<sizeX<<" "<<sizeY<<" "<<sizeZ<<endl;
00240            cout<<"p  : "<<point.x<<" "<<point.y<<" "<<point.z<<endl;
00241            cout<<"c  : "<<centerCell.x<<" "<<centerCell.y<<" "<<centerCell.z<<endl;
00242            cout<<"id : "<<indX<<" "<<indY<<" "<<indZ<<endl;
00243            cout<<"cs : "<<cellSizeX<<" "<<cellSizeY<<" "<<cellSizeZ<<endl;
00244          */
00245         activeCells.push_back(dataArray[indX][indY][indZ]);
00246     }
00247     dataArray[indX][indY][indZ]->addPoint(point);
00248     return dataArray[indX][indY][indZ];
00249 }
00250 
00251 typename SpatialIndex::CellVectorItr LazyGrid::begin()
00252 {
00253     return activeCells.begin();
00254 }
00255 
00256 typename SpatialIndex::CellVectorItr LazyGrid::end()
00257 {
00258     return activeCells.end();
00259 }
00260 
00261 int LazyGrid::size()
00262 {
00263     return activeCells.size();
00264 }
00265 
00266 SpatialIndex* LazyGrid::clone() const
00267 {
00268     return new LazyGrid(cellSizeX);
00269 }
00270 
00271 SpatialIndex* LazyGrid::copy() const
00272 {
00273     LazyGrid *ret = new LazyGrid(cellSizeX);
00274     typename std::vector<NDTCell*>::const_iterator it;
00275     it = activeCells.begin();
00276     while(it!=activeCells.end())
00277     {
00278         NDTCell* r = (*it);
00279         if(r == NULL) continue;
00280         for(unsigned int i=0; i<r->points_.size(); i++)
00281         {
00282             ret->addPoint(r->points_[i]);
00283         }
00284         it++;
00285     }
00286     return ret;
00287 }
00288 
00289 void LazyGrid::getNeighbors(const pcl::PointXYZ &point, const double &radius, std::vector<NDTCell*> &cells)
00290 {
00291     int indX,indY,indZ;
00292     this->getIndexForPoint(point, indX,indY,indZ);
00293     if(indX >= sizeX || indY >= sizeY || indZ >= sizeZ)
00294     {
00295         cells.clear();
00296         return;
00297     }
00298 
00299     for(int x = indX - radius/cellSizeX; x<indX+radius/cellSizeX; x++)
00300     {
00301         if(x < 0 || x >= sizeX) continue;
00302         for(int y = indY - radius/cellSizeY; y<indY+radius/cellSizeY; y++)
00303         {
00304             if(y < 0 || y >= sizeY) continue;
00305             for(int z = indZ - radius/cellSizeZ; z<indZ+radius/cellSizeZ; z++)
00306             {
00307                 if(z < 0 || z >= sizeZ) continue;
00308                 if(dataArray[x][y][z]==NULL) continue;
00309                 cells.push_back(dataArray[x][y][z]);
00310             }
00311         }
00312     }
00313 
00314 }
00315 
00316 void LazyGrid::getIndexForPoint(const pcl::PointXYZ& point, int &indX, int &indY, int &indZ)
00317 {
00318     indX = floor((point.x - centerX)/cellSizeX+0.5) + sizeX/2.0;
00319     indY = floor((point.y - centerY)/cellSizeY+0.5) + sizeY/2.0;
00320     indZ = floor((point.z - centerZ)/cellSizeZ+0.5) + sizeZ/2.0;
00321 }
00322 
00323 std::vector<NDTCell*> LazyGrid::getClosestCells(const pcl::PointXYZ &pt)
00324 {
00325     int indXn,indYn,indZn;
00326     int indX,indY,indZ;
00327     this->getIndexForPoint(pt,indX,indY,indZ);
00328     std::vector<NDTCell*> cells;
00329 
00330     int i =2; //how many cells on each side
00331     //the strange thing for the indeces is for convenience of writing
00332     //basicly, we go through 2* the number of cells and use parity to
00333     //decide if we subtract or add. should work nicely
00334     for(int x=1; x<2*i+2; x++)
00335     {
00336         indXn = (x%2 == 0) ? indX+x/2 : indX-x/2;
00337         for(int y=1; y<2*i+2; y++)
00338         {
00339             indYn = (y%2 == 0) ? indY+y/2 : indY-y/2;
00340             for(int z=1; z<2*i+2; z++)
00341             {
00342                 indZn = (z%2 == 0) ? indZ+z/2 : indZ-z/2;
00343                 if(checkCellforNDT(indXn,indYn,indZn,true))
00344                 {
00345                     cells.push_back(dataArray[indXn][indYn][indZn]);
00346                 }
00347             }
00348         }
00349     }
00350     return cells;
00351 }
00352 
00353 std::vector<NDTCell*> LazyGrid::getClosestNDTCells(const pcl::PointXYZ &point, int &n_neigh, bool checkForGaussian)
00354 {
00355 
00356     int indXn,indYn,indZn;
00357     int indX,indY,indZ;
00358     this->getIndexForPoint(point,indX,indY,indZ);
00359     std::vector<NDTCell*> cells;
00360 
00361     int i = n_neigh; //how many cells on each side
00362 
00363     //for(int i=1; i<= maxNumberOfCells; i++) {
00364     //the strange thing for the indeces is for convenience of writing
00365     //basicly, we go through 2* the number of cells and use parity to
00366     //decide if we subtract or add. should work nicely
00367     for(int x=1; x<2*i+2; x++)
00368     {
00369         indXn = (x%2 == 0) ? indX+x/2 : indX-x/2;
00370         for(int y=1; y<2*i+2; y++)
00371         {
00372             indYn = (y%2 == 0) ? indY+y/2 : indY-y/2;
00373             for(int z=1; z<2*i+2; z++)
00374             {
00375                 indZn = (z%2 == 0) ? indZ+z/2 : indZ-z/2;
00376                 if(checkCellforNDT(indXn,indYn,indZn,checkForGaussian))
00377                 {
00378                     cells.push_back(dataArray[indXn][indYn][indZn]);
00379                 }
00380             }
00381         }
00382     }
00383 
00384     return cells;
00385 }
00386 
00387 NDTCell* LazyGrid::getClosestNDTCell(const pcl::PointXYZ &point, bool checkForGaussian)
00388 {
00389     int indXn,indYn,indZn;
00390     int indX,indY,indZ;
00391     this->getIndexForPoint(point,indX,indY,indZ);
00392     NDTCell *ret = NULL;
00393     std::vector<NDTCell*> cells;
00394 
00395     if(!checkForGaussian)
00396     {
00397         //just give me whatever is in this cell
00398         if(checkCellforNDT(indX,indY,indZ,checkForGaussian))
00399         {
00400             ret = (dataArray[indX][indY][indZ]);
00401         }
00402         return ret;
00403     }
00404 
00405     int i =1; //how many cells on each side
00406 
00407     //for(int i=1; i<= maxNumberOfCells; i++) {
00408     //the strange thing for the indeces is for convenience of writing
00409     //basicly, we go through 2* the number of cells and use parity to
00410     //decide if we subtract or add. should work nicely
00411     for(int x=1; x<2*i+2; x++)
00412     {
00413         indXn = (x%2 == 0) ? indX+x/2 : indX-x/2;
00414         for(int y=1; y<2*i+2; y++)
00415         {
00416             indYn = (y%2 == 0) ? indY+y/2 : indY-y/2;
00417             for(int z=1; z<2*i+2; z++)
00418             {
00419                 indZn = (z%2 == 0) ? indZ+z/2 : indZ-z/2;
00420                 if(checkCellforNDT(indXn,indYn,indZn))
00421                 {
00422                     ret = (dataArray[indXn][indYn][indZn]);
00423                     cells.push_back(ret);
00424                 }
00425             }
00426         }
00427     }
00428 
00429     double minDist = INT_MAX;
00430     Eigen::Vector3d tmean;
00431     pcl::PointXYZ pt = point;
00432     for(unsigned int i=0; i<cells.size(); i++)
00433     {
00434         tmean = cells[i]->getMean();
00435         tmean(0) -= pt.x;
00436         tmean(1) -= pt.y;
00437         tmean(2) -= pt.z;
00438         double d = tmean.norm();
00439         if(d<minDist)
00440         {
00441             minDist = d;
00442             ret = cells[i];
00443         }
00444     }
00445     cells.clear();
00446     return ret;
00447 }
00448 
00449 bool LazyGrid::checkCellforNDT(int indX, int indY, int indZ, bool checkForGaussian)
00450 {
00451 
00452     if(indX < sizeX && indY < sizeY && indZ < sizeZ &&
00453             indX >=0 && indY >=0 && indZ >=0)
00454     {
00455         if(dataArray[indX][indY][indZ]!=NULL)
00456         {
00457             if(dataArray[indX][indY][indZ]->hasGaussian_ || (!checkForGaussian))
00458             {
00459                 return true;
00460             }
00461         }
00462     }
00463     return false;
00464 }
00465 
00466 void LazyGrid::setCellType(NDTCell *type)
00467 {
00468     if(type!=NULL)
00469     {
00470         protoType = type->clone();
00471     }
00472 }
00473 
00474 void LazyGrid::getCellSize(double &cx, double &cy, double &cz)
00475 {
00476     cx = cellSizeX;
00477     cy = cellSizeY;
00478     cz = cellSizeZ;
00479 }
00480 
00481 void LazyGrid::getCenter(double &cx, double &cy, double &cz)
00482 {
00483     cx = centerX;
00484     cy = centerY;
00485     cz = centerZ;
00486 
00487 }
00488 
00489 void LazyGrid::getGridSize(int &cx, int &cy, int &cz)
00490 {
00491     cx = sizeX;
00492     cy = sizeY;
00493     cz = sizeZ;
00494 }
00495 
00496 void LazyGrid::getGridSizeInMeters(double &cx, double &cy, double &cz)
00497 {
00498     cx = sizeXmeters;
00499     cy = sizeYmeters;
00500     cz = sizeZmeters;
00501 }
00502 
00503 int LazyGrid::loadFromJFF(FILE * jffin)
00504 {
00505     double lazyGridData[9]; // = { sizeXmeters, sizeYmeters, sizeZmeters,
00506     //     cellSizeX,   cellSizeY,   cellSizeZ,
00507     //     centerX,     centerY,     centerZ };
00508     NDTCell prototype_;
00509     if(fread(&lazyGridData, sizeof(double), 9, jffin) <= 0)
00510     {
00511         JFFERR("reading lazyGridData failed");
00512     }
00513     if(fread(&prototype_, sizeof(NDTCell), 1, jffin) <= 0)
00514     {
00515         JFFERR("reading prototype_ failed");
00516     }
00517 
00518     // just in case someone was messing around with the new NDTMap
00519     centerIsSet = false;
00520     sizeIsSet = false;
00521 
00522     protoType = prototype_.clone();
00523 
00524     std::cerr<<"size meters: "<<lazyGridData[0]<<" "<<lazyGridData[1]<<" "<<lazyGridData[2]<<std::endl;
00525     std::cerr<<"cell size: "<<lazyGridData[3]<<" "<<lazyGridData[4]<<" "<<lazyGridData[5]<<std::endl;
00526     std::cerr<<"center meters: "<<lazyGridData[6]<<" "<<lazyGridData[7]<<" "<<lazyGridData[8]<<std::endl;
00527 
00528     this->setSize(lazyGridData[0], lazyGridData[1], lazyGridData[2]);
00529 
00530     cellSizeX = lazyGridData[3];
00531     cellSizeY = lazyGridData[4];
00532     cellSizeZ = lazyGridData[5];
00533 
00534     this->setCenter(lazyGridData[6], lazyGridData[7], lazyGridData[8]);
00535 
00536     this->initializeAll();
00537     int indX, indY, indZ;
00538     float r,g,b;
00539     double xs,ys,zs;
00540     pcl::PointXYZ centerCell;
00541     float occ;
00542     unsigned int N;
00543 
00544     // load all cells
00545     while (1)
00546     {
00547         if(prototype_.loadFromJFF(jffin) < 0)
00548         {
00549             if(feof(jffin))
00550             {
00551                 break;
00552             }
00553             else
00554             {
00555                 JFFERR("loading cell failed");
00556             }
00557         }
00558 
00559         if(!feof(jffin))
00560         {
00561             // std::cout << prototype_.getOccupancy() << std::endl; /* for debugging */
00562         }
00563         else
00564         {
00565             break;
00566         }
00567         centerCell = prototype_.getCenter();
00568         this->getIndexForPoint(centerCell, indX, indY, indZ);
00569         if(indX < 0 || indX >= sizeX) continue; 
00570         if(indY < 0 || indY >= sizeY) continue; 
00571         if(indZ < 0 || indZ >= sizeZ) continue; 
00572         if(!initialized) return -1;
00573         if(dataArray == NULL) return -1;
00574         if(dataArray[indX] == NULL) return -1;
00575         if(dataArray[indX][indY] == NULL) return -1;
00576 
00577         if(dataArray[indX][indY][indZ] != NULL)
00578         {
00579             NDTCell* ret = dataArray[indX][indY][indZ];
00580             prototype_.getRGB(r,g,b);
00581             prototype_.getDimensions(xs,ys,zs);
00582 
00583             ret->setDimensions(xs,ys,zs);
00584             ret->setCenter(centerCell);
00585             ret->setMean(prototype_.getMean());
00586             ret->setCov(prototype_.getCov());
00587             ret->setRGB(r,g,b);
00588             ret->setOccupancy(prototype_.getOccupancy());
00589             ret->setEmptyval(prototype_.getEmptyval());
00590             ret->setEventData(prototype_.getEventData());
00591             ret->setN(prototype_.getN());
00592             ret->isEmpty = prototype_.isEmpty;
00593             ret->hasGaussian_ = prototype_.hasGaussian_;
00594             ret->consistency_score = prototype_.consistency_score;
00595 
00596         } else {
00597             //initialize cell
00598             std::cerr<<"NEW CELL\n";
00599             dataArray[indX][indY][indZ] = prototype_.copy();
00600             activeCells.push_back(dataArray[indX][indY][indZ]);
00601         }
00602     }
00603 
00604     return 0;
00605 }
00606 
00607 bool LazyGrid::traceLine(const Eigen::Vector3d &origin, const pcl::PointXYZ &endpoint,const Eigen::Vector3d &diff_ ,
00608         const double& maxz, std::vector<NDTCell*> &cells)
00609 {
00610         if(endpoint.z>maxz)
00611         {
00612                 return false;
00613         }
00614         
00615         double min1 = std::min(cellSizeX,cellSizeY);
00616         double min2 = std::min(cellSizeZ,cellSizeY);
00617         double resolution = std::min(min1,min2); 
00618         
00619         if(resolution<0.01)
00620         {
00621                 fprintf(stderr,"Resolution very very small (%lf) :( \n",resolution);
00622                 return false;
00623         }
00624         double l = diff_.norm();
00625         int N = l / (resolution);
00626         //if(N <= 0)
00627         //{
00628         //      //fprintf(stderr,"N=%d (r=%lf l=%lf) :( ",N,resolution,l);
00629         //      return false;
00630         //}
00631         
00632         NDTCell* ptCell = NULL;    
00633         pcl::PointXYZ pt;
00634         pcl::PointXYZ po;
00635         po.x = origin(0); po.y = origin(1); po.z = origin(2);
00636         Eigen::Vector3d diff = diff_/(float)N;
00637         
00638         int idxo=0, idyo=0,idzo=0;
00639         for(int i=0; i<N-2; i++)
00640         {
00641                 pt.x = origin(0) + ((float)(i+1)) *diff(0);
00642                 pt.y = origin(1) + ((float)(i+1)) *diff(1);
00643                 pt.z = origin(2) + ((float)(i+1)) *diff(2);
00644                 int idx,idy,idz;
00645                 idx = floor((pt.x - centerX)/cellSizeX+0.5) + sizeX/2.0;
00646                 idy = floor((pt.y - centerY)/cellSizeY+0.5) + sizeY/2.0;
00647                 idz = floor((pt.z - centerZ)/cellSizeZ+0.5) + sizeZ/2.0;
00650                 if(idx == idxo && idy==idyo && idz ==idzo)
00651                 {
00652                         continue;
00653                 }
00654                 else
00655                 {
00656                         idxo = idx;
00657                         idyo = idy;
00658                         idzo = idz;
00659                 }
00660                 
00661                 if(idx < sizeX && idy < sizeY && idz < sizeZ && idx >=0 && idy >=0 && idz >=0){
00662                         ptCell = dataArray[idx][idy][idz];
00663                         if(ptCell !=NULL) {
00664                                 cells.push_back(ptCell);
00665                         } else {
00666                                 this->addPoint(pt); 
00667                         }
00668                 }
00669         }
00670         return true;
00671         
00672 }
00673 
00674 bool LazyGrid::traceLineWithEndpoint(const Eigen::Vector3d &origin, const pcl::PointXYZ &endpoint,const Eigen::Vector3d &diff_ ,const double& maxz, std::vector<NDTCell*> &cells, Eigen::Vector3d &final_point)
00675 {
00676     if(endpoint.z>maxz)
00677     {
00678         return false;
00679     }
00680 
00681     double min1 = std::min(cellSizeX,cellSizeY);
00682     double min2 = std::min(cellSizeZ,cellSizeY);
00683     double resolution = std::min(min1,min2); 
00684 
00685     if(resolution<0.01)
00686     {
00687         fprintf(stderr,"Resolution very very small (%lf) :( \n",resolution);
00688         return false;
00689     }
00690     double l = diff_.norm();
00691     int N = l / (resolution);
00692     NDTCell* ptCell = NULL;    
00693     pcl::PointXYZ pt;
00694     pcl::PointXYZ po;
00695     po.x = origin(0); po.y = origin(1); po.z = origin(2);
00696     if(N == 0)
00697     {
00698         //fprintf(stderr,"N=%d (r=%lf l=%lf) :( ",N,resolution,l);
00699         //return false;
00700         this->getNDTCellAt(po,ptCell);
00701         if(ptCell!=NULL) {
00702             cells.push_back(ptCell);
00703         }
00704         return true;
00705     }
00706 
00707     Eigen::Vector3d diff = diff_/(float)N;
00708 
00709     int idxo=0, idyo=0,idzo=0;
00710     bool complete = true;
00711     for(int i=0; i<N-2; i++)
00712     {
00713         pt.x = origin(0) + ((float)(i+1)) *diff(0);
00714         pt.y = origin(1) + ((float)(i+1)) *diff(1);
00715         pt.z = origin(2) + ((float)(i+1)) *diff(2);
00716         int idx,idy,idz;
00717         idx = floor((pt.x - centerX)/cellSizeX+0.5) + sizeX/2.0;
00718         idy = floor((pt.y - centerY)/cellSizeY+0.5) + sizeY/2.0;
00719         idz = floor((pt.z - centerZ)/cellSizeZ+0.5) + sizeZ/2.0;
00722         if(idx == idxo && idy==idyo && idz ==idzo)
00723         {
00724             continue;
00725         }
00726         else
00727         {
00728             idxo = idx;
00729             idyo = idy;
00730             idzo = idz;
00731         }
00732 
00733         if(idx < sizeX && idy < sizeY && idz < sizeZ && idx >=0 && idy >=0 && idz >=0){
00734             ptCell = dataArray[idx][idy][idz];
00735             if(ptCell !=NULL) {
00736                 cells.push_back(ptCell);
00737             } else {
00738                 this->addPoint(pt); 
00739             }
00740         } else {
00741             //out of the map, we won't be coming back any time soon
00742             complete = false;
00743             final_point = origin+(float(i))*diff;
00744             break;
00745         }
00746     }
00747     if(complete) final_point = origin+diff_;
00748     return true;
00749 
00750 }
00751 
00752 } //end namespace


ndt_map
Author(s): Todor Stoyanov, Jari Saarinen, Henrik Andreasson
autogenerated on Wed Aug 26 2015 15:24:40