source: trunk/src/testing/app/mframes/init_code/initf.cpp @ 4

Revision 4, 31.1 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1/******************************************************************************
2 * SAGE - Scalable Adaptive Graphics Environment
3 *
4 * Copyright (C) 2004 Electronic Visualization Laboratory,
5 * University of Illinois at Chicago
6 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above
15 *    copyright notice, this list of conditions and the following disclaimer
16 *    in the documentation and/or other materials provided with the distribution.
17 *  * Neither the name of the University of Illinois at Chicago nor
18 *    the names of its contributors may be used to endorse or promote
19 *    products derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/
34 *
35 *****************************************************************************/
36#include "initf.h"
37
38//#define SAGE
39
40#define HORIZONTAL 0
41#define VERTICAL 1
42
43
44AnimViewer animviewer;
45
46double PointerPosition[2];
47
48int uiaction;
49double pos[3];
50
51double cw0, cw1, ch0, ch1;
52
53int window_w, window_h;
54
55// default is 1
56int ScreensPerNode = 1;
57
58
59
60char* hostname;
61
62string appname;
63
64
65
66bool SAGE;
67string sageStr;
68
69AnimViewer::AnimViewer() {
70
71        QueryWindow.virgin = true;
72
73        text->SetInput("Loading!!!");
74        text->SetDisplayPosition(300, 350);
75        text->GetTextProperty()->SetFontFamilyToArial();
76        text->GetTextProperty()->SetFontSize(150);
77        text->GetTextProperty()->SetColor(0.0,0.0,0.0);
78
79        sphere->SetThetaResolution(32);
80        sphere->SetPhiResolution(32);
81
82        sphereMapper->SetInput(sphere->GetOutput());
83        pointer->SetMapper(sphereMapper);
84        pointer->GetProperty()->SetColor(1,0,0);
85        pointer->GetProperty()->SetOpacity(0.6);
86        pointer->GetProperty()->BackfaceCullingOn();
87
88}
89
90AnimViewer::~AnimViewer() {
91
92        delete [] this->Blank;
93        this->Blank = NULL;
94
95}
96
97string AnimViewer::IntToString(long num) {
98
99        ostringstream o;
100        if (!(o << num))
101                printf("Error in conversion from int to string \n");
102
103        return o.str();
104
105}
106
107long AnimViewer::GetNumOfCols(long wtotal, long wblock) {
108
109
110        return (long) ceil(double(wtotal) / double(wblock));
111
112}
113
114long AnimViewer::GetNumOfRows(long wtotal, long wblock) {
115
116        return (long) ceil(double(wtotal) / double(wblock));
117
118}
119
120void AnimViewer::GetAnimFiles() {
121
122        long cols = this->NumOfCols;
123        long rows = this->NumOfRows;
124
125        string dirname, filename;
126
127        this->AnimFiles = new string**[this->NumOfLevels];
128
129        for(int i=0; i<this->NumOfLevels; i++) {
130
131                this->AnimFiles[i] = new string*[cols];
132
133                dirname.clear();
134                dirname += "/frame";
135                dirname += this->IntToString(i);
136
137                for(int c=0 ; c<cols; c++) {
138
139                        this->AnimFiles[i][c] = new string[rows];
140
141                        for(int r=0; r<rows; r++) {
142
143                                filename.clear();
144                                filename += dirname;
145                                filename += "/b_";
146                                filename += this->IntToString(c);
147                                filename += "_";
148                                filename += this->IntToString(r);
149
150
151                                this->AnimFiles[i][c][r] += filename;
152
153                                /*
154                                printf("%i %i %i ... ", i, c, r);
155                                printf("%s\n", this->AnimFiles[i][c][r].c_str());
156                                */
157                        }
158                }
159        }
160
161        printf("Created file locator.\n");
162
163}
164
165unsigned char* AnimViewer::MMap(long l, long c, long r) {
166        string filename;
167        filename += AnimDir;
168        filename += this->AnimFiles[l][c][r];
169
170        size_t len = this->BlockDimensions[0] * this->BlockDimensions[1] * MODE;
171
172        // Open file
173        this->Block[l][c][r].fd = open(filename.c_str(), O_RDONLY);
174
175        if(this->Block[l][c][r].fd < 0) {
176                printf("File does not exist. Could not map.\n");
177                printf("Assigning a blank tile...\n");
178
179                this->pixelbuff = this->Blank;
180        }
181        else {
182                //printf("Mapping %s.\n", filename.c_str());
183                this->pixelbuff = (unsigned char*) mmap(0,len,PROT_READ,MAP_SHARED,
184                                                                this->Block[l][c][r].fd,0);
185
186                madvise(0, len, MADV_SEQUENTIAL);
187        }
188
189        // Close file
190        close(this->Block[l][c][r].fd);
191
192        return this->pixelbuff;
193}
194
195/* Takes the filename and returns its pixels content */
196unsigned char* AnimViewer::GetPixelsFromFile(long l, long c, long r) {
197        string filename;
198        filename += AnimDir;
199        filename += this->AnimFiles[l][c][r];
200
201        this->pixelbuff =
202                        new unsigned char[this->BlockDimensions[0] * this->BlockDimensions[1] * MODE];
203
204        //fprintf(stderr, "Opening [%s]\n", filename.c_str() );
205
206        FILE* fptr = fopen(filename.c_str(),"rb");
207        //setvbuf(fptr, NULL, _IOFBF, 3*1024*1024);
208
209        if(!fptr) {
210                if(rank != 0)
211                        printf("Can't open %s\n", filename.c_str());
212
213                this->pixelbuff = this->Blank;
214        }
215        else {
216                //printf("Opening file %s\n", name.c_str());
217                fread(this->pixelbuff, this->BlockDimensions[0] *
218                                this->BlockDimensions[1] * MODE, sizeof(unsigned char),
219                                        fptr);
220
221                fclose(fptr);
222        }
223        //fprintf(stderr, "Closing [%s]\n", filename.c_str() );
224
225        return this->pixelbuff;
226}
227
228void AnimViewer::InitRendering() {
229
230        long cols = this->NumOfCols;
231        long rows = this->NumOfRows;
232
233
234        this->Block = new Geometry**[this->NumOfLevels];
235
236        for(int i=0 ; i<this->NumOfLevels; i++) {
237
238        this->Block[i] = new Geometry*[cols];
239
240        for(int c=0 ; c<cols; c++) {
241
242                this->Block[i][c] = new Geometry[rows];
243
244                for(int r=0; r<rows; r++) {
245
246                        this->Block[i][c][r].importer = vtkImageImport::New();
247                        this->Block[i][c][r].texture = vtkTexture::New();
248                        this->Block[i][c][r].actor = vtkActor::New();
249                        this->Block[i][c][r].plane = vtkPlaneSource::New();
250                        this->Block[i][c][r].mapper = vtkPolyDataMapper::New();
251                        this->Block[i][c][r].flip = vtkImageFlip::New();
252
253                        this->Block[i][c][r].textSource = vtkTextSource::New();
254                        this->Block[i][c][r].textMapper = vtkPolyDataMapper::New();
255                        this->Block[i][c][r].textActor = vtkActor::New();
256
257                        this->Block[i][c][r].importer->ReleaseDataFlagOn();
258                        this->Block[i][c][r].flip->ReleaseDataFlagOn();
259
260                }
261        }
262
263        }
264
265
266}
267
268void AnimViewer::SetUpPipeline() {
269
270        long cols = this->NumOfCols;
271        long rows = this->NumOfRows;
272
273        for(int i=0 ; i<this->NumOfLevels; i++) {
274
275        for(int c=0 ; c<cols; c++) {
276
277                for(int r=0; r<rows; r++) {
278
279                        this->Block[i][c][r].plane->SetResolution(1,1);
280
281                        this->Block[i][c][r].texture->RepeatOff();
282
283                        this->Block[i][c][r].texture->InterpolateOn();
284
285                        this->Block[i][c][r].mapper->SetInput(
286                                        (vtkPolyData*) this->Block[i][c][r].plane->GetOutput());
287
288                        this->Block[i][c][r].actor->SetMapper(
289                                        this->Block[i][c][r].mapper);
290
291                        this->Block[i][c][r].actor->GetProperty()->BackfaceCullingOn();
292
293                        this->Block[i][c][r].textSource->SetForegroundColor(1,0,0);
294                        this->Block[i][c][r].textSource->BackingOff();
295                        this->Block[i][c][r].textMapper->SetInput(
296                                                this->Block[i][c][r].textSource->GetOutput());
297                        this->Block[i][c][r].textActor->SetMapper(this->Block[i][c][r].textMapper);
298                                this->Block[i][c][r].textActor->GetProperty()->SetOpacity(0.5);
299
300                }
301        }
302        }
303
304}
305
306void AnimViewer::CopyBuffer(unsigned char* dest, unsigned char* origin) {
307
308        for(int i=0; i <
309                this->BlockDimensions[0] * this->BlockDimensions[1] * MODE; i++) {
310
311                dest[i] = origin[i];
312        }
313
314}
315
316
317
318
319void AnimViewer::ImportQueryWindowPixels() {
320
321        long cols = this->NumOfCols;
322        long rows = this->NumOfRows;
323
324        if(QueryWindow.virgin == true) {
325                this->QueryWindow.pixels = new unsigned char**[cols];
326
327                for(int c = 0; c < cols; c++) {
328
329                        this->QueryWindow.pixels[c] = new unsigned char*[rows];
330                }
331        }
332        else {
333                for(int c=0; c < cols; c++) {
334
335                        for(int r=0; r < rows; r++) {
336
337                                munmap(this->QueryWindow.pixels[c][r],
338                                        this->BlockDimensions[0] * this->BlockDimensions[1] * MODE);
339                                //delete [] this->QueryWindow.pixels[c][r];
340                                //this->QueryWindow.pixels[c][r] = NULL;
341                        }
342                }
343        }
344
345        for(int c = 0; c < cols; c++ ) {
346                for(int r = 0; r < rows; r++) {
347
348                                this->QueryWindow.pixels[c][r] = //GetPixelsFromFile(this->QueryWindow.level, c, r);
349                                this->MMap(this->QueryWindow.level, c, r);
350
351                                string label;
352                                label += IntToString(this->QueryWindow.level);
353                                label += " ";
354                                label += IntToString(c);
355                                label += " ";
356                                label += IntToString(r);
357
358                                this->Block[this->QueryWindow.level][c][r].textSource->SetText(label.c_str());
359                }
360        }
361
362        QueryWindow.virgin = false;
363
364        printf("IMPORTED PIXELS\n");
365
366}
367
368void AnimViewer::GivePixelsToVTK() {
369
370        long l = this->QueryWindow.level;
371        long cols = this->NumOfCols;
372        long rows = this->NumOfRows;
373
374        for(int c = 0; c < cols; c++ ) {
375                for(int r = 0; r < rows; r++) {
376
377                        this->Block[l][c][r].importer->
378                                                        SetDataScalarTypeToUnsignedChar();
379                        this->Block[l][c][r].importer->
380                                                        SetNumberOfScalarComponents(MODE);
381                        this->Block[l][c][r].importer->
382                                                        SetDataOrigin(0,0,0);
383                        this->Block[l][c][r].importer->
384                                                        SetWholeExtent(0, this->BlockDimensions[0] - 1, 0,
385                                                                                          this->BlockDimensions[1] - 1, 0, 0);
386                        this->Block[l][c][r].importer->
387                                                        SetDataExtentToWholeExtent();
388
389                        this->Block[l][c][r].importer->
390                                                        SetImportVoidPointer(this->QueryWindow.pixels[c][r]);
391
392                        this->UpdateBlock(l, c, r);
393
394                }
395        }
396
397        this->AddRegion(l, 0, 0, cols, rows);
398
399}
400
401void AnimViewer::UpdateBlock(long l, long c, long r) {
402
403        /* flip the image */
404        this->Block[l][c][r].flip->
405                        SetInput(this->Block[l][c][r].importer->GetOutput());
406
407        this->Block[l][c][r].flip->SetFilteredAxis(1);
408        this->Block[l][c][r].flip->FlipAboutOriginOn();
409
410        this->Block[l][c][r].texture->
411                        SetInput(this->Block[l][c][r].flip->GetOutput());
412
413        this->Block[l][c][r].actor->
414                        SetTexture(this->Block[l][c][r].texture);
415}
416
417void AnimViewer::AddBlock(long l, long c, long r) {
418
419        renA->AddActor(this->Block[l][c][r].actor);
420        if(ScreensPerNode > 1)
421                renB->AddActor(this->Block[l][c][r].actor);
422#ifdef DEBUG
423        renA->AddActor(this->Block[l][c][r].textActor);
424#endif
425}
426
427void AnimViewer::RemoveBlock(long l, long c, long r) {
428
429        renA->RemoveActor(this->Block[l][c][r].actor);
430
431        if(ScreensPerNode > 1)
432                renB->RemoveActor(this->Block[l][c][r].actor);
433#ifdef DEBUG
434        renA->RemoveActor(this->Block[l][c][r].textActor);
435#endif
436}
437
438
439void AnimViewer::RemoveRegion(long l, long c, long r, long w, long h) {
440
441        for(int i = c; i < c+w; i++) {
442                for(int j = r; j < r+h; j++) {
443                        this->RemoveBlock(l, i, j);
444                }
445        }
446}
447
448void AnimViewer::AddRegion(long l, long c, long r, long w, long h) {
449
450        for(int i = c; i < c+w; i++) {
451                for(int j = r; j < r+h; j++) {
452                        this->AddBlock(l, i, j);
453                }
454        }
455}
456
457
458void AnimViewer::CleanUpMainWindow() {
459
460        this->RemoveRegion(this->QueryWindow.level, 0, 0, this->NumOfCols, this->NumOfCols);
461}
462
463
464
465void AnimViewer::ComputeStartingBlockSize() {
466
467        double sw, sh;
468
469        double dw = double(this->BlockDimensions[0]) /
470                                                double(this->BlockDimensions[1]);
471        double dh = double(this->BlockDimensions[1]) /
472                                                double(this->BlockDimensions[0]);
473
474        if(dw > dh) {
475
476                sw = double(this->BlockDimensions[0]) /
477                                                double(this->BlockDimensions[0]);
478                sh = double(this->BlockDimensions[1]) /
479                                                double(this->BlockDimensions[0]);
480
481        }
482        else {
483
484                sw = double(this->BlockDimensions[0]) /
485                                                double(this->BlockDimensions[1]);
486                sh = double(this->BlockDimensions[1]) /
487                                                double(this->BlockDimensions[1]);
488        }
489
490        this->BlockInitSize[0] = sw;
491        this->BlockInitSize[1] = sh;
492}
493
494
495
496
497void AnimViewer::PositionAndScaleBlocks() {
498
499
500        long cols = this->NumOfCols;
501        long rows = this->NumOfRows;
502
503        this->ComputeStartingBlockSize();
504
505        this->BlockInfo = new Info*[cols];
506
507        for(int c=0 ; c<cols; c++) {
508
509                this->BlockInfo[c] = new Info[rows];
510
511                for(int r=0 ; r<rows; r++) {
512
513                        this->BlockInfo[c][r].scale[0] =
514                                                this->BlockInitSize[0];
515
516                        this->BlockInfo[c][r].scale[1] =
517                                                this->BlockInitSize[1];
518
519                        this->BlockInfo[c][r].position[0] =
520                                        double(c) * this->BlockInitSize[0];
521
522                        this->BlockInfo[c][r].position[1] =
523                                        double(r) * this->BlockInitSize[1];
524
525                        this->BlockInfo[c][r].position[2] = 0.1;
526
527                }
528        }
529
530        for(int l = 0; l<this->NumOfLevels; l++) {
531                for(int c=0 ; c<cols; c++) {
532                        for(int r=0 ; r<rows; r++) {
533
534
535                                this->Block[l][c][r].actor->
536                                        SetPosition(this->BlockInfo[c][r].position[0],
537                                                -this->BlockInfo[c][r].position[1],
538                                                        -this->BlockInfo[c][r].position[2]);
539
540                                this->Block[l][c][r].actor->
541                                        SetScale(this->BlockInfo[c][r].scale[0],
542                                                this->BlockInfo[c][r].scale[1], 0);
543
544
545                                this->Block[l][c][r].textActor->
546                                        SetPosition(this->BlockInfo[c][r].position[0],
547                                                -this->BlockInfo[c][r].position[1],
548                                                        -this->BlockInfo[c][r].position[2]);
549
550                                this->Block[l][c][r].textActor->
551                                        SetScale(this->BlockInfo[c][r].scale[0] / 70,
552                                                this->BlockInfo[c][r].scale[1] / 40, 0);
553
554                        }
555                }
556
557        }
558}
559
560
561void AnimViewer::AdjustLevelMovement(int direction, int axis) {
562
563        PanAmount = this->BlockInfo[0][0].scale[axis] / PANSUBDIVISIONS;
564
565}
566
567
568void AnimViewer::InitViewer(char* name) {
569
570        ifstream file(name);
571
572        if (!file.is_open()) {
573                printf("Can not read dat file.\n");
574                exit(0);
575    }
576
577
578        file >> sageStr >> appname >> xWindowPos >> yWindowPos >>
579                         this->GlobalDimensions[0] >> this->GlobalDimensions[1]
580                 >> this->BlockDimensions[0] >> this->BlockDimensions[1]
581                 >> this->NumOfLevels >> this->AnimDir >> this->Colorspace;
582
583        if(!strcmp(sageStr.c_str(), "SAGE")) {
584                SAGE = true;
585                printf("Render to sage.   %s\n", sageStr.c_str());
586        }
587        else {
588
589                SAGE == false;
590                printf("No sage  %s\n", sageStr.c_str());
591        }
592
593
594        end = this->NumOfLevels;
595        MODE = this->Colorspace;
596
597        this->NumOfCols = this->GetNumOfCols(GlobalDimensions[0], BlockDimensions[0]);
598        this->NumOfRows = this->GetNumOfRows(GlobalDimensions[1], BlockDimensions[1]);
599
600        printf("Columns = %i Rows = %i \n", this->NumOfCols, this->NumOfRows);
601
602        this->Blank =
603                new unsigned char[this->BlockDimensions[0] *
604                                                this->BlockDimensions[1] * MODE];
605
606        for(int i=0; i < this->BlockDimensions[0] *
607                        this->BlockDimensions[1] * MODE; i++) {
608                this->Blank[i] = (unsigned char) 0;
609        }
610
611        memset(this->Blank,
612                0, this->BlockDimensions[0] * this->BlockDimensions[1] * MODE);
613
614        /*
615        printf("------------------------\n");
616        printf("Global dim: %i %i\nBlock dim: %i %i\nNum of levels: %i\n",
617                this->GlobalDimensions[0], this->GlobalDimensions[1],
618                this->BlockDimensions[0], this->BlockDimensions[1],
619                this->NumOfLevels);
620        printf("------------------------\n");
621        */
622}
623
624
625void AnimViewer::PanLeft() {
626        PointerPosition[0] -= PanAmount;
627        pointer->SetPosition(PointerPosition[0], PointerPosition[1], 0);
628        //CamPosition[0] -= PanAmount;
629        //Fpoint[0] -= PanAmount;
630        //UpdateCamera(CamPosition, Fpoint);
631}
632
633void AnimViewer::PanRight() {
634
635        PointerPosition[0] += PanAmount;
636        pointer->SetPosition(PointerPosition[0], PointerPosition[1], 0);
637
638        //CamPosition[0] += PanAmount;
639        //Fpoint[0] += PanAmount;
640        //UpdateCamera(CamPosition, Fpoint);
641}
642
643void AnimViewer::PanUp() {
644
645        PointerPosition[1] += PanAmount;
646        pointer->SetPosition(PointerPosition[0], PointerPosition[1], 0);
647
648        //CamPosition[1] += PanAmount;
649        //Fpoint[1] += PanAmount;
650        //UpdateCamera(CamPosition, Fpoint);
651}
652
653void AnimViewer::PanDown() {
654        PointerPosition[1] -= PanAmount;
655        pointer->SetPosition(PointerPosition[0], PointerPosition[1], 0);
656
657        //CamPosition[1] -= PanAmount;
658        //Fpoint[1] -= PanAmount;
659        //UpdateCamera(CamPosition, Fpoint);
660}
661
662void AnimViewer::UpdateCamera(double c[], double f[]) {
663
664        pointer->SetScale(CamPosition[2]/20, CamPosition[2]/20, 0);
665        pointer->SetPosition(CamPosition[0], CamPosition[1], 0);
666
667        PointerPosition[0] = CamPosition[0];
668        PointerPosition[1] = CamPosition[1];
669
670
671        renA->GetActiveCamera()->SetPosition(c);
672        renA->GetActiveCamera()->SetFocalPoint(f);
673
674        if(ScreensPerNode > 1) {
675                renB->GetActiveCamera()->SetPosition(c);
676                renB->GetActiveCamera()->SetFocalPoint(f);
677        }
678}
679
680
681
682int AnimViewer::BlockOutOfBounds(long c, long r, long cols, long rows) {
683        //printf("Available cols and rows: %i %i\n", cols, rows);
684
685        if(c >= 0 && c < cols && r >= 0 && r < rows) {
686                printf("Accepted %i %i\n", c, r);
687                return ACCEPT;
688        }
689        else {
690                printf("This one is off: %i %i\n", c, r);
691                if( c < 0 )
692                        return RIGHT;
693                if( c >= cols )
694                        return LEFT;
695                if( r < 0 )
696                        return UP;
697                if( r >= rows )
698                        return DOWN;
699        }
700
701}
702
703void AnimViewer::CreateZoomBounds() {
704
705        ZoomBounds = new ZBound[this->StartingLevel + 1];
706        int i;
707        double Z = StartingZ;
708        for(i = this->StartingLevel; i >= 0; i--) {
709                ZoomBounds[i].bound = Z;
710                Z /=2;
711        }
712
713        this->NumOfZBounds = this->StartingLevel + 1;
714}
715
716void AnimViewer::GetZProjection(double zpos) {
717
718        for(int i = 0 ; i < this->NumOfZBounds; i++) {
719                if(ZoomBounds[i].bound >= zpos) {
720                        CamProjection.l = i;
721                        cout<<i<<endl;
722                        return;
723                }
724        }
725}
726
727void AnimViewer::SetInitialCamPosition() {
728        long cols = this->NumOfCols;
729        long rows = this->NumOfRows;
730
731
732        CamPosition[0] = this->BlockInfo[cols/2][rows/2].position[0] -
733                        this->BlockInfo[0][0].scale[0]/2;
734        CamPosition[1] = -this->BlockInfo[cols/2][rows/2].position[1] +
735                        this->BlockInfo[0][0].scale[1]/2;
736
737
738        Fpoint[0] = CamPosition[0];
739        Fpoint[1] = CamPosition[1];
740
741
742        CamPosition[2] = (cols * this->BlockInfo[0][0].scale[0]) * 0.3;
743        //CamPosition[2] = 3.2;
744}
745
746char* WaitForMessage()
747{
748        if(recmsg == NULL)
749                recmsg = new char[TRANSMISSION_SIZE];
750        memset(recmsg,0,TRANSMISSION_SIZE);
751        static int size = TRANSMISSION_SIZE;
752
753        if(client == NULL && server != NULL)
754        {
755                fprintf(stderr,"Waiting For Client To Connect.\n");
756                client = server->waitForNewConnection();
757                fprintf(stderr,"Ready For Messages.\n");
758
759
760                int size;
761                size = sizeof(float) * 3;
762                float array[3];
763                array[0] = CamPosition[0];
764                array[1] = CamPosition[1];
765                array[2] = CamPosition[2];
766                client->write((const char*)array,&size,QUANTAnet_tcpClient_c::BLOCKING);
767        }
768
769        if(client)
770        {
771                int status;
772                status = client->read(recmsg,&size,QUANTAnet_tcpClient_c::NON_BLOCKING);
773            if (status == QUANTAnet_tcpClient_c::OK)
774                        return recmsg;
775                else
776                        {
777                                if (status == QUANTAnet_tcpClient_c::NON_BLOCKING_HAS_NO_DATA) usleep(1000);
778                                return NULL;
779                        }
780        }
781
782        return NULL;
783}
784
785void AnimViewer::SetTimeStep(int direction) {
786        int step = CurrentLevel + direction;
787
788        if(step == this->NumOfLevels) {
789                step = 0;
790        }
791
792        if(step >= 0 && step < this->NumOfLevels) {
793                this->CleanUpMainWindow();
794
795                CurrentLevel = step;
796                this->QueryWindow.level = CurrentLevel;
797                cout<<"TIMESTEP *************"<<step<<endl;;
798        }
799}
800
801void AnimViewer::SetCurrentLevel(int frame) {
802        CurrentLevel = frame;
803        this->QueryWindow.level = CurrentLevel;
804}
805
806
807
808void vtkWindowUp(int resx, int resy, double cw0, double ch0, double cw1, double ch1) {
809
810        culler->SetMinimumCoverage(0.0);
811        culler->SetMaximumCoverage(0.0);
812        culler->SetSortingStyleToFrontToBack();
813
814        light->SetLightTypeToSceneLight();
815        light->SetPosition(0,0,10);
816
817        renA->AddLight(light);
818        renA->SetBackground(0,0,0);
819
820        renB->AddLight(light);
821        renB->SetBackground(0,0,0);
822
823        if(rank > 0) {
824                if(resx > resy) {
825                        renA->SetViewport(0,0,0.5,1);
826                        renB->SetViewport(0.5,0,1,1);
827                }
828                else {
829
830                        renA->SetViewport(0,0,1,0.5);
831                        renB->SetViewport(0,0.5,1,1);
832                }
833
834                renA->GetActiveCamera()->SetWindowCenter(cw0, ch0);
835                renB->GetActiveCamera()->SetWindowCenter(cw1, ch1);
836        }
837
838        renA->AddCuller(culler);
839        renB->AddCuller(culler);
840
841        renWin->AddRenderer(renA);
842        renWin->AddRenderer(renB);
843
844        //renA->ResetCamera();
845        //renB->ResetCamera();
846
847        renWin->BordersOff();
848        renWin->SetSize(resx, resy);
849
850
851}
852
853
854
855void vtkWindowUp(int resx, int resy, double cw, double ch) {
856
857        culler->SetMinimumCoverage(0.0);
858        culler->SetMaximumCoverage(0.0);
859        culler->SetSortingStyleToFrontToBack();
860
861        light->SetLightTypeToSceneLight();
862        light->SetPosition(0,0,10);
863
864        renA->AddLight(light);
865        renA->SetBackground(0,0,0);
866
867        if(rank > 0)
868                renA->GetActiveCamera()->SetWindowCenter(cw, ch);
869
870        renA->AddCuller(culler);
871
872        renWin->AddRenderer(renA);
873
874        //renA->ResetCamera();
875
876        renWin->BordersOff();
877        renWin->SetSize(resx, resy );
878}
879
880// Bad way to figure out location of global image center
881// BAD!!!!!!!! BAD!!!!!!!!??? BAD!!!!!!!!
882void UpCam() {
883
884        renWin->AddRenderer(renA);
885        renA->ResetCamera();
886
887        if(ScreensPerNode > 1) {
888                renWin->AddRenderer(renB);
889                renB->ResetCamera();
890        }
891}
892
893
894
895int main(int argc, char* argv[]) {
896
897        char temp[HOST_NAME_MAX];
898        gethostname(temp, HOST_NAME_MAX);
899        hostname = temp;
900        printf("Hostname: %s \n", hostname);
901
902        if(rank == 0) {
903
904                PORT = atoi(argv[3]);
905        }
906
907
908
909        unsigned char* windowBuf = NULL;
910
911        MPI_Datatype action;
912        MPI_Datatype position;
913
914        // MPI Initialization
915        MPI_Init(&argc, &argv);
916        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
917        MPI_Comm_size(MPI_COMM_WORLD, &size);
918
919        MPI_Type_contiguous(1, MPI_LONG, &action);
920        MPI_Type_contiguous(3, MPI_DOUBLE, &position);
921        MPI_Type_commit(&position);
922
923
924        animviewer.InitViewer(argv[2]);
925        printf("App name: %s\n",appname.c_str());
926        printf("X: %i Y: %i\n", xWindowPos, yWindowPos);
927
928
929        // SAGE --------------------------------------------------------------------
930
931        VirtualDesktop *vd = new VirtualDesktop();
932        PhysicalDisplay *pd = new PhysicalDisplay();
933        DisplayCluster *dc = new DisplayCluster();
934
935        if(rank > 0) {
936
937                int retcode;
938                retcode = stdInputFileParser( argv[1], pd, dc, vd);
939                if( retcode < 0 )
940                {
941                        cout << "Node " << rank << " Failed to parse config file!" << endl;
942                        return 1;
943                }
944
945                //find the display node that matches our hostname
946                char hostname[128] = "";
947                if( gethostname( hostname, 128 ) )
948                {
949                        cout << "Node " << rank << " Could not retrieve hostname!" << endl;
950                        return 1;
951                }
952
953                Display_Node* dn = dc->getFirstNode();
954
955                while( dn && strcmp( dn->Name(), hostname ))
956                {
957                        dn = dn->Next();
958                }
959
960                if( !dn || dn->NumberOfMaps() <= 0)
961                {
962                        cout << "Node " << rank << "Could not match a display node with hostname: "
963                                << hostname << endl;
964                        return 1;
965                }
966
967                //assume only one mapping per node for the app
968                VDTtoPTmapper** maps = dn->Maps();
969
970                // initialize SAGE
971                sailConfig scfg;
972
973//#ifdef SAGE
974if(SAGE == true) {
975                //sailConfig scfg;
976                scfg.init("mframes.conf");
977                cout<<"~~~~~~~~~~~~~~"<<endl;
978                scfg.setAppName((char*)appname.c_str());
979                scfg.rank = rank-1;
980                scfg.nwID = 1;
981                scfg.resX = maps[0]->VirtualDesktopTile()->WidthInPixels();
982                scfg.resY = maps[0]->VirtualDesktopTile()->HeightInPixels();
983
984                sageRect renderImageMap;
985                renderImageMap.left = maps[0]->VirtualDesktopTile()->LowerLeftX();
986                renderImageMap.bottom = maps[0]->VirtualDesktopTile()->LowerLeftY();
987                renderImageMap.top = maps[0]->VirtualDesktopTile()->LowerLeftY() +
988                        maps[0]->VirtualDesktopTile()->HeightRatio();
989                renderImageMap.right = maps[0]->VirtualDesktopTile()->LowerLeftX() +
990                        maps[0]->VirtualDesktopTile()->WidthRatio();
991
992                scfg.imageMap = renderImageMap;
993                scfg.pixFmt = PIXFMT_888;
994                scfg.rowOrd = BOTTOM_TO_TOP;
995
996                cout << "NODE: " << rank << " = w: " << scfg.resX
997                        << " h: " << scfg.resY
998                        << " coords: " << renderImageMap.left << ", "
999                        << renderImageMap.top << " to "
1000                        << renderImageMap.right << ", "
1001                        << renderImageMap.bottom << endl;
1002
1003}
1004//#endif
1005                int resX = window_w = maps[0]->VirtualDesktopTile()->WidthInPixels();
1006                int resY = window_h = maps[0]->VirtualDesktopTile()->HeightInPixels();
1007
1008
1009//#ifdef SAGE
1010if(SAGE == true) {
1011                sageInf.init(scfg);
1012                windowBuf = (unsigned char*)sageInf.getBuffer();
1013                cout << "sail initialized " << endl;
1014}
1015//#endif
1016
1017
1018                cout << "NODE " << rank << " WAITING TO QUIT" << endl;
1019
1020                // Viewport window center calculations
1021                cw0 = - (vd->HorizRes() - 2*vd->HorizRes() *
1022                                        maps[0]->VirtualDesktopTile()->LowerLeftX()) /
1023                                        maps[0]->VirtualDesktopTile()->WidthInPixels() +
1024                                        (1.0 - maps[0]->PhysicalTile()->LeftMullionThickness() / 10);
1025
1026                ch0 = - (vd->VertRes() - 2*vd->VertRes() *
1027                                        maps[0]->VirtualDesktopTile()->LowerLeftY()) /
1028                                        maps[0]->VirtualDesktopTile()->HeightInPixels() +
1029                                        (1.0 - maps[0]->PhysicalTile()->BottomMullionThickness() / 10);
1030
1031
1032                if(dn->NumberOfMaps() > 1) {
1033                                ScreensPerNode = 2;
1034
1035                                cw1 = - (vd->HorizRes() - 2*vd->HorizRes() *
1036                                        maps[1]->VirtualDesktopTile()->LowerLeftX()) /
1037                                        maps[1]->VirtualDesktopTile()->WidthInPixels() +
1038                                        (1.0 - maps[1]->PhysicalTile()->LeftMullionThickness() / 10);
1039
1040                                ch1 = - (vd->VertRes() - 2*vd->VertRes() *
1041                                        maps[1]->VirtualDesktopTile()->LowerLeftY()) /
1042                                        maps[1]->VirtualDesktopTile()->HeightInPixels() +
1043                                        (1.0 - maps[1]->PhysicalTile()->BottomMullionThickness() / 10);
1044
1045
1046                                if(maps[0]->VirtualDesktopTile()->LowerLeftY() ==
1047                                                        maps[1]->VirtualDesktopTile()->LowerLeftY())
1048                                        vtkWindowUp(resX*2, resY, cw0, ch0, cw1, ch1);
1049                                else
1050                                        vtkWindowUp(resX, resY*2, cw0, ch0, cw1, ch1);
1051                }
1052
1053                else {
1054
1055                        vtkWindowUp(resX, resY, cw0, ch0);
1056                }
1057        }
1058
1059        // SAGE --------------------------------------------------------------------
1060
1061        //UpCam();
1062
1063
1064        renWin->SetPosition(xWindowPos, yWindowPos);
1065
1066        animviewer.GetAnimFiles();
1067        animviewer.InitRendering();
1068        animviewer.SetUpPipeline();
1069
1070        animviewer.PositionAndScaleBlocks();
1071        animviewer.ImportQueryWindowPixels();
1072        animviewer.GivePixelsToVTK();
1073
1074
1075        animviewer.SetInitialCamPosition();
1076        /*
1077        renA->InteractiveOff();
1078        renA->GetActiveCamera()->GetPosition(CamPosition);
1079        renA->GetActiveCamera()->SetClippingRange(-500,500);
1080        renA->GetActiveCamera()->GetFocalPoint(Fpoint);
1081
1082
1083
1084        if(ScreensPerNode > 1) {
1085
1086                renB->InteractiveOff();
1087                //renB->GetActiveCamera()->GetPosition(CamPosition);
1088                renB->GetActiveCamera()->SetClippingRange(-500,500);
1089                //renB->GetActiveCamera()->GetFocalPoint(Fpoint);
1090        }
1091
1092        CamPosition[2] = 3.2;
1093
1094        */
1095
1096        StartingZ = CamPosition[2];
1097        animviewer.UpdateCamera(CamPosition, Fpoint);
1098
1099
1100        if(rank == 0) {
1101
1102
1103                QUANTAinit();
1104                server = new QUANTAnet_tcpServer_c();
1105                client = NULL;
1106                recmsg = NULL;
1107
1108                if( server->init(PORT) <  0)
1109                {
1110                        printf("Bad port: %i", PORT);
1111                        server->close();
1112                        delete server;
1113                        server = NULL;
1114                        exit(0);
1115                }
1116
1117                printf("Running on port %i\n", PORT);
1118
1119
1120                while(true) {
1121
1122                        uiaction = -100;
1123
1124                        char* msg = WaitForMessage();
1125
1126                        if (msg != NULL)
1127                        {
1128                                printf("%s\n", msg);
1129
1130                                if(!strcmp(msg, SLIDER1)) {
1131                                        uiaction = 0;
1132
1133                                        static int readsize = sizeof(float) * 1;
1134                                        float readarray[1];
1135                                        if( client ) {
1136                                                client->read((char*)readarray,&readsize,QUANTAnet_tcpClient_c::BLOCKING);
1137                                        }
1138
1139                                        pos[0] = readarray[0];
1140                                }
1141
1142                                else if(!strcmp(msg, SLIDER2)) {
1143                                        uiaction = 1;
1144
1145                                        static int readsize = sizeof(float) * 1;
1146                                        float readarray[1];
1147                                        if( client ) {
1148                                                client->read((char*)readarray,&readsize,QUANTAnet_tcpClient_c::BLOCKING);
1149                                        }
1150
1151                                        pos[1] = readarray[0];
1152                                }
1153
1154                                else if(!strcmp(msg, SLIDER3)) {
1155                                        uiaction = 2;
1156
1157                                        static int readsize = sizeof(float) * 1;
1158                                        float readarray[1];
1159                                        if( client ) {
1160                                                client->read((char*)readarray,&readsize,QUANTAnet_tcpClient_c::BLOCKING);
1161                                        }
1162
1163                                        pos[2] = readarray[0];
1164                                }
1165
1166                                else if(!strcmp(msg, RELEASE)) {
1167                                        uiaction = 3;
1168                                }
1169
1170                                else if(!strcmp(msg, PRESS)) {
1171                                        uiaction = 4;
1172                                }
1173
1174                                else if(!strcmp(msg, BROWSELEFT)) {
1175                                        uiaction = 5;
1176                                }
1177
1178                                else if(!strcmp(msg, BROWSERIGHT)) {
1179                                        uiaction = 6;
1180                                }
1181
1182                                else if(!strcmp(msg, PANLEFT)) {
1183                                        uiaction = 7;
1184                                }
1185
1186                                else if(!strcmp(msg, PANRIGHT)) {
1187                                        uiaction = 8;
1188                                }
1189
1190                                else if(!strcmp(msg, PANUP)) {
1191                                        uiaction = 9;
1192                                }
1193
1194                                else if(!strcmp(msg, PANDOWN)) {
1195                                        uiaction = 10;
1196                                }
1197
1198                                else if(!strcmp(msg, QUIT)) {
1199                                        if(client)
1200                                                client->close();
1201
1202                                        if(server)
1203                                                server->close();
1204
1205                                        if(client)
1206                                                delete client;
1207
1208                                        if(server)
1209                                                delete server;
1210
1211                                        if(msg)
1212                                                delete [] msg;
1213
1214                                        uiaction = 11;
1215                                }
1216
1217                                else if(!strcmp(msg, CENTER)) {
1218                                        uiaction = 12;
1219                                }
1220
1221                        }
1222
1223                        for (int i = 0 ; i < size - 1 ; i++) {
1224                                MPI_Send(&uiaction, 1, action, i+1, 1, MPI_COMM_WORLD);
1225                        }
1226
1227                        for (int i = 0 ; i < size - 1 ; i++) {
1228                                MPI_Send(&pos, 1, position, i+1, 1, MPI_COMM_WORLD);
1229                        }
1230
1231                        if(uiaction == 11) break;
1232
1233                        // synchronize
1234                        MPI_Barrier(MPI_COMM_WORLD);
1235                }
1236
1237        }
1238
1239        else if (rank > 0) {
1240                // MPI status variable
1241        MPI_Status status;
1242                MPI_Status status1;
1243
1244                animviewer.CreateZoomBounds();
1245
1246                cout<<"Cam "<<CamPosition[0]<<" "<<CamPosition[1]
1247                                        <<" "<<CamPosition[2]<<endl;
1248
1249                /*
1250                if(rank == 5) {
1251                        renA->AddActor2D(text);
1252                        renWin->Render();
1253                }
1254
1255                int preload = 0;
1256                if(end < 20) preload = end;
1257                else preload = 20;
1258
1259                for(int i=start; i<preload; i++) {
1260                        animviewer.SetTimeStep(1);
1261                        animviewer.ImportQueryWindowPixels();
1262                        animviewer.GivePixelsToVTK();
1263                        renWin->Render();
1264                }
1265
1266                if(rank == 5) {
1267                        renA->RemoveActor2D(text);
1268                }
1269                */
1270
1271                pointer->SetPosition(CamPosition[0], CamPosition[1], 0);
1272                renA->AddActor(pointer);
1273                if(ScreensPerNode > 1) renB->AddActor(pointer);
1274
1275                pointer->SetScale(CamPosition[2]/20, CamPosition[2]/20, 0);
1276
1277
1278                // Reset to first frame
1279                animviewer.SetCurrentLevel(start);
1280                //animviewer.ImportQueryWindowPixels();
1281                //animviewer.GivePixelsToVTK();
1282                renWin->Render();
1283
1284                while(true) {
1285
1286                        MPI_Recv(&uiaction, 1, action, 0, 1, MPI_COMM_WORLD, &status);
1287
1288                        MPI_Recv(&pos, 1, position, 0, 1, MPI_COMM_WORLD, &status1);
1289
1290                        if(uiaction == 0) {
1291
1292                                CamPosition[0] = pos[0];
1293                                Fpoint[0] = pos[0];
1294
1295                                animviewer.UpdateCamera(CamPosition, Fpoint);
1296
1297                        }
1298
1299                        if(uiaction == 1) {
1300
1301                                CamPosition[1] = pos[1];
1302                                Fpoint[1] = pos[1];
1303
1304                                animviewer.UpdateCamera(CamPosition, Fpoint);
1305
1306                        }
1307
1308                        if(uiaction == 2) {
1309                                if(pos[2] > 0) {
1310                                        CamPosition[2] = pos[2];
1311
1312                                        animviewer.UpdateCamera(CamPosition, Fpoint);
1313                                }
1314                        }
1315
1316                        if(uiaction == 5) {
1317
1318                                animviewer.SetTimeStep(-1);
1319
1320                                //if(rank == 5) {
1321                                //      renA->AddActor2D(text);
1322                                //      renWin->Render();
1323                                //}
1324
1325                                animviewer.ImportQueryWindowPixels();
1326                                animviewer.GivePixelsToVTK();
1327
1328                                //if(rank == 5) {
1329                                //      renA->RemoveActor2D(text);
1330                                //}
1331
1332                        }
1333
1334                        if(uiaction == 6) {
1335
1336                                animviewer.SetTimeStep(1);
1337
1338                                //if(rank == 5) {
1339                                //      renA->AddActor2D(text);
1340                                //      renWin->Render();
1341                                //}
1342
1343                                animviewer.ImportQueryWindowPixels();
1344                                animviewer.GivePixelsToVTK();
1345
1346                                //if(rank == 5) {
1347                                //      renA->RemoveActor2D(text);
1348                                //}
1349
1350                        }
1351
1352
1353                        if(uiaction == 7) {
1354
1355                                animviewer.AdjustLevelMovement(LEFT,X);
1356
1357                                animviewer.PanLeft();
1358
1359
1360                        }
1361
1362                        if(uiaction == 8) {
1363
1364                                animviewer.AdjustLevelMovement(RIGHT,X);
1365
1366                                animviewer.PanRight();
1367
1368
1369                        }
1370
1371                        if(uiaction == 10) {
1372
1373
1374                                animviewer.AdjustLevelMovement(DOWN,Y);
1375
1376                                animviewer.PanDown();
1377
1378                        }
1379
1380                        if(uiaction == 9) {
1381
1382                                animviewer.AdjustLevelMovement(UP,Y);
1383
1384                                animviewer.PanUp();
1385
1386                        }
1387
1388
1389                        if(uiaction == 3) {
1390
1391                        }
1392
1393                        if(uiaction == 4) {
1394                                //animviewer.CleanUpMainWindow();
1395                        }
1396
1397                        if(uiaction == 12) {
1398                                CamPosition[0] = PointerPosition[0];
1399                                CamPosition[1] = PointerPosition[1];
1400                                Fpoint[0] = PointerPosition[0];
1401                                Fpoint[1] = PointerPosition[1];
1402
1403                                animviewer.UpdateCamera(CamPosition, Fpoint);
1404
1405
1406                        }
1407
1408                        if(uiaction == 11) {
1409                                if(SAGE == true) {
1410                                        //if (windowBuf) delete [] windowBuf;
1411                                }
1412                                break;
1413                        }
1414
1415
1416
1417
1418                        renWin->Render();
1419//#ifdef SAGE
1420                        if(SAGE == true) {
1421
1422                        //if (windowBuf) delete [] windowBuf;
1423
1424
1425                        memcpy(windowBuf, renWin->GetPixelData(0, 0, window_w-1, window_h-1, 1), window_w*window_h*3);
1426}
1427//#endif
1428
1429                        // synchronize
1430                        MPI_Barrier(MPI_COMM_WORLD);
1431//#ifdef SAGE
1432if(SAGE == true) {
1433                        sageInf.swapBuffer();
1434                        windowBuf = (unsigned char *)sageInf.getBuffer();
1435}
1436//#endif
1437
1438                }
1439        }
1440
1441        // finalize
1442        MPI_Finalize();
1443
1444        renA->Delete();
1445        renB->Delete();
1446        renWin->Delete();
1447        light->Delete();
1448        culler->Delete();
1449        text->Delete();
1450
1451        cone->Delete();
1452        cube->Delete();
1453        sphere->Delete();
1454        sphereMapper->Delete();
1455        pointer->Delete();
1456
1457        splash->Delete();
1458        splashMapper->Delete();
1459        splashScreen->Delete();
1460
1461        // exit
1462        return EXIT_SUCCESS;
1463}
Note: See TracBrowser for help on using the repository browser.