Salome HOME
sources v1.2c
[modules/visu.git] / src / CONVERTOR / VISU_DatConvertor.cxx
1 //  Copyright (C) 2003  CEA/DEN, EDF R&D
2 //
3 //
4 //
5 //  File   : VISU_DatConvertor.cxx
6 //  Author : Alexey PETROV
7 //  Module : VISU
8
9 using namespace std;
10 #include "VISU_DatConvertor.hxx"
11 using namespace std;
12
13 #ifdef DEBUG
14 static int MYDEBUG = 1;
15 #else
16 static int MYDEBUG = 0;
17 #endif
18 static int PRECISION = 7;
19
20 extern "C"
21 VISU_Convertor* CreateDatConvertor(const string& theFileName) throw(std::runtime_error&){
22   VISU_DatConvertor* aConvertor = new VISU_DatConvertor(theFileName);
23   aConvertor->Build();
24   return aConvertor;
25 }
26 VISU_DatConvertor::VISU_DatConvertor(const string& theFileName) throw(std::runtime_error&){
27   myFileInfo.setFile(QString(theFileName.c_str()));
28   myName = (const char*)(myFileInfo.baseName());
29 }
30
31 VISU_Convertor* VISU_DatConvertor::Build() throw (std::runtime_error&){
32   /*
33   ifstream stmIn(myFileInfo.absFilePath());
34   if(!stmIn.good())
35     throw std::runtime_error(string("VISU_DatConvertor::CreateResult() >> can't open file - ") + 
36                              (const char*)myFileInfo.absFilePath());
37   int aNbPoints, aNbCells, aNbFields;
38   ImportHead(&stmIn,&aNbPoints,&aNbCells,&aNbFields);
39   ImportPoints(&stmIn,aNbPoints);
40   vector<int> aCellsType(aNbCells);
41   ImportCells(&stmIn,&aCellsType);
42   for(int i = 0; i < aNbFields; i++)
43     ImportField(&stmIn,aNbPoints,&aCellsType);
44   myIsDone = true;
45   */
46   return this; 
47 }
48
49 int VISU_DatConvertor::ImportField(ifstream* pStmIn, int theNbPoints, vector<int>* pCellsType)
50      throw (std::runtime_error&)
51 {
52   /*
53   ifstream &stmIn = *pStmIn;
54   vector<int>& aCellsType = *pCellsType;
55   int aNbCells = aCellsType.size();
56   string aName;  stmIn>>aName; stmIn>>aName; 
57   VISU::TMesh& aMesh = myMeshMap[myName];
58   VISU::TMeshOnEntity& aMeshOnEntity = aMesh.myMeshOnEntityMap[VISU::CELL_ENTITY];
59   VISU::TField& aField = aMeshOnEntity.myFieldMap[aName];
60   aField.myMeshName = myName;
61   stmIn>>(aField.myNbComp); 
62   aField.myNbComp %= 100;
63   int iEnd;  stmIn>>iEnd; 
64   if(iEnd == theNbPoints)
65     aField.myEntity = VISU::NODE_ENTITY; 
66   else if(iEnd == aNbCells)
67     aField.myEntity = VISU::CELL_ENTITY;
68   else
69     throw std::runtime_error("ImportChamp >> elements doesn't connected to points or cells");
70   if(MYDEBUG)
71     MESSAGE("FieldInfo - myMeshName = "<<aField.myMeshName<<"; myNbComp = "<<aField.myNbComp);
72   for(int i = 0, tmp; i < iEnd; i++) {
73     stmIn>>tmp;
74     VISU::TField::TTime aTime(0.0,"");
75     VISU::TField::TValForCellsWithType &anArray = aField.myValField[aTime][aCellsType[i]];
76     for(int j = 0; j < aField.myNbComp; j++){
77       float tmp;  stmIn>>tmp;
78       anArray.push_back(tmp);
79     }
80   }
81   */
82   return 0; 
83 }
84
85 int VISU_DatConvertor::ImportHead(ifstream* pStmIn, int* theNbPoints, 
86                                   int* theNbCells, int* theNbFields) 
87      throw (std::runtime_error&)
88 {
89   /*
90   ifstream &stmIn = *pStmIn;
91   const int BUFSIZE = 256;
92   char buf[BUFSIZE];
93   stmIn.getline(buf,BUFSIZE);
94   istrstream strIn(buf);
95   VISU::TMesh& aMesh = myMeshMap[myName];
96   *theNbFields = 0;
97   strIn>>(*theNbPoints); strIn>>(*theNbCells); strIn>>(*theNbFields);
98   if(MYDEBUG)
99     MESSAGE("ImportHead - theNbPoints = "<<(*theNbPoints)<<
100             "; theNbCells = "<<(*theNbCells)<<
101             "; theNbFields = "<<(*theNbFields));
102   */
103   return 0;
104 }
105
106 int VISU_DatConvertor::ImportPoints(ifstream* pStmIn, int theNbPoints) 
107      throw (std::runtime_error&)
108 {
109   /*
110   ifstream &stmIn = *pStmIn;
111   VISU::TMesh& aMesh = myMeshMap[myName];
112   aMesh.myName = myName;
113   aMesh.myDim = 3;
114   int iEnd = aMesh.myDim*theNbPoints;
115   if(MYDEBUG) MESSAGE("ImportPoints - iEnd = "<<iEnd);
116   vector<float> &aPointsCoord = aMesh.myPointsCoord;
117   aPointsCoord.resize(iEnd);
118   for(int i = 0, tmp; i < iEnd;){
119     stmIn>>tmp;  
120     stmIn>>aPointsCoord[i++]; stmIn>>aPointsCoord[i++]; stmIn>>aPointsCoord[i++];
121   }
122   */
123   return 0;
124 }
125
126 int VISU_DatConvertor::ImportCells(ifstream* pStmIn, vector<int>* pCellsType) 
127      throw (std::runtime_error&)
128 {
129   /*
130   ifstream &stmIn = *pStmIn;
131   vector<int>& aCellsType = *pCellsType;
132   int aNbCells = aCellsType.size();
133   if(MYDEBUG) MESSAGE("ImportCells - theNbCells - Beginning");
134   VISU::TMesh& aMesh = myMeshMap[myName];
135   VISU::TMeshOnEntity& aMeshOnEntity = aMesh.myMeshOnEntityMap[VISU::CELL_ENTITY];
136   aMeshOnEntity.myEntity = VISU::CELL_ENTITY;
137   aMeshOnEntity.myMeshName = myName;
138   for(int i = 0, aMedType, aVtkType; i < aNbCells; i++){
139     stmIn>>aMedType; stmIn>>aMedType;
140     aCellsType[i] = aVtkType = med2vtkCellType(aMedType);
141     VISU::TMeshOnEntity::TConnForCellType& anArray = aMeshOnEntity.myCellsConn[aVtkType];
142     anArray.push_back(VISU::TMeshOnEntity::TConnect());
143     VISU::TMeshOnEntity::TConnect& aVector = anArray.back();
144     int jEnd = getNbMedNodes(aMedType);
145     aVector.resize(jEnd);
146     for(int j = 0, tmp; j < jEnd; j++){
147       stmIn>>tmp; 
148       aVector[j] = tmp - 1;
149     }
150   }
151   if(MYDEBUG) MESSAGE("ImportCells - theNbCells - End");
152   */
153   return 0;
154 }
155
156 /*
157 int VISU_Convertor_impl::ToDatFile(const string& theFileName) throw(std::runtime_error&){
158   if(MYDEBUG) MESSAGE("VISU_Convertor_impl::ToDatFile - "<<theFileName);
159   if(!myIsDone && Build() != NULL) myIsDone = true;
160   ofstream stmOut(theFileName.c_str(),ios::out);
161   stmOut.precision(PRECISION);
162   stmOut.setf(ios::scientific,ios::floatfield);
163   
164   VISU::TMeshMap::const_iterator aMeshIter = myMeshMap.begin();
165   if(aMeshIter != myMeshMap.end()){
166     const VISU::TMesh &aMesh = aMeshIter->second;
167     if(MYDEBUG) MESSAGE("VISU_Convertor_impl::ToDatFile - aMeshName = "<<aMeshIter->first);
168     VISU::TFieldMap aFieldMap = GetFieldMapForMesh(aMeshIter->first);
169     const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh.myMeshOnEntityMap;
170     VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter = aMeshOnEntityMap.find(VISU::CELL_ENTITY);
171     if(aMeshOnEntityMapIter != aMeshOnEntityMap.end()){
172       const VISU::TMeshOnEntity& aMeshOnEntity = aMeshOnEntityMapIter->second;
173       int aNbPoints = aMesh.myPointsCoord.size()/aMesh.myDim;
174       int aNbCells = aMeshOnEntity.GetCellsDims().first;
175       stmOut<<aNbPoints<<"\t"<<aNbCells<<"\t"<<aFieldMap.size()<<endl;
176       if(MYDEBUG) 
177         MESSAGE("VISU_Convertor_impl::ToDatFile - aNbPoints = "<<aNbPoints<<
178                 "; aNbCells = "<<aNbCells<<"; aNbFields = "<<aFieldMap.size());
179       for(int i = 0, j = 0; i < aNbPoints; i++, j = 3*i){
180         stmOut<<(i+1)<<"\t"; 
181         stmOut<<aMesh.myPointsCoord[j]<<"\t"; 
182         stmOut<<aMesh.myPointsCoord[j+1]<<"\t";
183         stmOut<<aMesh.myPointsCoord[j+2]<<endl;
184       } 
185       const VISU::TMeshOnEntity::TCellsConn &aCellsConn = aMeshOnEntity.myCellsConn;
186       VISU::TMeshOnEntity::TCellsConn::const_iterator iter = aCellsConn.begin();
187       for(int i = 0; iter != aCellsConn.end(); iter++){
188         if(MYDEBUG) 
189           MESSAGE("VISU_Convertor_impl::ToDatFile - vtkCellType = "<<iter->first);
190         const VISU::TMeshOnEntity::TConnForCellType& anArray = iter->second;
191         for(int j = 0, jEnd = anArray.size(); j < jEnd; j++){
192           stmOut<<(++i)<<"\t";
193           stmOut<<vtk2medCellType(iter->first)<<"\t";
194           const vector<int>& aVector = anArray[j];
195           int kEnd = aVector.size();
196           for(int k = 0; k < kEnd; k++)
197             stmOut<<aVector[k]<<"\t";
198           stmOut<<endl;
199         }
200       }
201       if(MYDEBUG) MESSAGE("VISU_Convertor_impl::ToDatFile - Beginning of Field Conversion");
202       VISU::TFieldMap::const_iterator aFieldIter = aFieldMap.begin();
203       for(int i = 0; aFieldIter != aFieldMap.end(); aFieldIter++){
204         const VISU::TField &aField = aFieldIter->second;
205         const VISU::TField::TValField &aValField = aField.myValField;
206         VISU::TField::TValField::const_reverse_iterator iter = aField.myValField.rbegin();
207         for(int i = 0; iter != aValField.rend(); iter++, i++) {
208           if(MYDEBUG) MESSAGE("VISU_Convertor_impl::ToDatFile - aTime = "<<i);
209           const VISU::TField::TValForTime& aValForTime = iter->second;
210           VISU::TField::TValForTime::const_iterator jter = aValForTime.begin();
211           stmOut<<(++i)<<endl;
212           int prefix = (aField.myEntity == VISU::CELL_ENTITY? 100: 200);
213           int jEnd = (aField.myEntity == VISU::CELL_ENTITY? aNbCells: aNbPoints);
214           string aName = GenerateName((aFieldIter->first).c_str(),i);
215           stmOut<<aName<<endl;
216           stmOut<<(prefix + aField.myNbComp)<<"\t"<<jEnd<<endl;
217           if(MYDEBUG) 
218             MESSAGE("VISU_Convertor_impl::ToDatFile - aName = "<<aName<<"; jEnd = "<<jEnd);
219           for(int j = 0; jter != aValForTime.end(); jter++) {
220             const VISU::TField::TValForCellsWithType& anArray = jter->second;
221             int kEnd = anArray.size(), aVtkType = jter->first;
222             switch(aField.myNbComp) {
223             case 1:
224               for (int k = 0; k < kEnd;) {
225                 stmOut<<(++j)<<"\t";
226                 stmOut<<anArray[k++]<<"\n";
227               }
228               break;
229             case 2:
230               for (int k = 0; k < kEnd;){
231                 stmOut<<(++j)<<"\t";
232                 stmOut<<anArray[k++]<<"\t";
233                 stmOut<<anArray[k++]<<"\t";
234                 stmOut<<0.0<<"\n";
235               }
236               break;
237             case 3: 
238               for (int k = 0; k < kEnd;){
239                 stmOut<<(++j)<<"\t";
240                 stmOut<<anArray[k++]<<"\t";
241                 stmOut<<anArray[k++]<<"\t";
242                 stmOut<<anArray[k++]<<"\n";
243               }
244               break;
245             }
246           }
247         }
248       }
249     }
250   }
251   return 1;
252 }
253 */