Developer Documentation
ovm_converter.cc
1 #include <fstream>
2 #include <sstream>
3 
4 #include <boost/spirit/include/qi.hpp>
5 
6 #include <OpenVolumeMesh/FileManager/FileManager.hh>
7 
8 #include "MeshGenerator.hpp"
9 
10 #include "Grammars/Tetmesh.hpp"
11 #include "Grammars/Netgen.hpp"
12 
13 void printUsage() {
14  std::cerr << "You need to specify a format and a source file to convert!" << std::endl << std::endl;
15  std::clog << "Usage: file_converter <format> <filename> [output_filename.ovm]" << std::endl << std::endl;
16  std::clog << "Available file formats:" << std::endl;
17  std::clog << " -t\tTetmesh" << std::endl;
18  std::clog << " -n\tNetgen" << std::endl;
19  std::clog << std::endl;
20 }
21 
22 int main(int _argc, char* _argv[]) {
23 
24  if(_argc < 3 || _argc > 4 ||
25  (_argc > 1 && (std::strcmp(_argv[1], "--help") == 0 || std::strcmp(_argv[1], "-h") == 0))) {
26  printUsage();
27  return -1;
28  }
29 
30  std::ifstream iff(_argv[2], std::ios::in);
31 
32  if(!iff.good()) {
33  std::cerr << "Could not open file " << _argv[1] << " for reading!" << std::endl;
34  return -1;
35  }
36 
38  MeshGenerator generator(mesh);
39 
40  std::ostringstream oss;
41  oss << iff.rdbuf();
42 
43  std::string fileContent = oss.str();
44 
45  // Instantiate grammar objects
48 
49  bool r = false;
50 
51  if(std::strcmp(_argv[1], "-t") == 0) {
52 
53  // Tetmesh format
54  r = boost::spirit::qi::phrase_parse(fileContent.begin(), fileContent.end(), tetmesh_grammar, qi::space);
55 
56  } else if(std::strcmp(_argv[1], "-n") == 0) {
57 
58  // Netgen format
59  r = boost::spirit::qi::phrase_parse(fileContent.begin(), fileContent.end(), netgen_grammar, qi::space);
60 
61  } else {
62  printUsage();
63  return -1;
64  }
65 
66  if(r) {
67  std::cout << "Successfully read file data!" << std::endl;
68  } else {
69  std::cout << "Parsing failed!" << std::endl;
70  }
71 
72  std::cerr << "Converted " << mesh.n_vertices() << " vertices," << std::endl;
73  std::cerr << "\t " << mesh.n_edges() << " edges," << std::endl;
74  std::cerr << "\t " << mesh.n_faces() << " faces," << std::endl;
75  std::cerr << "\t " << mesh.n_cells() << " cells!" << std::endl;
76 
78 
79  std::string filename;
80 
81  if(_argc == 3) {
82  filename = _argv[2];
83  std::string::size_type idx = filename.rfind('.');
84 
85  filename = filename.substr(0, idx);
86  filename.append(".ovm");
87  } else {
88  filename = _argv[3];
89  }
90 
91  // Write mesh to file
92  fileManager.writeFile(filename.c_str(), mesh);
93 
94  return 0;
95 }
bool writeFile(const std::string &_filename, const MeshT &_mesh) const
Write a mesh to a file.
Read/Write mesh data from/to files.
Definition: FileManager.hh:60