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

cross_routing_context.cpp « routing - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f9fb4bc724637f7b75ccf21cf21dd1a087e4a4cb (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
#include "routing/cross_routing_context.hpp"

#include "geometry/mercator.hpp"
#include "indexer/point_to_int64.hpp"

namespace
{
uint32_t constexpr kCoordBits = POINT_COORD_BITS;

double constexpr kMwmCrossingNodeEqualityRadiusDegrees = 0.001;
}  // namespace

namespace routing
{
void OutgoingCrossNode::Save(Writer & w) const
{
  uint64_t point = PointToInt64(m2::PointD(m_point.lon, m_point.lat), kCoordBits);
  char buff[sizeof(m_nodeId) + sizeof(point) + sizeof(m_outgoingIndex)];
  *reinterpret_cast<decltype(m_nodeId) *>(&buff[0]) = m_nodeId;
  *reinterpret_cast<decltype(point) *>(&(buff[sizeof(m_nodeId)])) = point;
  *reinterpret_cast<decltype(m_outgoingIndex) *>(&(buff[sizeof(m_nodeId) + sizeof(point)])) = m_outgoingIndex;
  w.Write(buff, sizeof(buff));
}

size_t OutgoingCrossNode::Load(const Reader & r, size_t pos, size_t adjacencyIndex)
{
  char buff[sizeof(m_nodeId) + sizeof(uint64_t) + sizeof(m_outgoingIndex)];
  r.Read(pos, buff, sizeof(buff));
  m_nodeId = *reinterpret_cast<decltype(m_nodeId) *>(&buff[0]);
  m2::PointD bufferPoint = Int64ToPoint(*reinterpret_cast<uint64_t *>(&(buff[sizeof(m_nodeId)])), kCoordBits);
  m_point = ms::LatLon(bufferPoint.y, bufferPoint.x);
  m_outgoingIndex = *reinterpret_cast<decltype(m_outgoingIndex) *>(&(buff[sizeof(m_nodeId) + sizeof(uint64_t)]));
  m_adjacencyIndex = adjacencyIndex;
  return pos + sizeof(buff);
}

void IngoingCrossNode::Save(Writer & w) const
{
  uint64_t point = PointToInt64(m2::PointD(m_point.lon, m_point.lat), kCoordBits);
  char buff[sizeof(m_nodeId) + sizeof(point)];
  *reinterpret_cast<decltype(m_nodeId) *>(&buff[0]) = m_nodeId;
  *reinterpret_cast<decltype(point) *>(&(buff[sizeof(m_nodeId)])) = point;
  w.Write(buff, sizeof(buff));
}

size_t IngoingCrossNode::Load(const Reader & r, size_t pos, size_t adjacencyIndex)
{
  char buff[sizeof(m_nodeId) + sizeof(uint64_t)];
  r.Read(pos, buff, sizeof(buff));
  m_nodeId = *reinterpret_cast<decltype(m_nodeId) *>(&buff[0]);
  m2::PointD bufferPoint = Int64ToPoint(*reinterpret_cast<uint64_t *>(&(buff[sizeof(m_nodeId)])), kCoordBits);
  m_point = ms::LatLon(bufferPoint.y, bufferPoint.x);
  m_adjacencyIndex = adjacencyIndex;
  return pos + sizeof(buff);
}

void CrossRoutingContextReader::Load(Reader const & r)
{
  size_t pos = 0;

  uint32_t size, ingoingSize;
  r.Read(pos, &ingoingSize, sizeof(ingoingSize));
  pos += sizeof(ingoingSize);

  for (size_t i = 0; i < ingoingSize; ++i)
  {
    IngoingCrossNode node;
    pos = node.Load(r, pos, i);
    m_ingoingIndex.Add(node);
  }

  r.Read(pos, &size, sizeof(size));
  pos += sizeof(size);
  m_outgoingNodes.resize(size);

  for (size_t i = 0; i < size; ++i)
    pos = m_outgoingNodes[i].Load(r, pos, i);

  size_t adjacencySize = ingoingSize * m_outgoingNodes.size();
  size_t const adjMatrixSize = sizeof(TWrittenEdgeWeight) * adjacencySize;
  m_adjacencyMatrix.resize(adjacencySize);
  r.Read(pos, &m_adjacencyMatrix[0], adjMatrixSize);
  pos += adjMatrixSize;

  uint32_t strsize;
  r.Read(pos, &strsize, sizeof(strsize));
  pos += sizeof(strsize);
  for (uint32_t i = 0; i < strsize; ++i)
  {
    r.Read(pos, &size, sizeof(size));
    pos += sizeof(size);
    vector<char> buffer(size);
    r.Read(pos, &buffer[0], size);
    m_neighborMwmList.push_back(string(&buffer[0], size));
    pos += size;
  }
}

bool CrossRoutingContextReader::ForEachIngoingNodeNearPoint(ms::LatLon const & point, function<void(IngoingCrossNode const & node)> && fn) const
{
  bool found = false;
  m_ingoingIndex.ForEachInRect(m2::RectD(point.lat - kMwmCrossingNodeEqualityRadiusDegrees,
                                     point.lon - kMwmCrossingNodeEqualityRadiusDegrees,
                                     point.lat + kMwmCrossingNodeEqualityRadiusDegrees,
                                     point.lon + kMwmCrossingNodeEqualityRadiusDegrees),
                               [&found, &fn](IngoingCrossNode const & node)
                               {
                                 fn(node);
                                 found = true;
                               });
  return found;
}

const string & CrossRoutingContextReader::GetOutgoingMwmName(
    OutgoingCrossNode const & outgoingNode) const
{
  ASSERT(outgoingNode.m_outgoingIndex < m_neighborMwmList.size(),
         ("Routing context out of size mwm name index:", outgoingNode.m_outgoingIndex,
          m_neighborMwmList.size()));
  return m_neighborMwmList[outgoingNode.m_outgoingIndex];
}

TWrittenEdgeWeight CrossRoutingContextReader::GetAdjacencyCost(IngoingCrossNode const & ingoing,
                                                              OutgoingCrossNode const & outgoing) const
{
  if (ingoing.m_adjacencyIndex == kInvalidAdjacencyIndex ||
      outgoing.m_adjacencyIndex == kInvalidAdjacencyIndex)
    return kInvalidContextEdgeWeight;

  size_t cost_index = m_outgoingNodes.size() * ingoing.m_adjacencyIndex + outgoing.m_adjacencyIndex;
  return cost_index < m_adjacencyMatrix.size() ? m_adjacencyMatrix[cost_index] : kInvalidContextEdgeWeight;
}

void CrossRoutingContextWriter::Save(Writer & w) const
{
  uint32_t size = static_cast<uint32_t>(m_ingoingNodes.size());
  w.Write(&size, sizeof(size));
  for (auto const & node : m_ingoingNodes)
    node.Save(w);

  size = static_cast<uint32_t>(m_outgoingNodes.size());
  w.Write(&size, sizeof(size));

  for (auto const & node : m_outgoingNodes)
    node.Save(w);

  CHECK(m_adjacencyMatrix.size() == m_outgoingNodes.size() * m_ingoingNodes.size(), ());
  w.Write(&m_adjacencyMatrix[0], sizeof(m_adjacencyMatrix[0]) * m_adjacencyMatrix.size());

  size = static_cast<uint32_t>(m_neighborMwmList.size());
  w.Write(&size, sizeof(size));
  for (string const & neighbor : m_neighborMwmList)
  {
    size = static_cast<uint32_t>(neighbor.size());
    w.Write(&size, sizeof(size));
    w.Write(neighbor.c_str(), neighbor.size());
  }
}

void CrossRoutingContextWriter::AddIngoingNode(TWrittenNodeId const nodeId, ms::LatLon const & point)
{
  size_t const adjIndex = m_ingoingNodes.size();
  m_ingoingNodes.emplace_back(nodeId, point, adjIndex);
}

void CrossRoutingContextWriter::AddOutgoingNode(TWrittenNodeId const nodeId, string const & targetMwm,
                                                ms::LatLon const & point)
{
  size_t const adjIndex = m_outgoingNodes.size();
  auto it = find(m_neighborMwmList.begin(), m_neighborMwmList.end(), targetMwm);
  if (it == m_neighborMwmList.end())
    it = m_neighborMwmList.insert(m_neighborMwmList.end(), targetMwm);
  m_outgoingNodes.emplace_back(nodeId, distance(m_neighborMwmList.begin(), it), point, adjIndex);
}

void CrossRoutingContextWriter::ReserveAdjacencyMatrix()
{
  m_adjacencyMatrix.resize(m_ingoingNodes.size() * m_outgoingNodes.size(),
                           kInvalidContextEdgeWeight);
}

void CrossRoutingContextWriter::SetAdjacencyCost(IngoingEdgeIteratorT ingoing,
                                                 OutgoingEdgeIteratorT outgoing,
                                                 TWrittenEdgeWeight value)
{
  size_t const index = m_outgoingNodes.size() * ingoing->m_adjacencyIndex + outgoing->m_adjacencyIndex;
  ASSERT_LESS(index, m_adjacencyMatrix.size(), ());
  m_adjacencyMatrix[index] = value;
}

pair<IngoingEdgeIteratorT, IngoingEdgeIteratorT> CrossRoutingContextWriter::GetIngoingIterators()
    const
{
  return make_pair(m_ingoingNodes.cbegin(), m_ingoingNodes.cend());
}

pair<OutgoingEdgeIteratorT, OutgoingEdgeIteratorT> CrossRoutingContextWriter::GetOutgoingIterators()
    const
{
  return make_pair(m_outgoingNodes.cbegin(), m_outgoingNodes.cend());
}
}