-
Notifications
You must be signed in to change notification settings - Fork 20
/
Visualizer.cpp
1414 lines (1196 loc) · 45.6 KB
/
Visualizer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************
Visualizer - Test application for the new visualization component
framework.
Copyright (c) 2005-2017 Oliver Kreylos
This file is part of the 3D Data Visualizer (Visualizer).
The 3D Data Visualizer is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
The 3D Data Visualizer is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the 3D Data Visualizer; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include "Visualizer.h"
#include <ctype.h>
#include <string.h>
#include <stdexcept>
#include <vector>
#include <iostream>
#include <string>
#include <Misc/ThrowStdErr.h>
#include <Misc/Timer.h>
#include <Misc/StandardMarshallers.h>
#include <Misc/FileNameExtensions.h>
#include <Misc/CreateNumberedFileName.h>
#include <Misc/StandardValueCoders.h>
#include <Misc/ConfigurationFile.h>
#include <IO/File.h>
#include <IO/OpenFile.h>
#include <IO/ValueSource.h>
#include <Cluster/MulticastPipe.h>
#include <Geometry/OrthogonalTransformation.h>
#include <GL/gl.h>
#include <GL/GLColorTemplates.h>
#include <GL/GLVertexTemplates.h>
#include <GL/GLGeometryWrappers.h>
#include <GLMotif/WidgetManager.h>
#include <GLMotif/StyleSheet.h>
#include <GLMotif/PopupMenu.h>
#include <GLMotif/PopupWindow.h>
#include <GLMotif/RowColumn.h>
#include <GLMotif/Separator.h>
#include <GLMotif/Label.h>
#include <GLMotif/TextField.h>
#include <GLMotif/Button.h>
#include <GLMotif/CascadeButton.h>
#include <SceneGraph/GLRenderState.h>
#include <SceneGraph/NodeCreator.h>
#include <SceneGraph/VRMLFile.h>
#include <Vrui/Vrui.h>
#include <Vrui/CoordinateManager.h>
#include <Vrui/OpenFile.h>
#include <Vrui/SceneGraphSupport.h>
#ifdef VISUALIZER_USE_COLLABORATION
#include <Collaboration/CollaborationClient.h>
#endif
#include <Abstract/DataSetRenderer.h>
#include <Abstract/CoordinateTransformer.h>
#include <Abstract/VariableManager.h>
#include <Abstract/Parameters.h>
#include <Abstract/BinaryParametersSink.h>
#include <Abstract/BinaryParametersSource.h>
#include <Abstract/FileParametersSource.h>
#include <Abstract/ConfigurationFileParametersSource.h>
#include <Abstract/Algorithm.h>
#include <Abstract/Element.h>
#include <Abstract/Module.h>
#include "CuttingPlane.h"
#ifdef VISUALIZER_USE_COLLABORATION
#include "SharedVisualizationClient.h"
#endif
#include "BaseLocator.h"
#include "CuttingPlaneLocator.h"
#include "ScalarEvaluationLocator.h"
#include "VectorEvaluationLocator.h"
#include "ExtractorLocator.h"
#include "ElementList.h"
#include "GLRenderState.h"
/***************************
Methods of class Visualizer:
***************************/
GLMotif::PopupMenu* Visualizer::createRenderingModesMenu(void)
{
GLMotif::PopupMenu* renderingModesMenuPopup=new GLMotif::PopupMenu("RenderingModesMenuPopup",Vrui::getWidgetManager());
GLMotif::Menu* renderingModesMenu=new GLMotif::Menu("RenderingModesMenu",renderingModesMenuPopup,false);
GLMotif::RadioBox* renderingModes=new GLMotif::RadioBox("RenderingModes",renderingModesMenu,false);
renderingModes->setSelectionMode(GLMotif::RadioBox::ATMOST_ONE);
int numRenderingModes=dataSetRenderer->getNumRenderingModes();
for(int i=0;i<numRenderingModes;++i)
renderingModes->addToggle(dataSetRenderer->getRenderingModeName(i));
if(renderDataSet)
renderingModes->setSelectedToggle(dataSetRenderer->getRenderingMode());
renderingModes->getValueChangedCallbacks().add(this,&Visualizer::changeRenderingModeCallback);
renderingModes->manageChild();
if(!sceneGraphs.empty())
{
new GLMotif::Separator("SceneGraphsSeparator",renderingModesMenu,GLMotif::Separator::HORIZONTAL,0.0f,GLMotif::Separator::LOWERED);
/* Create a set of toggle buttons to enable/disable individual additional scene graphs: */
int i=0;
for(std::vector<SG>::iterator sgIt=sceneGraphs.begin();sgIt!=sceneGraphs.end();++sgIt,++i)
{
char sgName[40];
snprintf(sgName,sizeof(sgName),"SceneGraph%d",i+1);
GLMotif::ToggleButton* sgToggle=new GLMotif::ToggleButton(sgName,renderingModesMenu,sgIt->name.c_str());
sgToggle->setToggle(sgIt->render);
sgToggle->getValueChangedCallbacks().add(this,&Visualizer::toggleSceneGraphCallback,i);
}
}
renderingModesMenu->manageChild();
return renderingModesMenuPopup;
}
GLMotif::PopupMenu* Visualizer::createScalarVariablesMenu(void)
{
GLMotif::PopupMenu* scalarVariablesMenuPopup=new GLMotif::PopupMenu("ScalarVariablesMenuPopup",Vrui::getWidgetManager());
GLMotif::Menu* scalarVariablesMenu=new GLMotif::Menu("ScalarVariablesMenu",scalarVariablesMenuPopup,false);
GLMotif::RadioBox* scalarVariables=new GLMotif::RadioBox("ScalarVariables",scalarVariablesMenu,false);
scalarVariables->setSelectionMode(GLMotif::RadioBox::ALWAYS_ONE);
for(int i=0;i<variableManager->getNumScalarVariables();++i)
scalarVariables->addToggle(variableManager->getScalarVariableName(i));
scalarVariables->setSelectedToggle(variableManager->getCurrentScalarVariable());
scalarVariables->getValueChangedCallbacks().add(this,&Visualizer::changeScalarVariableCallback);
scalarVariables->manageChild();
scalarVariablesMenu->manageChild();
return scalarVariablesMenuPopup;
}
GLMotif::PopupMenu* Visualizer::createVectorVariablesMenu(void)
{
GLMotif::PopupMenu* vectorVariablesMenuPopup=new GLMotif::PopupMenu("VectorVariablesMenuPopup",Vrui::getWidgetManager());
GLMotif::Menu* vectorVariablesMenu=new GLMotif::Menu("VectorVariablesMenu",vectorVariablesMenuPopup,false);
GLMotif::RadioBox* vectorVariables=new GLMotif::RadioBox("VectorVariables",vectorVariablesMenu,false);
vectorVariables->setSelectionMode(GLMotif::RadioBox::ALWAYS_ONE);
for(int i=0;i<variableManager->getNumVectorVariables();++i)
vectorVariables->addToggle(variableManager->getVectorVariableName(i));
vectorVariables->setSelectedToggle(variableManager->getCurrentVectorVariable());
vectorVariables->getValueChangedCallbacks().add(this,&Visualizer::changeVectorVariableCallback);
vectorVariables->manageChild();
vectorVariablesMenu->manageChild();
return vectorVariablesMenuPopup;
}
GLMotif::PopupMenu* Visualizer::createAlgorithmsMenu(void)
{
GLMotif::PopupMenu* algorithmsMenuPopup=new GLMotif::PopupMenu("AlgorithmsMenuPopup",Vrui::getWidgetManager());
GLMotif::Menu* algorithmsMenu=new GLMotif::Menu("AlgorithmsMenu",algorithmsMenuPopup,false);
GLMotif::RadioBox* algorithms=new GLMotif::RadioBox("Algorithms",algorithmsMenu,false);
algorithms->setSelectionMode(GLMotif::RadioBox::ALWAYS_ONE);
/* Add the cutting plane algorithm: */
int algorithmIndex=0;
algorithms->addToggle("Cutting Plane");
++algorithmIndex;
if(variableManager->getNumScalarVariables()>0)
{
/* Add the scalar evaluator algorithm: */
algorithms->addToggle("Evaluate Scalars");
++algorithmIndex;
/* Add scalar algorithms: */
firstScalarAlgorithmIndex=algorithmIndex;
for(int i=0;i<module->getNumScalarAlgorithms();++i)
{
algorithms->addToggle(module->getScalarAlgorithmName(i));
++algorithmIndex;
}
}
if(variableManager->getNumVectorVariables()>0)
{
/* Add the vector evaluator algorithm: */
algorithms->addToggle("Evaluate Vectors");
++algorithmIndex;
/* Add vector algorithms: */
firstVectorAlgorithmIndex=algorithmIndex;
for(int i=0;i<module->getNumVectorAlgorithms();++i)
{
algorithms->addToggle(module->getVectorAlgorithmName(i));
++algorithmIndex;
}
}
algorithms->setSelectedToggle(algorithm);
algorithms->getValueChangedCallbacks().add(this,&Visualizer::changeAlgorithmCallback);
algorithms->manageChild();
algorithmsMenu->manageChild();
return algorithmsMenuPopup;
}
GLMotif::PopupMenu* Visualizer::createElementsMenu(void)
{
GLMotif::PopupMenu* elementsMenuPopup=new GLMotif::PopupMenu("ElementsMenuPopup",Vrui::getWidgetManager());
/* Create the elements menu: */
GLMotif::Menu* elementsMenu=new GLMotif::Menu("ElementsMenu",elementsMenuPopup,false);
showElementListToggle=new GLMotif::ToggleButton("ShowElementListToggle",elementsMenu,"Show Element List");
showElementListToggle->getValueChangedCallbacks().add(this,&Visualizer::showElementListCallback);
GLMotif::Button* loadElementsButton=new GLMotif::Button("LoadElementsButton",elementsMenu,"Load Visualization Elements");
loadElementsButton->getSelectCallbacks().add(this,&Visualizer::loadElementsCallback);
GLMotif::Button* saveElementsButton=new GLMotif::Button("SaveElementsButton",elementsMenu,"Save Visualization Elements");
saveElementsButton->getSelectCallbacks().add(this,&Visualizer::saveElementsCallback);
new GLMotif::Separator("ClearElementsSeparator",elementsMenu,GLMotif::Separator::HORIZONTAL,0.0f,GLMotif::Separator::LOWERED);
GLMotif::Button* clearElementsButton=new GLMotif::Button("ClearElementsButton",elementsMenu,"Clear Visualization Elements");
clearElementsButton->getSelectCallbacks().add(this,&Visualizer::clearElementsCallback);
elementsMenu->manageChild();
return elementsMenuPopup;
}
GLMotif::PopupMenu* Visualizer::createStandardLuminancePalettesMenu(void)
{
GLMotif::PopupMenu* standardLuminancePalettesMenuPopup=new GLMotif::PopupMenu("StandardLuminancePalettesMenuPopup",Vrui::getWidgetManager());
/* Create the palette creation menu and add entries for all standard palettes: */
GLMotif::Menu* standardLuminancePalettes=new GLMotif::Menu("StandardLuminancePalettes",standardLuminancePalettesMenuPopup,false);
standardLuminancePalettes->addEntry("Grey");
standardLuminancePalettes->addEntry("Red");
standardLuminancePalettes->addEntry("Yellow");
standardLuminancePalettes->addEntry("Green");
standardLuminancePalettes->addEntry("Cyan");
standardLuminancePalettes->addEntry("Blue");
standardLuminancePalettes->addEntry("Magenta");
standardLuminancePalettes->getEntrySelectCallbacks().add(this,&Visualizer::createStandardLuminancePaletteCallback);
standardLuminancePalettes->manageChild();
return standardLuminancePalettesMenuPopup;
}
GLMotif::PopupMenu* Visualizer::createStandardSaturationPalettesMenu(void)
{
GLMotif::PopupMenu* standardSaturationPalettesMenuPopup=new GLMotif::PopupMenu("StandardSaturationPalettesMenuPopup",Vrui::getWidgetManager());
/* Create the palette creation menu and add entries for all standard palettes: */
GLMotif::Menu* standardSaturationPalettes=new GLMotif::Menu("StandardSaturationPalettes",standardSaturationPalettesMenuPopup,false);
standardSaturationPalettes->addEntry("Red -> Cyan");
standardSaturationPalettes->addEntry("Yellow -> Blue");
standardSaturationPalettes->addEntry("Green -> Magenta");
standardSaturationPalettes->addEntry("Cyan -> Red");
standardSaturationPalettes->addEntry("Blue -> Yellow");
standardSaturationPalettes->addEntry("Magenta -> Green");
standardSaturationPalettes->addEntry("Rainbow");
standardSaturationPalettes->getEntrySelectCallbacks().add(this,&Visualizer::createStandardSaturationPaletteCallback);
standardSaturationPalettes->manageChild();
return standardSaturationPalettesMenuPopup;
}
GLMotif::PopupMenu* Visualizer::createColorMenu(void)
{
GLMotif::PopupMenu* colorMenuPopup=new GLMotif::PopupMenu("ColorMenuPopup",Vrui::getWidgetManager());
/* Create the color menu and add entries for all standard palettes: */
GLMotif::Menu* colorMenu=new GLMotif::Menu("ColorMenu",colorMenuPopup,false);
GLMotif::CascadeButton* standardLuminancePalettesCascade=new GLMotif::CascadeButton("StandardLuminancePalettesCascade",colorMenu,"Create Luminance Palette");
standardLuminancePalettesCascade->setPopup(createStandardLuminancePalettesMenu());
GLMotif::CascadeButton* standardSaturationPalettesCascade=new GLMotif::CascadeButton("StandardSaturationPalettesCascade",colorMenu,"Create Saturation Palette");
standardSaturationPalettesCascade->setPopup(createStandardSaturationPalettesMenu());
GLMotif::Button* loadPaletteButton=new GLMotif::Button("LoadPaletteButton",colorMenu,"Load Palette File");
loadPaletteButton->getSelectCallbacks().add(this,&Visualizer::loadPaletteCallback);
showColorBarToggle=new GLMotif::ToggleButton("ShowColorBarToggle",colorMenu,"Show Color Bar");
showColorBarToggle->getValueChangedCallbacks().add(this,&Visualizer::showColorBarCallback);
showPaletteEditorToggle=new GLMotif::ToggleButton("ShowPaletteEditorToggle",colorMenu,"Show Palette Editor");
showPaletteEditorToggle->getValueChangedCallbacks().add(this,&Visualizer::showPaletteEditorCallback);
colorMenu->manageChild();
return colorMenuPopup;
}
GLMotif::PopupMenu* Visualizer::createMainMenu(void)
{
GLMotif::PopupMenu* mainMenuPopup=new GLMotif::PopupMenu("MainMenuPopup",Vrui::getWidgetManager());
mainMenuPopup->setTitle("3D Visualizer");
GLMotif::Menu* mainMenu=new GLMotif::Menu("MainMenu",mainMenuPopup,false);
GLMotif::CascadeButton* renderingModesCascade=new GLMotif::CascadeButton("RenderingModesCascade",mainMenu,"Rendering Modes");
renderingModesCascade->setPopup(createRenderingModesMenu());
if(variableManager->getNumScalarVariables()>0)
{
GLMotif::CascadeButton* scalarVariablesCascade=new GLMotif::CascadeButton("ScalarVariablesCascade",mainMenu,"Scalar Variables");
scalarVariablesCascade->setPopup(createScalarVariablesMenu());
}
if(variableManager->getNumVectorVariables()>0)
{
GLMotif::CascadeButton* vectorVariablesCascade=new GLMotif::CascadeButton("VectorVariablesCascade",mainMenu,"Vector Variables");
vectorVariablesCascade->setPopup(createVectorVariablesMenu());
}
GLMotif::CascadeButton* algorithmsCascade=new GLMotif::CascadeButton("AlgorithmsCascade",mainMenu,"Algorithms");
algorithmsCascade->setPopup(createAlgorithmsMenu());
GLMotif::CascadeButton* elementsCascade=new GLMotif::CascadeButton("ElementsCascade",mainMenu,"Elements");
elementsCascade->setPopup(createElementsMenu());
GLMotif::CascadeButton* colorCascade=new GLMotif::CascadeButton("ColorCascade",mainMenu,"Color Maps");
colorCascade->setPopup(createColorMenu());
#ifdef VISUALIZER_USE_COLLABORATION
if(collaborationClient!=0)
{
showClientDialogToggle=new GLMotif::ToggleButton("ShowClientDialogToggle",mainMenu,"Show Client Dialog");
showClientDialogToggle->getValueChangedCallbacks().add(this,&Visualizer::showClientDialogCallback);
}
#endif
mainMenu->manageChild();
return mainMenuPopup;
}
void Visualizer::loadElements(const char* elementFileName,bool ascii)
{
/* Open a pipe for cluster communication: */
Cluster::MulticastPipe* pipe=Vrui::openPipe();
if(pipe==0||pipe->isMaster())
{
/* Create a data sink to send element parameters to the slaves: */
Visualization::Abstract::BinaryParametersSink sink(variableManager,*pipe,true);
if(ascii)
{
/* Open the element file: */
IO::ValueSource elementFile(IO::openFile(elementFileName));
elementFile.setPunctuation("");
elementFile.setQuotes("\"");
elementFile.skipWs();
/* Read all elements from the file: */
while(!elementFile.eof())
{
/* Read the next algorithm name: */
std::string algorithmName=elementFile.readLine();
elementFile.skipWs();
if(pipe!=0)
{
/* Send the algorithm name to the slaves: */
Misc::Marshaller<std::string>::write(algorithmName,*pipe);
pipe->flush(); // Redundant!!!
}
/* Create an extractor for the given name: */
Cluster::MulticastPipe* algorithmPipe=Vrui::openPipe();
Algorithm* algorithm=module->getAlgorithm(algorithmName.c_str(),variableManager,algorithmPipe);
/* Extract an element using the given extractor: */
if(algorithm!=0)
{
std::cout<<"Creating "<<algorithmName<<"..."<<std::flush;
Misc::Timer extractionTimer;
try
{
/* Read the element's extraction parameters from the file: */
Visualization::Abstract::FileParametersSource source(variableManager,elementFile);
Parameters* parameters=algorithm->cloneParameters();
parameters->read(source);
if(pipe!=0)
{
/* Send the extraction parameters to the slaves: */
pipe->write<int>(1);
parameters->write(sink);
pipe->flush();
}
/* Extract the element: */
Element* element=algorithm->createElement(parameters);
/* Store the element: */
elementList->addElement(element,algorithmName.c_str());
}
catch(std::runtime_error err)
{
if(pipe!=0)
{
/* Tell the slaves there was a problem: */
pipe->write<int>(0);
pipe->flush();
}
std::cout<<"Cancelled due to exception "<<err.what()<<"...";
}
/* Destroy the extractor: */
delete algorithm;
extractionTimer.elapse();
std::cout<<" done in "<<extractionTimer.getTime()*1000.0<<" ms"<<std::endl;
}
else
{
std::cout<<"Ignoring unknown algorithm "<<algorithmName<<std::endl;
delete algorithmPipe;
}
}
}
else
{
/* Open the element file and create a data source to read from it: */
IO::FilePtr elementFile(IO::openFile(elementFileName));
elementFile->setEndianness(Misc::LittleEndian);
Visualization::Abstract::BinaryParametersSource source(variableManager,*elementFile,false);
/* Read all elements from the file: */
while(!elementFile->eof())
{
/* Read the next algorithm name: */
std::string algorithmName=Misc::Marshaller<std::string>::read(*elementFile);
if(pipe!=0)
{
/* Send the algorithm name to the slaves: */
Misc::Marshaller<std::string>::write(algorithmName,*pipe);
}
/* Create an extractor for the given name: */
Cluster::MulticastPipe* algorithmPipe=Vrui::openPipe();
Algorithm* algorithm=module->getAlgorithm(algorithmName.c_str(),variableManager,algorithmPipe);
/* Extract an element using the given extractor: */
if(algorithm!=0)
{
std::cout<<"Creating "<<algorithmName<<"..."<<std::flush;
Misc::Timer extractionTimer;
try
{
/* Read the element's extraction parameters from the file: */
Parameters* parameters=algorithm->cloneParameters();
parameters->read(source);
if(pipe!=0)
{
/* Send the extraction parameters to the slaves: */
pipe->write<int>(1);
parameters->write(sink);
pipe->flush();
}
/* Extract the element: */
Element* element=algorithm->createElement(parameters);
/* Store the element: */
elementList->addElement(element,algorithmName.c_str());
}
catch(std::runtime_error err)
{
if(pipe!=0)
{
/* Tell the slaves there was a problem: */
pipe->write<int>(0);
pipe->flush();
}
std::cout<<"Cancelled due to exception "<<err.what()<<"...";
}
/* Destroy the extractor: */
delete algorithm;
extractionTimer.elapse();
std::cout<<" done in "<<extractionTimer.getTime()*1000.0<<" ms"<<std::endl;
}
else
{
std::cout<<"Ignoring unknown algorithm "<<algorithmName<<std::endl;
delete algorithmPipe;
}
}
}
if(pipe!=0)
{
/* Send an empty algorithm name to signal end-of-file to the slaves: */
Misc::Marshaller<std::string>::write("",*pipe);
pipe->flush();
}
}
else
{
std::cout<<"Ready to receive elements"<<std::endl;
/* Create a data source to read elements' parameters: */
Visualization::Abstract::BinaryParametersSource source(variableManager,*pipe,true);
/* Receive all visualization elements from the master: */
while(true)
{
/* Receive the algorithm name from the master: */
std::cout<<"Reading algorithm name"<<std::endl;
std::string algorithmName=Misc::Marshaller<std::string>::read(*pipe);
if(algorithmName.empty()) // Check for end-of-file indicator
break;
// DEBUGGING
std::cout<<"Received algorithm "<<algorithmName<<std::endl;
/* Create an extractor for the given name: */
Cluster::MulticastPipe* algorithmPipe=Vrui::openPipe();
Algorithm* algorithm=module->getAlgorithm(algorithmName.c_str(),variableManager,algorithmPipe);
/* Extract an element using the given extractor: */
if(algorithm!=0)
{
/* Check if there are valid parameters: */
if(pipe->read<int>()!=0)
{
std::cout<<"Receiving parameters"<<std::endl;
/* Receive the extraction parameters: */
Parameters* parameters=algorithm->cloneParameters();
parameters->read(source);
std::cout<<"Receiving element"<<std::endl;
/* Receive the element: */
Element* element=algorithm->startSlaveElement(parameters);
algorithm->continueSlaveElement();
std::cout<<"Done"<<std::endl;
/* Store the element: */
elementList->addElement(element,algorithmName.c_str());
}
/* Destroy the extractor: */
delete algorithm;
}
else
delete algorithmPipe;
}
std::cout<<"Done"<<std::endl;
}
if(pipe!=0)
{
/* Close the communication pipe: */
delete pipe;
}
}
Visualizer::Visualizer(int& argc,char**& argv)
:Vrui::Application(argc,argv),
moduleManager(VISUALIZER_MODULENAMETEMPLATE),
module(0),dataSet(0),variableManager(0),
renderDataSet(true),dataSetRenderer(0),
renderSceneGraphs(false),
coordinateTransformer(0),
firstScalarAlgorithmIndex(0),firstVectorAlgorithmIndex(0),
#ifdef VISUALIZER_USE_COLLABORATION
collaborationClient(0),sharedVisualizationClient(0),
#endif
numCuttingPlanes(0),cuttingPlanes(0),
elementList(0),
algorithm(0),
mainMenu(0),
inLoadPalette(false),inLoadElements(false)
{
/* Parse the command line: */
std::string baseDirectory="";
std::string moduleClassName="";
std::vector<std::string> dataSetArgs;
const char* argColorMapName=0;
std::vector<const char*> loadFileNames;
for(int i=1;i<argc;++i)
{
if(argv[i][0]=='-')
{
if(strcasecmp(argv[i]+1,"class")==0)
{
/* Get visualization module class name and data set arguments from command line: */
++i;
if(i>=argc)
Misc::throwStdErr("Visualizer::Visualizer: missing module class name after -class");
moduleClassName=argv[i];
++i;
while(i<argc&&strcmp(argv[i],";")!=0)
{
dataSetArgs.push_back(argv[i]);
++i;
}
}
else if(strcasecmp(argv[i]+1,"palette")==0)
{
++i;
if(i<argc)
argColorMapName=argv[i];
else
std::cerr<<"Missing palette file name after -palette"<<std::endl;
}
else if(strcasecmp(argv[i]+1,"load")==0)
{
++i;
if(i<argc)
{
/* Load an element file later: */
loadFileNames.push_back(argv[i]);
}
else
std::cerr<<"Missing element file name after -load"<<std::endl;
}
else if(strcasecmp(argv[i]+1,"sceneGraph")==0)
{
++i;
if(i<argc)
{
try
{
/* Create a node creator: */
SceneGraph::NodeCreator nodeCreator;
/* Create the scene graph's root node: */
SG sg;
sg.root=new SceneGraph::GroupNode;
/* Load the VRML file: */
SceneGraph::VRMLFile vrmlFile(argv[i],Vrui::openFile(argv[i]),nodeCreator,Vrui::getClusterMultiplexer());
vrmlFile.parse(sg.root);
/* Store the scene graph's name: */
char* nameStart=argv[i];
char* nameEnd=0;
for(char* nPtr=argv[i];*nPtr!='\0';++nPtr)
{
if(*nPtr=='/')
{
nameStart=nPtr+1;
nameEnd=0;
}
if(*nPtr=='.')
nameEnd=nPtr;
}
sg.name=nameEnd!=0?std::string(nameStart,nameEnd):std::string(nameStart);
/* Store the scene graph in the list: */
sg.render=true;
sceneGraphs.push_back(sg);
renderSceneGraphs=true;
}
catch(std::runtime_error err)
{
std::cerr<<"Ignoring scene graph "<<argv[i]<<" due to exception "<<err.what()<<std::endl;
}
}
else
std::cerr<<"Missing scene graph file name after -sceneGraph"<<std::endl;
}
#ifdef VISUALIZER_USE_COLLABORATION
else if(strcasecmp(argv[i]+1,"share")==0)
{
try
{
/* Create a configuration object: */
Collaboration::CollaborationClient::Configuration* cfg=new Collaboration::CollaborationClient::Configuration;
/* Check if the next argument is a server name: */
if(i+2<argc&&strcasecmp(argv[i+1],"-server")==0)
{
i+=2;
/* Split the server name into host name and port ID: */
char* colonPtr=0;
for(char* sPtr=argv[i];*sPtr!='\0';++sPtr)
if(*sPtr==':')
colonPtr=sPtr;
if(colonPtr!=0)
cfg->setServer(std::string(argv[i],colonPtr),atoi(colonPtr+1));
else
{
/* Use the default port: */
cfg->setServer(argv[i],26000);
}
}
/* Create the collaboration client: */
collaborationClient=new Collaboration::CollaborationClient(cfg);
/* Register the shared Visualizer protocol: */
collaborationClient->registerProtocol(new SharedVisualizationClient(this));
}
catch(std::runtime_error err)
{
std::cerr<<"Caught exception "<<err.what()<<" while creating shared Visualizer client"<<std::endl;
delete collaborationClient;
collaborationClient=0;
}
}
#endif
}
else
{
/* Set the base directory to the directory containing the meta-input file: */
char* slashPtr=0;
for(char* aPtr=argv[i];*aPtr!='\0';++aPtr)
if(*aPtr=='/')
slashPtr=aPtr;
if(slashPtr!=0)
baseDirectory=std::string(argv[i],slashPtr+1);
/* Read the meta-input file of the given name: */
IO::ValueSource metaInputFile(Vrui::openFile(argv[i]));
metaInputFile.setPunctuation("#");
metaInputFile.skipWs();
/* Read the module class name while skipping any comments: */
while((moduleClassName=metaInputFile.readString())=="#")
{
/* Skip the rest of the line: */
metaInputFile.skipLine();
metaInputFile.skipWs();
}
/* Read the data set arguments: */
dataSetArgs.clear();
while(!metaInputFile.eof())
{
/* Read the next module argument: */
std::string argument=metaInputFile.readString();
/* Check for comments: */
if(argument=="#")
{
/* Skip the rest of the line: */
metaInputFile.skipLine();
metaInputFile.skipWs();
}
else
{
/* Store the argument: */
dataSetArgs.push_back(argument);
}
}
}
}
/* Check if a module class name and data set arguments were provided: */
if(moduleClassName=="")
Misc::throwStdErr("Visualizer::Visualizer: no visualization module class name provided");
if(dataSetArgs.empty())
Misc::throwStdErr("Visualizer::Visualizer: no data set arguments provided");
/* Load a visualization module and a data set: */
try
{
/* Load the appropriate visualization module: */
module=moduleManager.loadClass(moduleClassName.c_str());
module->setBaseDirectory(baseDirectory);
/* Load a data set: */
Misc::Timer t;
Cluster::MulticastPipe* pipe=Vrui::openPipe(); // Implicit synchronization point
dataSet=module->load(dataSetArgs,pipe);
delete pipe; // Implicit synchronization point
t.elapse();
if(Vrui::isMaster())
std::cout<<"Time to load data set: "<<t.getTime()*1000.0<<" ms"<<std::endl;
}
catch(std::runtime_error err)
{
Misc::throwStdErr("Visualizer::Visualizer: Could not load data set due to exception %s",err.what());
}
/* Create a variable manager: */
variableManager=new VariableManager(dataSet,argColorMapName);
variableManager->getColorBarDialog()->setCloseButton(true);
variableManager->getColorBarDialog()->getCloseCallbacks().add(this,&Visualizer::colorBarClosedCallback);
variableManager->getPaletteEditor()->setCloseButton(true);
variableManager->getPaletteEditor()->getCloseCallbacks().add(this,&Visualizer::paletteEditorClosedCallback);
/* Determine the color to render the data set: */
for(int i=0;i<3;++i)
dataSetRenderColor[i]=1.0f-Vrui::getBackgroundColor()[i];
dataSetRenderColor[3]=0.2f;
/* Create a data set renderer: */
dataSetRenderer=module->getRenderer(dataSet);
/* Get the data set's coordinate transformer: */
coordinateTransformer=dataSet->getCoordinateTransformer();
/* Set Vrui's application unit: */
if(dataSet->getUnit().unit!=Geometry::LinearUnit::UNKNOWN)
Vrui::getCoordinateManager()->setUnit(dataSet->getUnit());
/* Create cutting planes: */
numCuttingPlanes=6;
cuttingPlanes=new CuttingPlane[numCuttingPlanes];
for(size_t i=0;i<numCuttingPlanes;++i)
{
cuttingPlanes[i].allocated=false;
cuttingPlanes[i].active=false;
}
#ifdef VISUALIZER_USE_COLLABORATION
if(collaborationClient!=0)
{
try
{
/* Connect to the server: */
collaborationClient->connect();
/* Get a pointer to the shared Visualizer protocol: */
sharedVisualizationClient=dynamic_cast<SharedVisualizationClient*>(collaborationClient->getProtocol(SharedVisualizationProtocol::protocolName));
/* Add a close button to the client dialog: */
collaborationClient->getDialog()->setCloseButton(true);
collaborationClient->getDialog()->getCloseCallbacks().add(this,&Visualizer::clientDialogClosedCallback);
}
catch(std::runtime_error err)
{
std::cerr<<"Caught exception "<<err.what()<<" while connecting to shared Visualizer server"<<std::endl;
delete collaborationClient;
collaborationClient=0;
}
}
#endif
/* Create the main menu: */
mainMenu=createMainMenu();
Vrui::setMainMenu(mainMenu);
/* Create the element list: */
elementList=new ElementList(Vrui::getWidgetManager());
elementList->getElementListDialog()->setCloseButton(true);
elementList->getElementListDialog()->getCloseCallbacks().add(this,&Visualizer::elementListClosedCallback);
/* Load all element files listed on the command line: */
for(std::vector<const char*>::const_iterator lfnIt=loadFileNames.begin();lfnIt!=loadFileNames.end();++lfnIt)
{
/* Determine the type of the element file: */
if(Misc::hasCaseExtension(*lfnIt,".asciielem"))
{
/* Load an ASCII elements file: */
loadElements(*lfnIt,true);
}
else if(Misc::hasCaseExtension(*lfnIt,".binelem"))
{
/* Load a binary elements file: */
loadElements(*lfnIt,false);
}
}
}
Visualizer::~Visualizer(void)
{
delete mainMenu;
/* Delete all finished visualization elements: */
delete elementList;
/* Delete all locators: */
for(BaseLocatorList::iterator blIt=baseLocators.begin();blIt!=baseLocators.end();++blIt)
delete *blIt;
/* Delete the cutting planes: */
delete[] cuttingPlanes;
#ifdef VISUALIZER_USE_COLLABORATION
/* Delete a shared visualization client: */
delete collaborationClient;
#endif
/* Delete the coordinate transformer: */
delete coordinateTransformer;
/* Delete the data set renderer: */
delete dataSetRenderer;
/* Delete the variable manager: */
delete variableManager;
/* Delete the data set: */
delete dataSet;
}
void Visualizer::toolCreationCallback(Vrui::ToolManager::ToolCreationCallbackData* cbData)
{
/* Check if the new tool is a locator tool: */
Vrui::LocatorTool* locatorTool=dynamic_cast<Vrui::LocatorTool*>(cbData->tool);
if(locatorTool!=0)
{
BaseLocator* newLocator;
if(cbData->cfg!=0)
{
/* Determine the algorithm type from the configuration file section: */
std::string algorithmName=cbData->cfg->retrieveString("./algorithm");
if(algorithmName=="Cutting Plane")
{
/* Create a cutting plane locator object and associate it with the new tool: */
newLocator=new CuttingPlaneLocator(locatorTool,this,cbData->cfg);
}
else if(algorithmName=="Evaluate Scalars")
{
/* Create a scalar evaluation locator object and associate it with the new tool: */
newLocator=new ScalarEvaluationLocator(locatorTool,this,cbData->cfg);
}
else if(algorithmName=="Evaluate Vectors")
{
/* Create a vector evaluation locator object and associate it with the new tool: */
newLocator=new VectorEvaluationLocator(locatorTool,this,cbData->cfg);
}
else
{
/* Create an extractor locator: */
Cluster::MulticastPipe* algorithmPipe=Vrui::openPipe();
Algorithm* extractor=module->getAlgorithm(algorithmName.c_str(),variableManager,algorithmPipe);
if(extractor!=0)
{
if(cbData->cfg!=0)
{
/* Read the extractor's parameters from the configuration file section: */
Visualization::Abstract::ConfigurationFileParametersSource source(variableManager,*cbData->cfg);
extractor->readParameters(source);
}
newLocator=new ExtractorLocator(locatorTool,this,extractor,cbData->cfg);
}
else
{
newLocator=0;
delete algorithmPipe;
}
}
}
else
{
if(algorithm==0)
{
/* Create a cutting plane locator object and associate it with the new tool: */
newLocator=new CuttingPlaneLocator(locatorTool,this);
}
else if(algorithm<firstScalarAlgorithmIndex)
{
/* Create a scalar evaluation locator object and associate it with the new tool: */