From d17820b9d7791bcc97c2b87f3d1b7d1f779fa516 Mon Sep 17 00:00:00 2001 From: Lev Dragunov Date: Tue, 3 Nov 2015 15:47:56 +0300 Subject: [PATCH 1/2] Router freezing fix. --- routing/routing_session.cpp | 20 +++++++++++++++----- routing/routing_session.hpp | 2 ++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/routing/routing_session.cpp b/routing/routing_session.cpp index 343462876b..354390163c 100644 --- a/routing/routing_session.cpp +++ b/routing/routing_session.cpp @@ -35,6 +35,9 @@ 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; } // namespace namespace routing @@ -45,6 +48,7 @@ RoutingSession::RoutingSession() m_state(RoutingNotActive), m_endPoint(m2::PointD::Zero()), m_lastWarnedSpeedCameraIndex(0), + m_lastCheckedSpeedCameraIndex(0), m_speedWarningSignal(false), m_passedDistanceOnRouteMeters(0.0) { @@ -133,6 +137,7 @@ void RoutingSession::Reset() m_passedDistanceOnRouteMeters = 0.0; m_lastWarnedSpeedCameraIndex = 0; + m_lastCheckedSpeedCameraIndex = 0; m_lastCheckedCamera = SpeedCameraRestriction(); m_speedWarningSignal = false; } @@ -329,6 +334,7 @@ void RoutingSession::AssignRoute(Route & route, IRouter::ResultCode e) route.SetRoutingSettings(m_routingSettings); m_route.Swap(route); m_lastWarnedSpeedCameraIndex = 0; + m_lastCheckedSpeedCameraIndex = 0; m_lastCheckedCamera = SpeedCameraRestriction(); } @@ -428,17 +434,21 @@ double RoutingSession::GetDistanceToCurrentCamM(SpeedCameraRestriction & camera, camera = m_lastCheckedCamera; return m_poly.GetDistanceM(currentIter, m_poly.GetIterToIndex(camera.m_index)); } - size_t const currentIndex = max(currentIter.m_ind, + size_t const currentIndex = max(max(currentIter.m_ind, m_lastCheckedSpeedCameraIndex), m_lastCheckedCamera.m_index + 1); - for (size_t i = currentIndex; i < m_poly.GetPolyline().GetSize(); ++i) + size_t moveAwayCounter = 0; + for (m_lastCheckedSpeedCameraIndex = currentIndex; m_lastCheckedSpeedCameraIndex < m_poly.GetPolyline().GetSize(); ++m_lastCheckedSpeedCameraIndex) { - uint8_t speed = CheckCameraInPoint(m_poly.GetPolyline().GetPoint(i), index); + uint8_t speed = CheckCameraInPoint(m_poly.GetPolyline().GetPoint(m_lastCheckedSpeedCameraIndex), index); if (speed != kNoSpeedCamera) { - camera = SpeedCameraRestriction(static_cast(i), speed); + camera = SpeedCameraRestriction(static_cast(m_lastCheckedSpeedCameraIndex), speed); m_lastCheckedCamera = camera; - return m_poly.GetDistanceM(currentIter, m_poly.GetIterToIndex(i)); + return m_poly.GetDistanceM(currentIter, m_poly.GetIterToIndex(m_lastCheckedSpeedCameraIndex)); } + ++moveAwayCounter; + if (moveAwayCounter > kSpeedCameraLookAheadCount) + return kInvalidSpeedCameraDistance; } m_lastCheckedCamera.m_index = m_poly.GetPolyline().GetSize(); return kInvalidSpeedCameraDistance; diff --git a/routing/routing_session.hpp b/routing/routing_session.hpp index a8e5c971e3..6d444301ef 100644 --- a/routing/routing_session.hpp +++ b/routing/routing_session.hpp @@ -148,6 +148,8 @@ private: m2::PointD m_endPoint; size_t m_lastWarnedSpeedCameraIndex; SpeedCameraRestriction m_lastCheckedCamera; + size_t m_lastCheckedSpeedCameraIndex; + // TODO (ldragunov) Rewrite UI interop to message queue and avoid mutable. /// This field is mutable because it's modified in a constant getter. Note that the notification /// about camera will be sent at most once. From 005d6674a4f0d6407c8dc84cbf340cb11be45572 Mon Sep 17 00:00:00 2001 From: Lev Dragunov Date: Tue, 3 Nov 2015 18:13:26 +0300 Subject: [PATCH 2/2] PR fixes. --- routing/routing_session.cpp | 23 +++++++++-------------- routing/routing_session.hpp | 3 ++- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/routing/routing_session.cpp b/routing/routing_session.cpp index 354390163c..8dadcc1a20 100644 --- a/routing/routing_session.cpp +++ b/routing/routing_session.cpp @@ -138,7 +138,7 @@ void RoutingSession::Reset() m_passedDistanceOnRouteMeters = 0.0; m_lastWarnedSpeedCameraIndex = 0; m_lastCheckedSpeedCameraIndex = 0; - m_lastCheckedCamera = SpeedCameraRestriction(); + m_lastFoundCamera = SpeedCameraRestriction(); m_speedWarningSignal = false; } @@ -335,7 +335,7 @@ void RoutingSession::AssignRoute(Route & route, IRouter::ResultCode e) m_route.Swap(route); m_lastWarnedSpeedCameraIndex = 0; m_lastCheckedSpeedCameraIndex = 0; - m_lastCheckedCamera = SpeedCameraRestriction(); + m_lastFoundCamera = SpeedCameraRestriction(); } void RoutingSession::SetRouter(unique_ptr && router, @@ -428,29 +428,24 @@ double RoutingSession::GetDistanceToCurrentCamM(SpeedCameraRestriction & camera, { auto const & m_poly = m_route.GetFollowedPolyline(); auto const & currentIter = m_poly.GetCurrentIter(); - if (currentIter.m_ind < m_lastCheckedCamera.m_index && - m_lastCheckedCamera.m_index < m_poly.GetPolyline().GetSize()) + if (currentIter.m_ind < m_lastFoundCamera.m_index && + m_lastFoundCamera.m_index < m_poly.GetPolyline().GetSize()) { - camera = m_lastCheckedCamera; + camera = m_lastFoundCamera; return m_poly.GetDistanceM(currentIter, m_poly.GetIterToIndex(camera.m_index)); } - size_t const currentIndex = max(max(currentIter.m_ind, m_lastCheckedSpeedCameraIndex), - m_lastCheckedCamera.m_index + 1); - size_t moveAwayCounter = 0; - for (m_lastCheckedSpeedCameraIndex = currentIndex; m_lastCheckedSpeedCameraIndex < m_poly.GetPolyline().GetSize(); ++m_lastCheckedSpeedCameraIndex) + 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(m_lastCheckedSpeedCameraIndex), speed); - m_lastCheckedCamera = camera; + m_lastFoundCamera = camera; return m_poly.GetDistanceM(currentIter, m_poly.GetIterToIndex(m_lastCheckedSpeedCameraIndex)); } - ++moveAwayCounter; - if (moveAwayCounter > kSpeedCameraLookAheadCount) - return kInvalidSpeedCameraDistance; } - m_lastCheckedCamera.m_index = m_poly.GetPolyline().GetSize(); return kInvalidSpeedCameraDistance; } } // namespace routing diff --git a/routing/routing_session.hpp b/routing/routing_session.hpp index 6d444301ef..a9d14c3e45 100644 --- a/routing/routing_session.hpp +++ b/routing/routing_session.hpp @@ -147,7 +147,8 @@ private: State m_state; m2::PointD m_endPoint; size_t m_lastWarnedSpeedCameraIndex; - SpeedCameraRestriction m_lastCheckedCamera; + SpeedCameraRestriction m_lastFoundCamera; + // Index of a last point on a route checked for a speed camera. size_t m_lastCheckedSpeedCameraIndex; // TODO (ldragunov) Rewrite UI interop to message queue and avoid mutable.