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) {
22 VISU_DatConvertor* aConvertor = new VISU_DatConvertor(theFileName);
26 VISU_DatConvertor::VISU_DatConvertor(const string& theFileName) {
27 myFileInfo.setFile(QString(theFileName.c_str()));
28 myName = (const char*)(myFileInfo.baseName());
31 VISU_Convertor* VISU_DatConvertor::Build() {
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)
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)
88 ifstream &stmIn = *pStmIn;
89 const int BUFSIZE = 256;
91 stmIn.getline(buf,BUFSIZE);
92 istrstream strIn(buf);
93 VISU::TMesh& aMesh = myMeshMap[myName];
95 strIn>>(*theNbPoints); strIn>>(*theNbCells); strIn>>(*theNbFields);
97 MESSAGE("ImportHead - theNbPoints = "<<(*theNbPoints)<<
98 "; theNbCells = "<<(*theNbCells)<<
99 "; theNbFields = "<<(*theNbFields));
104 int VISU_DatConvertor::ImportPoints(ifstream* pStmIn, int theNbPoints)
107 ifstream &stmIn = *pStmIn;
108 VISU::TMesh& aMesh = myMeshMap[myName];
109 aMesh.myName = myName;
111 int iEnd = aMesh.myDim*theNbPoints;
112 if(MYDEBUG) MESSAGE("ImportPoints - iEnd = "<<iEnd);
113 vector<float> &aPointsCoord = aMesh.myPointsCoord;
114 aPointsCoord.resize(iEnd);
115 for(int i = 0, tmp; i < iEnd;){
117 stmIn>>aPointsCoord[i++]; stmIn>>aPointsCoord[i++]; stmIn>>aPointsCoord[i++];
123 int VISU_DatConvertor::ImportCells(ifstream* pStmIn, vector<int>* pCellsType)
126 ifstream &stmIn = *pStmIn;
127 vector<int>& aCellsType = *pCellsType;
128 int aNbCells = aCellsType.size();
129 if(MYDEBUG) MESSAGE("ImportCells - theNbCells - Beginning");
130 VISU::TMesh& aMesh = myMeshMap[myName];
131 VISU::TMeshOnEntity& aMeshOnEntity = aMesh.myMeshOnEntityMap[VISU::CELL_ENTITY];
132 aMeshOnEntity.myEntity = VISU::CELL_ENTITY;
133 aMeshOnEntity.myMeshName = myName;
134 for(int i = 0, aMedType, aVtkType; i < aNbCells; i++){
135 stmIn>>aMedType; stmIn>>aMedType;
136 aCellsType[i] = aVtkType = med2vtkCellType(aMedType);
137 VISU::TMeshOnEntity::TConnForCellType& anArray = aMeshOnEntity.myCellsConn[aVtkType];
138 anArray.push_back(VISU::TMeshOnEntity::TConnect());
139 VISU::TMeshOnEntity::TConnect& aVector = anArray.back();
140 int jEnd = getNbMedNodes(aMedType);
141 aVector.resize(jEnd);
142 for(int j = 0, tmp; j < jEnd; j++){
144 aVector[j] = tmp - 1;
147 if(MYDEBUG) MESSAGE("ImportCells - theNbCells - End");
153 int VISU_Convertor_impl::ToDatFile(const string& theFileName) throw(std::runtime_error&){
154 if(MYDEBUG) MESSAGE("VISU_Convertor_impl::ToDatFile - "<<theFileName);
155 if(!myIsDone && Build() != NULL) myIsDone = true;
156 ofstream stmOut(theFileName.c_str(),ios::out);
157 stmOut.precision(PRECISION);
158 stmOut.setf(ios::scientific,ios::floatfield);
160 VISU::TMeshMap::const_iterator aMeshIter = myMeshMap.begin();
161 if(aMeshIter != myMeshMap.end()){
162 const VISU::TMesh &aMesh = aMeshIter->second;
163 if(MYDEBUG) MESSAGE("VISU_Convertor_impl::ToDatFile - aMeshName = "<<aMeshIter->first);
164 VISU::TFieldMap aFieldMap = GetFieldMapForMesh(aMeshIter->first);
165 const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh.myMeshOnEntityMap;
166 VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter = aMeshOnEntityMap.find(VISU::CELL_ENTITY);
167 if(aMeshOnEntityMapIter != aMeshOnEntityMap.end()){
168 const VISU::TMeshOnEntity& aMeshOnEntity = aMeshOnEntityMapIter->second;
169 int aNbPoints = aMesh.myPointsCoord.size()/aMesh.myDim;
170 int aNbCells = aMeshOnEntity.GetCellsDims().first;
171 stmOut<<aNbPoints<<"\t"<<aNbCells<<"\t"<<aFieldMap.size()<<endl;
173 MESSAGE("VISU_Convertor_impl::ToDatFile - aNbPoints = "<<aNbPoints<<
174 "; aNbCells = "<<aNbCells<<"; aNbFields = "<<aFieldMap.size());
175 for(int i = 0, j = 0; i < aNbPoints; i++, j = 3*i){
177 stmOut<<aMesh.myPointsCoord[j]<<"\t";
178 stmOut<<aMesh.myPointsCoord[j+1]<<"\t";
179 stmOut<<aMesh.myPointsCoord[j+2]<<endl;
181 const VISU::TMeshOnEntity::TCellsConn &aCellsConn = aMeshOnEntity.myCellsConn;
182 VISU::TMeshOnEntity::TCellsConn::const_iterator iter = aCellsConn.begin();
183 for(int i = 0; iter != aCellsConn.end(); iter++){
185 MESSAGE("VISU_Convertor_impl::ToDatFile - vtkCellType = "<<iter->first);
186 const VISU::TMeshOnEntity::TConnForCellType& anArray = iter->second;
187 for(int j = 0, jEnd = anArray.size(); j < jEnd; j++){
189 stmOut<<vtk2medCellType(iter->first)<<"\t";
190 const vector<int>& aVector = anArray[j];
191 int kEnd = aVector.size();
192 for(int k = 0; k < kEnd; k++)
193 stmOut<<aVector[k]<<"\t";
197 if(MYDEBUG) MESSAGE("VISU_Convertor_impl::ToDatFile - Beginning of Field Conversion");
198 VISU::TFieldMap::const_iterator aFieldIter = aFieldMap.begin();
199 for(int i = 0; aFieldIter != aFieldMap.end(); aFieldIter++){
200 const VISU::TField &aField = aFieldIter->second;
201 const VISU::TField::TValField &aValField = aField.myValField;
202 VISU::TField::TValField::const_reverse_iterator iter = aField.myValField.rbegin();
203 for(int i = 0; iter != aValField.rend(); iter++, i++) {
204 if(MYDEBUG) MESSAGE("VISU_Convertor_impl::ToDatFile - aTime = "<<i);
205 const VISU::TField::TValForTime& aValForTime = iter->second;
206 VISU::TField::TValForTime::const_iterator jter = aValForTime.begin();
208 int prefix = (aField.myEntity == VISU::CELL_ENTITY? 100: 200);
209 int jEnd = (aField.myEntity == VISU::CELL_ENTITY? aNbCells: aNbPoints);
210 string aName = GenerateName((aFieldIter->first).c_str(),i);
212 stmOut<<(prefix + aField.myNbComp)<<"\t"<<jEnd<<endl;
214 MESSAGE("VISU_Convertor_impl::ToDatFile - aName = "<<aName<<"; jEnd = "<<jEnd);
215 for(int j = 0; jter != aValForTime.end(); jter++) {
216 const VISU::TField::TValForCellsWithType& anArray = jter->second;
217 int kEnd = anArray.size(), aVtkType = jter->first;
218 switch(aField.myNbComp) {
220 for (int k = 0; k < kEnd;) {
222 stmOut<<anArray[k++]<<"\n";
226 for (int k = 0; k < kEnd;){
228 stmOut<<anArray[k++]<<"\t";
229 stmOut<<anArray[k++]<<"\t";
234 for (int k = 0; k < kEnd;){
236 stmOut<<anArray[k++]<<"\t";
237 stmOut<<anArray[k++]<<"\t";
238 stmOut<<anArray[k++]<<"\n";