From f95b30a7af92770bf89d88c882417a7ab557be41 Mon Sep 17 00:00:00 2001 From: ExMix Date: Fri, 12 Sep 2014 10:15:45 +0300 Subject: [PATCH] [core] check routing session state. --- geometry/polyline2d.hpp | 23 ++++++++++++++++++++--- map/bookmark.cpp | 2 +- map/routing_session.cpp | 31 +++++++++++++++++++++++++------ map/routing_session.hpp | 8 ++++++-- map/track.cpp | 15 ++------------- 5 files changed, 54 insertions(+), 25 deletions(-) diff --git a/geometry/polyline2d.hpp b/geometry/polyline2d.hpp index 6fca13d213..14b169e3cc 100644 --- a/geometry/polyline2d.hpp +++ b/geometry/polyline2d.hpp @@ -2,6 +2,7 @@ #include "point2d.hpp" #include "rect2d.hpp" +#include "distance.hpp" #include "../base/internal/message.hpp" @@ -36,6 +37,21 @@ public: return dist; } + double GetShortestSquareDistance(m2::PointD const & point) const + { + double res = numeric_limits::max(); + m2::DistanceToLineSquare d; + + TIter i = Begin(); + for (TIter j = i + 1; j != End(); ++i, ++j) + { + d.SetBounds(*i, *j); + res = min(res, d(point)); + } + + return res; + } + Rect GetLimitRect() const { // @todo add cached value and lazy init @@ -54,9 +70,10 @@ public: size_t GetSize() const { return m_points.size(); } - typedef typename vector >::const_iterator IterT; - IterT Begin() const { return m_points.begin(); } - IterT End() const { return m_points.end(); } + typedef vector > TContainer; + typedef typename TContainer::const_iterator TIter; + TIter Begin() const { return m_points.begin(); } + TIter End() const { return m_points.end(); } friend string DebugPrint(PolylineT const & p) { diff --git a/map/bookmark.cpp b/map/bookmark.cpp index 36f3ab245e..5edb36f926 100644 --- a/map/bookmark.cpp +++ b/map/bookmark.cpp @@ -767,7 +767,7 @@ void BookmarkCategory::SaveToKML(ostream & s) s << " "; Track::PolylineD const & poly = track->GetPolyline(); - for (Track::PolylineD::IterT pt = poly.Begin(); pt != poly.End(); ++pt) + for (Track::PolylineD::TIter pt = poly.Begin(); pt != poly.End(); ++pt) s << PointToString(*pt) << " "; s << " \n" diff --git a/map/routing_session.cpp b/map/routing_session.cpp index 6d2e8d38a6..4ad7097d33 100644 --- a/map/routing_session.cpp +++ b/map/routing_session.cpp @@ -5,6 +5,7 @@ namespace routing RoutingSession::RoutingSession(IRouter * router) : m_router(router) + , m_state(RouteNotReady) { } @@ -17,8 +18,10 @@ void RoutingSession::BuildRoute(m2::PointD const & startPoint, m2::PointD const void RoutingSession::RebuildRoute(m2::PointD const & startPoint, IRouter::ReadyCallback const & callback) { + m_state = RouteNotReady; m_router->CalculateRoute(startPoint, [this, callback](Route const & route) { + m_state = RouteNotStarted; m_routeGeometry = route.GetPoly(); callback(route); }); @@ -27,24 +30,40 @@ void RoutingSession::RebuildRoute(m2::PointD const & startPoint, IRouter::ReadyC RoutingSession::State RoutingSession::OnLocationPositionChanged(m2::PointD const & position, double errorRadius) { - if (m_state != RoutFinished && m_state != RouteLeft) + if (m_state == RouteNotReady || m_state == RouteFinished || m_state == RouteLeft) + return m_state; + + if (m_state == OnRoute && IsOnDestPoint(position, errorRadius)) { - if (IsOnDestPoint(position, errorRadius)) - m_state = RoutFinished; - else - m_state = IsOnRoute(position, errorRadius) ? OnRout : RouteLeft; + m_state = RouteFinished; + return m_state; } + bool isOnRoute = IsOnRoute(position, errorRadius); + if (isOnRoute) + m_state = OnRoute; + else if (m_state != RouteNotStarted) + m_state = RouteLeft; + return m_state; } bool RoutingSession::IsOnRoute(m2::PointD const & position, double errorRadius) const { - return true; + if (errorRadius > MaxValidError || m_routeGeometry.GetShortestSquareDistance(position) < errorRadius) + return true; + + return false; } bool RoutingSession::IsOnDestPoint(m2::PointD const & position, double errorRadius) const { + if (errorRadius > MaxValidError) + return false; + + m2::PointD lastPoint = *reverse_iterator(m_routeGeometry.End()); + if (lastPoint.SquareLength(position) < errorRadius * errorRadius) + return true; return false; } diff --git a/map/routing_session.hpp b/map/routing_session.hpp index 4e4d99ef66..91e9090315 100644 --- a/map/routing_session.hpp +++ b/map/routing_session.hpp @@ -16,9 +16,11 @@ class RoutingSession public: enum State { - OnRout, + RouteNotReady, + RouteNotStarted, + OnRoute, RouteLeft, - RoutFinished + RouteFinished }; /// startPoint and destPoint in mercator @@ -38,6 +40,8 @@ private: unique_ptr m_router; m2::PolylineD m_routeGeometry; State m_state; + + constexpr static double MaxValidError = 0.005; }; } diff --git a/map/track.cpp b/map/track.cpp index 85ec8ab00e..50b34684ff 100644 --- a/map/track.cpp +++ b/map/track.cpp @@ -94,7 +94,7 @@ double Track::GetLengthMeters() const { double res = 0.0; - PolylineD::IterT i = m_polyline.Begin(); + PolylineD::TIter i = m_polyline.Begin(); double lat1 = MercatorBounds::YToLat(i->y); double lon1 = MercatorBounds::XToLon(i->x); for (++i; i != m_polyline.End(); ++i) @@ -111,18 +111,7 @@ double Track::GetLengthMeters() const double Track::GetShortestSquareDistance(m2::PointD const & point) const { - double res = numeric_limits::max(); - m2::DistanceToLineSquare d; - - typedef PolylineD::IterT IterT; - IterT i = m_polyline.Begin(); - for (IterT j = i+1; j != m_polyline.End(); ++i, ++j) - { - d.SetBounds(*i, *j); - res = min(res, d(point)); - } - - return res; + return m_polyline.GetShortestSquareDistance(point); } void Track::Swap(Track & rhs)