1 // Copyright (C) 2021-2023 CEA, EDF
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"
38 #if !defined(WIN32) && !defined(__APPLE__)
44 class DataArrayDouble;
48 class MEDFileFieldMultiTS;
50 class MEDCouplingUMesh;
52 struct MeshFormatElement
54 MeshFormatElement(int type, int id = 0) : _type(type), _id(id) {}
58 inline friend bool operator==(const MeshFormatElement &a, const MeshFormatElement &b)
60 return ((a._type == b._type) && (a._id == b._id));
64 struct MeshFormatFamily
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;
74 void insert(std::pair<int, MeshFormatElement> addToFamily, int dimRelMax)
76 insertPairInMap(_meshFormatFams, addToFamily);
77 insertPairInMap(getMapAtLevel(dimRelMax), addToFamily);
81 void insertPairInMap(std::map<int, std::vector<MeshFormatElement> *> &aMap, std::pair<int, MeshFormatElement> addToFamily)
83 std::map<int, std::vector<MeshFormatElement> *>::iterator it = aMap.find(addToFamily.first);
86 aMap[addToFamily.first]->push_back(addToFamily.second);
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));
97 void remove(std::pair<int, MeshFormatElement> removeFromFamily, int dimRelMax)
99 removePairFromMap(_meshFormatFams, removeFromFamily);
100 removePairFromMap(getMapAtLevel(dimRelMax), removeFromFamily);
104 void removePairFromMap(std::map<int, std::vector<MeshFormatElement> *> &aMap, const std::pair<int, MeshFormatElement> removeFromFamily)
109 std::map<int, std::vector<MeshFormatElement> *>::iterator itTmp = aMap.find(removeFromFamily.first);
110 if (itTmp == aMap.end())
114 std::vector<MeshFormatElement> *tmpVec2 = aMap[removeFromFamily.first];
115 std::vector<MeshFormatElement>::iterator itt2;
116 const MeshFormatElement e = removeFromFamily.second;
117 itt2 = std::find(tmpVec2->begin(), tmpVec2->end(), e);
118 if (itt2 != tmpVec2->end())
119 tmpVec2->erase(itt2);
121 if (!tmpVec2->size())
124 aMap.erase(removeFromFamily.first);
130 std::map<int, std::vector<MeshFormatElement> *> &getMapAtLevel(int dimRelMax)
135 return _meshFormatFams_0;
138 return _meshFormatFams_1;
141 return _meshFormatFams_2;
144 return _meshFormatFamsNodes;
147 THROW_IK_EXCEPTION("getMapAtLevel : dimRelMax must be in [0,-1,-2,1]");
154 freeMap(_meshFormatFams);
155 freeMap(_meshFormatFams_0);
156 freeMap(_meshFormatFams_1);
157 freeMap(_meshFormatFams_2);
158 freeMap(_meshFormatFamsNodes);
162 void freeMap(std::map<int, std::vector<MeshFormatElement> *> &aMap)
164 std::map<int, std::vector<MeshFormatElement> *>::iterator it = aMap.begin();
165 for (; it != aMap.end(); ++it)
169 class MeshFormatReader
172 MEDLOADER_EXPORT MeshFormatReader();
173 MEDLOADER_EXPORT MeshFormatReader(const std::string &meshFileName, const std::vector<std::string> &fieldFileName);
174 MEDLOADER_EXPORT ~MeshFormatReader();
176 MEDLOADER_EXPORT MEDCoupling::MCAuto<MEDCoupling::MEDFileData> loadInMedFileDS();
177 MEDLOADER_EXPORT void setMeshName(const std::string &theMeshName);
178 MEDLOADER_EXPORT std::string getMeshName() const;
179 MEDLOADER_EXPORT void setFile(const std::string &theFileName);
180 MEDLOADER_EXPORT void setFieldFileNames(const std::vector<std::string> &theFieldFileNames);
181 MEDLOADER_EXPORT std::vector<std::string> getFieldFileNames() const;
182 MEDLOADER_EXPORT std::vector<std::string> getErroMessage() const;
185 MeshFormat::Status addMessage(const std::string &msg, const bool isFatal = false);
186 MeshFormat::Status perform();
187 MeshFormat::Status performFields();
188 MeshFormat::Status setNodes(MEDCoupling::DataArrayDouble *coordArray);
189 void setEdges(MEDCoupling::MEDCouplingUMesh *dimMesh1, int nbEdges);
190 void setTriangles(MEDCoupling::MEDCouplingUMesh *dimMesh2, int nbTria);
191 void setQuadrangles(MEDCoupling::MEDCouplingUMesh *dimMesh2, int nbQuad);
192 void setTetrahedras(MEDCoupling::MEDCouplingUMesh *dimMesh3, int nbTet);
193 void setPyramids(MEDCoupling::MEDCouplingUMesh *dimMesh3, int nbPyr);
194 void setHexahedras(MEDCoupling::MEDCouplingUMesh *dimMesh3, int nbHex);
195 void setPrisms(MEDCoupling::MEDCouplingUMesh *dimMesh3, int nbPrism);
196 void callParserGetLin(MeshFormat::GmfKwdCod kwd, double *val, int valSize, int *ref);
197 void setTypeOfFieldAndDimRel(MeshFormat::GmfKwdCod kwd, MEDCoupling::TypeOfField *typeOfField, int *dimRel);
198 void backward_shift(MEDCoupling::mcIdType *, int size);
199 void setFields(MeshFormat::GmfKwdCod kwd, int nmbSol, int nbComp);
200 INTERP_KERNEL::NormalizedCellType toMedType(MeshFormat::GmfKwdCod kwd);
201 void buildFamilies();
202 void buildNodesFamilies();
203 void buildCellsFamilies();
206 MeshFormat::MeshFormatParser _reader;
207 std::string _myCurrentOpenFile;
208 int _myCurrentFileId;
209 std::string _myMeshName;
210 std::vector<std::string> _myFieldFileNames;
212 std::vector<std::string> _myErrorMessages;
213 MeshFormat::Status _myStatus;
214 MEDCoupling::MCAuto<MEDCoupling::MEDFileData> _myMed;
215 MEDCoupling::MCAuto<MEDCoupling::MEDFileUMesh> _uMesh;
216 MEDCoupling::MCAuto<MEDCoupling::MEDFileFields> _fields;
217 // map id family --element
218 MeshFormatFamily _fams;
224 } // namespace MEDCoupling
225 #endif //MESHFORMATREADER_HXX