3 // File: VISU_Convertor.cxx
4 // Created: Wed Jan 15 17:43:32 2003
5 // Author: Alexey PETROV
6 // <apo@ivanox.nnov.matra-dtv.fr>
9 #include "VISU_Convertor.hxx"
10 #include <vtkCellType.h>
12 #include <qfileinfo.h>
13 #include <qstringlist.h>
18 static int MYDEBUG = 1;
20 static int MYDEBUG = 1;
24 TVtkCellInfoMap aVtkCellInfoMap;
26 aVtkCellInfoMap[VTK_VERTEX] = TVtkCellInfo("VTK_VERTEX",1),
27 aVtkCellInfoMap[VTK_LINE] = TVtkCellInfo("VTK_LINE",2),
28 aVtkCellInfoMap[VTK_TRIANGLE] = TVtkCellInfo("VTK_TRIANGLE",3),
29 aVtkCellInfoMap[VTK_QUAD] = TVtkCellInfo("VTK_QUAD",4),
30 aVtkCellInfoMap[VTK_TETRA] = TVtkCellInfo("VTK_TETRA",4),
31 aVtkCellInfoMap[VTK_HEXAHEDRON] = TVtkCellInfo("VTK_HEXAHEDRON",8),
32 aVtkCellInfoMap[VTK_WEDGE] = TVtkCellInfo("VTK_WEDGE",6),
33 aVtkCellInfoMap[VTK_PYRAMID] = TVtkCellInfo("VTK_PYRAMID",5),
36 pair<int,int> TMeshOnEntity::GetCellsDims(const string& theFamilyName) const
37 throw(std::runtime_error&)
39 bool isFamilyPresent = (theFamilyName != "");
40 int aNbCells = 0, aCellsSize = 0;
42 TCellsConn::const_iterator aCellsConnIter = myCellsConn.begin();
43 for(; aCellsConnIter != myCellsConn.end(); aCellsConnIter++){
44 const TConnForCellType& aConnForCellType = aCellsConnIter->second;
45 if(!aConnForCellType.empty()){
46 aNbCells += aConnForCellType.size();
47 aCellsSize += aConnForCellType.size()*(aConnForCellType[0].size()+1);
51 TFamilyMap::const_iterator aFamilyMapIter = myFamilyMap.find(theFamilyName);
52 if(aFamilyMapIter == myFamilyMap.end())
53 throw std::runtime_error("GetCellsDims >> There is no family on the mesh with entity !!!");
54 const TFamily& aFamily = aFamilyMapIter->second;
55 const TFamily::TSubMesh& aSubMesh = aFamily.mySubMesh;
56 TFamily::TSubMesh::const_iterator aSubMeshIter = aSubMesh.begin();
57 for(; aSubMeshIter != aSubMesh.end(); aSubMeshIter++){
58 const TFamily::TSubMeshOnCellType& aSubMeshOnCellType = aSubMeshIter->second;
59 if(!aSubMeshOnCellType.empty()){
60 int tmp = aSubMeshOnCellType.size();
62 int aVtkType = aSubMeshIter->first;
63 int aVtkSize = aVtkCellInfoMap[aVtkType].mySize;
64 aCellsSize += tmp*(aVtkSize+1);
68 return make_pair(aNbCells,aCellsSize);
71 void TMesh::CreateMeshOnNodes(){
72 TMeshOnEntity& aMeshOnEntity = myMeshOnEntityMap[NODE_ENTITY];
73 TMeshOnEntity::TConnForCellType& aConnForCellType = aMeshOnEntity.myCellsConn[VTK_VERTEX];
74 int jEnd = myPointsCoord.size()/myDim;
75 aConnForCellType.resize(jEnd);
76 for (int j = 0; j < jEnd; j++) aConnForCellType[j] = TMeshOnEntity::TConnect(1,j);
79 const TField* TMesh::GetField(const string& theFieldName) const {
80 TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter = myMeshOnEntityMap.begin();
81 for(; aMeshOnEntityMapIter != myMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
82 const TFieldMap& aFieldMap = (aMeshOnEntityMapIter->second).myFieldMap;
83 TFieldMap::const_iterator aFieldMapIter = aFieldMap.begin();
84 for(; aFieldMapIter != aFieldMap.end(); aFieldMapIter++)
85 if(theFieldName == aFieldMapIter->first) return &(aFieldMapIter->second);
90 const TFamily* GetFamily(const VISU::TMeshOnEntity& theMeshOnEntity, const string& theFamilyName)
91 throw(std::runtime_error&)
93 if(theFamilyName == "") return NULL;
94 const VISU::TFamilyMap& aFamilyMap = theMeshOnEntity.myFamilyMap;
95 VISU::TFamilyMap::const_iterator aFamilyMapIter = aFamilyMap.find(theFamilyName);
96 if(aFamilyMapIter == aFamilyMap.end())
97 throw std::runtime_error("GetFamily >> There is no family on the mesh with entity !!!");
98 const VISU::TFamily& aFamily = aFamilyMapIter->second;
102 TFamily* GetFamily(VISU::TMeshOnEntity& theMeshOnEntity, const string& theFamilyName)
103 throw(std::runtime_error&)
105 if(theFamilyName == "") return NULL;
106 VISU::TFamilyMap& aFamilyMap = theMeshOnEntity.myFamilyMap;
107 VISU::TFamilyMap::iterator aFamilyMapIter = aFamilyMap.find(theFamilyName);
108 if(aFamilyMapIter == aFamilyMap.end())
109 throw std::runtime_error("GetFamily >> There is no family on the mesh with entity !!!");
110 VISU::TFamily& aFamily = aFamilyMapIter->second;
114 void TField::ShallowCopy(const TField& aField){
115 myEntity = aField.myEntity;
116 myMeshName = aField.myMeshName;
117 myNbComp = aField.myNbComp;
118 VISU::TField::TValField::const_iterator iter = aField.myValField.begin();
119 for(; iter != aField.myValField.end(); iter++)
120 myValField[iter->first];
124 const VISU::TMesh* VISU_Convertor::GetMesh(const string& theMeshName) const {
125 string aMeshName(theMeshName == ""? myMeshMap.begin()->first: theMeshName);
126 VISU::TMeshMap::const_iterator aMeshMapIter = myMeshMap.find(theMeshName);
127 if(aMeshMapIter == myMeshMap.end()) return NULL;
128 return &(aMeshMapIter->second);
131 const VISU::TField* VISU_Convertor::GetField(const string& theMeshName, VISU::TEntity theEntity,
132 const string& theFieldName) const
134 VISU::TMeshMap::const_iterator aMeshMapIter = myMeshMap.find(theMeshName);
135 if(aMeshMapIter == myMeshMap.end()) return NULL;
136 const VISU::TMesh& aMesh = aMeshMapIter->second;
137 VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter = aMesh.myMeshOnEntityMap.find(theEntity);
138 if(aMeshOnEntityMapIter == aMesh.myMeshOnEntityMap.end()) return NULL;
139 const VISU::TMeshOnEntity& aMeshOnEntity = aMeshOnEntityMapIter->second;
140 const VISU::TFieldMap& aFieldMap = aMeshOnEntity.myFieldMap;
141 VISU::TFieldMap::const_iterator aFieldMapIter = aFieldMap.find(theFieldName);
142 if(aFieldMapIter == aFieldMap.end()) return NULL;
143 return &(aFieldMapIter->second);
146 string VISU_Convertor::GenerateName(const VISU::TField::TTime& aTime){
147 static QString aName;
148 const string aUnits = aTime.second, tmp(aUnits.size(),' ');
149 if(aUnits == "" || aUnits == tmp)
150 aName.sprintf("%g, ---",aTime.first);
152 aName.sprintf("%g, %s",aTime.first,aTime.second.c_str());
153 aName = aName.simplifyWhiteSpace();
154 return aName.latin1();
157 string VISU_Convertor::GenerateName(const string& theName, unsigned int theTimeId) {
158 static QString aName;
159 aName = QString(theName.c_str()).simplifyWhiteSpace();
160 int iEnd = strlen(aName);
161 static int VtkHighLevelLength = 12; //25
162 if(iEnd > VtkHighLevelLength) iEnd = VtkHighLevelLength;
163 char aNewName[iEnd+1];
164 aNewName[iEnd] = '\0';
165 strncpy(aNewName,aName,iEnd);
166 replace(aNewName,aNewName+iEnd,' ','_');
167 if(true || theTimeId == 0)
170 aName.sprintf("%s_%d",aNewName,theTimeId);
171 return aName.latin1();
174 void parseFile(const char* theFileName) throw(std::runtime_error&){
176 auto_ptr<VISU_Convertor> aCon(CreateConvertor(theFileName));
177 const VISU::TMeshMap& aMeshMap = *(aCon->GetMeshMap());
178 VISU::TMeshMap::const_iterator aMeshMapIter = aMeshMap.begin();
179 for(; aMeshMapIter != aMeshMap.end(); aMeshMapIter++){
180 const string& aMeshName = aMeshMapIter->first;
181 const VISU::TMesh& aMesh = aMeshMapIter->second;
182 const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh.myMeshOnEntityMap;
183 VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter;
185 aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
186 for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
187 const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
188 const VISU::TMeshOnEntity& aMeshOnEntity = aMeshOnEntityMapIter->second;
189 const VISU::TFieldMap& aFieldMap = aMeshOnEntity.myFieldMap;
190 VISU::TFieldMap::const_iterator aFieldMapIter = aFieldMap.begin();
191 for(; aFieldMapIter != aFieldMap.end(); aFieldMapIter++){
192 const string& aFieldName = aFieldMapIter->first;
193 const VISU::TField& aField = aFieldMapIter->second;
194 const VISU::TField::TValField& aValField = aField.myValField;
195 VISU::TField::TValField::const_iterator aValFieldIter = aValField.begin();
196 for(; aValFieldIter != aValField.end(); aValFieldIter++){
197 int aTimeStamp = aValFieldIter->first;
198 aCon->GetFieldOnMesh(aMeshName,anEntity,aFieldName,aTimeStamp);
203 const VISU::TGroupMap& aGroupMap = aMesh.myGroupMap;
204 VISU::TGroupMap::const_iterator aGroupMapIter = aGroupMap.begin();
205 for(; aGroupMapIter != aGroupMap.end(); aGroupMapIter++){
206 const string& aGroupName = aGroupMapIter->first;
207 aCon->GetMeshOnGroup(aMeshName,aGroupName);
210 aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
211 for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
212 const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
213 const VISU::TMeshOnEntity& aMeshOnEntity = aMeshOnEntityMapIter->second;
214 //aCon->GetMeshOnEntity(aMeshName,anEntity);
215 const VISU::TFamilyMap& aFamilyMap = aMeshOnEntity.myFamilyMap;
216 VISU::TFamilyMap::const_iterator aFamilyMapIter = aFamilyMap.begin();
217 for(; aFamilyMapIter != aFamilyMap.end(); aFamilyMapIter++){
218 const string& aFamilyName = aFamilyMapIter->first;
219 aCon->GetMeshOnEntity(aMeshName,anEntity,aFamilyName);
222 //Import mesh on entity
223 aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
224 for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
225 const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
226 aCon->GetMeshOnEntity(aMeshName,anEntity);
229 }catch(std::runtime_error& exc){
230 MESSAGE("Follow exception was accured in file:"<<theFileName<<"\n"<<exc.what());
232 MESSAGE("Unknown exception was accured in VISU_Convertor_impl in file:"<<theFileName);
236 int main(int argc, char** argv){
239 for(int i = 0; i < 1; i++){
240 QFileInfo fi(argv[1]);
243 QDir aDir(fi.absFilePath());
244 cout<<aDir.absPath()<<endl;
245 QStringList aStringList = aDir.entryList("*.med",QDir::Files);
246 int jEnd = aStringList.count();
247 for(int j = 0; j < jEnd; j++)
248 parseFile(aDir.filePath(aStringList[j]).latin1());
255 aCon->ToFile((aDir+"a.med").c_str());
256 aCon.reset(CreateConvertor((aDir+"a.med").c_str()));
257 aCon->ToFile((aDir+"a.dat").c_str());
258 auto_ptr<VISU_Convertor> aCon(CreateConvertor(argv[1]));
259 aCon->ToFile((aDir+"a.med").c_str());
260 aCon.reset(CreateConvertor((aDir+"a.med").c_str()));
261 aCon->ToFile((aDir+"a.med").c_str());
262 aCon.reset(CreateConvertor((aDir+"b.med").c_str()));
263 aCon->ToFile((aDir+"c.dat").c_str());
267 }catch(std::runtime_error& exc){
268 MESSAGE("Follow exception was accured :\n"<<exc.what());
270 MESSAGE("Unknown exception was accured in VISU_Convertor_impl");
276 #include "VISU_Extractor.hxx"
277 #include "VISU_FieldTransform.hxx"
278 #include "VISU_LookupTable.hxx"
279 #include "VISU_ScalarBarActor.hxx"
281 #include <vtkRenderWindow.h>
282 #include <vtkRenderer.h>
283 #include <vtkRenderWindowInteractor.h>
284 #include <vtkActor.h>
286 #include <vtkUnstructuredGridReader.h>
287 #include <vtkDataSetMapper.h>
288 #include <vtkUnstructuredGrid.h>
289 #include <vtkDataSet.h>
290 #include <vtkUnstructuredGridWriter.h>
292 int main(int argc, char** argv){
293 vtkRenderWindow *renWin = vtkRenderWindow::New();
294 vtkRenderer *ren = vtkRenderer::New();
295 renWin->AddRenderer(ren);
296 ren->GetActiveCamera()->ParallelProjectionOn();
297 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
298 iren->SetRenderWindow(renWin);
299 VISU_Convertor* aConvertor = CreateConvertor(argv[1]);
300 float aScalarRange[2];
301 int aFieldIndex = 1, aNumberOfColors = 64, aNumberOfLabels = 5;
302 FieldInfo aFieldInfo = *(aConvertor->GetFieldInfo(aFieldIndex));
303 VISU_Extractor *anExtractor = VISU_Extractor::New();
304 anExtractor->Extract(aConvertor->GetOutput(),aFieldInfo.myName.c_str(),0,
305 aFieldInfo.myNumComponent,aFieldInfo.myType);
306 vtkUnstructuredGrid *aDataSet = anExtractor->GetUnstructuredGridOutput();
307 VISU_FieldTransform* aFieldTransform = VISU_FieldTransform::New();
308 aFieldTransform->SetInput(aDataSet);
310 aFieldTransform->SetTransformFunction(&log10);
311 vtkDataSetMapper *aMapper = vtkDataSetMapper::New();
312 aMapper->SetInput(aFieldTransform->GetUnstructuredGridOutput());
313 aMapper->ScalarVisibilityOn();
314 anExtractor->GetUnstructuredGridOutput()->GetScalarRange(aScalarRange);
316 VISU_LookupTable* aActorLookupTbl = VISU_LookupTable::New();
317 cout<<"aActorLookupTbl = "<<aActorLookupTbl<<endl;
319 aActorLookupTbl->SetLog(true);
320 aActorLookupTbl->SetHueRange(0.667,0.0);
321 aActorLookupTbl->SetNumberOfColors(aNumberOfColors);
322 aActorLookupTbl->SetTableRange(aScalarRange[0],aScalarRange[1]); //
323 aActorLookupTbl->SetMapScale(1.);
324 aActorLookupTbl->Build();
325 aMapper->SetLookupTable(aActorLookupTbl);
326 aMapper->SetScalarRange(aScalarRange);
327 aActorLookupTbl->Delete();
329 vtkActor* aActor = vtkActor::New();
330 aActor->SetMapper(aMapper);
332 VISU_ScalarBarActor* aBar = VISU_ScalarBarActor::New();
333 VISU_LookupTable* aBarLookupTbl = VISU_LookupTable::New();
334 cout<<"aBarLookupTbl = "<<aBarLookupTbl<<endl;
336 aBarLookupTbl->SetLog(true);
337 aBarLookupTbl->SetHueRange(0.667,0.0);
338 aBarLookupTbl->SetNumberOfColors(aNumberOfColors);
339 aBarLookupTbl->SetTableRange(aScalarRange[0],aScalarRange[1]); //
340 aBarLookupTbl->Build();
341 aBar->SetLookupTable(aBarLookupTbl);
342 aBarLookupTbl->Delete();
344 aBar->SetTitle((aFieldInfo.myName).c_str());
345 aBar->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
346 aBar->SetNumberOfLabels(aNumberOfLabels);
348 vtkProperty* aProperty = aActor->GetProperty();
349 aProperty->SetSpecularColor( 1, 1, 1);
350 aProperty->SetSpecular( 0.3 );
351 aProperty->SetSpecularPower( 20 );
352 aProperty->SetAmbient( 0.2);
353 aProperty->SetDiffuse( 0.8 );
354 aProperty->EdgeVisibilityOn();
356 ren->AddActor(aActor);
357 ren->AddActor2D(aBar);
369 int main(int argc, char** argv){
373 if(argc > 2) iEnd = atoi(argv[2]);
374 string aFileName("/tmp/AAA");
375 ofstream stmOut(aFileName.c_str(),ios::out);
376 for(int i = 0; i < iEnd; i++){
377 MESSAGE("\n--------------------------- "<<i<<" ---------------------------");
378 stmOut<<CreateConvertor(argv[1])->ToString();
382 }catch(std::runtime_error& exc){
383 MESSAGE("Follow exception was accured :\n"<<exc.what());
386 MESSAGE("Unknown exception was accured in VISU_Convertor_impl");