source: trunk/packages/vizservers/nanovis/transfer-function/MainWindow.cpp @ 2798

Last change on this file since 2798 was 2798, checked in by ldelgass, 12 years ago

Add emacs mode magic line in preparation for indentation cleanup

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