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

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

// TODO (@darina) TEMPORARY Remove me
#include "map/bookmark_manager.hpp"

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

#include "storage/index.hpp"

#include "drape/pointers.hpp"

#include "tracking/reporter.hpp"

#include "base/thread_checker.hpp"

#include <functional>
#include <memory>
#include <string>
#include <utility>
#include <vector>

namespace df
{
class DrapeEngine;
}

namespace storage
{
class CountryInfoGetter;
class Storage;
}

namespace routing
{
class NumMwmIds;
}

class Index;

class RoutingManager final
{
public:
  class Delegate
  {
  public:
    virtual void OnRouteFollow(routing::RouterType type) = 0;
    virtual void RegisterCountryFilesOnRoute(std::shared_ptr<routing::NumMwmIds> ptr) const = 0;

    virtual ~Delegate() = default;
  };

  struct Callbacks
  {
    using FeatureIndexGetterFn = std::function<Index &()>;
    using CountryInfoGetterFn = std::function<storage::CountryInfoGetter &()>;
    using VisualizerFn = std::function<void(m2::PointD const &)>;

    Callbacks(FeatureIndexGetterFn && featureIndexGetter, CountryInfoGetterFn && countryInfoGetter,
              VisualizerFn && visualizer)
      : m_featureIndexGetter(std::move(featureIndexGetter))
      , m_countryInfoGetter(std::move(countryInfoGetter))
      , m_visualizer(std::move(visualizer))
    {
    }

    FeatureIndexGetterFn m_featureIndexGetter;
    CountryInfoGetterFn m_countryInfoGetter;
    VisualizerFn m_visualizer;
  };

  using RouteBuildingCallback =
      std::function<void(routing::IRouter::ResultCode, storage::TCountriesVec const &)>;
  using RouteProgressCallback = std::function<void(float)>;

  RoutingManager(Callbacks && callbacks, Delegate & delegate);

  routing::RoutingSession const & RoutingSession() const { return m_routingSession; }
  routing::RoutingSession & RoutingSession() { return m_routingSession; }
  void SetRouter(routing::RouterType type);
  routing::RouterType GetRouter() const { return m_currentRouterType; }
  bool IsRoutingActive() const { return m_routingSession.IsActive(); }
  bool IsRouteBuilt() const { return m_routingSession.IsBuilt(); }
  bool IsRouteBuilding() const { return m_routingSession.IsBuilding(); }
  bool IsRouteRebuildingOnly() const { return m_routingSession.IsRebuildingOnly(); }
  bool IsRouteNotReady() const { return m_routingSession.IsNotReady(); }
  bool IsRouteFinished() const { return m_routingSession.IsFinished(); }
  bool IsOnRoute() const { return m_routingSession.IsOnRoute(); }
  bool IsRoutingFollowing() const { return m_routingSession.IsFollowing(); }
  void BuildRoute(m2::PointD const & finish, uint32_t timeoutSec);
  void BuildRoute(m2::PointD const & start, m2::PointD const & finish, bool isP2P,
                  uint32_t timeoutSec);
  void SetUserCurrentPosition(m2::PointD const & position);
  void ResetRoutingSession() { m_routingSession.Reset(); }
  // FollowRoute has a bug where the router follows the route even if the method hads't been called.
  // This method was added because we do not want to break the behaviour that is familiar to our
  // users.
  bool DisableFollowMode();
  /// @TODO(AlexZ): Warning! These two routing callbacks are the only callbacks which are not called
  /// in the main thread context.
  /// UI code should take it into an account. This is a result of current implementation, that can
  /// be improved:
  /// Drape core calls some RunOnGuiThread with "this" pointers, and it causes crashes on Android,
  /// when Drape engine is destroyed
  /// while switching between activities. Current workaround cleans all callbacks when destroying
  /// Drape engine
  /// (@see MwmApplication.clearFunctorsOnUiThread on Android). Better soulution can be fair copying
  /// of all needed information into
  /// lambdas/functors before calling RunOnGuiThread.
  void SetRouteBuildingListener(RouteBuildingCallback const & buildingCallback)
  {
    m_routingCallback = buildingCallback;
  }
  /// See warning above.
  void SetRouteProgressListener(RouteProgressCallback const & progressCallback)
  {
    m_routingSession.SetProgressCallback(progressCallback);
  }
  void FollowRoute();
  void CloseRouting();
  void GetRouteFollowingInfo(location::FollowingInfo & info) const
  {
    m_routingSession.GetRouteFollowingInfo(info);
  }
  m2::PointD GetRouteEndPoint() const { return m_routingSession.GetEndPoint(); }
  /// Returns the most situable router engine type.
  routing::RouterType GetBestRouter(m2::PointD const & startPoint,
                                    m2::PointD const & finalPoint) const;
  routing::RouterType GetLastUsedRouter() const;
  void SetLastUsedRouter(routing::RouterType type);
  // Sound notifications for turn instructions.
  void EnableTurnNotifications(bool enable) { m_routingSession.EnableTurnNotifications(enable); }
  bool AreTurnNotificationsEnabled() const
  {
    return m_routingSession.AreTurnNotificationsEnabled();
  }
  /// \brief Sets a locale for TTS.
  /// \param locale is a string with locale code. For example "en", "ru", "zh-Hant" and so on.
  /// \note See sound/tts/languages.txt for the full list of available locales.
  void SetTurnNotificationsLocale(std::string const & locale)
  {
    m_routingSession.SetTurnNotificationsLocale(locale);
  }
  /// @return current TTS locale. For example "en", "ru", "zh-Hant" and so on.
  /// In case of error returns an empty string.
  /// \note The method returns correct locale after SetTurnNotificationsLocale has been called.
  /// If not, it returns an empty string.
  std::string GetTurnNotificationsLocale() const
  {
    return m_routingSession.GetTurnNotificationsLocale();
  }
  /// \brief When an end user is going to a turn he gets sound turn instructions.
  /// If C++ part wants the client to pronounce an instruction GenerateTurnNotifications (in
  /// turnNotifications) returns
  /// an array of one of more strings. C++ part assumes that all these strings shall be pronounced
  /// by the client's TTS.
  /// For example if C++ part wants the client to pronounce "Make a right turn." this method returns
  /// an array with one string "Make a right turn.". The next call of the method returns nothing.
  /// GenerateTurnNotifications shall be called by the client when a new position is available.
  void GenerateTurnNotifications(std::vector<std::string> & turnNotifications);

  // TODO (@darina) Change interface to add/remove route point
  void SetRouteStartPoint(BookmarkManager & bm, m2::PointD const & pt, bool isValid);
  void SetRouteFinishPoint(BookmarkManager & bm, m2::PointD const & pt, bool isValid);

  void SetRouterImpl(routing::RouterType type);
  void RemoveRoute(bool deactivateFollowing);

  void CheckLocationForRouting(location::GpsInfo const & info);
  void CallRouteBuilded(routing::IRouter::ResultCode code,
                        storage::TCountriesVec const & absentCountries);
  void OnBuildRouteReady(routing::Route const & route, routing::IRouter::ResultCode code);
  void OnRebuildRouteReady(routing::Route const & route, routing::IRouter::ResultCode code);
  void OnLocationUpdate(location::GpsInfo & info);
  void SetAllowSendingPoints(bool isAllowed)
  {
    m_trackingReporter.SetAllowSendingPoints(isAllowed);
  }

  void SetTurnNotificationsUnits(measurement_utils::Units const units)
  {
    m_routingSession.SetTurnNotificationsUnits(units);
  }
  void SetDrapeEngine(ref_ptr<df::DrapeEngine> engine, bool is3dAllowed);
  /// \returns true if altitude information along |m_route| is available and
  /// false otherwise.
  bool HasRouteAltitude() const;

  /// \brief Generates 4 bytes per point image (RGBA) and put the data to |imageRGBAData|.
  /// \param width is width of chart shall be generated in pixels.
  /// \param height is height of chart shall be generated in pixels.
  /// \param imageRGBAData is bits of result image in RGBA.
  /// \param minRouteAltitude is min altitude along the route in altitudeUnits.
  /// \param maxRouteAltitude is max altitude along the route in altitudeUnits.
  /// \param altitudeUnits is units (meters or feet) which is used to pass min and max altitudes.
  /// \returns If there is valid route info and the chart was generated returns true
  /// and false otherwise. If the method returns true it is guaranteed that the size of
  /// |imageRGBAData| is not zero.
  /// \note If HasRouteAltitude() method returns true, GenerateRouteAltitudeChart(...)
  /// could return false if route was deleted or rebuilt between the calls.
  bool GenerateRouteAltitudeChart(uint32_t width, uint32_t height, std::vector<uint8_t> & imageRGBAData,
                                  int32_t & minRouteAltitude, int32_t & maxRouteAltitude,
                                  measurement_utils::Units & altitudeUnits) const;

private:
  void InsertRoute(routing::Route const & route);
  bool IsTrackingReporterEnabled() const;
  void MatchLocationToRoute(location::GpsInfo & info,
                            location::RouteMatchingInfo & routeMatchingInfo) const;

  RouteBuildingCallback m_routingCallback = nullptr;
  Callbacks m_callbacks;
  ref_ptr<df::DrapeEngine> m_drapeEngine = nullptr;
  routing::RouterType m_currentRouterType = routing::RouterType::Count;
  routing::RoutingSession m_routingSession;
  Delegate & m_delegate;
  tracking::Reporter m_trackingReporter;

  DECLARE_THREAD_CHECKER(m_threadChecker);
};