Rev 651 | Rev 662 | 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 | |
373 | werner | 12 | #include "global.h" |
13 | |||
247 | werner | 14 | /** Grid class (template). |
15 | Werner | 15 | |
74 | Werner | 16 | Orientation |
656 | werner | 17 | The grid is oriented as typically coordinates on the northern hemisphere: higher y-values -> north, higher x-values-> east. |
490 | werner | 18 | The projection is reversed for drawing on screen (Viewport). |
74 | Werner | 19 | N |
490 | werner | 20 | (0/2) (1/2) (2/2) |
656 | werner | 21 | W (0/1) (1/1) (2/1) E |
74 | Werner | 22 | (0/0) (1/0) (2/0) |
23 | S |
||
24 | */ |
||
15 | Werner | 25 | template <class T> |
26 | class Grid { |
||
27 | public: |
||
28 | |||
29 | Grid(); |
||
30 | Grid(int cellsize, int sizex, int sizey) { mData=0; setup(cellsize, sizex, sizey); } |
||
58 | Werner | 31 | Grid(const QRectF rect_metric, const float cellsize) { mData=0; setup(rect_metric,cellsize); } |
33 | Werner | 32 | // copy ctor |
33 | Grid(const Grid<T>& toCopy); |
||
105 | Werner | 34 | ~Grid() { clear(); } |
35 | void clear() { if (mData) delete[] mData; mData=0; } |
||
15 | Werner | 36 | |
18 | Werner | 37 | bool setup(const float cellsize, const int sizex, const int sizey); |
22 | Werner | 38 | bool setup(const QRectF& rect, const double cellsize); |
453 | werner | 39 | bool setup(Grid<T>& source) { mData = 0; mRect = source.mRect; return setup(source.cellsize(), source.sizeX(), source.sizeY()); } |
75 | Werner | 40 | void initialize(const T& value) {for( T *p = begin();p!=end(); ++p) *p=value; } |
150 | iland | 41 | void wipe(); ///< write 0-bytes with memcpy to the whole area |
154 | werner | 42 | void wipe(const T value); ///< overwrite the whole area with "value" size of T must be the size of "int" ERRORNOUS!!! |
15 | Werner | 43 | |
145 | Werner | 44 | int sizeX() const { return mSizeX; } |
45 | int sizeY() const { return mSizeY; } |
||
46 | float metricSizeX() const { return mSizeX*mCellsize; } |
||
47 | float metricSizeY() const { return mSizeY*mCellsize; } |
||
49 | Werner | 48 | QRectF metricRect() const { return mRect; } |
145 | Werner | 49 | float cellsize() const { return mCellsize; } |
373 | werner | 50 | int count() const { return mCount; } ///< returns the number of elements of the grid |
51 | bool isEmpty() const { return mData==NULL; } ///< returns false if the grid was not setup |
||
32 | Werner | 52 | // operations |
15 | Werner | 53 | // query |
33 | Werner | 54 | /// access (const) with index variables. use int. |
55 | inline const T& operator()(const int ix, const int iy) const { return constValueAtIndex(ix, iy); } |
||
56 | /// access (const) using metric variables. use float. |
||
57 | inline const T& operator()(const float x, const float y) const { return constValueAt(x, y); } |
||
48 | Werner | 58 | inline const T& operator[] (const QPointF &p) const { return constValueAt(p); } |
33 | Werner | 59 | |
77 | Werner | 60 | inline T& valueAtIndex(const QPoint& pos); ///< value at position defined by indices (x,y) |
33 | Werner | 61 | T& valueAtIndex(const int ix, const int iy) { return valueAtIndex(QPoint(ix,iy)); } ///< const value at position defined by indices (x,y) |
285 | werner | 62 | T& valueAtIndex(const int index) {return mData[index]; } ///< get a ref ot value at (one-dimensional) index 'index'. |
33 | Werner | 63 | |
64 | const T& constValueAtIndex(const QPoint& pos) const; ///< value at position defined by a (integer) QPoint |
||
32 | Werner | 65 | const T& constValueAtIndex(const int ix, const int iy) const { return constValueAtIndex(QPoint(ix,iy)); } |
549 | werner | 66 | const T& constValueAtIndex(const int index) const {return mData[index]; } ///< get a ref ot value at (one-dimensional) index 'index'. |
33 | Werner | 67 | |
68 | T& valueAt(const QPointF& posf); ///< value at position defined by metric coordinates (QPointF) |
||
69 | const T& constValueAt(const QPointF& posf) const; ///< value at position defined by metric coordinates (QPointF) |
||
70 | |||
71 | T& valueAt(const float x, const float y); ///< value at position defined by metric coordinates (x,y) |
||
72 | const T& constValueAt(const float x, const float y) const; ///< value at position defined by metric coordinates (x,y) |
||
73 | |||
105 | Werner | 74 | bool coordValid(const float x, const float y) const { return x>=mRect.left() && x<mRect.right() && y>=mRect.top() && y<mRect.bottom(); } |
49 | Werner | 75 | bool coordValid(const QPointF &pos) const { return coordValid(pos.x(), pos.y()); } |
75 | Werner | 76 | |
55 | Werner | 77 | 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) |
538 | werner | 78 | /// get index (x/y) of the (linear) index 'index' (0..count-1) |
79 | QPoint indexOf(const int index) const {return QPoint(index % mSizeX, index / mSizeX); } |
||
373 | werner | 80 | bool isIndexValid(const QPoint& pos) const { return (pos.x()>=0 && pos.x()<mSizeX && pos.y()>=0 && pos.y()<mSizeY); } ///< return true, if position is within the grid |
81 | bool isIndexValid(const int x, const int y) const {return (x>=0 && x<mSizeX && y>=0 && y<mSizeY); } ///< return true, if index is within the grid |
||
75 | Werner | 82 | /// force @param pos to contain valid indices with respect to this grid. |
55 | Werner | 83 | 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 | 84 | /// get the (metric) centerpoint of cell with index @p pos |
549 | werner | 85 | QPointF cellCenterPoint(const QPoint &pos) const { return QPointF( (pos.x()+0.5)*mCellsize+mRect.left(), (pos.y()+0.5)*mCellsize + mRect.top());} ///< get metric coordinates of the cells center |
105 | Werner | 86 | /// get the metric rectangle of the cell with index @pos |
439 | werner | 87 | QRectF cellRect(const QPoint &pos) const { QRectF r( QPointF(mRect.left() + mCellsize*pos.x(), mRect.top() + pos.y()*mCellsize), |
55 | Werner | 88 | QSizeF(mCellsize, mCellsize)); return r; } ///< return coordinates of rect given by @param pos. |
105 | Werner | 89 | |
27 | Werner | 90 | inline T* begin() const { return mData; } ///< get "iterator" pointer |
37 | Werner | 91 | inline T* end() const { return mEnd; } ///< get iterator end-pointer |
487 | werner | 92 | inline QPoint indexOf(T* element) const; ///< retrieve index (x/y) of the pointer element. returns -1/-1 if element is not valid. |
27 | Werner | 93 | // special queries |
33 | Werner | 94 | T max() const; ///< retrieve the maximum value of a grid |
95 | T sum() const; ///< retrieve the sum of the grid |
||
96 | T avg() const; ///< retrieve the average value of a grid |
||
391 | werner | 97 | // modifying operations |
98 | void add(const T& summand); |
||
99 | void multiply(const T& factor); |
||
33 | Werner | 100 | /// creates a grid with lower resolution and averaged cell values. |
101 | /// @param factor factor by which grid size is reduced (e.g. 3 -> 3x3=9 pixels are averaged to 1 result pixel) |
||
102 | /// @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) |
||
103 | /// @return Grid with size sizeX()/factor x sizeY()/factor |
||
104 | Grid<T> averaged(const int factor, const int offsetx=0, const int offsety=0) const; |
||
105 | /// normalized returns a normalized grid, in a way that the sum() = @param targetvalue. |
||
106 | /// if the grid is empty or the sum is 0, no modifications are performed. |
||
107 | Grid<T> normalized(const T targetvalue) const; |
||
373 | werner | 108 | T* ptr(int x, int y) { return &(mData[y*mSizeX + x]); } ///< get a pointer to the element denoted by "x" and "y" |
109 | inline double distance(const QPoint &p1, const QPoint &p2); ///< distance (metric) between p1 and p2 |
||
110 | const QPoint randomPosition() const; ///< returns a (valid) random position within the grid |
||
15 | Werner | 111 | private: |
77 | Werner | 112 | |
15 | Werner | 113 | T* mData; |
37 | Werner | 114 | T* mEnd; ///< pointer to 1 element behind the last |
49 | Werner | 115 | QRectF mRect; |
36 | Werner | 116 | float mCellsize; ///< size of a cell in meter |
117 | int mSizeX; ///< count of cells in x-direction |
||
118 | int mSizeY; ///< count of cells in y-direction |
||
119 | int mCount; ///< total number of cells in the grid |
||
15 | Werner | 120 | }; |
121 | |||
122 | typedef Grid<float> FloatGrid; |
||
123 | |||
643 | werner | 124 | enum GridViewType { GridViewRainbow, GridViewRainbowReverse, GridViewGray, GridViewGrayReverse }; |
125 | |||
438 | werner | 126 | /** @class GridRunner is a helper class to iterate over a rectangular fraction of a grid |
127 | */ |
||
128 | template <class T> |
||
129 | class GridRunner { |
||
130 | public: |
||
650 | werner | 131 | // constructors with a QRectF (metric coordinates) |
617 | werner | 132 | GridRunner(Grid<T> &target_grid, const QRectF &rectangle) {setup(&target_grid, rectangle);} |
133 | GridRunner(const Grid<T> &target_grid, const QRectF &rectangle) {setup(&target_grid, rectangle);} |
||
134 | GridRunner(Grid<T> *target_grid, const QRectF &rectangle) {setup(target_grid, rectangle);} |
||
650 | werner | 135 | // constructors with a QRect (indices within the grid) |
136 | GridRunner(Grid<T> &target_grid, const QRect &rectangle) {setup(&target_grid, rectangle);} |
||
137 | GridRunner(const Grid<T> &target_grid, const QRect &rectangle) {setup(&target_grid, rectangle);} |
||
138 | GridRunner(Grid<T> *target_grid, const QRect &rectangle) {setup(target_grid, rectangle);} |
||
438 | werner | 139 | T* next(); ///< to to next element, return NULL if finished |
574 | werner | 140 | T* current() { return mCurrent; } |
650 | werner | 141 | void reset() { mCurrent = mFirst-1; mCurrentCol = -1; } |
142 | // helpers |
||
143 | /// fill array with pointers to neighbors (north, east, west, south) |
||
144 | /// or Null-pointers if out of range. |
||
145 | /// the target array (rArray) is not checked and must be valid! |
||
146 | void neighbors4(T** rArray); |
||
147 | void neighbors8(T** rArray); |
||
438 | werner | 148 | private: |
617 | werner | 149 | void setup(const Grid<T> *target_grid, const QRectF &rectangle); |
650 | werner | 150 | void setup(const Grid<T> *target_grid, const QRect &rectangle); |
151 | T* mFirst; // points to the first element of the grid |
||
152 | T* mLast; // points to the last element of the grid |
||
438 | werner | 153 | T* mCurrent; |
154 | size_t mLineLength; |
||
155 | size_t mCols; |
||
156 | size_t mCurrentCol; |
||
157 | }; |
||
158 | |||
646 | werner | 159 | /** @class Vector3D is a simple 3d vector. |
160 | QVector3D (from Qt) is in QtGui so we needed a replacement. |
||
161 | */ |
||
162 | class Vector3D |
||
163 | { |
||
164 | public: |
||
165 | Vector3D(): mX(0.), mY(0.), mZ(0.) {} |
||
166 | Vector3D(const double x, const double y, const double z): mX(x), mY(y), mZ(z) {} |
||
167 | double x() const { return mX; } ///< get x-coordinate |
||
168 | double y() const { return mY; } ///< get y-coordinate |
||
169 | double z() const { return mZ; } ///< get z-coordinate |
||
170 | // set variables |
||
171 | void setX(const double x) { mX=x; } ///< set value of the x-coordinate |
||
172 | void setY(const double y) { mY=y; } ///< set value of the y-coordinate |
||
173 | void setZ(const double z) { mZ=z; } ///< set value of the z-coordinate |
||
174 | private: |
||
175 | double mX; |
||
176 | double mY; |
||
177 | double mZ; |
||
178 | }; |
||
438 | werner | 179 | |
33 | Werner | 180 | // copy constructor |
181 | template <class T> |
||
182 | Grid<T>::Grid(const Grid<T>& toCopy) |
||
183 | { |
||
40 | Werner | 184 | mData = 0; |
50 | Werner | 185 | mRect = toCopy.mRect; |
33 | Werner | 186 | setup(toCopy.cellsize(), toCopy.sizeX(), toCopy.sizeY()); |
187 | const T* end = toCopy.end(); |
||
188 | T* ptr = begin(); |
||
189 | for (T* i= toCopy.begin(); i!=end; ++i, ++ptr) |
||
190 | *ptr = *i; |
||
191 | } |
||
22 | Werner | 192 | |
33 | Werner | 193 | // normalize function |
32 | Werner | 194 | template <class T> |
33 | Werner | 195 | Grid<T> Grid<T>::normalized(const T targetvalue) const |
32 | Werner | 196 | { |
33 | Werner | 197 | Grid<T> target(*this); |
198 | T total = sum(); |
||
199 | T multiplier; |
||
200 | if (total) |
||
201 | multiplier = targetvalue / total; |
||
202 | else |
||
203 | return target; |
||
204 | for (T* p=target.begin();p!=target.end();++p) |
||
205 | *p *= multiplier; |
||
40 | Werner | 206 | return target; |
33 | Werner | 207 | } |
208 | |||
209 | |||
210 | template <class T> |
||
211 | Grid<T> Grid<T>::averaged(const int factor, const int offsetx, const int offsety) const |
||
212 | { |
||
32 | Werner | 213 | Grid<T> target; |
214 | target.setup(cellsize()*factor, sizeX()/factor, sizeY()/factor); |
||
215 | int x,y; |
||
216 | T sum=0; |
||
217 | target.initialize(sum); |
||
218 | // sum over array of 2x2, 3x3, 4x4, ... |
||
219 | for (x=offsetx;x<mSizeX;x++) |
||
220 | for (y=offsety;y<mSizeY;y++) { |
||
221 | target.valueAtIndex((x-offsetx)/factor, (y-offsety)/factor) += constValueAtIndex(x,y); |
||
222 | } |
||
223 | // divide |
||
224 | double fsquare = factor*factor; |
||
225 | for (T* p=target.begin();p!=target.end();++p) |
||
226 | *p /= fsquare; |
||
227 | return target; |
||
228 | } |
||
22 | Werner | 229 | |
15 | Werner | 230 | template <class T> |
22 | Werner | 231 | T& Grid<T>::valueAtIndex(const QPoint& pos) |
232 | { |
||
75 | Werner | 233 | //if (isIndexValid(pos)) { |
77 | Werner | 234 | return mData[pos.y()*mSizeX + pos.x()]; |
75 | Werner | 235 | //} |
236 | //qCritical("Grid::valueAtIndex. invalid: %d/%d", pos.x(), pos.y()); |
||
237 | //return mData[0]; |
||
22 | Werner | 238 | } |
36 | Werner | 239 | |
27 | Werner | 240 | template <class T> |
241 | const T& Grid<T>::constValueAtIndex(const QPoint& pos) const |
||
242 | { |
||
75 | Werner | 243 | //if (isIndexValid(pos)) { |
77 | Werner | 244 | return mData[pos.y()*mSizeX + pos.x()]; |
75 | Werner | 245 | //} |
246 | //qCritical("Grid::constValueAtIndex. invalid: %d/%d", pos.x(), pos.y()); |
||
247 | //return mData[0]; |
||
27 | Werner | 248 | } |
22 | Werner | 249 | |
250 | template <class T> |
||
33 | Werner | 251 | T& Grid<T>::valueAt(const float x, const float y) |
252 | { |
||
253 | return valueAtIndex( indexAt(QPointF(x,y)) ); |
||
254 | } |
||
36 | Werner | 255 | |
33 | Werner | 256 | template <class T> |
257 | const T& Grid<T>::constValueAt(const float x, const float y) const |
||
258 | { |
||
259 | return constValueAtIndex( indexAt(QPointF(x,y)) ); |
||
260 | } |
||
36 | Werner | 261 | |
33 | Werner | 262 | template <class T> |
22 | Werner | 263 | T& Grid<T>::valueAt(const QPointF& posf) |
264 | { |
||
265 | return valueAtIndex( indexAt(posf) ); |
||
266 | } |
||
36 | Werner | 267 | |
33 | Werner | 268 | template <class T> |
269 | const T& Grid<T>::constValueAt(const QPointF& posf) const |
||
270 | { |
||
271 | return constValueAtIndex( indexAt(posf) ); |
||
272 | } |
||
22 | Werner | 273 | |
274 | template <class T> |
||
15 | Werner | 275 | Grid<T>::Grid() |
276 | { |
||
37 | Werner | 277 | mData = 0; mCellsize=0.f; |
278 | mEnd = 0; |
||
15 | Werner | 279 | } |
280 | |||
281 | template <class T> |
||
18 | Werner | 282 | bool Grid<T>::setup(const float cellsize, const int sizex, const int sizey) |
15 | Werner | 283 | { |
37 | Werner | 284 | mSizeX=sizex; mSizeY=sizey; mCellsize=cellsize; |
50 | Werner | 285 | if (mRect.isNull()) // only set rect if not set before |
286 | mRect.setCoords(0., 0., cellsize*sizex, cellsize*sizey); |
||
15 | Werner | 287 | mCount = mSizeX*mSizeY; |
37 | Werner | 288 | if (mData) { |
289 | delete[] mData; mData=NULL; |
||
290 | } |
||
15 | Werner | 291 | if (mCount>0) |
37 | Werner | 292 | mData = new T[mCount]; |
293 | mEnd = &(mData[mCount]); |
||
15 | Werner | 294 | return true; |
295 | } |
||
296 | |||
297 | template <class T> |
||
22 | Werner | 298 | bool Grid<T>::setup(const QRectF& rect, const double cellsize) |
15 | Werner | 299 | { |
49 | Werner | 300 | mRect = rect; |
22 | Werner | 301 | int dx = int(rect.width()/cellsize); |
49 | Werner | 302 | if (mRect.left()+cellsize*dx<rect.right()) |
22 | Werner | 303 | dx++; |
304 | int dy = int(rect.height()/cellsize); |
||
49 | Werner | 305 | if (mRect.top()+cellsize*dy<rect.bottom()) |
22 | Werner | 306 | dy++; |
307 | return setup(cellsize, dx, dy); |
||
15 | Werner | 308 | } |
309 | |||
261 | werner | 310 | /** retrieve from the index from an element reversely from a pointer to that element. |
311 | The internal memory layout is (for dimx=6, dimy=3): |
||
312 | |||
313 | 6 7 8 9 10 11 |
||
314 | 12 13 14 15 16 17 |
||
315 | Note: north and south are reversed, thus the item with index 0 is located in the south-western edge of the grid! */ |
||
487 | werner | 316 | template <class T> inline |
27 | Werner | 317 | QPoint Grid<T>::indexOf(T* element) const |
25 | Werner | 318 | { |
487 | werner | 319 | // QPoint result(-1,-1); |
25 | Werner | 320 | if (element==NULL || element<mData || element>=end()) |
487 | werner | 321 | return QPoint(-1, -1); |
25 | Werner | 322 | int idx = element - mData; |
487 | werner | 323 | return QPoint(idx % mSizeX, idx / mSizeX); |
324 | // result.setX( idx % mSizeX); |
||
325 | // result.setY( idx / mSizeX); |
||
326 | // return result; |
||
25 | Werner | 327 | } |
22 | Werner | 328 | |
27 | Werner | 329 | template <class T> |
330 | T Grid<T>::max() const |
||
331 | { |
||
143 | Werner | 332 | T maxv = -std::numeric_limits<T>::max(); |
27 | Werner | 333 | T* p; |
334 | T* pend = end(); |
||
335 | for (p=begin(); p!=pend;++p) |
||
336 | maxv = std::max(maxv, *p); |
||
337 | return maxv; |
||
338 | } |
||
339 | |||
33 | Werner | 340 | template <class T> |
341 | T Grid<T>::sum() const |
||
342 | { |
||
343 | T* pend = end(); |
||
344 | T total = 0; |
||
345 | for (T *p=begin(); p!=pend;++p) |
||
346 | total += *p; |
||
347 | return total; |
||
348 | } |
||
349 | |||
350 | template <class T> |
||
351 | T Grid<T>::avg() const |
||
352 | { |
||
353 | if (count()) |
||
354 | return sum() / T(count()); |
||
355 | else return 0; |
||
356 | } |
||
357 | |||
150 | iland | 358 | template <class T> |
391 | werner | 359 | void Grid<T>::add(const T& summand) |
360 | { |
||
361 | T* pend = end(); |
||
362 | for (T *p=begin(); p!=pend;*p+=summand,++p) |
||
363 | ; |
||
364 | } |
||
365 | |||
366 | template <class T> |
||
367 | void Grid<T>::multiply(const T& factor) |
||
368 | { |
||
369 | T* pend = end(); |
||
370 | for (T *p=begin(); p!=pend;*p*=factor,++p) |
||
371 | ; |
||
372 | } |
||
373 | |||
374 | |||
375 | |||
376 | template <class T> |
||
150 | iland | 377 | void Grid<T>::wipe() |
378 | { |
||
379 | memset(mData, 0, mCount*sizeof(T)); |
||
380 | } |
||
381 | template <class T> |
||
382 | void Grid<T>::wipe(const T value) |
||
383 | { |
||
154 | werner | 384 | /* this does not work properly !!! */ |
153 | werner | 385 | if (sizeof(T)==sizeof(int)) { |
386 | float temp = value; |
||
387 | float *pf = &temp; |
||
388 | |||
389 | memset(mData, *((int*)pf), mCount*sizeof(T)); |
||
390 | } else |
||
150 | iland | 391 | initialize(value); |
392 | } |
||
393 | |||
373 | werner | 394 | template <class T> |
395 | double Grid<T>::distance(const QPoint &p1, const QPoint &p2) |
||
396 | { |
||
397 | QPointF fp1=cellCenterPoint(p1); |
||
398 | QPointF fp2=cellCenterPoint(p2); |
||
399 | double distance = sqrt( (fp1.x()-fp2.x())*(fp1.x()-fp2.x()) + (fp1.y()-fp2.y())*(fp1.y()-fp2.y())); |
||
400 | return distance; |
||
401 | } |
||
402 | |||
403 | template <class T> |
||
404 | const QPoint Grid<T>::randomPosition() const |
||
405 | { |
||
406 | return QPoint(irandom(0,mSizeX-1), irandom(0, mSizeY-1)); |
||
407 | } |
||
438 | werner | 408 | |
373 | werner | 409 | //////////////////////////////////////////////////////////// |
438 | werner | 410 | // grid runner |
411 | //////////////////////////////////////////////////////////// |
||
412 | template <class T> |
||
650 | werner | 413 | void GridRunner<T>::setup(const Grid<T> *target_grid, const QRect &rectangle) |
438 | werner | 414 | { |
650 | werner | 415 | QPoint upper_left = rectangle.topLeft(); |
651 | werner | 416 | // due to the strange behavior of QRect::bottom() and right(): |
650 | werner | 417 | QPoint lower_right = rectangle.bottomRight(); |
617 | werner | 418 | mCurrent = const_cast<Grid<T> *>(target_grid)->ptr(upper_left.x(), upper_left.y()); |
650 | werner | 419 | mFirst = mCurrent; |
585 | werner | 420 | mCurrent--; // point to first element -1 |
617 | werner | 421 | mLast = const_cast<Grid<T> *>(target_grid)->ptr(lower_right.x()-1, lower_right.y()-1); |
438 | werner | 422 | mCols = lower_right.x() - upper_left.x(); // |
617 | werner | 423 | mLineLength = target_grid->sizeX() - mCols; |
585 | werner | 424 | mCurrentCol = -1; |
425 | // qDebug() << "GridRunner: rectangle:" << rectangle |
||
426 | // << "upper_left:" << target_grid.cellCenterPoint(target_grid.indexOf(mCurrent)) |
||
427 | // << "lower_right:" << target_grid.cellCenterPoint(target_grid.indexOf(mLast)); |
||
438 | werner | 428 | } |
429 | |||
430 | template <class T> |
||
650 | werner | 431 | void GridRunner<T>::setup(const Grid<T> *target_grid, const QRectF &rectangle_metric) |
432 | { |
||
433 | QRect rect(target_grid->indexAt(rectangle_metric.topLeft()), |
||
434 | target_grid->indexAt(rectangle_metric.bottomRight()) ); |
||
435 | setup (target_grid, rect); |
||
436 | } |
||
437 | |||
438 | template <class T> |
||
438 | werner | 439 | T* GridRunner<T>::next() |
440 | { |
||
441 | if (mCurrent>mLast) |
||
442 | return NULL; |
||
443 | mCurrent++; |
||
444 | mCurrentCol++; |
||
585 | werner | 445 | |
438 | werner | 446 | if (mCurrentCol >= mCols) { |
447 | mCurrent += mLineLength; // skip to next line |
||
448 | mCurrentCol = 0; |
||
449 | } |
||
585 | werner | 450 | if (mCurrent>mLast) |
451 | return NULL; |
||
452 | else |
||
453 | return mCurrent; |
||
438 | werner | 454 | } |
455 | |||
650 | werner | 456 | template <class T> |
656 | werner | 457 | /// get pointers the the 4-neighborhood |
458 | /// north, east, south, west |
||
650 | werner | 459 | void GridRunner<T>::neighbors4(T** rArray) |
460 | { |
||
461 | // north: |
||
651 | werner | 462 | rArray[0] = mCurrent + mCols + mLineLength > mLast?0: mCurrent + mCols + mLineLength; |
650 | werner | 463 | // south: |
651 | werner | 464 | rArray[3] = mCurrent - (mCols + mLineLength) < mFirst?0: mCurrent - (mCols + mLineLength); |
650 | werner | 465 | // east / west |
656 | werner | 466 | rArray[1] = mCurrentCol<mCols? mCurrent + 1 : 0; |
467 | rArray[2] = mCurrentCol>0? mCurrent-1 : 0; |
||
650 | werner | 468 | } |
469 | |||
470 | /// get pointers to the 8-neighbor-hood |
||
471 | /// north/east/west/south/NE/NW/SE/SW |
||
472 | template <class T> |
||
473 | void GridRunner<T>::neighbors8(T** rArray) |
||
474 | { |
||
475 | neighbors4(rArray); |
||
476 | // north-east |
||
656 | werner | 477 | rArray[4] = rArray[0] && rArray[1]? rArray[0]+1: 0; |
650 | werner | 478 | // north-west |
656 | werner | 479 | rArray[5] = rArray[0] && rArray[2]? rArray[0]-1: 0; |
650 | werner | 480 | // south-east |
656 | werner | 481 | rArray[6] = rArray[3] && rArray[1]? rArray[3]+1: 0; |
650 | werner | 482 | // south-west |
656 | werner | 483 | rArray[7] = rArray[3] && rArray[2]? rArray[3]-1: 0; |
650 | werner | 484 | |
485 | } |
||
486 | |||
438 | werner | 487 | //////////////////////////////////////////////////////////// |
36 | Werner | 488 | // global functions |
373 | werner | 489 | //////////////////////////////////////////////////////////// |
36 | Werner | 490 | |
491 | /// dumps a FloatGrid to a String. |
||
46 | Werner | 492 | /// rows will be y-lines, columns x-values. (see grid.cpp) |
599 | werner | 493 | QString gridToString(const FloatGrid &grid, const QChar sep=QChar(';'), const int newline_after=-1); |
36 | Werner | 494 | |
495 | /// creates and return a QImage from Grid-Data. |
||
496 | /// @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) |
||
497 | /// @param min_value, max_value min/max bounds for color calcuations. values outside bounds are limited to these values. defaults: min=0, max=1 |
||
498 | /// @param reverse if true, color ramps are inversed (to: min_value = white (black and white mode) or red (color mode). default = false. |
||
499 | /// @return a QImage with the Grids size of pixels. Pixel coordinates relate to the index values of the grid. |
||
500 | QImage gridToImage(const FloatGrid &grid, |
||
501 | bool black_white=false, |
||
502 | double min_value=0., double max_value=1., |
||
503 | bool reverse=false); |
||
504 | |||
556 | werner | 505 | |
285 | werner | 506 | /** load into 'rGrid' the content of the image pointed at by 'fileName'. |
507 | Pixels are converted to grey-scale and then transformend to a value ranging from 0..1 (black..white). |
||
508 | */ |
||
509 | bool loadGridFromImage(const QString &fileName, FloatGrid &rGrid); |
||
510 | |||
46 | Werner | 511 | /// template version for non-float grids (see also version for FloatGrid) |
599 | werner | 512 | /// @param sep string separator |
513 | /// @param newline_after if <>-1 a newline is added after every 'newline_after' data values |
||
36 | Werner | 514 | template <class T> |
599 | werner | 515 | QString gridToString(const Grid<T> &grid, const QChar sep=QChar(';'), const int newline_after=-1) |
36 | Werner | 516 | { |
517 | QString res; |
||
518 | QTextStream ts(&res); |
||
519 | |||
599 | werner | 520 | int newl_counter = newline_after; |
46 | Werner | 521 | for (int y=0;y<grid.sizeY();y++){ |
522 | for (int x=0;x<grid.sizeX();x++){ |
||
599 | werner | 523 | ts << grid.constValueAtIndex(x,y) << sep; |
524 | if (--newl_counter==0) { |
||
525 | ts << "\r\n"; |
||
526 | newl_counter = newline_after; |
||
527 | } |
||
36 | Werner | 528 | } |
529 | ts << "\r\n"; |
||
530 | } |
||
531 | |||
532 | return res; |
||
533 | } |
||
46 | Werner | 534 | |
599 | werner | 535 | /// template version for non-float grids (see also version for FloatGrid) |
536 | /// @param valueFunction pointer to a function with the signature: QString func(const T&) : this should return a QString |
||
537 | /// @param sep string separator |
||
538 | /// @param newline_after if <>-1 a newline is added after every 'newline_after' data values |
||
539 | template <class T> |
||
540 | QString gridToString(const Grid<T> &grid, QString (*valueFunction)(const T& value), const QChar sep=QChar(';'), const int newline_after=-1 ) |
||
541 | { |
||
542 | QString res; |
||
543 | QTextStream ts(&res); |
||
544 | |||
545 | int newl_counter = newline_after; |
||
546 | for (int y=0;y<grid.sizeY();y++){ |
||
547 | for (int x=0;x<grid.sizeX();x++){ |
||
548 | ts << (*valueFunction)(grid.constValueAtIndex(x,y)) << sep; |
||
549 | |||
550 | if (--newl_counter==0) { |
||
551 | ts << "\r\n"; |
||
552 | newl_counter = newline_after; |
||
553 | } |
||
554 | } |
||
555 | ts << "\r\n"; |
||
556 | } |
||
557 | |||
558 | return res; |
||
559 | } |
||
646 | werner | 560 | void modelToWorld(const Vector3D &From, Vector3D &To); |
599 | werner | 561 | |
562 | template <class T> |
||
563 | QString gridToESRIRaster(const Grid<T> &grid, QString (*valueFunction)(const T& value) ) |
||
564 | { |
||
646 | werner | 565 | Vector3D model(grid.metricRect().left(), grid.metricRect().top(), 0.); |
566 | Vector3D world; |
||
599 | werner | 567 | modelToWorld(model, world); |
607 | werner | 568 | QString result = QString("ncols %1\r\nnrows %2\r\nxllcorner %3\r\nyllcorner %4\r\ncellsize %5\r\nNODATA_value %6\r\n") |
599 | werner | 569 | .arg(grid.sizeX()) |
570 | .arg(grid.sizeY()) |
||
600 | werner | 571 | .arg(world.x(),0,'f').arg(world.y(),0,'f') |
599 | werner | 572 | .arg(grid.cellsize()).arg(-9999); |
600 | werner | 573 | QString line = gridToString(grid, valueFunction, QChar(' '), grid.sizeX()); // for special grids |
574 | QStringList lines = line.split("\r\n"); |
||
575 | for (int i=lines.count()-1; i>=0; --i) |
||
576 | result+=lines[i]; |
||
599 | werner | 577 | return result; |
578 | |||
579 | } |
||
580 | template <class T> |
||
581 | QString gridToESRIRaster(const Grid<T> &grid ) |
||
582 | { |
||
646 | werner | 583 | Vector3D model(grid.metricRect().left(), grid.metricRect().top(), 0.); |
584 | Vector3D world; |
||
599 | werner | 585 | modelToWorld(model, world); |
586 | QString result = QString("ncols %1\r\nnrows %2\r\nxllcorner %3\r\n yllcorner %4\r\ncellsize %5\r\nNODATA_value %6\r\n") |
||
587 | .arg(grid.sizeX()) |
||
588 | .arg(grid.sizeY()) |
||
589 | .arg(world.x()).arg(world.y()) |
||
590 | .arg(grid.cellsize()).arg(-9999); |
||
600 | werner | 591 | QString line = gridToString(grid, QChar(' '), grid.sizeX()); // for normal grids (e.g. float) |
592 | QStringList lines = line.split("\r\n"); |
||
593 | for (int i=lines.count()-1; i>=0; --i) |
||
594 | result+=lines[i]; |
||
599 | werner | 595 | return result; |
596 | |||
597 | } |
||
598 | |||
15 | Werner | 599 | #endif // GRID_H |