]> SALOME platform Git repositories - tools/medcoupling.git/blob - src/MEDLoader/MeshFormatReader.hxx
Salome HOME
9f55a46bddcf25dc88cc16e8948a611bd4778e7b
[tools/medcoupling.git] / src / MEDLoader / MeshFormatReader.hxx
1 // Copyright (C) 2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef MESHFORMATREADER_HXX
21 #define MESHFORMATREADER_HXX
22
23 #include <vector>
24 #include <string>
25 #include <map>
26 #include <algorithm>
27 #include <utility>
28 #include <iterator>
29 #include "MCAuto.hxx"
30 #include "InterpKernelException.hxx"
31 #include "NormalizedUnstructuredMesh.hxx"
32 #include "MCType.hxx"
33 #include "MEDMESHConverterUtilities.hxx"
34 #include "libmesh5.hxx"
35
36 #include <fstream>
37
38 #ifndef WIN32
39 #include <features.h>
40 #endif
41
42 namespace MEDCoupling
43 {
44 class DataArrayDouble;
45 class DataArrayInt;
46 class MEDFileData;
47 class MEDFileFields;
48 class MEDFileFieldMultiTS;
49 class MEDFileUMesh;
50 class MEDCouplingUMesh;
51
52
53 struct MeshFormatElement {
54     MeshFormatElement(int type, int id=0):_type(type), _id(id) {}
55     int _type;
56     int _id;
57
58     inline friend bool operator==(const MeshFormatElement& a, const MeshFormatElement&b ) {
59         return ((a._type == b._type) && (a._id == b._id));
60     }
61 };
62
63 struct MeshFormatFamily {
64
65     std::map <int, std::vector<MeshFormatElement>* > _meshFormatFams;
66     std::map <int, std::vector<MeshFormatElement>* > _meshFormatFams_0;
67     std::map <int, std::vector<MeshFormatElement>* > _meshFormatFams_1;
68     std::map <int, std::vector<MeshFormatElement>* > _meshFormatFams_2;
69     std::map <int, std::vector<MeshFormatElement>* > _meshFormatFamsNodes;
70
71 public:
72     void insert(std::pair<int, MeshFormatElement> addToFamily, int dimRelMax)
73     {
74         insertPairInMap(_meshFormatFams, addToFamily);
75         insertPairInMap(getMapAtLevel(dimRelMax), addToFamily);
76     }
77 private:
78     void insertPairInMap(std::map <int, std::vector<MeshFormatElement>* >& aMap, std::pair<int, MeshFormatElement> addToFamily)
79     {
80         std::map <int, std::vector<MeshFormatElement>* >::iterator it = aMap.find(addToFamily.first);
81         if (it!= aMap.end())
82         {
83             aMap[addToFamily.first]->push_back(addToFamily.second);
84         }
85         else
86         {
87             std::vector <MeshFormatElement>* tmpVec = new std::vector <MeshFormatElement> ;
88             tmpVec->push_back(addToFamily.second);
89             aMap.insert(std::pair <int, std::vector <MeshFormatElement>* > (addToFamily.first, tmpVec) );
90
91         }
92
93     }
94 public:
95     void remove(std::pair<int, MeshFormatElement> removeFromFamily, int dimRelMax)
96     {
97         removePairFromMap(_meshFormatFams, removeFromFamily);
98         removePairFromMap(getMapAtLevel(dimRelMax), removeFromFamily);
99
100     }
101 private:
102     void removePairFromMap(std::map <int, std::vector<MeshFormatElement>* > & aMap, const std::pair<int, MeshFormatElement> removeFromFamily)
103     {
104
105         if (!aMap.size() ) return;
106         std::map <int, std::vector<MeshFormatElement>* >::iterator itTmp = aMap.find(removeFromFamily.first);
107         if (itTmp == aMap.end()) return;
108         else
109         {
110           std::vector <MeshFormatElement>* tmpVec2 = aMap[removeFromFamily.first];
111 #if __GNUC_PREREQ(4,9)
112           std::vector <MeshFormatElement>::const_iterator itt2;
113 #else
114           std::vector <MeshFormatElement>::iterator itt2;
115 #endif
116           const MeshFormatElement e = removeFromFamily.second;
117           itt2 = std::find(tmpVec2->begin(), tmpVec2->end(), e);
118           if (itt2 != tmpVec2->end())
119             tmpVec2->erase(itt2);
120           
121           if (!tmpVec2->size())
122             {
123               delete tmpVec2;
124               aMap.erase(removeFromFamily.first);
125             }
126         }
127     }
128 public:
129     std::map <int, std::vector<MeshFormatElement>* > & getMapAtLevel(int dimRelMax)
130     {
131         switch(dimRelMax)
132         {
133         case 0 :
134             return  _meshFormatFams_0;
135             break;
136         case -1 :
137             return  _meshFormatFams_1;
138             break;
139         case -2 :
140             return  _meshFormatFams_2;
141             break;
142         case 1 :
143             return  _meshFormatFamsNodes;
144             break;
145         }
146     }
147 public:
148     ~MeshFormatFamily()
149     {
150
151         freeMap(_meshFormatFams);
152         freeMap(_meshFormatFams_0);
153         freeMap(_meshFormatFams_1);
154         freeMap(_meshFormatFams_2);
155         freeMap(_meshFormatFamsNodes);
156
157     }
158
159 private:
160     void freeMap(std::map <int, std::vector<MeshFormatElement>* > & aMap)
161     {
162         std::map <int, std::vector<MeshFormatElement>* >::iterator it = aMap.begin();
163         for (; it !=aMap.end(); ++it) delete it->second;
164     }
165
166 };
167 class MeshFormatReader
168 {
169 public :
170     MEDLOADER_EXPORT MeshFormatReader();
171     MEDLOADER_EXPORT MeshFormatReader(const std::string& meshFileName, const std::vector<std::string>& fieldFileName);
172     MEDLOADER_EXPORT ~MeshFormatReader();
173   
174     MEDLOADER_EXPORT MEDCoupling::MCAuto<MEDCoupling::MEDFileData> loadInMedFileDS() ;
175     MEDLOADER_EXPORT void setMeshName(const std::string& theMeshName);
176     MEDLOADER_EXPORT std::string getMeshName() const;
177     MEDLOADER_EXPORT void setFile(const std::string& theFileName);
178     MEDLOADER_EXPORT void setFieldFileNames(const std::vector<std::string>& theFieldFileNames);
179     MEDLOADER_EXPORT std::vector<std::string> getFieldFileNames() const;
180     MEDLOADER_EXPORT std::vector< std::string > getErroMessage() const;
181
182 private :
183
184     MeshFormat::Status addMessage(const std::string& msg, const bool isFatal=false);
185     MeshFormat::Status perform();
186     MeshFormat::Status performFields();
187     MeshFormat::Status setNodes(MEDCoupling::DataArrayDouble* coordArray);
188     void setEdges(MEDCoupling::MEDCouplingUMesh* dimMesh1, int nbEdges);
189     void setTriangles( MEDCoupling::MEDCouplingUMesh* dimMesh2, int nbTria);
190     void setQuadrangles( MEDCoupling::MEDCouplingUMesh* dimMesh2, int nbQuad);
191     void setTetrahedras( MEDCoupling::MEDCouplingUMesh* dimMesh3, int nbTet);
192     void setPyramids(MEDCoupling::MEDCouplingUMesh* dimMesh3, int nbPyr);
193     void setHexahedras(MEDCoupling::MEDCouplingUMesh* dimMesh3, int nbHex);
194     void setPrisms(MEDCoupling::MEDCouplingUMesh* dimMesh3, int nbPrism);
195     void callParserGetLin( MeshFormat::GmfKwdCod kwd,  double* val, int valSize, int* ref);
196     void setTypeOfFieldAndDimRel(MeshFormat::GmfKwdCod kwd, MEDCoupling::TypeOfField* typeOfField, int* dimRel );
197     void backward_shift(MEDCoupling::mcIdType*, int size);
198     void setFields( MeshFormat::GmfKwdCod kwd, int nmbSol, int nbComp);
199     INTERP_KERNEL::NormalizedCellType toMedType(MeshFormat::GmfKwdCod kwd);
200     void buildFamilies();
201     void buildNodesFamilies();
202     void buildCellsFamilies();
203
204     std::string _myFile;
205     MeshFormat::MeshFormatParser _reader;
206     std::string _myCurrentOpenFile;
207     int _myCurrentFileId;
208     std::string _myMeshName;
209     std::vector<std::string> _myFieldFileNames;
210     int _dim, _version;
211     std::vector< std::string > _myErrorMessages;
212     MeshFormat::Status                     _myStatus;
213     MEDCoupling::MCAuto<MEDCoupling::MEDFileData> _myMed;
214     MEDCoupling::MCAuto<MEDCoupling::MEDFileUMesh> _uMesh;
215     MEDCoupling::MCAuto<MEDCoupling::MEDFileFields> _fields;
216     // map id family --element
217     MeshFormatFamily _fams;
218
219     int _dim1NbEl;
220     int _dim2NbEl;
221     int _dim3NbEl;
222 };
223 }
224 #endif //MESHFORMATREADER_HXX