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

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

#include "routing/base/astar_algorithm.hpp"
#include "routing/base/astar_progress.hpp"
#include "routing/road_graph.hpp"

#include "base/assert.hpp"

#include "geometry/mercator.hpp"

namespace routing
{

namespace
{
uint32_t constexpr kVisitPeriod = 4;
float constexpr kProgressInterval = 2;

double constexpr KMPH2MPS = 1000.0 / (60 * 60);

inline double TimeBetweenSec(Junction const & j1, Junction const & j2, double speedMPS)
{
  ASSERT(speedMPS > 0.0, ());
  ASSERT_NOT_EQUAL(j1.GetAltitude(), feature::kInvalidAltitude, ());
  ASSERT_NOT_EQUAL(j2.GetAltitude(), feature::kInvalidAltitude, ());

  double const distanceM = MercatorBounds::DistanceOnEarth(j1.GetPoint(), j2.GetPoint());
  double const altitudeDiffM =
      static_cast<double>(j2.GetAltitude()) - static_cast<double>(j1.GetAltitude());
  return sqrt(distanceM * distanceM + altitudeDiffM * altitudeDiffM) / speedMPS;
}

/// A class which represents an weighted edge used by RoadGraph.
class WeightedEdge
{
public:
  WeightedEdge(Junction const & target, double weight) : target(target), weight(weight) {}

  inline Junction const & GetTarget() const { return target; }

  inline double GetWeight() const { return weight; }

private:
  Junction const target;
  double const weight;
};

/// A wrapper around IRoadGraph, which makes it possible to use IRoadGraph with astar algorithms.
class RoadGraph
{
public:
  using Vertex = Junction;
  using Edge = WeightedEdge;
  using Weight = double;

  RoadGraph(IRoadGraph const & roadGraph)
    : m_roadGraph(roadGraph)
    , m_maxSpeedMPS(roadGraph.GetMaxSpeedKMPH() * KMPH2MPS)
  {}

  void GetOutgoingEdgesList(Junction const & v, vector<WeightedEdge> & adj) const
  {
    IRoadGraph::TEdgeVector edges;
    m_roadGraph.GetOutgoingEdges(v, edges);

    adj.clear();
    adj.reserve(edges.size());

    for (auto const & e : edges)
    {
      ASSERT_EQUAL(v, e.GetStartJunction(), ());

      double const speedMPS = m_roadGraph.GetSpeedKMPH(e) * KMPH2MPS;
      adj.emplace_back(e.GetEndJunction(), TimeBetweenSec(e.GetStartJunction(), e.GetEndJunction(), speedMPS));
    }
  }

  void GetIngoingEdgesList(Junction const & v, vector<WeightedEdge> & adj) const
  {
    IRoadGraph::TEdgeVector edges;
    m_roadGraph.GetIngoingEdges(v, edges);

    adj.clear();
    adj.reserve(edges.size());

    for (auto const & e : edges)
    {
      ASSERT_EQUAL(v, e.GetEndJunction(), ());

      double const speedMPS = m_roadGraph.GetSpeedKMPH(e) * KMPH2MPS;
      adj.emplace_back(e.GetStartJunction(), TimeBetweenSec(e.GetStartJunction(), e.GetEndJunction(), speedMPS));
    }
  }

  double HeuristicCostEstimate(Junction const & v, Junction const & w) const
  {
    return TimeBetweenSec(v, w, m_maxSpeedMPS);
  }

private:
  IRoadGraph const & m_roadGraph;
  double const m_maxSpeedMPS;
};

typedef AStarAlgorithm<RoadGraph> TAlgorithmImpl;

IRoutingAlgorithm::Result Convert(TAlgorithmImpl::Result value)
{
  switch (value)
  {
  case TAlgorithmImpl::Result::OK: return IRoutingAlgorithm::Result::OK;
  case TAlgorithmImpl::Result::NoPath: return IRoutingAlgorithm::Result::NoPath;
  case TAlgorithmImpl::Result::Cancelled: return IRoutingAlgorithm::Result::Cancelled;
  }
  ASSERT(false, ("Unexpected TAlgorithmImpl::Result value:", value));
  return IRoutingAlgorithm::Result::NoPath;
}
}  // namespace

string DebugPrint(IRoutingAlgorithm::Result const & value)
{
  switch (value)
  {
  case IRoutingAlgorithm::Result::OK:
    return "OK";
  case IRoutingAlgorithm::Result::NoPath:
    return "NoPath";
  case IRoutingAlgorithm::Result::Cancelled:
    return "Cancelled";
  }
  return string();
}

// *************************** AStar routing algorithm implementation *************************************

IRoutingAlgorithm::Result AStarRoutingAlgorithm::CalculateRoute(IRoadGraph const & graph,
                                                                Junction const & startPos,
                                                                Junction const & finalPos,
                                                                RouterDelegate const & delegate,
                                                                RoutingResult<IRoadGraph::Vertex, IRoadGraph::Weight> & path)
{
  AStarProgress progress(0, 95);
  uint32_t visitCount = 0;

  auto onVisitJunctionFn = [&](Junction const & junction, Junction const & /* target */) {
    if (++visitCount % kVisitPeriod != 0)
      return;

    delegate.OnPointCheck(junction.GetPoint());
    auto const lastValue = progress.GetLastValue();
    auto const newValue = progress.GetProgressForDirectedAlgo(junction.GetPoint());
    if (newValue - lastValue > kProgressInterval)
      delegate.OnProgress(newValue);

  };

  base::Cancellable const & cancellable = delegate;
  progress.Initialize(startPos.GetPoint(), finalPos.GetPoint());
  RoadGraph roadGraph(graph);
  TAlgorithmImpl::Params params(roadGraph, startPos, finalPos, nullptr /* prevRoute */, cancellable,
                                onVisitJunctionFn, {} /* checkLength */);
  TAlgorithmImpl::Result const res = TAlgorithmImpl().FindPath(params, path);
  return Convert(res);
}

// *************************** AStar-bidirectional routing algorithm implementation ***********************

IRoutingAlgorithm::Result AStarBidirectionalRoutingAlgorithm::CalculateRoute(
    IRoadGraph const & graph, Junction const & startPos, Junction const & finalPos,
    RouterDelegate const & delegate, RoutingResult<IRoadGraph::Vertex, IRoadGraph::Weight> & path)
{
  AStarProgress progress(0, 95);
  uint32_t visitCount = 0;

  auto onVisitJunctionFn = [&](Junction const & junction, Junction const & target) {
    if (++visitCount % kVisitPeriod != 0)
      return;

    delegate.OnPointCheck(junction.GetPoint());
    auto const lastValue = progress.GetLastValue();
    auto const newValue =
        progress.GetProgressForBidirectedAlgo(junction.GetPoint(), target.GetPoint());
    if (newValue - lastValue > kProgressInterval)
      delegate.OnProgress(newValue);
  };

  base::Cancellable const & cancellable = delegate;
  progress.Initialize(startPos.GetPoint(), finalPos.GetPoint());
  RoadGraph roadGraph(graph);
  TAlgorithmImpl::Params params(roadGraph, startPos, finalPos, {} /* prevRoute */, cancellable,
                                onVisitJunctionFn, {} /* checkLength */);
  TAlgorithmImpl::Result const res = TAlgorithmImpl().FindPathBidirectional(params, path);
  return Convert(res);
}

}  // namespace routing