]> SALOME platform Git repositories - tools/medcoupling.git/blob - src/MEDLoader/MeshFormatReader.hxx
Salome HOME
Get rid of tab introduced in 7fef2459b44
[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   struct MeshFormatElement
53   {
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     {
60       return ((a._type == b._type) && (a._id == b._id));
61     }
62   };
63
64   struct MeshFormatFamily
65   {
66
67     std::map<int, std::vector<MeshFormatElement> *> _meshFormatFams;
68     std::map<int, std::vector<MeshFormatElement> *> _meshFormatFams_0;
69     std::map<int, std::vector<MeshFormatElement> *> _meshFormatFams_1;
70     std::map<int, std::vector<MeshFormatElement> *> _meshFormatFams_2;
71     std::map<int, std::vector<MeshFormatElement> *> _meshFormatFamsNodes;
72
73   public:
74     void insert(std::pair<int, MeshFormatElement> addToFamily, int dimRelMax)
75     {
76       insertPairInMap(_meshFormatFams, addToFamily);
77       insertPairInMap(getMapAtLevel(dimRelMax), addToFamily);
78     }
79
80   private:
81     void insertPairInMap(std::map<int, std::vector<MeshFormatElement> *> &aMap, std::pair<int, MeshFormatElement> addToFamily)
82     {
83       std::map<int, std::vector<MeshFormatElement> *>::iterator it = aMap.find(addToFamily.first);
84       if (it != aMap.end())
85       {
86         aMap[addToFamily.first]->push_back(addToFamily.second);
87       }
88       else
89       {
90         std::vector<MeshFormatElement> *tmpVec = new std::vector<MeshFormatElement>;
91         tmpVec->push_back(addToFamily.second);
92         aMap.insert(std::pair<int, std::vector<MeshFormatElement> *>(addToFamily.first, tmpVec));
93       }
94     }
95
96   public:
97     void remove(std::pair<int, MeshFormatElement> removeFromFamily, int dimRelMax)
98     {
99       removePairFromMap(_meshFormatFams, removeFromFamily);
100       removePairFromMap(getMapAtLevel(dimRelMax), removeFromFamily);
101     }
102
103   private:
104     void removePairFromMap(std::map<int, std::vector<MeshFormatElement> *> &aMap, const std::pair<int, MeshFormatElement> removeFromFamily)
105     {
106
107       if (!aMap.size())
108         return;
109       std::map<int, std::vector<MeshFormatElement> *>::iterator itTmp = aMap.find(removeFromFamily.first);
110       if (itTmp == aMap.end())
111         return;
112       else
113       {
114         std::vector<MeshFormatElement> *tmpVec2 = aMap[removeFromFamily.first];
115 #if __GNUC_PREREQ(4, 9)
116         std::vector<MeshFormatElement>::const_iterator itt2;
117 #else
118         std::vector<MeshFormatElement>::iterator itt2;
119 #endif
120         const MeshFormatElement e = removeFromFamily.second;
121         itt2 = std::find(tmpVec2->begin(), tmpVec2->end(), e);
122         if (itt2 != tmpVec2->end())
123           tmpVec2->erase(itt2);
124
125         if (!tmpVec2->size())
126         {
127           delete tmpVec2;
128           aMap.erase(removeFromFamily.first);
129         }
130       }
131     }
132
133   public:
134     std::map<int, std::vector<MeshFormatElement> *> &getMapAtLevel(int dimRelMax)
135     {
136       switch (dimRelMax)
137       {
138       case 0:
139         return _meshFormatFams_0;
140         break;
141       case -1:
142         return _meshFormatFams_1;
143         break;
144       case -2:
145         return _meshFormatFams_2;
146         break;
147       case 1:
148         return _meshFormatFamsNodes;
149         break;
150       }
151     }
152
153   public:
154     ~MeshFormatFamily()
155     {
156
157       freeMap(_meshFormatFams);
158       freeMap(_meshFormatFams_0);
159       freeMap(_meshFormatFams_1);
160       freeMap(_meshFormatFams_2);
161       freeMap(_meshFormatFamsNodes);
162     }
163
164   private:
165     void freeMap(std::map<int, std::vector<MeshFormatElement> *> &aMap)
166     {
167       std::map<int, std::vector<MeshFormatElement> *>::iterator it = aMap.begin();
168       for (; it != aMap.end(); ++it)
169         delete it->second;
170     }
171   };
172   class MeshFormatReader
173   {
174   public:
175     MEDLOADER_EXPORT MeshFormatReader();
176     MEDLOADER_EXPORT MeshFormatReader(const std::string &meshFileName, const std::vector<std::string> &fieldFileName);
177     MEDLOADER_EXPORT ~MeshFormatReader();
178
179     MEDLOADER_EXPORT MEDCoupling::MCAuto<MEDCoupling::MEDFileData> loadInMedFileDS();
180     MEDLOADER_EXPORT void setMeshName(const std::string &theMeshName);
181     MEDLOADER_EXPORT std::string getMeshName() const;
182     MEDLOADER_EXPORT void setFile(const std::string &theFileName);
183     MEDLOADER_EXPORT void setFieldFileNames(const std::vector<std::string> &theFieldFileNames);
184     MEDLOADER_EXPORT std::vector<std::string> getFieldFileNames() const;
185     MEDLOADER_EXPORT std::vector<std::string> getErroMessage() const;
186
187   private:
188     MeshFormat::Status addMessage(const std::string &msg, const bool isFatal = false);
189     MeshFormat::Status perform();
190     MeshFormat::Status performFields();
191     MeshFormat::Status setNodes(MEDCoupling::DataArrayDouble *coordArray);
192     void setEdges(MEDCoupling::MEDCouplingUMesh *dimMesh1, int nbEdges);
193     void setTriangles(MEDCoupling::MEDCouplingUMesh *dimMesh2, int nbTria);
194     void setQuadrangles(MEDCoupling::MEDCouplingUMesh *dimMesh2, int nbQuad);
195     void setTetrahedras(MEDCoupling::MEDCouplingUMesh *dimMesh3, int nbTet);
196     void setPyramids(MEDCoupling::MEDCouplingUMesh *dimMesh3, int nbPyr);
197     void setHexahedras(MEDCoupling::MEDCouplingUMesh *dimMesh3, int nbHex);
198     void setPrisms(MEDCoupling::MEDCouplingUMesh *dimMesh3, int nbPrism);
199     void callParserGetLin(MeshFormat::GmfKwdCod kwd, double *val, int valSize, int *ref);
200     void setTypeOfFieldAndDimRel(MeshFormat::GmfKwdCod kwd, MEDCoupling::TypeOfField *typeOfField, int *dimRel);
201     void backward_shift(MEDCoupling::mcIdType *, int size);
202     void setFields(MeshFormat::GmfKwdCod kwd, int nmbSol, int nbComp);
203     INTERP_KERNEL::NormalizedCellType toMedType(MeshFormat::GmfKwdCod kwd);
204     void buildFamilies();
205     void buildNodesFamilies();
206     void buildCellsFamilies();
207
208     std::string _myFile;
209     MeshFormat::MeshFormatParser _reader;
210     std::string _myCurrentOpenFile;
211     int _myCurrentFileId;
212     std::string _myMeshName;
213     std::vector<std::string> _myFieldFileNames;
214     int _dim, _version;
215     std::vector<std::string> _myErrorMessages;
216     MeshFormat::Status _myStatus;
217     MEDCoupling::MCAuto<MEDCoupling::MEDFileData> _myMed;
218     MEDCoupling::MCAuto<MEDCoupling::MEDFileUMesh> _uMesh;
219     MEDCoupling::MCAuto<MEDCoupling::MEDFileFields> _fields;
220     // map id family --element
221     MeshFormatFamily _fams;
222
223     int _dim1NbEl;
224     int _dim2NbEl;
225     int _dim3NbEl;
226   };
227 } // namespace MEDCoupling
228 #endif //MESHFORMATREADER_HXX