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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
#include "testing/testing.hpp"
#include "routing/routing_tests/index_graph_tools.hpp"
#include "routing/base/astar_algorithm.hpp"
#include "routing/car_model.hpp"
#include "routing/edge_estimator.hpp"
#include "routing/index_graph.hpp"
#include "routing/index_graph_serialization.hpp"
#include "routing/index_graph_starter.hpp"
#include "routing/vehicle_mask.hpp"
#include "routing/routing_tests/index_graph_tools.hpp"
#include "geometry/point2d.hpp"
#include "coding/reader.hpp"
#include "coding/writer.hpp"
#include "base/assert.hpp"
#include "std/algorithm.hpp"
#include "std/unique_ptr.hpp"
#include "std/unordered_map.hpp"
#include "std/vector.hpp"
namespace
{
using namespace routing;
using namespace routing_test;
void TestRoute(IndexGraph & graph, RoadPoint const & start, RoadPoint const & finish,
size_t expectedLength, vector<RoadPoint> const * expectedRoute = nullptr)
{
vector<RoadPoint> route;
IndexGraphStarter starter(graph, start, finish);
auto const resultCode = CalculateRoute(starter, route);
TEST_EQUAL(resultCode, AStarAlgorithm<IndexGraphStarter>::Result::OK, ());
TEST_EQUAL(route.size(), expectedLength, ());
if (expectedRoute)
TEST_EQUAL(route, *expectedRoute, ());
}
void TestEdges(IndexGraph & graph, Joint::Id jointId, vector<Joint::Id> const & expectedTargets,
bool forward)
{
vector<JointEdge> edges;
graph.GetEdgeList(jointId, forward, false /* graphWithoutRestrictions */, edges);
vector<Joint::Id> targets;
for (JointEdge const & edge : edges)
targets.push_back(edge.GetTarget());
sort(targets.begin(), targets.end());
TEST_EQUAL(targets, expectedTargets, ());
}
void TestOutgoingEdges(IndexGraph & graph, Joint::Id jointId,
vector<Joint::Id> const & expectedTargets)
{
TestEdges(graph, jointId, expectedTargets, true);
}
void TestIngoingEdges(IndexGraph & graph, Joint::Id jointId,
vector<Joint::Id> const & expectedTargets)
{
TestEdges(graph, jointId, expectedTargets, false);
}
uint32_t AbsDelta(uint32_t v0, uint32_t v1) { return v0 > v1 ? v0 - v1 : v1 - v0; }
} // namespace
namespace routing_test
{
// R4 (one way down)
//
// R1 J2--------J3 -1
// ^ v
// ^ v
// R0 *---J0----*---J1----* 0
// ^ v
// ^ v
// R2 J4--------J5 1
//
// R3 (one way up) y
//
// x: 0 1 2 3 4
//
UNIT_TEST(EdgesTest)
{
unique_ptr<TestGeometryLoader> loader = make_unique<TestGeometryLoader>();
loader->AddRoad(
0 /* featureId */, false, 1.0 /* speed */,
RoadGeometry::Points({{0.0, 0.0}, {1.0, 0.0}, {2.0, 0.0}, {3.0, 0.0}, {4.0, 0.0}}));
loader->AddRoad(1 /* featureId */, false, 1.0 /* speed */,
RoadGeometry::Points({{1.0, -1.0}, {3.0, -1.0}}));
loader->AddRoad(2 /* featureId */, false, 1.0 /* speed */,
RoadGeometry::Points({{1.0, -1.0}, {3.0, -1.0}}));
loader->AddRoad(3 /* featureId */, true, 1.0 /* speed */,
RoadGeometry::Points({{1.0, 1.0}, {1.0, 0.0}, {1.0, -1.0}}));
loader->AddRoad(4 /* featureId */, true, 1.0 /* speed */,
RoadGeometry::Points({{3.0, -1.0}, {3.0, 0.0}, {3.0, 1.0}}));
traffic::TrafficCache const trafficCache;
IndexGraph graph(move(loader), CreateEstimator(trafficCache));
vector<Joint> joints;
joints.emplace_back(MakeJoint({{0, 1}, {3, 1}})); // J0
joints.emplace_back(MakeJoint({{0, 3}, {4, 1}})); // J1
joints.emplace_back(MakeJoint({{1, 0}, {3, 2}})); // J2
joints.emplace_back(MakeJoint({{1, 1}, {4, 0}})); // J3
joints.emplace_back(MakeJoint({{2, 0}, {3, 0}})); // J4
joints.emplace_back(MakeJoint({{2, 1}, {4, 2}})); // J5
graph.Import(joints);
TestOutgoingEdges(graph, 0, {1, 2});
TestOutgoingEdges(graph, 1, {0, 5});
TestOutgoingEdges(graph, 2, {3});
TestOutgoingEdges(graph, 3, {1, 2});
TestOutgoingEdges(graph, 4, {0, 5});
TestOutgoingEdges(graph, 5, {4});
TestIngoingEdges(graph, 0, {1, 4});
TestIngoingEdges(graph, 1, {0, 3});
TestIngoingEdges(graph, 2, {0, 3});
TestIngoingEdges(graph, 3, {2});
TestIngoingEdges(graph, 4, {5});
TestIngoingEdges(graph, 5, {1, 4});
}
// Roads R1:
//
// -2
// -1
// R0 -2 -1 0 1 2
// 1
// 2
//
UNIT_TEST(FindPathCross)
{
unique_ptr<TestGeometryLoader> loader = make_unique<TestGeometryLoader>();
loader->AddRoad(
0 /* featureId */, false, 1.0 /* speed */,
RoadGeometry::Points({{-2.0, 0.0}, {-1.0, 0.0}, {0.0, 0.0}, {1.0, 0.0}, {2.0, 0.0}}));
loader->AddRoad(
1 /* featureId */, false, 1.0 /* speed */,
RoadGeometry::Points({{0.0, -2.0}, {-1.0, 0.0}, {0.0, 0.0}, {0.0, 1.0}, {0.0, 2.0}}));
traffic::TrafficCache const trafficCache;
IndexGraph graph(move(loader), CreateEstimator(trafficCache));
graph.Import({MakeJoint({{0, 2}, {1, 2}})});
vector<RoadPoint> points;
for (uint32_t i = 0; i < 5; ++i)
{
points.emplace_back(0, i);
points.emplace_back(1, i);
}
for (auto const & start : points)
{
for (auto const & finish : points)
{
uint32_t expectedLength;
// Length of the route is the number of route points.
// Example: p0 --- p1 --- p2
// 2 segments, 3 points,
// Therefore route length = geometrical length + 1
if (start.GetFeatureId() == finish.GetFeatureId())
expectedLength = AbsDelta(start.GetPointId(), finish.GetPointId()) + 1;
else
expectedLength = AbsDelta(start.GetPointId(), 2) + AbsDelta(finish.GetPointId(), 2) + 1;
TestRoute(graph, start, finish, expectedLength);
}
}
}
// Roads R4 R5 R6 R7
//
// R0 0 - * - * - *
// | | | |
// R1 * - 1 - * - *
// | | | |
// R2 * - * - 2 - *
// | | | |
// R3 * - * - * - 3
//
UNIT_TEST(FindPathManhattan)
{
uint32_t constexpr kCitySize = 4;
unique_ptr<TestGeometryLoader> loader = make_unique<TestGeometryLoader>();
for (uint32_t i = 0; i < kCitySize; ++i)
{
RoadGeometry::Points street;
RoadGeometry::Points avenue;
for (uint32_t j = 0; j < kCitySize; ++j)
{
street.emplace_back(static_cast<double>(i), static_cast<double>(j));
avenue.emplace_back(static_cast<double>(j), static_cast<double>(i));
}
loader->AddRoad(i, false, 1.0 /* speed */, street);
loader->AddRoad(i + kCitySize, false, 1.0 /* speed */, avenue);
}
traffic::TrafficCache const trafficCache;
IndexGraph graph(move(loader), CreateEstimator(trafficCache));
vector<Joint> joints;
for (uint32_t i = 0; i < kCitySize; ++i)
{
for (uint32_t j = 0; j < kCitySize; ++j)
joints.emplace_back(MakeJoint({{i, j}, {j + kCitySize, i}}));
}
graph.Import(joints);
for (uint32_t startY = 0; startY < kCitySize; ++startY)
{
for (uint32_t startX = 0; startX < kCitySize; ++startX)
{
for (uint32_t finishY = 0; finishY < kCitySize; ++finishY)
{
for (uint32_t finishX = 0; finishX < kCitySize; ++finishX)
TestRoute(graph, {startX, startY}, {finishX, finishY},
AbsDelta(startX, finishX) + AbsDelta(startY, finishY) + 1);
}
}
}
}
// Roads y:
//
// R0: * - * - * -1
// / \
// R1: J0 * -* - * - *- * J1 0
// \ /
// R2: * - * - * 1
//
// x: 0 1 2 3 4
//
UNIT_TEST(RedressRace)
{
unique_ptr<TestGeometryLoader> loader = make_unique<TestGeometryLoader>();
loader->AddRoad(
0 /* featureId */, false, 1.0 /* speed */,
RoadGeometry::Points({{0.0, 0.0}, {1.0, -1.0}, {2.0, -1.0}, {3.0, -1.0}, {4.0, 0.0}}));
loader->AddRoad(
1 /* featureId */, false, 1.0 /* speed */,
RoadGeometry::Points({{0.0, 0.0}, {1.0, 0.0}, {2.0, 0.0}, {3.0, 0.0}, {4.0, 0.0}}));
loader->AddRoad(
2 /* featureId */, false, 1.0 /* speed */,
RoadGeometry::Points({{0.0, 0.0}, {1.0, 1.0}, {2.0, 1.0}, {3.0, 1.0}, {4.0, 0.0}}));
traffic::TrafficCache const trafficCache;
IndexGraph graph(move(loader), CreateEstimator(trafficCache));
vector<Joint> joints;
joints.emplace_back(MakeJoint({{0, 0}, {1, 0}, {2, 0}})); // J0
joints.emplace_back(MakeJoint({{0, 4}, {1, 4}, {2, 4}})); // J1
graph.Import(joints);
vector<RoadPoint> const expectedRoute({{1, 0}, {1, 1}, {1, 2}, {1, 3}, {1, 4}});
TestRoute(graph, {0, 0}, {0, 4}, 5, &expectedRoute);
}
// Roads y:
//
// fast road R0 * - * - * -1
// / \
// slow road R1 J0 * - - * - - * J1 0
//
// x: 0 1 2 3 4
//
UNIT_TEST(RoadSpeed)
{
unique_ptr<TestGeometryLoader> loader = make_unique<TestGeometryLoader>();
loader->AddRoad(
0 /* featureId */, false, 10.0 /* speed */,
RoadGeometry::Points({{0.0, 0.0}, {1.0, -1.0}, {2.0, -1.0}, {3.0, -1.0}, {4.0, 0.0}}));
loader->AddRoad(1 /* featureId */, false, 1.0 /* speed */,
RoadGeometry::Points({{0.0, 0.0}, {2.0, 0.0}, {4.0, 0.0}}));
traffic::TrafficCache const trafficCache;
IndexGraph graph(move(loader), CreateEstimator(trafficCache));
vector<Joint> joints;
joints.emplace_back(MakeJoint({{0, 0}, {1, 0}})); // J0
joints.emplace_back(MakeJoint({{0, 4}, {1, 2}})); // J1
graph.Import(joints);
vector<RoadPoint> const expectedRoute({{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}});
TestRoute(graph, {0, 0}, {0, 4}, 5, &expectedRoute);
}
//
// Road R0 (ped) R1 (car) R2 (car)
// 0----------1 * 0----------1 * 0----------1
// Joints J0 J1
//
UNIT_TEST(SerializeSimpleGraph)
{
vector<uint8_t> buffer;
{
IndexGraph graph;
vector<Joint> joints = {
MakeJoint({{0, 1}, {1, 0}}), MakeJoint({{1, 1}, {2, 0}}),
};
graph.Import(joints);
unordered_map<uint32_t, VehicleMask> masks;
masks[0] = kPedestrianMask;
masks[1] = kCarMask;
masks[2] = kCarMask;
MemWriter<vector<uint8_t>> writer(buffer);
IndexGraphSerializer::Serialize(graph, masks, writer);
}
{
IndexGraph graph;
MemReader reader(buffer.data(), buffer.size());
ReaderSource<MemReader> source(reader);
IndexGraphSerializer::Deserialize(graph, source, kAllVehiclesMask);
TEST_EQUAL(graph.GetNumRoads(), 3, ());
TEST_EQUAL(graph.GetNumJoints(), 2, ());
TEST_EQUAL(graph.GetNumStaticPoints(), 4, ());
TEST_EQUAL(graph.GetJointId({0, 0}), Joint::kInvalidId, ());
TEST_EQUAL(graph.GetJointId({0, 1}), 1, ());
TEST_EQUAL(graph.GetJointId({1, 0}), 1, ());
TEST_EQUAL(graph.GetJointId({1, 1}), 0, ());
TEST_EQUAL(graph.GetJointId({2, 0}), 0, ());
TEST_EQUAL(graph.GetJointId({2, 1}), Joint::kInvalidId, ());
}
{
IndexGraph graph;
MemReader reader(buffer.data(), buffer.size());
ReaderSource<MemReader> source(reader);
IndexGraphSerializer::Deserialize(graph, source, kCarMask);
TEST_EQUAL(graph.GetNumRoads(), 2, ());
TEST_EQUAL(graph.GetNumJoints(), 1, ());
TEST_EQUAL(graph.GetNumStaticPoints(), 2, ());
TEST_EQUAL(graph.GetJointId({0, 0}), Joint::kInvalidId, ());
TEST_EQUAL(graph.GetJointId({0, 1}), Joint::kInvalidId, ());
TEST_EQUAL(graph.GetJointId({1, 0}), Joint::kInvalidId, ());
TEST_EQUAL(graph.GetJointId({1, 1}), 0, ());
TEST_EQUAL(graph.GetJointId({2, 0}), 0, ());
TEST_EQUAL(graph.GetJointId({2, 1}), Joint::kInvalidId, ());
}
}
} // namespace routing_test
|