Go to the documentation of this file.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
00036 #ifndef GRID2D_H_
00037 #define GRID2D_H_
00038
00039 #include <vector>
00040
00041 #include <iostream>
00042
00043 template<typename CellType>
00044 class Grid2D {
00045 public:
00046 typedef CellType value_type;
00047 typedef std::vector<CellType> Cells;
00048 typedef std::pair<unsigned int, unsigned int> Indices;
00049 typedef std::pair<int, int> SignedIndices;
00050
00051
00052 Grid2D() : mGrid(0, CellType()), mWidth(0), mHeight(0){}
00053
00054 Grid2D(const unsigned int& width, const unsigned int& height, const CellType& c = CellType())
00055 : mGrid(width*height, c), mWidth(width), mHeight(height){
00056 }
00057
00058
00059 const unsigned int& getWidth() const { return mWidth; }
00060 const unsigned int& getHeight() const { return mHeight; }
00061 const Cells& getCells() const { return mGrid; }
00062 Cells& getCells() { return mGrid; }
00063
00064 typename Cells::const_reference operator() (unsigned int w, unsigned int h) const { return mGrid[getIndex(w, h, mWidth)]; }
00065 typename Cells::reference operator() (unsigned int w, unsigned int h) { return mGrid[getIndex(w, h, mWidth)]; }
00066 typename Cells::const_reference operator() (const Indices& indices) const { return (*this)(indices.first, indices.second); }
00067 typename Cells::reference operator() (const Indices& indices) { return (*this)(indices.first, indices.second); }
00068
00069
00070 void getNeighbors(Cells& cells, const SignedIndices& indices, unsigned int neighborhoodRadius) const{
00071 int wstart = std::max(int(0), std::min(int(mWidth), indices.first - int(neighborhoodRadius)));
00072 int wend = std::max(int(0), std::min(int(mWidth), indices.first + int(neighborhoodRadius)));
00073 int hstart = std::max(int(0), std::min(int(mHeight), indices.second - int(neighborhoodRadius)));
00074 int hend = std::max(int(0), std::min(int(mHeight), indices.second+ int(neighborhoodRadius)));
00075 for(int wind = wstart; wind < wend; ++wind){
00076 for(int hind = hstart; hind < hend; ++hind){
00077 if(wind < int(mWidth) && hind < int(mHeight)){
00078 cells.push_back((*this)(wind, hind));
00079 }
00080 }
00081 }
00082 }
00083
00084 protected:
00085 static unsigned int getIndex(unsigned int w, unsigned int h, unsigned int width) {
00086 return h*width+w;
00087 }
00088
00089 Cells mGrid;
00090 unsigned int mWidth;
00091 unsigned int mHeight;
00092 };
00093
00094 #endif