From 640a3b82efe457682b79ece379300ceb93d77097 Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Wed, 19 Jan 2022 17:24:12 -0500 Subject: [PATCH] Use key formatter for dot --- .../tests/testDiscreteFactorGraph.cpp | 14 +++---- gtsam/inference/DotWriter.cpp | 23 ++++++----- gtsam/inference/DotWriter.h | 7 +--- gtsam/inference/FactorGraph-inst.h | 2 +- gtsam/nonlinear/NonlinearFactorGraph.cpp | 7 +++- tests/testNonlinearFactorGraph.cpp | 40 +++++++++---------- 6 files changed, 48 insertions(+), 45 deletions(-) diff --git a/gtsam/discrete/tests/testDiscreteFactorGraph.cpp b/gtsam/discrete/tests/testDiscreteFactorGraph.cpp index c591881f8..579244c57 100644 --- a/gtsam/discrete/tests/testDiscreteFactorGraph.cpp +++ b/gtsam/discrete/tests/testDiscreteFactorGraph.cpp @@ -401,16 +401,16 @@ TEST(DiscreteFactorGraph, DotWithNames) { "graph {\n" " size=\"5,5\";\n" "\n" - " var0[label=\"C\"];\n" - " var1[label=\"A\"];\n" - " var2[label=\"B\"];\n" + " varC[label=\"C\"];\n" + " varA[label=\"A\"];\n" + " varB[label=\"B\"];\n" "\n" " factor0[label=\"\", shape=point];\n" - " var0--factor0;\n" - " var1--factor0;\n" + " varC--factor0;\n" + " varA--factor0;\n" " factor1[label=\"\", shape=point];\n" - " var0--factor1;\n" - " var2--factor1;\n" + " varC--factor1;\n" + " varB--factor1;\n" "}\n"; EXPECT(actual == expected); } diff --git a/gtsam/inference/DotWriter.cpp b/gtsam/inference/DotWriter.cpp index fb3ea0505..18130c35d 100644 --- a/gtsam/inference/DotWriter.cpp +++ b/gtsam/inference/DotWriter.cpp @@ -35,7 +35,8 @@ void DotWriter::DrawVariable(Key key, const KeyFormatter& keyFormatter, const boost::optional& position, ostream* os) { // Label the node with the label from the KeyFormatter - *os << " var" << key << "[label=\"" << keyFormatter(key) << "\""; + *os << " var" << keyFormatter(key) << "[label=\"" << keyFormatter(key) + << "\""; if (position) { *os << ", pos=\"" << position->x() << "," << position->y() << "!\""; } @@ -51,22 +52,26 @@ void DotWriter::DrawFactor(size_t i, const boost::optional& position, *os << "];\n"; } -void DotWriter::ConnectVariables(Key key1, Key key2, ostream* os) { - *os << " var" << key1 << "--" - << "var" << key2 << ";\n"; +static void ConnectVariables(Key key1, Key key2, + const KeyFormatter& keyFormatter, + ostream* os) { + *os << " var" << keyFormatter(key1) << "--" + << "var" << keyFormatter(key2) << ";\n"; } -void DotWriter::ConnectVariableFactor(Key key, size_t i, ostream* os) { - *os << " var" << key << "--" +static void ConnectVariableFactor(Key key, const KeyFormatter& keyFormatter, + size_t i, ostream* os) { + *os << " var" << keyFormatter(key) << "--" << "factor" << i << ";\n"; } void DotWriter::processFactor(size_t i, const KeyVector& keys, + const KeyFormatter& keyFormatter, const boost::optional& position, ostream* os) const { if (plotFactorPoints) { if (binaryEdges && keys.size() == 2) { - ConnectVariables(keys[0], keys[1], os); + ConnectVariables(keys[0], keys[1], keyFormatter, os); } else { // Create dot for the factor. DrawFactor(i, position, os); @@ -74,7 +79,7 @@ void DotWriter::processFactor(size_t i, const KeyVector& keys, // Make factor-variable connections if (connectKeysToFactor) { 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 key2 : keys) { if (key2 > key1) { - ConnectVariables(key1, key2, os); + ConnectVariables(key1, key2, keyFormatter, os); } } } diff --git a/gtsam/inference/DotWriter.h b/gtsam/inference/DotWriter.h index a606d67df..93c229c2b 100644 --- a/gtsam/inference/DotWriter.h +++ b/gtsam/inference/DotWriter.h @@ -57,14 +57,9 @@ struct GTSAM_EXPORT DotWriter { static void DrawFactor(size_t i, const boost::optional& position, 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. void processFactor(size_t i, const KeyVector& keys, + const KeyFormatter& keyFormatter, const boost::optional& position, std::ostream* os) const; }; diff --git a/gtsam/inference/FactorGraph-inst.h b/gtsam/inference/FactorGraph-inst.h index 058075f2d..3ea17fc7f 100644 --- a/gtsam/inference/FactorGraph-inst.h +++ b/gtsam/inference/FactorGraph-inst.h @@ -144,7 +144,7 @@ void FactorGraph::dot(std::ostream& os, const auto& factor = at(i); if (factor) { const KeyVector& factorKeys = factor->keys(); - writer.processFactor(i, factorKeys, boost::none, &os); + writer.processFactor(i, factorKeys, keyFormatter, boost::none, &os); } } diff --git a/gtsam/nonlinear/NonlinearFactorGraph.cpp b/gtsam/nonlinear/NonlinearFactorGraph.cpp index 89236ea87..da8935d5f 100644 --- a/gtsam/nonlinear/NonlinearFactorGraph.cpp +++ b/gtsam/nonlinear/NonlinearFactorGraph.cpp @@ -33,8 +33,10 @@ # include #endif +#include #include #include +#include using namespace std; @@ -127,7 +129,7 @@ void NonlinearFactorGraph::dot(std::ostream& os, const Values& values, // Create factors and variable connections size_t i = 0; for (const KeyVector& factorKeys : structure) { - writer.processFactor(i++, factorKeys, boost::none, &os); + writer.processFactor(i++, factorKeys, keyFormatter, boost::none, &os); } } else { // 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); if (factor) { const KeyVector& factorKeys = factor->keys(); - writer.processFactor(i, factorKeys, writer.factorPos(min, i), &os); + writer.processFactor(i, factorKeys, keyFormatter, + writer.factorPos(min, i), &os); } } } diff --git a/tests/testNonlinearFactorGraph.cpp b/tests/testNonlinearFactorGraph.cpp index e1a88d616..05a6e7f45 100644 --- a/tests/testNonlinearFactorGraph.cpp +++ b/tests/testNonlinearFactorGraph.cpp @@ -335,21 +335,21 @@ TEST(NonlinearFactorGraph, dot) { "graph {\n" " size=\"5,5\";\n" "\n" - " var7782220156096217089[label=\"l1\"];\n" - " var8646911284551352321[label=\"x1\"];\n" - " var8646911284551352322[label=\"x2\"];\n" + " varl1[label=\"l1\"];\n" + " varx1[label=\"x1\"];\n" + " varx2[label=\"x2\"];\n" "\n" " factor0[label=\"\", shape=point];\n" - " var8646911284551352321--factor0;\n" + " varx1--factor0;\n" " factor1[label=\"\", shape=point];\n" - " var8646911284551352321--factor1;\n" - " var8646911284551352322--factor1;\n" + " varx1--factor1;\n" + " varx2--factor1;\n" " factor2[label=\"\", shape=point];\n" - " var8646911284551352321--factor2;\n" - " var7782220156096217089--factor2;\n" + " varx1--factor2;\n" + " varl1--factor2;\n" " factor3[label=\"\", shape=point];\n" - " var8646911284551352322--factor3;\n" - " var7782220156096217089--factor3;\n" + " varx2--factor3;\n" + " varl1--factor3;\n" "}\n"; const NonlinearFactorGraph fg = createNonlinearFactorGraph(); @@ -363,21 +363,21 @@ TEST(NonlinearFactorGraph, dot_extra) { "graph {\n" " size=\"5,5\";\n" "\n" - " var7782220156096217089[label=\"l1\", pos=\"0,0!\"];\n" - " var8646911284551352321[label=\"x1\", pos=\"1,0!\"];\n" - " var8646911284551352322[label=\"x2\", pos=\"1,1.5!\"];\n" + " varl1[label=\"l1\", pos=\"0,0!\"];\n" + " varx1[label=\"x1\", pos=\"1,0!\"];\n" + " varx2[label=\"x2\", pos=\"1,1.5!\"];\n" "\n" " factor0[label=\"\", shape=point];\n" - " var8646911284551352321--factor0;\n" + " varx1--factor0;\n" " factor1[label=\"\", shape=point];\n" - " var8646911284551352321--factor1;\n" - " var8646911284551352322--factor1;\n" + " varx1--factor1;\n" + " varx2--factor1;\n" " factor2[label=\"\", shape=point];\n" - " var8646911284551352321--factor2;\n" - " var7782220156096217089--factor2;\n" + " varx1--factor2;\n" + " varl1--factor2;\n" " factor3[label=\"\", shape=point];\n" - " var8646911284551352322--factor3;\n" - " var7782220156096217089--factor3;\n" + " varx2--factor3;\n" + " varl1--factor3;\n" "}\n"; const NonlinearFactorGraph fg = createNonlinearFactorGraph();