-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvol_mesh.cpp
More file actions
231 lines (210 loc) · 4.76 KB
/
vol_mesh.cpp
File metadata and controls
231 lines (210 loc) · 4.76 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
#include "vol_mesh.h"
#include <stdexcept>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace mesh3d;
/** Parser states */
enum State {
ST_NORM,
ST_SURF_INIT,
ST_SURF_DATA,
ST_VOL_INIT,
ST_VOL_DATA,
ST_PTS_INIT,
ST_PTS_DATA,
ST_CURVE_INIT,
ST_CURVE_DATA,
ST_STOP,
};
#define HAS_PREFIX(string, prefix) (0 == strncmp((string), (prefix), sizeof(prefix) / sizeof(prefix[0]) - 1))
const std::string getStateString(State st) {
#define STATE_CASE(x) case x: return #x
switch (st) {
STATE_CASE(ST_NORM);
STATE_CASE(ST_SURF_INIT);
STATE_CASE(ST_SURF_DATA);
STATE_CASE(ST_VOL_INIT);
STATE_CASE(ST_VOL_DATA);
STATE_CASE(ST_PTS_INIT);
STATE_CASE(ST_PTS_DATA);
STATE_CASE(ST_CURVE_INIT);
STATE_CASE(ST_CURVE_DATA);
STATE_CASE(ST_STOP);
default:
return "(error state)";
}
#undef STATE_CASE
}
double get_double(char *&p) {
char *q;
double ret = strtod(p, &q);
p = q;
return ret;
}
int get_int(char *&p) {
char *q;
long ret = strtol(p, &q, 10);
p = q;
return ret;
}
long long get_ll(char *&p) {
char *q;
long long ret = strtoll(p, &q, 10);
p = q;
return ret;
}
vol_mesh::vol_mesh(const char *fn) {
std::ifstream f(fn, std::ios::in);
if (!f)
throw std::invalid_argument("Could not read file `" + std::string(fn) +"'");
char buf[1024];
nV = 0;
nB = 0;
nT = 0;
int iext = 0;
int i = 0, cnt = 0;
State state = ST_NORM;
while (!!f.getline(buf, 1024)) {
if (buf[0] == '#')
continue;
if (state == ST_NORM) {
if (HAS_PREFIX(buf, "endmesh")) {
state = ST_STOP;
break;
}
if (HAS_PREFIX(buf, "surfaceelements")) {
state = ST_SURF_INIT;
continue;
}
if (HAS_PREFIX(buf, "points")) {
state = ST_PTS_INIT;
continue;
}
if (HAS_PREFIX(buf, "volumeelements")) {
state = ST_VOL_INIT;
continue;
}
if (HAS_PREFIX(buf, "edgesegmentsgi2")) {
state = ST_CURVE_INIT;
continue;
}
}
if (state == ST_PTS_INIT) {
nV = cnt = atoi(buf);
vert.resize(3 * nV);
i = 0;
state = cnt ? ST_PTS_DATA : ST_NORM;
continue;
}
if (state == ST_VOL_INIT) {
nT = cnt = atoi(buf);
tet.resize(4 * nT);
tetmat.resize(nT);
i = 0;
state = cnt ? ST_VOL_DATA : ST_NORM;
continue;
}
if (state == ST_SURF_INIT) {
nB = cnt = atoi(buf);
bnd.resize(3 * nB);
bndmat.resize(nB);
iext = 0;
i = 0;
state = cnt ? ST_SURF_DATA : ST_NORM;
continue;
}
if (state == ST_CURVE_INIT) {
cnt = atoi(buf);
i = 0;
state = cnt ? ST_CURVE_DATA : ST_NORM;
/* Just ignore it */
continue;
};
if (state == ST_PTS_DATA) {
// sscanf(buf, "%lf %lf %lf", &vert[3*i], &vert[3*i + 1], &vert[3*i + 2]);
char *p = buf;
vert[3*i + 0] = get_double(p);
vert[3*i + 1] = get_double(p);
vert[3*i + 2] = get_double(p);
if (++i == cnt)
state = ST_NORM;
continue;
}
if (state == ST_VOL_DATA) {
int np;
// sscanf(buf, "%lld %d %lld %lld %lld %lld", &tetmat[i], &np,
// &tet[4*i], &tet[4*i + 1], &tet[4*i + 2], &tet[4*i + 3]);
char *p = buf;
tetmat[i] = get_ll(p);
np = get_int(p);
tet[4*i + 0] = get_ll(p) - 1;
tet[4*i + 1] = get_ll(p) - 1;
tet[4*i + 2] = get_ll(p) - 1;
tet[4*i + 3] = get_ll(p) - 1;
if (np != 4)
throw std::domain_error("High-order tetrahedrons not implemented");
if (++i == cnt)
state = ST_NORM;
continue;
}
if (state == ST_SURF_DATA) {
int domout, np;
//sscanf(buf, "%d %lld %d %d %d %lld %lld %lld", &sn, &bndmat[iext], &domin, &domout, &np,
// &bnd[3*iext], &bnd[3*iext + 1], &bnd[3*iext + 2]);
char *p = buf;
get_int(p);
bndmat[iext] = get_ll(p);
get_int(p);
domout = get_int(p);
np = get_int(p);
bnd[3*iext + 0] = get_ll(p) - 1;
bnd[3*iext + 1] = get_ll(p) - 1;
bnd[3*iext + 2] = get_ll(p) - 1;
if (domout == 0)
iext++;
if (np != 3)
throw std::domain_error("High-order faces not implemented");
if (++i == cnt) {
state = ST_NORM;
nB = iext;
}
continue;
}
if (state == ST_CURVE_DATA) {
if (++i == cnt)
state = ST_NORM;
continue;
}
}
f.close();
if (state != ST_STOP)
throw std::logic_error("Parse vol file failed: state = `" +
getStateString(state) + "' at the end of file");
}
vol_mesh::~vol_mesh() {
}
mesh3d::index vol_mesh::num_vertices() const {
return nV;
}
mesh3d::index vol_mesh::num_tetrahedrons() const {
return nT;
}
mesh3d::index vol_mesh::num_bnd_faces() const {
return nB;
}
const double *vol_mesh::vertex_coord(mesh3d::index i) const {
return &vert[3 * i];
}
const mesh3d::index *vol_mesh::tet_verts(mesh3d::index i) const {
return &tet[4 * i];
}
const mesh3d::index *vol_mesh::bnd_verts(mesh3d::index i) const {
return &bnd[3 * i];
}
mesh3d::index vol_mesh::tet_material(mesh3d::index i) const {
return tetmat[i];
}
mesh3d::index vol_mesh::bnd_material(mesh3d::index i) const {
return bndmat[i];
}