首页 > 解决方案 > 执行布局算法后可视化提升图拓扑

问题描述

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 文件并且我可以可视化 Dot 文件?

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

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

标签: boostlayoutboost-graphtopology

解决方案


您可以使用动态属性来添加 graphviz 属性,请参见示例:

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

在 graphviz 中指定位置的属性在这里

在此处输入图像描述

节点或样条控制点的位置。

对于节点,位置表示节点的中心。输出时,坐标以点为单位。

neatofdp中,pos 可用于设置节点的初始位置。默认情况下,坐标以英寸为单位。但是,-s命令行标志可用于指定不同的单位。由于输出坐标是以点为单位的,因此将 Graphviz 程序布局的图形的输出馈送到neatofdp几乎总是需要-s标志。

-n命令行标志与 一起使用时neato,假定位置已由布局程序之一设置,因此以点为单位。因此,neato -n可以在不需要-s标志的情况下正确接受输入,事实上,忽略任何这样的标志。

适用于:边、节点。


推荐阅读