29 #ifndef COccupancyGridMap2D_H
30 #define COccupancyGridMap2D_H
44 #include <mrpt/config.h>
46 #if !defined(OCCUPANCY_GRIDMAP_CELL_SIZE_8BITS) && !defined(OCCUPANCY_GRIDMAP_CELL_SIZE_16BITS)
47 #error One of OCCUPANCY_GRIDMAP_CELL_SIZE_16BITS or OCCUPANCY_GRIDMAP_CELL_SIZE_8BITS must be defined.
50 #if defined(OCCUPANCY_GRIDMAP_CELL_SIZE_8BITS) && defined(OCCUPANCY_GRIDMAP_CELL_SIZE_16BITS)
51 #error Only one of OCCUPANCY_GRIDMAP_CELL_SIZE_16BITS or OCCUPANCY_GRIDMAP_CELL_SIZE_8BITS must be defined at a time.
57 namespace poses {
class CPose2D; }
60 using namespace mrpt::poses;
61 using namespace mrpt::utils;
63 class CObservation2DRangeScan;
64 class CObservationRange;
95 #ifdef OCCUPANCY_GRIDMAP_CELL_SIZE_8BITS
106 #ifdef OCCUPANCY_GRIDMAP_CELL_SIZE_8BITS
130 std::vector<cellType>
map;
160 virtual void OnPostSuccesfulInsertObs(
const CObservation *);
172 static double H(
double p);
176 inline void setCell_nocheck(
int x,
int y,
float value)
178 map[x+y*size_x]=p2l(value);
183 inline float getCell_nocheck(
int x,
int y)
const
185 return l2p(map[x+y*size_x]);
190 inline void setRawCell(
unsigned int cellIndex, cellType b)
192 if (cellIndex<size_x*size_y)
205 double computeObservationLikelihood_Consensus(
212 double computeObservationLikelihood_ConsensusOWA(
218 double computeObservationLikelihood_CellsDifference(
224 double computeObservationLikelihood_MI(
230 double computeObservationLikelihood_rayTracing(
236 double computeObservationLikelihood_likelihoodField_Thrun(
242 double computeObservationLikelihood_likelihoodField_II(
248 virtual void internal_clear( );
259 virtual bool internal_insertObservation(
const CObservation *obs,
const CPose3D *robotPose = NULL );
266 void updateCell(
int x,
int y,
float v);
274 int cellsUpdated=0) : enabled(enabled),
276 cellsUpdated(cellsUpdated),
296 } updateInfoChangeOnly;
302 float min_y = -20.0f,
304 float resolution = 0.05f
309 void fill(
float default_value = 0.5f );
324 void setSize(
float x_min,
float x_max,
float y_min,
float y_max,
float resolution,
float default_value = 0.5f);
335 void resizeGrid(
float new_x_min,
float new_x_max,
float new_y_min,
float new_y_max,
float new_cells_default_value = 0.5f,
bool additionalMargin =
true)
MRPT_NO_THROWS;
338 inline
double getArea()
const {
return size_x*size_y*
square(resolution); }
342 inline unsigned int getSizeX()
const {
return size_x; }
346 inline unsigned int getSizeY()
const {
return size_y; }
350 inline float getXMin()
const {
return x_min; }
354 inline float getXMax()
const {
return x_max; }
358 inline float getYMin()
const {
return y_min; }
362 inline float getYMax()
const {
return y_max; }
370 inline int x2idx(
float x)
const {
return static_cast<int>((x-x_min)/resolution ); }
371 inline int y2idx(
float y)
const {
return static_cast<int>((y-y_min)/resolution ); }
373 inline int x2idx(
double x)
const {
return static_cast<int>((x-x_min)/resolution ); }
374 inline int y2idx(
double y)
const {
return static_cast<int>((y-y_min)/resolution ); }
378 inline float idx2x(
const size_t cx)
const {
return x_min+(cx+0.5f)*resolution; }
379 inline float idx2y(
const size_t cy)
const {
return y_min+(cy+0.5f)*resolution; }
383 inline int x2idx(
float x,
float x_min)
const {
return static_cast<int>((x-x_min)/resolution ); }
384 inline int y2idx(
float y,
float y_min)
const {
return static_cast<int>((y-y_min)/resolution ); }
388 static inline float l2p(
const cellType l)
390 return m_logodd_lut.
l2p(l);
395 static inline uint8_t l2p_255(
const cellType l)
397 return m_logodd_lut.
l2p_255(l);
402 static inline cellType p2l(
const float p)
404 return m_logodd_lut.
p2l(p);
409 inline void setCell(
int x,
int y,
float value)
412 if (static_cast<unsigned int>(x)>=size_x || static_cast<unsigned int>(y)>=size_y)
414 else map[x+y*size_x]=p2l(value);
419 inline float getCell(
int x,
int y)
const
422 if (static_cast<unsigned int>(x)>=size_x || static_cast<unsigned int>(y)>=size_y)
424 else return l2p(map[x+y*size_x]);
429 inline cellType *
getRow(
int cy ) {
if (cy<0 || static_cast<unsigned int>(cy)>=size_y)
return NULL;
else return &map[0+cy*size_x]; }
433 inline const cellType *
getRow(
int cy )
const {
if (cy<0 || static_cast<unsigned int>(cy)>=size_y)
return NULL;
else return &map[0+cy*size_x]; }
437 inline void setPos(
float x,
float y,
float value) { setCell(x2idx(x),y2idx(y),value); }
441 inline float getPos(
float x,
float y)
const {
return getCell(x2idx(x),y2idx(y)); }
445 inline bool isStaticPos(
float x,
float y,
float threshold = 0.7f)
const {
return isStaticCell(x2idx(x),y2idx(y),threshold); }
446 inline bool isStaticCell(
int cx,
int cy,
float threshold = 0.7f)
const {
return (getCell(cx,cy)<=threshold); }
450 inline void setBasisCell(
int x,
int y,uint8_t value)
464 inline unsigned char getBasisCell(
int x,
int y)
const
481 TEntropyInfo() : H(0),I(0),mean_H(0),mean_I(0),effectiveMappedArea(0),effectiveMappedCells(0)
531 void loadFromConfigFile(
533 const std::string §ion);
537 void dumpToTextStream(
CStream &out)
const;
586 lmMeanInformation = 0,
615 void loadFromConfigFile(
617 const std::string §ion);
621 void dumpToTextStream(
CStream &out)
const;
717 void subSample(
int downRatio );
737 void buildVoronoiDiagram(
float threshold,
float robot_size,
int x1=0,
int x2=0,
int y1=0,
int y2=0);
740 inline uint16_t getVoroniClearance(
int cx,
int cy)
const
748 const uint16_t *cell=m_voronoi_diagram.
cellByIndex(cx,cy);
754 inline void setVoroniClearance(
int cx,
int cy,uint16_t dist)
756 uint16_t *cell=m_voronoi_diagram.
cellByIndex(cx,cy);
779 void findCriticalPoints(
float filter_distance );
795 int computeClearance(
int cx,
int cy,
int *basis_x,
int *basis_y,
int *nBasis,
bool GetContourPoint =
false )
const;
800 float computeClearance(
float x,
float y,
float maxSearchDistance )
const;
805 float computePathCost(
float x1,
float y1,
float x2,
float y2 )
const;
826 void laserScanSimulator(
829 float threshold = 0.5f,
832 unsigned int decimation = 1,
833 float angleNoiseStd =
DEG2RAD(0) )
const;
849 float threshold = 0.5f,
850 float rangeNoiseStd = 0,
851 float angleNoiseStd =
DEG2RAD(0) )
const;
855 inline void simulateScanRay(
856 const double x,
const double y,
const double angle_direction,
857 float &out_range,
bool &out_valid,
858 const unsigned int max_ray_len,
859 const float threshold_free=0.5f,
860 const double noiseStd=0,
const double angleNoiseStd=0 )
const;
882 bool canComputeObservationLikelihood(
const CObservation *obs );
889 double computeLikelihoodField_Thrun(
const CPointsMap *pm,
const CPose2D *relativePose = NULL);
896 double computeLikelihoodField_II(
const CPointsMap *pm,
const CPose2D *relativePose = NULL);
902 bool saveAsBitmapFile(
const std::string &file)
const;
908 static bool saveAsBitmapTwoMapsWithCorrespondences(
909 const std::string &fileName,
918 static bool saveAsEMFTwoMapsWithCorrespondences(
919 const std::string &fileName,
928 template <
class CLANDMARKSMAP>
929 bool saveAsBitmapFileWithLandmarks(
930 const std::string &file,
931 const CLANDMARKSMAP *landmarks,
932 bool addTextLabels =
false,
937 getAsImageFiltered( img,
false,
true );
938 const bool topleft = img.isOriginTopLeft();
939 for (
unsigned int i=0;i<landmarks->landmarks.size();i++)
941 const typename CLANDMARKSMAP::landmark_type *lm = landmarks->landmarks.get( i );
942 int px = x2idx( lm->pose_mean.x );
943 int py = topleft ? size_y-1- y2idx( lm->pose_mean.y ) : y2idx( lm->pose_mean.y );
944 img.rectangle( px - 7, (py + 7), px +7, (py -7), marks_color );
945 img.rectangle( px - 6, (py + 6), px +6, (py -6), marks_color );
949 return img.saveToFile(file.c_str() );
958 void getAsImage(
utils::CImage &img,
bool verticalFlip =
false,
bool forceRGB=
false,
bool tricolor =
false)
const;
964 void getAsImageFiltered(
utils::CImage &img,
bool verticalFlip =
false,
bool forceRGB=
false)
const;
973 bool isEmpty()
const;
983 bool loadFromBitmapFile(
const std::string &file,
float resolution,
float xCentralPixel = -1,
float yCentralPixel =-1 );
993 bool loadFromBitmap(
const mrpt::utils::CImage &img,
float resolution,
float xCentralPixel = -1,
float yCentralPixel =-1 );
1000 void computeMatchingWith2D(
1001 const CMetricMap *otherMap,
1003 float maxDistForCorrespondence,
1004 float maxAngularDistForCorrespondence,
1005 const CPose2D &angularDistPivotPoint,
1007 float &correspondencesRatio,
1008 float *sumSqrDist = NULL,
1009 bool onlyKeepTheClosest =
false,
1010 bool onlyUniqueRobust =
false,
1011 const size_t decimation_other_map_points = 1,
1012 const size_t offset_other_map_points = 0 )
const;
1025 float compute3DMatchingRatio(
1026 const CMetricMap *otherMap,
1028 float minDistForCorr = 0.10f,
1029 float minMahaDistForCorr = 2.0f
1034 void saveMetricMapRepresentationToFile(
1035 const std::string &filNamePrefix
1049 std::vector<int> x,
y;
1056 } CriticalPointsList;
1065 inline unsigned char GetNeighborhood(
int cx,
int cy )
const;
1071 int direccion_vecino_x[8],direccion_vecino_y[8];
1077 int direction2idx(
int dx,
int dy);