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
|
#include "routing/road_graph.hpp"
#include "routing/road_graph_router.hpp"
#include "routing/route.hpp"
#include "geometry/distance_on_sphere.hpp"
#include "geometry/mercator.hpp"
#include "geometry/segment2d.hpp"
#include "base/assert.hpp"
#include "base/checked_cast.hpp"
#include "base/math.hpp"
#include "base/stl_helpers.hpp"
#include <algorithm>
#include <limits>
#include <sstream>
using namespace std;
namespace routing
{
namespace
{
bool OnEdge(Junction const & p, Edge const & ab)
{
auto const & a = ab.GetStartJunction();
auto const & b = ab.GetEndJunction();
return m2::IsPointOnSegmentEps(p.GetPoint(), a.GetPoint(), b.GetPoint(), 1e-9);
}
void SplitEdge(Edge const & ab, Junction const & p, vector<Edge> & edges)
{
auto const & a = ab.GetStartJunction();
auto const & b = ab.GetEndJunction();
// No need to split the edge by its endpoints.
if (a.GetPoint() == p.GetPoint() || b.GetPoint() == p.GetPoint())
return;
edges.push_back(Edge::MakeFake(a, p, ab));
edges.push_back(Edge::MakeFake(p, b, ab));
}
} // namespace
// Junction --------------------------------------------------------------------
Junction::Junction() : m_point(m2::PointD::Zero()), m_altitude(feature::kDefaultAltitudeMeters) {}
Junction::Junction(m2::PointD const & point, feature::TAltitude altitude)
: m_point(point), m_altitude(altitude)
{}
string DebugPrint(Junction const & r)
{
ostringstream ss;
ss << "Junction{point:" << DebugPrint(r.m_point) << ", altitude:" << r.GetAltitude() << "}";
return ss.str();
}
// Edge ------------------------------------------------------------------------
// static
Edge Edge::MakeReal(FeatureID const & featureId, bool forward, uint32_t segId,
Junction const & startJunction, Junction const & endJunction)
{
return {Type::Real, featureId, forward, segId, startJunction, endJunction};
}
// static
Edge Edge::MakeFake(Junction const & startJunction, Junction const & endJunction)
{
return {Type::FakeWithoutRealPart, FeatureID(), true /* forward */, 0 /* segmentId */,
startJunction, endJunction};
}
// static
Edge Edge::MakeFake(Junction const & startJunction, Junction const & endJunction,
Edge const & prototype)
{
auto e = prototype;
e.m_startJunction = startJunction;
e.m_endJunction = endJunction;
e.m_type = e.HasRealPart() ? Type::FakeWithRealPart : Type::FakeWithoutRealPart;
return e;
}
Edge::Edge(Type type, FeatureID const & featureId, bool forward, uint32_t segId,
Junction const & startJunction, Junction const & endJunction)
: m_type(type)
, m_featureId(featureId)
, m_forward(forward)
, m_segId(segId)
, m_startJunction(startJunction)
, m_endJunction(endJunction)
{
ASSERT_LESS(segId, numeric_limits<uint32_t>::max(), ());
ASSERT((m_featureId.IsValid() && HasRealPart()) || (!m_featureId.IsValid() && !HasRealPart()),
());
}
Edge Edge::GetReverseEdge() const
{
Edge edge = *this;
edge.m_forward = !edge.m_forward;
swap(edge.m_startJunction, edge.m_endJunction);
return edge;
}
bool Edge::SameRoadSegmentAndDirection(Edge const & r) const
{
return m_featureId == r.m_featureId &&
m_forward == r.m_forward &&
m_segId == r.m_segId;
}
bool Edge::operator==(Edge const & r) const
{
return m_type == r.m_type && m_featureId == r.m_featureId && m_forward == r.m_forward &&
m_segId == r.m_segId && m_startJunction == r.m_startJunction &&
m_endJunction == r.m_endJunction;
}
bool Edge::operator<(Edge const & r) const
{
if (m_type != r.m_type)
return m_type < r.m_type;
if (m_featureId != r.m_featureId)
return m_featureId < r.m_featureId;
if (m_forward != r.m_forward)
return (m_forward == false);
if (m_segId != r.m_segId)
return m_segId < r.m_segId;
if (!(m_startJunction == r.m_startJunction))
return m_startJunction < r.m_startJunction;
if (!(m_endJunction == r.m_endJunction))
return m_endJunction < r.m_endJunction;
return false;
}
string DebugPrint(Edge const & r)
{
ostringstream ss;
ss << "Edge{featureId: " << DebugPrint(r.GetFeatureId()) << ", isForward:" << r.IsForward()
<< ", partOfReal:" << r.HasRealPart() << ", segId:" << r.m_segId
<< ", startJunction:" << DebugPrint(r.m_startJunction)
<< ", endJunction:" << DebugPrint(r.m_endJunction) << "}";
return ss.str();
}
// IRoadGraph::RoadInfo --------------------------------------------------------
IRoadGraph::RoadInfo::RoadInfo()
: m_speedKMPH(0.0), m_bidirectional(false)
{}
IRoadGraph::RoadInfo::RoadInfo(RoadInfo && ri)
: m_junctions(move(ri.m_junctions))
, m_speedKMPH(ri.m_speedKMPH)
, m_bidirectional(ri.m_bidirectional)
{}
IRoadGraph::RoadInfo::RoadInfo(bool bidirectional, double speedKMPH,
initializer_list<Junction> const & points)
: m_junctions(points), m_speedKMPH(speedKMPH), m_bidirectional(bidirectional)
{}
// IRoadGraph::CrossOutgoingLoader ---------------------------------------------
void IRoadGraph::CrossOutgoingLoader::LoadEdges(FeatureID const & featureId, RoadInfo const & roadInfo)
{
ForEachEdge(roadInfo, [&featureId, &roadInfo, this](size_t segId, Junction const & endJunction,
bool forward) {
if (forward || roadInfo.m_bidirectional || m_mode == IRoadGraph::Mode::IgnoreOnewayTag)
{
m_edges.push_back(Edge::MakeReal(featureId, forward, base::asserted_cast<uint32_t>(segId),
m_cross, endJunction));
}
});
}
// IRoadGraph::CrossIngoingLoader ----------------------------------------------
void IRoadGraph::CrossIngoingLoader::LoadEdges(FeatureID const & featureId, RoadInfo const & roadInfo)
{
ForEachEdge(roadInfo, [&featureId, &roadInfo, this](size_t segId, Junction const & endJunction,
bool forward) {
if (!forward || roadInfo.m_bidirectional || m_mode == IRoadGraph::Mode::IgnoreOnewayTag)
{
m_edges.push_back(Edge::MakeReal(featureId, !forward, base::asserted_cast<uint32_t>(segId),
endJunction, m_cross));
}
});
}
// IRoadGraph ------------------------------------------------------------------
void IRoadGraph::GetOutgoingEdges(Junction const & junction, TEdgeVector & edges) const
{
GetFakeOutgoingEdges(junction, edges);
GetRegularOutgoingEdges(junction, edges);
}
void IRoadGraph::GetIngoingEdges(Junction const & junction, TEdgeVector & edges) const
{
GetFakeIngoingEdges(junction, edges);
GetRegularIngoingEdges(junction, edges);
}
void IRoadGraph::GetRegularOutgoingEdges(Junction const & junction, TEdgeVector & edges) const
{
CrossOutgoingLoader loader(junction, GetMode(), edges);
ForEachFeatureClosestToCross(junction.GetPoint(), loader);
}
void IRoadGraph::GetRegularIngoingEdges(Junction const & junction, TEdgeVector & edges) const
{
CrossIngoingLoader loader(junction, GetMode(), edges);
ForEachFeatureClosestToCross(junction.GetPoint(), loader);
}
void IRoadGraph::GetFakeOutgoingEdges(Junction const & junction, TEdgeVector & edges) const
{
auto const it = m_fakeOutgoingEdges.find(junction);
if (it != m_fakeOutgoingEdges.cend())
edges.insert(edges.end(), it->second.cbegin(), it->second.cend());
}
void IRoadGraph::GetFakeIngoingEdges(Junction const & junction, TEdgeVector & edges) const
{
auto const it = m_fakeIngoingEdges.find(junction);
if (it != m_fakeIngoingEdges.cend())
edges.insert(edges.end(), it->second.cbegin(), it->second.cend());
}
void IRoadGraph::ResetFakes()
{
m_fakeOutgoingEdges.clear();
m_fakeIngoingEdges.clear();
}
void IRoadGraph::AddEdge(Junction const & j, Edge const & e, map<Junction, TEdgeVector> & edges)
{
auto & cont = edges[j];
ASSERT(is_sorted(cont.cbegin(), cont.cend()), ());
auto const range = equal_range(cont.cbegin(), cont.cend(), e);
// Note. The "if" condition below is necessary to prevent duplicates which may be added when
// edges from |j| to "projection of |j|" and an edge in the opposite direction are added.
if (range.first == range.second)
cont.insert(range.second, e);
}
void IRoadGraph::AddFakeEdges(Junction const & junction,
vector<pair<Edge, Junction>> const & vicinity)
{
for (auto const & v : vicinity)
{
Edge const & ab = v.first;
Junction const p = v.second;
vector<Edge> edges;
SplitEdge(ab, p, edges);
edges.push_back(Edge::MakeFake(junction, p));
edges.push_back(Edge::MakeFake(p, junction));
ForEachFakeEdge([&](Edge const & uv)
{
if (OnEdge(p, uv))
SplitEdge(uv, p, edges);
});
for (auto const & uv : edges)
{
AddOutgoingFakeEdge(uv);
AddIngoingFakeEdge(uv);
}
}
}
void IRoadGraph::AddOutgoingFakeEdge(Edge const & e)
{
AddEdge(e.GetStartJunction(), e, m_fakeOutgoingEdges);
}
void IRoadGraph::AddIngoingFakeEdge(Edge const & e)
{
AddEdge(e.GetEndJunction(), e, m_fakeIngoingEdges);
}
double IRoadGraph::GetSpeedKMPH(Edge const & edge) const
{
double const speedKMPH = (edge.IsFake() ? GetMaxSpeedKMPH() : GetSpeedKMPH(edge.GetFeatureId()));
ASSERT(speedKMPH <= GetMaxSpeedKMPH(), ());
return speedKMPH;
}
void IRoadGraph::GetEdgeTypes(Edge const & edge, feature::TypesHolder & types) const
{
if (edge.IsFake())
types = feature::TypesHolder(feature::GEOM_LINE);
else
GetFeatureTypes(edge.GetFeatureId(), types);
}
string DebugPrint(IRoadGraph::Mode mode)
{
switch (mode)
{
case IRoadGraph::Mode::ObeyOnewayTag: return "ObeyOnewayTag";
case IRoadGraph::Mode::IgnoreOnewayTag: return "IgnoreOnewayTag";
}
}
IRoadGraph::RoadInfo MakeRoadInfoForTesting(bool bidirectional, double speedKMPH,
initializer_list<m2::PointD> const & points)
{
IRoadGraph::RoadInfo ri(bidirectional, speedKMPH, {});
for (auto const & p : points)
ri.m_junctions.emplace_back(MakeJunctionForTesting(p));
return ri;
}
// RoadGraphBase ------------------------------------------------------------------
bool RoadGraphBase::IsRouteEdgesImplemented() const { return false; }
bool RoadGraphBase::IsRouteSegmentsImplemented() const { return false; }
void RoadGraphBase::GetRouteEdges(TEdgeVector & routeEdges) const
{
NOTIMPLEMENTED()
}
void RoadGraphBase::GetRouteSegments(std::vector<Segment> &) const
{
NOTIMPLEMENTED()
}
} // namespace routing
|