43 #define FILEMANAGERT_CC
52 #include <OpenVolumeMesh/Geometry/VectorT.hh>
53 #include <OpenVolumeMesh/Mesh/PolyhedralMesh.hh>
55 #include "FileManager.hh"
65 template <
class MeshT>
67 bool _topologyCheck,
bool _computeBottomUpIncidences)
const {
69 std::ifstream iff(_filename.c_str(), std::ios::in);
72 std::cerr <<
"Error: Could not open file " << _filename <<
" for reading!" << std::endl;
77 std::stringstream sstr;
81 typedef typename MeshT::PointT Point;
82 Point v = Point(0.0, 0.0, 0.0);
88 _mesh.enable_bottom_up_incidences(
false);
94 bool header_found =
true;
97 getCleanLine(iff, line);
102 std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
105 header_found =
false;
106 std::cerr <<
"The specified file might not be in OpenVolumeMesh format!" << std::endl;
112 std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
113 if(s_tmp ==
"BINARY") {
115 std::cerr <<
"Binary files are not supported at the moment!" << std::endl;
126 getCleanLine(iff, line);
132 std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
133 if(s_tmp !=
"VERTICES") {
135 std::cerr <<
"No vertex section defined!" << std::endl;
140 getCleanLine(iff, line);
146 for(
unsigned int i = 0u; i < c; ++i) {
148 getCleanLine(iff, line);
161 getCleanLine(iff, line);
165 std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
166 if(s_tmp !=
"EDGES") {
168 std::cerr <<
"No edge section defined!" << std::endl;
173 getCleanLine(iff, line);
179 for(
unsigned int i = 0u; i < c; ++i) {
183 getCleanLine(iff, line);
195 getCleanLine(iff, line);
199 std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
200 if(s_tmp !=
"FACES") {
202 std::cerr <<
"No face section defined!" << std::endl;
207 getCleanLine(iff, line);
213 for(
unsigned int i = 0u; i < c; ++i) {
215 getCleanLine(iff, line);
219 std::vector<HalfEdgeHandle> hes;
222 unsigned int val = 0u;
226 for(
unsigned int e = 0; e < val; ++e) {
233 _mesh.add_face(hes, _topologyCheck);
240 getCleanLine(iff, line);
244 std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
245 if(s_tmp !=
"POLYHEDRA") {
247 std::cerr <<
"No polyhedra section defined!" << std::endl;
252 getCleanLine(iff, line);
258 for(
unsigned int i = 0u; i < c; ++i) {
260 getCleanLine(iff, line);
264 std::vector<HalfFaceHandle> hfs;
267 unsigned int val = 0u;
271 for(
unsigned int f = 0; f < val; ++f) {
278 _mesh.add_cell(hfs, _topologyCheck);
287 readProperty(iff, _mesh);
292 if(_computeBottomUpIncidences) {
294 _mesh.enable_bottom_up_incidences(
true);
297 std::cerr <<
"######## openvolumemesh info #########" << std::endl;
298 std::cerr <<
"#vertices: " << _mesh.n_vertices() << std::endl;
299 std::cerr <<
"#edges: " << _mesh.n_edges() << std::endl;
300 std::cerr <<
"#faces: " << _mesh.n_faces() << std::endl;
301 std::cerr <<
"#cells: " << _mesh.n_cells() << std::endl;
302 std::cerr <<
"######################################" << std::endl;
309 template <
class MeshT>
310 void FileManager::readProperty(std::istream& _iff, MeshT& _mesh)
const {
312 std::string line, entity_t, prop_t, name;
313 std::stringstream sstr;
315 getCleanLine(_iff, line);
317 if(line.empty())
return;
322 std::transform(entity_t.begin(), entity_t.end(), entity_t.begin(), ::tolower);
324 std::transform(prop_t.begin(), prop_t.end(), prop_t.begin(), ::tolower);
326 extractQuotedText(name);
328 if(prop_t == typeName<int>()) generateGenericProperty<int, MeshT>(entity_t, name, _iff, _mesh);
329 else if(prop_t == typeName<unsigned int>()) generateGenericProperty<unsigned int, MeshT>(entity_t, name, _iff, _mesh);
330 else if(prop_t == typeName<short>()) generateGenericProperty<short, MeshT>(entity_t, name, _iff, _mesh);
331 else if(prop_t == typeName<long>()) generateGenericProperty<long, MeshT>(entity_t, name, _iff, _mesh);
332 else if(prop_t == typeName<unsigned long>()) generateGenericProperty<unsigned long, MeshT>(entity_t, name, _iff, _mesh);
333 else if(prop_t == typeName<char>()) generateGenericProperty<char, MeshT>(entity_t, name, _iff, _mesh);
334 else if(prop_t == typeName<unsigned char>()) generateGenericProperty<unsigned char, MeshT>(entity_t, name, _iff, _mesh);
335 else if(prop_t == typeName<bool>()) generateGenericProperty<bool, MeshT>(entity_t, name, _iff, _mesh);
336 else if(prop_t == typeName<float>()) generateGenericProperty<float, MeshT>(entity_t, name, _iff, _mesh);
337 else if(prop_t == typeName<double>()) generateGenericProperty<double, MeshT>(entity_t, name, _iff, _mesh);
338 else if(prop_t == typeName<std::string>()) generateGenericProperty<std::string, MeshT>(entity_t, name, _iff, _mesh);
340 else if(prop_t == typeName<Vec2f>()) generateGenericProperty<Vec2f, MeshT>(entity_t, name, _iff, _mesh);
341 else if(prop_t == typeName<Vec2d>()) generateGenericProperty<Vec2d, MeshT>(entity_t, name, _iff, _mesh);
342 else if(prop_t == typeName<Vec2i>()) generateGenericProperty<Vec2i, MeshT>(entity_t, name, _iff, _mesh);
343 else if(prop_t == typeName<Vec2ui>()) generateGenericProperty<Vec2ui, MeshT>(entity_t, name, _iff, _mesh);
345 else if(prop_t == typeName<Vec3f>()) generateGenericProperty<Vec3f, MeshT>(entity_t, name, _iff, _mesh);
346 else if(prop_t == typeName<Vec3d>()) generateGenericProperty<Vec3d, MeshT>(entity_t, name, _iff, _mesh);
347 else if(prop_t == typeName<Vec3i>()) generateGenericProperty<Vec3i, MeshT>(entity_t, name, _iff, _mesh);
348 else if(prop_t == typeName<Vec3ui>()) generateGenericProperty<Vec3ui, MeshT>(entity_t, name, _iff, _mesh);
350 else if(prop_t == typeName<Vec4f>()) generateGenericProperty<Vec4f, MeshT>(entity_t, name, _iff, _mesh);
351 else if(prop_t == typeName<Vec4d>()) generateGenericProperty<Vec4d, MeshT>(entity_t, name, _iff, _mesh);
352 else if(prop_t == typeName<Vec4i>()) generateGenericProperty<Vec4i, MeshT>(entity_t, name, _iff, _mesh);
353 else if(prop_t == typeName<Vec4ui>()) generateGenericProperty<Vec4ui, MeshT>(entity_t, name, _iff, _mesh);
361 template <
class PropT,
class MeshT>
362 void FileManager::generateGenericProperty(
const std::string& _entity_t,
const std::string& _name,
363 std::istream& _iff, MeshT& _mesh)
const {
365 if(_entity_t ==
"vprop") {
367 prop.deserialize(_iff);
368 _mesh.set_persistent(prop);
369 }
else if(_entity_t ==
"eprop") {
370 EdgePropertyT<PropT> prop = _mesh.template request_edge_property<PropT>(_name);
371 prop.deserialize(_iff);
372 _mesh.set_persistent(prop);
373 }
else if(_entity_t ==
"heprop") {
374 HalfEdgePropertyT<PropT> prop = _mesh.template request_halfedge_property<PropT>(_name);
375 prop.deserialize(_iff);
376 _mesh.set_persistent(prop);
377 }
else if(_entity_t ==
"fprop") {
378 FacePropertyT<PropT> prop = _mesh.template request_face_property<PropT>(_name);
379 prop.deserialize(_iff);
380 _mesh.set_persistent(prop);
381 }
else if(_entity_t ==
"hfprop") {
382 HalfFacePropertyT<PropT> prop = _mesh.template request_halfface_property<PropT>(_name);
383 prop.deserialize(_iff);
384 _mesh.set_persistent(prop);
385 }
else if(_entity_t ==
"cprop") {
386 CellPropertyT<PropT> prop = _mesh.template request_cell_property<PropT>(_name);
387 prop.deserialize(_iff);
388 _mesh.set_persistent(prop);
389 }
else if(_entity_t ==
"mprop") {
390 MeshPropertyT<PropT> prop = _mesh.template request_mesh_property<PropT>(_name);
391 prop.deserialize(_iff);
392 _mesh.set_persistent(prop);
398 template<
class MeshT>
401 std::ofstream off(_filename.c_str(), std::ios::out);
404 std::cerr <<
"Error: Could not open file " << _filename <<
" for writing!" << std::endl;
410 off <<
"OVM ASCII" << std::endl;
412 unsigned int n_vertices(_mesh.n_vertices());
413 off <<
"Vertices" << std::endl;
414 off << n_vertices << std::endl;
416 typedef typename MeshT::PointT Point;
419 for(
VertexIter v_it = _mesh.v_iter(); v_it; ++v_it) {
421 Point v = _mesh.vertex(*v_it);
422 off << v[0] <<
" " << v[1] <<
" " << v[2] << std::endl;
425 unsigned int n_edges(_mesh.n_edges());
426 off <<
"Edges" << std::endl;
427 off << n_edges << std::endl;
430 for(
EdgeIter e_it = _mesh.e_iter(); e_it; ++e_it) {
432 VertexHandle from_vertex = _mesh.edge(*e_it).from_vertex();
434 off << from_vertex <<
" " << to_vertex << std::endl;
437 unsigned int n_faces(_mesh.n_faces());
438 off <<
"Faces" << std::endl;
439 off << n_faces << std::endl;
442 for(
FaceIter f_it = _mesh.f_iter(); f_it; ++f_it) {
444 off << _mesh.face(*f_it).halfedges().size() <<
" ";
446 std::vector<HalfEdgeHandle> halfedges = _mesh.face(*f_it).halfedges();
448 for(
typename std::vector<HalfEdgeHandle>::const_iterator it = halfedges.begin(); it
449 != halfedges.end(); ++it) {
453 if((it + 1) != halfedges.end())
460 unsigned int n_cells(_mesh.n_cells());
461 off <<
"Polyhedra" << std::endl;
462 off << n_cells << std::endl;
464 for(
CellIter c_it = _mesh.c_iter(); c_it; ++c_it) {
466 off << _mesh.cell(*c_it).halffaces().size() <<
" ";
468 std::vector<HalfFaceHandle> halffaces = _mesh.cell(*c_it).halffaces();
470 for(
typename std::vector<HalfFaceHandle>::const_iterator it = halffaces.begin(); it
471 != halffaces.end(); ++it) {
475 if((it + 1) != halffaces.end())
483 writeProps(off, _mesh.vertex_props_begin(), _mesh.vertex_props_end());
485 writeProps(off, _mesh.edge_props_begin(), _mesh.edge_props_end());
487 writeProps(off, _mesh.halfedge_props_begin(), _mesh.halfedge_props_end());
489 writeProps(off, _mesh.face_props_begin(), _mesh.face_props_end());
491 writeProps(off, _mesh.halfface_props_begin(), _mesh.halfface_props_end());
493 writeProps(off, _mesh.cell_props_begin(), _mesh.cell_props_end());
495 writeProps(off, _mesh.mesh_props_begin(), _mesh.mesh_props_end());
504 template<
class IteratorT>
505 void FileManager::writeProps(std::ostream& _ostr,
const IteratorT& _begin,
const IteratorT& _end)
const {
508 for(IteratorT p_it = _begin;
509 p_it != _end; ++p_it) {
510 if(!(*p_it)->persistent())
continue;
511 if((*p_it)->anonymous()) {
512 std::cerr <<
"Serialization of anonymous properties is not supported!" << std::endl;
516 _ostr << (*p_it)->entityType() <<
" ";
517 _ostr << (*p_it)->typeNameWrapper() <<
" ";
518 _ostr <<
"\"" << (*p_it)->name() <<
"\"" << std::endl;
520 (*p_it)->serialize(_ostr);
bool readFile(const std::string &_filename, MeshT &_mesh, bool _topologyCheck=true, bool _computeBottomUpIncidences=true) const
Read a mesh from a file.
Property classes for the different entity types.
bool writeFile(const std::string &_filename, const MeshT &_mesh) const
Write a mesh to a file.