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