43 #define FILEMANAGERT_CC 53 #include <OpenVolumeMesh/Geometry/VectorT.hh> 54 #include <OpenVolumeMesh/Mesh/PolyhedralMesh.hh> 56 #include "FileManager.hh" 68 bool _topologyCheck,
bool _computeBottomUpIncidences)
const 70 std::stringstream sstr;
74 typedef typename MeshT::PointT Point;
75 Point v = Point(0.0, 0.0, 0.0);
81 _mesh.enable_bottom_up_incidences(
false);
87 bool header_found =
true;
90 getCleanLine(_istream, line);
95 std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
99 if (verbosity_level_ >= 1) {
100 std::cerr <<
"The specified file might not be in OpenVolumeMesh format!" << std::endl;
107 std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
108 if(s_tmp ==
"BINARY") {
109 if (verbosity_level_ >= 1) {
110 std::cerr <<
"Binary files are not supported at the moment!" << std::endl;
122 getCleanLine(_istream, line);
128 std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
129 if(s_tmp !=
"VERTICES") {
130 if (verbosity_level_ >= 1) {
131 std::cerr <<
"No vertex section defined!" << std::endl;
137 getCleanLine(_istream, line);
143 for(uint64_t i = 0u; i < c; ++i) {
145 getCleanLine(_istream, line);
158 getCleanLine(_istream, line);
162 std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
163 if(s_tmp !=
"EDGES") {
164 if (verbosity_level_ >= 2) {
165 std::cerr <<
"No edge section defined!" << std::endl;
171 getCleanLine(_istream, line);
177 for(uint64_t i = 0u; i < c; ++i) {
181 getCleanLine(_istream, line);
193 getCleanLine(_istream, line);
197 std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
198 if(s_tmp !=
"FACES") {
199 if (verbosity_level_ >= 2) {
200 std::cerr <<
"No face section defined!" << std::endl;
206 getCleanLine(_istream, line);
212 for(uint64_t i = 0u; i < c; ++i) {
214 getCleanLine(_istream, line);
218 std::vector<HalfEdgeHandle> hes;
225 for(
unsigned int e = 0; e < val; ++e) {
232 _mesh.add_face(hes, _topologyCheck);
239 getCleanLine(_istream, line);
243 std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
244 if(s_tmp !=
"POLYHEDRA") {
245 if (verbosity_level_ >= 2) {
246 std::cerr <<
"No polyhedra section defined!" << std::endl;
252 getCleanLine(_istream, line);
258 for(uint64_t i = 0u; i < c; ++i) {
260 getCleanLine(_istream, line);
264 std::vector<HalfFaceHandle> hfs;
271 for(
unsigned int f = 0; f < val; ++f) {
278 _mesh.add_cell(hfs, _topologyCheck);
282 while(!_istream.eof()) {
287 readProperty(_istream, _mesh);
290 if(_computeBottomUpIncidences) {
292 _mesh.enable_bottom_up_incidences(
true);
295 if (verbosity_level_ >= 2) {
296 std::cerr <<
"######## openvolumemesh info #########" << std::endl;
297 std::cerr <<
"#vertices: " << _mesh.n_vertices() << std::endl;
298 std::cerr <<
"#edges: " << _mesh.n_edges() << std::endl;
299 std::cerr <<
"#faces: " << _mesh.n_faces() << std::endl;
300 std::cerr <<
"#cells: " << _mesh.n_cells() << std::endl;
301 std::cerr <<
"######################################" << std::endl;
307 template <
class MeshT>
309 bool _topologyCheck,
bool _computeBottomUpIncidences)
const {
311 std::ifstream iff(_filename.c_str(), std::ios::in);
314 if (verbosity_level_ >= 1) {
315 std::cerr <<
"Error: Could not open file " << _filename <<
" for reading!" << std::endl;
320 return readStream(iff, _mesh, _topologyCheck,_computeBottomUpIncidences);
325 template <
class MeshT>
326 void FileManager::readProperty(std::istream& _iff, MeshT& _mesh)
const {
328 std::string line, entity_t, prop_t, name;
329 std::stringstream sstr;
331 getCleanLine(_iff, line);
333 if(line.empty())
return;
338 std::transform(entity_t.begin(), entity_t.end(), entity_t.begin(), ::tolower);
340 std::transform(prop_t.begin(), prop_t.end(), prop_t.begin(), ::tolower);
342 extractQuotedText(name);
344 if (verbosity_level_ >= 2) {
345 std::cerr <<
"OVM read property " << name <<
" of type " << prop_t << std::endl;
348 if(prop_t == typeName<int>()) generateGenericProperty<int, MeshT>(entity_t, name, _iff, _mesh);
349 else if(prop_t == typeName<unsigned int>()) generateGenericProperty<unsigned int, MeshT>(entity_t, name, _iff, _mesh);
350 else if(prop_t == typeName<short>()) generateGenericProperty<short, MeshT>(entity_t, name, _iff, _mesh);
351 else if(prop_t == typeName<long>()) generateGenericProperty<long, MeshT>(entity_t, name, _iff, _mesh);
352 else if(prop_t == typeName<unsigned long>()) generateGenericProperty<unsigned long, MeshT>(entity_t, name, _iff, _mesh);
353 else if(prop_t == typeName<char>()) generateGenericProperty<char, MeshT>(entity_t, name, _iff, _mesh);
354 else if(prop_t == typeName<unsigned char>()) generateGenericProperty<unsigned char, MeshT>(entity_t, name, _iff, _mesh);
355 else if(prop_t == typeName<bool>()) generateGenericProperty<bool, MeshT>(entity_t, name, _iff, _mesh);
356 else if(prop_t == typeName<float>()) generateGenericProperty<float, MeshT>(entity_t, name, _iff, _mesh);
357 else if(prop_t == typeName<double>()) generateGenericProperty<double, MeshT>(entity_t, name, _iff, _mesh);
358 else if(prop_t == typeName<std::string>()) generateGenericProperty<std::string, MeshT>(entity_t, name, _iff, _mesh);
359 else if(prop_t ==
typeName<std::map<HalfEdgeHandle, int> >()) generateGenericProperty<std::map<HalfEdgeHandle, int>, MeshT>(entity_t, name, _iff, _mesh);
360 else if(prop_t ==
typeName<std::vector<double> >()) generateGenericProperty<std::vector<double> , MeshT>(entity_t, name, _iff, _mesh);
361 else if(prop_t ==
typeName<std::vector<VertexHandle> >()) generateGenericProperty<std::vector<VertexHandle> , MeshT>(entity_t, name, _iff, _mesh);
362 else if(prop_t ==
typeName<std::vector<HalfFaceHandle> >()) generateGenericProperty<std::vector<HalfFaceHandle> , MeshT>(entity_t, name, _iff, _mesh);
363 else if(prop_t ==
typeName<std::vector<std::vector<HalfFaceHandle> > >()) generateGenericProperty<std::vector<std::vector<HalfFaceHandle> > , MeshT>(entity_t, name, _iff, _mesh);
365 else if(prop_t == typeName<Vec2f>()) generateGenericProperty<Vec2f, MeshT>(entity_t, name, _iff, _mesh);
366 else if(prop_t == typeName<Vec2d>()) generateGenericProperty<Vec2d, MeshT>(entity_t, name, _iff, _mesh);
367 else if(prop_t == typeName<Vec2i>()) generateGenericProperty<Vec2i, MeshT>(entity_t, name, _iff, _mesh);
368 else if(prop_t == typeName<Vec2ui>()) generateGenericProperty<Vec2ui, MeshT>(entity_t, name, _iff, _mesh);
370 else if(prop_t == typeName<Vec3f>()) generateGenericProperty<Vec3f, MeshT>(entity_t, name, _iff, _mesh);
371 else if(prop_t == typeName<Vec3d>()) generateGenericProperty<Vec3d, MeshT>(entity_t, name, _iff, _mesh);
372 else if(prop_t == typeName<Vec3i>()) generateGenericProperty<Vec3i, MeshT>(entity_t, name, _iff, _mesh);
373 else if(prop_t == typeName<Vec3ui>()) generateGenericProperty<Vec3ui, MeshT>(entity_t, name, _iff, _mesh);
375 else if(prop_t == typeName<Vec4f>()) generateGenericProperty<Vec4f, MeshT>(entity_t, name, _iff, _mesh);
376 else if(prop_t == typeName<Vec4d>()) generateGenericProperty<Vec4d, MeshT>(entity_t, name, _iff, _mesh);
377 else if(prop_t == typeName<Vec4i>()) generateGenericProperty<Vec4i, MeshT>(entity_t, name, _iff, _mesh);
378 else if(prop_t == typeName<Vec4ui>()) generateGenericProperty<Vec4ui, MeshT>(entity_t, name, _iff, _mesh);
386 template <
class PropT,
class MeshT>
387 void FileManager::generateGenericProperty(
const std::string& _entity_t,
const std::string& _name,
388 std::istream& _iff, MeshT& _mesh)
const {
390 if(_entity_t ==
"vprop") {
392 prop.deserialize(_iff);
393 _mesh.set_persistent(prop);
394 }
else if(_entity_t ==
"eprop") {
396 prop.deserialize(_iff);
397 _mesh.set_persistent(prop);
398 }
else if(_entity_t ==
"heprop") {
400 prop.deserialize(_iff);
401 _mesh.set_persistent(prop);
402 }
else if(_entity_t ==
"fprop") {
404 prop.deserialize(_iff);
405 _mesh.set_persistent(prop);
406 }
else if(_entity_t ==
"hfprop") {
408 prop.deserialize(_iff);
409 _mesh.set_persistent(prop);
410 }
else if(_entity_t ==
"cprop") {
412 prop.deserialize(_iff);
413 _mesh.set_persistent(prop);
414 }
else if(_entity_t ==
"mprop") {
416 prop.deserialize(_iff);
417 _mesh.set_persistent(prop);
424 template<
class MeshT>
428 _ostream <<
"OVM ASCII" << std::endl;
430 uint64_t n_vertices(_mesh.n_vertices());
431 _ostream <<
"Vertices" << std::endl;
432 _ostream << n_vertices << std::endl;
434 typedef typename MeshT::PointT Point;
437 for(
VertexIter v_it = _mesh.v_iter(); v_it; ++v_it) {
439 Point v = _mesh.vertex(*v_it);
440 _ostream << v[0] <<
" " << v[1] <<
" " << v[2] << std::endl;
443 uint64_t n_edges(_mesh.n_edges());
444 _ostream <<
"Edges" << std::endl;
445 _ostream << n_edges << std::endl;
448 for(
EdgeIter e_it = _mesh.e_iter(); e_it; ++e_it) {
450 VertexHandle from_vertex = _mesh.edge(*e_it).from_vertex();
452 _ostream << from_vertex <<
" " << to_vertex << std::endl;
455 uint64_t n_faces(_mesh.n_faces());
456 _ostream <<
"Faces" << std::endl;
457 _ostream << n_faces << std::endl;
460 for(
FaceIter f_it = _mesh.f_iter(); f_it; ++f_it) {
462 _ostream << static_cast<uint64_t>(_mesh.face(*f_it).halfedges().size()) <<
" ";
464 std::vector<HalfEdgeHandle> halfedges = _mesh.face(*f_it).halfedges();
466 for(
typename std::vector<HalfEdgeHandle>::const_iterator it = halfedges.begin(); it
467 != halfedges.end(); ++it) {
469 _ostream << it->idx();
471 if((it + 1) != halfedges.end())
475 _ostream << std::endl;
478 uint64_t n_cells(_mesh.n_cells());
479 _ostream <<
"Polyhedra" << std::endl;
480 _ostream << n_cells << std::endl;
482 for(
CellIter c_it = _mesh.c_iter(); c_it; ++c_it) {
484 _ostream << static_cast<uint64_t>(_mesh.cell(*c_it).halffaces().size()) <<
" ";
486 std::vector<HalfFaceHandle> halffaces = _mesh.cell(*c_it).halffaces();
488 for(
typename std::vector<HalfFaceHandle>::const_iterator it = halffaces.begin(); it
489 != halffaces.end(); ++it) {
491 _ostream << it->idx();
493 if((it + 1) != halffaces.end())
497 _ostream << std::endl;
501 writeProps(_ostream, _mesh.vertex_props_begin(), _mesh.vertex_props_end());
503 writeProps(_ostream, _mesh.edge_props_begin(), _mesh.edge_props_end());
505 writeProps(_ostream, _mesh.halfedge_props_begin(), _mesh.halfedge_props_end());
507 writeProps(_ostream, _mesh.face_props_begin(), _mesh.face_props_end());
509 writeProps(_ostream, _mesh.halfface_props_begin(), _mesh.halfface_props_end());
511 writeProps(_ostream, _mesh.cell_props_begin(), _mesh.cell_props_end());
513 writeProps(_ostream, _mesh.mesh_props_begin(), _mesh.mesh_props_end());
516 template<
class MeshT>
519 std::ofstream off(_filename.c_str(), std::ios::out);
522 if (verbosity_level_ >= 1) {
523 std::cerr <<
"Error: Could not open file " << _filename <<
" for writing!" << std::endl;
527 writeStream(off, _mesh);
533 template<
class IteratorT>
534 void FileManager::writeProps(std::ostream& _ostr,
const IteratorT& _begin,
const IteratorT& _end)
const {
537 for(IteratorT p_it = _begin;
538 p_it != _end; ++p_it) {
539 if(!(*p_it)->persistent())
continue;
540 if((*p_it)->anonymous()) {
541 if (verbosity_level_ >= 2) {
542 std::cerr <<
"Serialization of anonymous properties is not supported!" << std::endl;
547 std::string type_name;
549 type_name = (*p_it)->typeNameWrapper();
550 }
catch (std::runtime_error &e) {
551 if (verbosity_level_ >= 1) {
552 std::cerr <<
"Failed to save property " << (*p_it)->name() <<
" , skipping: " << e.what() << std::endl;
556 _ostr << (*p_it)->entityType() <<
" ";
557 _ostr << type_name <<
" ";
558 _ostr <<
"\"" << (*p_it)->name() <<
"\"" << std::endl;
560 (*p_it)->serialize(_ostr);
DLLEXPORT QString typeName(DataType _id)
Get the name of a type with given id.
bool readStream(std::istream &_istream, MeshT &_mesh, bool _topologyCheck=true, bool _computeBottomUpIncidences=true) const
Read a mesh from an std::istream.
bool readFile(const std::string &_filename, MeshT &_mesh, bool _topologyCheck=true, bool _computeBottomUpIncidences=true) const
Read a mesh from a file.
void writeStream(std::ostream &_ostream, const MeshT &_mesh) const
Write a mesh to an std::ostream.
bool writeFile(const std::string &_filename, const MeshT &_mesh) const
Write a mesh to a file.