Newer
Older
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
/*
Basic diffusion and FRAP simulation
Jonathan Ward and Francois Nedelec, Copyright EMBL Nov. 2007-2009
To compile on the mac:
g++ main.cc -framework GLUT -framework OpenGL -o frap
To compile on Windows using Cygwin:
g++ main.cc -lopengl32 -lglut32 -o frap
To compile on Linux:
g++ main.cc -lglut -lopengl -o frap
*/
#include "params.h"
#ifdef __APPLE__
# include <GLUT/glut.h>
#else
# include "GL/glut.h"
#endif
#include <list>
#include <vector>
#include <iostream>
#include <math.h>
#include "../cmath3d/TriangleMesh.h"
#include "../cmath3d_v/TriangleMesh_v.h"
//link to the global params
extern ParamsClass params;
extern ActiveMesh mesh;
///whether the simulation is paused (=1) or not (=0)
int pauseSimulation=1;
///how big chunks of steps should be taken between drawing
int stepGranularity=1;
///OpenGL window size -> to recalculate from windows coords to scene coords
int windowSizeX=0, windowSizeY=0;
float uhel_x = 0.0, uhel_y = 0.0;
float zoom = 1.0;
bool MouseActive = false;
int last_x = 0, last_y = 0;
///manage \e count steps of the simulation and then print and draw
void step(int count)
{
while ((params.currTime < params.stopTime) && (count > 0))
{
//do some job
params.currTime += params.incrTime;
--count;
}
if (params.currTime >= params.stopTime)
{
std::cout << "END OF THE SIMULATION HAS BEEN REACHED\n";
pauseSimulation=1;
}
std::cout << "\nnow at time: " << params.currTime << "\n";
//ask GLUT to call the display function
glutPostRedisplay();
}
///draws the frame around the playground
void displayFrame(void)
{
glColor3f(params.sceneBorderColour.r,
params.sceneBorderColour.g,
params.sceneBorderColour.b);
glLineWidth(1);
glBegin(GL_LINE_LOOP);
glVertex3f( params.sceneOffset.x , params.sceneOffset.y , params.sceneCentre.z );
glVertex3f( params.sceneOffset.x + params.sceneSize.x , params.sceneOffset.y , params.sceneCentre.z );
glVertex3f( params.sceneOffset.x + params.sceneSize.x , params.sceneOffset.y + params.sceneSize.y , params.sceneCentre.z );
glVertex3f( params.sceneOffset.x , params.sceneOffset.y + params.sceneSize.y , params.sceneCentre.z );
glEnd();
}
void displayAxes(void)
{
glLineWidth(2);
glBegin(GL_LINES);
glColor3f(1.0,0.0,0.0); //red -- x axis
glVertex3f(params.sceneCentre.x,params.sceneCentre.y,params.sceneCentre.z);
glVertex3f(params.sceneCentre.x+params.sceneSize.x/4.f,params.sceneCentre.y,params.sceneCentre.z);
glColor3f(0.0,1.0,0.0); //green -- y axis
glVertex3f(params.sceneCentre.x,params.sceneCentre.y,params.sceneCentre.z);
glVertex3f(params.sceneCentre.x,params.sceneCentre.y+params.sceneSize.y/4.f,params.sceneCentre.z);
glColor3f(0.0,0.0,1.0); //blue -- z axis
glVertex3f(params.sceneCentre.x,params.sceneCentre.y,params.sceneCentre.z);
glVertex3f(params.sceneCentre.x,params.sceneCentre.y,params.sceneCentre.z+params.sceneSize.z/4.f);
glEnd();
glLineWidth(1);
}
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
void ActiveMesh::displayMesh(void)
{
glColor3f(0.8,0.8,0.8);
/*
for (unsigned int i=0; i < ID.size(); i+=3)
{
//glBegin(GL_TRIANGLES);
glBegin(GL_LINE_LOOP);
glNormal3f(norm[i/3].x,norm[i/3].y,norm[i/3].z);
glVertex3f(Pos[ID[i+0]].x,Pos[ID[i+0]].y,Pos[ID[i+0]].z);
glVertex3f(Pos[ID[i+1]].x,Pos[ID[i+1]].y,Pos[ID[i+1]].z);
glVertex3f(Pos[ID[i+2]].x,Pos[ID[i+2]].y,Pos[ID[i+2]].z);
glEnd();
}
*/
//long unsigned int* id=&ID.front();
long unsigned int* id=ID.data();
for (unsigned int i=0; i < ID.size(); i+=3)
{
//glBegin(GL_TRIANGLES);
glBegin(GL_LINE_LOOP);
glNormal3f(norm[i/3].x,norm[i/3].y,norm[i/3].z);
glVertex3f(Pos[*id].x,Pos[*id].y,Pos[*id].z); ++id;
glVertex3f(Pos[*id].x,Pos[*id].y,Pos[*id].z); ++id;
glVertex3f(Pos[*id].x,Pos[*id].y,Pos[*id].z); ++id;
glEnd();
}
std::vector<size_t> neigs;
ulm::getVertexImmediateNeighbours(*this,VertexID,neigs);
glPointSize(4.0f);
glBegin(GL_POINTS);
// centre vertex:
glColor3f(0.0,0.0,1.0);
glVertex3f(Pos[VertexID].x,Pos[VertexID].y,Pos[VertexID].z);
// neigs:
glColor3f(1.0,0.0,0.0);
for (unsigned int i=0; i < neigs.size(); ++i)
glVertex3f(Pos[neigs[i]].x,Pos[neigs[i]].y,Pos[neigs[i]].z);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(params.sceneCentre.x,params.sceneCentre.y,params.sceneCentre.z);
glScalef(zoom,zoom,zoom);
Vector3d<float> eye=params.sceneCentre;
eye.x+=params.sceneSize.z*sinf(uhel_x)*cosf(uhel_y);
eye.y+=params.sceneSize.z*sinf(uhel_y);
eye.z+=params.sceneSize.z*cosf(uhel_x)*cosf(uhel_y);
gluLookAt(eye.x,eye.y,eye.z, //eye
params.sceneCentre.x,params.sceneCentre.y,params.sceneCentre.z, //center
//0.0, cosf(-uhel_y), sinf(-uhel_y)); // up
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
0.0, 1.0, 0.0); // up
displayFrame();
displayAxes();
mesh.displayMesh();
glFlush();
glutSwapBuffers();
}
// called automatically after a certain amount of time
void timer(int)
{
//do simulation and then draw
step(stepGranularity);
//ask GLUT to call 'timer' again in 'delay' milli-seconds:
//in the meantime, you can inspect the drawing :-)
if (!pauseSimulation) glutTimerFunc(500, timer, 0);
}
void GetSceneViewSize(const int w,const int h,
float& xVisF, float& xVisT,
float& yVisF, float& yVisT)
{
//what is the desired size of the scene
const float xBound=params.sceneSize.x
+ 2.f*params.sceneOuterBorder.x;
const float yBound=params.sceneSize.y
+ 2.f*params.sceneOuterBorder.y;
xVisF = params.sceneOffset.x - params.sceneOuterBorder.x;
yVisF = params.sceneOffset.y - params.sceneOuterBorder.y;
xVisT = xBound + xVisF;
yVisT = yBound + yVisF;
if ( (float)w * yBound < (float)h * xBound ) //equals to w/h < x/y
{
//window is closer to the rectangle than the scene shape
yVisF *= ((float)h / float(w)) * (xBound/yBound);
yVisT *= ((float)h / float(w)) * (xBound/yBound);
}
else
{
xVisF *= ((float)w / float(h)) * (yBound/xBound);
xVisT *= ((float)w / float(h)) * (yBound/xBound);
}
}
void printKeysHelp(void)
{
std::cout << "\nKeys:\n";
std::cout << "ESC or 'q': quit\n";
}
void printDispayedFeauters(void)
{
if (pauseSimulation) std::cout << "\nsimulation is now paused\n";
else std::cout << "\nsimulation is now in progress\n";
std::cout << "simulation time progress " << params.currTime/params.stopTime*100.f
<< "%, delta time is " << params.incrTime << " minutes\n";
std::cout << "viewing: x_ang=" << uhel_x << ", y_ang=" << uhel_y
<< ", zoom=" << zoom << "\n";
std::cout << "Currently displayed features:\n";
}
// called when a key is pressed
void keyboard(unsigned char key, int mx, int my)
{
//required for calculating the mouse position within the scene coords
float xVisF, yVisF;
float xVisT, yVisT;
float sx,sy;
switch (key) {
case 27: //this corresponds to 'escape'
case 'q': //quit
exit(EXIT_SUCCESS);
break;
//control execution of the simulation
case ' ': //pause/unpause, refresh every 10 cycles
if (params.currTime < params.stopTime) pauseSimulation^=1;
if (!pauseSimulation)
{
stepGranularity=1; //= 1 min
timer(0);
}
break;
case 'z': //pause/unpause, refresh every 600 cycles
if (params.currTime < params.stopTime) pauseSimulation^=1;
if (!pauseSimulation)
{
stepGranularity=100; //= 10 min
timer(0);
}
break;
case 'n': //pause, advance by one frame
pauseSimulation=1;
step(1);
break;
//etc stuff...
case 'r': //set scale 1:1
uhel_x = 0.0;
uhel_y = 0.0;
zoom = 1.0;
glutReshapeWindow(int(params.imgSizeX),int(params.imgSizeY));
glutPostRedisplay();
break;
case 'h': //print help
printKeysHelp();
break;
case 'd': //print what is displayed
printDispayedFeauters();
break;
case 'o':
zoom -= 0.2f;
if (zoom < 0.2f) zoom=0.2f;
glutPostRedisplay();
break;
case 'O':
zoom += 0.2f;
glutPostRedisplay();
break;
case 'v':
++VertexID;
glutPostRedisplay();
break;
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
case 'i': //inspect a cell
GetSceneViewSize(windowSizeX,windowSizeY, xVisF,xVisT,yVisF,yVisT);
sx=(float)mx /(float)windowSizeX * (xVisT-xVisF) + xVisF;
sy=(float)(windowSizeY-my)/(float)windowSizeY * (yVisT-yVisF) + yVisF;
xVisF=sx=sy; //just to avoid warning, TODO REMOVE
/*
for (c=agents.begin(); c != agents.end(); c++)
if ((*c)->IsPointInCell(Vector3d<float>(sx,sy,0)))
{
bool wasSelected=(*c)->isSelected;
(*c)->isSelected=true;
(*c)->ReportState();
(*c)->isSelected=wasSelected;
}
*/
break;
case 13: //Enter key -- just empty lines
std::cout << std::endl;
break;
}
}
// called when a special key is pressed
void Skeyboard(int key, int mx, int my)
{
key=mx=my; //TODO REMOVE
mx=key; //TODO REMOVE
/*
//required for calculating the mouse position within the scene coords
float xVisF, yVisF;
float xVisT, yVisT;
float sx,sy;
switch (key) {
case GLUT_KEY_LEFT:
//rotate cell left by 30deg
GetSceneViewSize(windowSizeX,windowSizeY, xVisF,xVisT,yVisF,yVisT);
sx=(float)mx /(float)windowSizeX * (xVisT-xVisF) + xVisF;
sy=(float)(windowSizeY-my)/(float)windowSizeY * (yVisT-yVisF) + yVisF;
for (c=agents.begin(); c != agents.end(); c++)
if ((*c)->IsPointInCell(Vector3d<float>(sx,sy,0)))
{
(*c)->desiredDirection += PI/6.f;
if ((*c)->desiredDirection > PI)
(*c)->desiredDirection -= 2.f*PI;
}
break;
case GLUT_KEY_RIGHT:
//rotate cell left by 30deg
GetSceneViewSize(windowSizeX,windowSizeY, xVisF,xVisT,yVisF,yVisT);
sx=(float)mx /(float)windowSizeX * (xVisT-xVisF) + xVisF;
sy=(float)(windowSizeY-my)/(float)windowSizeY * (yVisT-yVisF) + yVisF;
for (c=agents.begin(); c != agents.end(); c++)
if ((*c)->IsPointInCell(Vector3d<float>(sx,sy,0)))
{
(*c)->desiredDirection -= PI/6.f;
if ((*c)->desiredDirection < -PI)
(*c)->desiredDirection += 2.f*PI;
}
break;
}
*/
}
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN) MouseActive = true;
else if (state == GLUT_UP) MouseActive = false;
last_x=button; //TODO REMOVE
last_x = x;
last_y = y;
}
void MouseMotion(int x, int y)
{
if (MouseActive)
{
uhel_x -= (float(x - last_x) * 0.01f);
uhel_y += (float(y - last_y) * 0.01f);
last_x = x;
last_y = y;
glutPostRedisplay();
}
}
// called when the window is reshaped, arguments are the new window size
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//remember the new window size...
windowSizeX=w; windowSizeY=h;
//what will be the final view port
float xVisF, yVisF;
float xVisT, yVisT;
GetSceneViewSize(w,h,xVisF,xVisT,yVisF,yVisT);
//glOrtho(xVisF, xVisT, yVisF, yVisT, params.sceneCentre.z-params.sceneSize.z,params.sceneCentre.z+params.sceneSize.z);
glOrtho(xVisF, xVisT, yVisF, yVisT, -1000,1000);
glMatrixMode(GL_MODELVIEW);
}
void initializeGL(void)
{
int Argc=1;
char msg[]="TRAgen: A Tool for Generation of Synthetic Time-Lapse Image Sequences of Living Cells";
char *Argv[10];
Argv[0]=msg;
glutInit(&Argc, Argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow(Argv[0]);
glutKeyboardFunc(keyboard);
glutSpecialFunc(Skeyboard);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(MouseMotion);
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_POINT_SMOOTH);
}
void loopGL(void)
{
glutMainLoop();
}
void closeGL(void)
{
}