Blame view

main.cpp 10.8 KB
44700501   pranathivemuri   program to genera...
1
2
3
4
  #include <stdlib.h>
  #include <string>
  #include <fstream>
  #include <algorithm>
44700501   pranathivemuri   program to genera...
5
  
627eed8d   David Mayerich   added a GUI for d...
6
7
8
9
10
  //OpenGL includes
  #include <GL/glut.h>
  
  //STIM includes
  #include <stim/visualization/gl_network.h>
4f517c76   David Mayerich   added color-mappe...
11
  #include <stim/visualization/gl_aaboundingbox.h>
627eed8d   David Mayerich   added a GUI for d...
12
13
14
15
  #include <stim/parser/arguments.h>
  #include <stim/visualization/camera.h>
  
  //ANN includes
44700501   pranathivemuri   program to genera...
16
  #include <ANN/ANN.h>
627eed8d   David Mayerich   added a GUI for d...
17
18
  
  //BOOST includes
44700501   pranathivemuri   program to genera...
19
  #include <boost/tuple/tuple.hpp>
03c5492b   pranathivemuri   networks and kd t...
20
  
4f517c76   David Mayerich   added color-mappe...
21
22
23
  //visualization objects
  stim::gl_aaboundingbox<float> bb;			//axis-aligned bounding box object
  stim::camera cam;					//camera object
627eed8d   David Mayerich   added a GUI for d...
24
  
4f517c76   David Mayerich   added color-mappe...
25
26
27
  unsigned num_nets = 0;
  stim::gl_network<float> GT;			//ground truth network
  stim::gl_network<float> T;			//test network
627eed8d   David Mayerich   added a GUI for d...
28
29
  
  //hard-coded parameters
4f517c76   David Mayerich   added color-mappe...
30
  float resample_rate = 0.5;			//sample rate for the network (fraction of sigma used as the maximum sample rate)
eb135b3c   David Mayerich   fixed compatibili...
31
32
  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
627eed8d   David Mayerich   added a GUI for d...
33
34
35
36
37
  
  //mouse position tracking
  int mouse_x;
  int mouse_y;
  
4f517c76   David Mayerich   added color-mappe...
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
  //OpenGL objects
  GLuint cmap_tex = 0;				//texture name for the color map
  
  //sets an OpenGL viewport taking up the entire window
  void glut_render_single_projection(){
  
  	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
  	int Y = glutGet(GLUT_WINDOW_HEIGHT);
  	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
  }
  
  //sets an OpenGL viewport taking up the left half of the window
  void glut_render_left_projection(){
  
  	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
  	int Y = glutGet(GLUT_WINDOW_HEIGHT);
  	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
  }
  
  //sets an OpenGL viewport taking up the right half of the window
  void glut_render_right_projection(){
  
  	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
  	int Y = glutGet(GLUT_WINDOW_HEIGHT);
  	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
  }
  
  void glut_render_modelview(){
  
  	glMatrixMode(GL_MODELVIEW);						//load the modelview matrix for editing
  	glLoadIdentity();								//start with the identity matrix
eb135b3c   David Mayerich   fixed compatibili...
81
82
83
  	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
4f517c76   David Mayerich   added color-mappe...
84
85
86
87
88
  
  	gluLookAt(eye[0], eye[1], eye[2], focus[0], focus[1], focus[2], up[0], up[1], up[2]);	//set up the OpenGL camera
  }
  
  //draws the network(s)
627eed8d   David Mayerich   added a GUI for d...
89
90
  void glut_render(void) {
  
4f517c76   David Mayerich   added color-mappe...
91
92
93
94
95
96
  	if(num_nets == 1){											//if a single network is loaded
  		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
  		GT.glCenterline(GT.nmags() - 1);						//render the GT network (the only one loaded)
  	}
627eed8d   David Mayerich   added a GUI for d...
97
  
4f517c76   David Mayerich   added color-mappe...
98
  	if(num_nets == 2){											//if two networks are loaded	
627eed8d   David Mayerich   added a GUI for d...
99
  
4f517c76   David Mayerich   added color-mappe...
100
101
102
  		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
627eed8d   David Mayerich   added a GUI for d...
103
  
4f517c76   David Mayerich   added color-mappe...
104
105
106
  		glEnable(GL_TEXTURE_1D);										//enable texture mapping
  		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);		//texture map will be used as the network color
  		glBindTexture(GL_TEXTURE_1D, cmap_tex);							//bind the Brewer texture map
627eed8d   David Mayerich   added a GUI for d...
107
  
4f517c76   David Mayerich   added color-mappe...
108
  		GT.glCenterline(GT.nmags() - 1);						//render the GT network
627eed8d   David Mayerich   added a GUI for d...
109
  
4f517c76   David Mayerich   added color-mappe...
110
111
112
  		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.glCenterline(T.nmags() - 1);							//render the T network
627eed8d   David Mayerich   added a GUI for d...
113
  
4f517c76   David Mayerich   added color-mappe...
114
  	}
627eed8d   David Mayerich   added a GUI for d...
115
116
117
118
119
120
  
  	glutSwapBuffers();
  }
  
  // defines camera motion based on mouse dragging
  void glut_motion(int x, int y){
4f517c76   David Mayerich   added color-mappe...
121
  	
627eed8d   David Mayerich   added a GUI for d...
122
  
4f517c76   David Mayerich   added color-mappe...
123
124
  	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
627eed8d   David Mayerich   added a GUI for d...
125
  
4f517c76   David Mayerich   added color-mappe...
126
  	cam.OrbitFocus(theta, phi);						//rotate the camera around the focal point
627eed8d   David Mayerich   added a GUI for d...
127
  
4f517c76   David Mayerich   added color-mappe...
128
  	mouse_x = x;									//update the mouse position
627eed8d   David Mayerich   added a GUI for d...
129
  	mouse_y = y;
4f517c76   David Mayerich   added color-mappe...
130
131
  		
  	glutPostRedisplay();							//re-draw the visualization
627eed8d   David Mayerich   added a GUI for d...
132
133
134
135
136
137
138
139
  }
  
  // sets the mouse position when clicked
  void glut_mouse(int button, int state, int x, int y){
  	mouse_x = x;
  	mouse_y = y;
  }
  
4f517c76   David Mayerich   added color-mappe...
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
  #define BREWER_CTRL_PTS 11							//number of control points in the Brewer map
  void texture_initialize(){
  
  	//define the colormap
  	static float  brewer_map[BREWER_CTRL_PTS][3] = {			//generate a Brewer color map (blue to red)
  		{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}
  	};
  
  	glGenTextures(1, &cmap_tex);								//generate a texture map name
  	glBindTexture(GL_TEXTURE_1D, cmap_tex);						//bind the texture map
  
  	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);		//enable linear interpolation
  	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  	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
  					brewer_map);
  }
  
  //Initialize the OpenGL (GLUT) window, including starting resolution, callbacks, texture maps, and camera
  void glut_initialize(){
  	
  	int myargc = 1;					//GLUT requires arguments, so create some bogus ones
  	char* myargv[1];
eb135b3c   David Mayerich   fixed compatibili...
173
174
  	myargv[0] = (char*)malloc(1);
  	myargv[0][0] = 'h';
4f517c76   David Mayerich   added color-mappe...
175
176
177
178
179
180
  
  	glutInit(&myargc, myargv);									//pass bogus arguments to glutInit()
  	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
627eed8d   David Mayerich   added a GUI for d...
181
  
4f517c76   David Mayerich   added color-mappe...
182
183
184
185
186
187
188
189
  	
  	// register callback functions
  	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
  
  	texture_initialize();					//set up texture mapping (create texture maps, enable features)
  
eb135b3c   David Mayerich   fixed compatibili...
190
  	stim::vec3<float> c = bb.center();		//get the center of the network bounding box
627eed8d   David Mayerich   added a GUI for d...
191
  
4f517c76   David Mayerich   added color-mappe...
192
  	//place the camera along the z-axis at a distance determined by the network size along x and y
eb135b3c   David Mayerich   fixed compatibili...
193
  	cam.setPosition(c + stim::vec3<float>(0, 0, camera_factor * std::max(bb.size()[0], bb.size()[1])));
4f517c76   David Mayerich   added color-mappe...
194
  	cam.LookAt(c[0], c[1], c[2]);						//look at the center of the network
627eed8d   David Mayerich   added a GUI for d...
195
196
  }
  
4f517c76   David Mayerich   added color-mappe...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  //compare both networks and fill the networks with error information
  void compare(float sigma){
  
  	GT = GT.compare(T, sigma);						//compare the ground truth to the test case - store errors in GT
      T = T.compare(GT, sigma);						//compare the test case to the ground truth - store errors in T
  
  	//calculate the metrics
  	float FPR = GT.average(1);						//calculate the metrics
  	float FNR = T.average(1);
  	
  	std::cout << "FNR: " << FPR << std::endl;		//print false alarms and misses
  	std::cout << "FPR: " << FNR << std::endl;
  }
  
  // Output an advertisement for the lab, authors, and usage information
627eed8d   David Mayerich   added a GUI for d...
212
  void advertise(){
627eed8d   David Mayerich   added a GUI for d...
213
214
215
216
217
218
219
220
221
222
223
224
  	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;
  	std::cout<<"Developers: Pranathi Vemuri, David Mayerich"<<std::endl;
  	std::cout<<"Source: https://git.stim.ee.uh.edu/segmentation/netmets"<<std::endl;
  	std::cout<<"========================================================================="<<std::endl<<std::endl;
  
  	std::cout<<"usage: netmets file1 file2 --sigma 10"<<std::endl;
  	std::cout<<"            compare two files with a tolerance of 10 (units defined by the network)"<<std::endl;
  	std::cout<<"       netmets file1 --gui"<<std::endl<<std::endl;
  	std::cout<<"            load a file and display it using OpenGL"<<std::endl;
627eed8d   David Mayerich   added a GUI for d...
225
  }
44700501   pranathivemuri   program to genera...
226
227
228
  
  int main(int argc, char* argv[])
  {
4f517c76   David Mayerich   added color-mappe...
229
  	stim::arglist args;						//create an instance of arglist
627eed8d   David Mayerich   added a GUI for d...
230
231
232
233
234
235
  
  	//add arguments
  	args.add("help", "prints this help");
  	args.add("sigma", "force a sigma value to specify the tolerance of the network comparison", "10");
  	args.add("gui", "display the network or network comparison using OpenGL");
  
4f517c76   David Mayerich   added color-mappe...
236
  	args.parse(argc, argv);					//parse the user arguments
627eed8d   David Mayerich   added a GUI for d...
237
  
4f517c76   David Mayerich   added color-mappe...
238
239
240
241
  	if(args["help"].is_set() || args.nargs() == 0){			//test for help
  		advertise();										//output the advertisement
  		std::cout<<args.str();								//output arguments
  		exit(1);											//exit
a0048e31   David Mayerich   added several tes...
242
  	}
4f517c76   David Mayerich   added color-mappe...
243
244
245
246
  	
  	if(args.nargs() >= 1){					//if at least one network file is specified
  		num_nets = 1;						//set the number of networks to one
  		GT.load_obj(args.arg(0));			//load the specified file as the ground truth
627eed8d   David Mayerich   added a GUI for d...
247
  	}
4f517c76   David Mayerich   added color-mappe...
248
249
250
  	
  	if(args.nargs() == 2){			//if two files are specified, they will be displayed in neighboring viewports and compared
  		num_nets = 2;										//set the number of networks to two
eb135b3c   David Mayerich   fixed compatibili...
251
  		float sigma = (float)args["sigma"].as_float();				//get the sigma value from the user
627eed8d   David Mayerich   added a GUI for d...
252
  		T.load_obj(args.arg(1));							//load the second (test) network
51c0cf6e   David Mayerich   cleaned up the me...
253
  
627eed8d   David Mayerich   added a GUI for d...
254
255
  		GT = GT.resample(resample_rate * sigma);			//resample both networks based on the sigma value
  		T = T.resample(resample_rate * sigma);
51c0cf6e   David Mayerich   cleaned up the me...
256
  
627eed8d   David Mayerich   added a GUI for d...
257
258
  		compare(sigma);										//run the comparison algorithm
  	}
51c0cf6e   David Mayerich   cleaned up the me...
259
  
627eed8d   David Mayerich   added a GUI for d...
260
  	//if a GUI is requested, display the network using OpenGL
4f517c76   David Mayerich   added color-mappe...
261
262
263
264
265
  	if(args["gui"].is_set()){		
  		bb = GT.boundingbox();					//generate a bounding volume		
  		glut_initialize();						//create the GLUT window and set callback functions		
  		glutMainLoop();							// enter GLUT event processing cycle
  	}	
ed5edb3e   pranathivemuri   added function to...
266
  }