2014-01-15 30 views
0

我想與頂點的composant拿到頂點描述符,這樣的頂點描述:BGL:獲取數據

struct WayPoint{ 
std::pair<float, float> pos; // with this composant 
}; 

的adjency列表:

typedef boost::adjacency_list< 
    boost::listS,    
    boost::vecS,     
    boost::undirectedS,   
    WayPoint,     
    WayPointConnection   
> WayPointGraph; 
typedef WayPointGraph::vertex_descriptor WayPointID; 
typedef WayPointGraph::edge_descriptor WayPointConnectionID; 

我建我的圖表和創建所有的頂點/邊... ....目標是在圖上應用astar。

void PathFinding::findMeAPath(std::pair<float, float>begin, std::pair<float, float>end) 
{ 
    std::vector<WayPointID> p(boost::num_vertices(graphe)); 
    std::vector<float>  d(boost::num_vertices(graphe)); 
    WayPointID start = // I want to find the WayPointID with begin 
    WayPointID goal = //same with end; 
    shortest_path.clear(); 
    try { 
     boost::astar_search 
     (
     graphe, 
     start, 
     boost::astar_heuristic<WayPointGraph, float>(), 
     boost::predecessor_map(&p[0]).distance_map(&d[0]).visitor(astar_goal_visitor(goal)).weight_map(boost::get(&WayPointConnection::dist, graphe)) 
     ); 

    } catch(found_goal fg) { 

    for(WayPointID v = goal;; v = p[v]) { 
     shortest_path.push_front(v); 
     if(p[v] == v) 
      break; 
    } 
    } 
    } 
+0

首先,d應該使用num_edges,而不是num_vertices。其次,真正的問題是什麼? –

回答

2

您需要編寫一個函數來查找給定位置的頂點。您定義的圖形類型使用std :: vector來存儲頂點,因此函數將不得不迭代遍歷它並將查詢的位置與每個WayPoint進行比較。這樣的事情可以做:

std::pair<WayPointID, bool> find_vertex(const WayPoint& wp, const WayPointGraph& graph) 
{ 
    for (WayPointID id = 0; id < boost::num_vertices(graph); ++id) 
    { 
    if (equal(graph[id], wp)) 
     return std::make_pair(id, true); 
    } 
    return std::make_pair(0, false); 
} 

注意,函數返回一個對(編號+布爾標誌),以指示搜索成功與否,所以你可以按如下方式使用它:

bool vertex_found; 
WayPointID start; 
std::tie (start, vertex_found) = find_vertex(begin, graphe); 
if (!vertex_found) 
    // do something about it 

另外該函數使用以下來比較位置:

bool equal(const std::pair<float, float>& p1, const std::pair<float, float>& p2) 
{ 
    const float EPS = 1e-6; 
    return (std::fabs(p1.first - p2.first) < EPS && 
      std::fabs(p1.second - p2.second) < EPS); 
}