Cleaned up example_imagesegmentation
[libdai.git] / utils / fg2dot.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2010 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 #include <iostream>
13 #include <fstream>
14 #include <cstdlib>
15 #include <string>
16 #include <dai/factorgraph.h>
17
18
19 using namespace dai;
20 using namespace std;
21
22
23 int main( int argc, char *argv[] ) {
24 if( argc != 3 ) {
25 // Display help message if number of command line arguments is incorrect
26 cout << "This program is part of libDAI - http://www.libdai.org/" << endl << endl;
27 cout << "Usage: ./fg2dot <in.fg> <out.dot>" << endl << endl;
28 cout << "Converts a libDAI factor graph file to a GraphViz .dot file for visualization." << endl;
29 cout << "The .dot file can be converted to .ps (PostScript) by" << endl;
30 cout << "'neato -T ps out.dot > out.ps' or by 'dot -T ps out.dot > out.ps'" << endl << endl;
31 return 1;
32 } else {
33 // Read factorgraph
34 FactorGraph fg;
35 char *infile = argv[1];
36 fg.ReadFromFile( infile );
37
38 // Open output file for writing (except if filename equals "-")
39 ostream *os = &cout;
40 ofstream outfile;
41 if( string( argv[2] ) != "-" ) {
42 outfile.open( argv[2] );
43 if( !outfile.is_open() ) {
44 cerr << "Cannot open " << argv[2] << " for writing" << endl;
45 return 1;
46 }
47 os = &outfile;
48 } // else, write to cout
49
50 // Write the .dot file
51 fg.printDot( *os );
52
53 return 0;
54 }
55 }