Grid2D.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2013, Fraunhofer FKIE
00003  *
00004  * Authors: Bastian Gaspers
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  * * Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * * Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * * Neither the name of the Fraunhofer FKIE nor the names of its
00015  *   contributors may be used to endorse or promote products derived from
00016  *   this software without specific prior written permission.
00017  *
00018  * This file is part of the StructureColoring ROS package.
00019  *
00020  * The StructureColoring ROS package is free software:
00021  * you can redistribute it and/or modify it under the terms of the
00022  * GNU Lesser General Public License as published by the Free
00023  * Software Foundation, either version 3 of the License, or
00024  * (at your option) any later version.
00025  *
00026  * The StructureColoring ROS package is distributed in the hope that it will be useful,
00027  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00028  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00029  * GNU Lesser General Public License for more details.
00030  *
00031  * You should have received a copy of the GNU Lesser General Public License
00032  * along with The StructureColoring ROS package.
00033  * If not, see <http://www.gnu.org/licenses/>.
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 //constructor
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 //getter
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 //calculations
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 /* GRID2D_H_ */


structure_coloring_fkie
Author(s): Bastian Gaspers
autogenerated on Sun Jan 5 2014 11:38:09