执行布局算法后可视化升压图拓扑

Visualize boost graph topology after layout algorithm is performed

我在图表上使用 fruchterman_reingold_force_directed_layout 算法 获得无簇布局。下面是我的顶点和边的代码

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);
}

想象一下,现在我有一个图表并执行我的布局算法,一切正常 我有每个顶点的位置信息。

如何可视化每个顶点在布局算法后的最终位置 完成的 ?有没有办法将位置信息获取到 Dot 文件,我可以想象 点文件 ?

我有一个函数可以将我的图形转换为点文件,但不知道如何 获取要点的位置信息 文件。提前致谢。

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

可以使用动态属性添加graphviz属性,看例子:

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

在graphviz中指定位置的属性是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.

neato -n 技巧非常有效。
在上面补充一点, yourProg 产生 graph.dot;然后
neato -n -Tpng graph.dot >graph.png
您可能需要 fiddle 和 yourProg 以确保 graph.dot 具有所有必需的属性,单位正确,等你可以手工制作一个小例子,然后
点-T点tiny.gv>tiny.dot 并比较两个 .dot 文件