Use key formatter for dot

release/4.3a0
Frank Dellaert 2022-01-19 17:24:12 -05:00
parent a1f5ae0a89
commit 640a3b82ef
6 changed files with 48 additions and 45 deletions

View File

@ -401,16 +401,16 @@ TEST(DiscreteFactorGraph, DotWithNames) {
"graph {\n" "graph {\n"
" size=\"5,5\";\n" " size=\"5,5\";\n"
"\n" "\n"
" var0[label=\"C\"];\n" " varC[label=\"C\"];\n"
" var1[label=\"A\"];\n" " varA[label=\"A\"];\n"
" var2[label=\"B\"];\n" " varB[label=\"B\"];\n"
"\n" "\n"
" factor0[label=\"\", shape=point];\n" " factor0[label=\"\", shape=point];\n"
" var0--factor0;\n" " varC--factor0;\n"
" var1--factor0;\n" " varA--factor0;\n"
" factor1[label=\"\", shape=point];\n" " factor1[label=\"\", shape=point];\n"
" var0--factor1;\n" " varC--factor1;\n"
" var2--factor1;\n" " varB--factor1;\n"
"}\n"; "}\n";
EXPECT(actual == expected); EXPECT(actual == expected);
} }

View File

@ -35,7 +35,8 @@ void DotWriter::DrawVariable(Key key, const KeyFormatter& keyFormatter,
const boost::optional<Vector2>& position, const boost::optional<Vector2>& position,
ostream* os) { ostream* os) {
// Label the node with the label from the KeyFormatter // Label the node with the label from the KeyFormatter
*os << " var" << key << "[label=\"" << keyFormatter(key) << "\""; *os << " var" << keyFormatter(key) << "[label=\"" << keyFormatter(key)
<< "\"";
if (position) { if (position) {
*os << ", pos=\"" << position->x() << "," << position->y() << "!\""; *os << ", pos=\"" << position->x() << "," << position->y() << "!\"";
} }
@ -51,22 +52,26 @@ void DotWriter::DrawFactor(size_t i, const boost::optional<Vector2>& position,
*os << "];\n"; *os << "];\n";
} }
void DotWriter::ConnectVariables(Key key1, Key key2, ostream* os) { static void ConnectVariables(Key key1, Key key2,
*os << " var" << key1 << "--" const KeyFormatter& keyFormatter,
<< "var" << key2 << ";\n"; ostream* os) {
*os << " var" << keyFormatter(key1) << "--"
<< "var" << keyFormatter(key2) << ";\n";
} }
void DotWriter::ConnectVariableFactor(Key key, size_t i, ostream* os) { static void ConnectVariableFactor(Key key, const KeyFormatter& keyFormatter,
*os << " var" << key << "--" size_t i, ostream* os) {
*os << " var" << keyFormatter(key) << "--"
<< "factor" << i << ";\n"; << "factor" << i << ";\n";
} }
void DotWriter::processFactor(size_t i, const KeyVector& keys, void DotWriter::processFactor(size_t i, const KeyVector& keys,
const KeyFormatter& keyFormatter,
const boost::optional<Vector2>& position, const boost::optional<Vector2>& position,
ostream* os) const { ostream* os) const {
if (plotFactorPoints) { if (plotFactorPoints) {
if (binaryEdges && keys.size() == 2) { if (binaryEdges && keys.size() == 2) {
ConnectVariables(keys[0], keys[1], os); ConnectVariables(keys[0], keys[1], keyFormatter, os);
} else { } else {
// Create dot for the factor. // Create dot for the factor.
DrawFactor(i, position, os); DrawFactor(i, position, os);
@ -74,7 +79,7 @@ void DotWriter::processFactor(size_t i, const KeyVector& keys,
// Make factor-variable connections // Make factor-variable connections
if (connectKeysToFactor) { if (connectKeysToFactor) {
for (Key key : keys) { for (Key key : keys) {
ConnectVariableFactor(key, i, os); ConnectVariableFactor(key, keyFormatter, i, os);
} }
} }
} }
@ -83,7 +88,7 @@ void DotWriter::processFactor(size_t i, const KeyVector& keys,
for (Key key1 : keys) { for (Key key1 : keys) {
for (Key key2 : keys) { for (Key key2 : keys) {
if (key2 > key1) { if (key2 > key1) {
ConnectVariables(key1, key2, os); ConnectVariables(key1, key2, keyFormatter, os);
} }
} }
} }

View File

@ -57,14 +57,9 @@ struct GTSAM_EXPORT DotWriter {
static void DrawFactor(size_t i, const boost::optional<Vector2>& position, static void DrawFactor(size_t i, const boost::optional<Vector2>& position,
std::ostream* os); std::ostream* os);
/// Connect two variables.
static void ConnectVariables(Key key1, Key key2, std::ostream* os);
/// Connect variable and factor.
static void ConnectVariableFactor(Key key, size_t i, std::ostream* os);
/// Draw a single factor, specified by its index i and its variable keys. /// Draw a single factor, specified by its index i and its variable keys.
void processFactor(size_t i, const KeyVector& keys, void processFactor(size_t i, const KeyVector& keys,
const KeyFormatter& keyFormatter,
const boost::optional<Vector2>& position, const boost::optional<Vector2>& position,
std::ostream* os) const; std::ostream* os) const;
}; };

View File

@ -144,7 +144,7 @@ void FactorGraph<FACTOR>::dot(std::ostream& os,
const auto& factor = at(i); const auto& factor = at(i);
if (factor) { if (factor) {
const KeyVector& factorKeys = factor->keys(); const KeyVector& factorKeys = factor->keys();
writer.processFactor(i, factorKeys, boost::none, &os); writer.processFactor(i, factorKeys, keyFormatter, boost::none, &os);
} }
} }

View File

@ -33,8 +33,10 @@
# include <tbb/parallel_for.h> # include <tbb/parallel_for.h>
#endif #endif
#include <algorithm>
#include <cmath> #include <cmath>
#include <fstream> #include <fstream>
#include <set>
using namespace std; using namespace std;
@ -127,7 +129,7 @@ void NonlinearFactorGraph::dot(std::ostream& os, const Values& values,
// Create factors and variable connections // Create factors and variable connections
size_t i = 0; size_t i = 0;
for (const KeyVector& factorKeys : structure) { for (const KeyVector& factorKeys : structure) {
writer.processFactor(i++, factorKeys, boost::none, &os); writer.processFactor(i++, factorKeys, keyFormatter, boost::none, &os);
} }
} else { } else {
// Create factors and variable connections // Create factors and variable connections
@ -135,7 +137,8 @@ void NonlinearFactorGraph::dot(std::ostream& os, const Values& values,
const NonlinearFactor::shared_ptr& factor = at(i); const NonlinearFactor::shared_ptr& factor = at(i);
if (factor) { if (factor) {
const KeyVector& factorKeys = factor->keys(); const KeyVector& factorKeys = factor->keys();
writer.processFactor(i, factorKeys, writer.factorPos(min, i), &os); writer.processFactor(i, factorKeys, keyFormatter,
writer.factorPos(min, i), &os);
} }
} }
} }

View File

@ -335,21 +335,21 @@ TEST(NonlinearFactorGraph, dot) {
"graph {\n" "graph {\n"
" size=\"5,5\";\n" " size=\"5,5\";\n"
"\n" "\n"
" var7782220156096217089[label=\"l1\"];\n" " varl1[label=\"l1\"];\n"
" var8646911284551352321[label=\"x1\"];\n" " varx1[label=\"x1\"];\n"
" var8646911284551352322[label=\"x2\"];\n" " varx2[label=\"x2\"];\n"
"\n" "\n"
" factor0[label=\"\", shape=point];\n" " factor0[label=\"\", shape=point];\n"
" var8646911284551352321--factor0;\n" " varx1--factor0;\n"
" factor1[label=\"\", shape=point];\n" " factor1[label=\"\", shape=point];\n"
" var8646911284551352321--factor1;\n" " varx1--factor1;\n"
" var8646911284551352322--factor1;\n" " varx2--factor1;\n"
" factor2[label=\"\", shape=point];\n" " factor2[label=\"\", shape=point];\n"
" var8646911284551352321--factor2;\n" " varx1--factor2;\n"
" var7782220156096217089--factor2;\n" " varl1--factor2;\n"
" factor3[label=\"\", shape=point];\n" " factor3[label=\"\", shape=point];\n"
" var8646911284551352322--factor3;\n" " varx2--factor3;\n"
" var7782220156096217089--factor3;\n" " varl1--factor3;\n"
"}\n"; "}\n";
const NonlinearFactorGraph fg = createNonlinearFactorGraph(); const NonlinearFactorGraph fg = createNonlinearFactorGraph();
@ -363,21 +363,21 @@ TEST(NonlinearFactorGraph, dot_extra) {
"graph {\n" "graph {\n"
" size=\"5,5\";\n" " size=\"5,5\";\n"
"\n" "\n"
" var7782220156096217089[label=\"l1\", pos=\"0,0!\"];\n" " varl1[label=\"l1\", pos=\"0,0!\"];\n"
" var8646911284551352321[label=\"x1\", pos=\"1,0!\"];\n" " varx1[label=\"x1\", pos=\"1,0!\"];\n"
" var8646911284551352322[label=\"x2\", pos=\"1,1.5!\"];\n" " varx2[label=\"x2\", pos=\"1,1.5!\"];\n"
"\n" "\n"
" factor0[label=\"\", shape=point];\n" " factor0[label=\"\", shape=point];\n"
" var8646911284551352321--factor0;\n" " varx1--factor0;\n"
" factor1[label=\"\", shape=point];\n" " factor1[label=\"\", shape=point];\n"
" var8646911284551352321--factor1;\n" " varx1--factor1;\n"
" var8646911284551352322--factor1;\n" " varx2--factor1;\n"
" factor2[label=\"\", shape=point];\n" " factor2[label=\"\", shape=point];\n"
" var8646911284551352321--factor2;\n" " varx1--factor2;\n"
" var7782220156096217089--factor2;\n" " varl1--factor2;\n"
" factor3[label=\"\", shape=point];\n" " factor3[label=\"\", shape=point];\n"
" var8646911284551352322--factor3;\n" " varx2--factor3;\n"
" var7782220156096217089--factor3;\n" " varl1--factor3;\n"
"}\n"; "}\n";
const NonlinearFactorGraph fg = createNonlinearFactorGraph(); const NonlinearFactorGraph fg = createNonlinearFactorGraph();