volmetrics
view src/main.cc @ 21:c22866ecb7ae
frame :p
author | Eleni Maria Stea <elene.mst@gmail.com> |
---|---|
date | Tue, 25 Mar 2014 00:17:39 +0200 |
parents | 21bc62bb3e14 |
children | 4e120dcd55ec |
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);
185 glDisable(GL_DEPTH_TEST);
187 glMatrixMode(GL_MODELVIEW);
188 glPushMatrix();
189 glLoadIdentity();
191 glMatrixMode(GL_PROJECTION);
192 glPushMatrix();
193 glLoadIdentity();
194 glOrtho(-aspect, aspect, -1.0, 1.0, -1.0, 1.0);
196 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
197 glEnable(GL_TEXTURE_3D);
198 glBegin(GL_QUADS);
199 glColor3f(1.0, 1.0, 1.0);
200 glTexCoord3f(0, 0, zeta); glVertex3f(-1.0, 1.0, 0.0);
201 glTexCoord3f(0, 1, zeta); glVertex3f(-1.0, 0.5, 0.0);
202 glTexCoord3f(1, 1, zeta); glVertex3f(-0.5, 0.5, 0.0);
203 glTexCoord3f(1, 0, zeta); glVertex3f(-0.5, 1.0, 0.0);
204 glEnd();
205 glDisable(GL_TEXTURE_3D);
207 glLineWidth(3);
208 glBegin(GL_LINE_LOOP);
209 glColor3f(0.4, 0.8, 0.4);
210 glVertex3f(-1.0, 1.0, 0.0);
211 glVertex3f(-1.0, 0.5, 0.0);
212 glVertex3f(-0.5, 0.5, 0.0);
213 glVertex3f(-0.5, 1.0, 0.0);
214 glEnd();
216 glMatrixMode(GL_PROJECTION);
217 glPopMatrix();
219 glMatrixMode(GL_MODELVIEW);
220 glPopMatrix();
222 glEnable(GL_DEPTH_TEST);
223 glEnable(GL_LIGHTING);
224 }
226 static void display(void)
227 {
228 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
230 glMatrixMode(GL_MODELVIEW);
231 glLoadIdentity();
232 glTranslatef(0, 0, -cam_dist);
233 glRotatef(cam_phi, 1, 0, 0);
234 glRotatef(cam_theta, 0, 1, 0);
236 /*
237 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
238 glEnable(GL_TEXTURE_3D);
239 glBegin(GL_QUADS);
240 glTexCoord3f(0, 0, cur_z); glVertex3f(-1, -1, 0);
241 glTexCoord3f(0, 1, cur_z); glVertex3f(-1, 1, 0);
242 glTexCoord3f(1, 1, cur_z); glVertex3f(1, 1, 0);
243 glTexCoord3f(1, 0, cur_z); glVertex3f(1, -1, 0);
244 glEnd();
245 glDisable(GL_TEXTURE_3D);
246 */
248 if(mesh->is_empty()) {
249 printf("recalculating isosurface ... ");
250 fflush(stdout);
251 vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]);
252 printf("done.\n");
253 }
254 mesh->draw();
256 volume_preview();
257 glutSwapBuffers();
258 assert(glGetError() == GL_NO_ERROR);
259 }
261 static void reshape(int x, int y)
262 {
263 glViewport(0, 0, x, y);
264 glMatrixMode(GL_PROJECTION);
265 glLoadIdentity();
266 gluPerspective(45, (float)x / (float)y, 0.5, 500);
268 if(x != win_xsz || y != win_ysz) {
269 win_xsz = x;
270 win_ysz = y;
271 }
272 }
274 static void keyboard(unsigned char key, int x, int y)
275 {
276 key_state[(int)key] = true;
278 switch(key) {
279 case 27:
280 exit(0);
281 case 'w':
282 {
283 static bool wire;
284 wire = !wire;
285 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
286 }
287 break;
288 default:
289 break;
290 }
291 }
293 static void keyboard_up(unsigned char key, int x, int y)
294 {
295 key_state[(int) key] = false;
296 }
298 static int prev_x, prev_y;
299 static bool bn_state[32];
300 static float prev_thres, prev_thres2;
302 static void mouse(int bn, int state, int x, int y)
303 {
304 prev_x = x;
305 prev_y = y;
307 bn_state[bn - GLUT_LEFT_BUTTON] = (state == GLUT_DOWN);
309 if(state == GLUT_DOWN) {
310 prev_thres = thres;
311 prev_thres2 = thres2;
312 }
313 else {
314 if(thres != prev_thres || thres2 != prev_thres2) {
315 mesh->clear();
316 }
317 }
318 }
320 static void motion(int x, int y)
321 {
322 int dx = x - prev_x;
323 int dy = y - prev_y;
325 if(!dx && !dy)
326 return;
328 prev_x = x;
329 prev_y = y;
331 if(key_state[(int)'t']) {
332 thres = (float)x / (float)win_xsz;
333 printf("threshold: %f\n", thres);
335 glutPostRedisplay();
336 return;
337 }
339 // camera
340 if(bn_state[0]) {
341 if(key_state[(int)'z']) {
342 //zoom the camera
344 cam_dist += dy * 0.1;
346 if(cam_dist < 0)
347 cam_dist = 0;
348 }
349 else {
350 //rotate the camera
352 cam_phi += dy * 0.5;
353 cam_theta += dx * 0.5;
355 if(cam_phi > 90)
356 cam_phi = 90;
357 if(cam_phi < -90)
358 cam_phi = -90;
359 }
361 glutPostRedisplay();
362 }
363 }
365 static bool init_xfer(void)
366 {
367 glutSetWindow(mainwin_id);
368 int x = glutGet(GLUT_WINDOW_X);
369 int y = glutGet(GLUT_WINDOW_Y) + glutGet(GLUT_WINDOW_HEIGHT);
371 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
372 glutInitWindowSize(512, 100);
373 glutInitWindowPosition(x, y);
375 xferwin_id = glutCreateWindow("Transfer Function");
376 glutDisplayFunc(display_xfer);
377 glutReshapeFunc(reshape_xfer);
378 glutMouseFunc(mouse_xfer);
379 glutMotionFunc(motion_xfer);
381 return true;
382 }
384 static void display_xfer(void)
385 {
386 glClear(GL_COLOR_BUFFER_BIT);
388 int num_val = glutGet(GLUT_WINDOW_WIDTH) / 4.0;
389 float x = 0;
390 float dx = 1.0 / num_val;
391 float low, high;
392 if(thres < thres2) {
393 low = thres;
394 high = thres2;
395 }
396 else {
397 low = thres2;
398 high = thres;
399 }
401 //drawing the transfer function curve
403 glLineWidth(3);
404 glBegin(GL_LINE_STRIP);
405 glColor3f(0.8, 0.3, 0.8);
406 for(int i=0; i<num_val; i++) {
407 float val = transfer_function(x, low, high);
408 glVertex2f(x, val);
409 x += dx;
410 }
411 glEnd();
413 //threshold bars
415 glLineWidth(2);
416 glBegin(GL_LINES);
417 glColor3f(0.4, 0.4, 0.8);
418 glVertex2f(low, 0);
419 glVertex2f(low, 1);
420 glColor3f(0.4, 0.8, 0.4);
421 glVertex2f(high, 0);
422 glVertex2f(high, 1);
423 glEnd();
425 /*
426 * gradient
427 */
428 /* glBegin(GL_QUADS);
429 for(int i=0; i<num_val; i++) {
430 float val = transfer_function(x, low, high);
431 glColor3f(val, val, val);
432 glVertex3f(x, 1.0, 0.0);
433 glVertex3f(x, 0.0, 0.0);
435 val = transfer_function(x + dx, low, high);
436 glColor3f(val, val, val);
437 glVertex3f(x + dx, 0.0, 0.0);
438 glVertex3f(x + dx, 1.0, 0.0);
439 x += dx;
440 }
441 glEnd(); */
443 glutSwapBuffers();
444 assert(glGetError() == GL_NO_ERROR);
445 }
447 static void reshape_xfer(int x, int y)
448 {
449 glViewport(0.0, 0.0, x, y);
450 glMatrixMode(GL_PROJECTION);
451 glLoadIdentity();
452 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
453 }
455 static float *select_thres;
456 static float prev_select_thres;
457 static void mouse_xfer(int button, int state, int x, int y)
458 {
459 if(button == GLUT_LEFT_BUTTON) {
460 if(state == GLUT_DOWN) {
461 int width = glutGet(GLUT_WINDOW_WIDTH);
462 float xpos = (float)x / (float)width;
463 if(fabs(xpos - thres) <= fabs(xpos - thres2)) {
464 select_thres = &thres;
465 }
466 else {
467 select_thres = &thres2;
468 }
469 prev_select_thres = *select_thres;
470 }
471 else {
472 if(fabs(*select_thres - prev_select_thres) > 0.001) {
473 mesh->clear();
474 ui->sync_live();
475 }
476 select_thres = 0;
477 }
478 }
479 }
481 static void motion_xfer(int x, int y)
482 {
483 if(!select_thres)
484 return;
486 int width = glutGet(GLUT_WINDOW_WIDTH);
487 float xpos = (float)x / (float)width;
489 *select_thres = xpos;
491 glutPostRedisplay();
492 glutSetWindow(mainwin_id);
493 glutPostRedisplay();
494 }