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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
#include "integration_tests/routing_test_tools.hpp"
#include "testing/testing.hpp"
#include "map/feature_vec_model.hpp"
#include "geometry/distance_on_sphere.hpp"
#include "geometry/latlon.hpp"
#include "routing/online_absent_fetcher.hpp"
#include "routing/online_cross_fetcher.hpp"
#include "routing/road_graph_router.hpp"
#include "routing/route.hpp"
#include "routing/router_delegate.hpp"
#include "search/search_engine.hpp"
#include "indexer/index.hpp"
#include "platform/local_country_file.hpp"
#include "platform/local_country_file_utils.hpp"
#include "platform/platform.hpp"
#include "platform/preferred_languages.hpp"
#include "geometry/distance_on_sphere.hpp"
#include "search/search_engine.hpp"
#include "search/search_query_factory.hpp"
#include <sys/resource.h>
using namespace routing;
using platform::LocalCountryFile;
namespace
{
void ChangeMaxNumberOfOpenFiles(size_t n)
{
struct rlimit rlp;
getrlimit(RLIMIT_NOFILE, &rlp);
rlp.rlim_cur = n;
setrlimit(RLIMIT_NOFILE, &rlp);
}
}
namespace integration
{
shared_ptr<model::FeaturesFetcher> CreateFeaturesFetcher(vector<LocalCountryFile> const & localFiles)
{
size_t const maxOpenFileNumber = 1024;
ChangeMaxNumberOfOpenFiles(maxOpenFileNumber);
shared_ptr<model::FeaturesFetcher> featuresFetcher(new model::FeaturesFetcher);
featuresFetcher->InitClassificator();
for (LocalCountryFile const & localFile : localFiles)
{
auto p = featuresFetcher->RegisterMap(localFile);
if (p.second != MwmSet::RegResult::Success)
{
ASSERT(false, ("Can't register", localFile));
return nullptr;
}
}
return featuresFetcher;
}
shared_ptr<search::Engine> CreateSearchEngine(shared_ptr<model::FeaturesFetcher> featuresFetcher)
{
ASSERT(featuresFetcher, ());
search::Engine::IndexType const & index = featuresFetcher->GetIndex();
Platform const & pl = GetPlatform();
try
{
shared_ptr<search::Engine> searchEngine(new search::Engine(
&index, pl.GetReader(SEARCH_CATEGORIES_FILE_NAME), pl.GetReader(PACKED_POLYGONS_FILE),
pl.GetReader(COUNTRIES_FILE), languages::GetCurrentOrig(),
make_unique<search::SearchQueryFactory>()));
return searchEngine;
}
catch (RootException const &e)
{
LOG(LCRITICAL, ("Error:", e.what(), " while creating searchEngine."));
return nullptr;
}
}
shared_ptr<OsrmRouter> CreateOsrmRouter(Index & index, search::Engine & searchEngine)
{
shared_ptr<OsrmRouter> osrmRouter(new OsrmRouter(
&index, [&searchEngine](m2::PointD const & pt)
{
return searchEngine.GetCountryFile(pt);
}
));
return osrmRouter;
}
shared_ptr<IRouter> CreatePedestrianRouter(Index & index, search::Engine & searchEngine)
{
auto countryFileGetter = [&searchEngine](m2::PointD const & pt)
{
return searchEngine.GetCountryFile(pt);
};
unique_ptr<IRouter> router = CreatePedestrianAStarBidirectionalRouter(index, countryFileGetter);
return shared_ptr<IRouter>(move(router));
}
class IRouterComponents
{
public:
virtual IRouter * GetRouter() const = 0;
virtual search::Engine * GetSearchEngine() const = 0;
virtual ~IRouterComponents() = default;
};
class OsrmRouterComponents : public IRouterComponents
{
public:
OsrmRouterComponents(vector<LocalCountryFile> const & localFiles)
: m_featuresFetcher(CreateFeaturesFetcher(localFiles)),
m_searchEngine(CreateSearchEngine(m_featuresFetcher)),
m_osrmRouter(CreateOsrmRouter(m_featuresFetcher->GetIndex(), *m_searchEngine.get()))
{
}
IRouter * GetRouter() const override { return m_osrmRouter.get(); }
search::Engine * GetSearchEngine() const override { return m_searchEngine.get(); }
private:
shared_ptr<model::FeaturesFetcher> m_featuresFetcher;
shared_ptr<search::Engine> m_searchEngine;
shared_ptr<OsrmRouter> m_osrmRouter;
};
class PedestrianRouterComponents : public IRouterComponents
{
public:
PedestrianRouterComponents(vector<LocalCountryFile> const & localFiles)
: m_featuresFetcher(CreateFeaturesFetcher(localFiles)),
m_searchEngine(CreateSearchEngine(m_featuresFetcher)),
m_router(CreatePedestrianRouter(m_featuresFetcher->GetIndex(), *m_searchEngine))
{
}
IRouter * GetRouter() const override { return m_router.get(); }
search::Engine * GetSearchEngine() const override { return m_searchEngine.get(); }
private:
shared_ptr<model::FeaturesFetcher> m_featuresFetcher;
shared_ptr<search::Engine> m_searchEngine;
shared_ptr<IRouter> m_router;
};
template <typename TRouterComponents>
shared_ptr<TRouterComponents> CreateAllMapsComponents()
{
// Setting stored paths from testingmain.cpp
Platform & pl = GetPlatform();
CommandLineOptions const & options = GetTestingOptions();
if (options.m_dataPath)
pl.SetWritableDirForTests(options.m_dataPath);
if (options.m_resourcePath)
pl.SetResourceDir(options.m_resourcePath);
vector<LocalCountryFile> localFiles;
platform::FindAllLocalMaps(localFiles);
for (auto & file : localFiles)
file.SyncWithDisk();
ASSERT(!localFiles.empty(), ());
return shared_ptr<TRouterComponents>(new TRouterComponents(localFiles));
}
shared_ptr<IRouterComponents> GetOsrmComponents(vector<platform::LocalCountryFile> const & localFiles)
{
return shared_ptr<IRouterComponents>(new OsrmRouterComponents(localFiles));
}
IRouterComponents & GetOsrmComponents()
{
static shared_ptr<IRouterComponents> const inst = CreateAllMapsComponents<OsrmRouterComponents>();
ASSERT(inst, ());
return *inst;
}
shared_ptr<IRouterComponents> GetPedestrianComponents(vector<platform::LocalCountryFile> const & localFiles)
{
return shared_ptr<IRouterComponents>(new PedestrianRouterComponents(localFiles));
}
IRouterComponents & GetPedestrianComponents()
{
static shared_ptr<IRouterComponents> const inst = CreateAllMapsComponents<PedestrianRouterComponents>();
ASSERT(inst, ());
return *inst;
}
TRouteResult CalculateRoute(IRouterComponents const & routerComponents,
m2::PointD const & startPoint, m2::PointD const & startDirection,
m2::PointD const & finalPoint)
{
RouterDelegate delegate;
IRouter * router = routerComponents.GetRouter();
ASSERT(router, ());
shared_ptr<Route> route(new Route("mapsme"));
IRouter::ResultCode result =
router->CalculateRoute(startPoint, startDirection, finalPoint, delegate, *route.get());
ASSERT(route, ());
return TRouteResult(route, result);
}
void TestTurnCount(routing::Route const & route, uint32_t expectedTurnCount)
{
// We use -1 for ignoring the "ReachedYourDestination" turn record.
TEST_EQUAL(route.GetTurns().size() - 1, expectedTurnCount, ());
}
void TestRouteLength(Route const & route, double expectedRouteMeters,
double relativeError)
{
double const delta = expectedRouteMeters * relativeError;
double const routeMeters = route.GetTotalDistanceMeters();
TEST(my::AlmostEqualAbs(routeMeters, expectedRouteMeters, delta),
("Route time test failed. Expected:", expectedRouteMeters, "have:", routeMeters, "delta:", delta));
}
void TestRouteTime(Route const & route, double expectedRouteSeconds, double relativeError)
{
double const delta = expectedRouteSeconds * relativeError;
double const routeSeconds = route.GetTotalTimeSec();
TEST(my::AlmostEqualAbs(routeSeconds, expectedRouteSeconds, delta),
("Route time test failed. Expected:", expectedRouteSeconds, "have:", routeSeconds, "delta:", delta));
}
void CalculateRouteAndTestRouteLength(IRouterComponents const & routerComponents,
m2::PointD const & startPoint,
m2::PointD const & startDirection,
m2::PointD const & finalPoint, double expectedRouteMeters,
double relativeError)
{
TRouteResult routeResult =
CalculateRoute(routerComponents, startPoint, startDirection, finalPoint);
IRouter::ResultCode const result = routeResult.second;
TEST_EQUAL(result, IRouter::NoError, ());
TestRouteLength(*routeResult.first, expectedRouteMeters, relativeError);
}
const TestTurn & TestTurn::TestValid() const
{
TEST(m_isValid, ());
return *this;
}
const TestTurn & TestTurn::TestNotValid() const
{
TEST(!m_isValid, ());
return *this;
}
const TestTurn & TestTurn::TestPoint(m2::PointD const & expectedPoint, double inaccuracyMeters) const
{
double const distanceMeters = ms::DistanceOnEarth(expectedPoint.y, expectedPoint.x, m_point.y, m_point.x);
TEST_LESS(distanceMeters, inaccuracyMeters, ());
return *this;
}
const TestTurn & TestTurn::TestDirection(routing::turns::TurnDirection expectedDirection) const
{
TEST_EQUAL(m_direction, expectedDirection, ());
return *this;
}
const TestTurn & TestTurn::TestOneOfDirections(
set<routing::turns::TurnDirection> const & expectedDirections) const
{
TEST(expectedDirections.find(m_direction) != expectedDirections.cend(), ());
return *this;
}
const TestTurn & TestTurn::TestRoundAboutExitNum(uint32_t expectedRoundAboutExitNum) const
{
TEST_EQUAL(m_roundAboutExitNum, expectedRoundAboutExitNum, ());
return *this;
}
TestTurn GetNthTurn(routing::Route const & route, uint32_t turnNumber)
{
Route::TTurns const & turns = route.GetTurns();
if (turnNumber >= turns.size())
return TestTurn();
TurnItem const & turn = turns[turnNumber];
return TestTurn(route.GetPoly().GetPoint(turn.m_index), turn.m_turn, turn.m_exitNum);
}
void TestOnlineFetcher(ms::LatLon const & startPoint, ms::LatLon const & finalPoint,
vector<string> const & expected, IRouterComponents & routerComponents)
{
auto countryFileGetter = [&routerComponents](m2::PointD const & p) -> string
{
return routerComponents.GetSearchEngine()->GetCountryFile(p);
};
auto localFileGetter = [&routerComponents](string const & countryFile) -> shared_ptr<LocalCountryFile>
{
//Always returns empty LocalFile
return make_shared<LocalCountryFile>();
};
routing::OnlineAbsentCountriesFetcher fetcher(countryFileGetter, localFileGetter);
fetcher.GenerateRequest(MercatorBounds::FromLatLon(startPoint),
MercatorBounds::FromLatLon(finalPoint));
vector<string> absent;
fetcher.GetAbsentCountries(absent);
if (expected.size() < 2)
{
// Single MWM case. Do not use online routing.
TEST(absent.empty(), ());
return;
}
TEST_EQUAL(absent.size(), expected.size(), ());
for (string const & name : expected)
{
TEST(find(absent.begin(), absent.end(), name) != absent.end(), ("Can't find ", name));
}
}
void TestOnlineCrosses(ms::LatLon const & startPoint, ms::LatLon const & finalPoint,
vector<string> const & expected,
IRouterComponents & routerComponents)
{
routing::OnlineCrossFetcher fetcher(OSRM_ONLINE_SERVER_URL, startPoint, finalPoint);
fetcher.Do();
vector<m2::PointD> const & points = fetcher.GetMwmPoints();
TEST_EQUAL(points.size(), expected.size(), ());
for (m2::PointD const & point : points)
{
string const mwmName = routerComponents.GetSearchEngine()->GetCountryFile(point);
TEST(find(expected.begin(), expected.end(), mwmName) != expected.end(), ("Can't find ", mwmName));
}
TestOnlineFetcher(startPoint, finalPoint, expected, routerComponents);
}
}
|