2016-08-20 38 views
2

我想创建一个应用程序,显示一个简单的图形,因为我使用boost :: graph作为底层数据结构,所以我想使用库中可用的布局算法。如何访问升压图拓扑布局中的坐标?

这里给出的回答解释了如何使用Boost库中的布局算法来布局图的顶点: How does the attractive force of Fruchterman Reingold work with Boost Graph Library

但遗憾的是它并没有解释如何 - 之后的布局已经计算 - 的坐标顶点实际上可以被访问。即使我们得到了一个位置向量(或者说点),float组件也是私有的,所以这没有帮助。 boost :: graph文档也没有解决这个问题。

那么在布局算法应用后,如何检索每个顶点的简单(X,Y)坐标?

回答

2

在回顾了boost图形源代码之后,事实证明这毕竟不是那么难。 我们可以用属性映射来遍历PositionsMap和[]操作符来访问坐标:

template<typename Graph, typename Positions> 
void print_positions(const Graph &g, const Positions &positions) { 
    auto index_map = boost::get(boost::vertex_index, graph); 

    using PropertyMap = boost::iterator_property_map<Positions::iterator, decltype(index_map)>; 
    PropertyMap position_map(positions.begin(), index_map); 
    BGL_FORALL_VERTICES(v, graph, Graph) { 
     Position pos = position_map[v]; 
     cout << v << ": " << pos[0] << "|" << pos[1] << endl; 
    } 
} 
+0

你好,你有这样的代码的一个完整的例子吗?因为我似乎无法复制此.. –

+0

@sdgawerzswer对不起,不再 - 也许你可以问你一个新的问题,我们可以找出问题所在。 –

+0

我写了一个自定义包装器,它只使用位置:基本上是一个pos [0]和pos [1]映射到个体节点的循环。无论如何,谢谢你。 –