MRXT: The Multi-Robot eXploration Tool
Multi-Robot autonomous exploration and mapping simulator.
include/architecture/slam/mapdata/occupancyGridMap.h
00001 /*
00002 *
00003 * Author: Miguel Julia <mjulia@umh.es> 
00004 * 
00005 * Date:   2008
00006 * 
00007 * Class occupancyGridMap
00008 *
00009 *
00010 */
00011 #pragma once
00012 #ifndef __OCCUPANCY_GRID_MAP___
00013 #define __OCCUPANCY_GRID_MAP___
00014 
00015 #include "binMap.h"
00016 #include "robotTypes.h"
00017 #include "matFuns.h"
00018 #include "rangeSensorData.h"
00019 #include "gridMapInterface.h"
00020 
00021 #define DILATERAD 2
00022 
00023 /**
00024 * @brief This is an abstract class that implements some common parts of the grid mapping algorithms
00025 *
00026 */
00027 class occupancyGridMap : public gridMapInterface
00028 {
00029 
00030 ///////////////////////////////////////////////////////////////////////
00031 //-------------------------  Attributes  ------------------------------
00032 ///////////////////////////////////////////////////////////////////////
00033 
00034 protected:
00035 
00036         unsigned short width;
00037         unsigned short height;
00038         float realWidth;
00039         float realHeight;
00040         float xorigin;          // Relation between discrete and real axis
00041         float yorigin;
00042         float resolution;
00043 
00044 //--------------------------------------------------------------------- 
00045 ///////////////////////////////////////////////////////////////////////
00046 
00047 ///////////////////////////////////////////////////////////////////////
00048 //-------------------------  Methods  --------------------------------
00049 ///////////////////////////////////////////////////////////////////////
00050 
00051 public:
00052 
00053         /// Defaulf constructor
00054         occupancyGridMap();
00055         /// Constructor with size in meters and related to real coordinates
00056         occupancyGridMap(float w, float h, float res, float x, float y);
00057         /// Default destructor
00058         virtual ~occupancyGridMap();
00059 
00060         // These methods have to be implemented for each algorithm in derived classes:
00061 
00062         /// Initializes with size in meters and related to real coordinates
00063         virtual void initialize(float w, float h, float res, float x, float y)=0;
00064         /// clone method
00065         virtual occupancyGridMap* clone() const=0;
00066         /// assigment operator
00067         virtual gridMapInterface& operator=(const gridMapInterface&);
00068 
00069         /// return the occupancy probability of the cell
00070         virtual float getValue(int x, int y) const=0;
00071         /// return the occupancy probability of the cell
00072         float getValue(int x, int y, int range) const;
00073         /// Resets the occupation probability for the total map
00074         virtual void reset()=0;
00075         /// Updates the occupancy grid using the new data
00076         virtual void update(const rangeSensorData& rsData, const pose& rpos, float disp)=0;
00077         
00078         /// sets the x real coordinates of pixel(0,0)
00079         void setXOrigin(float xori);
00080         /// sets the y real coordinates of pixel(0,0)
00081         void setYOrigin(float yori);
00082         /// sets the grid map resolution in meters
00083         void setResolution(float res);
00084         
00085         /// returns the width of the grid map
00086         int getWidth() const;
00087         /// returns the height of the grid map
00088         int getHeight() const;
00089         /// returns the width of the grid map
00090         float getRealWidth() const;
00091         /// returns the height of the grid map
00092         float getRealHeight() const;
00093         /// returns the x real coordinates of pixel(0,0)
00094         float getXOrigin() const;
00095         /// returns the y real coordinates of pixel(0,0)
00096         float getYOrigin() const;
00097         /// returns the grid map resolution in meters
00098         float getResolution() const;
00099         
00100         /// Returns a binMap of the frontiers
00101         int frontiers(binMap& frontiers) const;
00102         /// Returns a binMap of the frontiers inside the given zone
00103         int frontiersIn(const binMap& zone, binMap& frontiers) const;
00104         /// Evaluates the expected safe zone viewed from a given point
00105         void esz(int x, int y, binMap& esz, int dilaterad, int size) const;
00106         /// Looks for gateways in a given zone
00107         void gateways(const binMap& esz, binMap& gateways) const;
00108         void clearUnconnect(binMap& vz, int x, int y) const;
00109         /// Return the occupied cells
00110         void occupiedCells(binMap& occupied, int dilateRad = 1) const;
00111         
00112         /// true if the cell is free
00113         bool isfree(int x, int y) const;
00114         /// true if the cell is occupied
00115         bool isoccupied(int x, int y) const;
00116         /// true if the cell is unknown
00117         bool isunknown(int x, int y) const;
00118         /// true if the cell is frontier
00119         bool isfrontier(int x, int y) const;
00120         
00121         /// true if all cells in a given square mask of radius rad are free (Totally free)
00122         bool isfree(int x, int y, int rad) const;       
00123         /// true if all cells in a diamond 1px mask of radius rad are free (Totally free)
00124         bool isfreeD(int x, int y) const;       
00125         /// true if a cell in a given square mask of radius rad is occupied (Partially occupied)
00126         bool isoccupied(int x, int y, int rad) const;
00127         /// true if a cell in a given square mask of radius rad is unknown (Partially unknown)
00128         bool isunknown(int x, int y, int size) const;
00129         
00130         /// Save function
00131         void saveMapAsImage(const char* file) const;
00132         /// Get the map as a new opencv image
00133         IplImage* getMapAsImage() const;
00134         /// Show binMap
00135         void showMap(const char* windowname)const;
00136 
00137         /// returns an array of points of size points for a line that joins point (x1,y1) and point x2,y2)
00138         point* getLine(int x1, int y1, int x2, int y2, int& points) const;
00139 
00140         /// save a map to disk
00141         virtual int saveMapToFile(const char* file) const = 0;
00142         /// load a map from disk
00143         virtual int loadMapFromFile(const char* file) = 0;
00144 
00145         int countAccessible(const point* p, int numpoints, binMap& accessible) const;
00146 
00147         point toCell(float x, float y) const;
00148         pointf toCoords(int x, int y) const;
00149 
00150 protected:
00151 
00152         int floodfill(const int& x, const int& y, binMap& processed) const;
00153 
00154 
00155 //--------------------------------------------------------------------- 
00156 ///////////////////////////////////////////////////////////////////////
00157 };
00158 
00159 inline void occupancyGridMap::setXOrigin(float xori)                    {xorigin = xori;}
00160 inline void occupancyGridMap::setYOrigin(float yori)                    {yorigin = yori;}
00161 inline void occupancyGridMap::setResolution(float res)                  {resolution = res;}
00162 inline int occupancyGridMap::getWidth() const                           {return width;}
00163 inline int occupancyGridMap::getHeight() const                          {return height;}
00164 inline float occupancyGridMap::getRealHeight() const                    {return realHeight;}
00165 inline float occupancyGridMap::getRealWidth() const                     {return realWidth;}
00166 inline float occupancyGridMap::getXOrigin() const                       {return xorigin;}
00167 inline float occupancyGridMap::getYOrigin() const                       {return yorigin;}
00168 inline float occupancyGridMap::getResolution() const                    {return resolution;}
00169 inline bool occupancyGridMap::isfree(int x, int y) const                {return (getValue(x,y) < 50.0f)? true: false;}
00170 inline bool occupancyGridMap::isoccupied(int x, int y) const            {return (getValue(x,y) > 50.0f)? true: false;}
00171 inline bool occupancyGridMap::isunknown(int x, int y) const             {float val = getValue(x,y);
00172                                                                          return (val == 50.0f)? true: false;}
00173 inline bool occupancyGridMap::isfrontier(int x, int y) const            {return (isfree(x,y) && isunknown(x,y,1) && !isoccupied(x,y,1))? true : false;}
00174 inline point occupancyGridMap::toCell(float x, float y) const           {return point(  (int)floor((x-xorigin)/resolution),
00175                                                                                         (int)floor((y-yorigin)/resolution));}
00176 inline pointf occupancyGridMap::toCoords(int x, int y) const            {return pointf( (x+0.5f)*resolution+xorigin,
00177                                                                                         (y+0.5f)*resolution+yorigin);};
00178 
00179 #endif
 All Classes Functions Variables Typedefs