[英]BGL : get vertex descriptor with data
我想用頂點的合成器獲取頂點描述符,如下所示:
struct WayPoint{
std::pair<float, float> pos; // with this composant
};
附件清單:
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;
}
}
}
您需要編寫一個函數來查找給定位置的頂點。 您定義的圖形類型使用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);
}
請注意,該函數返回一對(Id +布爾標志)以指示搜索是否成功,因此您將按如下方式使用它:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.