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