From a2e53e22bd8550608c3a0edeb5efafaaedf4af2d Mon Sep 17 00:00:00 2001 From: tatiana-yan Date: Tue, 16 Mar 2021 10:55:17 +0300 Subject: [PATCH] Review fixes. --- transit/world_feed/feed_helpers.cpp | 26 +++++++++++++++++++------- transit/world_feed/feed_helpers.hpp | 3 ++- transit/world_feed/world_feed.cpp | 2 +- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/transit/world_feed/feed_helpers.cpp b/transit/world_feed/feed_helpers.cpp index b5bf224c08..d51c01b6f2 100644 --- a/transit/world_feed/feed_helpers.cpp +++ b/transit/world_feed/feed_helpers.cpp @@ -22,15 +22,17 @@ struct ProjectionData size_t m_indexOnShape = 0; // Distance from point to its projection. double m_distFromPoint = 0.0; - // Distance from start point on polyline to the projection. + // Distance from the first ending (start for forward direction, end for backward) point on + // polyline to the projection. double m_distFromEnding = 0.0; // Point on polyline almost equal to the projection can already exist, so we don't need to // insert projection. Or we insert it to the polyline. bool m_needsInsertion = false; }; -// Returns true if |p1| is much closer to start then |p2| (parameter |distDeltaStart|) and its -// distance to projections to polyline |m_distFromPoint| is comparable. +// Returns true if |p1| is much closer to the first ending (start for forward direction, end for +// backward) then |p2| (parameter |distDeltaEnding|) and its distance to projections to polyline +// |m_distFromPoint| is comparable. bool CloserToEndingAndOnSimilarDistToLine(ProjectionData const & p1, ProjectionData const & p2) { // Delta between two points distances from start point on polyline. @@ -62,6 +64,8 @@ ProjectionData GetProjection(std::vector const & polyline, size_t in projData.m_proj = proj.m_point; auto const next = direction == Direction::Forward ? index + 1 : index - 1; + CHECK_GREATER_OR_EQUAL(next, 0, ()); + CHECK_LESS(next, polyline.size(), ()); if (base::AlmostEqualAbs(proj.m_point, polyline[index], kEps)) { @@ -86,17 +90,25 @@ void FillProjections(std::vector & polyline, size_t startIndex, size m2::PointD const & point, double distStopsM, Direction direction, std::vector & projections) { + CHECK_LESS_OR_EQUAL(startIndex, endIndex, ()); + double distTravelledM = 0.0; // Stop can't be further from its projection to line then |maxDistFromStopM|. double constexpr maxDistFromStopM = 1000; size_t const from = direction == Direction::Forward ? startIndex : endIndex; - auto const endCriteria = [&](size_t i) { + + auto const endCriterion = [&](size_t i) { return direction == Direction::Forward ? i < endIndex : i > startIndex; }; - auto const move = [direction](size_t & i) { direction == Direction::Forward ? ++i : --i; }; - for (size_t i = from; endCriteria(i); move(i)) + auto const move = [&](size_t & i) { + direction == Direction::Forward ? ++i : --i; + CHECK_GREATER_OR_EQUAL(i, 0, ()); + CHECK_LESS_OR_EQUAL(i, polyline.size(), ()); + }; + + for (size_t i = from; endCriterion(i); move(i)) { auto const current = i; auto const prev = direction == Direction::Forward ? i - 1 : i + 1; @@ -152,7 +164,7 @@ std::pair PrepareNearestPointOnTrack(m2::PointD const & point, if (CloserToEndingAndOnSimilarDistToLine(p2, p1)) return false; - if (base::AlmostEqualAbs(p1.m_distFromPoint, p2.m_distFromPoint, kEps)) + if (p1.m_distFromPoint == p2.m_distFromPoint) return p1.m_distFromEnding < p2.m_distFromEnding; return p1.m_distFromPoint < p2.m_distFromPoint; diff --git a/transit/world_feed/feed_helpers.hpp b/transit/world_feed/feed_helpers.hpp index f9e0be4b5b..ec1b5142da 100644 --- a/transit/world_feed/feed_helpers.hpp +++ b/transit/world_feed/feed_helpers.hpp @@ -33,7 +33,8 @@ ProjectionToShape ProjectStopOnTrack(m2::PointD const & stopPoint, m2::PointD co /// \returns index of the nearest track point to the |point| and flag if it was inserted to the /// shape. If this index doesn't match already existent points, the stop projection is inserted to -/// the |polyline| and the flag is set to true. +/// the |polyline| and the flag is set to true. New point should follow prevPoint in the direction +/// |direction|. std::pair PrepareNearestPointOnTrack(m2::PointD const & point, std::optional const & prevPoint, size_t prevIndex, Direction direction, diff --git a/transit/world_feed/world_feed.cpp b/transit/world_feed/world_feed.cpp index 0a516a7a6f..529c74149a 100644 --- a/transit/world_feed/world_feed.cpp +++ b/transit/world_feed/world_feed.cpp @@ -957,7 +957,7 @@ bool WorldFeed::ProjectStopsToShape( CHECK(itStop != m_stops.m_data.end(), (stopId)); auto const & stop = itStop->second; - size_t const prevIdx = i == 0 ? direction == Direction::Forward ? 0 : shape.size() + size_t const prevIdx = i == 0 ? (direction == Direction::Forward ? 0 : shape.size()) : stopsToIndexes[stopIds[i - 1]].back(); auto const [curIdx, pointInserted] = PrepareNearestPointOnTrack(stop.m_point, prevPoint, prevIdx, direction, shape);