volmetrics

view src/main.cc @ 16:add30e2d5253

foo
author Eleni Maria Stea <elene.mst@gmail.com>
date Mon, 03 Feb 2014 02:52:46 +0200
parents a93c8aa85e05
children 0f4fff558737
line source
1 #include <GL/glew.h>
2 #include <GL/glut.h>
3 #include <GL/glui.h>
5 #include <stdio.h>
6 #include <assert.h>
8 #include <vector>
10 #include "mesh.h"
11 #include "volume.h"
13 static bool init(void);
14 static GLUI *create_ui(void);
15 static void display(void);
16 static void reshape(int x, int y);
17 static void keyboard(unsigned char key, int x, int y);
18 static void keyboard_up(unsigned char key, int x, int y);
19 static void mouse(int button, int state, int x, int y);
20 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 motion_xfer(int x, int y);
27 static int mainwin_id, xferwin_id;
29 static int win_xsz, win_ysz;
30 static float cam_phi, cam_theta, cam_dist = 6;
31 static std::vector<bool> key_state(256);
33 static const char *vol_fname = "data/test1.vol";
35 static Volume *vol;
36 static Mesh *mesh;
37 static float cur_z, thres = 0.5, thres2 = 1.0;
39 static int use_orig_vol_res = 1;
40 static int vol_res[3]; // volume sampling resolution x/y/z
42 static GLUI *ui;
44 int main(int argc, char **argv)
45 {
46 glutInit(&argc, argv);
47 if(argv[1])
48 vol_fname = argv[1];
50 if(!init()) {
51 fprintf(stderr, "Failed to initialize program.\n");
52 return 1;
53 }
55 init_xfer();
57 glutMainLoop();
58 return 0;
59 }
61 static bool init()
62 {
63 glutInitWindowSize(512, 512);
64 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
66 mainwin_id = glutCreateWindow("CT scan");
67 glutDisplayFunc(display);
68 glutReshapeFunc(reshape);
69 glutKeyboardFunc(keyboard);
70 glutKeyboardUpFunc(keyboard_up);
71 glutMouseFunc(mouse);
72 glutMotionFunc(motion);
74 glewInit();
76 glEnable(GL_DEPTH_TEST);
77 glEnable(GL_NORMALIZE);
79 glEnable(GL_LIGHTING); //TODO: shaders
80 glEnable(GL_LIGHT0);
81 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
83 vol = new Volume;
84 if(!vol->load(vol_fname)) {
85 fprintf(stderr, "Failed to load %s", vol_fname);
86 return false;
87 }
88 mesh = new Mesh;
89 cur_z = 0.5;
91 vol_res[0] = vol->get_slice(0)->get_width();
92 vol_res[1] = vol->get_slice(0)->get_height();
93 vol_res[2] = vol->get_slice_count();
95 if(!(ui = create_ui())) {
96 return false;
97 }
99 return true;
100 }
102 static GLUI_Spinner *res_spin[3];
103 static void toggle_use_orig(int id)
104 {
105 for(int i=0; i<3; i++) {
106 if(use_orig_vol_res) {
107 res_spin[i]->disable();
108 } else {
109 res_spin[i]->enable();
110 }
111 }
112 }
113 static void thres_change(int id)
114 {
115 static float prev_thres = thres;
116 static float prev_thres2 = thres2;
118 if(prev_thres != thres || prev_thres2 != thres2) {
119 prev_thres = thres;
120 prev_thres2 = thres2;
121 mesh->clear();
122 }
124 glutSetWindow(xferwin_id);
125 glutPostRedisplay();
126 glutSetWindow(mainwin_id);
127 glutPostRedisplay();
128 }
129 static void res_change(int id)
130 {
131 static float prev_resx = vol_res[0];
132 static float prev_resy = vol_res[1];
133 static float prev_resz = vol_res[2];
135 if(prev_resx != vol_res[0] || prev_resy != vol_res[1] || prev_resz != vol_res[2]) {
136 prev_resx = vol_res[0];
137 prev_resy = vol_res[1];
138 prev_resz = vol_res[2];
139 mesh->clear();
140 }
141 }
142 static GLUI *create_ui()
143 {
144 GLUI *ui = GLUI_Master.create_glui("ui");
145 assert(ui);
147 ui->set_main_gfx_window(glutGetWindow());
149 GLUI_Panel *thres_panel = ui->add_panel("iso thresholds");
151 GLUI_Spinner *thres_spin = ui->add_spinner_to_panel(thres_panel, "T1", GLUI_SPINNER_FLOAT, &thres, 0, thres_change);
152 thres_spin->set_float_limits(0, 1);
154 GLUI_Spinner *thres2_spin = ui->add_spinner_to_panel(thres_panel, "T2", GLUI_SPINNER_FLOAT, &thres2, 0, thres_change);
155 thres2_spin->set_float_limits(0, 1);
157 GLUI_Panel *res_panel = ui->add_panel("volume resolution");
159 ui->add_checkbox_to_panel(res_panel, "original resolution", &use_orig_vol_res, 0, toggle_use_orig);
161 static const char *res_spin_name[] = {"x", "y", "z"};
162 for(int i=0; i<3; i++) {
163 res_spin[i] = ui->add_spinner_to_panel(res_panel, res_spin_name[i], GLUI_SPINNER_INT, vol_res + i, 0, res_change);
164 res_spin[i]->set_int_limits(8, 256); // TODO limits as arguments or config
165 res_spin[i]->disable();
166 }
168 return ui;
169 }
171 static void display(void)
172 {
173 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
175 glMatrixMode(GL_MODELVIEW);
176 glLoadIdentity();
177 glTranslatef(0, 0, -cam_dist);
178 glRotatef(cam_phi, 1, 0, 0);
179 glRotatef(cam_theta, 0, 1, 0);
181 /*
182 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
183 glEnable(GL_TEXTURE_3D);
184 glBegin(GL_QUADS);
185 glTexCoord3f(0, 0, cur_z); glVertex3f(-1, -1, 0);
186 glTexCoord3f(0, 1, cur_z); glVertex3f(-1, 1, 0);
187 glTexCoord3f(1, 1, cur_z); glVertex3f(1, 1, 0);
188 glTexCoord3f(1, 0, cur_z); glVertex3f(1, -1, 0);
189 glEnd();
190 glDisable(GL_TEXTURE_3D);
191 */
193 if(mesh->is_empty()) {
194 printf("recalculating isosurface ... ");
195 fflush(stdout);
196 vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]);
197 printf("done.\n");
198 }
199 mesh->draw();
201 //TODO: draw threshold
202 glutSwapBuffers();
203 assert(glGetError() == GL_NO_ERROR);
204 }
206 static void reshape(int x, int y)
207 {
208 glViewport(0, 0, x, y);
209 glMatrixMode(GL_PROJECTION);
210 glLoadIdentity();
211 gluPerspective(45, (float)x / (float)y, 0.5, 500);
213 if(x != win_xsz || y != win_ysz) {
214 win_xsz = x;
215 win_ysz = y;
216 }
217 }
219 static void keyboard(unsigned char key, int x, int y)
220 {
221 key_state[(int)key] = true;
223 switch(key) {
224 case 27:
225 exit(0);
226 case 'w':
227 {
228 static bool wire;
229 wire = !wire;
230 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
231 }
232 break;
233 default:
234 break;
235 }
236 }
238 static void keyboard_up(unsigned char key, int x, int y)
239 {
240 key_state[(int) key] = false;
241 }
243 static int prev_x, prev_y;
244 static bool bn_state[32];
245 static float prev_thres, prev_thres2;
247 static void mouse(int bn, int state, int x, int y)
248 {
249 prev_x = x;
250 prev_y = y;
252 bn_state[bn - GLUT_LEFT_BUTTON] = (state == GLUT_DOWN);
254 if(state == GLUT_DOWN) {
255 prev_thres = thres;
256 prev_thres2 = thres2;
257 }
258 else {
259 if(thres != prev_thres || thres2 != prev_thres2) {
260 mesh->clear();
261 }
262 }
263 }
265 static void motion(int x, int y)
266 {
267 int dx = x - prev_x;
268 int dy = y - prev_y;
270 if(!dx && !dy)
271 return;
273 prev_x = x;
274 prev_y = y;
276 if(key_state[(int)'t']) {
277 thres = (float)x / (float)win_xsz;
278 printf("threshold: %f\n", thres);
280 glutPostRedisplay();
281 return;
282 }
284 // camera
285 if(bn_state[0]) {
286 if(key_state[(int)'z']) {
287 //zoom the camera
289 cam_dist += dy * 0.1;
291 if(cam_dist < 0)
292 cam_dist = 0;
293 }
294 else {
295 //rotate the camera
297 cam_phi += dy * 0.5;
298 cam_theta += dx * 0.5;
300 if(cam_phi > 90)
301 cam_phi = 90;
302 if(cam_phi < -90)
303 cam_phi = -90;
304 }
306 glutPostRedisplay();
307 }
308 }
310 static bool init_xfer(void)
311 {
312 glutSetWindow(mainwin_id);
313 int x = glutGet(GLUT_WINDOW_X);
314 int y = glutGet(GLUT_WINDOW_Y) + glutGet(GLUT_WINDOW_HEIGHT);
316 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
317 glutInitWindowSize(512, 100);
318 glutInitWindowPosition(x, y);
320 xferwin_id = glutCreateWindow("Transfer Function");
321 glutDisplayFunc(display_xfer);
322 glutReshapeFunc(reshape_xfer);
323 glutMotionFunc(motion_xfer);
325 return true;
326 }
328 static void display_xfer(void)
329 {
330 glClear(GL_COLOR_BUFFER_BIT);
332 int num_val = glutGet(GLUT_WINDOW_WIDTH) / 4.0;
333 float x = 0;
334 float dx = 1.0 / num_val;
335 float low, high;
336 if(thres < thres2) {
337 low = thres;
338 high = thres2;
339 }
340 else {
341 low = thres2;
342 high = thres;
343 }
345 glBegin(GL_QUADS);
346 for(int i=0; i<num_val; i++) {
347 float val = transfer_function(x, low, high);
348 glColor3f(val, val, val);
349 glVertex3f(x, 1.0, 0.0);
350 glVertex3f(x, 0.0, 0.0);
352 val = transfer_function(x + dx, low, high);
353 glColor3f(val, val, val);
354 glVertex3f(x + dx, 0.0, 0.0);
355 glVertex3f(x + dx, 1.0, 0.0);
356 x += dx;
357 }
358 glEnd();
360 glutSwapBuffers();
361 assert(glGetError() == GL_NO_ERROR);
362 }
364 static void reshape_xfer(int x, int y)
365 {
366 glViewport(0.0, 0.0, x, y);
367 glMatrixMode(GL_PROJECTION);
368 glLoadIdentity();
369 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
370 }
372 static void motion_xfer(int x, int y)
373 {
374 //to pontiki sto kontinotero akro
375 //glutPostRedisplay k sta 2 win
376 }