Drawing Graphs
Graphs can be visualized by using either GraphMakie or rendering to .dot files (which can be viewed using xdot).
GraphMakie
GraphMakie plotting is available if GraphMakie is imported before DFG is imported. Install GraphMakie using the following command:
using Pkg
Pkg.add("GraphMakie")Then bring GraphMakie in before DFG:
using GraphMakie
using DistributedFactorGraphsAny factor graph can then be drawn by calling plotDFG:
using Cairo # hide
# Construct graph using IIF
using IncrementalInference
# Create graph
dfg = GraphsDFG{SolverParams}(solverParams=SolverParams())
v1 = addVariable!(dfg, :x0, ContinuousScalar, tags = [:POSE], solvable=1)
v2 = addVariable!(dfg, :x1, ContinuousScalar, tags = [:POSE], solvable=1)
v3 = addVariable!(dfg, :l0, ContinuousScalar, tags = [:LANDMARK], solvable=1)
prior = addFactor!(dfg, [:x0], Prior(Normal(0,1)))
f1 = addFactor!(dfg, [:x0; :x1], LinearRelative(Normal(50.0,2.0)), solvable=1)
f1 = addFactor!(dfg, [:l0; :x0], LinearRelative(Normal(40.0,5.0)), solvable=1)
f1 = addFactor!(dfg, [:l0; :x1], LinearRelative(Normal(-10.0,5.0)), solvable=1)
# Plot graph
plotDFG(dfg)Rendering GraphMakie to PDF
The graph can be rendered to PDF, SVG or JPG in the following way by including compose:
using Compose
# lets add another variable and factor and plot it
dfg.solverParams.graphinit = false # hide
addVariable!(dfg, :x2, ContinuousScalar);
addFactor!(dfg, [:x1; :x2], LinearRelative(Normal(50.0,2.0)));
# Save to SVG
draw(SVG("graph.svg", 10cm, 10cm), plotDFG(dfg));
nothing # hideFIXME show graph.svg
More Information
More information at GraphMakie.jl
Dot Files
Dot files are a standard format for visualizing graphs and applications such as xdot are available to view the files. Dot plotting does not require GraphMakie and can be drawn by either:
- Calling
DFG.toDoton any graph to produce a string of the graph - Calling
DFG.toDotFileon any graph to save it directly to a dotfile
using DistributedFactorGraphs
# Construct graph using IIF
using IncrementalInference
# Create graph
dfg = GraphsDFG{SolverParams}(solverParams=SolverParams())
v1 = addVariable!(dfg, :x0, ContinuousScalar, tags = [:POSE], solvable=1)
v2 = addVariable!(dfg, :x1, ContinuousScalar, tags = [:POSE], solvable=1)
v3 = addVariable!(dfg, :l0, ContinuousScalar, tags = [:LANDMARK], solvable=1)
prior = addFactor!(dfg, [:x0], Prior(Normal(0,1)))
f1 = addFactor!(dfg, [:x0; :x1], LinearRelative(Normal(50.0,2.0)), solvable=1)
f1 = addFactor!(dfg, [:l0; :x0], LinearRelative(Normal(40.0,5.0)), solvable=1)
f1 = addFactor!(dfg, [:l0; :x1], LinearRelative(Normal(-10.0,5.0)), solvable=1)
# Save to dot file
toDotFile(dfg, "/tmp/test.dot")
# Open with xdot
# run(`xdot /tmp/test.dot`)
# nothing # hide