Repository navigation
Expand file tree
/
Copy pathMEDITParser.cpp
More file actions
199 lines (179 loc) · 5.6 KB
/
Copy pathMEDITParser.cpp
File metadata and controls
199 lines (179 loc) · 5.6 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
/* This file is part of PyMesh. Copyright (c) 2018 by Qingnan Zhou */
#include "MEDITParser.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <Core/Exception.h>
#include "IOUtils.h"
using namespace PyMesh;
bool MEDITParser::parse(const std::string& filename) {
std::ifstream fin(filename.c_str());
if (!fin.is_open()) {
std::stringstream err_msg;
err_msg << "failed to open file \"" << filename << "\"";
throw IOError(err_msg.str());
}
bool success = true;
success &= parse_header(fin);
if (!success) {
throw IOError("Error parsing file header.");
}
const size_t LINE_SIZE = 256;
char line[LINE_SIZE];
while (!fin.eof()) {
std::string elem_type;
fin >> elem_type;
if (elem_type.size() == 0) continue;
if (elem_type[0] == '#') {
fin.getline(line, LINE_SIZE);
continue;
}
if (elem_type == "Vertices") {
success &= parse_vertices(fin);
} else if (elem_type == "Triangles") {
m_vertex_per_face = 3;
success &= parse_faces(fin);
} else if (elem_type == "Quadrilaterals") {
m_vertex_per_face = 4;
success &= parse_faces(fin);
} else if (elem_type == "Tetrahedra") {
if (m_faces.size() == 0) {
m_vertex_per_face = 3;
} else if (m_vertex_per_face != 3){
success = false;
}
m_vertex_per_voxel = 4;
success &= parse_voxels(fin);
} else if (elem_type == "Hexahedra") {
if (m_faces.size() == 0) {
m_vertex_per_face = 4;
} else if (m_vertex_per_face != 4){
success = false;
}
m_vertex_per_voxel = 8;
success &= parse_voxels(fin);
} else if (elem_type == "End") {
break;
} else {
if (elem_type != "Edges" &&
elem_type != "Corners" &&
elem_type != "RequiredVertices" &&
elem_type != "Ridges" &&
elem_type != "RequiredEdges" &&
elem_type != "Normals" &&
elem_type != "Tangents" &&
elem_type != "NormalAtVertices" &&
elem_type != "NormalAtTriangleVertices" &&
elem_type != "NormalAtQuadrilateralVertices" &&
elem_type != "TangentAtEdges" &&
elem_type != "End") {
success = false;
} else {
std::cerr << "Warning: Skipping " << elem_type
<< " field" << std::endl;
success &= skip_field(fin);
}
}
if (!success) {
std::stringstream err_msg;
err_msg << "Error parsing \"" << elem_type << "\" field in " <<
filename;
throw IOError(err_msg.str());
}
}
fin.close();
return success;
}
size_t MEDITParser::num_faces() const {
if (m_vertex_per_face == 0) return 0;
else return m_faces.size() / m_vertex_per_face;
}
size_t MEDITParser::num_voxels() const {
if (m_vertex_per_voxel == 0) return 0;
else return m_voxels.size() / m_vertex_per_voxel;
}
MEDITParser::AttrNames MEDITParser::get_attribute_names() const {
MEDITParser::AttrNames attr_names;
return attr_names;
}
void MEDITParser::export_vertices(Float* buffer) {
std::copy(m_vertices.begin(), m_vertices.end(), buffer);
}
void MEDITParser::export_faces(int* buffer) {
std::copy(m_faces.begin(), m_faces.end(), buffer);
}
void MEDITParser::export_voxels(int* buffer) {
std::copy(m_voxels.begin(), m_voxels.end(), buffer);
}
void MEDITParser::export_attribute(const std::string& name, Float* buffer) {
std::cerr << "Warning: attribute " << name << " does not exist." <<
std::endl;
}
bool MEDITParser::parse_header(std::ifstream& fin) {
std::string str_val;
int int_val;
fin >> str_val >> int_val;
if (str_val != "MeshVersionFormatted" || (int_val != 1 && int_val != 2)) {
return false;
}
fin >> str_val >> m_dim;
if (str_val != "Dimension" || (m_dim != 3 && m_dim != 2)) {
return false;
}
return true;
}
bool MEDITParser::parse_vertices(std::ifstream& fin) {
size_t num_vertices=0;
fin >> num_vertices;
Float val;
int ref;
for (size_t i=0; i<num_vertices; i++) {
for (size_t j=0; j<m_dim; j++) {
fin >> val;
m_vertices.push_back(val);
}
fin >> ref;
if (!fin.good()) return false;
}
return true;
}
bool MEDITParser::parse_faces(std::ifstream& fin) {
size_t num_faces=0;
fin >> num_faces;
int val, ref;
for (size_t i=0; i<num_faces; i++) {
for (size_t j=0; j<m_vertex_per_face; j++) {
fin >> val;
m_faces.push_back(val-1);
}
fin >> ref;
if (!fin.good()) return false;
}
return true;
}
bool MEDITParser::parse_voxels(std::ifstream& fin) {
size_t num_voxels=0;
fin >> num_voxels;
int val, ref;
for (size_t i=0; i<num_voxels; i++) {
for (size_t j=0; j<m_vertex_per_voxel; j++) {
fin >> val;
m_voxels.push_back(val-1);
}
fin >> ref;
if (!fin.good()) return false;
}
return true;
}
bool MEDITParser::skip_field(std::ifstream& fin) {
const size_t LINE_SIZE = 256;
char line[LINE_SIZE];
size_t num_lines=0;
fin >> num_lines;
IOUtils::eat_white_space(fin);
for (size_t i=0; i<num_lines; i++) {
fin.getline(line, LINE_SIZE);
if (!fin.good()) return false;
}
return true;
}