-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathTetGenLoader.cpp
More file actions
262 lines (225 loc) · 5.79 KB
/
Copy pathTetGenLoader.cpp
File metadata and controls
262 lines (225 loc) · 5.79 KB
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
#include "TetGenLoader.h"
#include <sstream>
#include <fstream>
#include <iostream>
#include "Logger.h"
using namespace Utilities;
using namespace std;
// Call this function to load a model from a *.tet file
void TetGenLoader::loadTetFile(const std::string &filename, std::vector<Vector3r>& vertices, std::vector<unsigned int>& tets)
{
LOG_INFO << "Loading " << filename;
// variables
size_t i, num_materials, num_vertices, num_tetras, num_triangles;
Real value;
string line, label;
stringstream sStream;
// try to open the file
ifstream fin(filename.c_str());
if(!fin)
{
LOG_ERR << "'" + filename + "' file not found.";
return;
}
// load tet version 1.2
getline(fin, line);
sStream << line;
sStream >> label; // tet
sStream >> label; // version
sStream >> value;
sStream.clear();
// load number of materials
getline(fin, line); // num_materials x
sStream << line;
sStream >> label;
sStream >> num_materials;
sStream.clear();
// load number of vertices
getline(fin, line); // num_vertices x
sStream << line;
sStream >> label;
sStream >> num_vertices;
sStream.clear();
// reverse the order of the vertices
vertices.resize(num_vertices);
//// read number of tetraeders
getline(fin, line); // num_tetras x
sStream << line;
sStream >> label;
sStream >> num_tetras;
sStream.clear();
tets.resize(4*num_tetras);
// read number of triangles
getline(fin, line); // num_triangles x
sStream << line;
sStream >> label;
sStream >> num_triangles;
sStream.clear();
// skip materials
getline(fin, line);
for(i = 0; i < num_materials; ++i)
getline(fin, line);
// skip the VERTICES label
getline(fin, line);
// read the vertices
for(i = 0; i < num_vertices; ++i)
{
Real x, y, z;
getline(fin, line);
sStream << line;
sStream >> x >> y >> z;
getline(sStream, line);
sStream.clear();
vertices[i] = Vector3r(x, y, z);
}
// skip TETRAS label
getline(fin, line);
// read tets
for(i = 0; i < num_tetras; ++i)
{
unsigned int tet[4];
unsigned int m;
getline(fin, line);
sStream << line;
sStream >> tet[0] >> tet[1] >> tet[2] >> tet[3] >> m;
//for (unsigned int j = 0; j < 4; j++)
// tet[j]--;
getline(sStream, line);
sStream.clear();
for (int j = 0; j < 4; j++)
tets[4*i +j] = tet[j];
}
// close file
fin.close();
LOG_INFO << "Number of tets: " << num_tetras;
LOG_INFO << "Number of vertices: " << num_vertices;
}
void TetGenLoader::loadTetgenModel(const std::string &nodeFilename, const std::string &eleFilename, std::vector<Vector3r>& vertices, std::vector<unsigned int>& tets)
{
LOG_INFO << "Loading " << nodeFilename;
LOG_INFO << "Loading " << eleFilename;
// variables
size_t i, num_vertices, num_tetras;
string nodeLine, eleLine, label;
stringstream sStream;
// try to open the file
ifstream finNode(nodeFilename.c_str());
ifstream finEle(eleFilename.c_str());
if(!finNode)
{
LOG_ERR << "'" + nodeFilename + "' file not found.";
return;
}
if(!finEle)
{
LOG_ERR << "'" + eleFilename + "' file not found.";
return;
}
// get num vertices
getline(finNode, nodeLine);
sStream << nodeLine;
sStream >> num_vertices;
sStream >> label; // 3
sStream >> label; // 0
sStream >> label; // 0
sStream.clear();
// get num tetras
getline(finEle, eleLine);
sStream << eleLine;
sStream >> num_tetras;
sStream >> label; // 4
sStream >> label; // 0
sStream >> label; // 0
sStream.clear();
vertices.resize(num_vertices);
tets.resize(4u*num_tetras);
// read vertices
for(i = 0; i < num_vertices; ++i)
{
unsigned nodeInd;
Real x, y, z;
getline(finNode, nodeLine);
sStream << nodeLine;
sStream >> nodeInd >> x >> y >> z;
getline(sStream, nodeLine);
sStream.clear();
vertices[i] = Vector3r(x, y, z);
}
// read tetrahedra
for(i = 0; i < num_tetras; ++i)
{
unsigned eleInd;
//unsigned int tet[4];
getline(finEle, eleLine);
sStream << eleLine ;
sStream >> eleInd >> tets[4*i+0] >> tets[4*i+1] >> tets[4*i+2] >> tets[4*i+3];
getline(sStream, eleLine);
sStream.clear();
}
// close file
finNode.close();
finEle.close();
LOG_INFO << "Number of tets: " << num_tetras;
LOG_INFO << "Number of vertices: " << num_vertices;
}
void TetGenLoader::loadMSHModel(const std::string &mshFilename, std::vector<Vector3r>& vertices, std::vector<unsigned int>& tets)
{
LOG_INFO << "Loading " << mshFilename;
// variables
size_t i, num_vertices, num_tetras;
string line, label;
stringstream sStream;
// try to open the file
ifstream mshStream(mshFilename.c_str());
if(!mshStream)
{
LOG_ERR << "'" << mshFilename << "' file not found.";
return;
}
// get num vertices
getline(mshStream, line);
getline(mshStream, line);
sStream << line;
sStream >> num_vertices;
sStream.clear();
vertices.resize(num_vertices);
// read vertices
for(i = 0; i < num_vertices; ++i)
{
unsigned nodeInd;
Real x, y, z;
getline(mshStream, line);
sStream << line;
sStream >> nodeInd >> x >> y >> z;
getline(sStream, line);
sStream.clear();
vertices[i] = Vector3r(x, y, z);
}
// get num tetras
getline(mshStream, line);
getline(mshStream, line);
getline(mshStream, line);
sStream << line;
sStream >> num_tetras;
sStream.clear();
tets.resize(4u*num_tetras);
// read tetrahedra
for(i = 0; i < num_tetras; ++i)
{
unsigned eleInd;
//unsigned int tet[4];
getline(mshStream, line);
sStream << line ;
sStream >> eleInd >> tets[4*i+0] >> tets[4*i+1] >> tets[4*i+2] >> tets[4*i+3];
--tets[4*i+0];
--tets[4*i+1];
--tets[4*i+2];
--tets[4*i+3];
getline(sStream, line);
sStream.clear();
}
// close file
mshStream.close();
LOG_INFO << "Number of tets: " << num_tetras;
LOG_INFO << "Number of vertices: " << num_vertices;
}