forked from organicmaps/organicmaps
[pedestrian] Removed Dijkstra router.
This commit is contained in:
parent
95864ac28b
commit
43c606ce63
5 changed files with 0 additions and 279 deletions
|
@ -1,128 +0,0 @@
|
|||
#include "routing/dijkstra_router.hpp"
|
||||
|
||||
#include "indexer/mercator.hpp"
|
||||
|
||||
#include "base/logging.hpp"
|
||||
|
||||
#include "std/algorithm.hpp"
|
||||
#include "std/map.hpp"
|
||||
#include "std/queue.hpp"
|
||||
|
||||
namespace routing
|
||||
{
|
||||
|
||||
// Power of two to make the division faster.
|
||||
uint32_t const kCancelledPollPeriod = 128;
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename E>
|
||||
void SortUnique(vector<E> & v)
|
||||
{
|
||||
sort(v.begin(), v.end());
|
||||
v.resize(unique(v.begin(), v.end()) - v.begin());
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
bool Contains(vector<E> const & v, E const & e)
|
||||
{
|
||||
return binary_search(v.begin(), v.end(), e);
|
||||
}
|
||||
|
||||
void ReconstructPath(RoadPos const & v, map<RoadPos, RoadPos> const & parent,
|
||||
vector<RoadPos> & route)
|
||||
{
|
||||
route.clear();
|
||||
RoadPos cur = v;
|
||||
while (true)
|
||||
{
|
||||
route.push_back(cur);
|
||||
auto it = parent.find(cur);
|
||||
if (it == parent.end())
|
||||
break;
|
||||
cur = it->second;
|
||||
}
|
||||
}
|
||||
|
||||
struct Vertex
|
||||
{
|
||||
Vertex(RoadPos const & pos, double dist) : pos(pos), dist(dist) {}
|
||||
|
||||
bool operator<(Vertex const & v) const { return dist > v.dist; }
|
||||
|
||||
RoadPos pos;
|
||||
double dist;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
IRouter::ResultCode DijkstraRouter::CalculateRouteM2M(vector<RoadPos> const & startPos,
|
||||
vector<RoadPos> const & finalPos,
|
||||
vector<RoadPos> & route)
|
||||
{
|
||||
priority_queue<Vertex> queue;
|
||||
|
||||
// Upper bound on the distance from start positions to current vertex.
|
||||
map<RoadPos, double> dist;
|
||||
|
||||
// Parent in the search tree.
|
||||
map<RoadPos, RoadPos> parent;
|
||||
|
||||
vector<uint32_t> sortedStartFeatures(startPos.size());
|
||||
for (size_t i = 0; i < startPos.size(); ++i)
|
||||
sortedStartFeatures[i] = startPos[i].GetFeatureId();
|
||||
SortUnique(sortedStartFeatures);
|
||||
|
||||
vector<RoadPos> sortedStartPos(startPos.begin(), startPos.end());
|
||||
SortUnique(sortedStartPos);
|
||||
|
||||
for (RoadPos const & fp : finalPos)
|
||||
dist[fp] = 0.0;
|
||||
for (auto const & p : dist)
|
||||
queue.push(Vertex(p.first, 0.0 /* distance */));
|
||||
|
||||
uint32_t steps = 0;
|
||||
|
||||
while (!queue.empty())
|
||||
{
|
||||
++steps;
|
||||
if (steps % kCancelledPollPeriod == 0)
|
||||
{
|
||||
if (IsCancelled())
|
||||
return IRouter::Cancelled;
|
||||
}
|
||||
|
||||
Vertex const v = queue.top();
|
||||
queue.pop();
|
||||
|
||||
if (v.dist > dist[v.pos])
|
||||
continue;
|
||||
|
||||
if (Contains(sortedStartFeatures, v.pos.GetFeatureId()) && Contains(sortedStartPos, v.pos))
|
||||
{
|
||||
ReconstructPath(v.pos, parent, route);
|
||||
return IRouter::NoError;
|
||||
}
|
||||
IRoadGraph::TurnsVectorT turns;
|
||||
m_roadGraph->GetNearestTurns(v.pos, turns);
|
||||
for (PossibleTurn const & turn : turns)
|
||||
{
|
||||
RoadPos const & pos = turn.m_pos;
|
||||
if (v.pos == pos)
|
||||
continue;
|
||||
// TODO (@gorshenin): distance must be measured in time units,
|
||||
// so it possibly worth to divide distance by the pedestrian's
|
||||
// speed.
|
||||
double const d =
|
||||
v.dist + MercatorBounds::DistanceOnEarth(v.pos.GetSegEndpoint(), pos.GetSegEndpoint());
|
||||
auto it = dist.find(pos);
|
||||
if (it == dist.end() || d < it->second)
|
||||
{
|
||||
dist[pos] = d;
|
||||
parent[pos] = v.pos;
|
||||
queue.push(Vertex(pos, d));
|
||||
}
|
||||
}
|
||||
}
|
||||
return IRouter::RouteNotFound;
|
||||
}
|
||||
} // namespace routing
|
|
@ -1,24 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "routing/road_graph_router.hpp"
|
||||
|
||||
namespace routing
|
||||
{
|
||||
class DijkstraRouter : public RoadGraphRouter
|
||||
{
|
||||
typedef RoadGraphRouter BaseT;
|
||||
|
||||
public:
|
||||
DijkstraRouter(Index const * pIndex = 0)
|
||||
: BaseT(pIndex, unique_ptr<IVehicleModel>(new PedestrianModel()))
|
||||
{
|
||||
}
|
||||
|
||||
// IRouter overrides:
|
||||
string GetName() const override { return "pedestrian-dijkstra"; }
|
||||
|
||||
// RoadGraphRouter overrides:
|
||||
ResultCode CalculateRouteM2M(vector<RoadPos> const & startPos, vector<RoadPos> const & finalPos,
|
||||
vector<RoadPos> & route) override;
|
||||
};
|
||||
} // namespace routing
|
|
@ -15,7 +15,6 @@ SOURCES += \
|
|||
astar_router.cpp \
|
||||
async_router.cpp \
|
||||
cross_routing_context.cpp \
|
||||
dijkstra_router.cpp \
|
||||
features_road_graph.cpp \
|
||||
nearest_road_pos_finder.cpp \
|
||||
osrm2feature_map.cpp \
|
||||
|
@ -34,7 +33,6 @@ HEADERS += \
|
|||
base/astar_algorithm.hpp \
|
||||
base/graph.hpp \
|
||||
cross_routing_context.hpp \
|
||||
dijkstra_router.hpp \
|
||||
features_road_graph.hpp \
|
||||
nearest_road_pos_finder.hpp \
|
||||
osrm2feature_map.hpp \
|
||||
|
|
|
@ -1,124 +0,0 @@
|
|||
#include "testing/testing.hpp"
|
||||
|
||||
#include "routing/routing_tests/road_graph_builder.hpp"
|
||||
#include "routing/routing_tests/features_road_graph_test.hpp"
|
||||
|
||||
#include "routing/dijkstra_router.hpp"
|
||||
#include "routing/features_road_graph.hpp"
|
||||
#include "routing/route.hpp"
|
||||
|
||||
#include "indexer/classificator_loader.hpp"
|
||||
|
||||
#include "base/logging.hpp"
|
||||
#include "base/macros.hpp"
|
||||
|
||||
|
||||
using namespace routing;
|
||||
using namespace routing_test;
|
||||
|
||||
|
||||
// Use mock graph source.
|
||||
template <size_t finalPosSize, size_t startPosSize, size_t expectedSize>
|
||||
void TestDijkstraRouterMock(RoadPos (&finalPos)[finalPosSize],
|
||||
RoadPos (&startPos)[startPosSize],
|
||||
RoadPos (&expected)[expectedSize])
|
||||
{
|
||||
DijkstraRouter router;
|
||||
{
|
||||
unique_ptr<RoadGraphMockSource> graph(new RoadGraphMockSource());
|
||||
InitRoadGraphMockSourceWithTest2(*graph);
|
||||
router.SetRoadGraph(move(graph));
|
||||
}
|
||||
vector<RoadPos> result;
|
||||
IRouter::ResultCode resultCode = router.CalculateRouteM2M(
|
||||
vector<RoadPos>(&startPos[0], &startPos[0] + ARRAY_SIZE(startPos)),
|
||||
vector<RoadPos>(&finalPos[0], &finalPos[0] + ARRAY_SIZE(finalPos)), result);
|
||||
TEST_EQUAL(IRouter::NoError, resultCode, ());
|
||||
TEST_EQUAL(vector<RoadPos>(&expected[0], &expected[0] + ARRAY_SIZE(expected)), result, ());
|
||||
}
|
||||
|
||||
// Use mwm features graph source.
|
||||
template <size_t finalPosSize, size_t startPosSize, size_t expectedSize>
|
||||
void TestDijkstraRouterMWM(RoadPos (&finalPos)[finalPosSize],
|
||||
RoadPos (&startPos)[startPosSize],
|
||||
RoadPos (&expected)[expectedSize],
|
||||
size_t pointsCount)
|
||||
{
|
||||
FeatureRoadGraphTester tester("route_test2.mwm");
|
||||
|
||||
DijkstraRouter router;
|
||||
router.SetRoadGraph(tester.StealGraph());
|
||||
|
||||
vector<RoadPos> finalV(&finalPos[0], &finalPos[0] + ARRAY_SIZE(finalPos));
|
||||
tester.Name2FeatureID(finalV);
|
||||
|
||||
vector<RoadPos> startV(&startPos[0], &startPos[0] + ARRAY_SIZE(startPos));
|
||||
tester.Name2FeatureID(startV);
|
||||
|
||||
vector<RoadPos> result;
|
||||
IRouter::ResultCode resultCode = router.CalculateRouteM2M(startV, finalV, result);
|
||||
TEST_EQUAL(IRouter::NoError, resultCode, ());
|
||||
LOG(LDEBUG, (result));
|
||||
|
||||
Route route(router.GetName());
|
||||
router.GetGraph()->ReconstructPath(result, route);
|
||||
LOG(LDEBUG, (route));
|
||||
TEST_EQUAL(route.GetPoly().GetSize(), pointsCount, ());
|
||||
|
||||
tester.FeatureID2Name(result);
|
||||
TEST_EQUAL(vector<RoadPos>(&expected[0], &expected[0] + ARRAY_SIZE(expected)), result, ());
|
||||
}
|
||||
|
||||
|
||||
UNIT_TEST(Dijkstra_Router_City_Simple)
|
||||
{
|
||||
classificator::Load();
|
||||
|
||||
// Uncomment to see debug log.
|
||||
//my::g_LogLevel = LDEBUG;
|
||||
|
||||
RoadPos finalPos[] = { RoadPos(7, true, 0) };
|
||||
RoadPos startPos[] = { RoadPos(1, true, 0) };
|
||||
|
||||
RoadPos expected1[] = { RoadPos(1, true, 0),
|
||||
RoadPos(1, true, 1),
|
||||
RoadPos(8, true, 0),
|
||||
RoadPos(8, true, 1),
|
||||
RoadPos(7, true, 0) };
|
||||
TestDijkstraRouterMock(finalPos, startPos, expected1);
|
||||
|
||||
RoadPos expected2[] = { RoadPos(1, true, 0),
|
||||
RoadPos(1, true, 1),
|
||||
RoadPos(8, true, 1),
|
||||
RoadPos(7, true, 0) };
|
||||
TestDijkstraRouterMWM(finalPos, startPos, expected2, 4);
|
||||
}
|
||||
|
||||
|
||||
UNIT_TEST(Dijkstra_Router_City_ReallyFunnyLoop)
|
||||
{
|
||||
classificator::Load();
|
||||
|
||||
// Uncomment to see debug log.
|
||||
//my::g_LogLevel = LDEBUG;
|
||||
|
||||
RoadPos finalPos[] = { RoadPos(1, true, 0) };
|
||||
RoadPos startPos[] = { RoadPos(1, true, 1) };
|
||||
RoadPos expected1[] = { RoadPos(1, true, 1),
|
||||
RoadPos(8, true, 0),
|
||||
RoadPos(8, true, 1),
|
||||
RoadPos(8, true, 3), // algorithm skips 8,2 segment
|
||||
RoadPos(4, false, 0),
|
||||
RoadPos(0, false, 1),
|
||||
RoadPos(0, false, 0),
|
||||
RoadPos(1, true, 0) };
|
||||
TestDijkstraRouterMock(finalPos, startPos, expected1);
|
||||
|
||||
RoadPos expected2[] = { RoadPos(1, true, 1),
|
||||
RoadPos(8, true, 4),
|
||||
RoadPos(2, true, 1),
|
||||
RoadPos(0, false, 0),
|
||||
RoadPos(1, true, 0) };
|
||||
TestDijkstraRouterMWM(finalPos, startPos, expected2, 9);
|
||||
|
||||
}
|
|
@ -22,7 +22,6 @@ SOURCES += \
|
|||
astar_algorithm_test.cpp \
|
||||
astar_router_test.cpp \
|
||||
cross_routing_tests.cpp \
|
||||
dijkstra_router_test.cpp \
|
||||
features_road_graph_test.cpp \
|
||||
osrm_router_test.cpp \
|
||||
road_graph_builder.cpp \
|
||||
|
|
Loading…
Add table
Reference in a new issue