MRXT: The Multi-Robot eXploration Tool
Multi-Robot autonomous exploration and mapping simulator.
include/architecture/slam/mapdata/binMap.h
00001 /*
00002 *
00003 * Author: Miguel Julia <mjulia@umh.es> 
00004 * 
00005 * Date:   2008
00006 * 
00007 * Class binMap
00008 *
00009 * Implements a binary gridmap
00010 *
00011 */
00012 
00013 #pragma once
00014 #ifndef __BIN_MAP__
00015 #define __BIN_MAP__
00016 
00017 #include "robotTypes.h"
00018 #include <opencv2/opencv.hpp>
00019 #include <vector>
00020 #include <bitset>
00021 #include <list>
00022 
00023 /** 
00024 * @brief Implements a binary gridmap
00025 *
00026 */
00027 class binMap{
00028 
00029 ///////////////////////////////////////////////////////////////////////
00030 //-------------------------  Attributes  ------------------------------
00031 ///////////////////////////////////////////////////////////////////////
00032 private:
00033 
00034         unsigned short width;                                   
00035         unsigned short height;
00036         unsigned short totalsize;
00037         float xorigin;                                                  // Relation between discrete and real axis
00038         float yorigin;
00039         float resolution;
00040         RoI roi;                                                                // Region of interest for full map operations
00041         std::vector< std::bitset<32> > cells;   // grid data (row by row)
00042 
00043 //--------------------------------------------------------------------- 
00044 ///////////////////////////////////////////////////////////////////////
00045 
00046 ///////////////////////////////////////////////////////////////////////
00047 //-------------------------  Methods  ---------------------------------
00048 ///////////////////////////////////////////////////////////////////////
00049 public:
00050 
00051         /// Default Constructor
00052         binMap();
00053         /// Copy Constructor
00054         binMap(const binMap &map);
00055         /// Constructor with size in pixels
00056         binMap(int w, int h);
00057         /// Constructor with size in pixels and related to real coordinates
00058         binMap(int w, int h, float res, float x, float y);
00059         /// Constructor with size in meters and related to real coordinates
00060         binMap(float w, float h, float res, float x, float y);
00061         /// Destructor
00062         virtual ~binMap();
00063         
00064         /// Initializes with size in pixels
00065         void initialize(int w, int h);
00066         /// Initializes with size in pixels and related to real coordinates
00067         void initialize(int w, int h, float res, float x, float y);
00068         /// Initializes with size in meters and related to real coordinates
00069         void initialize(float w, float h, float res, float x, float y);
00070 
00071         /// assigment operator
00072         binMap& operator=(const binMap&);
00073 
00074         /// get pixel value
00075         bool get(int x, int y) const;
00076         /// set pixel value
00077         void set(int x, int y, bool value);
00078         /// set cells from cluster cell
00079         void set(const std::vector<clusterCell>& cl);
00080 
00081         /// checks if all cells in a mask of defined size is true 
00082         bool isInside(int x, int y, int size) const; 
00083         /// checks if a 1px diamant over that cell is inside the binmap
00084         bool isInsideD(int x, int y) const; 
00085         /// Checks if at least one cell in a mask of defined size is true
00086         bool isOver(int x, int y, int size) const;
00087 
00088         /// sets the x real coordinates of pixel(0,0)
00089         void setXOrigin(float xori);
00090         /// sets the y real coordinates of pixel(0,0)
00091         void setYOrigin(float yori);
00092         /// sets the grid map resolution in meters
00093         void setResolution(float res);
00094 
00095         /// returns the width of the grid map
00096         int getWidth() const;
00097         /// returns the height of the grid map
00098         int getHeight() const;
00099         /// returns the x real coordinates of pixel(0,0)
00100         float getXOrigin() const;
00101         /// returns the y real coordinates of pixel(0,0)
00102         float getYOrigin() const;
00103         /// returns the grid map resolution in meters
00104         float getResolution() const;
00105 
00106         /// subtraction cell to cell 
00107         void sub(const binMap& map);
00108         /// logic or cell to cell 
00109         void add(const binMap& map);
00110         /// logic and cell to cell 
00111         void times(const binMap& map);
00112         /// logic and cell to cell 
00113         void invert();
00114         /// clears the binMap
00115         void clear();
00116         /// computer vision opening function
00117         void opening(int rad);
00118         /// computer vision closing function
00119         void closing(int rad);
00120         /// computer vision dilation function
00121         void dilate(int rad);
00122         /// computer vision erosion function
00123         void erode(int rad);
00124 
00125         /// removes unconnected pixel from one point
00126         void removeUnconnected(int x, int y);
00127 
00128         /// Sets the region of interest 
00129         void setRoi(const RoI& roi);
00130         /// Returns the region of interest
00131         const RoI& getRoi() const;
00132 
00133         /// Draws a line
00134         void line(int x1, int y1, int x2, int y2);
00135 
00136         /// Save function
00137         void saveMapAsImage(const char* file) const;
00138 
00139         /// Show binMap
00140         void showMap(const char* windowname)const;
00141 
00142         /// Get the map as a new opencv image
00143         IplImage* getMapAsImage() const;
00144 
00145         /// Adds to the vector the coordinates of all the cells that have a 1 value. 
00146         int getPositives(std::vector<point>& positives) const;
00147 
00148         /// Recursive method to find clusters of connected components in the binMap
00149         int cluster(std::vector<clusterCell>& clusterList, int minSize) const;
00150 
00151         /// Counts the number of 1 values
00152         int count() const;
00153 
00154 private:
00155         /// auxiliary method to remove unconnected pixels
00156         void remUnconnectRec(int x, int y, const binMap& prevData);
00157         /// auxiliary method of the cluster function
00158         bool clustering(const int &i, const int &j, binMap& aux, clusterCell& cl, std::list<point> &seq, bool backfront) const;
00159 
00160 //--------------------------------------------------------------------- 
00161 ///////////////////////////////////////////////////////////////////////
00162 };
00163 
00164 inline void binMap::setXOrigin(float xori)              {xorigin = xori;}
00165 inline void binMap::setYOrigin(float yori)              {yorigin = yori;}
00166 inline void binMap::setResolution(float res)            {resolution = res;}
00167 inline int binMap::getWidth() const                     {return width;}
00168 inline int binMap::getHeight() const                    {return height;}
00169 inline float binMap::getXOrigin() const                 {return xorigin;}
00170 inline float binMap::getYOrigin() const                 {return yorigin;}
00171 inline float binMap::getResolution() const              {return resolution;}
00172 inline const RoI& binMap::getRoi() const                {return roi;}
00173 inline void binMap::setRoi(const RoI& r)                {this->roi = r;}
00174 
00175 inline bool binMap::get(int x, int y) const{
00176         int idx = y*width+x;
00177         div_t idx32 = div (idx, 32);
00178         return (idx32.quot>=0 && idx32.quot<totalsize) ? cells[idx32.quot][idx32.rem] : false;
00179 }
00180 
00181 inline void binMap::set(int x, int y, bool value){
00182         int idx = y*width+x;
00183         div_t idx32 = div (idx, 32);
00184         if (idx32.quot>=0 && idx32.quot<totalsize && idx32.rem >=0 && idx32.rem<32) cells[idx32.quot].set(idx32.rem,value);
00185 }
00186 
00187 #endif
 All Classes Functions Variables Typedefs