From bad708e551e98964d40915261ad9d26fa27aef21 Mon Sep 17 00:00:00 2001 From: Yuri Gorshenin Date: Tue, 7 Apr 2015 12:42:47 +0300 Subject: [PATCH] [pedestrian,routing] An attempt to make bizarre names more readable. --- routing/astar_router.cpp | 9 ++-- routing/features_road_graph.cpp | 12 ++--- routing/road_graph.cpp | 14 +++--- routing/road_graph.hpp | 50 +++++++++++-------- .../features_road_graph_test.cpp | 10 ++-- routing/routing_tests/road_graph_builder.cpp | 40 ++++++++------- routing/routing_tests/road_graph_builder.hpp | 15 ++++-- 7 files changed, 83 insertions(+), 67 deletions(-) diff --git a/routing/astar_router.cpp b/routing/astar_router.cpp index 2c5c6cca98..8d71be1620 100644 --- a/routing/astar_router.cpp +++ b/routing/astar_router.cpp @@ -84,8 +84,8 @@ double AStarRouter::HeuristicCostEstimate(AStarRouter::ShortestPath const * s1, /// @todo support of more than one goals ASSERT_GREATER(goals.size(), 0, ()); - m2::PointD const & b = (*goals.begin()).GetEndCoordinates(); - m2::PointD const & e = s1->GetPos().GetEndCoordinates(); + m2::PointD const & b = (*goals.begin()).GetSegEndpoint(); + m2::PointD const & e = s1->GetPos().GetSegEndpoint(); return ms::DistanceOnEarth(MercatorBounds::YToLat(b.y), MercatorBounds::XToLon(b.x), @@ -95,9 +95,8 @@ double AStarRouter::HeuristicCostEstimate(AStarRouter::ShortestPath const * s1, double AStarRouter::DistanceBetween(ShortestPath const * p1, ShortestPath const * p2) { - - m2::PointD const & b = p1->GetPos().GetEndCoordinates(); - m2::PointD const & e = p2->GetPos().GetEndCoordinates(); + m2::PointD const & b = p1->GetPos().GetSegEndpoint(); + m2::PointD const & e = p2->GetPos().GetSegEndpoint(); return ms::DistanceOnEarth(MercatorBounds::YToLat(b.y), MercatorBounds::XToLon(b.x), MercatorBounds::YToLat(e.y), diff --git a/routing/features_road_graph.cpp b/routing/features_road_graph.cpp index 6a9576bd15..d16935954e 100644 --- a/routing/features_road_graph.cpp +++ b/routing/features_road_graph.cpp @@ -141,7 +141,7 @@ void FeaturesRoadGraph::GetPossibleTurns(RoadPos const & pos, vector 0; --i) + for (int i = pos.GetSegmentId(); i > 0; --i) distance += CalcDistanceMeters(fc.m_points[i], fc.m_points[i - 1]); thisTurn.m_pos = RoadPos(ftID, true, count - 2, fc.m_points[count - 1]); @@ -227,7 +227,7 @@ void FeaturesRoadGraph::GetPossibleTurns(RoadPos const & pos, vector::max(), ()); } string DebugPrint(RoadPos const & r) { ostringstream ss; - ss << "{" << r.GetFeatureId() << ", " << r.IsForward() << ", " << r.m_pointId << "}"; + ss << "{ featureId: " << r.GetFeatureId() << ", isForward: " << r.IsForward() + << ", segmentId:" << r.m_segmentId << "}"; return ss.str(); } -} // namespace routing +} // namespace routing diff --git a/routing/road_graph.hpp b/routing/road_graph.hpp index a0826f275b..490442956d 100644 --- a/routing/road_graph.hpp +++ b/routing/road_graph.hpp @@ -6,45 +6,50 @@ #include "../std/vector.hpp" - namespace routing { - class Route; /// Defines position on a feature with direction. class RoadPos { public: - RoadPos() : m_featureId(0), m_pointId(0), m_endCoordinates(0, 0) {} - RoadPos(uint32_t featureId, bool bForward, size_t pointId, m2::PointD const & p = m2::PointD::Zero()); + RoadPos() : m_featureId(0), m_segmentId(0), m_segEndpoint(0, 0) {} + RoadPos(uint32_t featureId, bool forward, size_t segmentId, + m2::PointD const & p = m2::PointD::Zero()); uint32_t GetFeatureId() const { return m_featureId >> 1; } bool IsForward() const { return (m_featureId & 1) != 0; } - uint32_t GetPointId() const { return m_pointId; } + uint32_t GetSegmentId() const { return m_segmentId; } + uint32_t GetSegStartPointId() const { return m_segmentId + (IsForward() ? 0 : 1); } + uint32_t GetSegEndPointId() const { return m_segmentId + (IsForward() ? 1 : 0); } + m2::PointD const & GetSegEndpoint() const { return m_segEndpoint; } bool operator==(RoadPos const & r) const { - return (m_featureId == r.m_featureId && m_pointId == r.m_pointId); + return (m_featureId == r.m_featureId && m_segmentId == r.m_segmentId); } - bool operator < (RoadPos const & r) const + bool operator<(RoadPos const & r) const { if (m_featureId != r.m_featureId) return m_featureId < r.m_featureId; - if (m_pointId != r.m_pointId) - return m_pointId < r.m_pointId; + if (m_segmentId != r.m_segmentId) + return m_segmentId < r.m_segmentId; return false; } - m2::PointD const & GetEndCoordinates() const { return m_endCoordinates; } - private: friend string DebugPrint(RoadPos const & r); + // Feature on which position is defined. uint32_t m_featureId; - uint32_t m_pointId; - m2::PointD m_endCoordinates; + + // Ordinal number of a segment on the road. + uint32_t m_segmentId; + + // End-point of the segment on the road. + m2::PointD m_segEndpoint; }; /// The turn from the old to the new road. @@ -52,7 +57,14 @@ struct PossibleTurn { /// New road information. RoadPos m_pos; - m2::PointD m_startPoint, m_endPoint; + + /// Start point on the old road. + m2::PointD m_startPoint; + + /// End point on the old road. + m2::PointD m_endPoint; + + /// Speed on the old road. double m_speed; /// Distance and time to get to this turn on old road. @@ -62,10 +74,7 @@ struct PossibleTurn PossibleTurn() : m_metersCovered(0.0), m_secondsCovered(0.0) {} }; -inline string DebugPrint(PossibleTurn const & r) -{ - return DebugPrint(r.m_pos); -} +inline string DebugPrint(PossibleTurn const & r) { return DebugPrint(r.m_pos); } class IRoadGraph { @@ -77,9 +86,10 @@ public: virtual ~IRoadGraph() {} /// Find all feature sections (turns), that route to the "pos" section. - virtual void GetPossibleTurns(RoadPos const & pos, TurnsVectorT & turns, bool noOptimize = true) = 0; + virtual void GetPossibleTurns(RoadPos const & pos, TurnsVectorT & turns, + bool noOptimize = true) = 0; /// Construct route by road positions (doesn't include first and last section). virtual void ReconstructPath(RoadPosVectorT const & positions, Route & route) = 0; }; -} // namespace routing +} // namespace routing diff --git a/routing/routing_tests/features_road_graph_test.cpp b/routing/routing_tests/features_road_graph_test.cpp index 8ee3c9def2..3a5b24447a 100644 --- a/routing/routing_tests/features_road_graph_test.cpp +++ b/routing/routing_tests/features_road_graph_test.cpp @@ -82,7 +82,7 @@ void FeatureRoadGraphTester::FeatureID2Name(routing::RoadPos & pos) int id = 0; VERIFY(strings::to_int(name, id), (name)); - pos = RoadPos(static_cast(id), pos.IsForward(), pos.GetPointId()); + pos = RoadPos(static_cast(id), pos.IsForward(), pos.GetSegmentId()); } void FeatureRoadGraphTester::FeatureID2Name(IRoadGraph::TurnsVectorT & vec) @@ -100,15 +100,15 @@ void FeatureRoadGraphTester::FeatureID2Name(vector & vec) void FeatureRoadGraphTester::Name2FeatureID(vector & vec) { for (size_t i = 0; i < vec.size(); ++i) - vec[i] = RoadPos(m_mapping.GetId(strings::to_string(vec[i].GetFeatureId())), - vec[i].IsForward(), - vec[i].GetPointId()); + vec[i] = RoadPos(m_mapping.GetId(strings::to_string(vec[i].GetFeatureId())), vec[i].IsForward(), + vec[i].GetSegmentId()); } void FeatureRoadGraphTester::GetPossibleTurns(RoadPos const & pos, IRoadGraph::TurnsVectorT & vec, bool noOptimize/* = true*/) { m_graph->GetPossibleTurns(RoadPos(m_mapping.GetId(strings::to_string(pos.GetFeatureId())), - pos.IsForward(), pos.GetPointId()), vec, noOptimize); + pos.IsForward(), pos.GetSegmentId()), + vec, noOptimize); FeatureID2Name(vec); } diff --git a/routing/routing_tests/road_graph_builder.cpp b/routing/routing_tests/road_graph_builder.cpp index aa6d3eee99..e8209b4c95 100644 --- a/routing/routing_tests/road_graph_builder.cpp +++ b/routing/routing_tests/road_graph_builder.cpp @@ -10,7 +10,7 @@ void RoadInfo::Swap(RoadInfo & r) { m_points.swap(r.m_points); std::swap(m_speedMS, r.m_speedMS); - std::swap(m_bothSides, r.m_bothSides); + std::swap(m_bidirectional, r.m_bidirectional); } void RoadGraphMockSource::AddRoad(RoadInfo & rd) @@ -20,7 +20,7 @@ void RoadGraphMockSource::AddRoad(RoadInfo & rd) uint32_t const id = m_roads.size(); // Count of sections! (not points) - size_t count = rd.m_points.size(); + size_t const count = rd.m_points.size(); ASSERT_GREATER(count, 1, ()); for (size_t i = 0; i < count; ++i) @@ -34,13 +34,15 @@ void RoadGraphMockSource::AddRoad(RoadInfo & rd) if (i > 0) { - t.m_pos = RoadPos(id, true, i - 1, rd.m_points[i]); + t.m_pos = RoadPos(id /* featureId */, true /* forward */, i - 1 /* segmentId */, + rd.m_points[i] /* segEndPoint */); j->second.push_back(t); } - if (rd.m_bothSides && (i < count - 1)) + if (rd.m_bidirectional && (i < count - 1)) { - t.m_pos = RoadPos(id, false, i, rd.m_points[i]); + t.m_pos = RoadPos(id /* featureId */, false /* forward */, i /* segmentId */, + rd.m_points[i] /* segEndPoint */); j->second.push_back(t); } } @@ -58,7 +60,7 @@ void RoadGraphMockSource::GetPossibleTurns(RoadPos const & pos, TurnsVectorT & t vector const & points = m_roads[fID].m_points; int const inc = pos.IsForward() ? -1 : 1; - int startID = pos.GetPointId(); + int startID = pos.GetSegmentId(); int const count = static_cast(points.size()); if (!pos.IsForward()) @@ -100,7 +102,7 @@ void InitRoadGraphMockSourceWithTest1(RoadGraphMockSource & src) { { RoadInfo ri; - ri.m_bothSides = true; + ri.m_bidirectional = true; ri.m_speedMS = 40; ri.m_points.push_back(m2::PointD(0, 0)); ri.m_points.push_back(m2::PointD(5, 0)); @@ -112,7 +114,7 @@ void InitRoadGraphMockSourceWithTest1(RoadGraphMockSource & src) { RoadInfo ri; - ri.m_bothSides = true; + ri.m_bidirectional = true; ri.m_speedMS = 40; ri.m_points.push_back(m2::PointD(10, -10)); ri.m_points.push_back(m2::PointD(10, -5)); @@ -124,7 +126,7 @@ void InitRoadGraphMockSourceWithTest1(RoadGraphMockSource & src) { RoadInfo ri; - ri.m_bothSides = true; + ri.m_bidirectional = true; ri.m_speedMS = 40; ri.m_points.push_back(m2::PointD(15, -5)); ri.m_points.push_back(m2::PointD(15, 0)); @@ -133,7 +135,7 @@ void InitRoadGraphMockSourceWithTest1(RoadGraphMockSource & src) { RoadInfo ri; - ri.m_bothSides = true; + ri.m_bidirectional = true; ri.m_speedMS = 40; ri.m_points.push_back(m2::PointD(20, 0)); ri.m_points.push_back(m2::PointD(25, 5)); @@ -146,7 +148,7 @@ void InitRoadGraphMockSourceWithTest1(RoadGraphMockSource & src) void InitRoadGraphMockSourceWithTest2(RoadGraphMockSource & graph) { RoadInfo ri0; - ri0.m_bothSides = true; + ri0.m_bidirectional = true; ri0.m_speedMS = 40; ri0.m_points.push_back(m2::PointD(0, 0)); ri0.m_points.push_back(m2::PointD(10, 0)); @@ -156,21 +158,21 @@ void InitRoadGraphMockSourceWithTest2(RoadGraphMockSource & graph) ri0.m_points.push_back(m2::PointD(80, 0)); RoadInfo ri1; - ri1.m_bothSides = false; + ri1.m_bidirectional = false; ri1.m_speedMS = 40; ri1.m_points.push_back(m2::PointD(0, 0)); ri1.m_points.push_back(m2::PointD(5, 10)); ri1.m_points.push_back(m2::PointD(5, 40)); RoadInfo ri2; - ri2.m_bothSides = false; + ri2.m_bidirectional = false; ri2.m_speedMS = 40; ri2.m_points.push_back(m2::PointD(12, 25)); ri2.m_points.push_back(m2::PointD(10, 10)); ri2.m_points.push_back(m2::PointD(10, 0)); RoadInfo ri3; - ri3.m_bothSides = true; + ri3.m_bidirectional = true; ri3.m_speedMS = 40; ri3.m_points.push_back(m2::PointD(5, 10)); ri3.m_points.push_back(m2::PointD(10, 10)); @@ -178,13 +180,13 @@ void InitRoadGraphMockSourceWithTest2(RoadGraphMockSource & graph) ri3.m_points.push_back(m2::PointD(80, 10)); RoadInfo ri4; - ri4.m_bothSides = true; + ri4.m_bidirectional = true; ri4.m_speedMS = 40; ri4.m_points.push_back(m2::PointD(25, 0)); ri4.m_points.push_back(m2::PointD(27, 25)); RoadInfo ri5; - ri5.m_bothSides = true; + ri5.m_bidirectional = true; ri5.m_speedMS = 40; ri5.m_points.push_back(m2::PointD(35, 0)); ri5.m_points.push_back(m2::PointD(37, 30)); @@ -192,20 +194,20 @@ void InitRoadGraphMockSourceWithTest2(RoadGraphMockSource & graph) ri5.m_points.push_back(m2::PointD(80, 30)); RoadInfo ri6; - ri6.m_bothSides = true; + ri6.m_bidirectional = true; ri6.m_speedMS = 40; ri6.m_points.push_back(m2::PointD(70, 0)); ri6.m_points.push_back(m2::PointD(70, 10)); ri6.m_points.push_back(m2::PointD(70, 30)); RoadInfo ri7; - ri7.m_bothSides = true; + ri7.m_bidirectional = true; ri7.m_speedMS = 40; ri7.m_points.push_back(m2::PointD(39, 55)); ri7.m_points.push_back(m2::PointD(80, 55)); RoadInfo ri8; - ri8.m_bothSides = false; + ri8.m_bidirectional = false; ri8.m_speedMS = 40; ri8.m_points.push_back(m2::PointD(5, 40)); ri8.m_points.push_back(m2::PointD(18, 55)); diff --git a/routing/routing_tests/road_graph_builder.hpp b/routing/routing_tests/road_graph_builder.hpp index 17d2f057c0..da23486b31 100644 --- a/routing/routing_tests/road_graph_builder.hpp +++ b/routing/routing_tests/road_graph_builder.hpp @@ -4,18 +4,23 @@ namespace routing_test { +/// This struct represents road as a broken line. struct RoadInfo { + /// Points of a broken line representing the road. vector m_points; - double m_speedMS; - bool m_bothSides; - RoadInfo() : m_speedMS(0.0), m_bothSides(false) {} + /// Speed on the road. + double m_speedMS; + + /// True when it's possible to move in both directions on the orad. + bool m_bidirectional; + + RoadInfo() : m_speedMS(0.0), m_bidirectional(false) {} + void Swap(RoadInfo & r); }; -inline void swap(RoadInfo & r1, RoadInfo & r2) { r1.Swap(r2); } - class RoadGraphMockSource : public routing::IRoadGraph { vector m_roads;