1 // Copyright (C) 2021 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #ifndef MESHFORMATREADER_HXX
21 #define MESHFORMATREADER_HXX
30 #include "InterpKernelException.hxx"
31 #include "NormalizedUnstructuredMesh.hxx"
33 #include "MEDMESHConverterUtilities.hxx"
34 #include "libmesh5.hxx"
44 class DataArrayDouble;
48 class MEDFileFieldMultiTS;
50 class MEDCouplingUMesh;
53 struct MeshFormatElement {
54 MeshFormatElement(int type, int id=0):_type(type), _id(id) {}
58 inline friend bool operator==(const MeshFormatElement& a, const MeshFormatElement&b ) {
59 return ((a._type == b._type) && (a._id == b._id));
63 struct MeshFormatFamily {
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;
72 void insert(std::pair<int, MeshFormatElement> addToFamily, int dimRelMax)
74 insertPairInMap(_meshFormatFams, addToFamily);
75 insertPairInMap(getMapAtLevel(dimRelMax), addToFamily);
78 void insertPairInMap(std::map <int, std::vector<MeshFormatElement>* >& aMap, std::pair<int, MeshFormatElement> addToFamily)
80 std::map <int, std::vector<MeshFormatElement>* >::iterator it = aMap.find(addToFamily.first);
83 aMap[addToFamily.first]->push_back(addToFamily.second);
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) );
95 void remove(std::pair<int, MeshFormatElement> removeFromFamily, int dimRelMax)
97 removePairFromMap(_meshFormatFams, removeFromFamily);
98 removePairFromMap(getMapAtLevel(dimRelMax), removeFromFamily);
102 void removePairFromMap(std::map <int, std::vector<MeshFormatElement>* > & aMap, const std::pair<int, MeshFormatElement> removeFromFamily)
105 if (!aMap.size() ) return;
106 std::map <int, std::vector<MeshFormatElement>* >::iterator itTmp = aMap.find(removeFromFamily.first);
107 if (itTmp == aMap.end()) return;
110 std::vector <MeshFormatElement>* tmpVec2 = aMap[removeFromFamily.first];
111 const MeshFormatElement e = removeFromFamily.second;
112 auto itt2 = std::find(tmpVec2->cbegin(), tmpVec2->cend(), e);
113 if (itt2 != tmpVec2->cend())
114 tmpVec2->erase(itt2);
116 if (!tmpVec2->size())
119 aMap.erase(removeFromFamily.first);
124 std::map <int, std::vector<MeshFormatElement>* > & getMapAtLevel(int dimRelMax)
129 return _meshFormatFams_0;
132 return _meshFormatFams_1;
135 return _meshFormatFams_2;
138 return _meshFormatFamsNodes;
146 freeMap(_meshFormatFams);
147 freeMap(_meshFormatFams_0);
148 freeMap(_meshFormatFams_1);
149 freeMap(_meshFormatFams_2);
150 freeMap(_meshFormatFamsNodes);
155 void freeMap(std::map <int, std::vector<MeshFormatElement>* > & aMap)
157 std::map <int, std::vector<MeshFormatElement>* >::iterator it = aMap.begin();
158 for (; it !=aMap.end(); ++it) delete it->second;
162 class MeshFormatReader
165 MEDLOADER_EXPORT MeshFormatReader();
166 MEDLOADER_EXPORT MeshFormatReader(const std::string& meshFileName, const std::vector<std::string>& fieldFileName);
167 MEDLOADER_EXPORT ~MeshFormatReader();
169 MEDLOADER_EXPORT MEDCoupling::MCAuto<MEDCoupling::MEDFileData> loadInMedFileDS() ;
170 MEDLOADER_EXPORT void setMeshName(const std::string& theMeshName);
171 MEDLOADER_EXPORT std::string getMeshName() const;
172 MEDLOADER_EXPORT void setFile(const std::string& theFileName);
173 MEDLOADER_EXPORT void setFieldFileNames(const std::vector<std::string>& theFieldFileNames);
174 MEDLOADER_EXPORT std::vector<std::string> getFieldFileNames() const;
175 MEDLOADER_EXPORT std::vector< std::string > getErroMessage() const;
179 MeshFormat::Status addMessage(const std::string& msg, const bool isFatal=false);
180 MeshFormat::Status perform();
181 MeshFormat::Status performFields();
182 MeshFormat::Status setNodes(MEDCoupling::DataArrayDouble* coordArray);
183 void setEdges(MEDCoupling::MEDCouplingUMesh* dimMesh1, int nbEdges);
184 void setTriangles( MEDCoupling::MEDCouplingUMesh* dimMesh2, int nbTria);
185 void setQuadrangles( MEDCoupling::MEDCouplingUMesh* dimMesh2, int nbQuad);
186 void setTetrahedras( MEDCoupling::MEDCouplingUMesh* dimMesh3, int nbTet);
187 void setPyramids(MEDCoupling::MEDCouplingUMesh* dimMesh3, int nbPyr);
188 void setHexahedras(MEDCoupling::MEDCouplingUMesh* dimMesh3, int nbHex);
189 void setPrisms(MEDCoupling::MEDCouplingUMesh* dimMesh3, int nbPrism);
190 void callParserGetLin( MeshFormat::GmfKwdCod kwd, double* val, int valSize, int* ref);
191 void setTypeOfFieldAndDimRel(MeshFormat::GmfKwdCod kwd, MEDCoupling::TypeOfField* typeOfField, int* dimRel );
192 void backward_shift(MEDCoupling::mcIdType*, int size);
193 void setFields( MeshFormat::GmfKwdCod kwd, int nmbSol, int nbComp);
194 INTERP_KERNEL::NormalizedCellType toMedType(MeshFormat::GmfKwdCod kwd);
195 void buildFamilies();
196 void buildNodesFamilies();
197 void buildCellsFamilies();
200 MeshFormat::MeshFormatParser _reader;
201 std::string _myCurrentOpenFile;
202 int _myCurrentFileId;
203 std::string _myMeshName;
204 std::vector<std::string> _myFieldFileNames;
206 std::vector< std::string > _myErrorMessages;
207 MeshFormat::Status _myStatus;
208 MEDCoupling::MCAuto<MEDCoupling::MEDFileData> _myMed;
209 MEDCoupling::MCAuto<MEDCoupling::MEDFileUMesh> _uMesh;
210 MEDCoupling::MCAuto<MEDCoupling::MEDFileFields> _fields;
211 // map id family --element
212 MeshFormatFamily _fams;
219 #endif //MESHFORMATREADER_HXX