1 // Copyright (C) 2010-2014 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay
21 #include "vtkMEDReader.h"
22 #include "vtkGenerateVectors.h"
24 #include "vtkMultiBlockDataSet.h"
25 #include "vtkInformation.h"
26 #include "vtkDataSetAttributes.h"
27 #include "vtkStringArray.h"
28 #include "vtkMutableDirectedGraph.h"
29 #include "vtkInformationStringKey.h"
31 #include "vtkUnsignedCharArray.h"
32 #include "vtkInformationVector.h"
33 #include "vtkSmartPointer.h"
34 #include "vtkVariantArray.h"
35 #include "vtkExecutive.h"
36 #include "vtkStreamingDemandDrivenPipeline.h"
37 #include "vtkMultiTimeStepAlgorithm.h"
38 #include "vtkUnstructuredGrid.h"
39 #include "vtkInformationQuadratureSchemeDefinitionVectorKey.h"
40 #include "vtkQuadratureSchemeDefinition.h"
41 #include "vtkPointData.h"
42 #include "vtkCellData.h"
43 #include "vtkCellType.h"
44 #include "vtkCellArray.h"
45 #include "vtkDoubleArray.h"
46 #include "vtkObjectFactory.h"
48 #include "MEDFileFieldRepresentationTree.hxx"
55 class vtkMEDReader::vtkMEDReaderInternal
59 vtkMEDReaderInternal():TK(0),IsMEDOrSauv(true),IsStdOrMode(false),GenerateVect(false),SIL(0),LastLev0(-1),FirstCall0(2)
71 ~vtkMEDReaderInternal()
77 MEDFileFieldRepresentationTree Tree;
80 //when true the file is MED file. when false it is a Sauv file
82 //when false -> std, true -> mode. By default std (false).
84 //when false -> do nothing. When true cut off or extend to nbOfCompo=3 vector arrays.
86 std::string DftMeshName;
87 // Store the vtkMutableDirectedGraph that represents links between family, groups and cell types
88 vtkMutableDirectedGraph* SIL;
89 // store the lev0 id in Tree corresponding to the TIME_STEPS in the pipeline.
92 unsigned char FirstCall0;
95 vtkStandardNewMacro(vtkMEDReader);
97 vtkMEDReader::vtkMEDReader():Internal(new vtkMEDReaderInternal)
99 this->SetNumberOfInputPorts(0);
100 this->SetNumberOfOutputPorts(1);
103 vtkMEDReader::~vtkMEDReader()
105 delete this->Internal;
108 void vtkMEDReader::Reload(int a)
112 std::cerr << "vtkMEDReader::Reload" << a << std::endl;
113 std::string fName((const char *)this->GetFileName());
114 delete this->Internal;
115 this->Internal=new vtkMEDReaderInternal;
116 this->SetFileName(fName.c_str());
119 void vtkMEDReader::GenerateVectors(int val)
121 bool val2((bool)val);
122 if(val2!=this->Internal->GenerateVect)
124 this->Internal->GenerateVect=val2;
129 void vtkMEDReader::ChangeMode(int newMode)
131 this->Internal->IsStdOrMode=newMode!=0;
132 //std::cerr << "vtkMEDReader::ChangeMode : " << this->Internal->IsStdOrMode << std::endl;
136 const char *vtkMEDReader::GetSeparator()
138 return MEDFileFieldRepresentationLeavesArrays::ZE_SEP;
141 void vtkMEDReader::SetFileName(const char *fname)
145 this->Internal->FileName=fname;
146 std::size_t pos(this->Internal->FileName.find_last_of('.'));
147 if(pos!=std::string::npos)
149 std::string ext(this->Internal->FileName.substr(pos));
150 if(ext.find("sauv")!=std::string::npos)
151 this->Internal->IsMEDOrSauv=false;
153 if(this->Internal->Tree.getNumberOfLeavesArrays()==0)
155 this->Internal->Tree.loadMainStructureOfFile(this->Internal->FileName.c_str(),this->Internal->IsMEDOrSauv);
156 this->Internal->Tree.activateTheFirst();//This line manually initialize the status of server (this) with the remote client.
157 this->Internal->TK.setMaxNumberOfTimeSteps(this->Internal->Tree.getMaxNumberOfTimeSteps());
161 catch(INTERP_KERNEL::Exception& e)
163 delete this->Internal;
165 std::ostringstream oss;
166 oss << "Exception has been thrown in vtkMEDReader::SetFileName : " << e.what() << std::endl;
167 if(this->HasObserver("ErrorEvent") )
168 this->InvokeEvent("ErrorEvent",const_cast<char *>(oss.str().c_str()));
170 vtkOutputWindowDisplayErrorText(const_cast<char *>(oss.str().c_str()));
171 vtkObject::BreakOnError();
175 char *vtkMEDReader::GetFileName()
177 return const_cast<char *>(this->Internal->FileName.c_str());
180 int vtkMEDReader::RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
182 //std::cerr << "########################################## RequestInformation ##########################################" << std::endl;
187 vtkInformation *outInfo(outputVector->GetInformationObject(0));
188 outInfo->Set(vtkStreamingDemandDrivenPipeline::MAXIMUM_NUMBER_OF_PIECES(),-1);
189 outInfo->Set(vtkDataObject::DATA_TYPE_NAME(),"vtkMultiBlockDataSet");
190 this->UpdateSIL(outInfo);
193 this->PublishTimeStepsIfNeeded(outInfo,dummy);
195 catch(INTERP_KERNEL::Exception& e)
197 std::cerr << "Exception has been thrown in vtkMEDReader::RequestInformation : " << e.what() << std::endl;
203 int vtkMEDReader::RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
205 //std::cerr << "########################################## RequestData ##########################################";
210 vtkInformation *outInfo(outputVector->GetInformationObject(0));
211 vtkMultiBlockDataSet *output(vtkMultiBlockDataSet::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())));
212 bool isUpdated(false);
214 if(outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP()))
215 reqTS=outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP());
216 //std::cerr << reqTS << std::endl;
217 this->FillMultiBlockDataSetInstance(output,reqTS);
218 output->GetInformation()->Set(vtkDataObject::DATA_TIME_STEP(),reqTS);
219 this->UpdateSIL(outInfo);
220 //this->UpdateProgress((float) progress/((float) maxprogress-1));
222 catch(INTERP_KERNEL::Exception& e)
224 std::cerr << "Exception has been thrown in vtkMEDReader::RequestInformation : " << e.what() << std::endl;
230 void vtkMEDReader::SetFieldsStatus(const char* name, int status)
232 //std::cerr << "vtkMEDReader::SetFieldsStatus(" << name << "," << status << ") called !" << std::endl;
233 this->Internal->Tree.changeStatusOfAndUpdateToHaveCoherentVTKDataSet(this->Internal->Tree.getIdHavingZeName(name),status);
234 if(std::string(name)==GetFieldsTreeArrayName(GetNumberOfFieldsTreeArrays()-1))
235 if(!this->Internal->PluginStart0())
239 int vtkMEDReader::GetNumberOfFieldsTreeArrays()
243 int ret(this->Internal->Tree.getNumberOfLeavesArrays());
244 //std::cerr << "vtkMEDReader::GetNumberOfFieldsTreeArrays called ! " << ret << std::endl;
248 const char *vtkMEDReader::GetFieldsTreeArrayName(int index)
250 std::string ret(this->Internal->Tree.getNameOf(index));
251 //std::cerr << "vtkMEDReader::GetFieldsTreeArrayName(" << index << ") called ! " << ret << std::endl;
255 int vtkMEDReader::GetFieldsTreeArrayStatus(const char *name)
257 int zeId(this->Internal->Tree.getIdHavingZeName(name));
258 int ret(this->Internal->Tree.getStatusOf(zeId));
262 void vtkMEDReader::SetTimesFlagsStatus(const char *name, int status)
265 std::istringstream iss(name); iss >> pos;
266 this->Internal->TK.getTimesFlagArray()[pos].first=(bool)status;
267 if(pos==this->Internal->TK.getTimesFlagArray().size()-1)
268 if(!this->Internal->PluginStart0())
271 //this->Internal->TK.printSelf(std::cerr);
275 int vtkMEDReader::GetNumberOfTimesFlagsArrays()
279 return (int)this->Internal->TK.getTimesFlagArray().size();
282 const char *vtkMEDReader::GetTimesFlagsArrayName(int index)
284 return this->Internal->TK.getTimesFlagArray()[index].second.c_str();
287 int vtkMEDReader::GetTimesFlagsArrayStatus(const char *name)
290 std::istringstream iss(name); iss >> pos;
291 return (int)this->Internal->TK.getTimesFlagArray()[pos].first;
294 void vtkMEDReader::UpdateSIL(vtkInformation *info)
298 vtkMutableDirectedGraph *sil(vtkMutableDirectedGraph::New());
299 std::string meshName(this->BuildSIL(sil));
300 if(meshName!=this->Internal->DftMeshName)
302 if(this->Internal->SIL)
303 this->Internal->SIL->Delete();
304 this->Internal->SIL=sil;
305 this->Internal->DftMeshName=meshName;
306 info->Set(vtkDataObject::SIL(),this->Internal->SIL);
307 //request->AppendUnique(vtkExecutive::KEYS_TO_COPY(),vtkDataObject::SIL());
316 * The returned string is the name of the mesh activated which groups and families are in \a sil.
318 std::string vtkMEDReader::BuildSIL(vtkMutableDirectedGraph* sil)
321 vtkSmartPointer<vtkVariantArray> childEdge(vtkSmartPointer<vtkVariantArray>::New());
322 childEdge->InsertNextValue(0);
323 vtkSmartPointer<vtkVariantArray> crossEdge(vtkSmartPointer<vtkVariantArray>::New());
324 crossEdge->InsertNextValue(1);
325 // CrossEdge is an edge linking hierarchies.
326 vtkUnsignedCharArray* crossEdgesArray=vtkUnsignedCharArray::New();
327 crossEdgesArray->SetName("CrossEdges");
328 sil->GetEdgeData()->AddArray(crossEdgesArray);
329 crossEdgesArray->Delete();
330 std::vector<std::string> names;
331 // Now build the hierarchy.
332 vtkIdType rootId=sil->AddVertex();
333 names.push_back("SIL");
334 // Add global fields root
335 vtkIdType fieldsRoot(sil->AddChild(rootId,childEdge));
336 names.push_back("FieldsStatusTree");
337 this->Internal->Tree.feedSIL(sil,fieldsRoot,childEdge,names);
338 vtkIdType meshesFamsGrpsRoot(sil->AddChild(rootId,childEdge));
339 names.push_back("MeshesFamsGrps");
340 std::string dftMeshName(this->Internal->Tree.feedSILForFamsAndGrps(sil,meshesFamsGrpsRoot,childEdge,names));
341 // This array is used to assign names to nodes.
342 vtkStringArray *namesArray(vtkStringArray::New());
343 namesArray->SetName("Names");
344 namesArray->SetNumberOfTuples(sil->GetNumberOfVertices());
345 sil->GetVertexData()->AddArray(namesArray);
346 namesArray->Delete();
347 std::vector<std::string>::const_iterator iter;
349 for(cc=0, iter=names.begin(); iter!=names.end(); ++iter, ++cc)
350 namesArray->SetValue(cc,(*iter).c_str());
354 double vtkMEDReader::PublishTimeStepsIfNeeded(vtkInformation *outInfo, bool& isUpdated)
357 std::vector<double> tsteps;
358 if(!this->Internal->IsStdOrMode)
359 tsteps=this->Internal->Tree.getTimeSteps(lev0,this->Internal->TK);
361 { tsteps.resize(1); tsteps[0]=0.; }
363 if(lev0!=this->Internal->LastLev0)
367 timeRange[0]=tsteps.front();
368 timeRange[1]=tsteps.back();
369 outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(),&tsteps[0],tsteps.size());
370 outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_RANGE(),timeRange,2);
371 this->Internal->LastLev0=lev0;
373 return tsteps.front();
376 void vtkMEDReader::FillMultiBlockDataSetInstance(vtkMultiBlockDataSet *output, double reqTS)
378 std::string meshName;
379 vtkDataSet *ret(this->Internal->Tree.buildVTKInstance(this->Internal->IsStdOrMode,reqTS,meshName,this->Internal->TK));
380 if(this->Internal->GenerateVect)
382 vtkGenerateVectors::Operate(ret->GetPointData());
383 vtkGenerateVectors::Operate(ret->GetCellData());
384 vtkGenerateVectors::Operate(ret->GetFieldData());
386 output->SetBlock(0,ret);
390 void vtkMEDReader::PrintSelf(ostream& os, vtkIndent indent)
392 this->Superclass::PrintSelf(os, indent);