1 // Copyright (C) 2003 CEA/DEN, EDF R&D
5 // File : VISU_DatConvertor.cxx
6 // Author : Alexey PETROV
10 #include "VISU_DatConvertor.hxx"
14 static int MYDEBUG = 1;
16 static int MYDEBUG = 0;
18 static int PRECISION = 7;
21 VISU_Convertor* CreateDatConvertor(const string& theFileName) throw(std::runtime_error&){
22 VISU_DatConvertor* aConvertor = new VISU_DatConvertor(theFileName);
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());
31 VISU_Convertor* VISU_DatConvertor::Build() throw (std::runtime_error&){
33 ifstream stmIn(myFileInfo.absFilePath());
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);
49 int VISU_DatConvertor::ImportField(ifstream* pStmIn, int theNbPoints, vector<int>* pCellsType)
50 throw (std::runtime_error&)
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;
69 throw std::runtime_error("ImportChamp >> elements doesn't connected to points or cells");
71 MESSAGE("FieldInfo - myMeshName = "<<aField.myMeshName<<"; myNbComp = "<<aField.myNbComp);
72 for(int i = 0, tmp; i < iEnd; i++) {
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);
85 int VISU_DatConvertor::ImportHead(ifstream* pStmIn, int* theNbPoints,
86 int* theNbCells, int* theNbFields)
87 throw (std::runtime_error&)
90 ifstream &stmIn = *pStmIn;
91 const int BUFSIZE = 256;
93 stmIn.getline(buf,BUFSIZE);
94 istrstream strIn(buf);
95 VISU::TMesh& aMesh = myMeshMap[myName];
97 strIn>>(*theNbPoints); strIn>>(*theNbCells); strIn>>(*theNbFields);
99 MESSAGE("ImportHead - theNbPoints = "<<(*theNbPoints)<<
100 "; theNbCells = "<<(*theNbCells)<<
101 "; theNbFields = "<<(*theNbFields));
106 int VISU_DatConvertor::ImportPoints(ifstream* pStmIn, int theNbPoints)
107 throw (std::runtime_error&)
110 ifstream &stmIn = *pStmIn;
111 VISU::TMesh& aMesh = myMeshMap[myName];
112 aMesh.myName = myName;
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;){
120 stmIn>>aPointsCoord[i++]; stmIn>>aPointsCoord[i++]; stmIn>>aPointsCoord[i++];
126 int VISU_DatConvertor::ImportCells(ifstream* pStmIn, vector<int>* pCellsType)
127 throw (std::runtime_error&)
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++){
148 aVector[j] = tmp - 1;
151 if(MYDEBUG) MESSAGE("ImportCells - theNbCells - End");
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);
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;
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){
181 stmOut<<aMesh.myPointsCoord[j]<<"\t";
182 stmOut<<aMesh.myPointsCoord[j+1]<<"\t";
183 stmOut<<aMesh.myPointsCoord[j+2]<<endl;
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++){
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++){
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";
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();
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);
216 stmOut<<(prefix + aField.myNbComp)<<"\t"<<jEnd<<endl;
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) {
224 for (int k = 0; k < kEnd;) {
226 stmOut<<anArray[k++]<<"\n";
230 for (int k = 0; k < kEnd;){
232 stmOut<<anArray[k++]<<"\t";
233 stmOut<<anArray[k++]<<"\t";
238 for (int k = 0; k < kEnd;){
240 stmOut<<anArray[k++]<<"\t";
241 stmOut<<anArray[k++]<<"\t";
242 stmOut<<anArray[k++]<<"\n";