Salome HOME
StaticMeshPlugin porting for PV5.11
[modules/paravis.git] / src / Plugins / StaticMesh / plugin / StaticMeshModule / vtkStaticEnSightGoldBinaryReader.cxx
1 /*=========================================================================
2
3   Program:   Visualization Toolkit
4   Module:    vtkStaticEnSightGoldBinaryReader.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 "vtkStaticEnSightGoldBinaryReader.h"
16
17 #include <vtkDataArray.h>
18 #include <vtkDataArrayCollection.h>
19 #include <vtkIdList.h>
20 #include <vtkIdListCollection.h>
21 #include <vtkInformation.h>
22 #include <vtkInformationVector.h>
23 #include <vtkMultiBlockDataSet.h>
24 #include <vtkObjectFactory.h>
25 #include <vtkStreamingDemandDrivenPipeline.h>
26 #include <vtksys/SystemTools.hxx>
27
28 vtkStandardNewMacro(vtkStaticEnSightGoldBinaryReader);
29
30 //----------------------------------------------------------------------------
31 int vtkStaticEnSightGoldBinaryReader::RequestData(vtkInformation* vtkNotUsed(request),
32   vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* outputVector)
33 {
34   vtkMultiBlockDataSet* output = vtkMultiBlockDataSet::GetData(outputVector);
35
36   vtkInformation* outInfo = outputVector->GetInformationObject(0);
37   int tsLength = outInfo->Length(vtkStreamingDemandDrivenPipeline::TIME_STEPS());
38   double* steps = outInfo->Get(vtkStreamingDemandDrivenPipeline::TIME_STEPS());
39
40   this->ActualTimeValue = this->TimeValue;
41
42   // Check if a particular time was requested by the pipeline.
43   // This overrides the ivar.
44   if (outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP()) && tsLength > 0)
45   {
46     // Get the requested time step. We only support requests of a single time
47     // step in this reader right now
48     double requestedTimeStep = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP());
49
50     // find the first time value larger than requested time value
51     // this logic could be improved
52     int cnt = 0;
53     while (cnt < tsLength - 1 && steps[cnt] < requestedTimeStep)
54     {
55       cnt++;
56     }
57     this->ActualTimeValue = steps[cnt];
58   }
59
60   if (!this->UseStaticMesh || this->CacheMTime < this->GetMTime())
61   {
62     if (vtksys::SystemTools::HasEnv("VTK_DEBUG_STATIC_MESH"))
63     {
64       vtkWarningMacro("Building static mesh cache");
65     }
66
67     if (!this->CaseFileRead)
68     {
69       vtkErrorMacro("error reading case file");
70       return 0;
71     }
72
73     this->NumberOfNewOutputs = 0;
74     this->NumberOfGeometryParts = 0;
75     if (this->GeometryFileName)
76     {
77       vtkIdType timeStep = 1;
78       vtkIdType timeStepInFile = 1;
79       int fileNum = 1;
80       std::string fileName(this->GeometryFileName);
81       fileName.resize(fileName.size() + 10);
82
83       if (this->UseTimeSets)
84       {
85         vtkIdType timeSet = this->TimeSetIds->IsId(this->GeometryTimeSet);
86         if (timeSet >= 0)
87         {
88           vtkDataArray* times = this->TimeSets->GetItem(timeSet);
89           this->GeometryTimeValue = times->GetComponent(0, 0);
90           for (vtkIdType i = 1; i < times->GetNumberOfTuples(); i++)
91           {
92             double newTime = times->GetComponent(i, 0);
93             if (newTime <= this->ActualTimeValue && newTime > this->GeometryTimeValue)
94             {
95               this->GeometryTimeValue = newTime;
96               timeStep++;
97               timeStepInFile++;
98             }
99           }
100           if (this->TimeSetFileNameNumbers->GetNumberOfItems() > 0)
101           {
102             int collectionNum = this->TimeSetsWithFilenameNumbers->IsId(this->GeometryTimeSet);
103             if (collectionNum > -1)
104             {
105               vtkIdList* filenameNumbers = this->TimeSetFileNameNumbers->GetItem(collectionNum);
106               int filenameNum = filenameNumbers->GetId(timeStep - 1);
107               if (!this->UseFileSets)
108               {
109                 this->ReplaceWildcardsHelper(&fileName[0], filenameNum);
110               }
111             }
112           }
113
114           // There can only be file sets if there are also time sets.
115           if (this->UseFileSets)
116           {
117             vtkIdType fileSet = this->FileSets->IsId(this->GeometryFileSet);
118             vtkIdList* numStepsList =
119               static_cast<vtkIdList*>(this->FileSetNumberOfSteps->GetItemAsObject(fileSet));
120
121             if (timeStep > numStepsList->GetId(0))
122             {
123               vtkIdType numSteps = numStepsList->GetId(0);
124               timeStepInFile -= numSteps;
125               fileNum = 2;
126               for (vtkIdType i = 1; i < numStepsList->GetNumberOfIds(); i++)
127               {
128                 numSteps += numStepsList->GetId(i);
129                 if (timeStep > numSteps)
130                 {
131                   fileNum++;
132                   timeStepInFile -= numStepsList->GetId(i);
133                 }
134               }
135             }
136             if (this->FileSetFileNameNumbers->GetNumberOfItems() > 0)
137             {
138               int collectionNum = this->FileSetsWithFilenameNumbers->IsId(this->GeometryFileSet);
139               if (collectionNum > -1)
140               {
141                 vtkIdList* filenameNumbers = this->FileSetFileNameNumbers->GetItem(collectionNum);
142                 int filenameNum = filenameNumbers->GetId(fileNum - 1);
143                 this->ReplaceWildcardsHelper(&fileName[0], filenameNum);
144               }
145             }
146           }
147         }
148       }
149
150       if (!this->ReadGeometryFile(fileName.data(), timeStepInFile, this->Cache))
151       {
152         vtkErrorMacro("error reading geometry file");
153         return 0;
154       }
155     }
156     if (this->MeasuredFileName)
157     {
158       vtkIdType timeStep = 1;
159       vtkIdType timeStepInFile = 1;
160       int fileNum = 1;
161       std::string fileName(this->MeasuredFileName);
162       fileName.resize(fileName.size() + 10);
163
164       if (this->UseTimeSets)
165       {
166         vtkIdType timeSet = this->TimeSetIds->IsId(this->MeasuredTimeSet);
167         if (timeSet >= 0)
168         {
169           vtkDataArray* times = this->TimeSets->GetItem(timeSet);
170           this->MeasuredTimeValue = times->GetComponent(0, 0);
171           for (vtkIdType i = 1; i < times->GetNumberOfTuples(); i++)
172           {
173             double newTime = times->GetComponent(i, 0);
174             if (newTime <= this->ActualTimeValue && newTime > this->MeasuredTimeValue)
175             {
176               this->MeasuredTimeValue = newTime;
177               timeStep++;
178               timeStepInFile++;
179             }
180           }
181           if (this->TimeSetFileNameNumbers->GetNumberOfItems() > 0)
182           {
183             int collectionNum = this->TimeSetsWithFilenameNumbers->IsId(this->MeasuredTimeSet);
184             if (collectionNum > -1)
185             {
186               vtkIdList* filenameNumbers = this->TimeSetFileNameNumbers->GetItem(collectionNum);
187               int filenameNum = filenameNumbers->GetId(timeStep - 1);
188               if (!this->UseFileSets)
189               {
190                 this->ReplaceWildcardsHelper(&fileName[0], filenameNum);
191               }
192             }
193           }
194
195           // There can only be file sets if there are also time sets.
196           if (this->UseFileSets)
197           {
198             vtkIdType fileSet = this->FileSets->IsId(this->MeasuredFileSet);
199             vtkIdList* numStepsList =
200               static_cast<vtkIdList*>(this->FileSetNumberOfSteps->GetItemAsObject(fileSet));
201
202             if (timeStep > numStepsList->GetId(0))
203             {
204               vtkIdType numSteps = numStepsList->GetId(0);
205               timeStepInFile -= numSteps;
206               fileNum = 2;
207               for (vtkIdType i = 1; i < numStepsList->GetNumberOfIds(); i++)
208               {
209                 numSteps += numStepsList->GetId(i);
210                 if (timeStep > numSteps)
211                 {
212                   fileNum++;
213                   timeStepInFile -= numStepsList->GetId(i);
214                 }
215               }
216             }
217             if (this->FileSetFileNameNumbers->GetNumberOfItems() > 0)
218             {
219               int collectionNum = this->FileSetsWithFilenameNumbers->IsId(this->MeasuredFileSet);
220               if (collectionNum > -1)
221               {
222                 vtkIdList* filenameNumbers = this->FileSetFileNameNumbers->GetItem(fileSet);
223                 int filenameNum = filenameNumbers->GetId(fileNum - 1);
224                 this->ReplaceWildcardsHelper(&fileName[0], filenameNum);
225               }
226             }
227           }
228         }
229       }
230       if (!this->ReadMeasuredGeometryFile(fileName.data(), timeStepInFile, this->Cache))
231       {
232         vtkErrorMacro("error reading measured geometry file");
233         return 0;
234       }
235     }
236     this->CacheMTime.Modified();
237   }
238   output->ShallowCopy(this->Cache);
239
240   if ((this->NumberOfVariables + this->NumberOfComplexVariables) > 0)
241   {
242     if (!this->ReadVariableFiles(output))
243     {
244       vtkErrorMacro("error reading variable files");
245       return 0;
246     }
247   }
248
249   return 1;
250 }