Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
671 | werner | 1 | /******************************************************************************************** |
2 | ** iLand - an individual based forest landscape and disturbance model |
||
3 | ** http://iland.boku.ac.at |
||
4 | ** Copyright (C) 2009- Werner Rammer, Rupert Seidl |
||
5 | ** |
||
6 | ** This program is free software: you can redistribute it and/or modify |
||
7 | ** it under the terms of the GNU General Public License as published by |
||
8 | ** the Free Software Foundation, either version 3 of the License, or |
||
9 | ** (at your option) any later version. |
||
10 | ** |
||
11 | ** This program is distributed in the hope that it will be useful, |
||
12 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
13 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
14 | ** GNU General Public License for more details. |
||
15 | ** |
||
16 | ** You should have received a copy of the GNU General Public License |
||
17 | ** along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
18 | ********************************************************************************************/ |
||
19 | |||
93 | Werner | 20 | #include "global.h" |
30 | Werner | 21 | #include "stamp.h" |
83 | Werner | 22 | #include "grid.h" |
23 | #include "stampcontainer.h" |
||
93 | Werner | 24 | #include "helper.h" |
487 | werner | 25 | |
26 | const Grid<float> *Stamp::mDistanceGrid = 0; |
||
27 | |||
30 | Werner | 28 | Stamp::Stamp() |
29 | { |
||
30 | m_data=0; |
||
31 | } |
||
487 | werner | 32 | |
30 | Werner | 33 | Stamp::~Stamp() |
34 | { |
||
35 | if( m_data) |
||
738 | werner | 36 | delete[] m_data; |
30 | Werner | 37 | } |
38 | |||
32 | Werner | 39 | void Stamp::setup(const int size) |
30 | Werner | 40 | { |
32 | Werner | 41 | int c=size*size; |
42 | m_size=size; |
||
34 | Werner | 43 | m_offset=0; |
47 | Werner | 44 | m_reader = 0; |
149 | werner | 45 | m_crownArea=0.f; |
46 | m_crownRadius=0.f; |
||
32 | Werner | 47 | if (m_data) |
48 | delete[] m_data; |
||
30 | Werner | 49 | m_data=new float[c]; |
50 | for (int i=0;i<c;i++) |
||
51 | m_data[i]=0.; |
||
487 | werner | 52 | // set static variable values |
53 | mDistanceGrid = &StampContainer::distanceGrid(); |
||
30 | Werner | 54 | } |
32 | Werner | 55 | |
487 | werner | 56 | //inline float Stamp::distanceToCenter(const int ix, const int iy) const |
57 | //{ |
||
58 | // // |
||
59 | // return StampContainer::distanceGrid().constValueAtIndex(abs(ix-m_offset), abs(iy-m_offset)); |
||
60 | //} |
||
401 | werner | 61 | |
321 | werner | 62 | QString Stamp::dump() const |
63 | { |
||
64 | QString result, line; |
||
65 | int x,y; |
||
66 | for (y=0;y<m_size;++y) { |
||
67 | line=""; |
||
68 | for (x=0;x<m_size;++x) { |
||
69 | line+= QString::number(*data(x,y)) + " "; |
||
70 | } |
||
71 | line+="\r\n"; |
||
72 | result+=line; |
||
73 | } |
||
74 | return result; |
||
75 | } |
||
76 | |||
32 | Werner | 77 | void Stamp::loadFromFile(const QString &fileName) |
78 | { |
||
79 | QString txt = Helper::loadTextFile(fileName); |
||
80 | QStringList lines = txt.split("\n"); |
||
81 | |||
82 | setup(lines.count()); |
||
83 | int l=0; |
||
84 | foreach(QString line, lines) { |
||
85 | QStringList cols=line.split(";"); |
||
86 | if (cols.count() != lines.count()) |
||
87 | MSGRETURN("Stamp::loadFromFile: invalid count of rows/cols."); |
||
88 | for (int i=0;i<cols.count();i++) |
||
89 | *data(i,l)=cols[i].toFloat(); |
||
90 | l++; |
||
91 | } |
||
92 | } |
||
33 | Werner | 93 | |
94 | // load from stream.... |
||
95 | void Stamp::load(QDataStream &in) |
||
96 | { |
||
97 | // see StampContainer doc for file stamp binary format |
||
98 | qint32 offset; |
||
99 | in >> offset; |
||
100 | m_offset = offset; |
||
101 | // load data |
||
102 | float data; |
||
35 | Werner | 103 | for (int i=0;i<count(); i++) { |
33 | Werner | 104 | in >> data; |
105 | m_data[i]=data; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | void Stamp::save(QDataStream &out) |
||
110 | { |
||
111 | // see StampContainer doc for file stamp binary format |
||
1102 | werner | 112 | out << static_cast<qint32>( m_offset ); |
35 | Werner | 113 | for (int i=0;i<count(); i++) { |
33 | Werner | 114 | out << m_data[i]; |
115 | } |
||
116 | } |
||
34 | Werner | 117 | |
118 | |||
119 | Stamp *stampFromGrid(const FloatGrid& grid, const int width) |
||
120 | { |
||
121 | Stamp::StampType type=Stamp::est4x4; |
||
35 | Werner | 122 | int c = grid.sizeX(); // total size of input grid |
34 | Werner | 123 | if (c%2==0 || width%2==0) { |
124 | qDebug() << "both grid and width should be uneven!!! returning NULL."; |
||
125 | return NULL; |
||
126 | } |
||
127 | |||
35 | Werner | 128 | if (width<=4) type = Stamp::est4x4; |
129 | else if (width<=8) type = Stamp::est8x8; |
||
130 | else if (width<=12) type = Stamp::est12x12; |
||
131 | else if (width<=16) type = Stamp::est16x16; |
||
132 | else if (width<=24) type = Stamp::est24x24; |
||
133 | else if (width<=32) type = Stamp::est32x32; |
||
131 | Werner | 134 | else if (width<=48) type = Stamp::est48x48; |
135 | else type = Stamp::est64x64; |
||
34 | Werner | 136 | |
736 | werner | 137 | Stamp *stamp = new Stamp(int(type)); |
52 | Werner | 138 | int swidth = width; |
131 | Werner | 139 | if (width>63) { |
140 | qDebug() << "Warning: grid too big, truncated stamp to 63x63px!"; |
||
141 | swidth = 63; |
||
52 | Werner | 142 | } |
143 | stamp->setOffset(swidth/2); |
||
144 | int coff = c/2 - swidth/2; // e.g.: grid=25, width=7 -> coff = 12 - 3 = 9 |
||
34 | Werner | 145 | int x,y; |
52 | Werner | 146 | for (x=0;x<swidth; x++) |
147 | for (y=0; y<swidth; y++) |
||
34 | Werner | 148 | stamp->setData(x,y, grid(coff+x, coff+y) ); // copy data (from a different rectangle) |
149 | return stamp; |
||
150 | |||
151 | } |