save graph to graphviz format

release/4.3a0
Kai Ni 2010-02-13 07:09:56 +00:00
parent d0a93ad9dd
commit f9c2000847
3 changed files with 62 additions and 0 deletions

View File

@ -5,11 +5,16 @@
* Author: Frank Dellaert
*/
#include <iostream>
#include <fstream>
#include <boost/format.hpp>
#include <boost/foreach.hpp>
#include "Point2.h"
#include "Ordering.h"
#include "SymbolicFactorGraph.h"
#include "SymbolicBayesNet.h"
#include "inference-inl.h"
#include "LieConfig-inl.h"
using namespace std;
@ -38,5 +43,40 @@ namespace gtsam {
return bayesNet;
}
/* ************************************************************************* */
void saveGraph(const SymbolicFactorGraph& fg, const SymbolicConfig& config, const std::string& s) {
Symbol key;
Point2 pt;
float scale = 100;
ofstream of(s.c_str());
of << "graph G{" << endl;
of << "bgcolor=\"transparent\";" << endl;
BOOST_FOREACH(boost::tie(key, pt), config){
of << (string)key << "[label=\"" << (string)key << "\"][pos=\"" << pt.x()*scale << "," << pt.y()*scale << "\"];" << endl;
}
int index = 0;
BOOST_FOREACH(const SymbolicFactorGraph::sharedFactor& factor, fg) {
index++;
Point2 center;
BOOST_FOREACH(const Symbol& key, factor->keys())
center = center + config[key];
center = Point2(center.x() / factor->keys().size(), center.y() / factor->keys().size());
of << "f" << index << "[pos=\"" << center.x()*scale << "," << center.y()*scale << "\"][shape=\"point\"];" << endl;
BOOST_FOREACH(const Symbol& key, factor->keys())
of << "f" << index << "--" << (string)key << endl;
}
of<<"}";
of.close();
char filename[100];
sscanf(s.c_str(), "%s.dot", filename);
string cmd = boost::str(boost::format("neato -s -n -Tpdf %s -o %s.pdf") % s % filename);
system(cmd.c_str());
}
/* ************************************************************************* */
}

View File

@ -14,9 +14,14 @@
#include "SymbolicFactor.h"
#include "SymbolicBayesNet.h"
#include "Key.h"
#include "LieConfig.h"
namespace gtsam {
class Point2;
typedef LieConfig<Symbol, Point2> SymbolicConfig;
class SymbolicConditional;
/** Symbolic Factor Graph */
@ -78,6 +83,8 @@ namespace gtsam {
};
// save graph to the graphviz format
void saveGraph(const SymbolicFactorGraph& fg, const SymbolicConfig& config, const std::string& s);
}
#endif /* SYMBOLICFACTORGRAPH_H_ */

View File

@ -134,6 +134,21 @@ TEST( GaussianFactorGraph, eliminate )
CHECK(assert_equal(expected,actual));
}
/* ************************************************************************* */
TEST( GaussianFactorGraph, eliminate2 )
{
// create a test graph
SymbolicFactorGraph fg;
fg.push_factor("x1", "x2");
fg.eliminateOne("x1");
SymbolicFactorGraph expected;
expected.push_back(boost::shared_ptr<SymbolicFactor>());
expected.push_factor("x2");
CHECK(assert_equal(expected, fg));
}
/* ************************************************************************* */
TEST( SymbolicFactorGraph, constructFromBayesNet )
{