ovm_converter.cc 2.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
#include <fstream>
#include <sstream>

#include <boost/spirit/include/qi.hpp>

#include <OpenVolumeMesh/FileManager/FileManager.hh>

#include "MeshGenerator.hpp"
#include "Grammars/Tetmesh.hpp"

int main(int _argc, char* _argv[]) {

13
    if(_argc < 3 || _argc > 4 ||
Mike Kremer's avatar
Mike Kremer committed
14
15
            (_argc > 1 && (std::strcmp(_argv[1], "--help") == 0 || std::strcmp(_argv[1], "-h") == 0))) {
        std::cerr << "You need to specify a source file to convert!" << std::endl << std::endl;
16
        std::clog << "Usage: file_converter <format> <filename> [output_filename]" << std::endl << std::endl;
Mike Kremer's avatar
Mike Kremer committed
17
18
19
        std::clog << "Available file formats:" << std::endl;
        std::clog << "  -t\tTetmesh" << std::endl;
        std::clog << std::endl;
20
21
22
        return -1;
    }

Mike Kremer's avatar
Mike Kremer committed
23
    std::ifstream iff(_argv[2], std::ios::in);
24
25
26
27
28
29
30
31
32
33
34
35
36
37

    if(!iff.good()) {
        std::cerr << "Could not open file " << _argv[1] << " for reading!" << std::endl;
        return -1;
    }

    MeshGenerator::PolyhedralMesh mesh;
    MeshGenerator generator(mesh);

    std::ostringstream oss;
    oss << iff.rdbuf();

    std::string fileContent = oss.str();

Mike Kremer's avatar
Mike Kremer committed
38
    // Instantiate grammar objects
39
40
    tetmesh_grammar<std::string::iterator> grammar(generator);

Mike Kremer's avatar
Mike Kremer committed
41
    bool r = false;
42

Mike Kremer's avatar
Mike Kremer committed
43
44
45
46
    if(std::strcmp(_argv[1], "-t") == 0) {
        // Tetmesh format
        r = boost::spirit::qi::phrase_parse(fileContent.begin(), fileContent.end(), grammar, qi::space);
    }
47
48

    if(r) {
Mike Kremer's avatar
Mike Kremer committed
49
        std::cout << "Successfully read file data!" << std::endl;
50
51
52
53
    } else {
        std::cout << "Parsing failed!" << std::endl;
    }

Mike Kremer's avatar
Mike Kremer committed
54
55
56
57
    std::cerr << "Converted " << mesh.n_vertices() << " vertices," << std::endl;
    std::cerr << "\t  " << mesh.n_edges() << " edges," << std::endl;
    std::cerr << "\t  " << mesh.n_faces() << " faces," << std::endl;
    std::cerr << "\t  " << mesh.n_cells() << " cells!" << std::endl;
58
59
60

    OpenVolumeMesh::IO::FileManager fileManager;

61
    std::string filename;
Mike Kremer's avatar
Mike Kremer committed
62

63
64
65
66
67
68
69
70
71
    if(_argc == 3) {
        filename = _argv[2];
        std::string::size_type idx = filename.rfind('.');

        filename = filename.substr(0, idx);
        filename.append(".ovm");
    } else {
        filename = _argv[3];
    }
Mike Kremer's avatar
Mike Kremer committed
72

73
    // Write mesh to file
74
    fileManager.writeFile(filename.c_str(), mesh);
75
76
77

    return 0;
}