Question

I'm working on a SFML / C++ project and I've some troubles with the boost graph library, in particular with the astar_search. I generated a Voronoi Diagram for a random map and a graph to use the astar method of the Boost Graph Library with the middle of each centers of the polygons

Establishment of the edges :

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

The edges are correctly established, when I want to draw them :

enter image description here

So I need to use the astar search with these edges but my code don't work :(

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

};

And the implementation :

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

The path is never found ... If you can help me about the astar implementation I'd appreciate it :)/ I'm sorry for the length of this post :(, the boost astar needs a lot of code implementation.

Thank you in advance

Was it helpful?

Solution

You insert too many vertices. You should keep, say, an unordred_map<Polygon*,vertex_descriptor>. Before calling add_vertex for a given polygon P you should first check whether P is already in the map. If yes, use the vertex_descriptor corresponding to P, do not call add_vertex. Otherwise, call v= add_vertex and add the pair (P,v) to the map. Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top