volmetrics

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