Blame view

main.cu 38 KB
7c1b82bc   Jiaming Guo   test for 00_GT.swc
1
  #include <stdlib.h>
db598823   David Mayerich   fixed GCC errors ...
2
3
  #include <string>
  #include <fstream>
f001495e   Jiaming Guo   fix minor errors ...
4
  #include <algorithm> 
db598823   David Mayerich   fixed GCC errors ...
5
  
8f96cac6   Jiaming Guo   pushed old changes
6
7
8
9
  // STIM includes
  #include <stim/parser/arguments.h>
  #include <stim/visualization/camera.h>
  #include <stim/gl/gl_texture.h>
db598823   David Mayerich   fixed GCC errors ...
10
11
12
  #include <stim/visualization/gl_network.h>
  #include <stim/biomodels/network.h>
  #include <stim/visualization/gl_aaboundingbox.h>
8f96cac6   Jiaming Guo   pushed old changes
13
14
15
16
  
  // OpenGL includes
  #include <GL/glut.h>
  #include <GL/freeglut.h>
db598823   David Mayerich   fixed GCC errors ...
17
  
f86a38d3   Jiaming Guo   add device choice...
18
19
20
21
22
  #ifdef __CUDACC__
  //CUDA includes
  #include <cuda.h>
  #endif
  
8f96cac6   Jiaming Guo   pushed old changes
23
  // BOOST includes
db598823   David Mayerich   fixed GCC errors ...
24
25
  #include <boost/tuple/tuple.hpp>
  
8f96cac6   Jiaming Guo   pushed old changes
26
27
28
29
30
  <<<<<<< HEAD
  // visualization objects
  stim::gl_aaboundingbox<float> bb;	// axis-aligned bounding box object
  stim::camera cam;					// camera object
  =======
db598823   David Mayerich   fixed GCC errors ...
31
32
  //visualization objects
  stim::gl_aaboundingbox<float> bb;			//axis-aligned bounding box object
09cb5950   David Mayerich   fixed some commen...
33
  stim::camera cam;							//camera object
8f96cac6   Jiaming Guo   pushed old changes
34
  >>>>>>> 09cb5950f628309ac65c6b85119d3f16e5bcd0a2
db598823   David Mayerich   fixed GCC errors ...
35
  
8f96cac6   Jiaming Guo   pushed old changes
36
37
38
39
40
  // overall parameters
  unsigned num_nets = 0;				// number of networks that've been loaded
  float sigma = 3.0;					// default sigma(resample rate) equals to 3.0
  float radius = 0.7;					// equals to radius
  float delta;						// camera moving parameter
f001495e   Jiaming Guo   fix minor errors ...
41
42
  
  // networks
8f96cac6   Jiaming Guo   pushed old changes
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
  <<<<<<< HEAD
  stim::gl_network<float> GT;			// ground truth network
  stim::gl_network<float> T;			// test network
  stim::gl_network<float> _GT;		// splitted GT
  stim::gl_network<float> _T;			// splitted T
  
  // flags
  bool flag_mapping = false;				// flag indicates mapping
  bool flag_stack = false;				// flag indicates loading image stacks
  bool flag_adjoint_network = false;		// flag indicates render a T overlaid on GT
  bool flag_light = false;				// flag indicates light on/off
  bool flag_highlight_difference;			// flag indicates highlight the difference between two networks
  
  // relationships
  std::vector<unsigned> _gt_t;			// store indices of nearest edge points in _T for _GT
  std::vector<unsigned> _t_gt;			// store indices of nearest edge points in _GT for _T
  
  // hard-coded parameters
  float resample_rate = 0.5f;			// sample rate for the network (fraction of sigma used as the maximum sample rate)
  float camera_factor = 1.2f;			// start point of the camera as a function of X and Y size
  float orbit_factor = 0.01f;			// degrees per pixel used to orbit the camera
  float zoom_factor = 10.0f;			// zooming factor
  float radius_factor = 0.5f;			// radius changing factor
  
  // mouse click
  bool LButtonDown = false;			// true when left button down
  =======
09cb5950   David Mayerich   fixed some commen...
70
71
72
73
  stim::gl_network<float> GT;					//ground truth network
  stim::gl_network<float> T;					//test network
  stim::gl_network<float> _GT;				//splitted GT
  stim::gl_network<float> _T;					//splitted T
9627c6e6   Jiaming Guo   add splitting and...
74
  
f001495e   Jiaming Guo   fix minor errors ...
75
  // indicator
09cb5950   David Mayerich   fixed some commen...
76
  unsigned ind = 0;							//indicator of mapping
9627c6e6   Jiaming Guo   add splitting and...
77
  
f001495e   Jiaming Guo   fix minor errors ...
78
  // relationships
09cb5950   David Mayerich   fixed some commen...
79
80
  std::vector<unsigned> _gt_t;				// store indices of nearest edge points in _T for _GT
  std::vector<unsigned> _t_gt;				// store indices of nearest edge points in _GT for _T
db598823   David Mayerich   fixed GCC errors ...
81
82
  
  //hard-coded parameters
09cb5950   David Mayerich   fixed some commen...
83
84
85
  float resample_rate = 0.5f;					//sample rate for the network (fraction of sigma used as the maximum sample rate)
  float camera_factor = 1.2f;					//start point of the camera as a function of X and Y size
  float orbit_factor = 0.01f;					//degrees per pixel used to orbit the camera
4d23da9c   Jiaming Guo   add 00_GT.swc,now...
86
87
  float zoom_factor = 10.0f;
  float radius_factor = 0.5f;
db598823   David Mayerich   fixed GCC errors ...
88
  
9627c6e6   Jiaming Guo   add splitting and...
89
  //mouse click
09cb5950   David Mayerich   fixed some commen...
90
  bool LButtonDown = false;					// true when left button down
8f96cac6   Jiaming Guo   pushed old changes
91
  >>>>>>> 09cb5950f628309ac65c6b85119d3f16e5bcd0a2
9627c6e6   Jiaming Guo   add splitting and...
92
93
  bool RButtonDown = false;
  
8f96cac6   Jiaming Guo   pushed old changes
94
  // mouse position tracking
db598823   David Mayerich   fixed GCC errors ...
95
96
97
  int mouse_x;
  int mouse_y;
  
f001495e   Jiaming Guo   fix minor errors ...
98
  // render modes
09cb5950   David Mayerich   fixed some commen...
99
  bool compareMode = true;					// default mode is compare mode
9627c6e6   Jiaming Guo   add splitting and...
100
  bool mappingMode = false;
8f96cac6   Jiaming Guo   pushed old changes
101
  bool volumeMode = false;
9627c6e6   Jiaming Guo   add splitting and...
102
  
5ac53c9e   Jiaming Guo   add keyboardfunc
103
  // random color set
9627c6e6   Jiaming Guo   add splitting and...
104
105
  std::vector<float> colormap;
  
5ac53c9e   Jiaming Guo   add keyboardfunc
106
107
108
  // special key indicator
  int mods;
  
8f96cac6   Jiaming Guo   pushed old changes
109
110
111
112
113
114
115
116
  <<<<<<< HEAD
  // OpenGL objects
  GLuint cmap_tex = 0;				// texture name for the color map
  
  // Stack view parameter
  stim::gl_texture<unsigned char> S;					// texture storing the image stack
  float planes[3] = { 0.0f, 0.0f, 0.0f };				// plane position in world space
  =======
db598823   David Mayerich   fixed GCC errors ...
117
  //OpenGL objects
09cb5950   David Mayerich   fixed some commen...
118
  GLuint cmap_tex = 0;						//texture name for the color map
db598823   David Mayerich   fixed GCC errors ...
119
  
4d23da9c   Jiaming Guo   add 00_GT.swc,now...
120
  float delta;
09cb5950   David Mayerich   fixed some commen...
121
122
  float sigma = 3;							//default sigma
  float radius = 0.7;							//equals to radius
19ec4731   Jiaming Guo   add light
123
  int adjoint_fac = 0;
e1700dd0   Jiaming Guo   fixed bugs when i...
124
  int light_fac = 0;
3a4a7f69   Jiaming Guo   add new way to se...
125
  int difference_fac = 0;
8f96cac6   Jiaming Guo   pushed old changes
126
  >>>>>>> 09cb5950f628309ac65c6b85119d3f16e5bcd0a2
7c1b82bc   Jiaming Guo   test for 00_GT.swc
127
  
8f96cac6   Jiaming Guo   pushed old changes
128
  // sets an OpenGL viewport taking up the entire window
db598823   David Mayerich   fixed GCC errors ...
129
130
  void glut_render_single_projection(){
  
8f96cac6   Jiaming Guo   pushed old changes
131
132
133
  	glMatrixMode(GL_PROJECTION);					// load the projection matrix for editing
  	glLoadIdentity();								// start with the identity matrix
  	int X = glutGet(GLUT_WINDOW_WIDTH);				// use the whole screen for rendering
db598823   David Mayerich   fixed GCC errors ...
134
  	int Y = glutGet(GLUT_WINDOW_HEIGHT);
8f96cac6   Jiaming Guo   pushed old changes
135
136
137
  	glViewport(0, 0, X, Y);							// specify a viewport for the entire window
  	float aspect = (float)X / (float)Y;				// calculate the aspect ratio
  	gluPerspective(60, aspect, 0.1, 1000000);		// set up a perspective projection
db598823   David Mayerich   fixed GCC errors ...
138
139
  }
  
8f96cac6   Jiaming Guo   pushed old changes
140
  // sets an OpenGL viewport taking up the left half of the window
db598823   David Mayerich   fixed GCC errors ...
141
142
  void glut_render_left_projection(){
  
8f96cac6   Jiaming Guo   pushed old changes
143
144
145
  	glMatrixMode(GL_PROJECTION);					// load the projection matrix for editing
  	glLoadIdentity();								// start with the identity matrix
  	int X = glutGet(GLUT_WINDOW_WIDTH) / 2;			// only use half of the screen for the viewport
db598823   David Mayerich   fixed GCC errors ...
146
  	int Y = glutGet(GLUT_WINDOW_HEIGHT);
8f96cac6   Jiaming Guo   pushed old changes
147
148
149
  	glViewport(0, 0, X, Y);							// specify the viewport on the left
  	float aspect = (float)X / (float)Y;				// calculate the aspect ratio
  	gluPerspective(60, aspect, 0.1, 1000000);		// set up a perspective projection
db598823   David Mayerich   fixed GCC errors ...
150
151
  }
  
8f96cac6   Jiaming Guo   pushed old changes
152
  // sets an OpenGL viewport taking up the right half of the window
db598823   David Mayerich   fixed GCC errors ...
153
154
  void glut_render_right_projection(){
  
8f96cac6   Jiaming Guo   pushed old changes
155
156
157
  	glMatrixMode(GL_PROJECTION);					// load the projection matrix for editing
  	glLoadIdentity();								// start with the identity matrix
  	int X = glutGet(GLUT_WINDOW_WIDTH) / 2;			// only use half of the screen for the viewport
db598823   David Mayerich   fixed GCC errors ...
158
  	int Y = glutGet(GLUT_WINDOW_HEIGHT);
8f96cac6   Jiaming Guo   pushed old changes
159
160
161
  	glViewport(X, 0, X, Y);							// specify the viewport on the right
  	float aspect = (float)X / (float)Y;				// calculate the aspect ratio
  	gluPerspective(60, aspect, 0.1, 1000000);		// set up a perspective projection
db598823   David Mayerich   fixed GCC errors ...
162
163
164
165
  }
  
  void glut_render_modelview(){
  
8f96cac6   Jiaming Guo   pushed old changes
166
167
168
169
170
  	glMatrixMode(GL_MODELVIEW);						// load the modelview matrix for editing
  	glLoadIdentity();								// start with the identity matrix
  	stim::vec3<float> eye = cam.getPosition();		// get the camera position (eye point)
  	stim::vec3<float> focus = cam.getLookAt();		// get the camera focal point
  	stim::vec3<float> up = cam.getUp();				// get the camera "up" orientation
db598823   David Mayerich   fixed GCC errors ...
171
  
8f96cac6   Jiaming Guo   pushed old changes
172
  	gluLookAt(eye[0], eye[1], eye[2], focus[0], focus[1], focus[2], up[0], up[1], up[2]);	// set up the OpenGL camera
db598823   David Mayerich   fixed GCC errors ...
173
174
  }
  
8f96cac6   Jiaming Guo   pushed old changes
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
303
304
305
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
  // draw x slice
  void draw_x_slice(float p) {
  	float x = p;
  	float y = S.size(1);
  	float z = S.size(2);
  
  	float tx = p / S.size(0);
  
  	glBegin(GL_QUADS);										
  	glTexCoord3f(tx, 0, 0);
  	glVertex3f(x, 0, 0);
  
  	glTexCoord3f(tx, 0, 1);
  	glVertex3f(x, 0, z);
  
  	glTexCoord3f(tx, 1, 1);
  	glVertex3f(x, y, z);
  
  	glTexCoord3f(tx, 1, 0);
  	glVertex3f(x, y, 0);
  	glEnd();
  }
  // draw y slice
  void draw_y_slice(float p) {
  	float x = S.size(0);
  	float y = p;
  	float z = S.size(2);
  
  	float ty = p / S.size(1);
  
  	glBegin(GL_QUADS);										
  	glTexCoord3f(0, ty, 0);
  	glVertex3f(0, y, 0);
  
  	glTexCoord3f(0, ty, 1);
  	glVertex3f(0, y, z);
  
  	glTexCoord3f(1, ty, 1);
  	glVertex3f(x, y, z);
  
  	glTexCoord3f(1, ty, 0);
  	glVertex3f(x, y, 0);
  	glEnd();
  }
  // draw z slice
  void draw_z_slice(float p) {
  	float x = S.size(0);
  	float y = S.size(1);
  	float z = p;
  
  	float tz = p / S.size(2);
  
  	glBegin(GL_QUADS);										
  	glTexCoord3f(0, 0, tz);
  	glVertex3f(0, 0, z);
  
  	glTexCoord3f(0, 1, tz);
  	glVertex3f(0, y, z);
  
  	glTexCoord3f(1, 1, tz);
  	glVertex3f(x, y, z);
  
  	glTexCoord3f(1, 0, tz);
  	glVertex3f(x, 0, z);
  	glEnd();
  }
  
  /// draw a bounding box around the data set
  void draw_box() {
  	float c[3] = { S.size(0), S.size(1), S.size(2) };
  	glLineWidth(1.0);
  
  	glBegin(GL_LINE_LOOP);
  	glColor3f(0, 0, 0);
  	glVertex3f(0, 0, 0);
  
  	glColor3f(0, 1, 0);
  	glVertex3f(0, c[1], 0);
  
  	glColor3f(0, 1, 1);
  	glVertex3f(0, c[1], c[2]);
  
  	glColor3f(0, 0, 1);
  	glVertex3f(0, 0, c[2]);
  	glEnd();
  
  	glBegin(GL_LINE_LOOP);
  	glColor3f(1, 0, 0);
  	glVertex3f(c[0], 0, 0);
  
  	glColor3f(1, 1, 0);
  	glVertex3f(c[0], c[1], 0);
  
  	glColor3f(1, 1, 1);
  	glVertex3f(c[0], c[1], c[2]);
  
  	glColor3f(1, 0, 1);
  	glVertex3f(c[0], 0, c[2]);
  	glEnd();
  
  	glBegin(GL_LINES);
  	glColor3f(0, 0, 0);
  	glVertex3f(0, 0, 0);
  	glColor3f(1, 0, 0);
  	glVertex3f(c[0], 0, 0);
  
  	glColor3f(0, 1, 0);
  	glVertex3f(0, c[1], 0);
  	glColor3f(1, 1, 0);
  	glVertex3f(c[0], c[1], 0);
  
  	glColor3f(0, 1, 1);
  	glVertex3f(0, c[1], c[2]);
  	glColor3f(1, 1, 1);
  	glVertex3f(c[0], c[1], c[2]);
  
  	glColor3f(0, 0, 1);
  	glVertex3f(0, 0, c[2]);
  	glColor3f(1, 0, 1);
  	glVertex3f(c[0], 0, c[2]);
  	glEnd();
  }
  void draw_frames() {
  	float c[3] = { S.size(0), S.size(1), S.size(2) };			// store the size of the data set for all three dimensions
  
  	glLineWidth(1.0);
  	glColor3f(1, 0, 0);											// draw the X plane
  	glBegin(GL_LINE_LOOP);
  	glVertex3f(planes[0], 0, 0);
  	glVertex3f(planes[0], c[1], 0);
  	glVertex3f(planes[0], c[1], c[2]);
  	glVertex3f(planes[0], 0, c[2]);
  	glEnd();
  
  	glColor3f(0, 1, 0);											// draw the Y plane
  	glBegin(GL_LINE_LOOP);
  	glVertex3f(0, planes[1], 0);
  	glVertex3f(c[0], planes[1], 0);
  	glVertex3f(c[0], planes[1], c[2]);
  	glVertex3f(0, planes[1], c[2]);
  	glEnd();
  
  	glColor3f(0, 0, 1);											// draw the Z plane
  	glBegin(GL_LINE_LOOP);
  	glVertex3f(0, 0, planes[2]);
  	glVertex3f(c[0], 0, planes[2]);
  	glVertex3f(c[0], c[1], planes[2]);
  	glVertex3f(0, c[1], planes[2]);
  	glEnd();
  }
  
  // enforce bound
  void enforce_bounds() {
  	for (int d = 0; d < 3; d++) {
  		if (planes[d] < 0) planes[d] = 0;
  		if (planes[d] > S.size(d)) planes[d] = S.size(d);
  	}
  }
  
  // draw the network(s)
db598823   David Mayerich   fixed GCC errors ...
335
336
  void glut_render(void) {
  
3a4a7f69   Jiaming Guo   add new way to se...
337
  	stim::vec3<float> p1 = cam.getLookAt() + cam.getUp() * 100000;
a1ce3752   David Mayerich   fixed minor light...
338
  	stim::vec3<float> p2 = cam.getPosition();
dea61e7f   Jiaming Guo   some tests
339
340
341
  
  	// light	
  	GLfloat global_ambient[] = { 0.5, 0.5, 0.5, 1.0 };
f85b1b3a   Jiaming Guo   add proper light
342
  	GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
dea61e7f   Jiaming Guo   some tests
343
  	GLfloat diffuse1[] = { 1.0, 1.0, 1.0, 1.0 };
f85b1b3a   Jiaming Guo   add proper light
344
  	GLfloat diffuse2[] = { 0.4, 0.4, 0.4, 1.0 };
dea61e7f   Jiaming Guo   some tests
345
346
347
  	GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
  	GLfloat position1[] = { p1[0], p1[1], p1[2], 1.0 };		// upper right light source
  	GLfloat position2[] = { p2[0], p2[1], p2[2], 1.0 };		// lower left light source
f85b1b3a   Jiaming Guo   add proper light
348
349
350
351
352
353
  
  	glClearColor(0.0, 0.0, 0.0, 1.0);
  	glShadeModel(GL_SMOOTH);
  
  	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
  
8f96cac6   Jiaming Guo   pushed old changes
354
355
356
357
  	glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);				// set ambient for light 0
  	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse1);				// set diffuse for light 0
  	glLightfv(GL_LIGHT0, GL_SPECULAR, specular);			// set specular for light 0
  	glLightfv(GL_LIGHT0, GL_POSITION, position1);			// set position for light 0
f85b1b3a   Jiaming Guo   add proper light
358
  
8f96cac6   Jiaming Guo   pushed old changes
359
360
361
362
  	glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);				// set ambient for light 1
  	glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse2);				// set diffuse for light 1
  	glLightfv(GL_LIGHT1, GL_SPECULAR, specular);			// set specular for light 1
  	glLightfv(GL_LIGHT1, GL_POSITION, position2);			// set position for light 1
f85b1b3a   Jiaming Guo   add proper light
363
  
21f03d55   Jiaming Guo   add functions for...
364
  	//no mapping, just comparing
8f96cac6   Jiaming Guo   pushed old changes
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
  	if (!flag_mapping) {	
  		if (num_nets == 1) {										// if a single network is loaded
  			glEnable(GL_DEPTH_TEST);								// enable depth
  			glut_render_single_projection();						// fill the entire viewport
  			glut_render_modelview();								// set up the modelview matrix with camera details
  			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// clear the screen
  			if (volumeMode) {
  				draw_box();
  				draw_frames();
  				glEnable(GL_TEXTURE_3D);							// enable 3D texture mapping
  				S.bind();											// bind the texture
  				draw_x_slice(planes[0]);							// draw the X plane
  				draw_y_slice(planes[1]);							// draw the Y plane
  				draw_z_slice(planes[2]);							// draw the Z plane
  				glDisable(GL_TEXTURE_3D);							// disable 3D texture mapping
  			}
  			glColor3f(1.0f, 1.0f, 1.0f);
  			GT.glCenterline0();										// render the GT network (the only one loaded)
  			glDisable(GL_DEPTH_TEST);	
7c1b82bc   Jiaming Guo   test for 00_GT.swc
384
  		}
3a4a7f69   Jiaming Guo   add new way to se...
385
  		
8f96cac6   Jiaming Guo   pushed old changes
386
387
388
389
  		if (num_nets == 2) {												// if two networks are loaded	
  			glEnable(GL_TEXTURE_1D);										// enable texture mapping
  			if (flag_light == 0)
  				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);	// texture map will be used as the network color
e1700dd0   Jiaming Guo   fixed bugs when i...
390
  			else
19ec4731   Jiaming Guo   add light
391
  				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
8f96cac6   Jiaming Guo   pushed old changes
392
  			glBindTexture(GL_TEXTURE_1D, cmap_tex);							// bind the Brewer texture map
c62540c7   Jiaming Guo   normalization
393
  			
8f96cac6   Jiaming Guo   pushed old changes
394
395
396
397
398
399
400
401
402
403
404
  			glEnable(GL_DEPTH_TEST);								// enable depth
  			glut_render_left_projection();							// set up a projection for the left half of the window
  			glut_render_modelview();								// set up the modelview matrix using camera details
  			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// clear the screen
  
  			GT.glCylinder(sigma, radius);							// render the GT network
  			if (flag_adjoint_network == 1) {
  				glDisable(GL_TEXTURE_1D);							// disable texture in order to render in other color
  				glEnable(GL_BLEND);									// enable color blend
  				glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	// set blend function
  				glDisable(GL_DEPTH_TEST);							// should disable depth to render transparancy
19ec4731   Jiaming Guo   add light
405
406
  				glColor4f(0.0f, 0.3f, 0.0f, 0.2f);
  				T.glAdjointCylinder(sigma, radius);
8b30eb84   Jiaming Guo   add SPACE keyboar...
407
408
  				glDisable(GL_BLEND);
  				glEnable(GL_DEPTH_TEST);
19ec4731   Jiaming Guo   add light
409
410
  				glEnable(GL_TEXTURE_1D);
  				glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
8b30eb84   Jiaming Guo   add SPACE keyboar...
411
  			}
7c1b82bc   Jiaming Guo   test for 00_GT.swc
412
  
8f96cac6   Jiaming Guo   pushed old changes
413
414
415
416
417
418
419
420
421
  			glut_render_right_projection();							// set up a projection for the right half of the window
  			glut_render_modelview();								// set up the modelview matrix using camera details
  			
  			T.glCylinder(sigma, radius);							// render the T network
  			if (flag_adjoint_network == 1) {
  				glDisable(GL_TEXTURE_1D);							// temporarily disable texture
  				glEnable(GL_BLEND);									// enable color blend
  				glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	// set blend function
  				glDisable(GL_DEPTH_TEST);							// should disable depth
3a4a7f69   Jiaming Guo   add new way to se...
422
423
  				glColor4f(0.0f, 0.3f, 0.0f, 0.2f);
  				GT.glAdjointCylinder(sigma, radius);
3a4a7f69   Jiaming Guo   add new way to se...
424
425
  				glDisable(GL_BLEND);
  				glEnable(GL_DEPTH_TEST);
8f96cac6   Jiaming Guo   pushed old changes
426
  				glEnable(GL_TEXTURE_1D);							// re-enable texture
3a4a7f69   Jiaming Guo   add new way to se...
427
428
  				glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  			}
19ec4731   Jiaming Guo   add light
429
  
4d23da9c   Jiaming Guo   add 00_GT.swc,now...
430
  			sigma = radius;											// set sigma equal to radius
3a4a7f69   Jiaming Guo   add new way to se...
431
  			glDisable(GL_TEXTURE_1D);
7c1b82bc   Jiaming Guo   test for 00_GT.swc
432
433
  		}
  	}
f7d360df   Jiaming Guo   resoloved conflicts
434
  
7c1b82bc   Jiaming Guo   test for 00_GT.swc
435
436
  	//do comparing and mapping
  	else {	
8f96cac6   Jiaming Guo   pushed old changes
437
438
  		if (num_nets == 1) {											// if a single network is loaded
  			std::cout << "You should have at least two networks to do mapping." << std::endl;	// exit program because there isn't enough network
7c1b82bc   Jiaming Guo   test for 00_GT.swc
439
440
  			exit(1);
  		}
8f96cac6   Jiaming Guo   pushed old changes
441
  		if (num_nets == 2) {											// if two networks are loaded
7c1b82bc   Jiaming Guo   test for 00_GT.swc
442
  			if (compareMode) {
8f96cac6   Jiaming Guo   pushed old changes
443
444
445
  				glEnable(GL_TEXTURE_1D);										// enable texture mapping
  				if (flag_light == 0)
  					glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);	// texture map will be used as the network color
e1700dd0   Jiaming Guo   fixed bugs when i...
446
  				else
8f96cac6   Jiaming Guo   pushed old changes
447
448
  					glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);// map light to texture
  				glBindTexture(GL_TEXTURE_1D, cmap_tex);							// bind the Brewer texture map
c62540c7   Jiaming Guo   normalization
449
  				
8f96cac6   Jiaming Guo   pushed old changes
450
451
452
  				glEnable(GL_DEPTH_TEST);								// enable depth
  				glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// clear the screen
  				glut_render_left_projection();							// set up a projection for the left half of the window
c62540c7   Jiaming Guo   normalization
453
  				glut_render_modelview();								//set up the modelview matrix using camera details
dea61e7f   Jiaming Guo   some tests
454
  
8f96cac6   Jiaming Guo   pushed old changes
455
456
457
458
459
460
  				_GT.glCylinder(sigma, radius);							// render the GT network
  				if (flag_adjoint_network == 1) {
  					glDisable(GL_TEXTURE_1D);							// temporarily disable texture
  					glEnable(GL_BLEND);									// enable color blend
  					glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	// set blend function
  					glDisable(GL_DEPTH_TEST);							// should disable depth
19ec4731   Jiaming Guo   add light
461
462
  					glColor4f(0.0f, 0.3f, 0.0f, 0.2f);
  					_T.glAdjointCylinder(sigma, radius);
8b30eb84   Jiaming Guo   add SPACE keyboar...
463
464
  					glDisable(GL_BLEND);
  					glEnable(GL_DEPTH_TEST);
8f96cac6   Jiaming Guo   pushed old changes
465
  					glEnable(GL_TEXTURE_1D);							// re-enable texture
19ec4731   Jiaming Guo   add light
466
  					glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
8b30eb84   Jiaming Guo   add SPACE keyboar...
467
  				}
9627c6e6   Jiaming Guo   add splitting and...
468
  
8f96cac6   Jiaming Guo   pushed old changes
469
470
  				glut_render_right_projection();							// set up a projection for the right half of the window
  				glut_render_modelview();								// set up the modelview matrix using camera details
19ec4731   Jiaming Guo   add light
471
  
8f96cac6   Jiaming Guo   pushed old changes
472
473
474
475
476
  				_T.glCylinder(sigma, radius);							// render the T network
  				if (flag_adjoint_network == 1) {
  					glDisable(GL_TEXTURE_1D);							// temporarily disable texture
  					glEnable(GL_BLEND);									// enable color blend
  					glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	// set blend function
3a4a7f69   Jiaming Guo   add new way to se...
477
478
479
  					glDisable(GL_DEPTH_TEST);							//should disable depth
  					glColor4f(0.0f, 0.3f, 0.0f, 0.2f);
  					_GT.glAdjointCylinder(sigma, radius);
3a4a7f69   Jiaming Guo   add new way to se...
480
481
  					glDisable(GL_BLEND);
  					glEnable(GL_DEPTH_TEST);
8f96cac6   Jiaming Guo   pushed old changes
482
  					glEnable(GL_TEXTURE_1D);							// re-enable texture
3a4a7f69   Jiaming Guo   add new way to se...
483
484
  					glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  				}
19ec4731   Jiaming Guo   add light
485
  
8f96cac6   Jiaming Guo   pushed old changes
486
  				sigma = radius;											// set sigma equal to radius
3a4a7f69   Jiaming Guo   add new way to se...
487
  				glDisable(GL_TEXTURE_1D);
7c1b82bc   Jiaming Guo   test for 00_GT.swc
488
  			}
8f96cac6   Jiaming Guo   pushed old changes
489
  			else if (mappingMode) {
dea61e7f   Jiaming Guo   some tests
490
  				glEnable(GL_COLOR_MATERIAL);
8f96cac6   Jiaming Guo   pushed old changes
491
492
493
494
  				glEnable(GL_DEPTH_TEST);								// enable depth
  				glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// clear the screen
  				glut_render_left_projection();							// set up a projection for the left half of the window
  				glut_render_modelview();								// set up the modelview matrix using camera details
3a4a7f69   Jiaming Guo   add new way to se...
495
  				
8f96cac6   Jiaming Guo   pushed old changes
496
  				if (flag_highlight_difference == 0)
3a4a7f69   Jiaming Guo   add new way to se...
497
  					_GT.glRandColorCylinder(0, _gt_t, colormap, sigma, radius);
3a4a7f69   Jiaming Guo   add new way to se...
498
499
500
  				else
  					_GT.glDifferenceCylinder(0, _gt_t, colormap, sigma, radius);
  				
9627c6e6   Jiaming Guo   add splitting and...
501
  
8f96cac6   Jiaming Guo   pushed old changes
502
503
  				glut_render_right_projection();							// set up a projection for the right half of the window
  				glut_render_modelview();								// set up the modelview matrix using camera details
3a4a7f69   Jiaming Guo   add new way to se...
504
  					
8f96cac6   Jiaming Guo   pushed old changes
505
  				if (flag_highlight_difference == 0)
3a4a7f69   Jiaming Guo   add new way to se...
506
  					_T.glRandColorCylinder(1, _t_gt, colormap, sigma, radius);
3a4a7f69   Jiaming Guo   add new way to se...
507
508
  				else
  					_T.glDifferenceCylinder(1, _t_gt, colormap, sigma, radius);
19ec4731   Jiaming Guo   add light
509
  
8f96cac6   Jiaming Guo   pushed old changes
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
  				sigma = radius;											// set sigma equal to radius
  			}
  			else if (volumeMode) {
  				
  				glEnable(GL_DEPTH_TEST);								// enable depth
  				glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// clear the screen
  				glut_render_left_projection();							// set up a projection for the left half of the window
  				glut_render_modelview();								// set up the modelview matrix using camera details
  				
  				draw_box();
  				draw_frames();
  				glDisable(GL_TEXTURE_1D);								// disable 1D  texture
  				glEnable(GL_TEXTURE_3D);								// enable 3D texture mapping
  				S.bind();												// bind the texture
  				draw_x_slice(planes[0]);								// draw the X plane
  				draw_y_slice(planes[1]);								// draw the Y plane
  				draw_z_slice(planes[2]);								// draw the Z plane
  				glDisable(GL_TEXTURE_3D);								// disable 3D texture mapping
  				GT.glCylinder(sigma, radius);
  
  				glut_render_right_projection();							// set up a projection for the right half of the window
  				glut_render_modelview();								// set up the modelview matrix using camera details
  
  				draw_box();
  				draw_frames();
  				glDisable(GL_TEXTURE_1D);								// disable 1D  texture
  				glEnable(GL_TEXTURE_3D);								// enable 3D texture mapping
  				S.bind();												// bind the texture
  				draw_x_slice(planes[0]);								// draw the X plane
  				draw_y_slice(planes[1]);								// draw the Y plane
  				draw_z_slice(planes[2]);								// draw the Z plane
  				glDisable(GL_TEXTURE_3D);								// disable 3D texture mapping
  				T.glCylinder(sigma, radius);
  				glColor3f(1.0f, 1.0f, 1.0f);
  
  				sigma = radius;
9627c6e6   Jiaming Guo   add splitting and...
546
547
  			}
  		}
db598823   David Mayerich   fixed GCC errors ...
548
  	}
c62540c7   Jiaming Guo   normalization
549
  	glDisable(GL_DEPTH_TEST);
e1700dd0   Jiaming Guo   fixed bugs when i...
550
  
8f96cac6   Jiaming Guo   pushed old changes
551
  	if (num_nets == 2) {												// works only with two networks
e1700dd0   Jiaming Guo   fixed bugs when i...
552
  		std::ostringstream ss;
8f96cac6   Jiaming Guo   pushed old changes
553
  		if (mappingMode)												// if it is in mapping mode
e1700dd0   Jiaming Guo   fixed bugs when i...
554
  			ss << "Mapping Mode";
8f96cac6   Jiaming Guo   pushed old changes
555
556
  		else if (compareMode)
  			ss << "Compare Mode";										// default mode is compare mode
e1700dd0   Jiaming Guo   fixed bugs when i...
557
  		else
8f96cac6   Jiaming Guo   pushed old changes
558
  			ss << "volumeDisplay";
e1700dd0   Jiaming Guo   fixed bugs when i...
559
  
8f96cac6   Jiaming Guo   pushed old changes
560
  		if (flag_light == 1)
e1700dd0   Jiaming Guo   fixed bugs when i...
561
  			glDisable(GL_LIGHTING);
8f96cac6   Jiaming Guo   pushed old changes
562
  		glMatrixMode(GL_PROJECTION);									// set up the 2d viewport for mode text printing
e1700dd0   Jiaming Guo   fixed bugs when i...
563
564
  		glPushMatrix();
  		glLoadIdentity();
8f96cac6   Jiaming Guo   pushed old changes
565
566
567
568
569
  		int X = glutGet(GLUT_WINDOW_WIDTH);								// get the current window width
  		int Y = glutGet(GLUT_WINDOW_HEIGHT);							// get the current window height
  		glViewport(0, 0, X / 2, Y);										// locate to left bottom corner
  		gluOrtho2D(0, X, 0, Y);											// define othogonal aspect
  		glColor3f(0.8, 0.0, 0.0);										// using red to show mode
e1700dd0   Jiaming Guo   fixed bugs when i...
570
571
572
573
574
  
  		glMatrixMode(GL_MODELVIEW);
  		glPushMatrix();
  		glLoadIdentity();
  
8f96cac6   Jiaming Guo   pushed old changes
575
  		glRasterPos2f(0, 5);											//print text in the left bottom corner
e1700dd0   Jiaming Guo   fixed bugs when i...
576
577
578
579
580
  		glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, (const unsigned char*)(ss.str().c_str()));
  
  		glPopMatrix();
  		glMatrixMode(GL_PROJECTION);
  		glPopMatrix();
8f96cac6   Jiaming Guo   pushed old changes
581
582
  		glColor3f(1.0, 1.0, 1.0);										//clear red color
  		if (flag_light == 1)
e1700dd0   Jiaming Guo   fixed bugs when i...
583
584
585
  			glEnable(GL_LIGHTING);
  	}
  
f85b1b3a   Jiaming Guo   add proper light
586
587
  	glDisable(GL_COLOR_MATERIAL);
  
db598823   David Mayerich   fixed GCC errors ...
588
589
590
591
592
593
  	glutSwapBuffers();
  }
  
  // defines camera motion based on mouse dragging
  void glut_motion(int x, int y){
  	
8f96cac6   Jiaming Guo   pushed old changes
594
595
  	int mods = glutGetModifiers();
  	if(LButtonDown == true && RButtonDown == false && mods == 0){
db598823   David Mayerich   fixed GCC errors ...
596
  
8f96cac6   Jiaming Guo   pushed old changes
597
598
  	float theta = orbit_factor * (mouse_x - x);		// determine the number of degrees along the x-axis to rotate
  	float phi = orbit_factor * (y - mouse_y);		// number of degrees along the y-axis to rotate
db598823   David Mayerich   fixed GCC errors ...
599
  
8f96cac6   Jiaming Guo   pushed old changes
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
  	cam.OrbitFocus(theta, phi);						// rotate the camera around the focal point
  	}
  	else if (mods != 0) {
  		float dx = (float)(x - mouse_x);
  		float dist = dx;							// calculate the distance that the mouse moved in pixel coordinates
  		float sdist = dist;							// scale the distance by the sensitivity
  		if (mods == GLUT_ACTIVE_SHIFT) {			// if the SHIFT key is pressed
  			planes[0] += (sdist)* S.spacing(0);		// move the X plane based on the mouse wheel direction
  		}
  		else if (mods == GLUT_ACTIVE_CTRL) {		// if the CTRL key is pressed
  			planes[1] += (sdist)* S.spacing(1);		// move the Y plane based on the mouse wheel direction
  		}
  		else if (mods == GLUT_ACTIVE_ALT) {			// if hte ALT key is pressed
  			planes[2] += (sdist)* S.spacing(2);		// move the Z plane based on the mouse wheel direction
  		}
  		enforce_bounds();
  	}
db598823   David Mayerich   fixed GCC errors ...
617
  
8f96cac6   Jiaming Guo   pushed old changes
618
  	mouse_x = x;									// update the mouse position
db598823   David Mayerich   fixed GCC errors ...
619
  	mouse_y = y;
8f96cac6   Jiaming Guo   pushed old changes
620
621
  
  	glutPostRedisplay();							// re-draw the visualization
db598823   David Mayerich   fixed GCC errors ...
622
623
  }
  
f001495e   Jiaming Guo   fix minor errors ...
624
625
  // sets the menu options
  void glut_menu(int value) {
8f96cac6   Jiaming Guo   pushed old changes
626
  	
f001495e   Jiaming Guo   fix minor errors ...
627
628
629
  	if (value == 1) {								// menu 1 represents comparing mode
  		compareMode = true;
  		mappingMode = false;
8f96cac6   Jiaming Guo   pushed old changes
630
  		volumeMode = false;
f001495e   Jiaming Guo   fix minor errors ...
631
632
633
634
  	}
  	if (value == 2) {								// menu 2 represents mapping mode
  		compareMode = false;
  		mappingMode = true;
8f96cac6   Jiaming Guo   pushed old changes
635
636
637
638
639
640
  		volumeMode = false;
  	}
  	if (value == 3) {								// menu 3 represents volume mode
  		compareMode = false;
  		mappingMode = false;
  		volumeMode = true;
f001495e   Jiaming Guo   fix minor errors ...
641
  	}
8f96cac6   Jiaming Guo   pushed old changes
642
  	if (value == 4) {
f001495e   Jiaming Guo   fix minor errors ...
643
644
645
646
647
  		exit(0);
  	}
  	glutPostRedisplay();
  }
  
db598823   David Mayerich   fixed GCC errors ...
648
649
  // sets the mouse position when clicked
  void glut_mouse(int button, int state, int x, int y){
9627c6e6   Jiaming Guo   add splitting and...
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
  	
  	if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  		mouse_x = x;
  		mouse_y = y;
  		LButtonDown = true;
  	}
  	else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){
  		mouse_x = x;
  		mouse_y = y;
  		RButtonDown = true;
  	}
  	else if(button == GLUT_LEFT_BUTTON && state == GLUT_UP){
  		mouse_x = x;
  		mouse_y = y;
  		LButtonDown = false;
  	}
  	else if(button == GLUT_RIGHT_BUTTON && state == GLUT_UP){
  		mouse_x = x;
  		mouse_y = y;
  		RButtonDown = false;
  	}
9627c6e6   Jiaming Guo   add splitting and...
671
672
  }
  
f001495e   Jiaming Guo   fix minor errors ...
673
674
  // define camera move based on mouse wheel move(actually we can combine this with glut_mouse)
  void glut_wheel(int wheel, int direction, int x, int y) {
8f96cac6   Jiaming Guo   pushed old changes
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
  	
  	int mods = glutGetModifiers();
  	if (mods == GLUT_ACTIVE_SHIFT) {					// if the SHIFT key is pressed
  		planes[0] += (direction)* S.spacing(0);			// move the X plane based on the mouse wheel direction
  	}
  	else if (mods == GLUT_ACTIVE_CTRL) {				// if the CTRL key is pressed
  		planes[1] += (direction)* S.spacing(1);			// move the Y plane based on the mouse wheel direction
  	}
  	else if (mods == GLUT_ACTIVE_ALT) {					// if hte ALT key is pressed
  		planes[2] += (direction)* S.spacing(2);			// move the Z plane based on the mouse wheel direction
  	}
  	else {
  		if (direction > 0)								// if it is button 3(up), move closer
  			delta = zoom_factor;
  		else											// if it is button 4(down), leave farther
  			delta = -zoom_factor;
  	}
  	enforce_bounds();
b31bfd7d   David Mayerich   added SWC files a...
693
694
  
  	cam.Push(delta);
f001495e   Jiaming Guo   fix minor errors ...
695
696
697
698
  	glutPostRedisplay();
  }
  
  // define keyboard inputs
9627c6e6   Jiaming Guo   add splitting and...
699
  void glut_keyboard(unsigned char key, int x, int y){
4d23da9c   Jiaming Guo   add 00_GT.swc,now...
700
701
702
703
704
  	
  	// register different keyboard operation
  	switch (key) {
  		
  		// change render mode
8f96cac6   Jiaming Guo   pushed old changes
705
706
  		case 'm':																			// if keyboard 'm' is pressed, then change render mode
  			if (compareMode && !mappingMode && flag_mapping && !flag_adjoint_network) {		// if current mode is comparing mode
4d23da9c   Jiaming Guo   add 00_GT.swc,now...
707
708
709
  				compareMode = false;
  				mappingMode = true;
  			}
8f96cac6   Jiaming Guo   pushed old changes
710
  			else if (!compareMode && mappingMode && flag_mapping && !flag_adjoint_network) {// if current mode is mapping mode
4d23da9c   Jiaming Guo   add 00_GT.swc,now...
711
712
713
714
715
  				compareMode = true;
  				mappingMode = false;
  			}
  			break;
  
8f96cac6   Jiaming Guo   pushed old changes
716
717
718
719
720
721
722
723
  		// render the image stack
  		case 'v':
  			if (!volumeMode && !flag_mapping)
  				volumeMode = true;
  			else if (volumeMode && !flag_mapping)
  				volumeMode = false;
  			break;
  
4d23da9c   Jiaming Guo   add 00_GT.swc,now...
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
  		// zooming
  		case 'w':						// if keyboard 'w' is pressed, then move closer
  			delta = zoom_factor;
  			cam.Push(delta);
  			break;
  		case 's':						// if keyboard 's' is pressed, then leave farther
  			delta = -zoom_factor;
  			cam.Push(delta);
  			break;
  
  		// resample and re-render the cylinder in different radius
  		case 'd':						// if keyboard 'd' is pressed, then increase radius by radius_factor
  			radius += radius_factor;
  			break;
  		case 'a':						// if keyboard 'a' is pressed, then decrease radius by radius_factor
  			radius -= radius_factor;
  			// get rid of the degenerated case when radius decrease below 0
  			if (radius < 0.001f)
  				radius = 0.2;
  			break;
19ec4731   Jiaming Guo   add light
744
  
e1700dd0   Jiaming Guo   fixed bugs when i...
745
746
  		// turn on/off the light
  		case 'l':						// if keyboard 'l' is pressed, then change the light
8f96cac6   Jiaming Guo   pushed old changes
747
748
  			if (!flag_light && !flag_adjoint_network) {
  				flag_light = 1;
e1700dd0   Jiaming Guo   fixed bugs when i...
749
750
751
  				glEnable(GL_LIGHTING);
  				glEnable(GL_LIGHT0);
  				glEnable(GL_LIGHT1);
e1700dd0   Jiaming Guo   fixed bugs when i...
752
  			}
8f96cac6   Jiaming Guo   pushed old changes
753
754
  			else if (flag_light && !flag_adjoint_network) {
  				flag_light = 0;
e1700dd0   Jiaming Guo   fixed bugs when i...
755
756
757
  				glDisable(GL_LIGHTING);
  				glDisable(GL_LIGHT0);
  				glDisable(GL_LIGHT1);
e1700dd0   Jiaming Guo   fixed bugs when i...
758
759
760
  			}
  			break;
  
19ec4731   Jiaming Guo   add light
761
  		// render a transparant T very close to GT in compare mode
8f96cac6   Jiaming Guo   pushed old changes
762
763
764
765
766
  		case 32:						// if keyboard 'SPACE' is pressed, then change the flag_adjoint_network
  			if (!flag_adjoint_network && compareMode && !flag_light)
  				flag_adjoint_network = 1;
  			else if (flag_adjoint_network && compareMode && !flag_light)
  				flag_adjoint_network = 0;
8b30eb84   Jiaming Guo   add SPACE keyboar...
767
  			break;
4d23da9c   Jiaming Guo   add 00_GT.swc,now...
768
  
3a4a7f69   Jiaming Guo   add new way to se...
769
770
  		// render only the difference
  		case 'h':
8f96cac6   Jiaming Guo   pushed old changes
771
772
773
774
  			if (!flag_highlight_difference && mappingMode && !flag_light)
  				flag_highlight_difference = 1;
  			else if (flag_highlight_difference && mappingMode && !flag_light)
  				flag_highlight_difference = 0;
3a4a7f69   Jiaming Guo   add new way to se...
775
776
  			break;
  
4d23da9c   Jiaming Guo   add 00_GT.swc,now...
777
778
779
  		// close window and exit application
  		case 27:						// if keyboard 'ESC' is pressed, then exit
  			exit(0);
f001495e   Jiaming Guo   fix minor errors ...
780
  	}
9627c6e6   Jiaming Guo   add splitting and...
781
  	glutPostRedisplay();
db598823   David Mayerich   fixed GCC errors ...
782
783
  }
  
8f96cac6   Jiaming Guo   pushed old changes
784
  #define BREWER_CTRL_PTS 11										// number of control points in the Brewer map
db598823   David Mayerich   fixed GCC errors ...
785
786
787
  void texture_initialize(){
  
  	//define the colormap
8f96cac6   Jiaming Guo   pushed old changes
788
  	static float  brewer_map[BREWER_CTRL_PTS][3] = {			// generate a Brewer color map (blue to red)
db598823   David Mayerich   fixed GCC errors ...
789
790
791
792
793
794
795
796
797
798
799
800
801
  		{0.192157f, 0.211765f, 0.584314f},
  		{0.270588f, 0.458824f, 0.705882f},
  		{0.454902f, 0.678431f, 0.819608f},
  		{0.670588f, 0.85098f, 0.913725f},
  		{0.878431f, 0.952941f, 0.972549f},
  		{1.0f, 1.0f, 0.74902f},
  		{0.996078f, 0.878431f, 0.564706f},
  		{0.992157f, 0.682353f, 0.380392f},
  		{0.956863f, 0.427451f, 0.262745f},
  		{0.843137f, 0.188235f, 0.152941f},
  		{0.647059f, 0.0f, 0.14902f}
  	};
  
8f96cac6   Jiaming Guo   pushed old changes
802
803
  	glGenTextures(1, &cmap_tex);								// generate a texture map name
  	glBindTexture(GL_TEXTURE_1D, cmap_tex);						// bind the texture map
db598823   David Mayerich   fixed GCC errors ...
804
  
8f96cac6   Jiaming Guo   pushed old changes
805
  	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);		// enable linear interpolation
db598823   David Mayerich   fixed GCC errors ...
806
  	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
8f96cac6   Jiaming Guo   pushed old changes
807
808
  	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP);			// clamp the values at the minimum and maximum
  	glTexImage1D(GL_TEXTURE_1D, 0, 3, BREWER_CTRL_PTS, 0, GL_RGB, GL_FLOAT,	// upload the texture map to the GPU
db598823   David Mayerich   fixed GCC errors ...
809
  					brewer_map);
8f96cac6   Jiaming Guo   pushed old changes
810
811
812
  	if (flag_stack == 1) {
  		S.attach();												// attach 3D texture
  	}
db598823   David Mayerich   fixed GCC errors ...
813
814
  }
  
8f96cac6   Jiaming Guo   pushed old changes
815
  // Initialize the OpenGL (GLUT) window, including starting resolution, callbacks, texture maps, and camera
db598823   David Mayerich   fixed GCC errors ...
816
817
  void glut_initialize(){
  	
8f96cac6   Jiaming Guo   pushed old changes
818
  	int myargc = 1;												// GLUT requires arguments, so create some bogus ones
db598823   David Mayerich   fixed GCC errors ...
819
820
821
  	char* myargv[1];
  	myargv [0]=strdup ("netmets");
  
8f96cac6   Jiaming Guo   pushed old changes
822
  	glutInit(&myargc, myargv);									// pass bogus arguments to glutInit()
f001495e   Jiaming Guo   fix minor errors ...
823
  	glutSetOption(GLUT_MULTISAMPLE, 8);
8f96cac6   Jiaming Guo   pushed old changes
824
825
826
827
828
829
830
831
832
833
834
835
  	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);	// generate a color buffer, depth buffer, and enable double buffering
  	glutInitWindowPosition(100,100);							// set the initial window position
  	glutInitWindowSize(320, 320);								// set the initial window size
  	glutCreateWindow("NetMets - STIM Lab, UH");					// set the dialog box title
  
  #ifdef _WIN32
  	GLenum err = glewInit();									// initialize GLEW (necessary for Windows)
  	if (GLEW_OK != err) {										// eror with GLEW
  		std::cout << "Error with GLEW: " << glewGetErrorString(err) << std::endl;
  		exit(1);
  	}
  #endif
db598823   David Mayerich   fixed GCC errors ...
836
  	
db598823   David Mayerich   fixed GCC errors ...
837
  	// register callback functions
8f96cac6   Jiaming Guo   pushed old changes
838
839
840
841
842
843
844
845
846
847
  	glutDisplayFunc(glut_render);				// function executed for rendering - renders networks
  	glutMouseFunc(glut_mouse);					// executed on a mouse click - sets starting mouse positions for rotations
  	glutMotionFunc(glut_motion);				// executed when the mouse is moved while a button is pressed
  	if (flag_mapping == 1) {					// only in mapping mode, keyboard will be used
  		glutCreateMenu(glut_menu);				// register menu option callback
  		glutAddMenuEntry("Comparing Mode", 1);	// register menu 1 as comparing mode
  		glutAddMenuEntry("Mapping Mode", 2);	// register menu 2 as mapping mode
  		glutAddMenuEntry("Volume Display", 3);	// register menu 3 as volume metric mode
  		glutAddMenuEntry("Exit", 4);			// register menu 4 as exiting
  		glutAttachMenu(GLUT_RIGHT_BUTTON);		// register right mouse to open menu option
4d23da9c   Jiaming Guo   add 00_GT.swc,now...
848
  	}		
8f96cac6   Jiaming Guo   pushed old changes
849
  	glutKeyboardFunc(glut_keyboard);			// register keyboard callback
b31bfd7d   David Mayerich   added SWC files a...
850
  	glutMouseWheelFunc(glut_wheel);		
db598823   David Mayerich   fixed GCC errors ...
851
  
8f96cac6   Jiaming Guo   pushed old changes
852
  	texture_initialize();						// set up texture mapping (create texture maps, enable features)
db598823   David Mayerich   fixed GCC errors ...
853
  
8f96cac6   Jiaming Guo   pushed old changes
854
  	stim::vec3<float> c = bb.center();			// get the center of the network bounding box
db598823   David Mayerich   fixed GCC errors ...
855
  
8f96cac6   Jiaming Guo   pushed old changes
856
  	// place the camera along the z-axis at a distance determined by the network size along x and y
db598823   David Mayerich   fixed GCC errors ...
857
  	cam.setPosition(c + stim::vec<float>(0, 0, camera_factor * std::max(bb.size()[0], bb.size()[1])));
8f96cac6   Jiaming Guo   pushed old changes
858
  	cam.LookAt(c[0], c[1], c[2]);				// look at the center of the network
db598823   David Mayerich   fixed GCC errors ...
859
860
  }
  
f86a38d3   Jiaming Guo   add device choice...
861
  #ifdef __CUDACC__
f001495e   Jiaming Guo   fix minor errors ...
862
  // set specific device to work on
f86a38d3   Jiaming Guo   add device choice...
863
864
  void setdevice(int &device){
  	int count;
8f96cac6   Jiaming Guo   pushed old changes
865
  	cudaGetDeviceCount(&count);					// numbers of device that are available
f86a38d3   Jiaming Guo   add device choice...
866
867
868
869
870
871
872
  	if(count < device + 1){
  	std::cout<<"No such device available, please set another device"<<std::endl;
  	exit(1);
  	}
  }
  #else
  void setdevice(int &device){
8f96cac6   Jiaming Guo   pushed old changes
873
  	device = -1;								// set to default -1
f86a38d3   Jiaming Guo   add device choice...
874
875
876
  }
  #endif
  
8f96cac6   Jiaming Guo   pushed old changes
877
  // compare both networks and fill the networks with error information
f86a38d3   Jiaming Guo   add device choice...
878
  void compare(float sigma, int device){
db598823   David Mayerich   fixed GCC errors ...
879
  
8f96cac6   Jiaming Guo   pushed old changes
880
881
  	GT = GT.compare(T, sigma, device);				// compare the ground truth to the test case - store errors in GT
      T = T.compare(GT, sigma, device);				// compare the test case to the ground truth - store errors in T
db598823   David Mayerich   fixed GCC errors ...
882
883
  
  	//calculate the metrics
8f96cac6   Jiaming Guo   pushed old changes
884
  	float FPR = GT.average();						// calculate the metrics
691fa079   David Mayerich   initial pass adap...
885
  	float FNR = T.average();
db598823   David Mayerich   fixed GCC errors ...
886
  	
8f96cac6   Jiaming Guo   pushed old changes
887
  	std::cout << "FNR: " << FPR << std::endl;		// print false alarms and misses
db598823   David Mayerich   fixed GCC errors ...
888
889
890
  	std::cout << "FPR: " << FNR << std::endl;
  }
  
8f96cac6   Jiaming Guo   pushed old changes
891
892
  // split and map two networks and fill the networks' R with metric information
  void mapping(float sigma, int device, float threshold){
9627c6e6   Jiaming Guo   add splitting and...
893
  
f001495e   Jiaming Guo   fix minor errors ...
894
  	// compare and split two networks
21f03d55   Jiaming Guo   add functions for...
895
896
  	_GT.split(GT, T, sigma, device, threshold);
  	_T.split(T, GT, sigma, device, threshold);
9627c6e6   Jiaming Guo   add splitting and...
897
  
f001495e   Jiaming Guo   fix minor errors ...
898
  	// mapping two new splitted networks and get their edge relation
21f03d55   Jiaming Guo   add functions for...
899
900
  	_GT.mapping(_T, _gt_t, device, threshold);
  	_T.mapping(_GT, _t_gt, device, threshold);
9627c6e6   Jiaming Guo   add splitting and...
901
  
f001495e   Jiaming Guo   fix minor errors ...
902
  	// generate random color set based on the number of edges in GT
8f96cac6   Jiaming Guo   pushed old changes
903
904
  	size_t num = _gt_t.size();						// also create random color for unmapping edge, but won't be used though
  	colormap.resize(3 * num);						// 3 portions compound RGB
5ac53c9e   Jiaming Guo   add keyboardfunc
905
  	for(int i = 0; i < 3 * num; i++)
8f96cac6   Jiaming Guo   pushed old changes
906
  		colormap[i] = rand()/(float)RAND_MAX;		// set to [0, 1]
9627c6e6   Jiaming Guo   add splitting and...
907
908
  	
  	//calculate the metrics
8f96cac6   Jiaming Guo   pushed old changes
909
  	float FPR = _GT.average(0);						// calculate the metrics
9627c6e6   Jiaming Guo   add splitting and...
910
911
  	float FNR = _T.average(0);
  	
8f96cac6   Jiaming Guo   pushed old changes
912
  	std::cout << "FNR: " << FPR << std::endl;		// print false alarms and misses
9627c6e6   Jiaming Guo   add splitting and...
913
914
915
  	std::cout << "FPR: " << FNR << std::endl;
  }
  
db598823   David Mayerich   fixed GCC errors ...
916
917
918
  // writes features of the networks i.e average segment length, tortuosity, branching index, contraction, fractal dimension, number of end and branch points to a csv file
  // Pranathi wrote this - saves network features to a CSV file
  void features(std::string filename){
f86a38d3   Jiaming Guo   add device choice...
919
  		double avgL_t, avgL_gt, avgT_t, avgT_gt, avgB_t, avgB_gt, avgC_t, avgC_gt, avgFD_t, avgFD_gt;
db598823   David Mayerich   fixed GCC errors ...
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
  		unsigned int e_t, e_gt, b_gt, b_t;
  		avgL_gt = GT.Lengths();
  		avgT_gt = GT.Tortuosities();
  		avgL_t = T.Lengths();
  		avgT_t = T.Tortuosities();
  		avgB_gt = GT.BranchingIndex();
  		avgB_t = T.BranchingIndex();
  		avgC_gt = GT.Contractions();
  		avgFD_gt = GT.FractalDimensions();
  		avgC_t = T.Contractions();
  		avgFD_t = T.FractalDimensions();
  		e_gt = GT.EndP();
  		e_t = T.EndP();
  		b_gt = GT.BranchP();
  		b_t = T.BranchP();
  		std::ofstream myfile;
  		myfile.open (filename.c_str());
  		myfile << "Length, Tortuosity, Contraction, Fractal Dimension, Branch Points, End points, Branching Index, \n";
  		myfile << avgL_gt << "," << avgT_gt << "," << avgC_gt << "," << avgFD_gt << "," << b_gt << "," << e_gt << "," << avgB_gt <<std::endl;
  		myfile << avgL_t << "," << avgT_t << "," << avgC_t << "," << avgFD_t << "," << b_t << "," << e_t << "," << avgB_t <<std::endl;
  		myfile.close();
  }
  
  // Output an advertisement for the lab, authors, and usage information
  void advertise(){
  	std::cout<<std::endl<<std::endl;
  	std::cout<<"========================================================================="<<std::endl;
  	std::cout<<"Thank you for using the NetMets network comparison tool!"<<std::endl;
  	std::cout<<"Scalable Tissue Imaging and Modeling (STIM) Lab, University of Houston"<<std::endl;
21f03d55   Jiaming Guo   add functions for...
949
  	std::cout<<"Developers: Pranathi Vemuri, David Mayerich, Jiaming Guo"<<std::endl;
9627c6e6   Jiaming Guo   add splitting and...
950
  	std::cout<<"Source: https://git.stim.ee.uh.edu/segmentation/netmets" <<std::endl;
db598823   David Mayerich   fixed GCC errors ...
951
952
  	std::cout<<"========================================================================="<<std::endl<<std::endl;
  
f86a38d3   Jiaming Guo   add device choice...
953
  	std::cout<<"usage: netmets file1 file2 --sigma 3"<<std::endl;
f001495e   Jiaming Guo   fix minor errors ...
954
  	std::cout<<"            compare two .obj files with a tolerance of 3 (units defined by the network)"<<std::endl<<std::endl;
f86a38d3   Jiaming Guo   add device choice...
955
956
957
958
  	std::cout<<"       netmets file1 --gui"<<std::endl;
  	std::cout<<"            load a file and display it using OpenGL"<<std::endl<<std::endl;
  	std::cout<<"       netmets file1 file2 --device 0"<<std::endl;
  	std::cout<<"            compare two files using device 0 (if there isn't a gpu, use cpu)"<<std::endl<<std::endl;
21f03d55   Jiaming Guo   add functions for...
959
960
  	std::cout<<"       netmets file1 file2 --mapping value"<<std::endl;
  	std::cout<<"            mapping two files in random colors with a threshold of value"<<std::endl<<std::endl;
db598823   David Mayerich   fixed GCC errors ...
961
962
  }
  
8f96cac6   Jiaming Guo   pushed old changes
963
964
965
966
967
  <<<<<<< HEAD
  int main(int argc, char* argv[])
  {
  	stim::arglist args;						// create an instance of arglist
  =======
09cb5950   David Mayerich   fixed some commen...
968
  int main(int argc, char* argv[]) {
db598823   David Mayerich   fixed GCC errors ...
969
  	stim::arglist args;						//create an instance of arglist
8f96cac6   Jiaming Guo   pushed old changes
970
  >>>>>>> 09cb5950f628309ac65c6b85119d3f16e5bcd0a2
db598823   David Mayerich   fixed GCC errors ...
971
  
8f96cac6   Jiaming Guo   pushed old changes
972
  	// add arguments
db598823   David Mayerich   fixed GCC errors ...
973
  	args.add("help", "prints this help");
f86a38d3   Jiaming Guo   add device choice...
974
  	args.add("sigma", "force a sigma value to specify the tolerance of the network comparison", "3");
db598823   David Mayerich   fixed GCC errors ...
975
  	args.add("gui", "display the network or network comparison using OpenGL");
f86a38d3   Jiaming Guo   add device choice...
976
  	args.add("device", "choose specific device to run", "0");
db598823   David Mayerich   fixed GCC errors ...
977
  	args.add("features", "save features to a CSV file, specify file name");
9627c6e6   Jiaming Guo   add splitting and...
978
  	args.add("mapping", "mapping input according to similarity");
8f96cac6   Jiaming Guo   pushed old changes
979
980
  	args.add("stack", "load the image stacks");
  	args.add("spacing", "spacing between pixel samples in each dimension", "1.0 1.0 1.0", "any real positive value");
db598823   David Mayerich   fixed GCC errors ...
981
  
8f96cac6   Jiaming Guo   pushed old changes
982
  	args.parse(argc, argv);					// parse the user arguments
db598823   David Mayerich   fixed GCC errors ...
983
  
8f96cac6   Jiaming Guo   pushed old changes
984
985
986
987
  	if(args["help"].is_set()){				// test for help
  		advertise();						// output the advertisement
  		std::cout<<args.str();				// output arguments
  		exit(1);							// exit
db598823   David Mayerich   fixed GCC errors ...
988
989
  	}
  	
8f96cac6   Jiaming Guo   pushed old changes
990
991
  	if (args.nargs() >= 1) {				// if at least one network file is specified
  		num_nets = 1;						// set the number of networks to one
f001495e   Jiaming Guo   fix minor errors ...
992
  		std::vector<std::string> tmp = stim::parser::split(args.arg(0), '.');	// split the filename at '.'
8f96cac6   Jiaming Guo   pushed old changes
993
994
995
996
  		if ("swc" == tmp[1]) 				// loading swc file
  			GT.load_swc(args.arg(0));		// load the specified file as the ground truth
  		else if ("obj" == tmp[1])			// loading obj file
  			GT.load_obj(args.arg(0));		// load the specified file as the ground truth
f001495e   Jiaming Guo   fix minor errors ...
997
998
999
1000
1001
1002
  		else {
  			std::cout << "Invalid loading file" << std::endl;
  			exit(1);
  		}	
  	}
  
8f96cac6   Jiaming Guo   pushed old changes
1003
1004
1005
1006
  	if (args.nargs() == 2) {								// if two files are specified, they will be displayed in neighboring viewports and compared
  		int device = args["device"].as_int();				// get the device value from the user
  		num_nets = 2;										// set the number of networks to two
  		sigma = args["sigma"].as_float();					// get the sigma value from the user
95850378   Jiaming Guo   add two good test...
1007
  		std::vector<std::string> tmp = stim::parser::split(args.arg(1), '.');	// split the filename at '.'
8f96cac6   Jiaming Guo   pushed old changes
1008
1009
1010
  		if ("swc" == tmp[1]) 								// loading swc files
  			T.load_swc(args.arg(1));                        // load the second (test) network
  		else if ("obj" == tmp[1])							// loading obj files
95850378   Jiaming Guo   add two good test...
1011
1012
1013
1014
1015
  			T.load_obj(args.arg(1));
  		else {
  			std::cout << "Invalid loading file" << std::endl;
  			exit(1);
  		}
8f96cac6   Jiaming Guo   pushed old changes
1016
  		if (args["features"].is_set())						// if the user wants to save features
540379da   Jiaming Guo   rewrite loading f...
1017
  			features(args["features"].as_string());
8f96cac6   Jiaming Guo   pushed old changes
1018
1019
  
  		GT = GT.resample(resample_rate * sigma);			// resample both networks based on the sigma value
540379da   Jiaming Guo   rewrite loading f...
1020
1021
1022
  		T = T.resample(resample_rate * sigma);
  		if (args["mapping"].is_set()) {
  			float threshold = args["mapping"].as_float();
8f96cac6   Jiaming Guo   pushed old changes
1023
1024
1025
1026
1027
  			mapping(sigma, device, threshold);
  		}
  <<<<<<< HEAD
  		else
  			compare(sigma, device);							// run the comparison algorithm
21f03d55   Jiaming Guo   add functions for...
1028
  		}
8f96cac6   Jiaming Guo   pushed old changes
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
  	
  	if (args["stack"].is_set()) {
  		S.load_images(args["stack"].as_string());
  		flag_stack = true;
  	}
  
  	float sp[3] = { 1.0f, 1.0f, 1.0f };						// allocate variables for grid spacing
  	if (args["spacing"].nargs() == 1)						// if only one argument is given
  		sp[2] = (float)args["spacing"].as_float(0);			// assume that it's the z coordinate (most often anisotropic)
  	else if (args["spacing"].nargs() == 3) {				// if three arguments are given
  		sp[0] = (float)args["spacing"].as_float(0);			// set the arguments as expected
  		sp[1] = (float)args["spacing"].as_float(1);
  		sp[2] = (float)args["spacing"].as_float(2);
  	}
  
  	S.spacing(sp[0], sp[1], sp[2]);							// set the spacing between samples
  
  	planes[0] = S.size(0) / 4.0f;							// initialize the start positions for the orthogonal display planes
  	planes[1] = S.size(1) / 4.0f;
  	planes[2] = S.size(2) / 4.0f;
  =======
09cb5950   David Mayerich   fixed some commen...
1050
  		else {
540379da   Jiaming Guo   rewrite loading f...
1051
  			compare(sigma, device);							//run the comparison algorithm
9627c6e6   Jiaming Guo   add splitting and...
1052
  		}
09cb5950   David Mayerich   fixed some commen...
1053
  	}
8f96cac6   Jiaming Guo   pushed old changes
1054
  >>>>>>> 09cb5950f628309ac65c6b85119d3f16e5bcd0a2
db598823   David Mayerich   fixed GCC errors ...
1055
1056
  
  	//if a GUI is requested, display the network using OpenGL
9627c6e6   Jiaming Guo   add splitting and...
1057
  	if(args["gui"].is_set()){
21f03d55   Jiaming Guo   add functions for...
1058
  		if (args["mapping"].is_set()) {
8f96cac6   Jiaming Guo   pushed old changes
1059
1060
1061
1062
  			flag_mapping = true;							// set flag of mapping to true
  			bb = _GT.boundingbox();							// generate a bounding volume		
  			glut_initialize();								// create the GLUT window and set callback functions		
  			glutMainLoop();									// enter GLUT event processing cycle
9627c6e6   Jiaming Guo   add splitting and...
1063
  		}
21f03d55   Jiaming Guo   add functions for...
1064
  		else {
8f96cac6   Jiaming Guo   pushed old changes
1065
1066
1067
  			bb = GT.boundingbox();							// generate a bounding volume		
  			glut_initialize();								// create the GLUT window and set callback functions		
  			glutMainLoop();									// enter GLUT event processing cycle
9627c6e6   Jiaming Guo   add splitting and...
1068
1069
  		}
  	}
09cb5950   David Mayerich   fixed some commen...
1070
  	return 1;
db598823   David Mayerich   fixed GCC errors ...
1071
  }