Rev 261 | Rev 373 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | |||
15 | Werner | 2 | #ifndef GRID_H |
3 | #define GRID_H |
||
4 | |||
22 | Werner | 5 | #include <QtCore> |
15 | Werner | 6 | |
7 | |||
8 | #include <stdexcept> |
||
145 | Werner | 9 | #include <limits> |
150 | iland | 10 | #include <cstring> |
15 | Werner | 11 | |
247 | werner | 12 | /** Grid class (template). |
15 | Werner | 13 | |
74 | Werner | 14 | Orientation |
15 | The grid is oriented as typically coordinates on the northern hemisphere: greater y-values -> north, greater x-values-> east. |
||
16 | The projection is reversed for drawing on screen (Viewport): |
||
17 | N |
||
18 | (2/0) (2/1) (2/2) |
||
19 | E (1/0) (1/1) (2/1) W |
||
20 | (0/0) (1/0) (2/0) |
||
21 | S |
||
22 | */ |
||
15 | Werner | 23 | template <class T> |
24 | class Grid { |
||
25 | public: |
||
26 | |||
27 | Grid(); |
||
28 | Grid(int cellsize, int sizex, int sizey) { mData=0; setup(cellsize, sizex, sizey); } |
||
58 | Werner | 29 | Grid(const QRectF rect_metric, const float cellsize) { mData=0; setup(rect_metric,cellsize); } |
33 | Werner | 30 | // copy ctor |
31 | Grid(const Grid<T>& toCopy); |
||
105 | Werner | 32 | ~Grid() { clear(); } |
33 | void clear() { if (mData) delete[] mData; mData=0; } |
||
15 | Werner | 34 | |
18 | Werner | 35 | bool setup(const float cellsize, const int sizex, const int sizey); |
22 | Werner | 36 | bool setup(const QRectF& rect, const double cellsize); |
75 | Werner | 37 | void initialize(const T& value) {for( T *p = begin();p!=end(); ++p) *p=value; } |
150 | iland | 38 | void wipe(); ///< write 0-bytes with memcpy to the whole area |
154 | werner | 39 | void wipe(const T value); ///< overwrite the whole area with "value" size of T must be the size of "int" ERRORNOUS!!! |
15 | Werner | 40 | |
145 | Werner | 41 | int sizeX() const { return mSizeX; } |
42 | int sizeY() const { return mSizeY; } |
||
43 | float metricSizeX() const { return mSizeX*mCellsize; } |
||
44 | float metricSizeY() const { return mSizeY*mCellsize; } |
||
49 | Werner | 45 | QRectF metricRect() const { return mRect; } |
145 | Werner | 46 | float cellsize() const { return mCellsize; } |
47 | int count() const { return mCount; } |
||
48 | bool isEmpty() const { return mData==NULL; } |
||
32 | Werner | 49 | // operations |
15 | Werner | 50 | // query |
33 | Werner | 51 | /// access (const) with index variables. use int. |
52 | inline const T& operator()(const int ix, const int iy) const { return constValueAtIndex(ix, iy); } |
||
53 | /// access (const) using metric variables. use float. |
||
54 | inline const T& operator()(const float x, const float y) const { return constValueAt(x, y); } |
||
48 | Werner | 55 | inline const T& operator[] (const QPointF &p) const { return constValueAt(p); } |
33 | Werner | 56 | |
77 | Werner | 57 | inline T& valueAtIndex(const QPoint& pos); ///< value at position defined by indices (x,y) |
33 | Werner | 58 | T& valueAtIndex(const int ix, const int iy) { return valueAtIndex(QPoint(ix,iy)); } ///< const value at position defined by indices (x,y) |
285 | werner | 59 | T& valueAtIndex(const int index) {return mData[index]; } ///< get a ref ot value at (one-dimensional) index 'index'. |
33 | Werner | 60 | |
61 | const T& constValueAtIndex(const QPoint& pos) const; ///< value at position defined by a (integer) QPoint |
||
32 | Werner | 62 | const T& constValueAtIndex(const int ix, const int iy) const { return constValueAtIndex(QPoint(ix,iy)); } |
33 | Werner | 63 | |
64 | T& valueAt(const QPointF& posf); ///< value at position defined by metric coordinates (QPointF) |
||
65 | const T& constValueAt(const QPointF& posf) const; ///< value at position defined by metric coordinates (QPointF) |
||
66 | |||
67 | T& valueAt(const float x, const float y); ///< value at position defined by metric coordinates (x,y) |
||
68 | const T& constValueAt(const float x, const float y) const; ///< value at position defined by metric coordinates (x,y) |
||
69 | |||
105 | Werner | 70 | bool coordValid(const float x, const float y) const { return x>=mRect.left() && x<mRect.right() && y>=mRect.top() && y<mRect.bottom(); } |
49 | Werner | 71 | bool coordValid(const QPointF &pos) const { return coordValid(pos.x(), pos.y()); } |
75 | Werner | 72 | |
55 | Werner | 73 | QPoint indexAt(const QPointF& pos) const { return QPoint(int((pos.x()-mRect.left()) / mCellsize), int((pos.y()-mRect.top())/mCellsize)); } ///< get index of value at position pos (metric) |
74 | bool isIndexValid(const QPoint& pos) const { return (pos.x()>=0 && pos.x()<mSizeX && pos.y()>=0 && pos.y()<mSizeY); } ///< get index of value at position pos (index) |
||
75 | Werner | 75 | /// force @param pos to contain valid indices with respect to this grid. |
55 | Werner | 76 | void validate(QPoint &pos) const{ 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. |
105 | Werner | 77 | /// get the (metric) centerpoint of cell with index @p pos |
78 | QPointF cellCenterPoint(const QPoint &pos) { return QPointF( (pos.x()+0.5)*mCellsize+mRect.left(), (pos.y()+0.5)*mCellsize + mRect.top());} ///< get metric coordinates of the cells center |
||
79 | /// get the metric rectangle of the cell with index @pos |
||
55 | Werner | 80 | QRectF cellRect(const QPoint &pos) { QRectF r( QPointF(mRect.left() + mCellsize*pos.x(), mRect.top() + pos.y()*mCellsize), |
81 | QSizeF(mCellsize, mCellsize)); return r; } ///< return coordinates of rect given by @param pos. |
||
105 | Werner | 82 | |
27 | Werner | 83 | inline T* begin() const { return mData; } ///< get "iterator" pointer |
37 | Werner | 84 | inline T* end() const { return mEnd; } ///< get iterator end-pointer |
27 | Werner | 85 | QPoint indexOf(T* element) const; ///< retrieve index (x/y) of the pointer element. returns -1/-1 if element is not valid. |
86 | // special queries |
||
33 | Werner | 87 | T max() const; ///< retrieve the maximum value of a grid |
88 | T sum() const; ///< retrieve the sum of the grid |
||
89 | T avg() const; ///< retrieve the average value of a grid |
||
90 | /// creates a grid with lower resolution and averaged cell values. |
||
91 | /// @param factor factor by which grid size is reduced (e.g. 3 -> 3x3=9 pixels are averaged to 1 result pixel) |
||
92 | /// @param offsetx, offsety: start averaging with an offset from 0/0 (e.g.: x=1, y=2, factor=3: -> 1/2-3/4 -> 0/0) |
||
93 | /// @return Grid with size sizeX()/factor x sizeY()/factor |
||
94 | Grid<T> averaged(const int factor, const int offsetx=0, const int offsety=0) const; |
||
95 | /// normalized returns a normalized grid, in a way that the sum() = @param targetvalue. |
||
96 | /// if the grid is empty or the sum is 0, no modifications are performed. |
||
97 | Grid<T> normalized(const T targetvalue) const; |
||
77 | Werner | 98 | T* ptr(int x, int y) { return &(mData[y*mSizeX + x]); } |
15 | Werner | 99 | private: |
77 | Werner | 100 | |
15 | Werner | 101 | T* mData; |
37 | Werner | 102 | T* mEnd; ///< pointer to 1 element behind the last |
49 | Werner | 103 | QRectF mRect; |
36 | Werner | 104 | float mCellsize; ///< size of a cell in meter |
105 | int mSizeX; ///< count of cells in x-direction |
||
106 | int mSizeY; ///< count of cells in y-direction |
||
107 | int mCount; ///< total number of cells in the grid |
||
15 | Werner | 108 | }; |
109 | |||
110 | typedef Grid<float> FloatGrid; |
||
111 | |||
33 | Werner | 112 | // copy constructor |
113 | template <class T> |
||
114 | Grid<T>::Grid(const Grid<T>& toCopy) |
||
115 | { |
||
40 | Werner | 116 | mData = 0; |
50 | Werner | 117 | mRect = toCopy.mRect; |
33 | Werner | 118 | setup(toCopy.cellsize(), toCopy.sizeX(), toCopy.sizeY()); |
119 | const T* end = toCopy.end(); |
||
120 | T* ptr = begin(); |
||
121 | for (T* i= toCopy.begin(); i!=end; ++i, ++ptr) |
||
122 | *ptr = *i; |
||
123 | } |
||
22 | Werner | 124 | |
33 | Werner | 125 | // normalize function |
32 | Werner | 126 | template <class T> |
33 | Werner | 127 | Grid<T> Grid<T>::normalized(const T targetvalue) const |
32 | Werner | 128 | { |
33 | Werner | 129 | Grid<T> target(*this); |
130 | T total = sum(); |
||
131 | T multiplier; |
||
132 | if (total) |
||
133 | multiplier = targetvalue / total; |
||
134 | else |
||
135 | return target; |
||
136 | for (T* p=target.begin();p!=target.end();++p) |
||
137 | *p *= multiplier; |
||
40 | Werner | 138 | return target; |
33 | Werner | 139 | } |
140 | |||
141 | |||
142 | template <class T> |
||
143 | Grid<T> Grid<T>::averaged(const int factor, const int offsetx, const int offsety) const |
||
144 | { |
||
32 | Werner | 145 | Grid<T> target; |
146 | target.setup(cellsize()*factor, sizeX()/factor, sizeY()/factor); |
||
147 | int x,y; |
||
148 | T sum=0; |
||
149 | target.initialize(sum); |
||
150 | // sum over array of 2x2, 3x3, 4x4, ... |
||
151 | for (x=offsetx;x<mSizeX;x++) |
||
152 | for (y=offsety;y<mSizeY;y++) { |
||
153 | target.valueAtIndex((x-offsetx)/factor, (y-offsety)/factor) += constValueAtIndex(x,y); |
||
154 | } |
||
155 | // divide |
||
156 | double fsquare = factor*factor; |
||
157 | for (T* p=target.begin();p!=target.end();++p) |
||
158 | *p /= fsquare; |
||
159 | return target; |
||
160 | } |
||
22 | Werner | 161 | |
15 | Werner | 162 | template <class T> |
22 | Werner | 163 | T& Grid<T>::valueAtIndex(const QPoint& pos) |
164 | { |
||
75 | Werner | 165 | //if (isIndexValid(pos)) { |
77 | Werner | 166 | return mData[pos.y()*mSizeX + pos.x()]; |
75 | Werner | 167 | //} |
168 | //qCritical("Grid::valueAtIndex. invalid: %d/%d", pos.x(), pos.y()); |
||
169 | //return mData[0]; |
||
22 | Werner | 170 | } |
36 | Werner | 171 | |
27 | Werner | 172 | template <class T> |
173 | const T& Grid<T>::constValueAtIndex(const QPoint& pos) const |
||
174 | { |
||
75 | Werner | 175 | //if (isIndexValid(pos)) { |
77 | Werner | 176 | return mData[pos.y()*mSizeX + pos.x()]; |
75 | Werner | 177 | //} |
178 | //qCritical("Grid::constValueAtIndex. invalid: %d/%d", pos.x(), pos.y()); |
||
179 | //return mData[0]; |
||
27 | Werner | 180 | } |
22 | Werner | 181 | |
182 | template <class T> |
||
33 | Werner | 183 | T& Grid<T>::valueAt(const float x, const float y) |
184 | { |
||
185 | return valueAtIndex( indexAt(QPointF(x,y)) ); |
||
186 | } |
||
36 | Werner | 187 | |
33 | Werner | 188 | template <class T> |
189 | const T& Grid<T>::constValueAt(const float x, const float y) const |
||
190 | { |
||
191 | return constValueAtIndex( indexAt(QPointF(x,y)) ); |
||
192 | } |
||
36 | Werner | 193 | |
33 | Werner | 194 | template <class T> |
22 | Werner | 195 | T& Grid<T>::valueAt(const QPointF& posf) |
196 | { |
||
197 | return valueAtIndex( indexAt(posf) ); |
||
198 | } |
||
36 | Werner | 199 | |
33 | Werner | 200 | template <class T> |
201 | const T& Grid<T>::constValueAt(const QPointF& posf) const |
||
202 | { |
||
203 | return constValueAtIndex( indexAt(posf) ); |
||
204 | } |
||
22 | Werner | 205 | |
206 | template <class T> |
||
15 | Werner | 207 | Grid<T>::Grid() |
208 | { |
||
37 | Werner | 209 | mData = 0; mCellsize=0.f; |
210 | mEnd = 0; |
||
15 | Werner | 211 | } |
212 | |||
213 | template <class T> |
||
18 | Werner | 214 | bool Grid<T>::setup(const float cellsize, const int sizex, const int sizey) |
15 | Werner | 215 | { |
37 | Werner | 216 | mSizeX=sizex; mSizeY=sizey; mCellsize=cellsize; |
50 | Werner | 217 | if (mRect.isNull()) // only set rect if not set before |
218 | mRect.setCoords(0., 0., cellsize*sizex, cellsize*sizey); |
||
15 | Werner | 219 | mCount = mSizeX*mSizeY; |
37 | Werner | 220 | if (mData) { |
221 | delete[] mData; mData=NULL; |
||
222 | } |
||
15 | Werner | 223 | if (mCount>0) |
37 | Werner | 224 | mData = new T[mCount]; |
225 | mEnd = &(mData[mCount]); |
||
15 | Werner | 226 | return true; |
227 | } |
||
228 | |||
229 | template <class T> |
||
22 | Werner | 230 | bool Grid<T>::setup(const QRectF& rect, const double cellsize) |
15 | Werner | 231 | { |
49 | Werner | 232 | mRect = rect; |
22 | Werner | 233 | int dx = int(rect.width()/cellsize); |
49 | Werner | 234 | if (mRect.left()+cellsize*dx<rect.right()) |
22 | Werner | 235 | dx++; |
236 | int dy = int(rect.height()/cellsize); |
||
49 | Werner | 237 | if (mRect.top()+cellsize*dy<rect.bottom()) |
22 | Werner | 238 | dy++; |
239 | return setup(cellsize, dx, dy); |
||
15 | Werner | 240 | } |
241 | |||
261 | werner | 242 | /** retrieve from the index from an element reversely from a pointer to that element. |
243 | The internal memory layout is (for dimx=6, dimy=3): |
||
244 | |||
245 | 6 7 8 9 10 11 |
||
246 | 12 13 14 15 16 17 |
||
247 | Note: north and south are reversed, thus the item with index 0 is located in the south-western edge of the grid! */ |
||
25 | Werner | 248 | template <class T> |
27 | Werner | 249 | QPoint Grid<T>::indexOf(T* element) const |
25 | Werner | 250 | { |
251 | QPoint result(-1,-1); |
||
252 | if (element==NULL || element<mData || element>=end()) |
||
253 | return result; |
||
254 | int idx = element - mData; |
||
105 | Werner | 255 | result.setX( idx % mSizeX); |
256 | result.setY( idx / mSizeX); |
||
25 | Werner | 257 | return result; |
258 | } |
||
22 | Werner | 259 | |
27 | Werner | 260 | template <class T> |
261 | T Grid<T>::max() const |
||
262 | { |
||
143 | Werner | 263 | T maxv = -std::numeric_limits<T>::max(); |
27 | Werner | 264 | T* p; |
265 | T* pend = end(); |
||
266 | for (p=begin(); p!=pend;++p) |
||
267 | maxv = std::max(maxv, *p); |
||
268 | return maxv; |
||
269 | } |
||
270 | |||
33 | Werner | 271 | template <class T> |
272 | T Grid<T>::sum() const |
||
273 | { |
||
274 | T* pend = end(); |
||
275 | T total = 0; |
||
276 | for (T *p=begin(); p!=pend;++p) |
||
277 | total += *p; |
||
278 | return total; |
||
279 | } |
||
280 | |||
281 | template <class T> |
||
282 | T Grid<T>::avg() const |
||
283 | { |
||
284 | if (count()) |
||
285 | return sum() / T(count()); |
||
286 | else return 0; |
||
287 | } |
||
288 | |||
150 | iland | 289 | template <class T> |
290 | void Grid<T>::wipe() |
||
291 | { |
||
292 | memset(mData, 0, mCount*sizeof(T)); |
||
293 | } |
||
294 | template <class T> |
||
295 | void Grid<T>::wipe(const T value) |
||
296 | { |
||
154 | werner | 297 | /* this does not work properly !!! */ |
153 | werner | 298 | if (sizeof(T)==sizeof(int)) { |
299 | float temp = value; |
||
300 | float *pf = &temp; |
||
301 | |||
302 | memset(mData, *((int*)pf), mCount*sizeof(T)); |
||
303 | } else |
||
150 | iland | 304 | initialize(value); |
305 | } |
||
306 | |||
36 | Werner | 307 | ////////////////////////////////////////////////////////////7 |
308 | // global functions |
||
309 | ////////////////////////////////////////////////////////////7 |
||
310 | |||
311 | /// dumps a FloatGrid to a String. |
||
46 | Werner | 312 | /// rows will be y-lines, columns x-values. (see grid.cpp) |
36 | Werner | 313 | QString gridToString(const FloatGrid &grid); |
314 | |||
315 | /// creates and return a QImage from Grid-Data. |
||
316 | /// @param black_white true: max_value = white, min_value = black, false: color-mode: uses a HSV-color model from blue (min_value) to red (max_value), default: color mode (false) |
||
317 | /// @param min_value, max_value min/max bounds for color calcuations. values outside bounds are limited to these values. defaults: min=0, max=1 |
||
318 | /// @param reverse if true, color ramps are inversed (to: min_value = white (black and white mode) or red (color mode). default = false. |
||
319 | /// @return a QImage with the Grids size of pixels. Pixel coordinates relate to the index values of the grid. |
||
320 | QImage gridToImage(const FloatGrid &grid, |
||
321 | bool black_white=false, |
||
322 | double min_value=0., double max_value=1., |
||
323 | bool reverse=false); |
||
324 | |||
285 | werner | 325 | /** load into 'rGrid' the content of the image pointed at by 'fileName'. |
326 | Pixels are converted to grey-scale and then transformend to a value ranging from 0..1 (black..white). |
||
327 | */ |
||
328 | bool loadGridFromImage(const QString &fileName, FloatGrid &rGrid); |
||
329 | |||
46 | Werner | 330 | /// template version for non-float grids (see also version for FloatGrid) |
36 | Werner | 331 | template <class T> |
46 | Werner | 332 | QString gridToString(const Grid<T> &grid) |
36 | Werner | 333 | { |
334 | QString res; |
||
335 | QTextStream ts(&res); |
||
336 | |||
46 | Werner | 337 | for (int y=0;y<grid.sizeY();y++){ |
338 | for (int x=0;x<grid.sizeX();x++){ |
||
36 | Werner | 339 | ts << grid.constValueAtIndex(x,y) << ";"; |
340 | } |
||
341 | ts << "\r\n"; |
||
342 | } |
||
343 | |||
344 | return res; |
||
345 | } |
||
46 | Werner | 346 | |
15 | Werner | 347 | #endif // GRID_H |