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

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

#include "base/internal/message.hpp"

#include "std/array.hpp"


namespace
{
using namespace routing::turns;
/// The order is important. Starting with the most frequent tokens according to
/// taginfo.openstreetmap.org we minimize the number of the comparisons in ParseSingleLane().
array<pair<LaneWay, string>, static_cast<size_t>(LaneWay::Count)> const g_laneWayNames = {
    {{LaneWay::Through, "through"},
     {LaneWay::Left, "left"},
     {LaneWay::Right, "right"},
     {LaneWay::None, "none"},
     {LaneWay::SharpLeft, "sharp_left"},
     {LaneWay::SlightLeft, "slight_left"},
     {LaneWay::MergeToRight, "merge_to_right"},
     {LaneWay::MergeToLeft, "merge_to_left"},
     {LaneWay::SlightRight, "slight_right"},
     {LaneWay::SharpRight, "sharp_right"},
     {LaneWay::Reverse, "reverse"}}};
static_assert(g_laneWayNames.size() == static_cast<size_t>(LaneWay::Count),
              "Check the size of g_laneWayNames");

array<pair<TurnDirection, string>, static_cast<size_t>(TurnDirection::Count)> const g_turnNames = {
    {{TurnDirection::NoTurn, "NoTurn"},
     {TurnDirection::GoStraight, "GoStraight"},
     {TurnDirection::TurnRight, "TurnRight"},
     {TurnDirection::TurnSharpRight, "TurnSharpRight"},
     {TurnDirection::TurnSlightRight, "TurnSlightRight"},
     {TurnDirection::TurnLeft, "TurnLeft"},
     {TurnDirection::TurnSharpLeft, "TurnSharpLeft"},
     {TurnDirection::TurnSlightLeft, "TurnSlightLeft"},
     {TurnDirection::UTurn, "UTurn"},
     {TurnDirection::TakeTheExit, "TakeTheExit"},
     {TurnDirection::EnterRoundAbout, "EnterRoundAbout"},
     {TurnDirection::LeaveRoundAbout, "LeaveRoundAbout"},
     {TurnDirection::StayOnRoundAbout, "StayOnRoundAbout"},
     {TurnDirection::StartAtEndOfStreet, "StartAtEndOfStreet"},
     {TurnDirection::ReachedYourDestination, "ReachedYourDestination"}}};
static_assert(g_turnNames.size() == static_cast<size_t>(TurnDirection::Count),
              "Check the size of g_turnNames");
}

namespace routing
{
namespace turns
{
bool TurnGeom::operator==(TurnGeom const & other) const
{
  return m_indexInRoute == other.m_indexInRoute && m_turnIndex == other.m_turnIndex
    && m_points == other.m_points;
}

bool SingleLaneInfo::operator==(SingleLaneInfo const & other) const
{
  return m_lane == other.m_lane && m_isRecommended == other.m_isRecommended;
}

string const GetTurnString(TurnDirection turn)
{
  for (auto const & p : g_turnNames)
  {
    if (p.first == turn)
      return p.second;
  }

  stringstream out;
  out << "unknown TurnDirection (" << static_cast<int>(turn) << ")";
  return out.str();
}

bool IsLeftTurn(TurnDirection t)
{
  return (t >= TurnDirection::TurnLeft && t <= TurnDirection::TurnSlightLeft);
}

bool IsRightTurn(TurnDirection t)
{
  return (t >= TurnDirection::TurnRight && t <= TurnDirection::TurnSlightRight);
}

bool IsLeftOrRightTurn(TurnDirection t)
{
  return IsLeftTurn(t) || IsRightTurn(t);
}

bool IsStayOnRoad(TurnDirection t)
{
  return (t == TurnDirection::GoStraight || t == TurnDirection::StayOnRoundAbout);
}

bool IsGoStraightOrSlightTurn(TurnDirection t)
{
  return (t == TurnDirection::GoStraight || t == TurnDirection::TurnSlightLeft ||
          t == TurnDirection::TurnSlightRight);
}

bool IsLaneWayConformedTurnDirection(LaneWay l, TurnDirection t)
{
  switch (t)
  {
    default:
      return false;
    case TurnDirection::GoStraight:
      return l == LaneWay::Through;
    case TurnDirection::TurnRight:
      return l == LaneWay::Right;
    case TurnDirection::TurnSharpRight:
      return l == LaneWay::SharpRight;
    case TurnDirection::TurnSlightRight:
      return l == LaneWay::SlightRight;
    case TurnDirection::TurnLeft:
      return l == LaneWay::Left;
    case TurnDirection::TurnSharpLeft:
      return l == LaneWay::SharpLeft;
    case TurnDirection::TurnSlightLeft:
      return l == LaneWay::SlightLeft;
    case TurnDirection::UTurn:
      return l == LaneWay::Reverse;
  }
}

bool IsLaneWayConformedTurnDirectionApproximately(LaneWay l, TurnDirection t)
{
  switch (t)
  {
    default:
      return false;
    case TurnDirection::GoStraight:
      return l == LaneWay::Through || l == LaneWay::SlightRight || l == LaneWay::SlightLeft;
    case TurnDirection::TurnRight:
      return l == LaneWay::Right || l == LaneWay::SharpRight || l == LaneWay::SlightRight;
    case TurnDirection::TurnSharpRight:
      return l == LaneWay::SharpRight || l == LaneWay::Right;
    case TurnDirection::TurnSlightRight:
      return l == LaneWay::SlightRight || l == LaneWay::Through || l == LaneWay::Right;
    case TurnDirection::TurnLeft:
      return l == LaneWay::Left || l == LaneWay::SlightLeft || l == LaneWay::SharpLeft;
    case TurnDirection::TurnSharpLeft:
      return l == LaneWay::SharpLeft || l == LaneWay::Left;
    case TurnDirection::TurnSlightLeft:
      return l == LaneWay::SlightLeft || l == LaneWay::Through || l == LaneWay::Left;
    case TurnDirection::UTurn:
      return l == LaneWay::Reverse;
  }
}

void SplitLanes(string const & lanesString, char delimiter, vector<string> & lanes)
{
  lanes.clear();
  istringstream lanesStream(lanesString);
  string token;
  while (getline(lanesStream, token, delimiter))
  {
    lanes.push_back(token);
  }
}

bool ParseSingleLane(string const & laneString, char delimiter, TSingleLane & lane)
{
  lane.clear();
  istringstream laneStream(laneString);
  string token;
  while (getline(laneStream, token, delimiter))
  {
    auto const it = find_if(g_laneWayNames.begin(), g_laneWayNames.end(),
                            [&token](pair<LaneWay, string> const & p)
    {
        return p.second == token;
    });
    if (it == g_laneWayNames.end())
      return false;
    lane.push_back(it->first);
  }
  return true;
}

bool ParseLanes(string lanesString, vector<SingleLaneInfo> & lanes)
{
  if (lanesString.empty())
    return false;
  lanes.clear();
  transform(lanesString.begin(), lanesString.end(), lanesString.begin(), tolower);
  lanesString.erase(remove_if(lanesString.begin(), lanesString.end(), isspace),
                         lanesString.end());

  vector<string> SplitLanesStrings;
  SingleLaneInfo lane;
  SplitLanes(lanesString, '|', SplitLanesStrings);
  for (string const & s : SplitLanesStrings)
  {
    if (!ParseSingleLane(s, ';', lane.m_lane))
    {
      lanes.clear();
      return false;
    }
    lanes.push_back(lane);
  }
  return true;
}

string DebugPrint(TurnGeom const & turnGeom)
{
  stringstream out;
  out << "[ TurnGeom: m_indexInRoute = " << turnGeom.m_indexInRoute
      << ", m_turnIndex = " << turnGeom.m_turnIndex << " ]" << endl;
  return out.str();
}

string DebugPrint(LaneWay const l)
{
  auto const it = find_if(g_laneWayNames.begin(), g_laneWayNames.end(),
                          [&l](pair<LaneWay, string> const & p)
  {
    return p.first == l;
  });

  if (it == g_laneWayNames.end())
  {
    stringstream out;
    out << "unknown LaneWay (" << static_cast<int>(l) << ")";
    return out.str();
  }
  return it->second;
}

string DebugPrint(TurnDirection const turn)
{
  stringstream out;
  out << "[ " << GetTurnString(turn) << " ]";
  return out.str();
}

string DebugPrint(SingleLaneInfo const & singleLaneInfo)
{
  stringstream out;
  out << "SingleLaneInfo [ m_isRecommended == " << singleLaneInfo.m_isRecommended
      << ", m_lane == " << ::DebugPrint(singleLaneInfo.m_lane) << " ]" << endl;
  return out.str();
}

}
}