BGL :获取带有数据的顶点描述符

BGL : get vertex descriptor with data

本文关键字:顶点 描述 数据 获取 BGL      更新时间:2023-10-16

我想用顶点的组成来获取顶点描述符,如下所示:

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);
}