]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_Convertor.cxx
Salome HOME
NRI : Remove dependence with KERNEL.
[modules/visu.git] / src / VISU_I / VISU_Convertor.cxx
1 using namespace std;
2
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>
7
8
9 #include "VISU_Convertor.hxx"
10 #include <vtkCellType.h>
11 #include <qdir.h>
12 #include <qfileinfo.h>
13 #include <qstringlist.h>
14 #include <memory>       
15 using namespace std;
16
17 #ifdef DEBUG
18 static int MYDEBUG = 1;
19 #else
20 static int MYDEBUG = 1;
21 #endif
22
23 namespace VISU{
24   TVtkCellInfoMap aVtkCellInfoMap;
25   static int INIT = (
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),
34                      1);
35
36   pair<int,int> TMeshOnEntity::GetCellsDims(const string& theFamilyName) const
37     throw(std::runtime_error&)
38       {
39         bool isFamilyPresent = (theFamilyName != "");
40         int aNbCells = 0, aCellsSize = 0;
41         if(!isFamilyPresent){
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);
48             }
49           }
50         }else{
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();
61               aNbCells += tmp;
62               int aVtkType = aSubMeshIter->first;
63               int aVtkSize = aVtkCellInfoMap[aVtkType].mySize;
64               aCellsSize += tmp*(aVtkSize+1);
65             }
66           }
67         }
68         return make_pair(aNbCells,aCellsSize);
69       }
70
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);
77   }
78
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);
86     }
87     return NULL;
88   }
89
90   const TFamily* GetFamily(const VISU::TMeshOnEntity& theMeshOnEntity, const string& theFamilyName)
91     throw(std::runtime_error&) 
92       {
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;
99         return &aFamily;
100       }
101   
102   TFamily* GetFamily(VISU::TMeshOnEntity& theMeshOnEntity, const string& theFamilyName)
103     throw(std::runtime_error&) 
104       {
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;
111         return &aFamily;
112       }
113   
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];
121   }
122 }
123
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);
129 }
130
131 const VISU::TField* VISU_Convertor::GetField(const string& theMeshName, VISU::TEntity theEntity, 
132                                              const string& theFieldName) const
133 {
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);
144 }
145
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);
151   else
152     aName.sprintf("%g, %s",aTime.first,aTime.second.c_str());
153   aName = aName.simplifyWhiteSpace();
154   return aName.latin1();
155 }
156
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)
168     aName = aNewName;
169   else
170     aName.sprintf("%s_%d",aNewName,theTimeId);
171   return aName.latin1();
172 }
173
174 void parseFile(const char* theFileName) throw(std::runtime_error&){
175   try{
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;
184       //Import fields
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);
199           }
200         }
201       }
202       //Importing groups
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);
208       }
209       //Import families
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);
220         }
221       }
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);
227       }
228     }
229   }catch(std::runtime_error& exc){
230     MESSAGE("Follow exception was accured in file:"<<theFileName<<"\n"<<exc.what());
231   }catch(...){
232     MESSAGE("Unknown exception was accured in VISU_Convertor_impl in file:"<<theFileName);
233   } 
234 }
235
236 int main(int argc, char** argv){ 
237   try{
238     if(argc > 1){
239       for(int i = 0; i < 1; i++){
240         QFileInfo fi(argv[1]);
241         if ( fi.exists() ) {
242           if ( fi.isDir() ) {
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());
249           }else
250             parseFile(argv[1]);
251         }
252       } 
253       /* 
254       aCon->ToString(); 
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());
264       */
265       return 0;
266     }
267   }catch(std::runtime_error& exc){
268     MESSAGE("Follow exception was accured :\n"<<exc.what());
269   }catch(...){
270     MESSAGE("Unknown exception was accured in VISU_Convertor_impl");
271   } 
272   return 1;
273 }
274
275 /*
276 #include "VISU_Extractor.hxx"
277 #include "VISU_FieldTransform.hxx"
278 #include "VISU_LookupTable.hxx"
279 #include "VISU_ScalarBarActor.hxx"
280
281 #include <vtkRenderWindow.h>
282 #include <vtkRenderer.h>
283 #include <vtkRenderWindowInteractor.h>
284 #include <vtkActor.h>
285
286 #include <vtkUnstructuredGridReader.h>
287 #include <vtkDataSetMapper.h>
288 #include <vtkUnstructuredGrid.h>
289 #include <vtkDataSet.h>
290 #include <vtkUnstructuredGridWriter.h>
291
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);
309   if(argc > 2) 
310     aFieldTransform->SetTransformFunction(&log10);
311   vtkDataSetMapper *aMapper = vtkDataSetMapper::New();
312   aMapper->SetInput(aFieldTransform->GetUnstructuredGridOutput());
313   aMapper->ScalarVisibilityOn();
314   anExtractor->GetUnstructuredGridOutput()->GetScalarRange(aScalarRange);
315
316   VISU_LookupTable* aActorLookupTbl = VISU_LookupTable::New();
317   cout<<"aActorLookupTbl = "<<aActorLookupTbl<<endl;
318   if(argc > 2) 
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();
328
329   vtkActor* aActor = vtkActor::New();
330   aActor->SetMapper(aMapper);
331
332   VISU_ScalarBarActor* aBar = VISU_ScalarBarActor::New();
333   VISU_LookupTable* aBarLookupTbl = VISU_LookupTable::New();
334   cout<<"aBarLookupTbl = "<<aBarLookupTbl<<endl;
335   if(argc > 2) 
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();
343
344   aBar->SetTitle((aFieldInfo.myName).c_str());
345   aBar->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
346   aBar->SetNumberOfLabels(aNumberOfLabels);
347
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();     
355
356   ren->AddActor(aActor);
357   ren->AddActor2D(aBar);
358   
359   renWin->Render();
360   iren->Start();
361   return 0;
362 }
363 */
364
365 /*
366 #include <fstream>      
367 #include <stdlib.h>
368
369 int main(int argc, char** argv){ 
370   try{
371     if(argc > 1){
372       int iEnd = 1;
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();
379       }
380     }
381     return 0;
382   }catch(std::runtime_error& exc){
383     MESSAGE("Follow exception was accured :\n"<<exc.what());
384     return 1;
385   }catch(...){
386     MESSAGE("Unknown exception was accured in VISU_Convertor_impl");
387     return 1;
388   } 
389 }
390 */