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