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

speed_camera_ser_des.hpp « routing - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4bea65657093803e4fda97e224d3ddf1b8227609 (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
#pragma once

#include "routing/route.hpp"
#include "routing/routing_session.hpp"
#include "routing/segment.hpp"
#include "routing/speed_camera.hpp"

#include "coding/file_container.hpp"
#include "coding/file_writer.hpp"
#include "coding/point_coding.hpp"
#include "coding/reader.hpp"
#include "coding/varint.hpp"
#include "coding/write_to_sink.hpp"

#include "geometry/point2d.hpp"

#include "base/assert.hpp"

#include <cstdint>
#include <limits>
#include <map>
#include <string>
#include <utility>
#include <vector>

namespace routing
{
static uint8_t constexpr kMaxCameraSpeedKmpH = std::numeric_limits<uint8_t>::max();

/// \brief |m_featureId| and |m_segmentId| identify the place where the camera is located.
/// |m_coef| is a factor [0, 1] where the camera is placed at the segment. |m_coef| == 0
/// means the camera is placed at the beginning of the segment.
struct SpeedCameraMwmPosition
{
  SpeedCameraMwmPosition() = default;
  SpeedCameraMwmPosition(uint32_t fId, uint32_t sId, double k)
    : m_featureId(fId), m_segmentId(sId), m_coef(k) {}

  uint32_t m_featureId = 0;
  uint32_t m_segmentId = 0;
  double m_coef = 0.0;
};

// Don't touch the order of enum (this is the part of mwm).
enum class SpeedCameraDirection
{
  Unknown = 0,
  Forward = 1,
  Backward = 2,
  Both = 3
};

struct SpeedCameraMetadata
{
  SpeedCameraMetadata() = default;
  SpeedCameraMetadata(m2::PointD const & center, uint8_t maxSpeed,
                      std::vector<routing::SpeedCameraMwmPosition> && ways)
    : m_center(center), m_maxSpeedKmPH(maxSpeed), m_ways(std::move(ways)) {}

  m2::PointD m_center;
  uint8_t m_maxSpeedKmPH = 0;
  std::vector<routing::SpeedCameraMwmPosition> m_ways;
  SpeedCameraDirection m_direction = SpeedCameraDirection::Unknown;
};

class SpeedCameraMwmHeader
{
public:
  static uint32_t constexpr kLatestVersion = 0;

  void SetVersion(uint32_t version) { m_version = version; }
  void SetAmount(uint32_t amount) { m_amount = amount; }
  uint32_t GetAmount() const { return m_amount; }

  template <typename T>
  void Serialize(T & sink) const
  {
    WriteToSink(sink, m_version);
    WriteToSink(sink, m_amount);
  }

  template <typename T>
  void Deserialize(T & sink)
  {
    ReadPrimitiveFromSource(sink, m_version);
    ReadPrimitiveFromSource(sink, m_amount);
  }

  bool IsValid() const { return m_version <= kLatestVersion; }

private:
  uint32_t m_version = 0;
  uint32_t m_amount = 0;
};

static_assert(sizeof(SpeedCameraMwmHeader) == 8, "Strange size of speed camera section header");

struct SegmentCoord
{
  SegmentCoord() = default;
  SegmentCoord(uint32_t fId, uint32_t sId) : m_featureId(fId), m_segmentId(sId) {}

  bool operator<(SegmentCoord const & rhs) const
  {
    if (m_featureId != rhs.m_featureId)
      return m_featureId < rhs.m_featureId;

    return m_segmentId < rhs.m_segmentId;
  }

  uint32_t m_featureId = 0;
  uint32_t m_segmentId = 0;
};

void SerializeSpeedCamera(FileWriter & writer, SpeedCameraMetadata const & data,
                          uint32_t & prevFeatureId);

template <typename Reader>
std::pair<SegmentCoord, RouteSegment::SpeedCamera> DeserializeSpeedCamera(
  ReaderSource<Reader> & src, uint32_t & prevFeatureId)
{
  auto featureId = ReadVarUint<uint32_t>(src);
  featureId += prevFeatureId;  // delta coding
  prevFeatureId = featureId;

  auto const segmentId = ReadVarUint<uint32_t>(src);

  uint32_t coefInt = 0;
  ReadPrimitiveFromSource(src, coefInt);
  double const coef = Uint32ToDouble(coefInt, 0.0 /* min */, 1.0 /* max */, 32 /* bits */);

  uint8_t speed = 0;
  ReadPrimitiveFromSource(src, speed);
  CHECK_LESS(speed, kMaxCameraSpeedKmpH, ());
  if (speed == 0)
    speed = routing::SpeedCameraOnRoute::kNoSpeedInfo;

  // We don't use direction of camera, because of bad data in OSM.
  UNUSED_VALUE(ReadPrimitiveFromSource<uint8_t>(src));  // direction

  // Number of time conditions of camera.
  auto const conditionsNumber = ReadVarUint<uint32_t>(src);
  CHECK_EQUAL(conditionsNumber, 0,
              ("Number of conditions should be 0, non zero number is not implemented now"));

  return {{featureId, segmentId} /* SegmentCoord */,
          {coef, speed}          /* RouteSegment::SpeedCamera */};
}

template <typename Reader>
void DeserializeSpeedCamsFromMwm(
  ReaderSource<Reader> & src,
  std::map<SegmentCoord, std::vector<RouteSegment::SpeedCamera>> & map)
{
  SpeedCameraMwmHeader header;
  header.Deserialize(src);
  CHECK(header.IsValid(), ("Bad header of speed cam section"));
  uint32_t const amount = header.GetAmount();

  SegmentCoord segment;
  RouteSegment::SpeedCamera speedCamera;
  uint32_t prevFeatureId = 0;
  for (uint32_t i = 0; i < amount; ++i)
  {
    std::tie(segment, speedCamera) = DeserializeSpeedCamera(src, prevFeatureId);
    map[segment].emplace_back(speedCamera);
  }
}

std::string DebugPrint(SegmentCoord const & segment);
}  // namespace routing