boost - 执行布局算法后可视化提升图拓扑
问题描述
我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();
}
解决方案
您可以使用动态属性来添加 graphviz 属性,请参见示例:
https://www.boost.org/doc/libs/1_74_0/libs/graph/example/graphviz.cpp
在 graphviz 中指定位置的属性在这里:
节点或样条控制点的位置。
对于节点,位置表示节点的中心。输出时,坐标以点为单位。
在
neato
和fdp
中,pos 可用于设置节点的初始位置。默认情况下,坐标以英寸为单位。但是,-s
命令行标志可用于指定不同的单位。由于输出坐标是以点为单位的,因此将 Graphviz 程序布局的图形的输出馈送到neato
或fdp
几乎总是需要-s
标志。当
-n
命令行标志与 一起使用时neato
,假定位置已由布局程序之一设置,因此以点为单位。因此,neato -n
可以在不需要-s
标志的情况下正确接受输入,事实上,忽略任何这样的标志。适用于:边、节点。
推荐阅读
- javascript - 在节点 js 路由中使用外部函数
- java - 从实体内部调用 Hibernate Session.save() 是一种反模式吗?
- mysql - SQL 不拆分 sec_to_time 总和
- ios - ARKit 中的屏幕空间是什么意思?
- javascript - 到期日期更改时动态更新项目
- ssis - 既然 SSDT 现在支持 64 位,这是否意味着我们可以使用 64 位 Excel 驱动程序?
- django - 如何单次将带有数据的多个图像发布到 Django REST
- python - 你好世界烧瓶程序关闭然后关闭
- ionic-framework - ionic cordova run android --prod 挂起
- login - gcloud 令牌在 60 分钟后过期。任何解决方法?