Welcome to mirror list, hosted at ThFree Co, Russian Federation.

transit_types.cpp « transit - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6f5298ba84ca434140d088857ab577850b585ed4 (plain)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include "transit/transit_types.hpp"

#include "transit/transit_serdes.hpp"
#include "transit/transit_version.hpp"

#include "base/string_utils.hpp"

namespace
{
double constexpr kPointsEqualEpsilon = 1e-6;
}  // namespace

namespace routing
{
namespace transit
{
// TransitHeader ----------------------------------------------------------------------------------
TransitHeader::TransitHeader(uint16_t version, uint32_t stopsOffset, uint32_t gatesOffset,
                             uint32_t edgesOffset, uint32_t transfersOffset, uint32_t linesOffset,
                             uint32_t shapesOffset, uint32_t networksOffset, uint32_t endOffset)
  : m_version(version)
  , m_reserve(0)
  , m_stopsOffset(stopsOffset)
  , m_gatesOffset(gatesOffset)
  , m_edgesOffset(edgesOffset)
  , m_transfersOffset(transfersOffset)
  , m_linesOffset(linesOffset)
  , m_shapesOffset(shapesOffset)
  , m_networksOffset(networksOffset)
  , m_endOffset(endOffset)
{
}

void TransitHeader::Reset()
{
  m_version = static_cast<uint16_t>(::transit::TransitVersion::OnlySubway);
  m_reserve = 0;
  m_stopsOffset = 0;
  m_gatesOffset = 0;
  m_edgesOffset = 0;
  m_transfersOffset = 0;
  m_linesOffset = 0;
  m_shapesOffset = 0;
  m_networksOffset = 0;
  m_endOffset = 0;
}

bool TransitHeader::IsEqualForTesting(TransitHeader const & header) const
{
  return m_version == header.m_version
         && m_reserve == header.m_reserve
         && m_stopsOffset == header.m_stopsOffset
         && m_gatesOffset == header.m_gatesOffset
         && m_edgesOffset == header.m_edgesOffset
         && m_transfersOffset == header.m_transfersOffset
         && m_linesOffset == header.m_linesOffset
         && m_shapesOffset == header.m_shapesOffset
         && m_networksOffset == header.m_networksOffset
         && m_endOffset == header.m_endOffset;
}

bool TransitHeader::IsValid() const
{
  return m_stopsOffset <= m_gatesOffset && m_gatesOffset <= m_edgesOffset &&
         m_edgesOffset <= m_transfersOffset && m_transfersOffset <= m_linesOffset &&
         m_linesOffset <= m_shapesOffset && m_shapesOffset <= m_networksOffset &&
         m_networksOffset <= m_endOffset;
}

// FeatureIdentifiers -----------------------------------------------------------------------------
FeatureIdentifiers::FeatureIdentifiers(bool serializeFeatureIdOnly)
  : m_serializeFeatureIdOnly(serializeFeatureIdOnly)
{
}

FeatureIdentifiers::FeatureIdentifiers(OsmId osmId, FeatureId const & featureId,
                                       bool serializeFeatureIdOnly)
  : m_osmId(osmId), m_featureId(featureId), m_serializeFeatureIdOnly(serializeFeatureIdOnly)
{
}

bool FeatureIdentifiers::operator<(FeatureIdentifiers const & rhs) const
{
  CHECK_EQUAL(m_serializeFeatureIdOnly, rhs.m_serializeFeatureIdOnly, ());
  if (m_serializeFeatureIdOnly)
    return m_featureId < rhs.m_featureId;

  if (m_featureId != rhs.m_featureId)
    return m_featureId < rhs.m_featureId;
  return m_osmId < rhs.m_osmId;
}

bool FeatureIdentifiers::operator==(FeatureIdentifiers const & rhs) const
{
  CHECK_EQUAL(m_serializeFeatureIdOnly, rhs.m_serializeFeatureIdOnly, ());
  return m_serializeFeatureIdOnly ? m_featureId == rhs.m_featureId
                                  : m_osmId == rhs.m_osmId && m_featureId == rhs.m_featureId;
}

bool FeatureIdentifiers::IsValid() const
{
  return m_serializeFeatureIdOnly ? m_featureId != kInvalidFeatureId
                                  : m_osmId != kInvalidOsmId && m_featureId != kInvalidFeatureId;
}

// TitleAnchor ------------------------------------------------------------------------------------
TitleAnchor::TitleAnchor(uint8_t minZoom, Anchor anchor) : m_minZoom(minZoom), m_anchor(anchor) {}

bool TitleAnchor::operator==(TitleAnchor const & titleAnchor) const
{
  return m_minZoom == titleAnchor.m_minZoom && m_anchor == titleAnchor.m_anchor;
}

bool TitleAnchor::IsEqualForTesting(TitleAnchor const & titleAnchor) const
{
  return *this == titleAnchor;
}

bool TitleAnchor::IsValid() const
{
  return m_anchor != kInvalidAnchor;
}

// Stop -------------------------------------------------------------------------------------------
Stop::Stop(StopId id, OsmId osmId, FeatureId featureId, TransferId transferId,
           std::vector<LineId> const & lineIds, m2::PointD const & point,
           std::vector<TitleAnchor> const & titleAnchors)
  : m_id(id)
  , m_featureIdentifiers(osmId, featureId, true /* serializeFeatureIdOnly */)
  , m_transferId(transferId)
  , m_lineIds(lineIds)
  , m_point(point)
  , m_titleAnchors(titleAnchors)
{
}

bool Stop::IsEqualForTesting(Stop const & stop) const
{
  double constexpr kPointsEqualEpsilon = 1e-6;
  return m_id == stop.m_id && m_featureIdentifiers == stop.m_featureIdentifiers &&
         m_transferId == stop.m_transferId && m_lineIds == stop.m_lineIds &&
         base::AlmostEqualAbs(m_point, stop.m_point, kPointsEqualEpsilon) &&
         m_titleAnchors == stop.m_titleAnchors;
}

bool Stop::IsValid() const
{
  return m_id.Get() != kInvalidStopId && !m_lineIds.empty();
}

// SingleMwmSegment -------------------------------------------------------------------------------
SingleMwmSegment::SingleMwmSegment(FeatureId featureId, uint32_t segmentIdx, bool forward)
  : m_featureId(featureId), m_segmentIdx(segmentIdx), m_forward(forward)
{
}

bool SingleMwmSegment::IsEqualForTesting(SingleMwmSegment const & s) const
{
  return m_featureId == s.m_featureId && m_segmentIdx == s.m_segmentIdx && m_forward == s.m_forward;
}

bool SingleMwmSegment::IsValid() const
{
  return m_featureId != kInvalidFeatureId;
}

// Gate -------------------------------------------------------------------------------------------
Gate::Gate(OsmId osmId, FeatureId featureId, bool entrance, bool exit, Weight weight,
           std::vector<StopId> const & stopIds, m2::PointD const & point)
  : m_featureIdentifiers(osmId, featureId, false /* serializeFeatureIdOnly */)
  , m_entrance(entrance)
  , m_exit(exit)
  , m_weight(weight)
  , m_stopIds(stopIds)
  , m_point(point)
{
}

bool Gate::operator<(Gate const & rhs) const
{
  if (m_featureIdentifiers != rhs.m_featureIdentifiers)
    return m_featureIdentifiers < rhs.m_featureIdentifiers;

  if (m_entrance != rhs.m_entrance)
    return m_entrance < rhs.m_entrance;

  if (m_exit != rhs.m_exit)
    return m_exit < rhs.m_exit;

  return m_stopIds < rhs.m_stopIds;
}

bool Gate::operator==(Gate const & rhs) const
{
  return m_featureIdentifiers == rhs.m_featureIdentifiers && m_entrance == rhs.m_entrance &&
         m_exit == rhs.m_exit && m_stopIds == rhs.m_stopIds;
}

bool Gate::IsEqualForTesting(Gate const & gate) const
{
  return m_featureIdentifiers == gate.m_featureIdentifiers && m_entrance == gate.m_entrance &&
         m_exit == gate.m_exit && m_weight == gate.m_weight && m_stopIds == gate.m_stopIds &&
         base::AlmostEqualAbs(m_point, gate.m_point, kPointsEqualEpsilon);
}

bool Gate::IsValid() const
{
  return m_featureIdentifiers.GetOsmId() != kInvalidOsmId && m_weight != kInvalidWeight &&
         (m_entrance || m_exit) && !m_stopIds.empty();
}

// ShapeId ----------------------------------------------------------------------------------------
bool ShapeId::operator<(ShapeId const & rhs) const
{
  if (m_stop1Id != rhs.m_stop1Id)
    return m_stop1Id < rhs.m_stop1Id;
  return m_stop2Id < rhs.m_stop2Id;
}

bool ShapeId::operator==(ShapeId const & rhs) const
{
  return m_stop1Id == rhs.m_stop1Id && m_stop2Id == rhs.m_stop2Id;
}

bool ShapeId::IsValid() const
{
  return m_stop1Id != kInvalidStopId && m_stop2Id != kInvalidStopId;
}

// EdgeFlags --------------------------------------------------------------------------------------
uint8_t EdgeFlags::GetFlags() const
{
  return BoolToUint(m_transfer) * kTransferMask +
         BoolToUint(m_isShapeIdsEmpty) * kEmptyShapeIdsMask +
         BoolToUint(m_isShapeIdsSingle) * kSingleShapeIdMask +
         BoolToUint(m_isShapeIdsSame) * kShapeIdIsSameMask +
         BoolToUint(m_isShapeIdsReversed) * kShapeIdIsReversedMask;
}

void EdgeFlags::SetFlags(uint8_t flags)
{
  m_transfer = GetBit(flags, kTransferMask);
  m_isShapeIdsEmpty = GetBit(flags, kEmptyShapeIdsMask);
  m_isShapeIdsSingle = GetBit(flags, kSingleShapeIdMask);
  m_isShapeIdsSame = GetBit(flags, kShapeIdIsSameMask);
  m_isShapeIdsReversed = GetBit(flags, kShapeIdIsReversedMask);
}

std::string DebugPrint(EdgeFlags const & f)
{
  std::ostringstream ss;
  ss << "EdgeFlags [transfer:" << f.m_transfer;
  ss << ", isShapeIdsEmpty:" << f.m_isShapeIdsEmpty;
  ss << ", isShapeIdsSingle:" << f.m_isShapeIdsSingle;
  ss << ", isShapeIdsSame:" << f.m_isShapeIdsSame;
  ss << ", isShapeIdsReversed:" << f.m_isShapeIdsReversed << "]";
  return ss.str();
}

// Edge -------------------------------------------------------------------------------------------
Edge::Edge(StopId stop1Id, StopId stop2Id, Weight weight, LineId lineId, bool transfer,
           std::vector<ShapeId> const & shapeIds)
  : m_stop1Id(stop1Id)
  , m_stop2Id(stop2Id)
  , m_weight(weight)
  , m_lineId(lineId)
  , m_flags(GetEdgeFlags(transfer, stop1Id, stop2Id, shapeIds))
  , m_shapeIds(shapeIds)
{
}

bool Edge::operator<(Edge const & rhs) const
{
  if (m_stop1Id != rhs.m_stop1Id)
    return m_stop1Id < rhs.m_stop1Id;
  if (m_stop2Id != rhs.m_stop2Id)
    return m_stop2Id < rhs.m_stop2Id;
  return m_lineId < rhs.m_lineId;
}

bool Edge::operator==(Edge const & rhs) const
{
  return m_stop1Id == rhs.m_stop1Id && m_stop2Id == rhs.m_stop2Id && m_lineId == rhs.m_lineId;
}

bool Edge::IsEqualForTesting(Edge const & edge) const
{
  return m_stop1Id == edge.m_stop1Id && m_stop2Id == edge.m_stop2Id && m_weight == edge.m_weight &&
         m_lineId == edge.m_lineId && GetTransfer() == edge.GetTransfer() &&
         m_shapeIds == edge.m_shapeIds;
}

bool Edge::IsValid() const
{
  if (GetTransfer() && (m_lineId != kInvalidLineId || !m_shapeIds.empty()))
    return false;

  if (!GetTransfer() && m_lineId == kInvalidLineId)
    return false;

  return m_stop1Id.Get() != kInvalidStopId && m_stop2Id != kInvalidStopId && m_weight != kInvalidWeight;
}

// Transfer ---------------------------------------------------------------------------------------
Transfer::Transfer(StopId id, m2::PointD const & point, std::vector<StopId> const & stopIds,
                   std::vector<TitleAnchor> const & titleAnchors)
  : m_id(id), m_point(point), m_stopIds(stopIds), m_titleAnchors(titleAnchors)
{
}

bool Transfer::IsEqualForTesting(Transfer const & transfer) const
{
  return m_id == transfer.m_id &&
         base::AlmostEqualAbs(m_point, transfer.m_point, kPointsEqualEpsilon) &&
         m_stopIds == transfer.m_stopIds && m_titleAnchors == transfer.m_titleAnchors;
}

bool Transfer::IsValid() const
{
  return m_id != kInvalidStopId && !m_stopIds.empty();
}

// Line -------------------------------------------------------------------------------------------
Line::Line(LineId id, std::string const & number, std::string const & title,
           std::string const & type, std::string const & color, NetworkId networkId,
           Ranges const & stopIds, Weight interval)
  : m_id(id)
  , m_number(number)
  , m_title(title)
  , m_type(type)
  , m_color(color)
  , m_networkId(networkId)
  , m_stopIds(stopIds)
  , m_interval(interval)
{
}

bool Line::IsEqualForTesting(Line const & line) const
{
  return m_id == line.m_id && m_number == line.m_number && m_title == line.m_title &&
         m_type == line.m_type && m_color == line.m_color && m_networkId == line.m_networkId &&
         m_stopIds == line.m_stopIds && m_interval == line.m_interval;
}

bool Line::IsValid() const
{
  return m_id != kInvalidLineId && m_color != kInvalidColor && m_networkId != kInvalidNetworkId &&
         m_stopIds.IsValid() && m_interval != kInvalidWeight;
}

// Shape ------------------------------------------------------------------------------------------
bool Shape::IsEqualForTesting(Shape const & shape) const
{
  if (!m_id.IsEqualForTesting(shape.m_id) || m_polyline.size() != shape.m_polyline.size())
    return false;

  for (size_t i = 0; i < m_polyline.size(); ++i)
  {
    if (!base::AlmostEqualAbs(m_polyline[i], shape.m_polyline[i], kPointsEqualEpsilon))
      return false;
  }
  return true;
}

// Network ----------------------------------------------------------------------------------------
Network::Network(NetworkId id, std::string const & title)
: m_id(id), m_title(title)
{
}

bool Network::IsEqualForTesting(Network const & shape) const
{
  return m_id == shape.m_id && m_title == shape.m_title;
}

bool Network::IsValid() const
{
  return m_id != kInvalidNetworkId;
}

EdgeFlags GetEdgeFlags(bool transfer, StopId stopId1, StopId stopId2,
                       std::vector<ShapeId> const & shapeIds)
{
  EdgeFlags flags;
  flags.m_transfer = transfer;
  flags.m_isShapeIdsEmpty = shapeIds.empty();
  bool const singleShapeId = (shapeIds.size() == 1);
  flags.m_isShapeIdsSingle = singleShapeId;
  if (singleShapeId)
  {
    flags.m_isShapeIdsSame =
        stopId1 == shapeIds[0].GetStop1Id() && stopId2 == shapeIds[0].GetStop2Id();
    flags.m_isShapeIdsReversed =
        stopId1 == shapeIds[0].GetStop2Id() && stopId2 == shapeIds[0].GetStop1Id();
  }
  return flags;
}
}  // namespace transit
}  // namespace routing