From 5694dfb596cbdba76bd761ca8538b33195232f8f Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Fri, 20 Oct 2017 11:22:29 +0300 Subject: [PATCH] Keeping osm id and feature id in FeatureIdentifiers to make code more understandable. --- generator/generator_tests/transit_test.cpp | 14 ++++--- generator/transit_generator.cpp | 12 +++--- generator/transit_generator.hpp | 2 +- .../routing_common_tests/transit_test.cpp | 10 +++-- routing_common/transit_serdes.hpp | 12 ++++++ routing_common/transit_types.cpp | 14 +++---- routing_common/transit_types.hpp | 40 ++++++++++--------- 7 files changed, 62 insertions(+), 42 deletions(-) diff --git a/generator/generator_tests/transit_test.cpp b/generator/generator_tests/transit_test.cpp index 2a2eabad26..1b13e711fe 100644 --- a/generator/generator_tests/transit_test.cpp +++ b/generator/generator_tests/transit_test.cpp @@ -89,10 +89,10 @@ UNIT_TEST(DeserializerFromJson_Stops) ]})"; vector const expected = { - Stop(343259523 /* id */, OsmId(1 /* feature id */), kInvalidTransferId /* transfer id */, + Stop(343259523 /* id */, FeatureIdentifiers(kInvalidOsmId, 1 /* feature id */), kInvalidTransferId /* transfer id */, {19207936, 19207937} /* lineIds */, {27.4970954, 64.20146835878187} /* point */, {} /* anchors */), - Stop(266680843 /* id */, OsmId(2 /* feature id */), 5 /* transfer id */, + Stop(266680843 /* id */, FeatureIdentifiers(kInvalidOsmId, 2 /* feature id */), 5 /* transfer id */, {19213568, 19213569} /* line ids */, {27.5227942, 64.25206634443111} /* point */, {TitleAnchor(12 /* min zoom */, 0 /* anchor */), TitleAnchor(15, 9)})}; @@ -132,10 +132,12 @@ UNIT_TEST(DeserializerFromJson_Gates) ]})"; vector const expected = { - Gate(OsmId(0 /* feature id */), true /* entrance */, true /* exit */, 60.0 /* weight */, - {442018474} /* stop ids */, {43.8594864, 68.33320554776377} /* point */), - Gate(OsmId(2 /* feature id */), true /* entrance */, true /* exit */, 60.0 /* weight */, - {442018465} /* stop ids */, {43.9290544, 68.41120791512581} /* point */)}; + Gate(FeatureIdentifiers(kInvalidOsmId, 0 /* feature id */), true /* entrance */, + true /* exit */, 60.0 /* weight */, {442018474} /* stop ids */, + {43.8594864, 68.33320554776377} /* point */), + Gate(FeatureIdentifiers(kInvalidOsmId, 2 /* feature id */), true /* entrance */, + true /* exit */, 60.0 /* weight */, {442018465} /* stop ids */, + {43.9290544, 68.41120791512581} /* point */)}; OsmIdToFeatureIdsMap mapping; mapping[osm::Id(46116860)] = vector({0}); diff --git a/generator/transit_generator.cpp b/generator/transit_generator.cpp index 9c1df02716..9a04bcf1e3 100644 --- a/generator/transit_generator.cpp +++ b/generator/transit_generator.cpp @@ -59,7 +59,7 @@ Stop const & FindStopById(vector const & stops, StopId stopId) { ASSERT(is_sorted(stops.cbegin(), stops.cend(), LessById), ()); auto s1Id = equal_range(stops.cbegin(), stops.cend(), - Stop(stopId, OsmId(), kInvalidTransferId, {}, m2::PointD(), {}), + Stop(stopId, FeatureIdentifiers(), kInvalidTransferId, {}, m2::PointD(), {}), LessById); CHECK(s1Id.first != stops.cend(), ("No a stop with id:", stopId, "in stops:", stops)); CHECK_EQUAL(distance(s1Id.first, s1Id.second), 1, ("A stop with id:", stopId, "is not unique in stops:", stops)); @@ -217,7 +217,7 @@ void DeserializerFromJson::operator()(m2::PointD & p, char const * name) FromJSONObject(pointItem, "y", p.y); } -void DeserializerFromJson::operator()(OsmId & osmId, char const * name) +void DeserializerFromJson::operator()(FeatureIdentifiers & id, char const * name) { // Conversion osm id to feature id. string osmIdStr; @@ -225,12 +225,12 @@ void DeserializerFromJson::operator()(OsmId & osmId, char const * name) CHECK(strings::is_number(osmIdStr), ()); uint64_t osmIdNum; CHECK(strings::to_uint64(osmIdStr, osmIdNum), ()); - osm::Id const id(osmIdNum); - auto const it = m_osmIdToFeatureIds.find(id); + osm::Id const osmId(osmIdNum); + auto const it = m_osmIdToFeatureIds.find(osmId); CHECK(it != m_osmIdToFeatureIds.cend(), ()); CHECK_EQUAL(it->second.size(), 1, - ("Osm id:", id, "from transit graph doesn't present by a single feature in mwm.")); - osmId = OsmId(it->second[0]); + ("Osm id:", osmId, "from transit graph doesn't present by a single feature in mwm.")); + id = FeatureIdentifiers(osmId.EncodedId() /* osm id */, it->second[0] /* feature id */); } void BuildTransit(string const & mwmPath, string const & osmIdsToFeatureIdPath, diff --git a/generator/transit_generator.hpp b/generator/transit_generator.hpp index 61db472ee0..82ec5f5958 100644 --- a/generator/transit_generator.hpp +++ b/generator/transit_generator.hpp @@ -38,7 +38,7 @@ public: void operator()(std::string & s, char const * name = nullptr) { GetField(s, name); } void operator()(m2::PointD & p, char const * name = nullptr); - void operator()(OsmId & osmId, char const * name = nullptr); + void operator()(FeatureIdentifiers & id, char const * name = nullptr); template void operator()(std::vector & vs, char const * name = nullptr) diff --git a/routing_common/routing_common_tests/transit_test.cpp b/routing_common/routing_common_tests/transit_test.cpp index 5ac931bbe1..3a938817fc 100644 --- a/routing_common/routing_common_tests/transit_test.cpp +++ b/routing_common/routing_common_tests/transit_test.cpp @@ -76,8 +76,9 @@ UNIT_TEST(Transit_StopSerialization) TestSerialization(stop); } { - Stop stop(1234 /* id */, OsmId(5678 /* feature id */), 7 /* transfer id */, - {7, 8, 9, 10} /* line id */, {55.0, 37.0} /* point */, {} /* anchors */); + Stop stop(1234 /* id */, FeatureIdentifiers(kInvalidOsmId, 5678 /* feature id */), + 7 /* transfer id */, {7, 8, 9, 10} /* line id */, {55.0, 37.0} /* point */, + {} /* anchors */); TestSerialization(stop); } } @@ -96,8 +97,9 @@ UNIT_TEST(Transit_SingleMwmSegmentSerialization) UNIT_TEST(Transit_GateSerialization) { - Gate gate(OsmId(12345 /* feature id */), true /* entrance */, false /* exit */, 117.8 /* weight */, - {1, 2, 3} /* stop ids */, {30.0, 50.0} /* point */); + Gate gate(FeatureIdentifiers(kInvalidOsmId, 12345 /* feature id */), true /* entrance */, + false /* exit */, 117.8 /* weight */, {1, 2, 3} /* stop ids */, + {30.0, 50.0} /* point */); TestSerialization(gate); } diff --git a/routing_common/transit_serdes.hpp b/routing_common/transit_serdes.hpp index a1465fee33..eee0640740 100644 --- a/routing_common/transit_serdes.hpp +++ b/routing_common/transit_serdes.hpp @@ -97,6 +97,11 @@ public: } } + void operator()(FeatureIdentifiers const & id, char const * name = nullptr) + { + (*this)(id.GetFeatureId(), name); + } + template void operator()(std::vector const & vs, char const * /* name */ = nullptr) { @@ -166,6 +171,13 @@ public: p = Int64ToPoint(ReadVarInt(m_source), POINT_COORD_BITS); } + void operator()(FeatureIdentifiers & id, char const * name = nullptr) + { + FeatureId featureId; + operator()(featureId, name); + id = FeatureIdentifiers(kInvalidOsmId, featureId); + } + void operator()(vector & vs, char const * /* name */ = nullptr) { auto const size = ReadVarUint(m_source); diff --git a/routing_common/transit_types.cpp b/routing_common/transit_types.cpp index a253db49d2..bcb9d2fcb7 100644 --- a/routing_common/transit_types.cpp +++ b/routing_common/transit_types.cpp @@ -80,11 +80,11 @@ bool TitleAnchor::IsValid() const } // Stop ------------------------------------------------------------------------------------------- -Stop::Stop(StopId id, OsmId const & osmId, TransferId transferId, +Stop::Stop(StopId id, FeatureIdentifiers const & featureIdentifiers, TransferId transferId, std::vector const & lineIds, m2::PointD const & point, std::vector const & titleAnchors) : m_id(id) - , m_osmId(osmId) + , m_featureIdentifiers(featureIdentifiers) , m_transferId(transferId) , m_lineIds(lineIds) , m_point(point) @@ -95,7 +95,7 @@ Stop::Stop(StopId id, OsmId const & osmId, TransferId transferId, bool Stop::IsEqualForTesting(Stop const & stop) const { double constexpr kPointsEqualEpsilon = 1e-6; - return m_id == stop.m_id && m_osmId == stop.m_osmId && + return m_id == stop.m_id && m_featureIdentifiers.IsEqualForTesting(stop.m_featureIdentifiers) && m_transferId == stop.m_transferId && m_lineIds == stop.m_lineIds && my::AlmostEqualAbs(m_point, stop.m_point, kPointsEqualEpsilon) && m_titleAnchors == stop.m_titleAnchors; @@ -123,9 +123,9 @@ bool SingleMwmSegment::IsValid() const } // Gate ------------------------------------------------------------------------------------------- -Gate::Gate(OsmId const & osmId, bool entrance, bool exit, double weight, +Gate::Gate(FeatureIdentifiers const & featureIdentifiers, bool entrance, bool exit, double weight, std::vector const & stopIds, m2::PointD const & point) - : m_osmId(osmId) + : m_featureIdentifiers(featureIdentifiers) , m_entrance(entrance) , m_exit(exit) , m_weight(weight) @@ -136,8 +136,8 @@ Gate::Gate(OsmId const & osmId, bool entrance, bool exit, double weight, bool Gate::IsEqualForTesting(Gate const & gate) const { - return m_osmId == gate.m_osmId && m_entrance == gate.m_entrance && - m_exit == gate.m_exit && + return m_featureIdentifiers.IsEqualForTesting(gate.m_featureIdentifiers) && + m_entrance == gate.m_entrance && m_exit == gate.m_exit && my::AlmostEqualAbs(m_weight, gate.m_weight, kWeightEqualEpsilon) && m_stopIds == gate.m_stopIds && my::AlmostEqualAbs(m_point, gate.m_point, kPointsEqualEpsilon); diff --git a/routing_common/transit_types.hpp b/routing_common/transit_types.hpp index 93ea45619b..9b6eb6ebff 100644 --- a/routing_common/transit_types.hpp +++ b/routing_common/transit_types.hpp @@ -20,6 +20,7 @@ using StopId = uint64_t; using TransferId = uint64_t; using NetworkId = uint32_t; using FeatureId = uint32_t; +using OsmId = uint64_t; using ShapeId = uint32_t; using Weight = double; using Anchor = uint8_t; @@ -29,6 +30,7 @@ StopId constexpr kInvalidStopId = std::numeric_limits::max(); TransferId constexpr kInvalidTransferId = std::numeric_limits::max(); NetworkId constexpr kInvalidNetworkId = std::numeric_limits::max(); FeatureId constexpr kInvalidFeatureId = std::numeric_limits::max(); +OsmId constexpr kInvalidOsmId = std::numeric_limits::max(); ShapeId constexpr kInvalidShapeId = std::numeric_limits::max(); // Note. Weight may be a default param at json. The default value should be saved as uint32_t in mwm anyway. // To convert double to uint32_t at better accuracy |kInvalidWeight| should be close to real weight. @@ -78,23 +80,24 @@ public: static_assert(sizeof(TransitHeader) == 32, "Wrong header size of transit section."); -class OsmId +/// \brief This class represents osm id and feature id of the same feature. +class FeatureIdentifiers { public: - OsmId() = default; - explicit OsmId(FeatureId featureId) : m_featureId(featureId) {} + FeatureIdentifiers() = default; + FeatureIdentifiers(OsmId osmId, FeatureId const & featureId) : m_osmId(osmId), m_featureId(featureId) {} - bool operator==(OsmId const & rhs) const { return m_featureId == rhs.m_featureId; } - bool IsEqualForTesting(OsmId const & osmId) const { return *this == osmId; } + bool IsEqualForTesting(FeatureIdentifiers const & rhs) const { return m_featureId == rhs.m_featureId; } bool IsValid() const { return m_featureId != kInvalidFeatureId; } FeatureId GetFeatureId() const { return m_featureId; } private: DECLARE_TRANSIT_TYPE_FRIENDS - DECLARE_VISITOR_AND_DEBUG_PRINT(OsmId, visitor(m_featureId, "feature_id")) + DECLARE_VISITOR_AND_DEBUG_PRINT(FeatureIdentifiers, visitor(m_osmId, "osm_id"), + visitor(m_featureId, "feature_id")) - // |m_featureId| is a feature id corresponding to osm id which presents the class. + OsmId m_osmId = kInvalidOsmId; FeatureId m_featureId = kInvalidFeatureId; }; @@ -124,14 +127,15 @@ class Stop { public: Stop() = default; - Stop(StopId id, OsmId const & osmId, TransferId transferId, std::vector const & lineIds, - m2::PointD const & point, std::vector const & titleAnchors); + Stop(StopId id, FeatureIdentifiers const & featureIdentifiers, TransferId transferId, + std::vector const & lineIds, m2::PointD const & point, + std::vector const & titleAnchors); bool IsEqualForTesting(Stop const & stop) const; bool IsValid() const; StopId GetId() const { return m_id; } - FeatureId GetFeatureId() const { return m_osmId.GetFeatureId(); } + FeatureId GetFeatureId() const { return m_featureIdentifiers.GetFeatureId(); } TransferId GetTransferId() const { return m_transferId; } std::vector const & GetLineIds() const { return m_lineIds; } m2::PointD const & GetPoint() const { return m_point; } @@ -139,13 +143,13 @@ public: private: DECLARE_TRANSIT_TYPE_FRIENDS - DECLARE_VISITOR_AND_DEBUG_PRINT(Stop, visitor(m_id, "id"), visitor(m_osmId, "osm_id"), + DECLARE_VISITOR_AND_DEBUG_PRINT(Stop, visitor(m_id, "id"), visitor(m_featureIdentifiers, "osm_id"), visitor(m_transferId, "transfer_id"), visitor(m_lineIds, "line_ids"), visitor(m_point, "point"), visitor(m_titleAnchors, "title_anchors")) StopId m_id = kInvalidStopId; - OsmId m_osmId; + FeatureIdentifiers m_featureIdentifiers; TransferId m_transferId = kInvalidTransferId; std::vector m_lineIds; m2::PointD m_point; @@ -179,13 +183,13 @@ class Gate { public: Gate() = default; - Gate(OsmId const & osmId, bool entrance, bool exit, double weight, std::vector const & stopIds, - m2::PointD const & point); + Gate(FeatureIdentifiers const & featureIdentifiers, bool entrance, bool exit, double weight, + std::vector const & stopIds, m2::PointD const & point); bool IsEqualForTesting(Gate const & gate) const; bool IsValid() const; void SetBestPedestrianSegment(SingleMwmSegment const & s) { m_bestPedestrianSegment = s; }; - FeatureId GetFeatureId() const { return m_osmId.GetFeatureId(); } + FeatureId GetFeatureId() const { return m_featureIdentifiers.GetFeatureId(); } SingleMwmSegment const & GetBestPedestrianSegment() const { return m_bestPedestrianSegment; } bool GetEntrance() const { return m_entrance; } bool GetExit() const { return m_exit; } @@ -195,14 +199,14 @@ public: private: DECLARE_TRANSIT_TYPE_FRIENDS - DECLARE_VISITOR_AND_DEBUG_PRINT(Gate, visitor(m_osmId, "osm_id"), + DECLARE_VISITOR_AND_DEBUG_PRINT(Gate, visitor(m_featureIdentifiers, "osm_id"), visitor(m_bestPedestrianSegment, "best_pedestrian_segment"), visitor(m_entrance, "entrance"), visitor(m_exit, "exit"), visitor(m_weight, "weight"), visitor(m_stopIds, "stop_ids"), visitor(m_point, "point")) - // |m_osmId| contains feature id of a feature which represents gates. Usually it's a point feature. - OsmId m_osmId; + // |m_featureIdentifiers| contains feature id of a feature which represents gates. Usually it's a point feature. + FeatureIdentifiers m_featureIdentifiers; // |m_bestPedestrianSegment| is a segment which can be used for pedestrian routing to leave and enter the gate. // The segment may be invalid because of map date. If so there's no pedestrian segment which can be used // to reach the gate.