1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
#include "routing/features_road_graph.hpp"
#include "routing/nearest_edge_finder.hpp"
#include "routing/pedestrian_directions.hpp"
#include "routing/pedestrian_model.hpp"
#include "routing/road_graph_router.hpp"
#include "routing/route.hpp"
#include "coding/reader_wrapper.hpp"
#include "indexer/feature.hpp"
#include "indexer/ftypes_matcher.hpp"
#include "indexer/index.hpp"
#include "platform/country_file.hpp"
#include "platform/local_country_file.hpp"
#include "platform/mwm_version.hpp"
#include "geometry/distance.hpp"
#include "std/queue.hpp"
#include "std/set.hpp"
#include "base/assert.hpp"
using platform::CountryFile;
using platform::LocalCountryFile;
namespace routing
{
namespace
{
size_t constexpr kMaxRoadCandidates = 6;
size_t constexpr kTestConnectivityVisitJunctionsLimit = 25;
uint64_t constexpr kMinPedestrianMwmVersion = 150713;
IRouter::ResultCode Convert(IRoutingAlgorithm::Result value)
{
switch (value)
{
case IRoutingAlgorithm::Result::OK: return IRouter::ResultCode::NoError;
case IRoutingAlgorithm::Result::NoPath: return IRouter::ResultCode::RouteNotFound;
case IRoutingAlgorithm::Result::Cancelled: return IRouter::ResultCode::Cancelled;
}
ASSERT(false, ("Unexpected IRoutingAlgorithm::Result value:", value));
return IRouter::ResultCode::RouteNotFound;
}
void Convert(vector<Junction> const & path, vector<m2::PointD> & geometry)
{
geometry.clear();
geometry.reserve(path.size());
for (auto const & pos : path)
geometry.emplace_back(pos.GetPoint());
}
// Check if the found edges lays on mwm with pedestrian routing support.
bool CheckMwmVersion(vector<pair<Edge, m2::PointD>> const & vicinities, vector<string> & mwmNames)
{
mwmNames.clear();
for (auto const & vicinity : vicinities)
{
auto const mwmInfo = vicinity.first.GetFeatureId().m_mwmId.GetInfo();
// mwmInfo gets version from a path of the file, so we must read the version header to be sure.
ModelReaderPtr reader = FilesContainerR(mwmInfo->GetLocalFile().GetPath(MapOptions::Map))
.GetReader(VERSION_FILE_TAG);
ReaderSrc src(reader.GetPtr());
version::MwmVersion version;
version::ReadVersion(src, version);
if (version.timestamp < kMinPedestrianMwmVersion)
mwmNames.push_back(mwmInfo->GetCountryName());
}
return !mwmNames.empty();
}
// Checks is edge connected with world graph.
// Function does BFS while it finds some number of edges, if graph ends
// before this number is reached then junction is assumed as not connected to the world graph.
bool CheckGraphConnectivity(IRoadGraph const & graph, Junction const & junction, size_t limit)
{
queue<Junction> q;
q.push(junction);
set<Junction> visited;
vector<Edge> edges;
while (!q.empty())
{
Junction const node = q.front();
q.pop();
if (visited.find(node) != visited.end())
continue;
visited.insert(node);
if (limit == visited.size())
return true;
edges.clear();
graph.GetOutgoingEdges(node, edges);
for (Edge const & e : edges)
q.push(e.GetEndJunction());
}
return false;
}
// Find closest candidates in the world graph
void FindClosestEdges(IRoadGraph const & graph, m2::PointD const & point,
vector<pair<Edge, m2::PointD>> & vicinity)
{
// WARNING: Take only one vicinity
// It is an oversimplification that is not as easily
// solved as tuning up this constant because if you set it too high
// you risk to find a feature that you cannot in fact reach because of
// an obstacle. Using only the closest feature minimizes (but not
// eliminates) this risk.
vector<pair<Edge, m2::PointD>> candidates;
graph.FindClosestEdges(point, kMaxRoadCandidates, candidates);
vicinity.clear();
for (auto const & candidate : candidates)
{
if (CheckGraphConnectivity(graph, candidate.first.GetStartJunction(), kTestConnectivityVisitJunctionsLimit))
{
vicinity.emplace_back(candidate);
break;
}
}
}
} // namespace
RoadGraphRouter::~RoadGraphRouter() {}
RoadGraphRouter::RoadGraphRouter(string const & name, Index & index,
TCountryFileFn const & countryFileFn,
unique_ptr<IVehicleModelFactory> && vehicleModelFactory,
unique_ptr<IRoutingAlgorithm> && algorithm,
unique_ptr<IDirectionsEngine> && directionsEngine)
: m_name(name)
, m_countryFileFn(countryFileFn)
, m_index(index)
, m_algorithm(move(algorithm))
, m_roadGraph(make_unique<FeaturesRoadGraph>(index, move(vehicleModelFactory)))
, m_directionsEngine(move(directionsEngine))
{
}
void RoadGraphRouter::ClearState()
{
m_roadGraph->ClearState();
}
bool RoadGraphRouter::CheckMapExistence(m2::PointD const & point, Route & route) const
{
string fileName = m_countryFileFn(point);
LocalCountryFile localCountryFile = m_index.GetMwmHandleByCountryFile(CountryFile(fileName)).GetInfo()->GetLocalFile();
if (!HasOptions(localCountryFile.GetFiles(), MapOptions::Map))
{
route.AddAbsentCountry(fileName);
return false;
}
return true;
}
IRouter::ResultCode RoadGraphRouter::CalculateRoute(m2::PointD const & startPoint,
m2::PointD const & /* startDirection */,
m2::PointD const & finalPoint,
RouterDelegate const & delegate, Route & route)
{
if (!CheckMapExistence(startPoint, route) || !CheckMapExistence(finalPoint, route))
return RouteFileNotExist;
vector<pair<Edge, m2::PointD>> finalVicinity;
FindClosestEdges(*m_roadGraph, finalPoint, finalVicinity);
if (finalVicinity.empty())
return EndPointNotFound;
// TODO (ldragunov) Remove this check after several releases. (Estimated in november)
vector<string> mwmNames;
if (CheckMwmVersion(finalVicinity, mwmNames))
{
for (auto const & name : mwmNames)
route.AddAbsentCountry(name);
}
vector<pair<Edge, m2::PointD>> startVicinity;
FindClosestEdges(*m_roadGraph, startPoint, startVicinity);
if (startVicinity.empty())
return StartPointNotFound;
// TODO (ldragunov) Remove this check after several releases. (Estimated in november)
if (CheckMwmVersion(startVicinity, mwmNames))
{
for (auto const & name : mwmNames)
route.AddAbsentCountry(name);
}
if (!route.GetAbsentCountries().empty())
return FileTooOld;
Junction const startPos(startPoint);
Junction const finalPos(finalPoint);
m_roadGraph->ResetFakes();
m_roadGraph->AddFakeEdges(startPos, startVicinity);
m_roadGraph->AddFakeEdges(finalPos, finalVicinity);
vector<Junction> path;
IRoutingAlgorithm::Result const resultCode =
m_algorithm->CalculateRoute(*m_roadGraph, startPos, finalPos, delegate, path);
if (resultCode == IRoutingAlgorithm::Result::OK)
{
ASSERT(!path.empty(), ());
ASSERT_EQUAL(path.front(), startPos, ());
ASSERT_EQUAL(path.back(), finalPos, ());
ReconstructRoute(move(path), route, delegate);
}
m_roadGraph->ResetFakes();
if (delegate.IsCancelled())
return IRouter::Cancelled;
return Convert(resultCode);
}
void RoadGraphRouter::ReconstructRoute(vector<Junction> && path, Route & route,
my::Cancellable const & cancellable) const
{
CHECK(!path.empty(), ("Can't reconstruct route from an empty list of positions."));
// By some reason there're two adjacent positions on a road with
// the same end-points. This could happen, for example, when
// direction on a road was changed. But it doesn't matter since
// this code reconstructs only geometry of a route.
path.erase(unique(path.begin(), path.end()), path.end());
if (path.size() == 1)
path.emplace_back(path.back());
vector<m2::PointD> geometry;
Convert(path, geometry);
Route::TTimes times;
Route::TTurns turnsDir;
turns::TTurnsGeom turnsGeom;
if (m_directionsEngine)
m_directionsEngine->Generate(*m_roadGraph, path, times, turnsDir, turnsGeom, cancellable);
route.SetGeometry(geometry.begin(), geometry.end());
route.SetSectionTimes(times);
route.SetTurnInstructions(turnsDir);
route.SetTurnInstructionsGeometry(turnsGeom);
}
unique_ptr<IRouter> CreatePedestrianAStarRouter(Index & index, TCountryFileFn const & countryFileFn)
{
unique_ptr<IVehicleModelFactory> vehicleModelFactory(new PedestrianModelFactory());
unique_ptr<IRoutingAlgorithm> algorithm(new AStarRoutingAlgorithm());
unique_ptr<IDirectionsEngine> directionsEngine(new PedestrianDirectionsEngine());
unique_ptr<IRouter> router(new RoadGraphRouter("astar-pedestrian", index, countryFileFn, move(vehicleModelFactory), move(algorithm), move(directionsEngine)));
return router;
}
unique_ptr<IRouter> CreatePedestrianAStarBidirectionalRouter(Index & index, TCountryFileFn const & countryFileFn)
{
unique_ptr<IVehicleModelFactory> vehicleModelFactory(new PedestrianModelFactory());
unique_ptr<IRoutingAlgorithm> algorithm(new AStarBidirectionalRoutingAlgorithm());
unique_ptr<IDirectionsEngine> directionsEngine(new PedestrianDirectionsEngine());
unique_ptr<IRouter> router(new RoadGraphRouter("astar-bidirectional-pedestrian", index, countryFileFn, move(vehicleModelFactory), move(algorithm), move(directionsEngine)));
return router;
}
} // namespace routing
|