1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkGenericEnSightReader.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkGenericEnSightReader.h"
16 
17 #include "vtkCallbackCommand.h"
18 #include "vtkCompositeDataPipeline.h"
19 #include "vtkDataArrayCollection.h"
20 #include "vtkDataArraySelection.h"
21 #include "vtkEnSight6BinaryReader.h"
22 #include "vtkEnSight6Reader.h"
23 #include "vtkEnSightGoldBinaryReader.h"
24 #include "vtkEnSightGoldReader.h"
25 #include "vtkMultiBlockDataSet.h"
26 #include "vtkIdListCollection.h"
27 #include "vtkInformation.h"
28 #include "vtkInformationVector.h"
29 #include "vtkObjectFactory.h"
30 
31 #include <string>
32 #include <map>
33 #include <cassert>
34 #include <ctype.h> /* isspace */
35 
36 vtkStandardNewMacro(vtkGenericEnSightReader);
37 
38 vtkCxxSetObjectMacro(vtkGenericEnSightReader,TimeSets,
39                      vtkDataArrayCollection);
40 
41 class TranslationTableType
42 {
43 public:
44   std::map<int,int> PartIdMap;
45 };
46 
47 
48 //----------------------------------------------------------------------------
vtkGenericEnSightReader()49 vtkGenericEnSightReader::vtkGenericEnSightReader()
50 {
51   this->Reader = NULL;
52   this->IS = NULL;
53   this->IFile = NULL;
54 
55   this->CaseFileName = NULL;
56   this->GeometryFileName = NULL;
57   this->FilePath = NULL;
58 
59   this->VariableTypes = NULL;
60   this->ComplexVariableTypes = NULL;
61 
62   this->VariableDescriptions = NULL;
63   this->ComplexVariableDescriptions = NULL;
64 
65   this->NumberOfVariables = 0;
66   this->NumberOfComplexVariables = 0;
67 
68   this->NumberOfScalarsPerNode = 0;
69   this->NumberOfVectorsPerNode = 0;
70   this->NumberOfTensorsSymmPerNode = 0;
71   this->NumberOfScalarsPerElement = 0;
72   this->NumberOfVectorsPerElement = 0;
73   this->NumberOfTensorsSymmPerElement = 0;
74   this->NumberOfScalarsPerMeasuredNode = 0;
75   this->NumberOfVectorsPerMeasuredNode = 0;
76   this->NumberOfComplexScalarsPerNode = 0;
77   this->NumberOfComplexVectorsPerNode = 0;
78   this->NumberOfComplexScalarsPerElement = 0;
79   this->NumberOfComplexVectorsPerElement = 0;
80 
81   this->TimeValue = 0;
82 
83   this->MinimumTimeValue = 0;
84   this->MaximumTimeValue = 0;
85 
86   this->TimeValueInitialized = 0;
87 
88   this->TimeSets = NULL;
89 
90   this->ReadAllVariables = 1;
91 
92   this->ByteOrder = FILE_UNKNOWN_ENDIAN;
93 
94   this->ParticleCoordinatesByIndex = 0;
95 
96   this->EnSightVersion = -1;
97 
98   this->PointDataArraySelection = vtkDataArraySelection::New();
99   this->CellDataArraySelection = vtkDataArraySelection::New();
100 
101   // Setup the selection callback to modify this object when an array
102   // selection is changed.
103   this->SelectionObserver = vtkCallbackCommand::New();
104   this->SelectionObserver->SetCallback(
105     &vtkGenericEnSightReader::SelectionModifiedCallback);
106   this->SelectionObserver->SetClientData(this);
107   this->PointDataArraySelection->AddObserver(vtkCommand::ModifiedEvent,
108                                              this->SelectionObserver);
109   this->CellDataArraySelection->AddObserver(vtkCommand::ModifiedEvent,
110                                             this->SelectionObserver);
111   this->SelectionModifiedDoNotCallModified = 0;
112   this->TranslationTable = new TranslationTableType;
113 
114   this->SetNumberOfInputPorts(0);
115 }
116 
117 //----------------------------------------------------------------------------
~vtkGenericEnSightReader()118 vtkGenericEnSightReader::~vtkGenericEnSightReader()
119 {
120   int i;
121 
122   if (this->Reader)
123     {
124     this->Reader->Delete();
125     this->Reader = NULL;
126     }
127   delete this->IS;
128   this->IS = NULL;
129 
130   delete [] this->CaseFileName;
131   this->CaseFileName = NULL;
132 
133   delete [] this->GeometryFileName;
134   this->GeometryFileName = NULL;
135 
136   delete [] this->FilePath;
137   this->FilePath = NULL;
138 
139   if (this->NumberOfVariables > 0)
140     {
141     for (i = 0; i < this->NumberOfVariables; i++)
142       {
143       delete [] this->VariableDescriptions[i];
144       }
145     delete [] this->VariableDescriptions;
146     delete [] this->VariableTypes;
147     this->VariableDescriptions = NULL;
148     this->VariableTypes = NULL;
149     }
150   if (this->NumberOfComplexVariables > 0)
151     {
152     for (i = 0; i < this->NumberOfComplexVariables; i++)
153       {
154       delete [] this->ComplexVariableDescriptions[i];
155       }
156     delete [] this->ComplexVariableDescriptions;
157     delete [] this->ComplexVariableTypes;
158     this->ComplexVariableDescriptions = NULL;
159     this->ComplexVariableTypes = NULL;
160     }
161 
162   this->SetTimeSets(0);
163   this->CellDataArraySelection->RemoveObserver(this->SelectionObserver);
164   this->PointDataArraySelection->RemoveObserver(this->SelectionObserver);
165   this->SelectionObserver->Delete();
166   this->CellDataArraySelection->Delete();
167   this->PointDataArraySelection->Delete();
168   delete this->TranslationTable;
169 }
170 
171 //-----------------------------------------------------------------------------
CanReadFile(const char * casefilename)172 int vtkGenericEnSightReader::CanReadFile(const char *casefilename)
173 {
174   vtkGenericEnSightReader *reader = vtkGenericEnSightReader::New();
175   reader->SetCaseFileName(casefilename);
176   int type = reader->DetermineEnSightVersion(1);
177   reader->Delete();
178   return (type != -1);
179 }
180 
181 //----------------------------------------------------------------------------
RequestData(vtkInformation * vtkNotUsed (request),vtkInformationVector ** vtkNotUsed (inputVector),vtkInformationVector * outputVector)182 int vtkGenericEnSightReader::RequestData(
183   vtkInformation *vtkNotUsed(request),
184   vtkInformationVector **vtkNotUsed(inputVector),
185   vtkInformationVector *outputVector)
186 {
187   int i;
188 
189   if ( !this->Reader )
190     {
191     return 0;
192     }
193 
194   vtkInformation *outInfo = outputVector->GetInformationObject(0);
195 
196   // Set the real reader's data array selections from ours.
197   this->SetReaderDataArraySelectionSetsFromSelf();
198 
199   this->Reader->SetTimeValue(this->GetTimeValue());
200   this->Reader->UpdateInformation();
201   vtkInformation* tmpOutInfo =
202     this->Reader->GetExecutive()->GetOutputInformation(0);
203   if (outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP()))
204     {
205     tmpOutInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP(),
206                     outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP()));
207     }
208 
209   // GHOST LEVEL
210   // uncomment these lines below if you want to active
211   // the ghost level system
212   /*
213   if (outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS()))
214     {
215     tmpOutInfo->CopyEntry(
216       outInfo,
217       vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS());
218     }
219    */
220 
221   this->Reader->Update();
222 
223   this->NumberOfScalarsPerNode = this->Reader->GetNumberOfScalarsPerNode();
224   this->NumberOfVectorsPerNode = this->Reader->GetNumberOfVectorsPerNode();
225   this->NumberOfTensorsSymmPerNode =
226     this->Reader->GetNumberOfTensorsSymmPerNode();
227   this->NumberOfScalarsPerElement =
228     this->Reader->GetNumberOfScalarsPerElement();
229   this->NumberOfVectorsPerElement =
230     this->Reader->GetNumberOfVectorsPerElement();
231   this->NumberOfTensorsSymmPerElement =
232     this->Reader->GetNumberOfTensorsSymmPerElement();
233   this->NumberOfScalarsPerMeasuredNode =
234     this->Reader->GetNumberOfScalarsPerMeasuredNode();
235   this->NumberOfVectorsPerMeasuredNode =
236     this->Reader->GetNumberOfVectorsPerMeasuredNode();
237   this->NumberOfComplexScalarsPerNode =
238     this->Reader->GetNumberOfComplexScalarsPerNode();
239   this->NumberOfComplexVectorsPerNode =
240     this->Reader->GetNumberOfComplexVectorsPerNode();
241   this->NumberOfComplexScalarsPerElement =
242     this->Reader->GetNumberOfComplexScalarsPerElement();
243   this->NumberOfComplexVectorsPerElement =
244     this->Reader->GetNumberOfComplexScalarsPerElement();
245 
246   vtkMultiBlockDataSet *output = vtkMultiBlockDataSet::SafeDownCast(
247     outInfo->Get(vtkDataObject::DATA_OBJECT()));
248 
249   output->ShallowCopy(this->Reader->GetOutput());
250 
251   if (this->NumberOfVariables > 0)
252     {
253     for (i = 0; i < this->NumberOfVariables; i++)
254       {
255       delete [] this->VariableDescriptions[i];
256       }
257     delete [] this->VariableDescriptions;
258     delete [] this->VariableTypes;
259     this->VariableDescriptions = NULL;
260     this->VariableTypes = NULL;
261     this->NumberOfVariables = 0;
262     }
263   if (this->NumberOfComplexVariables > 0)
264     {
265     for (i = 0; i < this->NumberOfComplexVariables; i++)
266       {
267       delete [] this->ComplexVariableDescriptions[i];
268       }
269     delete [] this->ComplexVariableDescriptions;
270     delete [] this->ComplexVariableTypes;
271     this->ComplexVariableDescriptions = NULL;
272     this->ComplexVariableTypes = NULL;
273     this->NumberOfComplexVariables = 0;
274     }
275 
276   for (i = 0; i < this->Reader->GetNumberOfVariables(); i++)
277     {
278     this->AddVariableDescription(this->Reader->GetDescription(i));
279     this->AddVariableType(this->Reader->GetVariableType(i));
280     this->NumberOfVariables++;
281     }
282   for (i = 0; i < this->Reader->GetNumberOfComplexVariables(); i++)
283     {
284     this->AddComplexVariableDescription(
285       this->Reader->GetComplexDescription(i));
286     this->AddComplexVariableType(this->Reader->GetComplexVariableType(i));
287     this->NumberOfComplexVariables++;
288     }
289 
290   return 1;
291 }
292 
293 //----------------------------------------------------------------------------
SetTimeValue(float value)294 void vtkGenericEnSightReader::SetTimeValue(float value)
295 {
296   vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting TimeValue to " << value);
297   if (this->TimeValue != value)
298     {
299     this->TimeValue = value;
300     this->Modified();
301     }
302   this->TimeValueInitialized = 1;
303 }
304 
305 //----------------------------------------------------------------------------
DetermineEnSightVersion(int quiet)306 int vtkGenericEnSightReader::DetermineEnSightVersion(int quiet)
307 {
308   char line[256], subLine[256], subLine1[256], subLine2[256], binaryLine[81];
309   char *binaryLinePtr;
310   int stringRead;
311   int timeSet = 1, fileSet = 1;
312   int xtimeSet= 1, xfileSet= 1;
313   char *fileName = NULL;
314   int lineRead;
315   if (!this->CaseFileName)
316     {
317     if (!quiet) vtkErrorMacro("A case file name must be specified.");
318     return -1;
319     }
320   std::string sfilename = "";
321   if (this->FilePath)
322     {
323     sfilename = this->FilePath;
324     if (sfilename.at(sfilename.length()-1) != '/')
325       {
326       sfilename += "/";
327       }
328     sfilename += this->CaseFileName;
329     vtkDebugMacro("full path to case file: "
330                   << sfilename.c_str());
331     }
332   else
333     {
334     sfilename = this->CaseFileName;
335     }
336 
337   this->IS = new ifstream(sfilename.c_str(), ios::in);
338   if (this->IS->fail())
339     {
340     if (!quiet) vtkErrorMacro("Unable to open file: " << sfilename.c_str());
341     delete this->IS;
342     this->IS = NULL;
343     return -1;
344     }
345 
346   this->ReadNextDataLine(line);
347 
348   if (strncmp(line, "FORMAT", 6) == 0)
349     {
350     // found the FORMAT section
351     vtkDebugMacro("*** FORMAT section");
352     this->ReadNextDataLine(line);
353 
354     stringRead = sscanf(line, " %*s %*s %s", subLine);
355     if (stringRead == 1)
356       {
357       sscanf(line, " %*s %s %s", subLine1, subLine2);
358       if (strncmp(subLine1,"ensight",7) == 0)
359         {
360         if (strncmp(subLine2,"gold",4) == 0)
361           {
362           lineRead = this->ReadNextDataLine(line);
363           while (strncmp(line, "GEOMETRY", 8) != 0 && lineRead != 0)
364             {
365             lineRead = this->ReadNextDataLine(line);
366             }
367           if (lineRead == 0)
368             {
369             return -1;
370             }
371           if (strncmp(line, "GEOMETRY", 8) == 0)
372             {
373             // found the GEOMETRY section
374             vtkDebugMacro("*** GEOMETRY section");
375 
376             this->ReadNextDataLine(line);
377             if (strncmp(line, "model:", 6) == 0)
378               {
379               if (sscanf(line, " %*s %d %d %s", &xtimeSet, &fileSet, subLine) == 3)
380                 {
381                 timeSet = xtimeSet;
382                 fileSet = xfileSet;
383                 this->SetGeometryFileName(subLine);
384                 }
385               else if (sscanf(line, " %*s %d%*[ \t]%s", &xtimeSet, subLine) == 2)
386                 {
387                 timeSet = xtimeSet;
388                 this->SetGeometryFileName(subLine);
389                 }
390               else if (sscanf(line, " %*s %s", subLine) == 1)
391                 {
392                 this->SetGeometryFileName(subLine);
393                 }
394               } // geometry file name set
395             delete this->IS;
396             this->IS = NULL;
397 
398             fileName = new char[strlen(this->GeometryFileName) + 1];
399             strcpy(fileName, this->GeometryFileName);
400 
401             if (!fileName)
402               {
403               if (!quiet)
404                 {
405                 vtkErrorMacro(
406                       "A GeometryFileName must be specified in the case file.");
407                 }
408               return 0;
409               }
410             if (strrchr(fileName, '*') != NULL)
411               {
412               // RE-open case file; find right time set and fill in
413               // wildcards from there if possible; if not, then find right
414               // file set and fill in wildcards from there.
415               if ( this->ReplaceWildcards(fileName, timeSet, fileSet) == 0 )
416                 {
417                 if (!quiet)
418                   {
419                   vtkErrorMacro(
420                     "upon DetermineEnSightVersion()'s call to ReplaceWildCards()");
421                   }
422                 return -1;
423                 }
424               }
425             sfilename = "";
426             if (this->FilePath)
427               {
428               sfilename = this->FilePath;
429               if (sfilename.at(sfilename.length()-1) != '/')
430                 {
431                 sfilename += "/";
432                 }
433               sfilename += fileName;
434               vtkDebugMacro("full path to geometry file: "
435                             << sfilename.c_str());
436               }
437             else
438               {
439               sfilename = fileName;
440               }
441 
442             // got full path to geometry file
443 
444             this->IFile = fopen(sfilename.c_str(), "rb");
445             if (this->IFile == NULL)
446               {
447               if (!quiet)
448                 {
449                 vtkErrorMacro("Unable to open file: " << sfilename.c_str());
450                 vtkWarningMacro("Assuming binary file.");
451                 }
452               this->IFile = NULL;
453               delete [] fileName;
454               return vtkGenericEnSightReader::ENSIGHT_GOLD_BINARY;
455               } // end if IFile == NULL
456 
457             this->ReadBinaryLine(binaryLine);
458             binaryLine[80] = '\0';
459             // because fortran stores 4 length bytes at the start,
460             // if the strlen is less than 4, skip the first 4
461             // and jump to the start of the actual string
462             binaryLinePtr = &binaryLine[0];
463             if (strlen(binaryLine)<4)
464               {
465               binaryLinePtr = &binaryLine[4];
466               }
467             sscanf(binaryLinePtr, " %*s %s", subLine);
468             // If the file is ascii, there might not be a null
469             // terminator. This leads to a UMR in sscanf
470             if (strncmp(subLine,"Binary",6) == 0 ||
471                 strncmp(subLine,"binary",6) == 0)
472               {
473               fclose(this->IFile);
474               this->IFile = NULL;
475               delete [] fileName;
476               return vtkGenericEnSightReader::ENSIGHT_GOLD_BINARY;
477               } //end if binary
478 
479             fclose(this->IFile);
480             this->IFile = NULL;
481             delete [] fileName;
482             return vtkGenericEnSightReader::ENSIGHT_GOLD;
483             } // if we found the geometry section in the case file
484           } // if ensight gold file
485         } // if regular ensight file (not master_server)
486       else if (strncmp(subLine1,"master_server",13) == 0)
487         {
488         return vtkGenericEnSightReader::ENSIGHT_MASTER_SERVER;
489         }
490       } // if the type line is like "type: xxxx xxxx"
491     else
492       {
493       this->ReadNextDataLine(line);
494       if (strncmp(line, "GEOMETRY", 8) == 0)
495         {
496         // found the GEOMETRY section
497         vtkDebugMacro("*** GEOMETRY section");
498 
499         this->ReadNextDataLine(line);
500         if (strncmp(line, "model:", 6) == 0)
501           {
502           if (sscanf(line, " %*s %d %d %s", &xtimeSet, &fileSet, subLine) == 3)
503             {
504             timeSet = xtimeSet;
505             fileSet = xfileSet;
506             this->SetGeometryFileName(subLine);
507             }
508           else if (sscanf(line, " %*s %d%*[ \t]%s", &xtimeSet, subLine) == 2)
509             {
510             timeSet = xtimeSet;
511             this->SetGeometryFileName(subLine);
512             }
513           else if (sscanf(line, " %*s %s", subLine) == 1)
514             {
515             this->SetGeometryFileName(subLine);
516             }
517           } // geometry file name set
518 
519         fileName = new char[strlen(this->GeometryFileName) + 1];
520         strcpy(fileName, this->GeometryFileName);
521 
522         delete this->IS;
523         this->IS = NULL;
524         if (!fileName)
525           {
526           if (!quiet)
527             {
528             vtkErrorMacro(
529                       "A GeometryFileName must be specified in the case file.");
530             }
531           return 0;
532           }
533         if (strrchr(fileName, '*') != NULL)
534           {
535           // reopen case file; find right time set and fill in wildcards from
536           // there if possible; if not, then find right file set and fill in
537           // wildcards from there.
538           this->ReplaceWildcards(fileName, timeSet, fileSet);
539           }
540         sfilename = "";
541         if (this->FilePath)
542           {
543           sfilename = this->FilePath;
544           if (sfilename.at(sfilename.length()-1) != '/')
545             {
546             sfilename += "/";
547             }
548           sfilename += fileName;
549           vtkDebugMacro("full path to geometry file: "
550                         << sfilename.c_str());
551           }
552         else
553           {
554           sfilename = fileName;
555           }
556 
557         // got full path to geometry file
558 
559         this->IFile = fopen(sfilename.c_str(), "rb");
560         if (this->IFile == NULL)
561           {
562           if (!quiet)
563             {
564             vtkErrorMacro("Unable to open file: " << sfilename.c_str());
565             vtkWarningMacro("Assuming binary file.");
566             }
567           this->IFile = NULL;
568           delete [] fileName;
569           return vtkGenericEnSightReader::ENSIGHT_6_BINARY;
570           } // end if IFile == NULL
571 
572         this->ReadBinaryLine(binaryLine);
573         // If the file is ascii, there might not be a null
574         // terminator. This leads to a UMR in sscanf
575         binaryLine[80] = '\0';
576         sscanf(binaryLine, " %*s %s", subLine);
577         if (strncmp(subLine,"Binary",6) == 0)
578           {
579           fclose(this->IFile);
580           this->IFile = NULL;
581           delete [] fileName;
582           return vtkGenericEnSightReader::ENSIGHT_6_BINARY;
583           } //end if binary
584 
585         fclose(this->IFile);
586         this->IFile = NULL;
587         delete [] fileName;
588         return vtkGenericEnSightReader::ENSIGHT_6;
589         } // if we found the geometry section in the case file
590       } // not ensight gold
591     } // if we found the format section in the case file
592 
593   if (fileName)
594     {
595     delete [] fileName;
596     }
597 
598   return -1;
599 }
600 
601 //----------------------------------------------------------------------------
SetCaseFileName(const char * fileName)602 void vtkGenericEnSightReader::SetCaseFileName(const char* fileName)
603 {
604   char *endingSlash;
605   char *path, *newFileName;
606   int position, numChars;
607 
608   if ( this->CaseFileName && fileName &&
609        (!strcmp(this->CaseFileName, fileName)))
610     {
611     return;
612     }
613   if (this->CaseFileName)
614     {
615     delete [] this->CaseFileName;
616     }
617   if (fileName)
618     {
619     this->CaseFileName = new char[strlen(fileName)+1];
620     strcpy(this->CaseFileName, fileName);
621     }
622   else
623     {
624     this->CaseFileName = NULL;
625     }
626 
627   this->Modified();
628   if (!this->CaseFileName)
629     {
630     return;
631     }
632 
633   // strip off the path and save it as FilePath if it was included in the
634   // filename
635   endingSlash = strrchr(this->CaseFileName, '/');
636   if(endingSlash == NULL)
637     {
638     // check Windows directory separator
639     endingSlash = strrchr(this->CaseFileName, '\\');
640     }
641 
642   if (endingSlash)
643     {
644     position = endingSlash - this->CaseFileName + 1;
645     path = new char[position + 1];
646     numChars = static_cast<int>(strlen(this->CaseFileName));
647     newFileName = new char[numChars - position + 1];
648     strcpy(path, "");
649     strncat(path, this->CaseFileName, position);
650     this->SetFilePath(path);
651     strcpy(newFileName, this->CaseFileName + position);
652     strcpy(this->CaseFileName, newFileName);
653     delete [] path;
654     delete [] newFileName;
655     }
656 }
657 
658 //----------------------------------------------------------------------------
659 // Internal function to read in a line up to 256 characters.
660 // Returns zero if there was an error.
ReadLine(char result[256])661 int vtkGenericEnSightReader::ReadLine(char result[256])
662 {
663   this->IS->getline(result,256);
664 //  if (this->IS->eof())
665   if (this->IS->fail())
666       {
667       // Reset the error flag before returning. This way, we can keep working
668       // if we handle the error downstream.
669       this->IS->clear();
670       return 0;
671       }
672 
673   return 1;
674 }
675 
676 //----------------------------------------------------------------------------
677 // Internal function to read in a line (from a binary file) up
678 // to 80 characters.  Returns zero if there was an error.
ReadBinaryLine(char result[80])679 int vtkGenericEnSightReader::ReadBinaryLine(char result[80])
680 {
681   int n = static_cast<int>(fread(result, sizeof(char), 80, this->IFile));
682 
683   if ((n<80) || feof(this->IFile) || ferror(this->IFile))
684     {
685     return 0;
686     }
687 
688   return 1;
689 }
690 
691 //----------------------------------------------------------------------------
692 // Internal function that skips blank lines and comment lines
693 // and reads the next line it finds (up to 256 characters).
694 // Returns 0 is there was an error.
ReadNextDataLine(char result[256])695 int vtkGenericEnSightReader::ReadNextDataLine(char result[256])
696 {
697   int isComment = 1;
698   int value = 1;
699 
700   while( isComment && value )
701     {
702     value = this->ReadLine(result);
703     if( *result && result[0] != '#' )
704       {
705       size_t len = strlen( result );
706       unsigned int i = 0;
707       while( i < len && (static_cast<unsigned int>(result[i]) <= 255) && isspace(result[i]) )
708         {
709         ++i;
710         }
711       // If there was only space characters this is a comment, thus skip it
712       if( i != len )
713         {
714         // The line was not empty, not beginning by '#' and not composed
715         // of only white space, this is not a comment
716         isComment = 0;
717         }
718       }
719     }
720 
721   return value;
722 }
723 
724 //----------------------------------------------------------------------------
RequestInformation(vtkInformation * request,vtkInformationVector ** inputVector,vtkInformationVector * outputVector)725 int vtkGenericEnSightReader::RequestInformation(
726   vtkInformation *request,
727   vtkInformationVector **inputVector,
728   vtkInformationVector *outputVector)
729   {
730   int version = this->DetermineEnSightVersion();
731   int createReader = 1;
732   if (version == vtkGenericEnSightReader::ENSIGHT_6)
733     {
734     vtkDebugMacro("EnSight6");
735     if (this->Reader)
736       {
737       if (strcmp(this->Reader->GetClassName(), "vtkEnSight6Reader") == 0)
738         {
739         createReader = 0;
740         }
741       else
742         {
743         this->Reader->Delete();
744         }
745       }
746     if (createReader)
747       {
748       this->Reader = vtkEnSight6Reader::New();
749       }
750     }
751   else if (version == vtkGenericEnSightReader::ENSIGHT_6_BINARY)
752     {
753     vtkDebugMacro("EnSight6 binary");
754     if (this->Reader)
755       {
756       if (strcmp(this->Reader->GetClassName(), "vtkEnSight6BinaryReader") == 0)
757         {
758         createReader = 0;
759         }
760       else
761         {
762         this->Reader->Delete();
763         }
764       }
765     if (createReader)
766       {
767       this->Reader = vtkEnSight6BinaryReader::New();
768       }
769     }
770   else if (version == vtkGenericEnSightReader::ENSIGHT_GOLD)
771     {
772     vtkDebugMacro("EnSightGold");
773     if (this->Reader)
774       {
775       if (strcmp(this->Reader->GetClassName(), "vtkEnSightGoldReader") == 0)
776         {
777         createReader = 0;
778         }
779       else
780         {
781         this->Reader->Delete();
782         }
783       }
784     if (createReader)
785       {
786       this->Reader = vtkEnSightGoldReader::New();
787       }
788     }
789   else if (version == vtkGenericEnSightReader::ENSIGHT_GOLD_BINARY)
790     {
791     vtkDebugMacro("EnSightGold binary");
792     if (this->Reader)
793       {
794       if (strcmp(this->Reader->GetClassName(),
795         "vtkEnSightGoldBinaryReader") == 0)
796         {
797         createReader = 0;
798         }
799       else
800         {
801         this->Reader->Delete();
802         }
803       }
804     if (createReader)
805       {
806       this->Reader = vtkEnSightGoldBinaryReader::New();
807       }
808     }
809   else
810     {
811     vtkErrorMacro("Error determining EnSightVersion");
812     this->EnSightVersion = -1;
813     return 0;
814     }
815   this->EnSightVersion = version;
816 
817   // Copy current array selections to internal reader.
818   this->SetReaderDataArraySelectionSetsFromSelf();
819   this->Reader->SetReadAllVariables(this->ReadAllVariables);
820   this->Reader->SetCaseFileName(this->GetCaseFileName());
821   this->Reader->SetFilePath(this->GetFilePath());
822 
823   // The following line, explicitly initializing this->ByteOrder to
824   // FILE_UNKNOWN_ENDIAN,  MUST !!NOT!! be removed as it is used to
825   // force vtkEnSightGoldBinaryReader::ReadPartId(...) to determine
826   // the actual endian type. Otherwise the endian type, the default
827   // value from combobox 'Byte Order' of the user interface -------
828   // FILE_BIG_ENDIAN unless the user manually toggles the combobox,
829   // would be forwarded to  this->Reader->ByteOrder through the next
830   // line and therefore would prevent vtkEnSightGoldBinaryReader::
831   // ReadPartId(...) from automatically checking the endian type. As
832   // a consequence, little-endian files such as the one mentioned in
833   // bug #0008237 would not be loadable. The following line might be
834   // removed ONLY WHEN the combobox is removed through
835   // ParaViews\Servers\ServerManager\Resources\readers.xml.
836   // Thus it is highly suggested that the following line be retained
837   // to guarantee the fix to bug #0007424 -- automatic determination
838   // of the endian type.
839   this->ByteOrder = FILE_UNKNOWN_ENDIAN;
840 
841   this->Reader->SetByteOrder(this->ByteOrder);
842   this->Reader->RequestInformation(request, inputVector, outputVector);
843   this->Reader->SetParticleCoordinatesByIndex(this->ParticleCoordinatesByIndex);
844 
845   this->SetTimeSets(this->Reader->GetTimeSets());
846   if(!this->TimeValueInitialized)
847     {
848     this->SetTimeValue(this->Reader->GetTimeValue());
849     }
850   this->MinimumTimeValue = this->Reader->GetMinimumTimeValue();
851   this->MaximumTimeValue = this->Reader->GetMaximumTimeValue();
852 
853   // Copy new data array selections from internal reader.
854   this->SetDataArraySelectionSetsFromReader();
855 
856   return 1;
857 }
858 
859 //----------------------------------------------------------------------------
AddVariableDescription(const char * description)860 void vtkGenericEnSightReader::AddVariableDescription(const char* description)
861 {
862   int size = this->NumberOfVariables;
863   int i;
864 
865   char ** newDescriptionList = new char *[size]; // temporary array
866 
867   // copy descriptions to temporary array
868   for (i = 0; i < size; i++)
869     {
870     newDescriptionList[i] =
871       new char[strlen(this->VariableDescriptions[i]) + 1];
872     strcpy(newDescriptionList[i], this->VariableDescriptions[i]);
873     delete [] this->VariableDescriptions[i];
874     }
875   if (this->VariableDescriptions)
876     {
877     delete [] this->VariableDescriptions;
878     }
879 
880   // make room for new description
881   this->VariableDescriptions = new char *[size+1];
882 
883   // copy existing descriptions back to first array
884   for (i = 0; i < size; i++)
885     {
886     this->VariableDescriptions[i] =
887       new char[strlen(newDescriptionList[i]) + 1];
888     strcpy(this->VariableDescriptions[i], newDescriptionList[i]);
889     delete [] newDescriptionList[i];
890     }
891   delete [] newDescriptionList;
892 
893   // add new description at end of first array
894   this->VariableDescriptions[size] = new char[strlen(description) + 1];
895   strcpy(this->VariableDescriptions[size], description);
896   vtkDebugMacro("description: " << this->VariableDescriptions[size]);
897 }
898 
899 //----------------------------------------------------------------------------
AddComplexVariableDescription(const char * description)900 void vtkGenericEnSightReader::AddComplexVariableDescription(const char* description)
901 {
902   int i;
903   int size = this->NumberOfComplexVariables;
904   char ** newDescriptionList = new char *[size]; // temporary array
905 
906   // copy descriptions to temporary array
907   for (i = 0; i < size; i++)
908     {
909     newDescriptionList[i] =
910       new char[strlen(this->ComplexVariableDescriptions[i]) + 1];
911     strcpy(newDescriptionList[i], this->ComplexVariableDescriptions[i]);
912     delete [] this->ComplexVariableDescriptions[i];
913     }
914   delete [] this->ComplexVariableDescriptions;
915 
916   // make room for new description
917   this->ComplexVariableDescriptions = new char *[size+1];
918 
919   // copy existing descriptions back to first array
920   for (i = 0; i < size; i++)
921     {
922     this->ComplexVariableDescriptions[i] =
923       new char[strlen(newDescriptionList[i]) + 1];
924     strcpy(this->ComplexVariableDescriptions[i], newDescriptionList[i]);
925     delete [] newDescriptionList[i];
926     }
927   delete [] newDescriptionList;
928 
929   // add new description at end of first array
930   this->ComplexVariableDescriptions[size] =
931     new char[strlen(description) + 1];
932   strcpy(this->ComplexVariableDescriptions[size], description);
933   vtkDebugMacro("description: "
934                 << this->ComplexVariableDescriptions[size]);
935 }
936 
937 //----------------------------------------------------------------------------
GetNumberOfVariables(int type)938 int vtkGenericEnSightReader::GetNumberOfVariables(int type)
939 {
940   switch (type)
941     {
942     case vtkEnSightReader::SCALAR_PER_NODE:
943       return this->GetNumberOfScalarsPerNode();
944     case vtkEnSightReader::VECTOR_PER_NODE:
945       return this->GetNumberOfVectorsPerNode();
946     case vtkEnSightReader::TENSOR_SYMM_PER_NODE:
947       return this->GetNumberOfTensorsSymmPerNode();
948     case vtkEnSightReader::SCALAR_PER_ELEMENT:
949       return this->GetNumberOfScalarsPerElement();
950     case vtkEnSightReader::VECTOR_PER_ELEMENT:
951       return this->GetNumberOfVectorsPerElement();
952     case vtkEnSightReader::TENSOR_SYMM_PER_ELEMENT:
953       return this->GetNumberOfTensorsSymmPerElement();
954     case vtkEnSightReader::SCALAR_PER_MEASURED_NODE:
955       return this->GetNumberOfScalarsPerMeasuredNode();
956     case vtkEnSightReader::VECTOR_PER_MEASURED_NODE:
957       return this->GetNumberOfVectorsPerMeasuredNode();
958     case vtkEnSightReader::COMPLEX_SCALAR_PER_NODE:
959       return this->GetNumberOfComplexScalarsPerNode();
960     case vtkEnSightReader::COMPLEX_VECTOR_PER_NODE:
961       return this->GetNumberOfComplexVectorsPerNode();
962     case vtkEnSightReader::COMPLEX_SCALAR_PER_ELEMENT:
963       return this->GetNumberOfComplexScalarsPerElement();
964     case vtkEnSightReader::COMPLEX_VECTOR_PER_ELEMENT:
965       return this->GetNumberOfComplexVectorsPerElement();
966     default:
967       vtkWarningMacro("unknow variable type");
968       return -1;
969     }
970 }
971 
972 //----------------------------------------------------------------------------
GetDescription(int n)973 const char* vtkGenericEnSightReader::GetDescription(int n)
974 {
975   if (n < this->NumberOfVariables)
976     {
977     return this->VariableDescriptions[n];
978     }
979   return NULL;
980 }
981 
982 //----------------------------------------------------------------------------
GetComplexDescription(int n)983 const char* vtkGenericEnSightReader::GetComplexDescription(int n)
984 {
985   if (n < this->NumberOfComplexVariables)
986     {
987     return this->ComplexVariableDescriptions[n];
988     }
989   return NULL;
990 }
991 
992 //----------------------------------------------------------------------------
GetDescription(int n,int type)993 const char* vtkGenericEnSightReader::GetDescription(int n, int type)
994 {
995   int i, numMatches = 0;
996 
997   if (type < 8)
998     {
999     for (i = 0; i < this->NumberOfVariables; i++)
1000       {
1001       if (this->VariableTypes[i] == type)
1002         {
1003         if (numMatches == n)
1004           {
1005           return this->VariableDescriptions[i];
1006           }
1007         else
1008           {
1009           numMatches++;
1010           }
1011         }
1012       }
1013     }
1014   else
1015     {
1016     for (i = 0; i < this->NumberOfVariables; i++)
1017       {
1018       if (this->ComplexVariableTypes[i] == type)
1019         {
1020         if (numMatches == n)
1021           {
1022           return this->ComplexVariableDescriptions[i];
1023           }
1024         else
1025           {
1026           numMatches++;
1027           }
1028         }
1029       }
1030     }
1031 
1032   return NULL;
1033 }
1034 
1035 //----------------------------------------------------------------------------
AddVariableType(int variableType)1036 void vtkGenericEnSightReader::AddVariableType(int variableType)
1037 {
1038   int size;
1039   int i;
1040   int *types;
1041 
1042   size = this->NumberOfVariables;
1043 
1044   types = new int[size];
1045 
1046   for (i = 0; i < size; i++)
1047     {
1048     types[i] = this->VariableTypes[i];
1049     }
1050   delete [] this->VariableTypes;
1051 
1052   this->VariableTypes = new int[size+1];
1053   for (i = 0; i < size; i++)
1054     {
1055     this->VariableTypes[i] = types[i];
1056     }
1057   delete [] types;
1058   this->VariableTypes[size] = variableType;
1059   vtkDebugMacro("variable type: " << this->VariableTypes[size]);
1060 }
1061 
1062 //----------------------------------------------------------------------------
AddComplexVariableType(int variableType)1063 void vtkGenericEnSightReader::AddComplexVariableType(int variableType)
1064 {
1065   int i;
1066   int* types = NULL;
1067   int size = this->NumberOfComplexVariables;
1068 
1069   if (size > 0)
1070     {
1071     types = new int[size];
1072     for (i = 0; i < size; i++)
1073       {
1074       types[i] = this->ComplexVariableTypes[i];
1075       }
1076     delete [] this->ComplexVariableTypes;
1077     }
1078 
1079   this->ComplexVariableTypes = new int[size+1];
1080   for (i = 0; i < size; i++)
1081     {
1082     this->ComplexVariableTypes[i] = types[i];
1083     }
1084 
1085   if (size > 0)
1086     {
1087     delete [] types;
1088     }
1089   this->ComplexVariableTypes[size] = variableType;
1090   vtkDebugMacro("complex variable type: "
1091                 << this->ComplexVariableTypes[size]);
1092 }
1093 
1094 //----------------------------------------------------------------------------
GetVariableType(int n)1095 int vtkGenericEnSightReader::GetVariableType(int n)
1096 {
1097   if (n < this->NumberOfVariables)
1098     {
1099     return this->VariableTypes[n];
1100     }
1101   return -1;
1102 }
1103 
1104 //----------------------------------------------------------------------------
GetComplexVariableType(int n)1105 int vtkGenericEnSightReader::GetComplexVariableType(int n)
1106 {
1107   if (n < this->NumberOfComplexVariables)
1108     {
1109     return this->ComplexVariableTypes[n];
1110     }
1111   return -1;
1112 }
1113 
1114 //----------------------------------------------------------------------------
ReplaceWildcards(char * fileName,int timeSet,int fileSet)1115 int vtkGenericEnSightReader::ReplaceWildcards(char* fileName, int timeSet,
1116                                               int fileSet)
1117 {
1118   char line[256],  subLine[256];
1119   int  cmpTimeSet, cmpFileSet, fileNameNum, lineReadResult, lineScanResult;
1120 
1121   std::string sfilename;
1122   if ( this->FilePath )
1123     {
1124     sfilename = this->FilePath;
1125     if (  sfilename.at( sfilename.length() - 1 ) != '/'  )
1126       {
1127       sfilename += "/";
1128       }
1129     sfilename += this->CaseFileName;
1130     vtkDebugMacro( "full path to case file: " << sfilename.c_str() );
1131     }
1132   else
1133     {
1134     sfilename = this->CaseFileName;
1135     }
1136 
1137   // We have got a valid CASE file name
1138   this->IS = new ifstream( sfilename.c_str(), ios::in );
1139 
1140   // Below is a revamped version of the code in support of inline & non-inline
1141   // file name numbers, in a CASE file, of which the first one is obtained to
1142   // make a geometry file name, through wildcards replacement, used to determine
1143   // the specific EnSight version.
1144 
1145   // Locate the 'TIME' section
1146   do
1147     {
1148     if ( this->ReadNextDataLine(line) == 0 )
1149       {
1150       vtkErrorMacro(
1151         "ReplaceWildCards() failed to find the 'TIME' section!" );
1152       delete this->IS;
1153       this->IS = NULL;
1154       return 0;
1155       }
1156     } while ( strncmp(line, "TIME", 4) != 0 );
1157 
1158 
1159   // Locate the very 'time set' entry by the index
1160   cmpTimeSet = -10000;
1161   do
1162     {
1163     if ( this->ReadNextDataLine(line) == 0 )
1164       {
1165       vtkErrorMacro(
1166         "ReplaceWildCards() failed to find the target 'time set' entry!" );
1167       delete this->IS;
1168       this->IS = NULL;
1169       return 0;
1170       }
1171 
1172       // 'time set: <int>' --- where to obtain cmpTimeSet, a time set index
1173       lineScanResult = sscanf(line, "%*s %s %d", subLine, &cmpTimeSet);
1174     } while ( lineScanResult != 2 || strncmp(line, "time", 4) != 0 ||
1175               strncmp(subLine, "set", 3) != 0 || cmpTimeSet != timeSet );
1176 
1177   // Skip 'time set: <int>' and 'number of steps: <int>' to go to
1178   // 'filename xxx: ...' --- where to obtain the actual file name number(s)
1179   for ( int  i = 0;  i < 2;  i ++ )
1180     {
1181     lineReadResult = this->ReadNextDataLine(line);
1182     if ( lineReadResult == 0 ||
1183          // check 'filename xxx: ...' upon the second line (i = 1)
1184          ( i == 1 &&
1185            ( strncmp(line, "filename", 8)    != 0 ||
1186              sscanf(line, "%*s %s", subLine) != 1
1187            )
1188          )
1189        )
1190       {
1191       vtkErrorMacro(
1192         "ReplaceWildCards() failed to find the target 'filename ...: ...' entry!" );
1193       delete this->IS;
1194       this->IS = NULL;
1195       return 0;
1196       }
1197     }
1198 
1199   fileNameNum = -10000;
1200 
1201   // 'filename numbers: ...'
1202   if ( strncmp(subLine, "numbers", 7) == 0 )
1203     {
1204     // The filename number(s) may be provided on the line(s) following
1205     // 'filename numbers:', as is usually the case --- not "inline". Thus we
1206     // need to go to the FIRST line that indeed contains the filename number(s).
1207     // Note that we only need to obtain the FIRST file name number since a
1208     // single geometry file allows us to determine the EnSight version. This is
1209     // based on the reasonable assumption that all geometry files referenced by
1210     // a CASE file have the same EnSight version.
1211 
1212     // not "inline"
1213     if ( sscanf(line, "%*s %*s %d", &fileNameNum) != 1 )
1214       {
1215       // let's go to the next VALID line that might be several empty lines apart
1216       if ( this->ReadNextDataLine(line) == 0 )
1217         {
1218         vtkErrorMacro(
1219           "ReplaceWildCards() failed to obtain any non-inline file name number!" );
1220         delete this->IS;
1221         this->IS = NULL;
1222         return 0;
1223         }
1224 
1225       // obtain the first file name number from the next valid line
1226       sscanf(line, "%d", &fileNameNum);
1227       }
1228     }
1229   // 'filename start number: ...' --- followed by 'filename increment: ...'
1230   else
1231     {
1232     char  subSubLine[256];
1233     lineScanResult = sscanf( line, "%*s %s %s %d",
1234                              subLine, subSubLine, &fileNameNum );
1235     if ( lineScanResult != 3 ||
1236          strncmp(subLine,    "start",  5) != 0 ||
1237          strncmp(subSubLine, "number", 6) != 0
1238        )
1239       {
1240       vtkErrorMacro(
1241         "ReplaceWildCards() failed to find 'filename start number: <int>'!" );
1242       delete this->IS;
1243       this->IS = NULL;
1244       return 0;
1245       }
1246     }
1247 
1248   // Let's resort to the 'FILE' section, just in case of a failure so far
1249   if ( fileNameNum == -10000 )
1250     {
1251     // Locate the 'FILE' section
1252     do
1253       {
1254       if ( this->ReadNextDataLine(line) == 0 )
1255         {
1256         vtkErrorMacro(
1257           "ReplaceWildCards() failed to find the optional 'FILE' section!" );
1258         delete this->IS;
1259         this->IS = NULL;
1260         return 0;
1261         }
1262       } while ( strncmp(line, "FILE", 4) != 0 );
1263 
1264     // Locate the very 'file set' entry by the index
1265     cmpFileSet = -10000;
1266     do
1267       {
1268       if ( this->ReadNextDataLine(line) == 0 )
1269         {
1270         vtkErrorMacro(
1271           "ReplaceWildCards() failed to find the target 'file set' entry!" );
1272         delete this->IS;
1273         this->IS = NULL;
1274         return 0;
1275         }
1276 
1277       // 'file set: <int>' --- to obtain cmpFileSet, a file set index
1278       lineScanResult = sscanf(line, "%*s %s %d", subLine, &cmpFileSet);
1279       } while ( lineScanResult != 2 || strncmp(line, "file", 4) != 0 ||
1280                 strncmp(subLine, "set", 3) != 0 || cmpFileSet != fileSet );
1281 
1282     // Skip 'file set: <int>' to go to
1283     // 'filename index: <int>' --- where to obtain ONE actual file name
1284     // Note that we here do NOT allow any non-'inline' scenarios since
1285     // there is ONE AND ONLY ONE integer value, within a 'filename index: <int>'
1286     // entry, that is used to specify a file name index. Thus any violation
1287     // of this reasonable assumption is considered to use an invalid EnSight
1288     // format that needs to be corrected by the EnSight CASE file user.
1289     lineReadResult = this->ReadNextDataLine(line);
1290     lineScanResult = sscanf( line, "%*s %s %d", subLine, &fileNameNum );
1291     if ( lineReadResult == 0               || lineScanResult != 2 ||
1292          strncmp(line, "filename", 8) != 0 || strncmp(subLine, "index", 5) != 0
1293        )
1294       {
1295       vtkErrorMacro(
1296         "ReplaceWildCards() failed to find 'filename index: <int>'!" );
1297       delete this->IS;
1298       this->IS = NULL;
1299       return 0;
1300       }
1301     }
1302 
1303   // So far we have got a file name index
1304   this->ReplaceWildcardsHelper(fileName, fileNameNum);
1305   delete this->IS;
1306   this->IS = NULL;
1307 
1308   return  1;
1309 }
1310 
1311 //----------------------------------------------------------------------------
ReplaceWildcardsHelper(char * fileName,int num)1312 void vtkGenericEnSightReader::ReplaceWildcardsHelper(char* fileName, int num)
1313 {
1314   int wildcardPos, numWildcards, numDigits = 1, i;
1315   int tmpNum = num, multTen = 1;
1316   char newChar;
1317   int newNum;
1318 
1319   wildcardPos = static_cast<int>(strcspn(fileName, "*"));
1320   numWildcards = static_cast<int>(strspn(fileName + wildcardPos, "*"));
1321 
1322   tmpNum /= 10;
1323   while (tmpNum >= 1)
1324     {
1325     numDigits++;
1326     multTen *= 10;
1327     tmpNum /= 10;
1328     }
1329 
1330   for (i = 0; i < numWildcards - numDigits; i++)
1331     {
1332     fileName[i + wildcardPos] = '0';
1333     }
1334 
1335   tmpNum = num;
1336   for (i = numWildcards - numDigits; i < numWildcards; i++)
1337     {
1338     newNum = tmpNum / multTen;
1339     switch (newNum)
1340       {
1341       case 0:
1342         newChar = '0';
1343         break;
1344       case 1:
1345         newChar = '1';
1346         break;
1347       case 2:
1348         newChar = '2';
1349         break;
1350       case 3:
1351         newChar = '3';
1352         break;
1353       case 4:
1354         newChar = '4';
1355         break;
1356       case 5:
1357         newChar = '5';
1358         break;
1359       case 6:
1360         newChar = '6';
1361         break;
1362       case 7:
1363         newChar = '7';
1364         break;
1365       case 8:
1366         newChar = '8';
1367         break;
1368       case 9:
1369         newChar = '9';
1370         break;
1371       default:
1372         // This case should never be reached.
1373         return;
1374       }
1375     assert( newChar == ('0' + newNum) );
1376 
1377     fileName[i + wildcardPos] = newChar;
1378     tmpNum -= multTen * newNum;
1379     multTen /= 10;
1380     }
1381 }
1382 
1383 //----------------------------------------------------------------------------
SetByteOrderToBigEndian()1384 void vtkGenericEnSightReader::SetByteOrderToBigEndian()
1385 {
1386   this->ByteOrder = FILE_BIG_ENDIAN;
1387 }
1388 
1389 //----------------------------------------------------------------------------
SetByteOrderToLittleEndian()1390 void vtkGenericEnSightReader::SetByteOrderToLittleEndian()
1391 {
1392   this->ByteOrder = FILE_LITTLE_ENDIAN;
1393 }
1394 
1395 //----------------------------------------------------------------------------
GetByteOrderAsString()1396 const char *vtkGenericEnSightReader::GetByteOrderAsString()
1397 {
1398   if ( this->ByteOrder ==  FILE_LITTLE_ENDIAN)
1399     {
1400     return "LittleEndian";
1401     }
1402   else
1403     {
1404     return "BigEndian";
1405     }
1406 }
1407 
1408 //----------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)1409 void vtkGenericEnSightReader::PrintSelf(ostream& os, vtkIndent indent)
1410 {
1411   this->Superclass::PrintSelf(os,indent);
1412 
1413   os << indent << "CaseFileName: "
1414      << (this->CaseFileName ? this->CaseFileName : "(none)") << endl;
1415   os << indent << "FilePath: "
1416      << (this->FilePath ? this->FilePath : "(none)") << endl;
1417   os << indent << "EnSight Version: "
1418      <<  this->EnSightVersion << endl;
1419   os << indent << "NumberOfComplexVariables: "
1420      << this->NumberOfComplexVariables << endl;
1421   os << indent << "NumberOfVariables: "
1422      << this->NumberOfVariables << endl;
1423   os << indent << "NumberOfComplexScalarsPerNode: "
1424      << this->NumberOfComplexScalarsPerNode << endl;
1425   os << indent << "NumberOfVectorsPerElement :"
1426      << this->NumberOfVectorsPerElement << endl;
1427   os << indent << "NumberOfTensorsSymmPerElement: "
1428      << this->NumberOfTensorsSymmPerElement << endl;
1429   os << indent << "NumberOfComplexVectorsPerNode: "
1430      << this->NumberOfComplexVectorsPerNode << endl;
1431   os << indent << "NumberOfScalarsPerElement: "
1432      << this->NumberOfScalarsPerElement << endl;
1433   os << indent << "NumberOfComplexVectorsPerElement: "
1434      << this->NumberOfComplexVectorsPerElement << endl;
1435   os << indent << "NumberOfComplexScalarsPerElement: "
1436      << this->NumberOfComplexScalarsPerElement << endl;
1437   os << indent << "NumberOfTensorsSymmPerNode: "
1438      << this->NumberOfTensorsSymmPerNode << endl;
1439   os << indent << "NumberOfScalarsPerMeasuredNode: "
1440      << this->NumberOfScalarsPerMeasuredNode << endl;
1441   os << indent << "NumberOfVectorsPerMeasuredNode: "
1442      << this->NumberOfVectorsPerMeasuredNode << endl;
1443   os << indent << "NumberOfScalarsPerNode: "
1444      << this->NumberOfScalarsPerNode << endl;
1445   os << indent << "NumberOfVectorsPerNode: "
1446      << this->NumberOfVectorsPerNode << endl;
1447   os << indent << "TimeValue: " << this->TimeValue << endl;
1448   os << indent << "MinimumTimeValue: " << this->MinimumTimeValue << endl;
1449   os << indent << "MaximumTimeValue: " << this->MaximumTimeValue << endl;
1450   os << indent << "TimeSets: " << this->TimeSets << endl;
1451   os << indent << "ReadAllVariables: " << this->ReadAllVariables << endl;
1452   os << indent << "ByteOrder: " << this->ByteOrder << endl;
1453   os << indent << "ParticleCoordinatesByIndex: " << this->ParticleCoordinatesByIndex << endl;
1454   os << indent << "CellDataArraySelection: " << this->CellDataArraySelection
1455      << endl;
1456   os << indent << "PointDataArraySelection: " << this->PointDataArraySelection
1457      << endl;
1458   os << indent << "GeometryFileName: " <<
1459      (this->GeometryFileName ? this->GeometryFileName : "(none)") << endl;
1460 }
1461 
1462 //----------------------------------------------------------------------------
CreateStringArray(int numStrings)1463 char** vtkGenericEnSightReader::CreateStringArray(int numStrings)
1464 {
1465   char** strings = new char*[numStrings];
1466   int i;
1467   for(i=0; i < numStrings; ++i)
1468     {
1469     strings[i] = 0;
1470     }
1471   return strings;
1472 }
1473 
1474 //----------------------------------------------------------------------------
DestroyStringArray(int numStrings,char ** strings)1475 void vtkGenericEnSightReader::DestroyStringArray(int numStrings,
1476                                                  char** strings)
1477 {
1478   int i;
1479   for(i=0; i < numStrings; ++i)
1480     {
1481     if(strings[i])
1482       {
1483       delete [] strings[i];
1484       }
1485     }
1486   delete[] strings;
1487 }
1488 
1489 //----------------------------------------------------------------------------
SetDataArraySelectionSetsFromVariables()1490 void vtkGenericEnSightReader::SetDataArraySelectionSetsFromVariables()
1491 {
1492   int numPointArrays = (this->NumberOfScalarsPerNode +
1493                         this->NumberOfVectorsPerNode +
1494                         this->NumberOfTensorsSymmPerNode +
1495                         this->NumberOfScalarsPerMeasuredNode +
1496                         this->NumberOfVectorsPerMeasuredNode +
1497                         this->NumberOfComplexScalarsPerNode +
1498                         this->NumberOfComplexVectorsPerNode);
1499   int numCellArrays = (this->NumberOfScalarsPerElement +
1500                        this->NumberOfVectorsPerElement +
1501                        this->NumberOfTensorsSymmPerElement +
1502                        this->NumberOfComplexScalarsPerElement +
1503                        this->NumberOfComplexVectorsPerElement);
1504 
1505   char** pointNames = this->CreateStringArray(numPointArrays);
1506   char** cellNames = this->CreateStringArray(numCellArrays);
1507   int pointArrayCount = 0;
1508   int cellArrayCount = 0;
1509 
1510   int i;
1511   for(i=0; i < this->NumberOfVariables; ++i)
1512     {
1513     switch (this->VariableTypes[i])
1514       {
1515       case vtkEnSightReader::SCALAR_PER_NODE:
1516       case vtkEnSightReader::VECTOR_PER_NODE:
1517       case vtkEnSightReader::TENSOR_SYMM_PER_NODE:
1518       case vtkEnSightReader::SCALAR_PER_MEASURED_NODE:
1519       case vtkEnSightReader::VECTOR_PER_MEASURED_NODE:
1520         pointNames[pointArrayCount] =
1521           new char[strlen(this->VariableDescriptions[i])+1];
1522         strcpy(pointNames[pointArrayCount], this->VariableDescriptions[i]);
1523         ++pointArrayCount;
1524         break;
1525       case vtkEnSightReader::SCALAR_PER_ELEMENT:
1526       case vtkEnSightReader::VECTOR_PER_ELEMENT:
1527       case vtkEnSightReader::TENSOR_SYMM_PER_ELEMENT:
1528         cellNames[cellArrayCount] =
1529           new char[strlen(this->VariableDescriptions[i])+1];
1530         strcpy(cellNames[cellArrayCount], this->VariableDescriptions[i]);
1531         ++cellArrayCount;
1532         break;
1533       }
1534     }
1535   for(i=0; i < this->NumberOfComplexVariables; ++i)
1536     {
1537     switch(this->ComplexVariableTypes[i])
1538       {
1539       case vtkEnSightReader::COMPLEX_SCALAR_PER_NODE:
1540       case vtkEnSightReader::COMPLEX_VECTOR_PER_NODE:
1541         pointNames[pointArrayCount] =
1542           new char[strlen(this->ComplexVariableDescriptions[i])+1];
1543         strcpy(pointNames[pointArrayCount],
1544                this->ComplexVariableDescriptions[i]);
1545         ++pointArrayCount;
1546         break;
1547       case vtkEnSightReader::COMPLEX_SCALAR_PER_ELEMENT:
1548       case vtkEnSightReader::COMPLEX_VECTOR_PER_ELEMENT:
1549         cellNames[cellArrayCount] =
1550           new char[strlen(this->ComplexVariableDescriptions[i])+1];
1551         strcpy(cellNames[cellArrayCount],
1552                this->ComplexVariableDescriptions[i]);
1553         ++cellArrayCount;
1554         break;
1555       }
1556     }
1557 
1558   this->PointDataArraySelection->SetArraysWithDefault(pointNames,
1559                                                       numPointArrays,
1560                                                       this->ReadAllVariables);
1561   this->CellDataArraySelection->SetArraysWithDefault(cellNames,
1562                                                      numCellArrays,
1563                                                      this->ReadAllVariables);
1564   this->DestroyStringArray(numPointArrays, pointNames);
1565   this->DestroyStringArray(numCellArrays, cellNames);
1566 }
1567 
1568 //----------------------------------------------------------------------------
SetDataArraySelectionSetsFromReader()1569 void vtkGenericEnSightReader::SetDataArraySelectionSetsFromReader()
1570 {
1571   this->SelectionModifiedDoNotCallModified = 1;
1572   this->PointDataArraySelection->CopySelections(
1573     this->Reader->GetPointDataArraySelection());
1574   this->CellDataArraySelection->CopySelections(
1575     this->Reader->GetCellDataArraySelection());
1576   this->SelectionModifiedDoNotCallModified = 0;
1577 }
1578 
1579 //----------------------------------------------------------------------------
SetReaderDataArraySelectionSetsFromSelf()1580 void vtkGenericEnSightReader::SetReaderDataArraySelectionSetsFromSelf()
1581 {
1582   // Set the real reader's data array selections from ours.
1583   this->Reader->GetPointDataArraySelection()->CopySelections(
1584     this->PointDataArraySelection);
1585   this->Reader->GetCellDataArraySelection()->CopySelections(
1586     this->CellDataArraySelection);
1587 }
1588 
1589 //----------------------------------------------------------------------------
SelectionModifiedCallback(vtkObject *,unsigned long,void * clientdata,void *)1590 void vtkGenericEnSightReader::SelectionModifiedCallback(vtkObject*,
1591                                                         unsigned long,
1592                                                         void* clientdata,
1593                                                         void*)
1594 {
1595   static_cast<vtkGenericEnSightReader*>(clientdata)->SelectionModified();
1596 }
1597 
1598 //----------------------------------------------------------------------------
SelectionModified()1599 void vtkGenericEnSightReader::SelectionModified()
1600 {
1601   if(!this->SelectionModifiedDoNotCallModified)
1602     {
1603     this->Modified();
1604     }
1605 }
1606 
1607 //----------------------------------------------------------------------------
GetNumberOfPointArrays()1608 int vtkGenericEnSightReader::GetNumberOfPointArrays()
1609 {
1610   return this->PointDataArraySelection->GetNumberOfArrays();
1611 }
1612 
1613 //----------------------------------------------------------------------------
GetPointArrayName(int index)1614 const char* vtkGenericEnSightReader::GetPointArrayName(int index)
1615 {
1616   return this->PointDataArraySelection->GetArrayName(index);
1617 }
1618 
1619 //----------------------------------------------------------------------------
GetPointArrayStatus(const char * name)1620 int vtkGenericEnSightReader::GetPointArrayStatus(const char* name)
1621 {
1622   return this->PointDataArraySelection->ArrayIsEnabled(name);
1623 }
1624 
1625 //----------------------------------------------------------------------------
SetPointArrayStatus(const char * name,int status)1626 void vtkGenericEnSightReader::SetPointArrayStatus(const char* name, int status)
1627 {
1628   if(status)
1629     {
1630     this->PointDataArraySelection->EnableArray(name);
1631     }
1632   else
1633     {
1634     this->PointDataArraySelection->DisableArray(name);
1635     }
1636 }
1637 
1638 //----------------------------------------------------------------------------
GetNumberOfCellArrays()1639 int vtkGenericEnSightReader::GetNumberOfCellArrays()
1640 {
1641   return this->CellDataArraySelection->GetNumberOfArrays();
1642 }
1643 
1644 //----------------------------------------------------------------------------
GetCellArrayName(int index)1645 const char* vtkGenericEnSightReader::GetCellArrayName(int index)
1646 {
1647   return this->CellDataArraySelection->GetArrayName(index);
1648 }
1649 
1650 //----------------------------------------------------------------------------
GetCellArrayStatus(const char * name)1651 int vtkGenericEnSightReader::GetCellArrayStatus(const char* name)
1652 {
1653   return this->CellDataArraySelection->ArrayIsEnabled(name);
1654 }
1655 
1656 //----------------------------------------------------------------------------
SetCellArrayStatus(const char * name,int status)1657 void vtkGenericEnSightReader::SetCellArrayStatus(const char* name, int status)
1658 {
1659   if(status)
1660     {
1661     this->CellDataArraySelection->EnableArray(name);
1662     }
1663   else
1664     {
1665     this->CellDataArraySelection->DisableArray(name);
1666     }
1667 }
1668 
1669 //----------------------------------------------------------------------------
InsertNewPartId(int partId)1670 int vtkGenericEnSightReader::InsertNewPartId(int partId)
1671 {
1672   int lastId = static_cast<int>(this->TranslationTable->PartIdMap.size());
1673   this->TranslationTable->PartIdMap.insert(
1674     std::map<int,int>::value_type(partId, lastId));
1675   lastId = this->TranslationTable->PartIdMap[partId];
1676   //assert( lastId == this->PartIdTranslationTable[partId] );
1677   return lastId;
1678 }
1679 
1680 //----------------------------------------------------------------------------
FillOutputPortInformation(int vtkNotUsed (port),vtkInformation * info)1681 int vtkGenericEnSightReader::FillOutputPortInformation(int vtkNotUsed(port),
1682                                                        vtkInformation* info)
1683 {
1684   info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkMultiBlockDataSet");
1685   return 1;
1686 }
1687