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

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

#include "traffic/traffic_info.hpp"

#include "indexer/ftypes_matcher.hpp"
#include "indexer/index.hpp"
#include "indexer/scales.hpp"

#include "geometry/point2d.hpp"

namespace
{
using namespace routing;
using namespace routing::turns;
using namespace traffic;

class RoutingResult : public IRoutingResult
{
public:
  RoutingResult(IRoadGraph::TEdgeVector const & routeEdges,
                BicycleDirectionsEngine::TAdjacentEdgesMap const & adjacentEdges,
                TUnpackedPathSegments const & pathSegments)
    : m_routeEdges(routeEdges)
    , m_adjacentEdges(adjacentEdges)
    , m_pathSegments(pathSegments)
    , m_routeLength(0)
  {
    for (auto const & edge : routeEdges)
    {
      m_routeLength += MercatorBounds::DistanceOnEarth(edge.GetStartJunction().GetPoint(),
                                                       edge.GetEndJunction().GetPoint());
    }
  }

  // turns::IRoutingResult overrides:
  TUnpackedPathSegments const & GetSegments() const override { return m_pathSegments; }

  void GetPossibleTurns(TNodeId node, m2::PointD const & /* ingoingPoint */,
                        m2::PointD const & /* junctionPoint */, size_t & ingoingCount,
                        TurnCandidates & outgoingTurns) const override
  {
    ingoingCount = 0;
    outgoingTurns.candidates.clear();

    auto const adjacentEdges = m_adjacentEdges.find(node);
    if (adjacentEdges == m_adjacentEdges.cend())
    {
      ASSERT(false, ());
      return;
    }

    ingoingCount = adjacentEdges->second.m_ingoingTurnsCount;
    outgoingTurns = adjacentEdges->second.m_outgoingTurns;
  }

  double GetPathLength() const override { return m_routeLength; }

  Junction GetStartPoint() const override
  {
    CHECK(!m_routeEdges.empty(), ());
    return m_routeEdges.front().GetStartJunction();
  }

  Junction GetEndPoint() const override
  {
    CHECK(!m_routeEdges.empty(), ());
    return m_routeEdges.back().GetEndJunction();
  }

private:
  IRoadGraph::TEdgeVector const & m_routeEdges;
  BicycleDirectionsEngine::TAdjacentEdgesMap const & m_adjacentEdges;
  TUnpackedPathSegments const & m_pathSegments;
  double m_routeLength;
};
}  // namespace

namespace routing
{
BicycleDirectionsEngine::BicycleDirectionsEngine(Index const & index) : m_index(index) {}

void BicycleDirectionsEngine::Generate(IRoadGraph const & graph, vector<Junction> const & path,
                                       Route::TTimes & times, Route::TTurns & turns,
                                       vector<Junction> & routeGeometry,
                                       vector<TrafficInfo::RoadSegmentId> & trafficSegs,
                                       my::Cancellable const & cancellable)
{
  times.clear();
  turns.clear();
  routeGeometry.clear();
  m_adjacentEdges.clear();
  m_pathSegments.clear();
  trafficSegs.clear();

  size_t const pathSize = path.size();
  if (pathSize == 0)
    return;

  auto emptyPathWorkaround = [&]()
  {
    turns.emplace_back(pathSize - 1, turns::TurnDirection::ReachedYourDestination);
    this->m_adjacentEdges[0] = AdjacentEdges(1);  // There's one ingoing edge to the finish.
  };

  if (pathSize == 1)
  {
    emptyPathWorkaround();
    return;
  }

  CalculateTimes(graph, path, times);

  IRoadGraph::TEdgeVector routeEdges;
  if (!ReconstructPath(graph, path, routeEdges, cancellable))
  {
    LOG(LDEBUG, ("Couldn't reconstruct path."));
    emptyPathWorkaround();
    return;
  }
  if (routeEdges.empty())
  {
    emptyPathWorkaround();
    return;
  }

  // Filling |m_adjacentEdges|.
  m_adjacentEdges.insert(make_pair(0, AdjacentEdges(0)));
  for (size_t i = 1; i < pathSize; ++i)
  {
    if (cancellable.IsCancelled())
      return;

    Junction const & prevJunction = path[i - 1];
    Junction const & currJunction = path[i];
    IRoadGraph::TEdgeVector outgoingEdges, ingoingEdges;
    graph.GetOutgoingEdges(currJunction, outgoingEdges);
    graph.GetIngoingEdges(currJunction, ingoingEdges);

    AdjacentEdges adjacentEdges = AdjacentEdges(ingoingEdges.size());
    // Outgoing edge angle is not used for bicyle routing.
    adjacentEdges.m_outgoingTurns.isCandidatesAngleValid = false;
    adjacentEdges.m_outgoingTurns.candidates.reserve(outgoingEdges.size());
    ASSERT_EQUAL(routeEdges.size(), pathSize - 1, ());
    FeatureID const inFeatureId = routeEdges[i - 1].GetFeatureId();

    for (auto const & edge : outgoingEdges)
    {
      auto const & outFeatureId = edge.GetFeatureId();
      // Checking for if |edge| is a fake edge.
      if (!outFeatureId.IsValid())
        continue;

      FeatureType ft;
      if (!GetLoader(outFeatureId.m_mwmId).GetFeatureByIndex(outFeatureId.m_index, ft))
        continue;

      auto const highwayClass = ftypes::GetHighwayClass(ft);
      ASSERT_NOT_EQUAL(highwayClass, ftypes::HighwayClass::Error, ());
      ASSERT_NOT_EQUAL(highwayClass, ftypes::HighwayClass::Undefined, ());
      adjacentEdges.m_outgoingTurns.candidates.emplace_back(0.0 /* angle */, outFeatureId.m_index,
                                                            highwayClass);
    }

    LoadedPathSegment pathSegment;
    // @TODO(bykoianko) This place should be fixed. Putting |prevJunction| and |currJunction|
    // for every route edge leads that all route points are duplicated. It's because
    // prevJunction == path[i - 1] and currJunction == path[i].
    if (inFeatureId.IsValid())
      LoadPathGeometry(inFeatureId, {prevJunction, currJunction}, pathSegment);
    pathSegment.m_trafficSegs = {
      {inFeatureId.m_index,
       static_cast<uint16_t>(routeEdges[i - 1].GetSegId()),
       routeEdges[i - 1].IsForward() ? TrafficInfo::RoadSegmentId::kForwardDirection
                                     : TrafficInfo::RoadSegmentId::kReverseDirection
      }};

    m_adjacentEdges.insert(make_pair(inFeatureId.m_index, move(adjacentEdges)));
    m_pathSegments.push_back(move(pathSegment));
  }

  RoutingResult resultGraph(routeEdges, m_adjacentEdges, m_pathSegments);
  RouterDelegate delegate;
  Route::TTimes turnAnnotationTimes;
  Route::TStreets streetNames;

  MakeTurnAnnotation(resultGraph, delegate, routeGeometry, turns, turnAnnotationTimes,
                     streetNames, trafficSegs);

  // @TODO(bykoianko) The invariant below it's an issue but now it's so and it should be checked.
  // The problem is every edge is added as a pair of points to route geometry.
  // So all the points except for beginning and ending are duplicated. It should
  // be fixed in the future.

  // Note 1. Accoding to current implementation all internal points are duplicated for
  // all A* routes.
  // Note 2. Number of |trafficSegs| should be equal to number of |routeEdges|.
  ASSERT_EQUAL(routeGeometry.size(), 2 * (pathSize - 2) + 2, ());
  ASSERT_EQUAL(trafficSegs.size(), pathSize - 1, ());
}

Index::FeaturesLoaderGuard & BicycleDirectionsEngine::GetLoader(MwmSet::MwmId const & id)
{
  if (!m_loader || id != m_loader->GetId())
    m_loader = make_unique<Index::FeaturesLoaderGuard>(m_index, id);
  return *m_loader;
}

void BicycleDirectionsEngine::LoadPathGeometry(FeatureID const & featureId,
                                               vector<Junction> const & path,
                                               LoadedPathSegment & pathSegment)
{
  pathSegment.Clear();

  if (!featureId.IsValid())
  {
    ASSERT(false, ());
    return;
  }

  FeatureType ft;
  if (!GetLoader(featureId.m_mwmId).GetFeatureByIndex(featureId.m_index, ft))
  {
    // The feature can't be read, therefore path geometry can't be
    // loaded.
    return;
  }

  auto const highwayClass = ftypes::GetHighwayClass(ft);
  ASSERT_NOT_EQUAL(highwayClass, ftypes::HighwayClass::Error, ());
  ASSERT_NOT_EQUAL(highwayClass, ftypes::HighwayClass::Undefined, ());

  pathSegment.m_highwayClass = highwayClass;
  pathSegment.m_isLink = ftypes::IsLinkChecker::Instance()(ft);

  ft.GetName(FeatureType::DEFAULT_LANG, pathSegment.m_name);

  pathSegment.m_nodeId = featureId.m_index;
  pathSegment.m_onRoundabout = ftypes::IsRoundAboutChecker::Instance()(ft);
  pathSegment.m_path = path;
  // @TODO(bykoianko) It's better to fill pathSegment.m_weight.
}
}  // namespace routing