-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.cpp
237 lines (193 loc) · 5.67 KB
/
Cell.cpp
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*----------------------------------------------------------------------------
*
* Copyright (C) 2020 Greta Bocedi, Stephen C.F. Palmer, Justin M.J. Travis, Anne-Kathleen Malchow, Damaris Zurell
*
* This file is part of RangeShifter.
*
* RangeShifter is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RangeShifter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RangeShifter. If not, see <https://www.gnu.org/licenses/>.
*
--------------------------------------------------------------------------*/
//---------------------------------------------------------------------------
#include "Cell.h"
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Cell functions
Cell::Cell(int xx,int yy,intptr patch,int hab)
{
x = xx; y = yy;
pPatch = patch;
envVal = 1.0; // default - no effect of any gradient
envDev = eps = 0.0;
habIxx.push_back(hab);
#if RSDEBUG
//DebugGUI(("Cell::Cell(): this=" + Int2Str((int)this)
// + " x=" + Int2Str(x) + " y=" + Int2Str(y)
// + " habIndex=" + Int2Str(habIndex)
//).c_str());
#endif
visits = 0;
smsData = 0;
}
Cell::Cell(int xx,int yy,intptr patch,float hab)
{
x = xx; y = yy;
pPatch = patch;
envVal = 1.0; // default - no effect of any gradient
envDev = eps = 0.0;
habitats.push_back(hab);
visits = 0;
smsData = 0;
}
Cell::~Cell() {
#if RSDEBUG
//DEBUGLOG << "Cell::~Cell(): this = " << this << " smsData = " << smsData << endl;
#endif
habIxx.clear();
habitats.clear();
if (smsData != 0) {
if (smsData->effcosts != 0) delete smsData->effcosts;
delete smsData;
}
#if RSDEBUG
//DEBUGLOG << "Cell::~Cell(): deleted" << endl;
#endif
}
void Cell::setHabIndex(short hx) {
#if RSDEBUG
//DebugGUI(("Cell::setHabIndex(): this=" + Int2Str((int)this)
// + " x=" + Int2Str(x) + " y=" + Int2Str(y)
// + " habIx=" + Int2Str(habIx)
//).c_str());
#endif
if (hx < 0) habIxx.push_back(0);
else habIxx.push_back(hx);
}
void Cell::changeHabIndex(short ix,short hx) {
if (ix >= 0 && ix < (short)habIxx.size() && hx >= 0) habIxx[ix] = hx;
else habIxx[ix] = 0;
}
int Cell::getHabIndex(int ix) {
if (ix < 0 || ix >= (int)habIxx.size())
// nodata cell OR should not occur, but treat as such
return -1;
else return habIxx[ix];
}
int Cell::nHabitats(void) {
int nh = (int)habIxx.size();
if ((int)habitats.size() > nh) nh = (int)habitats.size();
return nh;
}
void Cell::setHabitat(float q) {
if (q >= 0.0 && q <= 100.0) habitats.push_back(q);
else habitats.push_back(0.0);
}
float Cell::getHabitat(int ix) {
if (ix < 0 || ix >= (int)habitats.size())
// nodata cell OR should not occur, but treat as such
return -1.0;
else return habitats[ix];
}
void Cell::setPatch(intptr p) {
pPatch = p;
}
intptr Cell::getPatch(void)
{
#if RSDEBUG
//DebugGUI(("Cell::getPatch(): this=" + Int2Str((int)this)
// + " x=" + Int2Str(x) + " y=" + Int2Str(y)
// + " habIxx[0]=" + Int2Str(habIxx[0]) + " pPatch=" + Int2Str(pPatch)
//).c_str());
#endif
return pPatch;
}
locn Cell::getLocn(void) { locn q; q.x = x; q.y = y; return q; }
void Cell::setEnvDev(float d) { envDev = d; }
float Cell::getEnvDev(void) { return envDev; }
void Cell::setEnvVal(float e) {
if (e >= 0.0) envVal = e;
}
float Cell::getEnvVal(void) { return envVal; }
void Cell::updateEps(float ac,float randpart) {
eps = eps*ac + randpart;
}
float Cell::getEps(void) { return eps; }
// Functions to handle costs for SMS
int Cell::getCost(void) {
int c;
if (smsData == 0) c = 0; // costs not yet set up
else c = smsData->cost;
return c;
}
void Cell::setCost(int c) {
if (smsData == 0) {
smsData = new smscosts;
smsData->effcosts = 0;
}
smsData->cost = c;
}
// Reset the cost and the effective cost of the cell
void Cell::resetCost(void) {
if (smsData != 0) { resetEffCosts(); delete smsData; }
smsData = 0;
}
array3x3f Cell::getEffCosts(void) {
array3x3f a;
if (smsData == 0 || smsData->effcosts == 0) { // effective costs have not been calculated
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
a.cell[i][j] = -1.0;
}
}
}
else
a = *smsData->effcosts;
return a;
}
void Cell::setEffCosts(array3x3f a) {
if (smsData->effcosts == 0) smsData->effcosts = new array3x3f;
*smsData->effcosts = a;
}
// Reset the effective cost, but not the cost, of the cell
void Cell::resetEffCosts(void) {
if (smsData != 0) {
if (smsData->effcosts != 0) {
delete smsData->effcosts;
smsData->effcosts = 0;
}
}
}
void Cell::resetVisits(void) { visits = 0; }
void Cell::incrVisits(void) { visits++; }
unsigned long int Cell::getVisits(void) { return visits; }
//---------------------------------------------------------------------------
// Initial species distribution cell functions
DistCell::DistCell(int xx,int yy) {
x = xx; y = yy; initialise = false;
}
DistCell::~DistCell() {
}
void DistCell::setCell(bool init) {
initialise = init;
}
bool DistCell::toInitialise(locn loc) {
if (loc.x == x && loc.y == y) return initialise;
else return false;
}
bool DistCell::selected(void) { return initialise; }
locn DistCell::getLocn(void) {
locn loc; loc.x = x; loc.y = y; return loc;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------