Salome HOME
[Intersect2D] Keep flexibility on orientation of mesh2
[tools/medcoupling.git] / src / MEDLoader / MeshFormatWriter.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 MESHFORMATWRITER_HXX
21 #define MESHFORMATWRITER_HXX
22
23 #include <vector>
24 #include <string>
25 #include <string>
26 #include "MCAuto.hxx"
27 #include "InterpKernelException.hxx"
28 #include "NormalizedUnstructuredMesh.hxx"
29 #include "MCType.hxx"
30 #include "MEDMESHConverterUtilities.hxx"
31 #include "libmesh5.hxx"
32
33 #include "MEDFileMesh.hxx"
34 #include <fstream>
35
36 namespace MEDCoupling
37 {
38 class DataArrayDouble;
39 class DataArrayInt;
40 class MEDFileData;
41 class MEDFileFields;
42 class MEDFileFieldMultiTS;
43 class MEDFileUMesh;
44
45
46 struct MeshFormatNode {
47     MeshFormatNode(double x, double y, double z=0, int id=0, int famId=0): _id(id), _famId(famId) {
48         xyz[0] = x;
49         xyz[1] = y;
50         xyz[2] = z;
51     }
52     int _id;
53     int _famId;
54     double xyz[3];
55
56     //~inline friend bool operator==(const MeshFormatNode& a, const MeshFormatNode&b ) { return ((a._type == b._type) && (a._id == b._id));}
57 };
58
59 struct MeshFormatCell {
60     MeshFormatCell(INTERP_KERNEL::NormalizedCellType  type, int id=0, int famId=0):_type(type), _id(id), _famId(famId) {}
61     INTERP_KERNEL::NormalizedCellType  _type;
62     int _id;
63     int _famId;
64     void setConn(const std::vector<MEDCoupling::mcIdType> cpy)
65     {
66         conn.clear();
67         std::copy(cpy.begin(), cpy.end(), std::back_inserter(conn));
68     }
69     std::vector<MEDCoupling::mcIdType> conn;
70 };
71
72
73 class MeshFormatWriter
74 {
75 public :
76     MEDLOADER_EXPORT MeshFormatWriter();
77     MEDLOADER_EXPORT MeshFormatWriter(const std::string& meshFileName, const std::vector<std::string>& fieldFileNames);
78     MEDLOADER_EXPORT ~MeshFormatWriter();
79     MEDLOADER_EXPORT void setMeshFileName(const std::string& meshFileName);
80     MEDLOADER_EXPORT std::string getMeshFileName() const;
81     MEDLOADER_EXPORT void setFieldFileNames(const std::vector<std::string>& fieldFileNames);
82     MEDLOADER_EXPORT std::vector<std::string> getFieldFileNames() const;
83     MEDLOADER_EXPORT void setMEDFileDS(MEDCoupling::MEDFileData* mfd);
84
85     MEDLOADER_EXPORT void write();
86
87 private :
88
89     void getNodes( MEDCoupling::MCAuto< MEDCoupling::MEDCouplingUMesh > umesh0);
90     void getNSEG2(  MEDCoupling::mcIdType nbEdgesNSEG2, MEDCoupling::MCAuto< MEDCoupling::MEDCouplingUMesh > umesh1);
91     void getNSEG3(  MEDCoupling::mcIdType nbEdgesNSEG2, MEDCoupling::MCAuto< MEDCoupling::MEDCouplingUMesh > umesh1);
92     void getTRI3(  MEDCoupling::mcIdType nbTRI3, MEDCoupling::MCAuto< MEDCoupling::MEDCouplingUMesh > umesh2);
93     void getTRI6(  MEDCoupling::mcIdType nbTRI6, MEDCoupling::MCAuto< MEDCoupling::MEDCouplingUMesh > umesh2);
94     void getQUAD4(  MEDCoupling::mcIdType nbQUAD4, MEDCoupling::MCAuto< MEDCoupling::MEDCouplingUMesh > umesh2);
95     void getQUAD8(  MEDCoupling::mcIdType nbQUAD8, MEDCoupling::MCAuto< MEDCoupling::MEDCouplingUMesh > umesh2);
96     void getQUAD9(  MEDCoupling::mcIdType nbQUAD9, MEDCoupling::MCAuto< MEDCoupling::MEDCouplingUMesh > umesh2);
97     void getTETRA4(  MEDCoupling::mcIdType nbTETRA4, MEDCoupling::MCAuto< MEDCoupling::MEDCouplingUMesh > umesh3);
98     void getTETRA10(  MEDCoupling::mcIdType nbTETRA10, MEDCoupling::MCAuto< MEDCoupling::MEDCouplingUMesh > umesh3);
99     void getPYRA5(  MEDCoupling::mcIdType nbPYRA5, MEDCoupling::MCAuto< MEDCoupling::MEDCouplingUMesh > umesh3);
100     void getHEXA8(  MEDCoupling::mcIdType nbHEXA8, MEDCoupling::MCAuto< MEDCoupling::MEDCouplingUMesh > umesh3);
101     void getHEXA20(  MEDCoupling::mcIdType nbHEXA20, MEDCoupling::MCAuto< MEDCoupling::MEDCouplingUMesh > umesh3);
102     void getHEXA27(  MEDCoupling::mcIdType nbHEXA27, MEDCoupling::MCAuto< MEDCoupling::MEDCouplingUMesh > umesh3);
103     void getPENTA6(  MEDCoupling::mcIdType nbPENTA6, MEDCoupling::MCAuto< MEDCoupling::MEDCouplingUMesh > umesh3);
104     int getGmfSolKwd(const int nbComp, const int dim);
105     MeshFormat::Status setFieldOnNodes(MEDCoupling::MEDFileFieldMultiTS * f, int iteration, int order, size_t compInfoSize);
106     MeshFormat::Status setFieldOnCells(MEDCoupling::MEDFileFieldMultiTS * f, int iteration, int order, std::vector<int> levs );
107     void extractSymetricTensor(double fullTensor[], double*& symTensor);
108     void linkFamilyToNodes();
109     void linkFamilyToCells();
110     void writeCells();
111
112     bool checkFileName();
113     bool checkFieldFileName();
114     void forward_shift(std::vector<MEDCoupling::mcIdType> &conn);
115     MeshFormat::Status perform();
116     MeshFormat::Status performFields();
117     MeshFormat::Status addMessage(const std::string& msg, const bool isFatal = false);
118
119     std::string _meshFileName;
120     MeshFormat::MeshFormatParser _writer;
121     std::vector<std::string> _fieldFileNames;
122     MEDCoupling::MEDFileData* _mfd;
123     MEDCoupling::MCAuto< MEDCoupling::MEDFileMesh > _mesh;
124     std::vector< MEDCoupling::MCAuto< MEDCoupling::MEDFileFieldMultiTS > > _fields;
125     std::vector< std::string > _myErrorMessages;
126     MeshFormat::Status                     _myStatus;
127     int _myCurrentFileId, _dim, _version;
128     std::string _myCurrentOpenFile;
129     std::map<int, MeshFormatNode> _idNodeToNode;
130     std::map < INTERP_KERNEL::NormalizedCellType, std::map<int, MeshFormatCell> > _typeToIdCellToCell;
131
132 };
133 }
134 #endif //MESHFORMATWRITER_HXX