C++ BGL:使用数据获取顶点描述符
我想用顶点的成分得到顶点描述符,如下所示:C++ BGL:使用数据获取顶点描述符,c++,boost,vertex,boost-graph,C++,Boost,Vertex,Boost Graph,我想用顶点的成分得到顶点描述符,如下所示: struct WayPoint{ std::pair<float, float> pos; // with this composant }; struct航路点{ std::pair pos;//使用此组件 }; 邻接列表: typedef boost::adjacency_list< boost::listS, boost::vecS, b
struct WayPoint{
std::pair<float, float> pos; // with this composant
};
struct航路点{
std::pair pos;//使用此组件
};
邻接列表:
typedef boost::adjacency_list<
boost::listS,
boost::vecS,
boost::undirectedS,
WayPoint,
WayPointConnection
> WayPointGraph;
typedef WayPointGraph::vertex_descriptor WayPointID;
typedef WayPointGraph::edge_descriptor WayPointConnectionID;
typedef boost::邻接列表<
boost::列表,
boost::vecS,
boost::无向,
航路点,
航路点连接
>航路点图;
typedef WayPointGraph::顶点描述符WayPointID;
typedef WayPointGraph::edge_描述符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;
}
}
}
void寻路::findMeAPath(std::pairbegin,std::pairend)
{
std::vector p(boost::num_顶点(graph));
std::vector d(boost::num_顶点(图形));
WayPointID start=//我想用begin查找WayPointID
WayPointID目标=//与end相同;
最短路径。清除();
试一试{
boost::astar\u搜索
(
图表,
开始
boost::astar_启发式(),
boost::前置图(&p[0])。距离图(&d[0])。访客(目标)。权重图(boost::get(&WayPointConnection::距离,图形))
);
}捕获(发现){
对于(航路点ID v=目标;v=p[v]){
最短路径。向前推(v);
如果(p[v]==v)
打破
}
}
}
您需要编写一个函数来查找给定位置的顶点。您定义的图形类型使用std::vector存储顶点,因此函数必须遍历它,并将查询的位置与每个航路点进行比较。这样做可以:
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 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);
}
bool相等(常数std::pair&p1,常数std::pair&p2)
{
常量浮点数EPS=1e-6;
返回(标准::晶圆厂(p1.first-p2.first)
首先,d应该使用num_边,而不是num_顶点。第二,实际问题是什么?
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);
}