volmetrics

view src/main.cc @ 33:3e4eb9a0d999

fixed t1, t2 selection with the mouse
author Eleni Maria Stea <elene.mst@gmail.com>
date Sun, 17 Aug 2014 02:25:01 +0300
parents 6d637706cfaf
children eb87d4a12bd3
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 "sdr.h"
13 #include "volume.h"
15 static bool init(void);
16 static GLUI *create_ui(void);
17 static void display(void);
18 static void reshape(int x, int y);
19 static void keyboard(unsigned char key, int x, int y);
20 static void keyboard_up(unsigned char key, int x, int y);
21 static void mouse(int button, int state, int x, int y);
22 static void motion(int x, int y);
23 static bool init_xfer(void);
24 static void display_xfer(void);
25 static void reshape_xfer(int x, int y);
26 static void mouse_xfer(int button, int state, int x, int y);
27 static void motion_xfer(int x, int y);
28 static void volume_preview();
29 static void draw_iso();
30 static void draw_volume();
32 static int mainwin_id, xferwin_id;
33 //todo keyb esc
35 static int win_xsz, win_ysz;
36 static float cam_phi, cam_theta, cam_dist = 6;
37 static std::vector<bool> key_state(256);
39 static const char *vol_fname = "data/test1.vol";
40 static const char *vsdr_path = "data/shaders/transfer.v.glsl";
41 static const char *fsdr_path = "data/shaders/transfer.f.glsl";
42 static const char *vsdr_vol_path = "data/shaders/vol.v.glsl";
43 static const char *fsdr_vol_path = "data/shaders/vol.f.glsl";
45 static unsigned int sprog;
46 static unsigned int sprog_vol;
48 static Volume *vol;
49 static Mesh *mesh;
50 static float cur_z, thres = 0.5, thres2 = 1.0;
51 static float slice_z = 0.5;
53 static int use_orig_vol_res = 1;
54 static int vol_res[3]; // volume sampling resolution x/y/z
55 static float bound_scale = 1.42;
57 static GLUI *ui;
59 static int num_poly = 128;
61 int main(int argc, char **argv)
62 {
63 glutInitWindowSize(512, 512);
64 glutInit(&argc, argv);
65 if(argv[1])
66 vol_fname = argv[1];
68 if(!init()) {
69 fprintf(stderr, "Failed to initialize program.\n");
70 return 1;
71 }
73 init_xfer();
75 glutMainLoop();
76 return 0;
77 }
79 static bool init()
80 {
81 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
83 mainwin_id = glutCreateWindow("CT scan");
84 glutDisplayFunc(display);
85 glutReshapeFunc(reshape);
86 glutKeyboardFunc(keyboard);
87 glutKeyboardUpFunc(keyboard_up);
88 glutMouseFunc(mouse);
89 glutMotionFunc(motion);
91 glewInit();
93 glEnable(GL_DEPTH_TEST);
94 glEnable(GL_NORMALIZE);
96 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
98 //FIXME shaders setup
99 if(!(sprog = sdr_getprog(vsdr_path, fsdr_path)))
100 {
101 fprintf(stderr, "Failed to create shader program!\n");
102 return false;
103 }
105 if(!(sprog_vol = sdr_getprog(vsdr_vol_path, fsdr_vol_path)))
106 {
107 fprintf(stderr, "Failed to create shader program!\n");
108 return false;
109 }
111 vol = new Volume;
112 if(!vol->load(vol_fname)) {
113 fprintf(stderr, "Failed to load %s", vol_fname);
114 return false;
115 }
116 mesh = new Mesh;
117 cur_z = 0.5;
119 vol_res[0] = vol->get_slice(0)->get_width();
120 vol_res[1] = vol->get_slice(0)->get_height();
121 vol_res[2] = vol->get_slice_count();
123 num_poly = std::max(vol_res[0], std::max(vol_res[1], vol_res[2])) * bound_scale;
125 if(!(ui = create_ui())) {
126 return false;
127 }
129 return true;
130 }
132 static GLUI_Spinner *res_spin[3];
133 static void toggle_use_orig(int id)
134 {
135 for(int i=0; i<3; i++) {
136 if(use_orig_vol_res) {
137 res_spin[i]->disable();
138 } else {
139 res_spin[i]->enable();
140 }
141 }
142 }
143 static void thres_change(int id)
144 {
145 static float prev_thres = thres;
146 static float prev_thres2 = thres2;
148 if(prev_thres != thres || prev_thres2 != thres2) {
149 prev_thres = thres;
150 prev_thres2 = thres2;
152 mesh->clear();
154 glutSetWindow(xferwin_id);
155 glutPostRedisplay();
156 glutSetWindow(mainwin_id);
157 glutPostRedisplay();
158 }
159 }
161 static void res_change(int id)
162 {
163 static float prev_resx = vol_res[0];
164 static float prev_resy = vol_res[1];
165 static float prev_resz = vol_res[2];
167 if(prev_resx != vol_res[0] || prev_resy != vol_res[1] || prev_resz != vol_res[2]) {
168 prev_resx = vol_res[0];
169 prev_resy = vol_res[1];
170 prev_resz = vol_res[2];
171 mesh->clear();
172 }
173 }
174 static GLUI *create_ui()
175 {
176 GLUI *ui = GLUI_Master.create_glui("ui");
177 assert(ui);
179 ui->set_main_gfx_window(glutGetWindow());
181 GLUI_Panel *thres_panel = ui->add_panel("iso thresholds");
183 GLUI_Spinner *thres_spin = ui->add_spinner_to_panel(thres_panel, "T1", GLUI_SPINNER_FLOAT, &thres, 0, thres_change);
184 thres_spin->set_float_limits(0, 1);
186 GLUI_Spinner *thres2_spin = ui->add_spinner_to_panel(thres_panel, "T2", GLUI_SPINNER_FLOAT, &thres2, 0, thres_change);
187 thres2_spin->set_float_limits(0, 1);
189 #ifdef ISO
190 GLUI_Panel *res_panel = ui->add_panel("volume resolution");
192 ui->add_checkbox_to_panel(res_panel, "original resolution", &use_orig_vol_res, 0, toggle_use_orig);
194 static const char *res_spin_name[] = {"x", "y", "z"};
195 for(int i=0; i<3; i++) {
196 res_spin[i] = ui->add_spinner_to_panel(res_panel, res_spin_name[i], GLUI_SPINNER_INT, vol_res + i, 0, res_change);
197 res_spin[i]->set_int_limits(8, 256); // TODO limits as arguments or config
198 res_spin[i]->disable();
199 }
200 #endif
201 GLUI_Panel *preview_panel = ui->add_panel("volume preview");
203 GLUI_Spinner *preview_spin = ui->add_spinner_to_panel(preview_panel, "slice z", GLUI_SPINNER_FLOAT, &slice_z, 0);
204 preview_spin->set_float_limits(0, 1);
206 return ui;
207 }
209 static void volume_preview ()
210 {
211 float aspect = win_xsz / win_ysz;
213 glDisable(GL_DEPTH_TEST);
215 glUseProgram(sprog);
216 int tmin_loc = glGetUniformLocation(sprog, "tmin");
217 if(tmin_loc != -1)
218 glUniform1f(tmin_loc, std::min(thres, thres2));
219 int tmax_loc = glGetUniformLocation(sprog, "tmax");
220 if(tmax_loc != -1)
221 glUniform1f(tmax_loc, std::max(thres, thres2));
223 glMatrixMode(GL_MODELVIEW);
224 glPushMatrix();
225 glLoadIdentity();
227 glMatrixMode(GL_PROJECTION);
228 glPushMatrix();
229 glLoadIdentity();
230 glOrtho(0, aspect, 0, 1, -1, 1);
232 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
233 glEnable(GL_TEXTURE_3D);
234 glBegin(GL_QUADS);
235 glColor3f(1.0, 1.0, 1.0);
236 glTexCoord3f(0, 0, slice_z); glVertex3f(0, 1, 0);
237 glTexCoord3f(0, 1, slice_z); glVertex3f(0, 0.75, 0);
238 glTexCoord3f(1, 1, slice_z); glVertex3f(0.25, 0.75, 0);
239 glTexCoord3f(1, 0, slice_z); glVertex3f(0.25, 1, 0);
240 glEnd();
241 glDisable(GL_TEXTURE_3D);
243 glUseProgram(0);
245 glLineWidth(2);
246 glBegin(GL_LINE_LOOP);
247 glColor3f(0.8, 0.3, 0.8);
248 glVertex3f(0, 1, 0);
249 glVertex3f(0, 0.75, 0);
250 glVertex3f(0.25, 0.75, 0);
251 glVertex3f(0.25, 1, 0);
252 glEnd();
254 glMatrixMode(GL_PROJECTION);
255 glPopMatrix();
257 glMatrixMode(GL_MODELVIEW);
258 glPopMatrix();
260 glEnable(GL_DEPTH_TEST);
261 }
263 static void draw_iso()
264 {
265 if(mesh->is_empty()) {
266 printf("recalculating isosurface ... ");
267 fflush(stdout);
268 vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]);
269 printf("done.\n");
270 }
271 mesh->draw();
272 }
274 static void draw_volume()
275 {
276 glUseProgram(sprog_vol);
278 int tmin_loc = glGetUniformLocation(sprog_vol, "tmin");
279 if(tmin_loc != -1)
280 glUniform1f(tmin_loc, std::min(thres, thres2));
281 int tmax_loc = glGetUniformLocation(sprog_vol, "tmax");
282 if(tmax_loc != -1)
283 glUniform1f(tmax_loc, std::max(thres, thres2));
285 int res_loc = glGetUniformLocation(sprog_vol, "res");
286 if(res_loc != -1)
287 glUniform3f(res_loc, (float)vol_res[0], (float)vol_res[1], (float)vol_res[2]);
289 glRotatef(-vol->get_volume_rotation(), 1, 0, 0);
291 glDisable(GL_DEPTH_TEST);
292 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
293 glEnable(GL_TEXTURE_3D);
294 glEnable(GL_BLEND);
295 glBegin(GL_QUADS);
296 for(int i=0; i<num_poly; i++)
297 {
298 float tex_z = (float)i / (float)num_poly;
299 float z = 2 * tex_z - 1;
301 glTexCoord3f(0, 0, tex_z); glVertex3f(-bound_scale, -bound_scale, z * bound_scale);
302 glTexCoord3f(0, 1, tex_z); glVertex3f(-bound_scale, bound_scale, z * bound_scale);
303 glTexCoord3f(1, 1, tex_z); glVertex3f(bound_scale, bound_scale, z * bound_scale);
304 glTexCoord3f(1, 0, tex_z); glVertex3f(bound_scale, -bound_scale, z * bound_scale);
305 }
306 glEnd();
307 glDisable(GL_BLEND);
308 glDisable(GL_TEXTURE_3D);
309 glEnable(GL_DEPTH_TEST);
311 glUseProgram(0);
312 }
314 static void display(void)
315 {
316 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
318 glMatrixMode(GL_MODELVIEW);
319 glLoadIdentity();
320 glTranslatef(0, 0, -cam_dist);
321 glRotatef(cam_phi, 1, 0, 0);
322 glRotatef(cam_theta, 0, 1, 0);
324 //draw_slices();
325 //draw_iso();
326 draw_volume();
328 volume_preview();
329 glutSwapBuffers();
330 assert(glGetError() == GL_NO_ERROR);
331 }
333 static void reshape(int x, int y)
334 {
335 glViewport(0, 0, x, y);
336 glMatrixMode(GL_PROJECTION);
337 glLoadIdentity();
338 gluPerspective(45, (float)x / (float)y, 0.5, 500);
340 win_xsz = x;
341 win_ysz = y;
342 }
344 static void keyboard(unsigned char key, int x, int y)
345 {
346 key_state[(int)key] = true;
348 switch(key) {
349 case 27:
350 exit(0);
351 case 'w':
352 {
353 static bool wire;
354 wire = !wire;
355 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
356 }
357 break;
358 default:
359 break;
360 }
361 }
363 static void keyboard_up(unsigned char key, int x, int y)
364 {
365 key_state[(int) key] = false;
366 }
368 static int prev_x, prev_y;
369 static bool bn_state[32];
370 static float prev_thres, prev_thres2;
372 static void mouse(int bn, int state, int x, int y)
373 {
374 prev_x = x;
375 prev_y = y;
377 bn_state[bn - GLUT_LEFT_BUTTON] = (state == GLUT_DOWN);
379 if(state == GLUT_DOWN) {
380 prev_thres = thres;
381 prev_thres2 = thres2;
382 }
383 else {
384 if(thres != prev_thres || thres2 != prev_thres2) {
385 mesh->clear();
386 }
387 }
388 }
390 static void motion(int x, int y)
391 {
392 int dx = x - prev_x;
393 int dy = y - prev_y;
395 if(!dx && !dy)
396 return;
398 prev_x = x;
399 prev_y = y;
401 if(key_state[(int)'t']) {
402 thres = (float)x / (float)win_xsz;
403 printf("threshold: %f\n", thres);
405 glutPostRedisplay();
406 return;
407 }
409 // camera
410 if(bn_state[0]) {
411 if(key_state[(int)'z']) {
412 //zoom the camera
414 cam_dist += dy * 0.1;
416 if(cam_dist < 0)
417 cam_dist = 0;
418 }
419 else {
420 //rotate the camera
422 cam_phi += dy * 0.5;
423 cam_theta += dx * 0.5;
425 if(cam_phi > 90)
426 cam_phi = 90;
427 if(cam_phi < -90)
428 cam_phi = -90;
429 }
431 glutPostRedisplay();
432 }
433 }
435 static bool init_xfer(void)
436 {
437 glutSetWindow(mainwin_id);
438 int x = glutGet(GLUT_WINDOW_X);
439 int y = glutGet(GLUT_WINDOW_Y) + glutGet(GLUT_WINDOW_HEIGHT);
441 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
442 glutInitWindowSize(512, 100);
443 glutInitWindowPosition(x, y);
445 xferwin_id = glutCreateWindow("Transfer Function");
446 glutDisplayFunc(display_xfer);
447 glutReshapeFunc(reshape_xfer);
448 glutMouseFunc(mouse_xfer);
449 glutMotionFunc(motion_xfer);
451 return true;
452 }
454 static void display_xfer(void)
455 {
456 glClear(GL_COLOR_BUFFER_BIT);
458 int num_val = glutGet(GLUT_WINDOW_WIDTH) / 4.0;
459 float x = 0;
460 float dx = 1.0 / num_val;
461 float low, high;
462 if(thres < thres2) {
463 low = thres;
464 high = thres2;
465 }
466 else {
467 low = thres2;
468 high = thres;
469 }
471 //drawing the transfer function curve
473 glLineWidth(3);
474 glBegin(GL_LINE_STRIP);
475 glColor3f(0.8, 0.3, 0.8);
476 for(int i=0; i<num_val; i++) {
477 float val = transfer_function(x, low, high);
478 glVertex2f(x, val);
479 x += dx;
480 }
481 glEnd();
483 //threshold bars
485 glLineWidth(2);
486 glBegin(GL_LINES);
487 glColor3f(0.4, 0.4, 0.8);
488 glVertex2f(low, 0);
489 glVertex2f(low, 1);
490 glColor3f(0.4, 0.8, 0.4);
491 glVertex2f(high, 0);
492 glVertex2f(high, 1);
493 glEnd();
495 /*
496 * gradient
497 */
498 /* glBegin(GL_QUADS);
499 for(int i=0; i<num_val; i++) {
500 float val = transfer_function(x, low, high);
501 glColor3f(val, val, val);
502 glVertex3f(x, 1.0, 0.0);
503 glVertex3f(x, 0.0, 0.0);
505 val = transfer_function(x + dx, low, high);
506 glColor3f(val, val, val);
507 glVertex3f(x + dx, 0.0, 0.0);
508 glVertex3f(x + dx, 1.0, 0.0);
509 x += dx;
510 }
511 glEnd(); */
513 glutSwapBuffers();
514 assert(glGetError() == GL_NO_ERROR);
515 }
517 static void reshape_xfer(int x, int y)
518 {
519 glViewport(0.0, 0.0, x, y);
520 glMatrixMode(GL_PROJECTION);
521 glLoadIdentity();
522 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
523 }
525 static float *select_thres;
526 static float prev_select_thres;
527 static void mouse_xfer(int button, int state, int x, int y)
528 {
529 if(button == GLUT_LEFT_BUTTON) {
530 if(state == GLUT_DOWN) {
531 int width = glutGet(GLUT_WINDOW_WIDTH);
532 float xpos = (float)x / (float)width;
533 if(fabs(xpos - thres) <= fabs(xpos - thres2) && fabs(xpos - thres) < 0.1) {
534 select_thres = &thres;
535 }
536 else if (fabs(xpos - thres2) < 0.1) {
537 select_thres = &thres2;
538 }
539 else
540 select_thres = 0;
542 if (select_thres)
543 prev_select_thres = *select_thres;
544 }
545 else {
546 if(!prev_select_thres || !select_thres) {
547 select_thres = 0;
548 return;
549 }
550 if(fabs(*select_thres - prev_select_thres) > 0.001) {
551 mesh->clear();
552 ui->sync_live();
553 }
554 select_thres = 0;
555 }
556 }
557 }
559 static void motion_xfer(int x, int y)
560 {
561 if(!select_thres)
562 return;
564 int width = glutGet(GLUT_WINDOW_WIDTH);
565 float xpos = (float)x / (float)width;
567 *select_thres = xpos;
568 thres_change(0);
570 glutPostRedisplay();
571 glutSetWindow(mainwin_id);
572 glutPostRedisplay();
573 }