2016-08-20 25 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]映射到個體節點的循環。無論如何,謝謝你。 –