This repository has been archived by the owner on Jul 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
302 lines (273 loc) · 9.51 KB
/
main.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* IMS Project - Application of celular automata in biology
* Authors: Filip Gulan and Marek Marusic
* E-mails: xgulan00@stud.fit.vutbr.cz and xmarus05@stud.fit.vutbr.cz
*/
#include <iostream>
#include <getopt.h>
#include <unistd.h>
#include <stdlib.h>
#include <GL/glut.h> // GLUT, include glu.h and gl.h
#include <GL/gl.h>
#include <GL/glu.h>
#include <fstream>
#include <iomanip>
#include "areaCells.h"
using namespace std;
#define FAULT 1 //exit code, no success
#define SUCCESS 0 //exit code on success
#define BLANK -1 //argument is blank
#define DEFINED 1 //argument is defined
#define UNDEFINED -1
#define EMPTY 0
#define STONE -1
//Global variables, needed becouse of OPENGL display function
int width = BLANK;
int vegetationTime = BLANK;
int deathTime = BLANK;
int seedRain = 0;
int extiction = 0;
int intenseExct = 0;
string fileOut = "";
int Time = BLANK;
int graphic = UNDEFINED;
int cmdLine = UNDEFINED;
int speed = BLANK;
int avg = UNDEFINED;
int mode = UNDEFINED;
int rock = UNDEFINED;
/*
* Function, that print results of simulation
*/
void printOutput(areaCells allCells){
//Open file or cout
ofstream f;
if(fileOut != "") //file to write is specified
f.open(fileOut.c_str(), std::ios::out);
std::ostream & outFile = (f.is_open() ? f : std::cout);
//print to ostream
if(avg == DEFINED) //get avarange percent of plants occupied lattice
{
double average = 0.0;
for(int i = 0; i < allCells.ocuppiedPercent.size(); i++)
{
average += allCells.ocuppiedPercent[i];
}
average = average/double(allCells.ocuppiedPercent.size());
outFile << std::setprecision(5) << average << endl;
}
else //we want normal values for line graph
{
outFile << "Čas [rok]" << "," << "Obsadená časť [%]" << endl;
for(int i = 0; i < allCells.ocuppiedPercent.size(); i++)
{
outFile << i+1 << "," << std::setprecision(5) << allCells.ocuppiedPercent[i] << endl;
}
}
//Close file
if (f.is_open())
{
f.close();
}
}
/*
* Function to print help to stdin
*/
void printHelp()
{
cout << "-------------------------------------------------------------------" << endl;
cout << "IMS Project - Application of celular automata in biology" << endl;
cout << "Authors: Filip Gulan and Marek Marusic" << endl;
cout << "E-mails: xgulan00@stud.fit.vutbr.cz and xmarus05@stud.fit.vutbr.cz" << endl;
cout << "-------------------------------------------------------------------" << endl;
cout << "Parameters:" << endl;
cout << " Required:" << endl;
cout << " -w X: width of matrix" << endl;
cout << " -t X: time of simulation in years" << endl;
cout << " -d X: death time of plant" << endl;
cout << " Optional:" << endl;
cout << " -v X: vegetation time of plant" << endl;
cout << " -r X: probability of seed rain in percent" << endl;
cout << " -e X: exctition probability in percent" << endl;
cout << " -x X: intensity of one time exctition in percent" << endl;
cout << " -f X: file to write simulation results to X file" << endl;
cout << " -s X: speed of simulation, 1 very slow, 1000 very fast" << endl;
cout << " -m: second mode, that fills matrix with plants" << endl;
cout << " -a: get avarange percent of plants in matrix during simulation" << endl;
cout << " -k: stone mode simulation" << endl;
cout << " -g: graphical OPENGL visualisation" << endl;
cout << " -c: command line visualisation" << endl;
cout << " -h: print this help" << endl;
return;
}
/*
* Function to set up OPENGL
*/
void setup() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
/*
* Function to display OPENGL window
*/
void display()
{
areaCells allCells(width, deathTime, vegetationTime, seedRain, extiction, Time, intenseExct);
allCells.fillMatrix();
//srand(time(NULL));
if(mode == DEFINED) //second mode, matrix is prefilled with plants
{
allCells.fillWithPlants();
}
if(rock == DEFINED) //second mode, matrix is prefilled with plants
{
allCells.fillRock();
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for(int t = 0; t < Time; t++) //simulate for t years
{
//DISPLAY THE MATRIX
GLfloat minSize = 60.0f/allCells.width;
GLfloat greenIncrement = 0.5f/deathTime;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 60, 60, 0.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, 600, 600);
for(int i = 0; i <= allCells.width; i++) //for row in matrix
{
for(int j = 0; j <= allCells.width; j++) //for col in matrix
{
if(allCells.matrixPresent[i + j * allCells.width].state == EMPTY)
glColor3f(0.32157f, 0.0941f, 0.0f);// Let it be brown
else if(allCells.matrixPresent[i + j * allCells.width].state == STONE)
glColor3f(0.0f, 0.301f, 0.301f);// Let it be dark blue
else
glColor3f(0.0f, 0.470f+(greenIncrement * allCells.matrixPresent[i + j * allCells.width].state), 0.0f);// Let it be green
glBegin(GL_QUADS); // 2x2 pixels
glVertex2f(0.0f+minSize*j, 0.0f+minSize*i);
glVertex2f(0.0f+minSize*(j+1), 0.0f+minSize*i);
glVertex2f(0.0f+minSize*(j+1), 0.0f+minSize*(i+1));
glVertex2f(0.0f+minSize*j, 0.0f+minSize*(i+1));
glEnd();
glEnd();
}
}
glutSwapBuffers();
usleep(speed);
allCells.updateMatrices();
}
printOutput(allCells);
}
/*
* Main function
*/
int main(int argc, char *argv[])
{
int c; //variable for iteration trought comand line parameters, hold last parameter
char *cvalue = NULL; //variable for value of specific argument (for -a 100, it is 100)
while((c = getopt(argc, argv, "kamf:v:d:s:e:x:r:w:t:hgc")) != -1) //iterate trought all parameters of comand line
{
switch(c)
{
case 'a':
avg = DEFINED;
break;
case 'f': //file to write results
fileOut = optarg;
break;
case 'v': //vegetation time
vegetationTime = atoi(optarg);
break;
case 'd': //death time
deathTime = atoi(optarg);
break;
case 'r': //seedRain prohability
seedRain = atoi(optarg);
break;
case 's': //seedRain prohability
speed = atoi(optarg);
break;
case 'e': //exictionTimeProhability
extiction = atoi(optarg);
break;
case 'm': //second mode
mode = DEFINED;
break;
case 'x': //intenseSingleExtiction
intenseExct = atoi(optarg);
break;
case 'w': //exictionTimeProhability
width = atoi(optarg);
break;
case 't': //time argument
Time = atoi(optarg);
break;
case 'k': //time argument
rock = DEFINED;
break;
case 'g': //opengl graphical visualisation
graphic = DEFINED;
break;
case 'c': //comandline text visualisation
cmdLine = DEFINED;
break;
case 'h': //help
printHelp();
return SUCCESS;
case '?': //somethink unkown as value
cerr << "Unkown option -" << (char) optopt << "! Type -h for help!" << endl;
return FAULT;
default: //somethink unkown
return FAULT;
}
}
if(deathTime <= BLANK || Time <= BLANK || width <= BLANK) //check if setings from parameters are not blank
{
cerr << "Invalid arguments! Type -h for help!" << endl;
return FAULT;
}
if(speed <= 0 ) //if speed is not specified with arguments
speed = 1000000;
else //speed of simulation is specified
speed = 1000000/speed;
srand(time(NULL));
if(graphic == DEFINED) //start simulation in graphic mode using openGL
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(600,600);
glutCreateWindow("Celular automata");
setup();
glutDisplayFunc(display);
glutMainLoop();
}
else //start simulation in comandline visualisation or without visualisation
{
areaCells allCells(width, deathTime, vegetationTime, seedRain, extiction, Time, intenseExct);
allCells.fillMatrix();
if(mode == DEFINED) //second mode, matrix is prefilled with plants
{
allCells.fillWithPlants();
}
if(rock == DEFINED) //mode with stone
{
allCells.fillRock();
}
for(int i = 0; i < Time; i++) //simulate from time 0 to specified time
{
if(cmdLine == DEFINED) //comandline visualisation
{
allCells.showInCmd();
usleep(speed);
}
allCells.updateMatrices();
}
if(cmdLine == DEFINED) //comandline visualisation add new lines at the end
{
allCells.endShowCmd();
}
printOutput(allCells);
}
exit(SUCCESS);
}