2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 // + This file is part of enGrid. +
6 // + Copyright 2008,2009 Oliver Gloth +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23 #include "gmshreader.h"
24 #include "correctsurfaceorientation.h"
26 void GmshReader::readAscii1(vtkUnstructuredGrid
*grid
)
28 vtkIdType Nnodes
, Ncells
;
29 QFile
file(getFileName());
30 file
.open(QIODevice::ReadOnly
| QIODevice::Text
);
34 if (word
!= "$NOD") EG_ERR_RETURN("$NOD expected");
36 EG_VTKSP(vtkPoints
, points
);
37 EG_VTKSP(vtkUnstructuredGrid
, ug
);
38 points
->SetNumberOfPoints(Nnodes
);
39 std::vector
<vtkIdType
> idxmap(Nnodes
+1);
40 for (vtkIdType i
= 0; i
< Nnodes
; ++i
) {
43 f
>> ir
>> x
>> y
>> z
;
45 points
->SetPoint(i
,x
,y
,z
);
47 ug
->SetPoints(points
);
49 if (word
!= "$ENDNOD") EG_ERR_RETURN("$ENDNOD expected");
51 if (word
!= "$ELM") EG_ERR_RETURN("$ELM expected");
54 EG_VTKSP(vtkIntArray
, cell_code
);
55 cell_code
->SetName("cell_code");
56 cell_code
->SetNumberOfValues(Ncells
);
57 for (vtkIdType i
= 0; i
< Ncells
; ++i
) {
59 int elm_type
, reg_phys
;
64 cell_code
->SetTuple1(i
,reg_phys
);
67 if (elm_type
== 2) { // triangle
68 for (vtkIdType j
= 0; j
< 3; ++j
) {
70 pts
[j
] = idxmap
[node
];
72 ug
->InsertNextCell(VTK_TRIANGLE
,3,pts
);
73 } else if (elm_type
== 3) { // quad
74 for (vtkIdType j
= 0; j
< 4; ++j
) {
76 pts
[j
] = idxmap
[node
];
78 ug
->InsertNextCell(VTK_QUAD
,4,pts
);
79 } else if (elm_type
== 4) { // tetrahedron
80 for (vtkIdType j
= 0; j
< 4; ++j
) {
82 pts
[j
] = idxmap
[node
];
87 ug
->InsertNextCell(VTK_TETRA
,4,pts
);
88 } else if (elm_type
== 5) { // hexhedron
89 for (vtkIdType j
= 0; j
< 8; ++j
) {
91 pts
[j
] = idxmap
[node
];
93 ug
->InsertNextCell(VTK_HEXAHEDRON
,8,pts
);
94 } else if (elm_type
== 6) { // prism/wedge
95 for (vtkIdType j
= 0; j
< 3; ++j
) {
97 pts
[j
+3] = idxmap
[node
];
99 for (vtkIdType j
= 0; j
< 3; ++j
) {
101 pts
[j
] = idxmap
[node
];
103 ug
->InsertNextCell(VTK_WEDGE
,6,pts
);
104 } else if (elm_type
== 7) { // pyramid
105 for (vtkIdType j
= 0; j
< 5; ++j
) {
107 pts
[j
] = idxmap
[node
];
109 ug
->InsertNextCell(VTK_PYRAMID
,5,pts
);
112 ug
->GetCellData()->AddArray(cell_code
);
116 void GmshReader::readAscii2(vtkUnstructuredGrid
*grid
)
118 vtkIdType Nnodes
, Ncells
;
119 QFile
file(getFileName());
120 file
.open(QIODevice::ReadOnly
| QIODevice::Text
);
121 QTextStream
f(&file
);
124 if (word
!= "$MeshFormat") EG_ERR_RETURN("$MeshFormat expected");
129 if (word
!= "$EndMeshFormat") EG_ERR_RETURN("$EndMeshFormat expected");
131 if (word
!= "$Nodes") EG_ERR_RETURN("$Nodes expected");
133 EG_VTKSP(vtkPoints
, points
);
134 EG_VTKSP(vtkUnstructuredGrid
, ug
);
135 points
->SetNumberOfPoints(Nnodes
);
136 std::vector
<vtkIdType
> idxmap(Nnodes
+1);
137 for (vtkIdType i
= 0; i
< Nnodes
; ++i
) {
140 f
>> ir
>> x
>> y
>> z
;
142 points
->SetPoint(i
,x
,y
,z
);
144 ug
->SetPoints(points
);
146 if (word
!= "$EndNodes") EG_ERR_RETURN("$EndNotes expected");
148 if (word
!= "$Elements") EG_ERR_RETURN("$Elements expected");
150 ug
->Allocate(Ncells
);
151 EG_VTKSP(vtkIntArray
, cell_code
);
152 cell_code
->SetName("cell_code");
153 cell_code
->SetNumberOfValues(Ncells
);
154 for (vtkIdType i
= 0; i
< Ncells
; ++i
) {
156 int elm_type
, Ntags
, bc
;
166 for (int j
= 1; j
< Ntags
; ++j
) f
>> word
;
168 cell_code
->SetTuple1(i
,bc
);
171 if (elm_type
== 2) { // triangle
172 for (vtkIdType j
= 0; j
< 3; ++j
) {
174 pts
[j
] = idxmap
[node
];
176 ug
->InsertNextCell(VTK_TRIANGLE
,3,pts
);
177 } else if (elm_type
== 3) { // quad
178 for (vtkIdType j
= 0; j
< 4; ++j
) {
180 pts
[j
] = idxmap
[node
];
182 ug
->InsertNextCell(VTK_QUAD
,4,pts
);
183 } else if (elm_type
== 4) { // tetrahedron
184 for (vtkIdType j
= 0; j
< 4; ++j
) {
186 pts
[j
] = idxmap
[node
];
188 ug
->InsertNextCell(VTK_TETRA
,4,pts
);
189 } else if (elm_type
== 5) { // hexhedron
190 for (vtkIdType j
= 0; j
< 8; ++j
) {
192 pts
[j
] = idxmap
[node
];
194 ug
->InsertNextCell(VTK_HEXAHEDRON
,8,pts
);
195 } else if (elm_type
== 6) { // prism/wedge
196 for (vtkIdType j
= 0; j
< 3; ++j
) {
198 pts
[j
+3] = idxmap
[node
];
200 for (vtkIdType j
= 0; j
< 3; ++j
) {
202 pts
[j
] = idxmap
[node
];
204 ug
->InsertNextCell(VTK_WEDGE
,6,pts
);
205 } else if (elm_type
== 7) { // pyramid
206 for (vtkIdType j
= 0; j
< 5; ++j
) {
208 pts
[j
] = idxmap
[node
];
210 ug
->InsertNextCell(VTK_PYRAMID
,5,pts
);
213 ug
->GetCellData()->AddArray(cell_code
);
216 CorrectSurfaceOrientation corr_surf
;
217 corr_surf
.setGrid(grid
);
222 void GmshReader::operate()
227 if (format
== ascii1
) readAscii1(grid
);
228 else if (format
== ascii2
) readAscii2(grid
);
229 createBasicFields(grid
, grid
->GetNumberOfCells(), grid
->GetNumberOfPoints(), false);
230 UpdateCellIndex(grid
);
232 } catch (Error err
) {