-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgriddata2.h
217 lines (196 loc) · 5.9 KB
/
griddata2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Name: GridData class 1.2
// Author: Andriy Bun
// Date: 28.05.2008
#ifndef griddata_h_
#define griddata_h_
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
class griddata
{
private:
int HorResolution;
int VerResolution;
int HorNeigh;
int VerNeigh;
double GridRows[];
double *grid, *gridPrev;
public:
griddata(int VR, int HR, double val);
griddata();
griddata(const griddata& g);
~griddata();
void ShowArray(); // prints array
void ShowArrayPrev(); // prints array for the previous year
void update(); // updates values for previous year to current values
void set(int x, int y, double val); // assigns value val to cell [x][y]
void setPrev(int x, int y, double val); // assigns previous year value val to cell [x][y]
void inc(int x, int y, double val); // adds value val to the existing value in cell [x][y]
double get(int x, int y); // returns value stored in cell [x][y]
double getPrev(int x, int y); // returns value for the previous year stored in cell [x][y]
void SetNeighNum(int n, int m); // sets number of neighbour cells to be considered
double GetMax(int x, int y); // returns maximum value of all neighbours for the previous year
double GetMin(int x, int y); // returns minimum value of all neighbours for the previous year
double GetAvg(int x, int y); // returns average value for the previous year
};
void griddata::ShowArray()
{
for (int j = 0; j < VerResolution; j++)
{
cout << j << "|\t";
for (int i = 0; i < HorResolution; i++)
{
cout << grid[j*HorResolution+i] << "\t";
}
cout << endl;
}
}
void griddata::ShowArrayPrev()
{
for (int j = 0; j < VerResolution; j++)
{
cout << j << "|\t";
for (int i = 0; i < HorResolution; i++)
{
cout << gridPrev[j*HorResolution+i] << "\t";
}
cout << endl;
}
}
void griddata::set(int x, int y, double val)
{
grid[y*HorResolution+x] = val;
}
void griddata::setPrev(int x, int y, double val)
{
gridPrev[y*HorResolution+x] = val;
}
void griddata::inc(int x, int y, double val)
{
grid[y*HorResolution+x] += val;
}
double griddata::get(int x, int y)
{
return (grid[y*HorResolution+x]);
}
double griddata::getPrev(int x, int y)
{
return (gridPrev[y*HorResolution+x]);
}
void griddata::update()
{
memcpy(gridPrev,grid,VerResolution*HorResolution*sizeof(double));
}
void griddata::SetNeighNum(int n, int m)
{
HorNeigh = n;
VerNeigh = m;
}
double griddata::GetMax(int x, int y)
{
int tmpx = x - HorNeigh;
if (tmpx < 0) tmpx = HorResolution + tmpx;
int tmpy = y - VerNeigh;
if (tmpy < 0) tmpy = 0;
double maxv = gridPrev[tmpy*HorResolution+tmpx];
for (int j = tmpy; j <= y + VerNeigh; j++)
{
if (j >= VerResolution) break;
for (int i = -HorNeigh; i <= HorNeigh; i++)
{
int ii = x + i;
if (ii >= HorResolution) ii -= HorResolution;
else if (ii < 0) ii += HorResolution;
if ((gridPrev[j*HorResolution+ii] > maxv) && !((ii == x) && (j == y)))
maxv = gridPrev[j*HorResolution+ii];
}
}
return(maxv);
}
double griddata::GetMin(int x, int y)
{
int tmpx = x - HorNeigh;
if (tmpx < 0) tmpx = HorResolution + tmpx;
int tmpy = y - VerNeigh;
if (tmpy < 0) tmpy = 0;
double minv = gridPrev[tmpy*HorResolution+tmpx];
for (int j = tmpy; j <= y + VerNeigh; j++)
{
if (j >= VerResolution) break;
for (int i = -HorNeigh; i <= HorNeigh; i++)
{
int ii = x + i;
if (ii >= HorResolution) ii -= HorResolution;
else if (ii < 0) ii += HorResolution;
if ((gridPrev[j*HorResolution+ii] < minv) && !((ii == x) && (j == y)))
minv = gridPrev[j*HorResolution+ii];
}
}
return(minv);
}
double griddata::GetAvg(int x, int y)
{
int count = 0;
int tmpx = x - HorNeigh;
if (tmpx < 0) tmpx = HorResolution + tmpx;
int tmpy = y - VerNeigh;
if (tmpy < 0) tmpy = 0;
double sumv = 0;
for (int j = tmpy; j <= y + VerNeigh; j++)
{
if (j >= VerResolution) break;
for (int i = -HorNeigh; i <= HorNeigh; i++)
{
int ii = x + i;
if (ii >= HorResolution) ii -= HorResolution;
else if (ii < 0) ii += HorResolution;
count++;
sumv += gridPrev[j*HorResolution+ii];
}
}
return(sumv/count);
}
// Class constructor
griddata::griddata(int HR, int VR, double val)
{
HorResolution = HR;
VerResolution = VR;
HorNeigh = 1;
VerNeigh = 1;
grid = new double[HorResolution*VerResolution];
gridPrev = new double[HorResolution*VerResolution];
memset(grid,val,HorResolution*VerResolution*sizeof(double));
memset(gridPrev,val,HorResolution*VerResolution*sizeof(double));
}
// Default constructor
griddata::griddata()
{
HorResolution = 720;
VerResolution = 360;
HorNeigh = 1;
VerNeigh = 1;
grid = new double[HorResolution*VerResolution];
gridPrev = new double[HorResolution*VerResolution];
memset(grid,0,HorResolution*VerResolution*sizeof(double));
memset(gridPrev,0,HorResolution*VerResolution*sizeof(double));
}
// Copy constructor
griddata::griddata(const griddata& g)
{
HorResolution = g.HorResolution;
VerResolution = g.VerResolution;
HorNeigh = g.HorNeigh;
VerNeigh = g.VerNeigh;
grid = new double[HorResolution*VerResolution];
gridPrev = new double[HorResolution*VerResolution];
memcpy(grid,g.grid,HorResolution*VerResolution*sizeof(double));
memcpy(gridPrev,g.gridPrev,HorResolution*VerResolution*sizeof(double));
}
// Destructor
griddata::~griddata()
{
delete []grid;
delete []gridPrev;
}
#endif