c++ - 使用边缘容器 boost astar_search

标签 c++ boost graph a-star

我正在处理一个 SFML/C++ 项目,我在使用 boost 图形库时遇到了一些问题,尤其是 astar_search。我为随机 map 生成了一个 Voronoi 图,并使用 Boost Graph Library 的 astar 方法在多边形的每个中心的中间生成了一个图形

边的建立:

for (Polygon *u : this->_map->_polygons)
{
    if (u->getPolygonType() == u->GROUND)
    {
        WayPointID wpID = boost::add_vertex(graphe); 
        graphe[wpID].pos = u->getCenter();
        for (std::deque<Edge_ *>::iterator it = u->getEdges().begin() ; it != u->getEdges().end() ; ++it)
        {
            std::pair<Polygon *, Polygon *> t = (*it)->_polygonsOwn;
            WayPointID wpID2 = boost::add_vertex(graphe);

            graphe[wpID2].pos = t.second->getCenter();
            if (t.first->getPolygonType() == t.first->GROUND)
            {
                float dx = abs(graphe[wpID].pos.first - graphe[wpID2].pos.first);
                float dy = abs(graphe[wpID].pos.second - graphe[wpID2].pos.second);

                boost::add_edge(wpID, wpID2, WayPointConnection(sqrt(dx * dx + dy * dy)), graphe);              
            }

当我想绘制它们时,边缘已正确建立:

enter image description here

所以我需要对这些边使用 astar 搜索,但我的代码不起作用 :(

struct found_goal {}; 
class astar_goal_visitor : public boost::default_astar_visitor{
private:
typedef boost::adjacency_list<  
    boost::listS,              
    boost::vecS,                
    boost::undirectedS,         
    WayPoint,                   
    WayPointConnection          
> WayPointGraph;
typedef WayPointGraph::vertex_descriptor WayPointID;
typedef WayPointGraph::edge_descriptor   WayPointConnectionID;
WayPointGraph graphe;
WayPointID m_goal;

public:
    astar_goal_visitor(WayPointID goal) : m_goal(goal) {}

void examine_vertex(WayPointID u, const WayPointGraph &amp){ 
    if(u == m_goal)
        throw found_goal(); 
    }

};

和实现:

boost::mt19937 gen(time(0));

std::vector<WayPointID> p(boost::num_vertices(graphe)); 
std::vector<float>      d(boost::num_vertices(graphe)); 
WayPointID start = boost::random_vertex(graphe, gen);
WayPointID goal = boost::random_vertex(graphe, gen);

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) { 
    std::cout << "is ok" << std::endl; 
}

从未找到路径...如果您能帮助我了解 astar 实现,我将不胜感激 :)/ 对于这篇文章的长度,我很抱歉:(,boost astar 需要大量代码实现。

提前致谢

最佳答案

您插入了太多顶点。你应该保留一个 unordred_map<Polygon*,vertex_descriptor> .在为给定的多边形 P 调用 add_vertex 之前,您应该首先检查 P 是否已经在 map 中。如果是,则使用P对应的vertex_descriptor,不要调用add_vertex。否则,调用 v= add_vertex 并将对 (P,v) 添加到 map 中。 祝你好运!

关于c++ - 使用边缘容器 boost astar_search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19779801/

相关文章:

c++ - 在 Ubuntu 中学习 OpenGL

c++ - 为什么不是所有的 cpp 文件都重新编译?

c++ - boost::program_options - 验证失败时在错误消息中显示用户输入的值

c++ - 与 boost::property_tree XML 解析器一起使用时 boost::coroutine 库崩溃

algorithm - 归纳图中的顶点和 - 动态规划

c++ - std::begin - 类型特征中不考虑用户定义的重载

c++ - 为什么迭代器不依赖于分配器? (也就是说,让迭代器变得可怕不会违反分配器的 typedef 抽象吗?)

c++ - Boost::geometry 查询返回索引

java - 使用 xyz 坐标和 jzy3d 构建 3d 曲面图

algorithm - 删除有向图中的重复边