-
Notifications
You must be signed in to change notification settings - Fork 299
/
MeshGmsh.cpp
564 lines (511 loc) · 23.1 KB
/
MeshGmsh.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
125
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <sofa/helper/io/File.h>
#include <sofa/helper/io/MeshGmsh.h>
#include <sofa/helper/system/FileRepository.h>
#include <sofa/helper/system/SetDirectory.h>
#include <sofa/helper/system/Locale.h>
#include <sofa/helper/logging/Messaging.h>
#include <istream>
#include <fstream>
#include <string>
#include <sofa/helper/narrow_cast.h>
namespace sofa::helper::io
{
using namespace sofa::type;
using namespace sofa::topology;
Creator<Mesh::FactoryMesh, MeshGmsh> MeshGmshClass("gmsh");
void MeshGmsh::init (std::string filename)
{
if (!sofa::helper::system::DataRepository.findFile(filename))
{
msg_error("MeshGmsh") << "File " << filename << " not found.";
return;
}
loaderType = "gmsh";
std::ifstream file(filename);
if (!file.good()) return;
unsigned int gmshFormat = 0;
std::string cmd;
// -- Looking for Gmsh version of this file.
std::getline(file, cmd); //First line should be the start of the $MeshFormat section
if (cmd.length() >= 11 && cmd.substr(0, 11) == "$MeshFormat") // Reading gmsh
{
// NB: .msh file header line for version >= 2 can be "$MeshFormat", "$MeshFormat\r", "$MeshFormat \r"
std::string version;
std::getline(file, version); // Getting the version line (e.g. 4.1 0 8)
gmshFormat = std::stoul(version.substr( 0, version.find(" ")) ); // Retrieving the mesh format, keeping only the integer part
std::getline(file, cmd);
if (cmd.length() < 14 || cmd.substr(0, 14) != std::string("$EndMeshFormat")) // it should end with "$EndMeshFormat" or "$EndMeshFormat\r"
{
msg_error("MeshGmsh") << "No $EndMeshFormat flag found at the end of the file. Closing File";
file.close();
return;
}
else
{
// Reading the file until the node section is hit. In recent versions of MSH file format,
// we may encounter various sections between $MeshFormat and $Nodes
while (cmd.length() < 6 || cmd.substr(0, 6) != std::string("$Nodes")) // can be "$Nodes" or "$Nodes\r"
{
std::getline(file, cmd); // First Command
if (file.eof())
{
msg_error("MeshGmsh") << "End of file reached without finding the $Nodes section expected in MSH file format. Closing file.";
file.close();
return;
}
}
}
}
else
{
// Legacy MSh format version 1 directly starts with the Nodes section
// https://gmsh.info/doc/texinfo/gmsh.html#MSH-file-format-version-1-_0028Legacy_0029
gmshFormat = 1;
// The next line is already the first line of the $Nodes section. The file can be passed
// to readGmdh in its current state
}
readGmsh(file, gmshFormat);
file.close();
}
void MeshGmsh::addInGroup(type::vector< sofa::type::PrimitiveGroup>& group, int tag, std::size_t /*eid*/)
{
for (std::size_t i = 0; i<group.size(); i++) {
if (tag == group[i].p0) {
group[i].nbp++;
return;
}
}
std::stringstream ss;
const std::string s;
ss << tag;
group.push_back(sofa::type::PrimitiveGroup(tag, 1, s, s, -1));
}
void MeshGmsh::normalizeGroup(type::vector< sofa::type::PrimitiveGroup>& group)
{
int start = 0;
for (unsigned i = 0; i<group.size(); i++) {
group[i].p0 = start;
start += group[i].nbp;
}
}
bool MeshGmsh::readGmsh(std::ifstream &file, const unsigned int gmshFormat)
{
int npoints = 0;
std::string cmd;
if (gmshFormat <= 2)
{
// --- Loading Vertices ---
file >> npoints; //nb points
std::vector<int> pmap; // map for reordering vertices possibly not well sorted
for (int i = 0; i < npoints; ++i)
{
int index = i;
double x, y, z;
file >> index >> x >> y >> z;
m_vertices.push_back(sofa::type::Vec3(x, y, z));
if ((int)pmap.size() <= index) pmap.resize(index + 1);
pmap[index] = i; // In case of hole or swit
}
file >> cmd;
if (cmd.length() < 7 || cmd.substr(0, 7) != "$ENDNOD") // can be "$ENDNOD" or "$ENDNOD\r"
{
if (cmd.length() < 9 || cmd.substr(0, 9) != "$EndNodes") // can be "$EndNodes" or "$EndNodes\r"
{
msg_error("MeshGmsh") << "'$ENDNOD' or '$EndNodes' expected, found '" << cmd << "'";
return false;
}
}
// --- Loading Elements ---
file >> cmd;
if (cmd.length() < 4 || cmd.substr(0, 4) != "$ELM") // can be "$ELM" or "$ELM\r"
{
if (cmd.length() < 9 || cmd.substr(0, 9) != "$Elements") // can be "$ELM" or "$ELM\r"
{
msg_error("MeshGmsh") << "'$ELM' or '$Elements' expected, found '" << cmd << "'";
return false;
}
}
int nelems = 0;
file >> nelems;
for (int i = 0; i < nelems; ++i) // for each elem
{
int index = -1, etype = -1, nnodes = -1, ntags = -1, tag = -1;
if (gmshFormat == 1)
{
// version 1.0 format is
// elm-number elm-type reg-phys reg-elem number-of-nodes <node-number-list ...>
int rphys = -1, relem = -1;
file >> index >> etype >> rphys >> relem >> nnodes;
}
else /*if (gmshFormat == 2)*/
{
// version 2.0 format is
// elm-number elm-type number-of-tags < tag > ... node-number-list
file >> index >> etype >> ntags;
for (int t = 0; t < ntags; t++)
{
file >> tag;
// read the tag but don't use it
}
switch (etype)
{
case 15: //point
nnodes = 1;
break;
case 1: // Line
nnodes = 2;
break;
case 2: // Triangle
nnodes = 3;
break;
case 3: // Quad
nnodes = 4;
break;
case 4: // Tetra
nnodes = 4;
break;
case 5: // Hexa
nnodes = 8;
break;
case 8: // Quadratic edge
nnodes = 3;
break;
case 9: // Quadratic Triangle
nnodes = 6;
break;
case 11: // Quadratic Tetrahedron
nnodes = 10;
break;
default:
msg_error("MeshGmsh") << "Elements of type 1, 2, 3, 4, 5, or 6 expected. Element of type " << etype << " found.";
nnodes = 0;
}
}
type::vector<unsigned int> nodes;
nodes.resize(nnodes);
constexpr unsigned int edgesInQuadraticTriangle[3][2] = { { 0,1 },{ 1,2 },{ 2,0 } };
constexpr unsigned int edgesInQuadraticTetrahedron[6][2] = { { 0,1 },{ 1,2 },{ 0,2 },{ 0,3 },{ 2,3 },{ 1,3 } };
std::set<Edge> edgeSet;
size_t j;
for (int n = 0; n < nnodes; ++n)
{
int t = 0;
file >> t;
nodes[n] = (((unsigned int)t) < pmap.size()) ? pmap[t] : 0;
}
switch (etype)
{
case 1: // Line
addInGroup(m_edgesGroups, tag, m_edges.size());
m_edges.push_back(Edge(nodes[0], nodes[1]));
break;
case 2: // Triangle
addInGroup(m_trianglesGroups, tag, m_triangles.size());
m_triangles.push_back(Triangle(nodes[0], nodes[1], nodes[2]));
break;
case 3: // Quad
addInGroup(m_quadsGroups, tag, m_quads.size());
m_quads.push_back(Quad(nodes[0], nodes[1], nodes[2], nodes[3]));
break;
case 4: // Tetra
addInGroup(m_tetrahedraGroups, tag, m_tetrahedra.size());
m_tetrahedra.push_back(Tetrahedron(nodes[0], nodes[1], nodes[2], nodes[3]));
break;
case 5: // Hexa
addInGroup(m_hexahedraGroups, tag, m_hexahedra.size());
m_hexahedra.push_back(Hexahedron(nodes[0], nodes[1], nodes[2], nodes[3], nodes[4], nodes[5], nodes[6], nodes[7]));
break;
case 8: // quadratic edge
addInGroup(m_edgesGroups, tag, m_edges.size());
m_edges.push_back(Edge(nodes[0], nodes[1]));
{
HighOrderEdgePosition hoep;
hoep[0] = nodes[2];
hoep[1] = sofa::helper::narrow_cast<PointID>(m_edges.size() - 1);
hoep[2] = 1;
hoep[3] = 1;
m_highOrderEdgePositions.push_back(hoep);
}
break;
case 9: // quadratic triangle
addInGroup(m_trianglesGroups, tag, m_triangles.size());
m_triangles.push_back(Triangle(nodes[0], nodes[1], nodes[2]));
{
HighOrderEdgePosition hoep;
for (j = 0; j < 3; ++j) {
auto v0 = std::min(nodes[edgesInQuadraticTriangle[j][0]],
nodes[edgesInQuadraticTriangle[j][1]]);
auto v1 = std::max(nodes[edgesInQuadraticTriangle[j][0]],
nodes[edgesInQuadraticTriangle[j][1]]);
Edge e(v0, v1);
if (edgeSet.find(e) == edgeSet.end()) {
edgeSet.insert(e);
m_edges.push_back(Edge(v0, v1));
hoep[0] = nodes[j + 3];
hoep[1] = sofa::helper::narrow_cast<PointID>(m_edges.size() - 1);
hoep[2] = 1;
hoep[3] = 1;
m_highOrderEdgePositions.push_back(hoep);
}
}
}
break;
case 11: // quadratic tetrahedron
addInGroup(m_tetrahedraGroups, tag, m_tetrahedra.size());
m_tetrahedra.push_back(Tetrahedron(nodes[0], nodes[1], nodes[2], nodes[3]));
{
HighOrderEdgePosition hoep;
for (j = 0; j < 6; ++j) {
auto v0 = std::min(nodes[edgesInQuadraticTetrahedron[j][0]],
nodes[edgesInQuadraticTetrahedron[j][1]]);
auto v1 = std::max(nodes[edgesInQuadraticTetrahedron[j][0]],
nodes[edgesInQuadraticTetrahedron[j][1]]);
Edge e(v0, v1);
if (edgeSet.find(e) == edgeSet.end()) {
edgeSet.insert(e);
m_edges.push_back(Edge(v0, v1));
hoep[0] = nodes[j + 4];
hoep[1] = sofa::helper::narrow_cast<PointID>(m_edges.size() - 1);
hoep[2] = 1;
hoep[3] = 1;
m_highOrderEdgePositions.push_back(hoep);
}
}
}
break;
default:
//if the type is not handled, skip rest of the line
std::string tmp;
std::getline(file, tmp);
}
}
normalizeGroup(m_edgesGroups);
normalizeGroup(m_trianglesGroups);
normalizeGroup(m_tetrahedraGroups);
normalizeGroup(m_hexahedraGroups);
}
else // gmshFormat >= 4
{
// --- Parsing the $Nodes section --- //
std::getline(file, cmd); // Getting first line of $Nodes
std::istringstream nodesHeader(cmd);
unsigned int nbEntityBlocks, nbNodes, minNodeTag, maxNodeTag;
nodesHeader >> nbEntityBlocks >> nbNodes >> minNodeTag >> maxNodeTag;
for (unsigned int entityIndex = 0; entityIndex < nbEntityBlocks; entityIndex++) // looping over the entity blocks
{
std::getline(file, cmd); // Reading the entity line
std::istringstream entitySummary(cmd);
unsigned int entityDim, entityTag, parametric, nbNodesInBlock;
entitySummary >> entityDim >> entityTag >> parametric >> nbNodesInBlock;
for (unsigned int nodeIndex = 0; nodeIndex < nbNodesInBlock; nodeIndex++)
std::getline(file, cmd); // Reading the node indices lines
for (unsigned int nodeIndex = 0; nodeIndex < nbNodesInBlock; nodeIndex++)
{
std::getline(file, cmd); // Reading the node coordinates
std::istringstream coordinates(cmd);
double x, y, z;
coordinates >> x >> y >> z;
m_vertices.push_back(sofa::type::Vec3(x, y, z));
}
}
std::getline(file, cmd);
if (cmd.substr(0, 9) != "$EndNodes")
{
msg_error("MeshGmsh") << "'$EndNodes' expected, found '" << cmd << "'";
return false;
}
// --- Parsing the $Elements section --- //
std::getline(file, cmd);
if (cmd.substr(0, 9) != "$Elements")
{
msg_error("MeshGmsh") << "'$Elements' expected, found '" << cmd << "'";
return false;
}
std::getline(file, cmd); // Getting first line of $Elements
std::istringstream elementsHeader(cmd);
unsigned int nbElements, minElementTag, maxElementTag;
elementsHeader >> nbEntityBlocks >> nbElements >> minElementTag >> maxElementTag;
// Common information to add second order triangles (elementType = 9) and tetrahedra (elementType = 11)
const unsigned int edgesInQuadraticTriangle[3][2] = { { 0,1 },{ 1,2 },{ 2,0 } };
const unsigned int edgesInQuadraticTetrahedron[6][2] = { { 0,1 },{ 1,2 },{ 0,2 },{ 0,3 },{ 2,3 },{ 1,3 } };
std::set<Edge> edgeSet;
for (unsigned int entityIndex = 0; entityIndex < nbEntityBlocks; entityIndex++) // looping over the entity blocks
{
std::getline(file, cmd); // Reading the entity line
std::istringstream entitySummary(cmd);
unsigned int entityDim, entityTag, nbElementsInBlock, elementType;
entitySummary >> entityDim >> entityTag >> elementType >> nbElementsInBlock;
unsigned int nnodes = 0;
switch (elementType)
{
case 1: // Line
nnodes = 2;
break;
case 2: // Triangle
nnodes = 3;
break;
case 3: // Quadrangle
nnodes = 4;
break;
case 4: // Tetrahedron
nnodes = 4;
break;
case 5: // Hexahedron
nnodes = 8;
break;
case 6: // Prism
nnodes = 6;
break;
case 8: // Second order line
nnodes = 3;
break;
case 9: // Second order triangle
nnodes = 6;
break;
case 11: // Second order tetrahedron
nnodes = 10;
break;
case 15: // Point
nnodes = 1;
break;
default:
msg_error("MeshGmsh") << "Elements of type 1, 2, 3, 4, 5, 8, 9, 11 or 15 expected. Element of type " << elementType << " found.";
// nnodes = 0;
}
for (unsigned int elemIndex = 0; elemIndex < nbElementsInBlock; elemIndex++)
{
std::getline(file, cmd); // Reading the element info
std::istringstream elementInfo(cmd);
unsigned int elementTag;
elementInfo >> elementTag;
type::vector<unsigned int> nodes;
unsigned int nodeId = 0;
nodes.resize(nnodes);
for (unsigned int i = 0; i < nnodes; i++)
{
elementInfo >> nodeId;
nodes[i] = nodeId-1; //To account for the fact that node indices in the MSH file format start with 1 instead of 0
}
switch (elementType)
{
case 1: // Line
addInGroup(m_edgesGroups, elementTag, m_edges.size());
m_edges.push_back(Edge(nodes[0], nodes[1]));
break;
case 2: // Triangle
addInGroup(m_trianglesGroups, elementTag, m_triangles.size());
m_triangles.push_back(Triangle(nodes[0], nodes[1], nodes[2]));
break;
case 3: // Quadrangle
addInGroup(m_quadsGroups, elementTag, m_quads.size());
m_quads.push_back(Quad(nodes[0], nodes[1], nodes[2], nodes[3]));
break;
case 4: // Tetrahedron
addInGroup(m_tetrahedraGroups, elementTag, m_tetrahedra.size());
m_tetrahedra.push_back(Tetrahedron(nodes[0], nodes[1], nodes[2], nodes[3]));
break;
case 5: // Hexahedron
addInGroup(m_hexahedraGroups, elementTag, m_hexahedra.size());
m_hexahedra.push_back(Hexahedron(nodes[0], nodes[1], nodes[2], nodes[3], nodes[4], nodes[5], nodes[6], nodes[7]));
break;
case 8: // Second order line
addInGroup(m_edgesGroups, elementTag, m_edges.size());
m_edges.push_back(Edge(nodes[0], nodes[1]));
{
HighOrderEdgePosition hoep;
hoep[0] = nodes[2];
hoep[1] = sofa::helper::narrow_cast<PointID>(m_edges.size() - 1);
hoep[2] = 1;
hoep[3] = 1;
m_highOrderEdgePositions.push_back(hoep);
}
break;
case 9: // Second order triangle
addInGroup(m_trianglesGroups, elementTag, m_triangles.size());
m_triangles.push_back(Triangle(nodes[0], nodes[1], nodes[2]));
{
HighOrderEdgePosition hoep;
for (size_t j = 0; j < 3; ++j)
{
auto v0 = std::min(nodes[edgesInQuadraticTriangle[j][0]],
nodes[edgesInQuadraticTriangle[j][1]]);
auto v1 = std::max(nodes[edgesInQuadraticTriangle[j][0]],
nodes[edgesInQuadraticTriangle[j][1]]);
Edge e(v0, v1);
if (edgeSet.find(e) == edgeSet.end())
{
edgeSet.insert(e);
m_edges.push_back(Edge(v0, v1));
hoep[0] = nodes[j + 3];
hoep[1] = sofa::helper::narrow_cast<PointID>(m_edges.size() - 1);
hoep[2] = 1;
hoep[3] = 1;
m_highOrderEdgePositions.push_back(hoep);
}
}
}
break;
case 11: // Second order tetrahedron
addInGroup(m_tetrahedraGroups, elementTag, m_tetrahedra.size());
m_tetrahedra.push_back(Tetrahedron(nodes[0], nodes[1], nodes[2], nodes[3]));
{
HighOrderEdgePosition hoep;
for (size_t j = 0; j < 6; ++j)
{
auto v0 = std::min(nodes[edgesInQuadraticTetrahedron[j][0]],
nodes[edgesInQuadraticTetrahedron[j][1]]);
auto v1 = std::max(nodes[edgesInQuadraticTetrahedron[j][0]],
nodes[edgesInQuadraticTetrahedron[j][1]]);
Edge e(v0, v1);
if (edgeSet.find(e) == edgeSet.end())
{
edgeSet.insert(e);
m_edges.push_back(Edge(v0, v1));
hoep[0] = nodes[j + 4];
hoep[1] = sofa::helper::narrow_cast<PointID>(m_edges.size() - 1);
hoep[2] = 1;
hoep[3] = 1;
m_highOrderEdgePositions.push_back(hoep);
}
}
}
break;
// default: if the type is not handled, nothing to be done
}
} // end of loop over the elements in one entity block
} //end of loop over the entity blocks
normalizeGroup(m_edgesGroups);
normalizeGroup(m_trianglesGroups);
normalizeGroup(m_tetrahedraGroups);
normalizeGroup(m_hexahedraGroups);
}
file >> cmd;
if (cmd != "$ENDELM" && cmd != "$EndElements")
{
msg_error("MeshGmsh") << "'$ENDELM' or '$EndElements' expected, found '" << cmd << "'";
return false;
}
return true;
}
} // namespace sofa::helper::io