Abhishek Mahajan
Abhishek Mahajan

Reputation: 35

Visualize boost graph topology after layout algorithm is performed

I am using fruchterman_reingold_force_directed_layout algorithm on my graph to get a cluster free layout. Below is code for my vertices and edge

using RectTopology = boost::rectangle_topology<>;
using point = RectTopology::point_type;

class MyVertex{
public:
    MyVertex(){ myObject = NULL; }
    Mybject* myObject;
    point position;
    std::string name;
};

class MyEdge{
public:
    MyEdge(){ myLine = NULL; }
    MyLine* myLine;
    double weight;
};

//Boost graph defination
using graphT = boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, MyVertex, MyEdge>;
using vertexT = boost::graph_traits<graphT>::vertex_descriptor; //Define Vertex
using vertexIt = boost::graph_traits<graphT>::vertex_iterator; //Vertex Iterator
using edgeT = boost::graph_traits<graphT>::edge_descriptor; //Define Edge
using edgeIt = boost::graph_traits<graphT>::edge_iterator; //Edge Iterator


forcedDirLay(){
    boost::minstd_rand gen;
    RectTopology rect_top(gen, 0, 0, 1, 1);
    boost::random_graph_layout(myGraph, boost::get(&SLDVertex::position, myGraph), rect_top);
    boost::fruchterman_reingold_force_directed_layout(myGraph, boost::get(&SLDVertex::position, myGraph), rect_top);
}

Imagine now I have a graph and perform my layout algorithm which all works fine and I have position info for every vertices.

How can I visualize each vertex where they end up after layout algorithm finished ? Is there a way to get position info to Dot file and I can visualize Dot file ?

I have a function to convert my graph to dot file but don't know how to get position info to dot file. Thanks in advance.

GraphToDotFile(){
    std::ofstream dot(".\\graph.dot");
    boost::write_graphviz(dot, myGraph, 
        boost::make_label_writer(boost::get(&MyVertex::name, myGraph)));
    dot.close();
}

Upvotes: 1

Views: 175

Answers (2)

sroush
sroush

Reputation: 6773

The neato -n trick works like a champ.
Adding a bit to the above, yourProg produces graph.dot; then
neato -n -Tpng graph.dot >graph.png
You may have to fiddle with yourProg to make sure that graph.dot has all the required attributes, units are correct, etc. You can produce a tiny example by hand, then
dot -Tdot tiny.gv >tiny.dot and compare the two .dot files

Upvotes: 1

sehe
sehe

Reputation: 393064

You can use dynamic properties to add graphviz attributes, see the example:

https://www.boost.org/doc/libs/1_74_0/libs/graph/example/graphviz.cpp

The attributes that specify the position in graphviz are here:

enter image description here

Position of node, or spline control points.

For nodes, the position indicates the center of the node. On output, the coordinates are in points.

In neato and fdp, pos can be used to set the initial position of a node. By default, the coordinates are assumed to be in inches. However, the -s command line flag can be used to specify different units. As the output coordinates are in points, feeding the output of a graph laid out by a Graphviz program into neato or fdp will almost always require the -s flag.

When the -n command line flag is used with neato, it is assumed the positions have been set by one of the layout programs, and are therefore in points. Thus, neato -n can accept input correctly without requiring a -s flag and, in fact, ignores any such flag.

Valid for: Edges, Nodes.

Upvotes: 1

Related Questions