Newer
Older
/**
* MeshIO.cpp
*
* Date: 2012/05/08
* Author: KR
*/
#include "GEOObjects.h"
#include "MeshIO.h"
#include "Node.h"
#include "Elements/Edge.h"
#include "Elements/Tri.h"
#include "Elements/Quad.h"
#include "Elements/Tet.h"
#include "Elements/Hex.h"
#include "Elements/Pyramid.h"
#include "Elements/Prism.h"
#include <iomanip>
#include <sstream>
namespace FileIO
{
MeshIO::MeshIO()
: _mesh(NULL)
{
}
MeshLib::Mesh* MeshIO::loadMeshFromFile(const std::string& file_name)
{
Karsten Rink
committed
std::cout << "Read mesh ... " << std::endl;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
#ifndef NDEBUG
QTime myTimer;
myTimer.start();
#endif
*/
std::ifstream in (file_name.c_str(),std::ios::in);
if (!in.is_open())
{
std::cout << "CFEMesh::FEMRead() - Could not open file...\n";
return NULL;
}
std::string line_string ("");
getline(in, line_string);
std::vector<MeshLib::Node*> nodes;
std::vector<MeshLib::Element*> elements;
if(line_string.find("#FEM_MSH") != std::string::npos) // OGS mesh file
{
while (!in.eof())
{
getline(in, line_string);
// check keywords
if (line_string.find("#STOP") != std::string::npos)
break;
else if (line_string.find("$NODES") != std::string::npos)
{
double x, y, z, double_dummy;
unsigned nNodes, idx;
in >> nNodes >> std::ws;
std::string s;
std::ios::pos_type position = in.tellg();
for (unsigned i = 0; i < nNodes; i++)
{
in >> idx >> x >> y >> z;
MeshLib::Node* node(new MeshLib::Node(x, y, z, nodes.size()));
nodes.push_back(node);
position = in.tellg();
in >> s;
if (s.find("$AREA") != std::string::npos)
in >> double_dummy;
else
in.seekg(position, std::ios::beg);
in >> std::ws;
}
}
else if (line_string.find("$ELEMENTS") != std::string::npos)
{
unsigned nElements;
in >> nElements >> std::ws;
for (unsigned i = 0; i < nElements; i++)
{
getline(in, line_string);
elements.push_back(readElement(line_string, nodes));
}
}
}
MeshLib::Mesh* mesh (new MeshLib::Mesh(file_name, nodes, elements));
std::cout << "Nr. Nodes: " << nodes.size() << std::endl;
std::cout << "Nr. Elements: " << elements.size() << std::endl;
/*
#ifndef NDEBUG
std::cout << "Loading time: " << myTimer.elapsed() << " ms" << std::endl;
#endif
*/
return mesh;
}
else
{
in.close();
return NULL;
}
}
MeshLib::Element* MeshIO::readElement(const std::string& line, const std::vector<MeshLib::Node*> &nodes)
{
std::stringstream ss (line);
std::string elem_type_str;
unsigned index, patch_index;
ss >> index >> patch_index >> elem_type_str;
MshElemType::type elem_type (String2MshElemType(elem_type_str));
unsigned* idx = new unsigned[8];
MeshLib::Element* elem;
Karsten Rink
committed
switch(elem_type)
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
{
case MshElemType::LINE:
for (int i = 0; i < 2; i++)
ss >> idx[i];
elem = new MeshLib::Edge(nodes[idx[0]], nodes[idx[1]], patch_index);
break;
case MshElemType::TRIANGLE:
for (int i = 0; i < 3; i++)
ss >> idx[i];
elem = new MeshLib::Tri(nodes[idx[0]], nodes[idx[1]], nodes[idx[2]], patch_index);
break;
case MshElemType::QUAD:
for (int i = 0; i < 4; i++)
ss >> idx[i];
elem = new MeshLib::Quad(nodes[idx[0]], nodes[idx[1]], nodes[idx[2]], nodes[idx[3]], patch_index);
break;
case MshElemType::TETRAHEDRON:
for (int i = 0; i < 4; i++)
ss >> idx[i];
elem = new MeshLib::Tet(nodes[idx[0]], nodes[idx[1]], nodes[idx[2]], nodes[idx[3]], patch_index);
break;
case MshElemType::HEXAHEDRON:
for (int i = 0; i < 8; i++)
ss >> idx[i];
elem = new MeshLib::Hex(nodes[idx[0]], nodes[idx[1]], nodes[idx[2]], nodes[idx[3]], nodes[idx[4]], nodes[idx[5]], nodes[idx[6]], nodes[idx[7]], patch_index);
break;
case MshElemType::PYRAMID:
for (int i = 0; i < 5; i++)
ss >> idx[i];
elem = new MeshLib::Pyramid(nodes[idx[0]], nodes[idx[1]], nodes[idx[2]], nodes[idx[3]], nodes[idx[4]], patch_index);
break;
case MshElemType::PRISM:
for (int i = 0; i < 6; i++)
ss >> idx[i];
elem = new MeshLib::Prism(nodes[idx[0]], nodes[idx[1]], nodes[idx[2]], nodes[idx[3]], nodes[idx[4]], nodes[idx[5]], patch_index);
break;
default:
elem = NULL;
}
/*
neighbors.resize(nfaces);
for (unsigned i = 0; i < nfaces; i++)
neighbors[i] = NULL;
*/
return elem;
}
int MeshIO::write(std::ostream &out)
{
/*
if(!_mesh)
{
std::cout << "OGSMeshIO cannot write: no mesh set!" << std::endl;
return 0;
}
setPrecision(9);
out << "#FEM_MSH" << std::endl;
out << "$PCS_TYPE" << std::endl << " " << _mesh->pcs_name << std::endl;
out << "$NODES" << std::endl << " ";
const size_t n_nodes(_mesh->GetNodesNumber(false));
out << n_nodes << std::endl;
for (size_t i(0); i < n_nodes; i++)
{
double const* const coords (_mesh->nod_vector[i]->getData());
out << i << " " << coords[0] << " " << coords[1] << " " << coords[2] << std::endl;
}
out << "$ELEMENTS" << std::endl << " ";
writeElementsExceptLines(_mesh->ele_vector, out);
out << " $LAYER" << std::endl;
out << " ";
out << _mesh->_n_msh_layer << std::endl;
out << "#STOP" << std::endl;
*/
return 1;
}
void MeshIO::setMesh(const MeshLib::Mesh* mesh)
{
_mesh = mesh;
}
} // end namespace FileIO