-
Notifications
You must be signed in to change notification settings - Fork 0
/
IO.cpp
328 lines (302 loc) · 11.4 KB
/
IO.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
#include "IO.hpp"
#include "IO_OBJ.hpp"
#include "GeometryTypes.hpp"
#include "GeometryUtils.hpp"
#include "Polyhedron3Utils.hpp"
#include "Strings.hpp"
#include <CGAL/Polygon_mesh_processing/self_intersections.h>
#include <CGAL/Polygon_mesh_processing/orientation.h>
#ifndef POLYHEDRON_USE_VECTOR
#include <CGAL/Polygon_mesh_processing/triangulate_faces.h>
#endif
namespace PMP = CGAL::Polygon_mesh_processing;
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
const std::string mesh_file_usage =
"Supported polyhedral mesh formats determined from file extension:\n"
" OBJ - each file can contain multiple, possibly named, meshes\n"
" OFF - specify a single 3D mesh each\n"
"In both formats normals, colors, textures, etc are all ignored. Multi-object OFF\n"
"files are not supported."
"\n\n"
"Filenames can also include options for reading/writing the files. They are given\n"
"after a colon (:) after the filename as a comma-separated list of name=value\n"
"items (or just names for boolean options). The names are case-insensitive.\n";
const std::string mesh_file_read_usage =
"Read Options:\n"
" OBJ - ObjectName - load the given object by name\n"
" GroupName - load the given group by name\n"
" ObjectIndex - load the given object by index\n"
" GroupIndex - load the given group by index\n"
" Default is to load the first group, GroupIndex=0\n"
" OFF - (no options)\n";
const std::string mesh_file_write_usage =
"Writing Options:\n"
" OBJ - ObjectName, GroupName, ObjectIndex, GroupIndex as per reading options\n"
" Append - if given data is appended to existing file\n"
" Default is to create a new file with a single unnamed group\n"
" If Append is given, ObjectName/GroupName must be given as well\n"
" OFF - Binary - if given the file in written in the binary OFF format\n";
void check_mesh(Polyhedron3* P)
{
// expensive operations that won't be necessary, usually
// TODO: add check for isolated vertices
if (!P->is_valid()) { throw std::invalid_argument("Error: invalid polyhedron."); }
if (!is_not_degenerate(P)) { throw std::invalid_argument("Error: degenerate polyhedron."); }
if (!P->is_closed()) { throw std::invalid_argument("Error: non-closed polyhedron."); }
if (!PMP::is_outward_oriented(*P)) { throw std::invalid_argument("Error: not outward oriented polyhedron"); }
if (PMP::does_self_intersect(*P)) { throw std::invalid_argument("Error: non-manifold polyhedron."); }
if (!P->is_pure_triangle())
{
#ifdef POLYHEDRON_USE_VECTOR
throw std::invalid_argument("Error: Polyhedron has non-triangle facets and unable to triangulate");
#else
// TODO: add check for planarity of faces?
std::cerr << "Polyhedron has non-triangle facets, triangulating..." << std::endl;
if (!PMP::triangulate_faces(*P))
{
throw std::invalid_argument("Error: Polyhedron has non-triangle facets and unable to triangulate");
}
#endif
}
}
#ifdef _MSC_VER
#pragma region General filename and options parsing
#endif
file_options parse_filename_and_options(const std::string& fao, std::string& filename)
{
const static int start =
#ifdef _WIN32
2; // avoid drive paths like C:
#else
1;
#endif
size_t c = fao.find_first_of(':', start);
filename = fao.substr(0, c);
return (c == std::string::npos) ? file_options() : parse_file_options(fao.substr(c+1));
}
file_options parse_file_options(const std::string& options)
{
std::stringstream opts_raw(options);
file_options opts;
std::string o;
while (std::getline(opts_raw, o, ','))
{
size_t eq = o.find_first_of('=');
opts[tolower(trim(o.substr(0, eq)))] = (eq == std::string::npos) ? "" : trim(o.substr(eq+1));
}
return opts;
}
file_type get_file_type(const std::string& filename)
{
size_t dot = filename.find_last_of('.');
if (dot == std::string::npos) { return file_type::UNKNOWN; }
std::string ext = tolower(filename.substr(dot+1));
if (ext == "obj") { return file_type::OBJ; }
else if (ext == "off") { return file_type::OFF; }
return file_type::UNKNOWN;
}
inline bool get_bool_opt(const std::string& value)
{
return !((value.length() == 1 && (value[0] == '0' || value[0] == 'f' || value[0] == 'F')) ||
(value.length() == 5 && tolower(value) == "false"));
}
#ifdef _MSC_VER
#pragma endregion
#endif
#ifdef _MSC_VER
#pragma region Reading
#endif
class P3Reader
{
public:
virtual Polyhedron3* operator()(std::istream &in) = 0;
virtual ~P3Reader() { }
};
class P3ReaderFunc : public P3Reader
{
public:
typedef Polyhedron3* (*Func)(std::istream &in);
P3ReaderFunc(Func f) : f(f) { }
virtual Polyhedron3* operator()(std::istream &in) { return this->f(in); }
private:
Func f;
};
class P3Reader_OBJ : public P3Reader
{
public:
P3Reader_OBJ(bool as_objects, size_t i) : from_name(false), as_objects(as_objects), i(i), name("") { }
P3Reader_OBJ(bool as_objects, const std::string& name) : from_name(true), as_objects(as_objects), i(0), name(name) { }
virtual Polyhedron3* operator()(std::istream &in)
{
return this->from_name ? ObjFile(in, this->as_objects)[this->name] : ObjFile(in, this->as_objects)[this->i];
}
private:
const bool from_name, as_objects;
const size_t i;
const std::string name;
};
static P3Reader* get_obj_reader(const file_options& options)
{
if (options.size() > 1) { throw std::invalid_argument("Error: invalid file options given for reading mesh"); }
if (options.size() == 0) { return new P3Reader_OBJ(false, 0); }
auto& entry = *options.begin();
const std::string& name = entry.second;
if (entry.first == "objectname") { return new P3Reader_OBJ(true, name); }
else if (entry.first == "groupname") { return new P3Reader_OBJ(false, name); }
else if (entry.first == "objectindex") { size_t i = read_int(name); return new P3Reader_OBJ(true, i); }
else if (entry.first == "groupindex") { size_t i = read_int(name); return new P3Reader_OBJ(false, i); }
else { throw std::invalid_argument("Error: invalid file options given for reading mesh"); }
}
class P3Reader_OFF : public P3Reader
{
public:
virtual Polyhedron3* operator()(std::istream &in)
{
Polyhedron3* P = new Polyhedron3();
in >> *P;
return P;
}
};
static P3Reader* get_off_reader(const file_options& options)
{
if (options.size()) { throw std::invalid_argument("Error: invalid file options given for reading mesh"); }
return new P3Reader_OFF();
}
Polyhedron3* read_mesh(const std::string& filename_and_options, bool assume_good)
{
std::string filename;
file_options opts = parse_filename_and_options(filename_and_options, filename);
return read_mesh(filename, opts, assume_good);
}
Polyhedron3* read_mesh(const std::string& filename, const std::string& options, bool assume_good)
{
return read_mesh(filename, parse_file_options(options), assume_good);
}
Polyhedron3* read_mesh(const std::string& filename, const file_options& options, bool assume_good)
{
std::ifstream f(filename.c_str());
if (f.bad()) { throw std::invalid_argument("Error: unable to open file for reading"); }
return read_mesh(f, get_file_type(filename), options, assume_good);
}
Polyhedron3* read_mesh(std::istream &in, file_type type, const file_options& options, bool assume_good)
{
if (in.bad()) { throw std::invalid_argument("Error: file handle is bad"); }
std::unique_ptr<P3Reader> reader;
switch (type)
{
case file_type::OBJ: reader.reset(get_obj_reader(options)); break;
case file_type::OFF: reader.reset(get_off_reader(options)); break;
default: throw std::invalid_argument("Error: invalid file type given the reading mesh");
}
Polyhedron3* P = reader->operator()(in);
if (!assume_good) { check_mesh(P); }
return P;
}
#ifdef _MSC_VER
#pragma endregion
#endif
#ifdef _MSC_VER
#pragma region Writing
#endif
class P3Writer
{
public:
virtual void operator()(const Polyhedron3* P, std::ostream &out) = 0;
virtual ~P3Writer() { }
};
class P3WriterFunc : public P3Writer
{
public:
typedef void (*Func)(const Polyhedron3* P, std::ostream &out);
P3WriterFunc(Func f) : f(f) { }
virtual void operator()(const Polyhedron3* P, std::ostream &out) { return this->f(P, out); }
private:
Func f;
};
class P3Writer_OBJ : public P3Writer
{
size_t off = 0;
public:
virtual void operator()(const Polyhedron3* P, std::ostream &out)
{
CGAL::set_ascii_mode(out);
write_obj(out, P, this->off);
}
};
static P3Writer* get_obj_writer(const file_options& options)
{
if (options.find("append") != options.end() ||
options.find("objectname") != options.end() ||
options.find("groupname") != options.end() ||
options.find("objectindex") != options.end() ||
options.find("groupindex") != options.end())
{
// TODO: implement
throw std::invalid_argument("Currently no options are actually support for OBJ files");
}
if (options.size() != 0)
{
throw std::invalid_argument("Error: invalid file options given for writing mesh");
}
return new P3Writer_OBJ();
}
class P3Writer_OFF : public P3Writer
{
bool binary;
public:
P3Writer_OFF(bool binary) : binary(binary) { }
virtual void operator()(const Polyhedron3* P, std::ostream &out)
{
if (this->binary) { CGAL::set_binary_mode(out); }
else
{
CGAL::set_ascii_mode(out);
//if (this->verbose) { CGAL::set_pretty_mode(out); }
}
out << *P;
}
};
static P3Writer* get_off_writer(const file_options& options)
{
if (options.size() > 1) { throw std::invalid_argument("Error: invalid file options given for writing mesh"); }
if (options.size() == 0) { return new P3Writer_OFF(false); }
auto& entry = *options.begin();
if (entry.first == "binary") { return new P3Writer_OFF(get_bool_opt(entry.second)); }
else { throw std::invalid_argument("Error: invalid file options given for writing mesh"); }
}
void write_mesh(const Polyhedron3* P, const std::string& filename_and_options)
{
std::string filename;
file_options opts = parse_filename_and_options(filename_and_options, filename);
write_mesh(P, filename, opts);
}
void write_mesh(const Polyhedron3* P, const std::string& filename, const std::string& options)
{
write_mesh(P, filename, parse_file_options(options));
}
void write_mesh(const Polyhedron3* P, const std::string& filename, const file_options& options)
{
std::ofstream f(filename.c_str());
if (f.bad()) { throw std::invalid_argument("Error: unable to open file for writing"); }
write_mesh(P, f, get_file_type(filename), options);
}
void write_mesh(const Polyhedron3* P, std::ostream &out, file_type type, const file_options& options)
{
if (out.bad()) { throw std::invalid_argument("Error: file handle is bad"); }
std::unique_ptr<P3Writer> writer;
switch (type)
{
case file_type::OBJ: writer.reset(get_obj_writer(options)); break;
case file_type::OFF: writer.reset(get_off_writer(options)); break;
default: throw std::invalid_argument("Error: invalid file type given the writing mesh");
}
// TODO: use writers across multiple Polyhedron3 to the same file
writer->operator()(P, out);
}
#ifdef _MSC_VER
#pragma endregion
#endif