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