Rev 671 | Rev 738 | 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 | |||
93 | Werner | 21 | #include "global.h" |
30 | Werner | 22 | #include "stamp.h" |
83 | Werner | 23 | #include "grid.h" |
24 | #include "stampcontainer.h" |
||
93 | Werner | 25 | #include "helper.h" |
487 | werner | 26 | |
27 | const Grid<float> *Stamp::mDistanceGrid = 0; |
||
28 | |||
30 | Werner | 29 | Stamp::Stamp() |
30 | { |
||
31 | m_data=0; |
||
32 | } |
||
487 | werner | 33 | |
30 | Werner | 34 | Stamp::~Stamp() |
35 | { |
||
36 | if( m_data) |
||
37 | delete m_data; |
||
38 | } |
||
39 | |||
32 | Werner | 40 | void Stamp::setup(const int size) |
30 | Werner | 41 | { |
32 | Werner | 42 | int c=size*size; |
43 | m_size=size; |
||
34 | Werner | 44 | m_offset=0; |
47 | Werner | 45 | m_reader = 0; |
149 | werner | 46 | m_crownArea=0.f; |
47 | m_crownRadius=0.f; |
||
32 | Werner | 48 | if (m_data) |
49 | delete[] m_data; |
||
30 | Werner | 50 | m_data=new float[c]; |
51 | for (int i=0;i<c;i++) |
||
52 | m_data[i]=0.; |
||
487 | werner | 53 | // set static variable values |
54 | mDistanceGrid = &StampContainer::distanceGrid(); |
||
30 | Werner | 55 | } |
32 | Werner | 56 | |
487 | werner | 57 | //inline float Stamp::distanceToCenter(const int ix, const int iy) const |
58 | //{ |
||
59 | // // |
||
60 | // return StampContainer::distanceGrid().constValueAtIndex(abs(ix-m_offset), abs(iy-m_offset)); |
||
61 | //} |
||
401 | werner | 62 | |
321 | werner | 63 | QString Stamp::dump() const |
64 | { |
||
65 | QString result, line; |
||
66 | int x,y; |
||
67 | for (y=0;y<m_size;++y) { |
||
68 | line=""; |
||
69 | for (x=0;x<m_size;++x) { |
||
70 | line+= QString::number(*data(x,y)) + " "; |
||
71 | } |
||
72 | line+="\r\n"; |
||
73 | result+=line; |
||
74 | } |
||
75 | return result; |
||
76 | } |
||
77 | |||
32 | Werner | 78 | void Stamp::loadFromFile(const QString &fileName) |
79 | { |
||
80 | QString txt = Helper::loadTextFile(fileName); |
||
81 | QStringList lines = txt.split("\n"); |
||
82 | |||
83 | setup(lines.count()); |
||
84 | int l=0; |
||
85 | foreach(QString line, lines) { |
||
86 | QStringList cols=line.split(";"); |
||
87 | if (cols.count() != lines.count()) |
||
88 | MSGRETURN("Stamp::loadFromFile: invalid count of rows/cols."); |
||
89 | for (int i=0;i<cols.count();i++) |
||
90 | *data(i,l)=cols[i].toFloat(); |
||
91 | l++; |
||
92 | } |
||
93 | } |
||
33 | Werner | 94 | |
95 | // load from stream.... |
||
96 | void Stamp::load(QDataStream &in) |
||
97 | { |
||
98 | // see StampContainer doc for file stamp binary format |
||
99 | qint32 offset; |
||
100 | in >> offset; |
||
101 | m_offset = offset; |
||
102 | // load data |
||
103 | float data; |
||
35 | Werner | 104 | for (int i=0;i<count(); i++) { |
33 | Werner | 105 | in >> data; |
106 | m_data[i]=data; |
||
107 | } |
||
108 | } |
||
109 | |||
110 | void Stamp::save(QDataStream &out) |
||
111 | { |
||
112 | // see StampContainer doc for file stamp binary format |
||
113 | out << (qint32) m_offset; |
||
35 | Werner | 114 | for (int i=0;i<count(); i++) { |
33 | Werner | 115 | out << m_data[i]; |
116 | } |
||
117 | } |
||
34 | Werner | 118 | |
119 | |||
120 | Stamp *stampFromGrid(const FloatGrid& grid, const int width) |
||
121 | { |
||
122 | Stamp::StampType type=Stamp::est4x4; |
||
35 | Werner | 123 | int c = grid.sizeX(); // total size of input grid |
34 | Werner | 124 | if (c%2==0 || width%2==0) { |
125 | qDebug() << "both grid and width should be uneven!!! returning NULL."; |
||
126 | return NULL; |
||
127 | } |
||
128 | |||
35 | Werner | 129 | if (width<=4) type = Stamp::est4x4; |
130 | else if (width<=8) type = Stamp::est8x8; |
||
131 | else if (width<=12) type = Stamp::est12x12; |
||
132 | else if (width<=16) type = Stamp::est16x16; |
||
133 | else if (width<=24) type = Stamp::est24x24; |
||
134 | else if (width<=32) type = Stamp::est32x32; |
||
131 | Werner | 135 | else if (width<=48) type = Stamp::est48x48; |
136 | else type = Stamp::est64x64; |
||
34 | Werner | 137 | |
736 | werner | 138 | Stamp *stamp = new Stamp(int(type)); |
52 | Werner | 139 | int swidth = width; |
131 | Werner | 140 | if (width>63) { |
141 | qDebug() << "Warning: grid too big, truncated stamp to 63x63px!"; |
||
142 | swidth = 63; |
||
52 | Werner | 143 | } |
144 | stamp->setOffset(swidth/2); |
||
145 | int coff = c/2 - swidth/2; // e.g.: grid=25, width=7 -> coff = 12 - 3 = 9 |
||
34 | Werner | 146 | int x,y; |
52 | Werner | 147 | for (x=0;x<swidth; x++) |
148 | for (y=0; y<swidth; y++) |
||
34 | Werner | 149 | stamp->setData(x,y, grid(coff+x, coff+y) ); // copy data (from a different rectangle) |
150 | return stamp; |
||
151 | |||
152 | } |