问题
Before you start reading, to help you understand my issue, I am telling that I have copied the code from this link: Dijkstra Shortest Path with VertexList = ListS in boost graph
So.. I am rewriting my program code to use boost, but now when 99% is ready I am stuck with my GPS (for a game). I have a list of nodes, which I added in a way which fortunately was easy to convert to the boost method. The thing I needed to do was just create a vertice variable like this:
Vertex Vx[MAX_NODES];
I copied the typedefs from the link I have given.
The way I add vertices is this:
stringstream s;
s << i;
Vx[i] = add_vertex(s.str(),dgraph);
Where "i" equals an integer number. (eg int i = 9)
And eges are also easy to add. Now, I have my own structured array called "xNode". and eg : xNode[i] holds all the information for X Y Z positions (xNode[i].X xNode[i].Y etc) of the nodes.
Now when using the code snippet from the link I have done this:
// Write shortest path
std::cout << "Shortest path from " << startid << " to " << endid << ":" << std::endl;
float totalDistance = 0;
for(PathType::reverse_iterator pathIterator = path.rbegin(); pathIterator != path.rend(); ++pathIterator)
{
std::cout << source(*pathIterator, dgraph) << " -> " << target(*pathIterator, dgraph)
<< " = " << get( boost::edge_weight, dgraph, *pathIterator ) << std::endl;
}
And this is where I am stuck, as "source(*pathIterator, dgraph)" and "target(*pathIterator, dgraph) " Get addresses, but I need the vertice indexes to access xNode[i], i is the NodeID (or well the vertice ID | Vx[i]). How can I do that?
EDIT: I tried to do:
for(PathType::reverse_iterator pathIterator = path.rbegin(); pathIterator != path.rend(); ++pathIterator)
{
for(int i = 0; i < MAX_NODES; ++i)
{
if(source(*pathIterator, dgraph) == *((Vertex*)Vx[i]))
{
cout << " " << i << " " << endl;
break;
}
}
}
but this just crashes..
回答1:
With the typedef
s from that question, you can use get(boost::vertex_index, dgraph, v)
to get the index of v
. You can also cache the property map using:
IndexMap vi = get(boost::vertex_index, dgraph);
then use get(vi, v)
to get the index for v
.
来源:https://stackoverflow.com/questions/10109862/boost-dijkstra-shortest-path-how-to-get-vertice-index-from-path-iterator