Added 'NonlinearFactorGraph::saveGraph' for writing factor graphs in graphviz format.

release/4.3a0
Richard Roberts 2012-11-03 21:11:01 +00:00
parent 4512e4c85d
commit 999e2ba577
2 changed files with 27 additions and 0 deletions

View File

@ -42,6 +42,30 @@ void NonlinearFactorGraph::print(const std::string& str, const KeyFormatter& key
} }
} }
/* ************************************************************************* */
void NonlinearFactorGraph::saveGraph(std::ostream &stm, const KeyFormatter& keyFormatter) const {
stm << "graph {\n";
// Create nodes for each variable in the graph
BOOST_FOREACH(Key key, this->keys()) {
// Label the node with the label from the KeyFormatter
stm << " var" << key << "[label=\"" << keyFormatter(key) << "\"];\n"; }
stm << "\n";
// Create factors and variable connections
for(size_t i = 0; i < this->size(); ++i) {
// Make each factor a dot
stm << " factor" << i << "[label=\"\", shape=point];\n";
// Make factor-variable connections
if(this->at(i)) {
BOOST_FOREACH(Key key, *this->at(i)) {
stm << " var" << key << "--" << "factor" << i << ";\n"; } }
}
stm << "}\n";
}
/* ************************************************************************* */ /* ************************************************************************* */
double NonlinearFactorGraph::error(const Values& c) const { double NonlinearFactorGraph::error(const Values& c) const {
gttic(NonlinearFactorGraph_error); gttic(NonlinearFactorGraph_error);

View File

@ -46,6 +46,9 @@ namespace gtsam {
/** print just calls base class */ /** print just calls base class */
void print(const std::string& str = "NonlinearFactorGraph: ", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const; void print(const std::string& str = "NonlinearFactorGraph: ", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
/** Write the graph in GraphViz format for visualization */
void saveGraph(std::ostream& stm, const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
/** return keys as an ordered set - ordering is by key value */ /** return keys as an ordered set - ordering is by key value */
FastSet<Key> keys() const; FastSet<Key> keys() const;