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

routing_session.cpp « routing - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cca937ec48303ef7144f92342f0b92643d04ab81 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#include "routing_session.hpp"

#include "speed_camera.hpp"

#include "geometry/mercator.hpp"

#include "platform/location.hpp"
#include "platform/measurement_utils.hpp"
#include "platform/platform.hpp"

#include "coding/internal/file_data.hpp"

#include "3party/Alohalytics/src/alohalytics.h"

using namespace location;

namespace
{

int constexpr kOnRouteMissedCount = 5;

// @TODO(vbykoianko) The distance should depend on the current speed.
double constexpr kShowLanesDistInMeters = 500.;

// @todo(kshalnev) The distance may depend on the current speed.
double constexpr kShowPedestrianTurnInMeters = 5.;

double constexpr kRunawayDistanceSensitivityMeters = 0.01;

// Minimal distance to speed camera to make sound bell on overspeed.
double constexpr kSpeedCameraMinimalWarningMeters = 200.;
// Seconds to warning user before speed camera for driving with current speed.
double constexpr kSpeedCameraWarningSeconds = 30;

double constexpr kKmHToMps = 1000. / 3600.;

double constexpr kInvalidSpeedCameraDistance = -1;

// It limits depth of a speed camera point lookup along the route to avoid freezing.
size_t constexpr kSpeedCameraLookAheadCount = 50;

double constexpr kCompletionPercentAccuracy = 5;

uint32_t constexpr kMinimumETASec = 60;
}  // namespace

namespace routing
{
RoutingSession::RoutingSession()
  : m_router(nullptr)
  , m_route(make_shared<Route>(string()))
  , m_state(RoutingNotActive)
  , m_isFollowing(false)
  , m_endPoint(m2::PointD::Zero())
  , m_lastWarnedSpeedCameraIndex(0)
  , m_lastCheckedSpeedCameraIndex(0)
  , m_speedWarningSignal(false)
  , m_passedDistanceOnRouteMeters(0.0)
  , m_lastCompletionPercent(0.0)
{
}

void RoutingSession::Init(TRoutingStatisticsCallback const & routingStatisticsFn,
                          RouterDelegate::TPointCheckCallback const & pointCheckCallback)
{
  ASSERT(m_router == nullptr, ());
  m_router.reset(new AsyncRouter(routingStatisticsFn, pointCheckCallback));
}

void RoutingSession::BuildRoute(m2::PointD const & startPoint, m2::PointD const & endPoint,
                                TReadyCallback const & readyCallback,
                                TProgressCallback const & progressCallback,
                                uint32_t timeoutSec)
{
  ASSERT(m_router != nullptr, ());
  m_lastGoodPosition = startPoint;
  m_endPoint = endPoint;
  m_router->ClearState();
  m_isFollowing = false;
  m_routingRebuildCount = -1; // -1 for the first rebuild.
  RebuildRoute(startPoint, readyCallback, progressCallback, timeoutSec, RouteBuilding);
}

void RoutingSession::RebuildRoute(m2::PointD const & startPoint,
    TReadyCallback const & readyCallback,
    TProgressCallback const & progressCallback, uint32_t timeoutSec, State routeRebuildingState)
{
  ASSERT(m_router != nullptr, ());
  ASSERT_NOT_EQUAL(m_endPoint, m2::PointD::Zero(), ("End point was not set"));
  RemoveRoute();
  SetState(routeRebuildingState);
  m_routingRebuildCount++;
  m_lastCompletionPercent = 0;

  // Use old-style callback construction, because lambda constructs buggy function on Android
  // (callback param isn't captured by value).
  m_router->CalculateRoute(startPoint, startPoint - m_lastGoodPosition, m_endPoint,
                           DoReadyCallback(*this, readyCallback, m_routeSessionMutex),
                           progressCallback, timeoutSec);
}

void RoutingSession::DoReadyCallback::operator()(Route & route, IRouter::ResultCode e)
{
  threads::MutexGuard guard(m_routeSessionMutexInner);
  UNUSED_VALUE(guard);

  ASSERT(m_rs.m_route, ());

  if (e != IRouter::NeedMoreMaps)
  {
    m_rs.AssignRoute(route, e);
  }
  else
  {
    for (string const & country : route.GetAbsentCountries())
      m_rs.m_route->AddAbsentCountry(country);
  }

  m_callback(*m_rs.m_route, e);
}

void RoutingSession::RemoveRouteImpl()
{
  SetState(RoutingNotActive);
  m_lastDistance = 0.0;
  m_moveAwayCounter = 0;
  m_turnNotificationsMgr.Reset();

  m_route = make_shared<Route>(string());
}

void RoutingSession::RemoveRoute()
{
  threads::MutexGuard guard(m_routeSessionMutex);
  UNUSED_VALUE(guard);

  RemoveRouteImpl();
}

void RoutingSession::Reset()
{
  ASSERT(m_router != nullptr, ());

  threads::MutexGuard guard(m_routeSessionMutex);
  UNUSED_VALUE(guard);

  RemoveRouteImpl();
  m_router->ClearState();

  m_passedDistanceOnRouteMeters = 0.0;
  m_lastWarnedSpeedCameraIndex = 0;
  m_lastCheckedSpeedCameraIndex = 0;
  m_lastFoundCamera = SpeedCameraRestriction();
  m_speedWarningSignal = false;
  m_isFollowing = false;
  m_lastCompletionPercent = 0;
}

RoutingSession::State RoutingSession::OnLocationPositionChanged(GpsInfo const & info, Index const & index)
 {
  ASSERT(m_state != RoutingNotActive, ());
  ASSERT(m_router != nullptr, ());

  if (m_state == RouteNeedRebuild || m_state == RouteFinished
      || m_state == RouteBuilding || m_state == RouteRebuilding
      || m_state == RouteNotReady || m_state == RouteNoFollowing)
    return m_state;

  threads::MutexGuard guard(m_routeSessionMutex);
  UNUSED_VALUE(guard);
  ASSERT(m_route, ());
  ASSERT(m_route->IsValid(), ());

  m_turnNotificationsMgr.SetSpeedMetersPerSecond(info.m_speed);

  if (m_route->MoveIterator(info))
  {
    m_moveAwayCounter = 0;
    m_lastDistance = 0.0;

    if (m_route->IsCurrentOnEnd())
    {
      m_passedDistanceOnRouteMeters += m_route->GetTotalDistanceMeters();
      SetState(RouteFinished);

      alohalytics::TStringMap params = {{"router", m_route->GetRouterId()},
                                        {"passedDistance", strings::to_string(m_passedDistanceOnRouteMeters)},
                                        {"rebuildCount", strings::to_string(m_routingRebuildCount)}};
      alohalytics::LogEvent("RouteTracking_ReachedDestination", params);
    }
    else
    {
      SetState(OnRoute);

      // Warning signals checks
      if (m_routingSettings.m_speedCameraWarning && !m_speedWarningSignal)
      {
        double const warningDistanceM = max(kSpeedCameraMinimalWarningMeters,
                                            info.m_speed * kSpeedCameraWarningSeconds);
        SpeedCameraRestriction cam(0, 0);
        double const camDistance = GetDistanceToCurrentCamM(cam, index);
        if (kInvalidSpeedCameraDistance != camDistance && camDistance < warningDistanceM)
        {
          if (cam.m_index > m_lastWarnedSpeedCameraIndex && info.m_speed > cam.m_maxSpeedKmH * kKmHToMps)
          {
            m_speedWarningSignal = true;
            m_lastWarnedSpeedCameraIndex = cam.m_index;
          }
        }
      }
    }
    m_lastGoodPosition = m_userCurrentPosition;
  }
  else
  {
    // Distance from the last known projection on route
    // (check if we are moving far from the last known projection).
    auto const & lastGoodPoint = m_route->GetFollowedPolyline().GetCurrentIter().m_pt;
    double const dist = MercatorBounds::DistanceOnEarth(lastGoodPoint,
                                                        MercatorBounds::FromLatLon(info.m_latitude, info.m_longitude));
    if (my::AlmostEqualAbs(dist, m_lastDistance, kRunawayDistanceSensitivityMeters))
        return m_state;
    if (dist > m_lastDistance)
    {
      ++m_moveAwayCounter;
      m_lastDistance = dist;
    }

    if (m_moveAwayCounter > kOnRouteMissedCount)
    {
      m_passedDistanceOnRouteMeters += m_route->GetCurrentDistanceFromBeginMeters();
      SetState(RouteNeedRebuild);
      alohalytics::TStringMap params = {
          {"router", m_route->GetRouterId()},
          {"percent", strings::to_string(GetCompletionPercent())},
          {"passedDistance", strings::to_string(m_passedDistanceOnRouteMeters)},
          {"rebuildCount", strings::to_string(m_routingRebuildCount)}};
      alohalytics::LogEvent(
          "RouteTracking_RouteNeedRebuild", params,
          alohalytics::Location::FromLatLon(MercatorBounds::YToLat(lastGoodPoint.y),
                                            MercatorBounds::XToLon(lastGoodPoint.x)));
    }
  }

  return m_state;
}

void RoutingSession::GetRouteFollowingInfo(FollowingInfo & info) const
{
  auto formatDistFn = [](double dist, string & value, string & suffix)
  {
    /// @todo Make better formatting of distance and units.
    UNUSED_VALUE(measurement_utils::FormatDistance(dist, value));

    size_t const delim = value.find(' ');
    ASSERT(delim != string::npos, ());
    suffix = value.substr(delim + 1);
    value.erase(delim);
  };

  threads::MutexGuard guard(m_routeSessionMutex);
  UNUSED_VALUE(guard);

  ASSERT(m_route, ());

  if (!m_route->IsValid())
  {
    // nothing should be displayed on the screen about turns if these lines are executed
    info = FollowingInfo();
    return;
  }

  if (!IsNavigable())
  {
    info = FollowingInfo();
    formatDistFn(m_route->GetTotalDistanceMeters(), info.m_distToTarget, info.m_targetUnitsSuffix);
    info.m_time = max(kMinimumETASec, m_route->GetCurrentTimeToEndSec());
    return;
  }

  formatDistFn(m_route->GetCurrentDistanceToEndMeters(), info.m_distToTarget, info.m_targetUnitsSuffix);

  double distanceToTurnMeters = 0.;
  turns::TurnItem turn;
  m_route->GetCurrentTurn(distanceToTurnMeters, turn);
  formatDistFn(distanceToTurnMeters, info.m_distToTurn, info.m_turnUnitsSuffix);
  info.m_turn = turn.m_turn;

  // The turn after the next one.
  if (m_routingSettings.m_showTurnAfterNext)
    info.m_nextTurn = m_turnNotificationsMgr.GetSecondTurnNotification();
  else
    info.m_nextTurn = routing::turns::TurnDirection::NoTurn;

  info.m_exitNum = turn.m_exitNum;
  info.m_time = max(kMinimumETASec, m_route->GetCurrentTimeToEndSec());
  m_route->GetCurrentStreetName(info.m_sourceName);
  m_route->GetStreetNameAfterIdx(turn.m_index, info.m_targetName);
  info.m_completionPercent = GetCompletionPercent();
  // Lane information.
  if (distanceToTurnMeters < kShowLanesDistInMeters)
  {
    // There are two nested loops below. Outer one is for lanes and inner one (ctor of
    // SingleLaneInfo) is
    // for each lane's directions. The size of turn.m_lanes is relatively small. Less than 10 in
    // most cases.
    info.m_lanes.clear();
    info.m_lanes.reserve(turn.m_lanes.size());
    for (size_t j = 0; j < turn.m_lanes.size(); ++j)
      info.m_lanes.emplace_back(turn.m_lanes[j]);
  }
  else
  {
    info.m_lanes.clear();
  }

  // Speedcam signal information.
  info.m_speedWarningSignal = m_speedWarningSignal;
  m_speedWarningSignal = false;

  // Pedestrian info
  m2::PointD pos;
  m_route->GetCurrentDirectionPoint(pos);
  info.m_pedestrianDirectionPos = MercatorBounds::ToLatLon(pos);
  info.m_pedestrianTurn =
      (distanceToTurnMeters < kShowPedestrianTurnInMeters) ? turn.m_pedestrianTurn : turns::PedestrianDirection::None;
}

double RoutingSession::GetCompletionPercent() const
{
  ASSERT(m_route, ());

  double const denominator = m_passedDistanceOnRouteMeters + m_route->GetTotalDistanceMeters();
  if (!m_route->IsValid() || denominator == 0.0)
    return 0;

  double const percent = 100.0 *
    (m_passedDistanceOnRouteMeters + m_route->GetCurrentDistanceFromBeginMeters()) /
    denominator;
  if (percent - m_lastCompletionPercent > kCompletionPercentAccuracy)
  {
    auto const lastGoodPoint =
        MercatorBounds::ToLatLon(m_route->GetFollowedPolyline().GetCurrentIter().m_pt);
    alohalytics::Stats::Instance().LogEvent(
        "RouteTracking_PercentUpdate", {{"percent", strings::to_string(percent)}},
        alohalytics::Location::FromLatLon(lastGoodPoint.lat, lastGoodPoint.lon));
    m_lastCompletionPercent = percent;
  }
  return percent;
}

void RoutingSession::GenerateTurnNotifications(vector<string> & turnNotifications)
{
  turnNotifications.clear();

  threads::MutexGuard guard(m_routeSessionMutex);
  UNUSED_VALUE(guard);

  ASSERT(m_route, ());

  // Voice turn notifications.
  if (!m_routingSettings.m_soundDirection)
    return;

  if (!m_route->IsValid() || !IsNavigable())
    return;

  vector<turns::TurnItemDist> turns;
  if (m_route->GetNextTurns(turns))
    m_turnNotificationsMgr.GenerateTurnNotifications(turns, turnNotifications);
}

void RoutingSession::AssignRoute(Route & route, IRouter::ResultCode e)
{
  if (e != IRouter::Cancelled)
  {
    if (route.IsValid())
      SetState(RouteNotStarted);
    else
      SetState(RoutingNotActive);

    if (e != IRouter::NoError)
      SetState(RouteNotReady);
  }
  else
  {
    SetState(RoutingNotActive);
  }

  ASSERT(m_route, ());

  route.SetRoutingSettings(m_routingSettings);
  m_route->Swap(route);
  m_lastWarnedSpeedCameraIndex = 0;
  m_lastCheckedSpeedCameraIndex = 0;
  m_lastFoundCamera = SpeedCameraRestriction();
}

void RoutingSession::SetRouter(unique_ptr<IRouter> && router,
                               unique_ptr<OnlineAbsentCountriesFetcher> && fetcher)
{
  ASSERT(m_router != nullptr, ());
  Reset();
  m_router->SetRouter(move(router), move(fetcher));
}

void RoutingSession::MatchLocationToRoute(location::GpsInfo & location,
                                          location::RouteMatchingInfo & routeMatchingInfo) const
{
  if (!IsOnRoute())
    return;

  threads::MutexGuard guard(m_routeSessionMutex);
  UNUSED_VALUE(guard);

  ASSERT(m_route, ());

  m_route->MatchLocationToRoute(location, routeMatchingInfo);
}

bool RoutingSession::DisableFollowMode()
{
  LOG(LINFO, ("Routing disables a following mode. State: ", m_state));
  if (m_state == RouteNotStarted || m_state == OnRoute)
  {
    SetState(RouteNoFollowing);
    m_isFollowing = false;
    return true;
  }
  return false;
}

bool RoutingSession::EnableFollowMode()
{
  LOG(LINFO, ("Routing enables a following mode. State: ", m_state));
  if (m_state == RouteNotStarted || m_state == OnRoute)
  {
    SetState(OnRoute);
    m_isFollowing = true;
  }
  return m_isFollowing;
}

void RoutingSession::SetRoutingSettings(RoutingSettings const & routingSettings)
{
  threads::MutexGuard guard(m_routeSessionMutex);
  UNUSED_VALUE(guard);
  m_routingSettings = routingSettings;
}

void RoutingSession::SetUserCurrentPosition(m2::PointD const & position)
{
  m_userCurrentPosition = position;
}

m2::PointD const & RoutingSession::GetUserCurrentPosition() const
{
  return m_userCurrentPosition;
}

void RoutingSession::EnableTurnNotifications(bool enable)
{
  threads::MutexGuard guard(m_routeSessionMutex);
  UNUSED_VALUE(guard);
  m_turnNotificationsMgr.Enable(enable);
}

bool RoutingSession::AreTurnNotificationsEnabled() const
{
  threads::MutexGuard guard(m_routeSessionMutex);
  UNUSED_VALUE(guard);
  return m_turnNotificationsMgr.IsEnabled();
}

void RoutingSession::SetTurnNotificationsUnits(measurement_utils::Units const units)
{
  threads::MutexGuard guard(m_routeSessionMutex);
  UNUSED_VALUE(guard);
  m_turnNotificationsMgr.SetLengthUnits(units);
}

void RoutingSession::SetTurnNotificationsLocale(string const & locale)
{
  LOG(LINFO, ("The language for turn notifications is", locale));
  threads::MutexGuard guard(m_routeSessionMutex);
  UNUSED_VALUE(guard);
  m_turnNotificationsMgr.SetLocale(locale);
}

string RoutingSession::GetTurnNotificationsLocale() const
{
  threads::MutexGuard guard(m_routeSessionMutex);
  UNUSED_VALUE(guard);
  return m_turnNotificationsMgr.GetLocale();
}

double RoutingSession::GetDistanceToCurrentCamM(SpeedCameraRestriction & camera, Index const & index)
{
  ASSERT(m_route, ());

  auto const & m_poly = m_route->GetFollowedPolyline();
  auto const & currentIter = m_poly.GetCurrentIter();
  if (currentIter.m_ind < m_lastFoundCamera.m_index &&
      m_lastFoundCamera.m_index < m_poly.GetPolyline().GetSize())
  {
    camera = m_lastFoundCamera;
    return m_poly.GetDistanceM(currentIter, m_poly.GetIterToIndex(camera.m_index));
  }
  size_t const currentIndex = max(currentIter.m_ind, m_lastCheckedSpeedCameraIndex + 1);
  size_t const upperBound = min(m_poly.GetPolyline().GetSize(), currentIndex + kSpeedCameraLookAheadCount);
  for (m_lastCheckedSpeedCameraIndex = currentIndex; m_lastCheckedSpeedCameraIndex < upperBound; ++m_lastCheckedSpeedCameraIndex)
  {
    uint8_t speed = CheckCameraInPoint(m_poly.GetPolyline().GetPoint(m_lastCheckedSpeedCameraIndex), index);
    if (speed != kNoSpeedCamera)
    {
      camera = SpeedCameraRestriction(static_cast<uint32_t>(m_lastCheckedSpeedCameraIndex), speed);
      m_lastFoundCamera = camera;
      return m_poly.GetDistanceM(currentIter, m_poly.GetIterToIndex(m_lastCheckedSpeedCameraIndex));
    }
  }
  return kInvalidSpeedCameraDistance;
}

void RoutingSession::EmitCloseRoutingEvent() const
{
  threads::MutexGuard guard(m_routeSessionMutex);
  ASSERT(m_route, ());

  if (!m_route->IsValid())
  {
    ASSERT(false, ());
    return;
  }
  auto const lastGoodPoint =
      MercatorBounds::ToLatLon(m_route->GetFollowedPolyline().GetCurrentIter().m_pt);
  alohalytics::Stats::Instance().LogEvent(
      "RouteTracking_RouteClosing",
      {{"percent", strings::to_string(GetCompletionPercent())},
       {"distance", strings::to_string(m_passedDistanceOnRouteMeters +
                                       m_route->GetCurrentDistanceToEndMeters())},
       {"router", m_route->GetRouterId()},
       {"rebuildCount", strings::to_string(m_routingRebuildCount)}},
      alohalytics::Location::FromLatLon(lastGoodPoint.lat, lastGoodPoint.lon));
}

bool RoutingSession::HasRouteAltitudeImpl() const
{
  ASSERT(m_route, ());

  return m_route->GetAltitudes().size() == m_route->GetSegDistanceM().size() + 1;
}

bool RoutingSession::HasRouteAltitude() const
{
  threads::MutexGuard guard(m_routeSessionMutex);
  return HasRouteAltitudeImpl();
}

bool RoutingSession::GetRouteAltitudesAndDistancesM(vector<double> & routeSegDistanceM,
                                                    feature::TAltitudes & routeAltitudesM) const
{
  threads::MutexGuard guard(m_routeSessionMutex);
  ASSERT(m_route, ());

  if (!m_route->IsValid() || !HasRouteAltitudeImpl())
    return false;

  routeSegDistanceM = m_route->GetSegDistanceM();
  routeAltitudesM.assign(m_route->GetAltitudes().cbegin(), m_route->GetAltitudes().cend());
  return true;
}

shared_ptr<Route> const RoutingSession::GetRoute() const
{
  threads::MutexGuard guard(m_routeSessionMutex);
  ASSERT(m_route, ());
  return m_route;
}

string DebugPrint(RoutingSession::State state)
{
  switch (state)
  {
  case RoutingSession::RoutingNotActive: return "RoutingNotActive";
  case RoutingSession::RouteBuilding: return "RouteBuilding";
  case RoutingSession::RouteNotReady: return "RouteNotReady";
  case RoutingSession::RouteNotStarted: return "RouteNotStarted";
  case RoutingSession::OnRoute: return "OnRoute";
  case RoutingSession::RouteNeedRebuild: return "RouteNeedRebuild";
  case RoutingSession::RouteFinished: return "RouteFinished";
  case RoutingSession::RouteNoFollowing: return "RouteNoFollowing";
  case RoutingSession::RouteRebuilding: return "RouteRebuilding";
  }
}
}  // namespace routing