【问题标题】:How do you access the coordinates in a boost graph topology layout?如何访问升压图拓扑布局中的坐标?
【发布时间】:2016-08-20 19:13:53
【问题描述】:

我正在尝试创建一个显示简单图形的应用程序,并且由于我将 boost::graph 用于底层数据结构,因此我也想使用库中提供的布局算法。

此处提供的答案解释了如何使用 boost 库中的布局算法来布局图形的顶点: How does the attractive force of Fruchterman Reingold work with Boost Graph Library

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

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

【问题讨论】:

    标签: c++ boost graph


    【解决方案1】:

    在查看了 boost graph 源代码后,发现这毕竟不是那么难。 我们可以使用属性映射来遍历 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;
        }
    }
    

    【讨论】:

    • 您好,您有此代码的完整示例吗?因为我似乎无法复制这个..
    • @sdgawerzswer 抱歉,不再是 - 也许您可以针对您的问题提出一个新问题,我们可以找出问题所在。
    • 我同时编写了一个自定义包装器,它只使用位置:基本上是一个循环,其中 pos[0] 和 pos[1] 映射到单个节点。还是谢谢你。
    猜你喜欢
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    相关资源
    最近更新 更多