Subversion Repositories public iLand

Rev

Rev 17 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1
 
15 Werner 2
#ifndef GRID_H
3
#define GRID_H
4
 
5
#include <QPoint>
6
#include <QPointF>
7
 
8
 
9
#include <stdexcept>
10
 
11
/** Grid class (template).
12
 
13
  */
14
template <class T>
15
class Grid {
16
public:
17
 
18
    Grid();
19
    Grid(int cellsize, int sizex, int sizey) { mData=0; setup(cellsize, sizex, sizey); }
20
    ~Grid() { if (mData) delete[] mData; }
21
 
22
    bool setup(int cellsize, int sizex, int sizey);
23
    void initialize(const T& value) {for( T *p = begin();p!=end(); ++p) *p=value;}
24
 
25
    const int sizeX() const { return mSizeX; }
26
    const int sizeY() const { return mSizeY; }
27
    const float metricSizeX() const { return mSizeX*mCellsize; }
28
    const float metricSizeY() const { return mSizeY*mCellsize; }
29
    const float cellsize() const { return mCellsize; }
30
    // query
31
    T& valueAtIndex(const QPoint& pos); /// value at position defined by indices (x,y)
32
    T& valueAt(const QPointF& posf); /// value at position defined by metric coordinates
33
    QPoint indexAt(const QPointF& pos) { return QPoint(int(pos.x() / mCellsize),  int(pos.y()/mCellsize)); } /// get index of value at position pos (metric)
34
    bool isIndexValid(const QPoint& pos) { return (pos.x()>=0 && pos.x()<mSizeX && pos.y()>=0 && pos.y()<mSizeY); } /// get index of value at position pos (index)
35
    void validate(QPoint &pos) { pos.setX( qMax(qMin(pos.x(), mSizeX-1), 0) );  pos.setY( qMax(qMin(pos.y(), mSizeY-1), 0) );} /// ensure that "pos" is a valid key. if out of range, pos is set to minimum/maximum values.
36
    QPointF getCellCoordinates(const QPoint &pos) { return QPointF( (pos.x()+0.5)*mCellsize, (pos.y()+0.5)*mCellsize );} /// get metric coordinates of the cells center
37
    inline  T* begin() { return mData; } /// get "iterator" pointer
38
    inline  T* end() { return &(mData[mCount]); } /// get iterator end-pointer
39
 
40
 
41
private:
42
    T* mData;
43
    float mCellsize; /// size of a cell in meter
44
    int mSizeX; /// count of cells in x-direction
45
    int mSizeY; /// count of cells in y-direction
46
    int mCount;
47
};
48
 
49
typedef Grid<float> FloatGrid;
50
 
51
template <class T>
52
Grid<T>::Grid()
53
{
54
    mData=0; mCellsize=0.f;
55
}
56
 
57
template <class T>
58
bool Grid<T>::setup(int cellsize, int sizex, int sizey)
59
{
60
    mSizeX=sizex; mSizeY=sizey; mCellsize=(float)cellsize;
61
    mCount = mSizeX*mSizeY;
62
    if (mData)
63
         delete[] mData;
64
   if (mCount>0)
65
    mData = new T[mCount];
66
   return true;
67
}
68
template <class T>
69
T&  Grid<T>::valueAtIndex(const QPoint& pos)
70
{
71
    if (isIndexValid(pos)) {
72
        return mData[pos.x()*mSizeX + pos.y()];
73
    }
74
    throw std::logic_error("TGrid: invalid Index!");
75
}
76
 
77
template <class T>
78
T&  Grid<T>::valueAt(const QPointF& posf)
79
{
80
    return valueAtIndex( indexAt(posf) );
81
}
82
 
83
#endif // GRID_H