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>
9 #include "VISU_DatConvertor.hxx"
13 static int MYDEBUG = 1;
15 static int MYDEBUG = 0;
17 static int PRECISION = 7;
20 VISU_Convertor* CreateDatConvertor(const string& theFileName) throw(std::runtime_error&){
21 VISU_DatConvertor* aConvertor = new VISU_DatConvertor(theFileName);
25 VISU_DatConvertor::VISU_DatConvertor(const string& theFileName) throw(std::runtime_error&){
26 myFileInfo.setFile(QString(theFileName.c_str()));
27 myName = myFileInfo.baseName();
30 VISU_Convertor* VISU_DatConvertor::Build() throw (std::runtime_error&){
32 ifstream stmIn(myFileInfo.absFilePath());
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);
48 int VISU_DatConvertor::ImportField(ifstream* pStmIn, int theNbPoints, vector<int>* pCellsType)
49 throw (std::runtime_error&)
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;
68 throw std::runtime_error("ImportChamp >> elements doesn't connected to points or cells");
70 MESSAGE("FieldInfo - myMeshName = "<<aField.myMeshName<<"; myNbComp = "<<aField.myNbComp);
71 for(int i = 0, tmp; i < iEnd; i++) {
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);
84 int VISU_DatConvertor::ImportHead(ifstream* pStmIn, int* theNbPoints,
85 int* theNbCells, int* theNbFields)
86 throw (std::runtime_error&)
89 ifstream &stmIn = *pStmIn;
90 const int BUFSIZE = 256;
92 stmIn.getline(buf,BUFSIZE);
93 istrstream strIn(buf);
94 VISU::TMesh& aMesh = myMeshMap[myName];
96 strIn>>(*theNbPoints); strIn>>(*theNbCells); strIn>>(*theNbFields);
98 MESSAGE("ImportHead - theNbPoints = "<<(*theNbPoints)<<
99 "; theNbCells = "<<(*theNbCells)<<
100 "; theNbFields = "<<(*theNbFields));
105 int VISU_DatConvertor::ImportPoints(ifstream* pStmIn, int theNbPoints)
106 throw (std::runtime_error&)
109 ifstream &stmIn = *pStmIn;
110 VISU::TMesh& aMesh = myMeshMap[myName];
111 aMesh.myName = myName;
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;){
119 stmIn>>aPointsCoord[i++]; stmIn>>aPointsCoord[i++]; stmIn>>aPointsCoord[i++];
125 int VISU_DatConvertor::ImportCells(ifstream* pStmIn, vector<int>* pCellsType)
126 throw (std::runtime_error&)
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++){
147 aVector[j] = tmp - 1;
150 if(MYDEBUG) MESSAGE("ImportCells - theNbCells - End");
155 int VISU_Convertor_impl::ToDatFile(const string& theFileName) throw(std::runtime_error&){
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);
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;
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){
180 stmOut<<aMesh.myPointsCoord[j]<<"\t";
181 stmOut<<aMesh.myPointsCoord[j+1]<<"\t";
182 stmOut<<aMesh.myPointsCoord[j+2]<<endl;
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++){
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++){
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";
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();
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);
215 stmOut<<(prefix + aField.myNbComp)<<"\t"<<jEnd<<endl;
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) {
223 for (int k = 0; k < kEnd;) {
225 stmOut<<anArray[k++]<<"\n";
229 for (int k = 0; k < kEnd;){
231 stmOut<<anArray[k++]<<"\t";
232 stmOut<<anArray[k++]<<"\t";
237 for (int k = 0; k < kEnd;){
239 stmOut<<anArray[k++]<<"\t";
240 stmOut<<anArray[k++]<<"\t";
241 stmOut<<anArray[k++]<<"\n";