2012-06-15 12:47:27 -04:00
|
|
|
/*
|
|
|
|
|
2013-10-14 07:42:28 -04:00
|
|
|
Copyright (c) 2013, Project OSRM, Dennis Luxen, others
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
2012-06-15 12:47:27 -04:00
|
|
|
|
|
|
|
#ifndef BASICROUTINGINTERFACE_H_
|
|
|
|
#define BASICROUTINGINTERFACE_H_
|
|
|
|
|
2013-08-14 07:41:23 -04:00
|
|
|
#include "../DataStructures/RawRouteData.h"
|
2013-09-19 12:53:34 -04:00
|
|
|
#include "../DataStructures/SearchEngineData.h"
|
2013-06-24 14:12:34 -04:00
|
|
|
#include "../Util/ContainerUtils.h"
|
2013-08-08 08:17:01 -04:00
|
|
|
#include "../Util/SimpleLogger.h"
|
2013-06-24 14:12:34 -04:00
|
|
|
|
2013-02-03 10:47:32 -05:00
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
|
2012-06-15 12:47:27 -04:00
|
|
|
#include <cassert>
|
|
|
|
#include <climits>
|
|
|
|
|
2013-06-24 14:12:34 -04:00
|
|
|
#include <stack>
|
2012-06-19 11:26:34 -04:00
|
|
|
|
2013-09-19 12:53:34 -04:00
|
|
|
template<class SearchEngineDataT>
|
2013-02-03 10:47:32 -05:00
|
|
|
class BasicRoutingInterface : boost::noncopyable{
|
2012-06-15 12:47:27 -04:00
|
|
|
protected:
|
2013-09-19 12:53:34 -04:00
|
|
|
SearchEngineDataT * engine_working_data;
|
2012-06-15 12:47:27 -04:00
|
|
|
public:
|
2013-09-19 12:53:34 -04:00
|
|
|
BasicRoutingInterface(SearchEngineDataT * engine_working_data) : engine_working_data(engine_working_data) { }
|
2012-06-15 12:47:27 -04:00
|
|
|
virtual ~BasicRoutingInterface(){ };
|
|
|
|
|
2013-09-19 12:53:34 -04:00
|
|
|
inline void RoutingStep(
|
|
|
|
SearchEngineData::QueryHeap & _forwardHeap,
|
|
|
|
SearchEngineData::QueryHeap & _backwardHeap,
|
|
|
|
NodeID *middle,
|
|
|
|
int *_upperbound,
|
|
|
|
const int edgeBasedOffset,
|
|
|
|
const bool forwardDirection
|
|
|
|
) const {
|
2013-02-03 10:47:32 -05:00
|
|
|
const NodeID node = _forwardHeap.DeleteMin();
|
|
|
|
const int distance = _forwardHeap.GetKey(node);
|
2013-08-08 08:17:01 -04:00
|
|
|
//SimpleLogger().Write() << "Settled (" << _forwardHeap.GetData( node ).parent << "," << node << ")=" << distance;
|
2013-02-03 10:47:32 -05:00
|
|
|
if(_backwardHeap.WasInserted(node) ){
|
|
|
|
const int newDistance = _backwardHeap.GetKey(node) + distance;
|
2012-06-15 12:47:27 -04:00
|
|
|
if(newDistance < *_upperbound ){
|
|
|
|
if(newDistance>=0 ) {
|
|
|
|
*middle = node;
|
|
|
|
*_upperbound = newDistance;
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(distance-edgeBasedOffset > *_upperbound){
|
2013-02-03 10:47:32 -05:00
|
|
|
_forwardHeap.DeleteAll();
|
2012-06-15 12:47:27 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-03 10:47:32 -05:00
|
|
|
//Stalling
|
2013-09-19 12:53:34 -04:00
|
|
|
for(
|
|
|
|
EdgeID edge = engine_working_data->BeginEdges( node );
|
|
|
|
edge < engine_working_data->EndEdges(node);
|
|
|
|
++edge
|
|
|
|
) {
|
|
|
|
const typename SearchEngineDataT::EdgeData & data = engine_working_data->GetEdgeData(edge);
|
2012-06-15 12:47:27 -04:00
|
|
|
bool backwardDirectionFlag = (!forwardDirection) ? data.forward : data.backward;
|
|
|
|
if(backwardDirectionFlag) {
|
2013-09-19 12:53:34 -04:00
|
|
|
const NodeID to = engine_working_data->GetTarget(edge);
|
2012-06-15 12:47:27 -04:00
|
|
|
const int edgeWeight = data.distance;
|
|
|
|
|
|
|
|
assert( edgeWeight > 0 );
|
|
|
|
|
2013-02-03 10:47:32 -05:00
|
|
|
if(_forwardHeap.WasInserted( to )) {
|
|
|
|
if(_forwardHeap.GetKey( to ) + edgeWeight < distance) {
|
2012-06-15 12:47:27 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-19 12:53:34 -04:00
|
|
|
for ( EdgeID edge = engine_working_data->BeginEdges( node ); edge < engine_working_data->EndEdges(node); ++edge ) {
|
|
|
|
const typename SearchEngineDataT::EdgeData & data = engine_working_data->GetEdgeData(edge);
|
2012-06-15 12:47:27 -04:00
|
|
|
bool forwardDirectionFlag = (forwardDirection ? data.forward : data.backward );
|
|
|
|
if(forwardDirectionFlag) {
|
|
|
|
|
2013-09-19 12:53:34 -04:00
|
|
|
const NodeID to = engine_working_data->GetTarget(edge);
|
2012-06-15 12:47:27 -04:00
|
|
|
const int edgeWeight = data.distance;
|
|
|
|
|
|
|
|
assert( edgeWeight > 0 );
|
|
|
|
const int toDistance = distance + edgeWeight;
|
|
|
|
|
|
|
|
//New Node discovered -> Add to Heap + Node Info Storage
|
2013-02-03 10:47:32 -05:00
|
|
|
if ( !_forwardHeap.WasInserted( to ) ) {
|
|
|
|
_forwardHeap.Insert( to, toDistance, node );
|
2012-06-15 12:47:27 -04:00
|
|
|
}
|
|
|
|
//Found a shorter Path -> Update distance
|
2013-02-03 10:47:32 -05:00
|
|
|
else if ( toDistance < _forwardHeap.GetKey( to ) ) {
|
|
|
|
_forwardHeap.GetData( to ).parent = node;
|
|
|
|
_forwardHeap.DecreaseKey( to, toDistance );
|
2012-06-15 12:47:27 -04:00
|
|
|
//new parent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-03 10:47:32 -05:00
|
|
|
inline void UnpackPath(const std::vector<NodeID> & packedPath, std::vector<_PathData> & unpackedPath) const {
|
2012-06-15 12:47:27 -04:00
|
|
|
const unsigned sizeOfPackedPath = packedPath.size();
|
|
|
|
std::stack<std::pair<NodeID, NodeID> > recursionStack;
|
|
|
|
|
|
|
|
//We have to push the path in reverse order onto the stack because it's LIFO.
|
|
|
|
for(unsigned i = sizeOfPackedPath-1; i > 0; --i){
|
|
|
|
recursionStack.push(std::make_pair(packedPath[i-1], packedPath[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<NodeID, NodeID> edge;
|
|
|
|
while(!recursionStack.empty()) {
|
|
|
|
edge = recursionStack.top();
|
|
|
|
recursionStack.pop();
|
|
|
|
|
2013-09-19 12:53:34 -04:00
|
|
|
EdgeID smallestEdge = SPECIAL_EDGEID;
|
2012-06-15 12:47:27 -04:00
|
|
|
int smallestWeight = INT_MAX;
|
2013-09-19 12:53:34 -04:00
|
|
|
for(EdgeID eit = engine_working_data->BeginEdges(edge.first);eit < engine_working_data->EndEdges(edge.first);++eit){
|
|
|
|
const int weight = engine_working_data->GetEdgeData(eit).distance;
|
|
|
|
if(engine_working_data->GetTarget(eit) == edge.second && weight < smallestWeight && engine_working_data->GetEdgeData(eit).forward){
|
2012-06-15 12:47:27 -04:00
|
|
|
smallestEdge = eit;
|
|
|
|
smallestWeight = weight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(smallestEdge == SPECIAL_EDGEID){
|
2013-09-19 12:53:34 -04:00
|
|
|
for(EdgeID eit = engine_working_data->BeginEdges(edge.second);eit < engine_working_data->EndEdges(edge.second);++eit){
|
|
|
|
const int weight = engine_working_data->GetEdgeData(eit).distance;
|
|
|
|
if(engine_working_data->GetTarget(eit) == edge.first && weight < smallestWeight && engine_working_data->GetEdgeData(eit).backward){
|
2012-06-15 12:47:27 -04:00
|
|
|
smallestEdge = eit;
|
|
|
|
smallestWeight = weight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(smallestWeight != INT_MAX);
|
|
|
|
|
2013-09-19 12:53:34 -04:00
|
|
|
const typename SearchEngineDataT::EdgeData& ed = engine_working_data->GetEdgeData(smallestEdge);
|
2012-06-15 12:47:27 -04:00
|
|
|
if(ed.shortcut) {//unpack
|
|
|
|
const NodeID middle = ed.id;
|
|
|
|
//again, we need to this in reversed order
|
|
|
|
recursionStack.push(std::make_pair(middle, edge.second));
|
|
|
|
recursionStack.push(std::make_pair(edge.first, middle));
|
|
|
|
} else {
|
|
|
|
assert(!ed.shortcut);
|
2013-08-16 08:06:04 -04:00
|
|
|
unpackedPath.push_back(
|
|
|
|
_PathData(
|
|
|
|
ed.id,
|
2013-09-19 12:53:34 -04:00
|
|
|
engine_working_data->GetNameIndexFromEdgeID(ed.id),
|
|
|
|
engine_working_data->GetTurnInstructionForEdgeID(ed.id),
|
2013-08-16 08:06:04 -04:00
|
|
|
ed.distance
|
|
|
|
)
|
|
|
|
);
|
2012-06-15 12:47:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-21 06:27:43 -04:00
|
|
|
inline void UnpackEdge(const NodeID s, const NodeID t, std::vector<NodeID> & unpackedPath) const {
|
|
|
|
std::stack<std::pair<NodeID, NodeID> > recursionStack;
|
|
|
|
recursionStack.push(std::make_pair(s,t));
|
|
|
|
|
|
|
|
std::pair<NodeID, NodeID> edge;
|
|
|
|
while(!recursionStack.empty()) {
|
|
|
|
edge = recursionStack.top();
|
|
|
|
recursionStack.pop();
|
|
|
|
|
2013-09-19 12:53:34 -04:00
|
|
|
EdgeID smallestEdge = SPECIAL_EDGEID;
|
2012-06-21 06:27:43 -04:00
|
|
|
int smallestWeight = INT_MAX;
|
2013-09-19 12:53:34 -04:00
|
|
|
for(EdgeID eit = engine_working_data->BeginEdges(edge.first);eit < engine_working_data->EndEdges(edge.first);++eit){
|
|
|
|
const int weight = engine_working_data->GetEdgeData(eit).distance;
|
|
|
|
if(engine_working_data->GetTarget(eit) == edge.second && weight < smallestWeight && engine_working_data->GetEdgeData(eit).forward){
|
2012-06-21 06:27:43 -04:00
|
|
|
smallestEdge = eit;
|
|
|
|
smallestWeight = weight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(smallestEdge == SPECIAL_EDGEID){
|
2013-09-19 12:53:34 -04:00
|
|
|
for(EdgeID eit = engine_working_data->BeginEdges(edge.second);eit < engine_working_data->EndEdges(edge.second);++eit){
|
|
|
|
const int weight = engine_working_data->GetEdgeData(eit).distance;
|
|
|
|
if(engine_working_data->GetTarget(eit) == edge.first && weight < smallestWeight && engine_working_data->GetEdgeData(eit).backward){
|
2012-06-21 06:27:43 -04:00
|
|
|
smallestEdge = eit;
|
|
|
|
smallestWeight = weight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(smallestWeight != INT_MAX);
|
|
|
|
|
2013-09-19 12:53:34 -04:00
|
|
|
const typename SearchEngineDataT::EdgeData& ed = engine_working_data->GetEdgeData(smallestEdge);
|
2012-06-21 06:27:43 -04:00
|
|
|
if(ed.shortcut) {//unpack
|
|
|
|
const NodeID middle = ed.id;
|
|
|
|
//again, we need to this in reversed order
|
|
|
|
recursionStack.push(std::make_pair(middle, edge.second));
|
|
|
|
recursionStack.push(std::make_pair(edge.first, middle));
|
|
|
|
} else {
|
|
|
|
assert(!ed.shortcut);
|
|
|
|
unpackedPath.push_back(edge.first );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unpackedPath.push_back(t);
|
|
|
|
}
|
|
|
|
|
2013-09-19 12:53:34 -04:00
|
|
|
inline void RetrievePackedPathFromHeap(
|
|
|
|
SearchEngineData::QueryHeap & _fHeap,
|
|
|
|
SearchEngineData::QueryHeap & _bHeap,
|
|
|
|
const NodeID middle,
|
|
|
|
std::vector<NodeID> & packedPath
|
|
|
|
) const {
|
2012-06-15 12:47:27 -04:00
|
|
|
NodeID pathNode = middle;
|
2013-02-03 10:47:32 -05:00
|
|
|
while(pathNode != _fHeap.GetData(pathNode).parent) {
|
|
|
|
pathNode = _fHeap.GetData(pathNode).parent;
|
2012-12-17 07:17:35 -05:00
|
|
|
packedPath.push_back(pathNode);
|
2012-06-15 12:47:27 -04:00
|
|
|
}
|
2012-09-27 13:46:44 -04:00
|
|
|
|
2012-12-17 07:17:35 -05:00
|
|
|
std::reverse(packedPath.begin(), packedPath.end());
|
2012-06-15 12:47:27 -04:00
|
|
|
packedPath.push_back(middle);
|
|
|
|
pathNode = middle;
|
2013-02-03 10:47:32 -05:00
|
|
|
while (pathNode != _bHeap.GetData(pathNode).parent){
|
|
|
|
pathNode = _bHeap.GetData(pathNode).parent;
|
2012-09-27 13:46:44 -04:00
|
|
|
packedPath.push_back(pathNode);
|
|
|
|
}
|
2012-06-15 12:47:27 -04:00
|
|
|
}
|
2013-02-03 10:47:32 -05:00
|
|
|
|
2013-09-19 12:53:34 -04:00
|
|
|
inline void RetrievePackedPathFromSingleHeap(SearchEngineData::QueryHeap & search_heap, const NodeID middle, std::vector<NodeID>& packed_path) const {
|
2013-02-03 10:47:32 -05:00
|
|
|
NodeID pathNode = middle;
|
|
|
|
while(pathNode != search_heap.GetData(pathNode).parent) {
|
|
|
|
pathNode = search_heap.GetData(pathNode).parent;
|
|
|
|
packed_path.push_back(pathNode);
|
|
|
|
}
|
|
|
|
}
|
2012-06-15 12:47:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* BASICROUTINGINTERFACE_H_ */
|