1 | /* |
---|
2 | * ---------------------------------------------------------------------- |
---|
3 | * MainWindow.h: implementation of the main transfer function gui window |
---|
4 | * |
---|
5 | * ====================================================================== |
---|
6 | * AUTHOR: Wei Qiao <qiaow@purdue.edu> |
---|
7 | * Purdue Rendering and Perceptualization Lab (PURPL) |
---|
8 | * |
---|
9 | * Copyright (c) 2004-2006 Purdue Research Foundation |
---|
10 | * |
---|
11 | * See the file "license.terms" for information on usage and |
---|
12 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
13 | * ====================================================================== |
---|
14 | */ |
---|
15 | |
---|
16 | #include "MainWindow.h" |
---|
17 | #include "TransferFunctionGLUTWindow.h" |
---|
18 | #include "ColorPaletteWindow.h" |
---|
19 | #include "ColorGradientGLUTWindow.h" |
---|
20 | #include "TransferFunctionMain.h" |
---|
21 | |
---|
22 | GLUI* main_glui; |
---|
23 | GLUI_Panel* main_pan; |
---|
24 | |
---|
25 | int main_winx, main_winy; |
---|
26 | int mainWindow; |
---|
27 | int colorMapWindow; |
---|
28 | int transferFunctionWindow=-1; |
---|
29 | int colorPaletteWindow = -1; |
---|
30 | int unitWidth, unitHeight; |
---|
31 | double distanceThreshold; |
---|
32 | |
---|
33 | MainTransferFunctionWindow::MainTransferFunctionWindow(){} |
---|
34 | MainTransferFunctionWindow::~MainTransferFunctionWindow(){} |
---|
35 | |
---|
36 | |
---|
37 | //initialize |
---|
38 | void MainTransferFunctionWindow::mainInit(){ |
---|
39 | |
---|
40 | distanceThreshold=36; |
---|
41 | |
---|
42 | main_winx=600; |
---|
43 | main_winy=300; |
---|
44 | |
---|
45 | glutInitWindowPosition(100, 100); |
---|
46 | glutInitWindowSize(main_winx, main_winy ); |
---|
47 | |
---|
48 | mainWindow=glutCreateWindow("Advanced Transfer Function"); |
---|
49 | |
---|
50 | glutDisplayFunc(mainDisplay); |
---|
51 | glutReshapeFunc(mainReshape); |
---|
52 | glutKeyboardFunc (mainKeyboard); |
---|
53 | //glutIdleFunc (mainIdle); |
---|
54 | |
---|
55 | |
---|
56 | //glutMouseFunc (mainMouse); |
---|
57 | //glutMotionFunc (mainMotion); |
---|
58 | |
---|
59 | TransferFunctionGLUTWindow::tfInit(main_winx, main_winy); |
---|
60 | transferFunctionWindow=glutCreateSubWindow(mainWindow, 0, 0, tf_winx, tf_winy); |
---|
61 | glutDisplayFunc(TransferFunctionGLUTWindow::tfDisplay); |
---|
62 | glutReshapeFunc(TransferFunctionGLUTWindow::tfReshape); |
---|
63 | glutMouseFunc(TransferFunctionGLUTWindow::tfMouse); |
---|
64 | glutKeyboardFunc(TransferFunctionGLUTWindow::tfKeyboard); |
---|
65 | glutMotionFunc (TransferFunctionGLUTWindow::tfMotion); |
---|
66 | |
---|
67 | ColorGradientGLUTWindow::cmInit(main_winx, main_winy); |
---|
68 | colorMapWindow=glutCreateSubWindow(mainWindow, 0, main_winy-cm_winy, cm_winx, cm_winy); |
---|
69 | //ColorGradientGLUTWindow::createGLUIWidgets(); |
---|
70 | glutDisplayFunc(ColorGradientGLUTWindow::cmDisplay); |
---|
71 | glutReshapeFunc(ColorGradientGLUTWindow::cmReshape); |
---|
72 | glutMouseFunc(ColorGradientGLUTWindow::cmMouse); |
---|
73 | glutMotionFunc(ColorGradientGLUTWindow::cmMotion); |
---|
74 | glutKeyboardFunc(ColorGradientGLUTWindow::cmKeyboard); |
---|
75 | |
---|
76 | GLUI_Master.set_glutIdleFunc(mainIdle); |
---|
77 | |
---|
78 | ColorPalette::cpInit(); |
---|
79 | |
---|
80 | //int tmp= glutGetWindow(); |
---|
81 | |
---|
82 | //load a initial trasfer function |
---|
83 | //for bluntfin dataset |
---|
84 | //MainTransferFunctionWindow::loadFile("bluntfin_tf_1"); |
---|
85 | //glutSetWindow(tmp); |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | void MainTransferFunctionWindow::mainDisplay(){ |
---|
90 | glutSetWindow(transferFunctionWindow); |
---|
91 | glutPostRedisplay(); |
---|
92 | glutSetWindow(colorMapWindow); |
---|
93 | glutPostRedisplay(); |
---|
94 | glutSetWindow(colorPaletteWindow); |
---|
95 | glutPostRedisplay(); |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | void MainTransferFunctionWindow::mainMouse(int button, int state, int x, int y){ |
---|
101 | fprintf(stderr, "main mouse\n"); |
---|
102 | //find out what area received the event |
---|
103 | if (y<=main_winy-tf_winy){ |
---|
104 | //transfer function received the event |
---|
105 | glutSetWindow(transferFunctionWindow); |
---|
106 | TransferFunctionGLUTWindow::tfMouse(button, state, x, y); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | void MainTransferFunctionWindow::mainMotion(int x, int y){ |
---|
116 | |
---|
117 | //////////////////////////////////////// |
---|
118 | //glutSetWindow(transferFunctionWindow); |
---|
119 | //TransferFunctionGLUTWindow::tfMotion(x, y); |
---|
120 | ///////////////////////////////////// |
---|
121 | |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | void MainTransferFunctionWindow::mainIdle(){ |
---|
127 | /* |
---|
128 | glutSetWindow(mainWindow); |
---|
129 | glutPostRedisplay(); |
---|
130 | glutSetWindow(transferFunctionWindow); |
---|
131 | glutPostRedisplay(); |
---|
132 | glutSetWindow(colorMapWindow); |
---|
133 | glutPostRedisplay(); |
---|
134 | glutSetWindow(colorPaletteWindow); |
---|
135 | glutPostRedisplay(); |
---|
136 | */ |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | void MainTransferFunctionWindow::mainKeyboard(unsigned char key, int x, int y){ |
---|
142 | switch(key) |
---|
143 | { |
---|
144 | case 'q': |
---|
145 | exit(0); |
---|
146 | break; |
---|
147 | case 'a': |
---|
148 | glutSetWindow(transferFunctionWindow); |
---|
149 | tf_pointEditState=1; |
---|
150 | break; |
---|
151 | case 'd': |
---|
152 | glutSetWindow(transferFunctionWindow); |
---|
153 | tf_pointEditState=2; |
---|
154 | if (tf_gvSelectedPoint!=NULL){ |
---|
155 | TransferFunctionGLUTWindow::removePoint(tf_gvSelectedPoint); |
---|
156 | tf_gvSelectedPoint=0; |
---|
157 | TransferFunctionGLUTWindow::WriteControlPoints(); |
---|
158 | glutPostRedisplay(); |
---|
159 | } |
---|
160 | tf_pointEditState=0; |
---|
161 | break; |
---|
162 | case 'p': |
---|
163 | TransferFunctionGLUTWindow::printPoints(); |
---|
164 | break; |
---|
165 | case 'w': |
---|
166 | TransferFunctionGLUTWindow::printInterpolation(); |
---|
167 | break; |
---|
168 | case 'r': |
---|
169 | TransferFunctionGLUTWindow::readHist(); |
---|
170 | break; |
---|
171 | case 'u': |
---|
172 | TransferFunctionGLUTWindow::dumpHist(); |
---|
173 | break; |
---|
174 | case 'i': |
---|
175 | TransferFunctionGLUTWindow::printHist(); |
---|
176 | break; |
---|
177 | |
---|
178 | default: |
---|
179 | break; |
---|
180 | } |
---|
181 | |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | void MainTransferFunctionWindow::mainDestroy(){} |
---|
186 | |
---|
187 | |
---|
188 | void MainTransferFunctionWindow::mainReshape(int x, int y){ |
---|
189 | |
---|
190 | if (x==0 || y==0) |
---|
191 | return; |
---|
192 | |
---|
193 | main_winx=x; |
---|
194 | main_winy=y; |
---|
195 | |
---|
196 | //change size and location of the subwindow |
---|
197 | cm_winx=main_winx; |
---|
198 | //cm_winy=main_winy/3; |
---|
199 | cm_winy=100; |
---|
200 | |
---|
201 | glutSetWindow(colorMapWindow); |
---|
202 | glutPositionWindow(0, main_winy-cm_winy); |
---|
203 | glutReshapeWindow(cm_winx, cm_winy); |
---|
204 | glutPostRedisplay(); |
---|
205 | |
---|
206 | //change size and location of the subwindow |
---|
207 | tf_winx=main_winx; |
---|
208 | //tf_winy=main_winy*2/3; |
---|
209 | tf_winy=main_winy-100; |
---|
210 | |
---|
211 | glutSetWindow(transferFunctionWindow); |
---|
212 | glutPositionWindow(0,0); |
---|
213 | glutReshapeWindow(tf_winx, tf_winy); |
---|
214 | glutPostRedisplay(); |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | ////////////////////////////////////////////////////////// |
---|
219 | //FILE STRUCTURE: |
---|
220 | // |
---|
221 | //<first section> |
---|
222 | //first line: number_of_opacity_control_points |
---|
223 | //following: each line is a control point |
---|
224 | //control point coordinate is in range [0,1], inclusive |
---|
225 | //empty line signals the end of first section |
---|
226 | // |
---|
227 | //<second section> |
---|
228 | //first line: number_of_color_control_points |
---|
229 | //following: each line is a control point |
---|
230 | //only x is recorded, control point coordinate is in range [0,1], inclusive |
---|
231 | //empty line signals the end of second section |
---|
232 | // |
---|
233 | //<third section> |
---|
234 | //each line is a color in RGB |
---|
235 | //each color value is in range [0,1], inclusive |
---|
236 | //"END" signals the end of file |
---|
237 | //////////////////////////////////////////////////////////// |
---|
238 | |
---|
239 | |
---|
240 | void MainTransferFunctionWindow::loadFile(char* fileName){ |
---|
241 | |
---|
242 | |
---|
243 | if (fileName==0 || strlen(fileName)<1){ |
---|
244 | fprintf(stderr, "Error: file name not supplied.\n"); |
---|
245 | return; |
---|
246 | } |
---|
247 | |
---|
248 | FILE* fp=fopen(fileName, "r"); |
---|
249 | |
---|
250 | if(!fp){ |
---|
251 | fprintf(stderr, "Error: open file.\n"); |
---|
252 | return; |
---|
253 | } |
---|
254 | |
---|
255 | fprintf(stderr, "File \"%s\" opened.\n", fileName); |
---|
256 | |
---|
257 | char buf[300]; //buffer to store one line from file |
---|
258 | char* token; //pointer to token in the buffer |
---|
259 | char seps[]=" \n"; //seperators |
---|
260 | |
---|
261 | |
---|
262 | int num_of_control_point; |
---|
263 | int num_of_color_point; |
---|
264 | double control_points[200]; |
---|
265 | double color_points[100]; |
---|
266 | double colors[300]; |
---|
267 | |
---|
268 | |
---|
269 | //clean up old control points |
---|
270 | TransferFunctionGLUTWindow::cleanUpPoints(); |
---|
271 | |
---|
272 | fgets(buf, 100, fp); |
---|
273 | token=strtok(buf, seps); |
---|
274 | num_of_control_point=(int)atof(token); |
---|
275 | |
---|
276 | //read control points |
---|
277 | for(int i=0; i<num_of_control_point; i++){ |
---|
278 | fgets(buf, 100, fp); |
---|
279 | token=strtok(buf, seps); |
---|
280 | control_points[2*i]=atof(token)*tf_unitWidth+15; |
---|
281 | |
---|
282 | token=strtok(NULL, seps); |
---|
283 | control_points[2*i+1]=(atof(token)*(tf_winy-15))+15; |
---|
284 | |
---|
285 | //add points |
---|
286 | TransferFunctionGLUTWindow::addPoint(control_points[2*i], control_points[2*i+1]); |
---|
287 | } |
---|
288 | //rewrite the interpolatoin |
---|
289 | TransferFunctionGLUTWindow::WriteControlPoints(); |
---|
290 | |
---|
291 | fgets(buf, 100, fp); //empty line |
---|
292 | |
---|
293 | |
---|
294 | fgets(buf, 100, fp); |
---|
295 | token=strtok(buf, seps); |
---|
296 | num_of_color_point=(int)atof(token); |
---|
297 | |
---|
298 | |
---|
299 | //read color points |
---|
300 | for(int i=0; i<num_of_color_point; i++){ |
---|
301 | fgets(buf, 100, fp); |
---|
302 | token=strtok(buf, seps); |
---|
303 | color_points[i]=atof(token)*cm_unitWidth+15; |
---|
304 | } |
---|
305 | |
---|
306 | |
---|
307 | //clean map |
---|
308 | map->cleanUp(); |
---|
309 | |
---|
310 | fgets(buf, 100, fp); //empty line |
---|
311 | |
---|
312 | //read colors |
---|
313 | for(int i=0; i<num_of_color_point; i++){ |
---|
314 | fgets(buf, 100, fp); |
---|
315 | token=strtok(buf, seps); |
---|
316 | colors[3*i]=atof(token); |
---|
317 | |
---|
318 | token=strtok(NULL, seps); |
---|
319 | colors[3*i+1]=atof(token); |
---|
320 | |
---|
321 | token=strtok(NULL, seps); |
---|
322 | colors[3*i+2]=atof(token); |
---|
323 | |
---|
324 | map->addKey(color_points[i], new Color(colors[3*i], colors[3*i+1], colors[3*i+2])); |
---|
325 | |
---|
326 | } |
---|
327 | //rewrite the interpolatoin |
---|
328 | ColorGradientGLUTWindow::WriteControlPoints(); |
---|
329 | |
---|
330 | glutSetWindow(transferFunctionWindow); |
---|
331 | TransferFunctionGLUTWindow::tfDisplay(); |
---|
332 | |
---|
333 | //read modelview settings |
---|
334 | fgets(buf, 100, fp); //empty line |
---|
335 | |
---|
336 | //camero location |
---|
337 | fgets(buf, 100, fp); |
---|
338 | token=strtok(buf, seps); |
---|
339 | //eye_shift_x=atof(token); |
---|
340 | |
---|
341 | token=strtok(NULL, seps); |
---|
342 | //eye_shift_y=atof(token); |
---|
343 | |
---|
344 | token=strtok(NULL, seps); |
---|
345 | //eyedistance=atof(token); |
---|
346 | |
---|
347 | |
---|
348 | fgets(buf, 100, fp); //empty line |
---|
349 | |
---|
350 | //object center location |
---|
351 | fgets(buf, 100, fp); |
---|
352 | token=strtok(buf, seps); |
---|
353 | //obj_center[0]=atof(token); |
---|
354 | |
---|
355 | token=strtok(NULL, seps); |
---|
356 | //obj_center[1]=atof(token); |
---|
357 | |
---|
358 | token=strtok(NULL, seps); |
---|
359 | //obj_center[2]=atof(token); |
---|
360 | |
---|
361 | |
---|
362 | fgets(buf, 100, fp); //empty line |
---|
363 | |
---|
364 | //scale and field_of_view |
---|
365 | fgets(buf, 100, fp); |
---|
366 | token=strtok(buf, seps); |
---|
367 | control_point_scale=atof(token); |
---|
368 | |
---|
369 | token=strtok(NULL, seps); |
---|
370 | //fovy=atof(token); |
---|
371 | |
---|
372 | if (colorPaletteWindow!=-1){ |
---|
373 | glutSetWindow(colorPaletteWindow); |
---|
374 | ColorPalette::cpSyncLive(); |
---|
375 | } |
---|
376 | |
---|
377 | fclose(fp); |
---|
378 | |
---|
379 | //update transfer function values |
---|
380 | if(transferFunctionWindow!=-1){ |
---|
381 | TransferFunctionGLUTWindow::WriteControlPoints(); |
---|
382 | } |
---|
383 | |
---|
384 | //render the volume again. |
---|
385 | //glutSetWindow(render_window); |
---|
386 | glutPostRedisplay(); |
---|
387 | } |
---|
388 | |
---|
389 | |
---|
390 | |
---|
391 | |
---|
392 | void MainTransferFunctionWindow::saveFile(char* fileName){ |
---|
393 | |
---|
394 | // Check for bad param |
---|
395 | if(fileName == NULL) |
---|
396 | return; |
---|
397 | if(strlen(fileName) < 1) |
---|
398 | return; |
---|
399 | |
---|
400 | FILE* fp = fopen(fileName, "w"); |
---|
401 | |
---|
402 | if(!fp) |
---|
403 | return; //error |
---|
404 | |
---|
405 | fprintf(fp, "%d\n", tf_numOfPoints); |
---|
406 | |
---|
407 | ControlPoint* cur=tf_pointList; |
---|
408 | |
---|
409 | for (int i=0; i<tf_numOfPoints; i++){ |
---|
410 | fprintf(fp, "%g %g\n", ((double)(cur->x-15))/((double)tf_unitWidth), ((double)(cur->y-15))/((double)tf_winy-15)); |
---|
411 | cur=cur->next; |
---|
412 | } |
---|
413 | fprintf(fp, "\n"); |
---|
414 | |
---|
415 | |
---|
416 | fprintf(fp, "%d\n", map->numOfKeys); |
---|
417 | cur=map->keyList; |
---|
418 | for(int i=0; i<map->numOfKeys; i++){ |
---|
419 | fprintf(fp, "%g\n", ((double)(cur->x-15))/((double)cm_unitWidth)); |
---|
420 | cur=cur->next; |
---|
421 | } |
---|
422 | fprintf(fp, "\n"); |
---|
423 | |
---|
424 | |
---|
425 | Color* curColor=map->keyColors; |
---|
426 | for(int i=0; i<map->numOfKeys; i++){ |
---|
427 | fprintf(fp, "%g %g %g\n", curColor->R, curColor->G, curColor->B); |
---|
428 | curColor=curColor->next; |
---|
429 | } |
---|
430 | fprintf(fp, "\n"); |
---|
431 | |
---|
432 | //eye location |
---|
433 | //fprintf(fp, "%g %g %g\n\n", eye_shift_x, eye_shift_y, eyedistance); |
---|
434 | |
---|
435 | //object center |
---|
436 | //fprintf(fp, "%g %g %g\n\n", obj_center[0], obj_center[1], obj_center[2]); |
---|
437 | |
---|
438 | //bluntfin quater |
---|
439 | //fprintf(fp, "%g %g %g %g\n\n", bluntfin_quater.s, bluntfin_quater.x, bluntfin_quater.y, bluntfin_quater.z); |
---|
440 | |
---|
441 | //scale and field_of_view |
---|
442 | //fprintf(fp, "%g %g\n", control_point_scale, fovy); |
---|
443 | |
---|
444 | fprintf(fp, "END\n"); |
---|
445 | |
---|
446 | fclose(fp); |
---|
447 | } |
---|