volmetrics

view src/main.cc @ 14:a828d8735957

foo
author Eleni Maria Stea <elene.mst@gmail.com>
date Sun, 02 Feb 2014 02:32:37 +0200
parents c5af275b6a60
children a93c8aa85e05
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 int win_xsz, win_ysz;
23 static float cam_phi, cam_theta, cam_dist = 6;
24 static std::vector<bool> key_state(256);
26 static const char *vol_fname = "data/test1.vol";
28 static Volume *vol;
29 static Mesh *mesh;
30 static float cur_z, thres = 0.5, thres2 = 1.0;
32 static int use_orig_vol_res = 1;
33 static int vol_res[3]; // volume sampling resolution x/y/z
35 static GLUI *ui;
37 int main(int argc, char **argv)
38 {
39 glutInit(&argc, argv);
40 glutInitWindowSize(512, 512);
41 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
43 if(argv[1])
44 vol_fname = argv[1];
46 glutCreateWindow("My Colonoscopie OEO!");
48 glutDisplayFunc(display);
49 glutReshapeFunc(reshape);
50 glutKeyboardFunc(keyboard);
51 glutKeyboardUpFunc(keyboard_up);
52 glutMouseFunc(mouse);
53 glutMotionFunc(motion);
55 glewInit();
56 if(!init()) {
57 fprintf(stderr, "Failed to initialize La votre Colonoscopie\n");
58 return 1;
59 }
61 //call init
63 glutMainLoop();
64 return 0;
65 }
67 static bool init()
68 {
69 glEnable(GL_DEPTH_TEST);
70 glEnable(GL_NORMALIZE);
72 glEnable(GL_LIGHTING); //TODO: shaders
73 glEnable(GL_LIGHT0);
74 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
76 vol = new Volume;
77 if(!vol->load(vol_fname)) {
78 fprintf(stderr, "Failed to load %s", vol_fname);
79 return false;
80 }
81 mesh = new Mesh;
82 cur_z = 0.5;
84 vol_res[0] = vol->get_slice(0)->get_width();
85 vol_res[1] = vol->get_slice(0)->get_height();
86 vol_res[2] = vol->get_slice_count();
88 if(!(ui = create_ui())) {
89 return false;
90 }
92 return true;
93 }
95 static GLUI_Spinner *res_spin[3];
96 static void toggle_use_orig(int id)
97 {
98 for(int i=0; i<3; i++) {
99 if(use_orig_vol_res) {
100 res_spin[i]->disable();
101 } else {
102 res_spin[i]->enable();
103 }
104 }
105 }
106 static void thres_change(int id)
107 {
108 static float prev_thres = thres;
109 static float prev_thres2 = thres2;
111 if(prev_thres != thres || prev_thres2 != thres2) {
112 prev_thres = thres;
113 prev_thres2 = thres2;
114 mesh->clear();
115 }
116 }
117 static void res_change(int id)
118 {
119 static float prev_resx = vol_res[0];
120 static float prev_resy = vol_res[1];
121 static float prev_resz = vol_res[2];
123 if(prev_resx != vol_res[0] || prev_resy != vol_res[1] || prev_resz != vol_res[2]) {
124 prev_resx = vol_res[0];
125 prev_resy = vol_res[1];
126 prev_resz = vol_res[2];
127 mesh->clear();
128 }
129 }
130 static GLUI *create_ui()
131 {
132 GLUI *ui = GLUI_Master.create_glui("ui");
133 assert(ui);
135 ui->set_main_gfx_window(glutGetWindow());
137 GLUI_Panel *thres_panel = ui->add_panel("iso thresholds");
139 GLUI_Spinner *thres_spin = ui->add_spinner_to_panel(thres_panel, "T1", GLUI_SPINNER_FLOAT, &thres, 0, thres_change);
140 thres_spin->set_float_limits(0, 1);
142 GLUI_Spinner *thres2_spin = ui->add_spinner_to_panel(thres_panel, "T2", GLUI_SPINNER_FLOAT, &thres2, 0, thres_change);
143 thres2_spin->set_float_limits(0, 1);
145 GLUI_Panel *res_panel = ui->add_panel("volume resolution");
147 ui->add_checkbox_to_panel(res_panel, "original resolution", &use_orig_vol_res, 0, toggle_use_orig);
149 static const char *res_spin_name[] = {"x", "y", "z"};
150 for(int i=0; i<3; i++) {
151 res_spin[i] = ui->add_spinner_to_panel(res_panel, res_spin_name[i], GLUI_SPINNER_INT, vol_res + i, 0, res_change);
152 res_spin[i]->set_int_limits(8, 256); // TODO limits as arguments or config
153 res_spin[i]->disable();
154 }
156 return ui;
157 }
159 static void display(void)
160 {
161 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
163 glMatrixMode(GL_MODELVIEW);
164 glLoadIdentity();
165 glTranslatef(0, 0, -cam_dist);
166 glRotatef(cam_phi, 1, 0, 0);
167 glRotatef(cam_theta, 0, 1, 0);
169 /*
170 glBindTexture(GL_TEXTURE_3D, vol->get_texture());
171 glEnable(GL_TEXTURE_3D);
172 glBegin(GL_QUADS);
173 glTexCoord3f(0, 0, cur_z); glVertex3f(-1, -1, 0);
174 glTexCoord3f(0, 1, cur_z); glVertex3f(-1, 1, 0);
175 glTexCoord3f(1, 1, cur_z); glVertex3f(1, 1, 0);
176 glTexCoord3f(1, 0, cur_z); glVertex3f(1, -1, 0);
177 glEnd();
178 glDisable(GL_TEXTURE_3D);
179 */
181 if(mesh->is_empty()) {
182 printf("recalculating isosurface ... ");
183 fflush(stdout);
184 vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]);
185 printf("done.\n");
186 }
187 mesh->draw();
189 //TODO: draw threshold
190 glutSwapBuffers();
191 assert(glGetError() == GL_NO_ERROR);
192 }
194 static void reshape(int x, int y)
195 {
196 glViewport(0, 0, x, y);
197 glMatrixMode(GL_PROJECTION);
198 glLoadIdentity();
199 gluPerspective(45, (float)x / (float)y, 0.5, 500);
201 if(x != win_xsz || y != win_ysz) {
202 win_xsz = x;
203 win_ysz = y;
204 }
205 }
207 static void keyboard(unsigned char key, int x, int y)
208 {
209 key_state[(int)key] = true;
211 switch(key) {
212 case 27:
213 exit(0);
214 case 'w':
215 {
216 static bool wire;
217 wire = !wire;
218 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
219 }
220 break;
221 default:
222 break;
223 }
224 }
226 static void keyboard_up(unsigned char key, int x, int y)
227 {
228 key_state[(int) key] = false;
229 }
231 static int prev_x, prev_y;
232 static bool bn_state[32];
233 static float prev_thres, prev_thres2;
235 static void mouse(int bn, int state, int x, int y)
236 {
237 prev_x = x;
238 prev_y = y;
240 bn_state[bn - GLUT_LEFT_BUTTON] = (state == GLUT_DOWN);
242 if(state == GLUT_DOWN) {
243 prev_thres = thres;
244 prev_thres2 = thres2;
245 }
246 else {
247 if(thres != prev_thres || thres2 != prev_thres2) {
248 mesh->clear();
249 }
250 }
251 }
253 static void motion(int x, int y)
254 {
255 int dx = x - prev_x;
256 int dy = y - prev_y;
258 if(!dx && !dy)
259 return;
261 prev_x = x;
262 prev_y = y;
264 if(key_state[(int)'t']) {
265 thres = (float)x / (float)win_xsz;
266 printf("threshold: %f\n", thres);
268 glutPostRedisplay();
269 return;
270 }
272 // camera
273 if(bn_state[0]) {
274 if(key_state[(int)'z']) {
275 //zoom the camera
277 cam_dist += dy * 0.1;
279 if(cam_dist < 0)
280 cam_dist = 0;
281 }
282 else {
283 //rotate the camera
285 cam_phi += dy * 0.5;
286 cam_theta += dx * 0.5;
288 if(cam_phi > 90)
289 cam_phi = 90;
290 if(cam_phi < -90)
291 cam_phi = -90;
292 }
294 glutPostRedisplay();
295 }
296 }