volmetrics

view src/main.cc @ 20:21bc62bb3e14

added spinner
author Eleni Maria Stea <elene.mst@gmail.com>
date Tue, 25 Mar 2014 00:07:12 +0200
parents 4e02e18e70ef
children c22866ecb7ae
line source
1 #include <GL/glew.h>
2 #include <GL/glut.h>
3 #include <GL/glui.h>
5 #include <math.h>
6 #include <stdio.h>
7 #include <assert.h>
9 #include <vector>
11 #include "mesh.h"
12 #include "volume.h"
14 static bool init(void);
15 static GLUI *create_ui(void);
16 static void display(void);
17 static void reshape(int x, int y);
18 static void keyboard(unsigned char key, int x, int y);
19 static void keyboard_up(unsigned char key, int x, int y);
20 static void mouse(int button, int state, int x, int y);
21 static void motion(int x, int y);
22 static bool init_xfer(void);
23 static void display_xfer(void);
24 static void reshape_xfer(int x, int y);
25 static void mouse_xfer(int button, int state, int x, int y);
26 static void motion_xfer(int x, int y);
27 static void volume_preview ();
29 static int mainwin_id, xferwin_id;
30 //todo keyb esc
32 static int win_xsz, win_ysz;
33 static float cam_phi, cam_theta, cam_dist = 6;
34 static std::vector<bool> key_state(256);
36 static const char *vol_fname = "data/test1.vol";
38 static Volume *vol;
39 static Mesh *mesh;
40 static float cur_z, thres = 0.5, thres2 = 1.0;
41 static float zeta;
43 static int use_orig_vol_res = 1;
44 static int vol_res[3]; // volume sampling resolution x/y/z
46 static GLUI *ui;
48 int main(int argc, char **argv)
49 {
50 glutInit(&argc, argv);
51 if(argv[1])
52 vol_fname = argv[1];
54 if(!init()) {
55 fprintf(stderr, "Failed to initialize program.\n");
56 return 1;
57 }
59 init_xfer();
61 glutMainLoop();
62 return 0;
63 }
65 static bool init()
66 {
67 glutInitWindowSize(512, 512);
68 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
70 mainwin_id = glutCreateWindow("CT scan");
71 glutDisplayFunc(display);
72 glutReshapeFunc(reshape);
73 glutKeyboardFunc(keyboard);
74 glutKeyboardUpFunc(keyboard_up);
75 glutMouseFunc(mouse);
76 glutMotionFunc(motion);
78 glewInit();
80 glEnable(GL_DEPTH_TEST);
81 glEnable(GL_NORMALIZE);
83 glEnable(GL_LIGHTING); //TODO: shaders
84 glEnable(GL_LIGHT0);
85 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
87 vol = new Volume;
88 if(!vol->load(vol_fname)) {
89 fprintf(stderr, "Failed to load %s", vol_fname);
90 return false;
91 }
92 mesh = new Mesh;
93 cur_z = 0.5;
95 vol_res[0] = vol->get_slice(0)->get_width();
96 vol_res[1] = vol->get_slice(0)->get_height();
97 vol_res[2] = vol->get_slice_count();
99 if(!(ui = create_ui())) {
100 return false;
101 }
103 return true;
104 }
106 static GLUI_Spinner *res_spin[3];
107 static void toggle_use_orig(int id)
108 {
109 for(int i=0; i<3; i++) {
110 if(use_orig_vol_res) {
111 res_spin[i]->disable();
112 } else {
113 res_spin[i]->enable();
114 }
115 }
116 }
117 static void thres_change(int id)
118 {
119 static float prev_thres = thres;
120 static float prev_thres2 = thres2;
122 if(prev_thres != thres || prev_thres2 != thres2) {
123 prev_thres = thres;
124 prev_thres2 = thres2;
125 mesh->clear();
126 }
128 glutSetWindow(xferwin_id);
129 glutPostRedisplay();
130 glutSetWindow(mainwin_id);
131 glutPostRedisplay();
132 }
133 static void res_change(int id)
134 {
135 static float prev_resx = vol_res[0];
136 static float prev_resy = vol_res[1];
137 static float prev_resz = vol_res[2];
139 if(prev_resx != vol_res[0] || prev_resy != vol_res[1] || prev_resz != vol_res[2]) {
140 prev_resx = vol_res[0];
141 prev_resy = vol_res[1];
142 prev_resz = vol_res[2];
143 mesh->clear();
144 }
145 }
146 static GLUI *create_ui()
147 {
148 GLUI *ui = GLUI_Master.create_glui("ui");
149 assert(ui);
151 ui->set_main_gfx_window(glutGetWindow());
153 GLUI_Panel *thres_panel = ui->add_panel("iso thresholds");
155 GLUI_Spinner *thres_spin = ui->add_spinner_to_panel(thres_panel, "T1", GLUI_SPINNER_FLOAT, &thres, 0, thres_change);
156 thres_spin->set_float_limits(0, 1);
158 GLUI_Spinner *thres2_spin = ui->add_spinner_to_panel(thres_panel, "T2", GLUI_SPINNER_FLOAT, &thres2, 0, thres_change);
159 thres2_spin->set_float_limits(0, 1);
161 GLUI_Panel *res_panel = ui->add_panel("volume resolution");
163 ui->add_checkbox_to_panel(res_panel, "original resolution", &use_orig_vol_res, 0, toggle_use_orig);
165 static const char *res_spin_name[] = {"x", "y", "z"};
166 for(int i=0; i<3; i++) {
167 res_spin[i] = ui->add_spinner_to_panel(res_panel, res_spin_name[i], GLUI_SPINNER_INT, vol_res + i, 0, res_change);
168 res_spin[i]->set_int_limits(8, 256); // TODO limits as arguments or config
169 res_spin[i]->disable();
170 }
172 GLUI_Panel *preview_panel = ui->add_panel("volume preview");
174 GLUI_Spinner *preview_spin = ui->add_spinner_to_panel(preview_panel, "slice z", GLUI_SPINNER_FLOAT, &zeta, 0);
175 preview_spin->set_float_limits(0, 1);
177 return ui;
178 }
180 static void volume_preview ()
181 {
182 float aspect = win_xsz / win_ysz;
184 glDisable(GL_LIGHTING);
186 glMatrixMode(GL_MODELVIEW);
187 glPushMatrix();
188 glLoadIdentity();
190 glMatrixMode(GL_PROJECTION);
191 glPushMatrix();
192 glLoadIdentity();
193 glOrtho(-aspect, aspect, -1.0, 1.0, -1.0, 1.0);
195 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
196 glEnable(GL_TEXTURE_3D);
197 glBegin(GL_QUADS);
198 glColor3f(1.0, 0.0, 0.0);
199 glTexCoord3f(0, 0, zeta); glVertex3f(-1.0, 1.0, 0.0);
200 glTexCoord3f(0, 1, zeta); glVertex3f(-1.0, 0.5, 0.0);
201 glTexCoord3f(1, 1, zeta); glVertex3f(-0.5, 0.5, 0.0);
202 glTexCoord3f(1, 0, zeta); glVertex3f(-0.5, 1.0, 0.0);
203 glEnd();
204 glDisable(GL_TEXTURE_3D);
206 glMatrixMode(GL_PROJECTION);
207 glPopMatrix();
209 glMatrixMode(GL_MODELVIEW);
210 glPopMatrix();
212 glEnable(GL_LIGHTING);
213 }
215 static void display(void)
216 {
217 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
219 glMatrixMode(GL_MODELVIEW);
220 glLoadIdentity();
221 glTranslatef(0, 0, -cam_dist);
222 glRotatef(cam_phi, 1, 0, 0);
223 glRotatef(cam_theta, 0, 1, 0);
225 /*
226 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
227 glEnable(GL_TEXTURE_3D);
228 glBegin(GL_QUADS);
229 glTexCoord3f(0, 0, cur_z); glVertex3f(-1, -1, 0);
230 glTexCoord3f(0, 1, cur_z); glVertex3f(-1, 1, 0);
231 glTexCoord3f(1, 1, cur_z); glVertex3f(1, 1, 0);
232 glTexCoord3f(1, 0, cur_z); glVertex3f(1, -1, 0);
233 glEnd();
234 glDisable(GL_TEXTURE_3D);
235 */
237 if(mesh->is_empty()) {
238 printf("recalculating isosurface ... ");
239 fflush(stdout);
240 vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]);
241 printf("done.\n");
242 }
243 mesh->draw();
245 volume_preview();
246 glutSwapBuffers();
247 assert(glGetError() == GL_NO_ERROR);
248 }
250 static void reshape(int x, int y)
251 {
252 glViewport(0, 0, x, y);
253 glMatrixMode(GL_PROJECTION);
254 glLoadIdentity();
255 gluPerspective(45, (float)x / (float)y, 0.5, 500);
257 if(x != win_xsz || y != win_ysz) {
258 win_xsz = x;
259 win_ysz = y;
260 }
261 }
263 static void keyboard(unsigned char key, int x, int y)
264 {
265 key_state[(int)key] = true;
267 switch(key) {
268 case 27:
269 exit(0);
270 case 'w':
271 {
272 static bool wire;
273 wire = !wire;
274 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
275 }
276 break;
277 default:
278 break;
279 }
280 }
282 static void keyboard_up(unsigned char key, int x, int y)
283 {
284 key_state[(int) key] = false;
285 }
287 static int prev_x, prev_y;
288 static bool bn_state[32];
289 static float prev_thres, prev_thres2;
291 static void mouse(int bn, int state, int x, int y)
292 {
293 prev_x = x;
294 prev_y = y;
296 bn_state[bn - GLUT_LEFT_BUTTON] = (state == GLUT_DOWN);
298 if(state == GLUT_DOWN) {
299 prev_thres = thres;
300 prev_thres2 = thres2;
301 }
302 else {
303 if(thres != prev_thres || thres2 != prev_thres2) {
304 mesh->clear();
305 }
306 }
307 }
309 static void motion(int x, int y)
310 {
311 int dx = x - prev_x;
312 int dy = y - prev_y;
314 if(!dx && !dy)
315 return;
317 prev_x = x;
318 prev_y = y;
320 if(key_state[(int)'t']) {
321 thres = (float)x / (float)win_xsz;
322 printf("threshold: %f\n", thres);
324 glutPostRedisplay();
325 return;
326 }
328 // camera
329 if(bn_state[0]) {
330 if(key_state[(int)'z']) {
331 //zoom the camera
333 cam_dist += dy * 0.1;
335 if(cam_dist < 0)
336 cam_dist = 0;
337 }
338 else {
339 //rotate the camera
341 cam_phi += dy * 0.5;
342 cam_theta += dx * 0.5;
344 if(cam_phi > 90)
345 cam_phi = 90;
346 if(cam_phi < -90)
347 cam_phi = -90;
348 }
350 glutPostRedisplay();
351 }
352 }
354 static bool init_xfer(void)
355 {
356 glutSetWindow(mainwin_id);
357 int x = glutGet(GLUT_WINDOW_X);
358 int y = glutGet(GLUT_WINDOW_Y) + glutGet(GLUT_WINDOW_HEIGHT);
360 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
361 glutInitWindowSize(512, 100);
362 glutInitWindowPosition(x, y);
364 xferwin_id = glutCreateWindow("Transfer Function");
365 glutDisplayFunc(display_xfer);
366 glutReshapeFunc(reshape_xfer);
367 glutMouseFunc(mouse_xfer);
368 glutMotionFunc(motion_xfer);
370 return true;
371 }
373 static void display_xfer(void)
374 {
375 glClear(GL_COLOR_BUFFER_BIT);
377 int num_val = glutGet(GLUT_WINDOW_WIDTH) / 4.0;
378 float x = 0;
379 float dx = 1.0 / num_val;
380 float low, high;
381 if(thres < thres2) {
382 low = thres;
383 high = thres2;
384 }
385 else {
386 low = thres2;
387 high = thres;
388 }
390 //drawing the transfer function curve
392 glLineWidth(3);
393 glBegin(GL_LINE_STRIP);
394 glColor3f(0.8, 0.3, 0.8);
395 for(int i=0; i<num_val; i++) {
396 float val = transfer_function(x, low, high);
397 glVertex2f(x, val);
398 x += dx;
399 }
400 glEnd();
402 //threshold bars
404 glLineWidth(2);
405 glBegin(GL_LINES);
406 glColor3f(0.4, 0.4, 0.8);
407 glVertex2f(low, 0);
408 glVertex2f(low, 1);
409 glColor3f(0.4, 0.8, 0.4);
410 glVertex2f(high, 0);
411 glVertex2f(high, 1);
412 glEnd();
414 /*
415 * gradient
416 */
417 /* glBegin(GL_QUADS);
418 for(int i=0; i<num_val; i++) {
419 float val = transfer_function(x, low, high);
420 glColor3f(val, val, val);
421 glVertex3f(x, 1.0, 0.0);
422 glVertex3f(x, 0.0, 0.0);
424 val = transfer_function(x + dx, low, high);
425 glColor3f(val, val, val);
426 glVertex3f(x + dx, 0.0, 0.0);
427 glVertex3f(x + dx, 1.0, 0.0);
428 x += dx;
429 }
430 glEnd(); */
432 glutSwapBuffers();
433 assert(glGetError() == GL_NO_ERROR);
434 }
436 static void reshape_xfer(int x, int y)
437 {
438 glViewport(0.0, 0.0, x, y);
439 glMatrixMode(GL_PROJECTION);
440 glLoadIdentity();
441 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
442 }
444 static float *select_thres;
445 static float prev_select_thres;
446 static void mouse_xfer(int button, int state, int x, int y)
447 {
448 if(button == GLUT_LEFT_BUTTON) {
449 if(state == GLUT_DOWN) {
450 int width = glutGet(GLUT_WINDOW_WIDTH);
451 float xpos = (float)x / (float)width;
452 if(fabs(xpos - thres) <= fabs(xpos - thres2)) {
453 select_thres = &thres;
454 }
455 else {
456 select_thres = &thres2;
457 }
458 prev_select_thres = *select_thres;
459 }
460 else {
461 if(fabs(*select_thres - prev_select_thres) > 0.001) {
462 mesh->clear();
463 ui->sync_live();
464 }
465 select_thres = 0;
466 }
467 }
468 }
470 static void motion_xfer(int x, int y)
471 {
472 if(!select_thres)
473 return;
475 int width = glutGet(GLUT_WINDOW_WIDTH);
476 float xpos = (float)x / (float)width;
478 *select_thres = xpos;
480 glutPostRedisplay();
481 glutSetWindow(mainwin_id);
482 glutPostRedisplay();
483 }