From aed8ac3dca68b3e9f67aa0816f90c7fb6736a320 Mon Sep 17 00:00:00 2001 From: LaGrunge Date: Mon, 14 Oct 2019 19:00:06 +0300 Subject: [PATCH] rm altitude_loader.hpp --- generator/generator_tests/altitude_test.cpp | 255 -------------------- indexer/CMakeLists.txt | 2 - indexer/altitude_loader.cpp | 124 ---------- indexer/altitude_loader.hpp | 49 ---- 4 files changed, 430 deletions(-) delete mode 100644 generator/generator_tests/altitude_test.cpp delete mode 100644 indexer/altitude_loader.cpp delete mode 100644 indexer/altitude_loader.hpp diff --git a/generator/generator_tests/altitude_test.cpp b/generator/generator_tests/altitude_test.cpp deleted file mode 100644 index d79dda1..0000000 --- a/generator/generator_tests/altitude_test.cpp +++ /dev/null @@ -1,255 +0,0 @@ -#include "testing/testing.hpp" - -#include "generator/altitude_generator.hpp" -#include "generator/generator_tests_support/test_feature.hpp" -#include "generator/generator_tests_support/test_mwm_builder.hpp" - -#include "routing/road_graph.hpp" -#include "routing/routing_helpers.hpp" - -#include "indexer/altitude_loader.hpp" -#include "indexer/classificator_loader.hpp" -#include "indexer/data_source.hpp" -#include "indexer/feature_processor.hpp" - -#include "geometry/point2d.hpp" - -#include "platform/country_file.hpp" -#include "platform/platform.hpp" -#include "platform/platform_tests_support/scoped_dir.hpp" -#include "platform/platform_tests_support/scoped_file.hpp" -#include "platform/platform_tests_support/writable_dir_changer.hpp" - -#include "base/file_name_utils.hpp" -#include "base/logging.hpp" -#include "base/scope_guard.hpp" - -#include "defines.hpp" - -#include - -using namespace feature; -using namespace generator; -using namespace platform; -using namespace platform::tests_support; -using namespace routing; - -namespace -{ -// These tests generate mwms with altitude sections and then check if altitudes -// in the mwms are correct. The mwms are initialized with different sets of features. -// There are several restrictions for these features: -// * all of them are linear -// * all coords of these features should be integer (it's necessary for easy implementation of -// MockAltitudeGetter) -// * if accoding to one feature a point has some altitude, the point of another feature -// with the same coords has to have the same altitude -// @TODO(bykoianko) Add ability to add to the tests not road features without altitude information. - -// Directory name for creating test mwm and temporary files. -std::string const kTestDir = "altitude_generation_test"; -// Temporary mwm name for testing. -std::string const kTestMwm = "test"; - -struct Point3D -{ - Point3D(int32_t x, int32_t y, TAltitude a) : m_point(x, y), m_altitude(a) {} - - m2::PointI m_point; - TAltitude m_altitude; -}; - -using TPoint3DList = vector; - -TPoint3DList const kRoad1 = {{0, -1, -1}, {0, 0, 0}, {0, 1, 1}}; -TPoint3DList const kRoad2 = {{0, 1, 1}, {5, 1, 1}, {10, 1, 1}}; -TPoint3DList const kRoad3 = {{10, 1, 1}, {15, 6, 100}, {20, 11, 110}}; -TPoint3DList const kRoad4 = {{-10, 1, -1}, {-20, 6, -100}, {-20, -11, -110}}; - -class MockAltitudeGetter : public AltitudeGetter -{ -public: - using TMockAltitudes = map; - - explicit MockAltitudeGetter(vector const & roads) - { - for (TPoint3DList const & geom3D : roads) - { - for (auto const & g : geom3D) - { - auto const it = m_altitudes.find(g.m_point); - if (it != m_altitudes.cend()) - { - CHECK_EQUAL(it->second, g.m_altitude, - ("Point", it->first, "is set with two different altitudes.")); - continue; - } - m_altitudes[g.m_point] = g.m_altitude; - } - } - } - - // AltitudeGetter overrides: - TAltitude GetAltitude(m2::PointD const & p) override - { - m2::PointI const rounded(static_cast(round(p.x)), static_cast(round(p.y))); - auto const it = m_altitudes.find(rounded); - if (it == m_altitudes.end()) - return kInvalidAltitude; - - return it->second; - } - -private: - - TMockAltitudes m_altitudes; -}; - -class MockNoAltitudeGetter : public AltitudeGetter -{ -public: - TAltitude GetAltitude(m2::PointD const &) override - { - return kInvalidAltitude; - } -}; - -vector ExtractPoints(TPoint3DList const & geom3D) -{ - vector result; - for (Point3D const & p : geom3D) - result.push_back(m2::PointD(p.m_point)); - return result; -} - -void BuildMwmWithoutAltitudes(vector const & roads, LocalCountryFile & country) -{ - generator::tests_support::TestMwmBuilder builder(country, feature::DataHeader::MapType::Country); - - for (TPoint3DList const & geom3D : roads) - builder.Add(generator::tests_support::TestStreet(ExtractPoints(geom3D), std::string(), std::string())); -} - -void TestAltitudes(DataSource const & dataSource, MwmSet::MwmId const & mwmId, - std::string const & mwmPath, bool hasAltitudeExpected, - AltitudeGetter & expectedAltitudes) -{ - AltitudeLoader loader(dataSource, mwmId); - TEST_EQUAL(loader.HasAltitudes(), hasAltitudeExpected, ()); - - auto processor = [&expectedAltitudes, &loader](FeatureType & f, uint32_t const & id) { - f.ParseGeometry(FeatureType::BEST_GEOMETRY); - size_t const pointsCount = f.GetPointsCount(); - TAltitudes const altitudes = loader.GetAltitudes(id, pointsCount); - - if (!routing::IsRoad(feature::TypesHolder(f))) - { - TEST(altitudes.empty(), ()); - return; - } - - TEST_EQUAL(altitudes.size(), pointsCount, ()); - - for (size_t i = 0; i < pointsCount; ++i) - { - TAltitude const fromGetter = expectedAltitudes.GetAltitude(f.GetPoint(i)); - TAltitude const expected = (fromGetter == kInvalidAltitude ? kDefaultAltitudeMeters : fromGetter); - TEST_EQUAL(expected, altitudes[i], ("A wrong altitude")); - } - }; - feature::ForEachFromDat(mwmPath, processor); -} - -void TestAltitudesBuilding(vector const & roads, bool hasAltitudeExpected, - AltitudeGetter & altitudeGetter) -{ - classificator::Load(); - Platform & platform = GetPlatform(); - std::string const testDirFullPath = base::JoinPath(platform.WritableDir(), kTestDir); - - // Building mwm without altitude section. - LocalCountryFile country(testDirFullPath, CountryFile(kTestMwm), 1); - ScopedDir testScopedDir(kTestDir); - ScopedFile testScopedMwm(base::JoinPath(kTestDir, kTestMwm + DATA_FILE_EXTENSION), - ScopedFile::Mode::Create); - - BuildMwmWithoutAltitudes(roads, country); - - // Adding altitude section to mwm. - auto const mwmPath = testScopedMwm.GetFullPath(); - BuildRoadAltitudes(mwmPath, altitudeGetter); - - // Reading from mwm and testing altitude information. - FrozenDataSource dataSource; - auto const regResult = dataSource.RegisterMap(country); - TEST_EQUAL(regResult.second, MwmSet::RegResult::Success, ()); - - TestAltitudes(dataSource, regResult.first /* mwmId */, mwmPath, hasAltitudeExpected, altitudeGetter); -} - -void TestBuildingAllFeaturesHaveAltitude(vector const & roads, bool hasAltitudeExpected) -{ - MockAltitudeGetter altitudeGetter(roads); - TestAltitudesBuilding(roads, hasAltitudeExpected, altitudeGetter); -} - -void TestBuildingNoFeatureHasAltitude(vector const & roads, bool hasAltitudeExpected) -{ - MockNoAltitudeGetter altitudeGetter; - TestAltitudesBuilding(roads, hasAltitudeExpected, altitudeGetter); -} - -UNIT_TEST(AltitudeGenerationTest_ZeroFeatures) -{ - vector const roads = {}; - TestBuildingAllFeaturesHaveAltitude(roads, false /* hasAltitudeExpected */); -} - -UNIT_TEST(AltitudeGenerationTest_OneRoad) -{ - vector const roads = {kRoad1}; - TestBuildingAllFeaturesHaveAltitude(roads, true /* hasAltitudeExpected */); -} - -UNIT_TEST(AltitudeGenerationTest_TwoConnectedRoads) -{ - vector const roads = {kRoad1, kRoad2}; - TestBuildingAllFeaturesHaveAltitude(roads, true /* hasAltitudeExpected */); -} - -UNIT_TEST(AltitudeGenerationTest_TwoDisconnectedRoads) -{ - vector const roads = {kRoad1, kRoad3}; - TestBuildingAllFeaturesHaveAltitude(roads, true /* hasAltitudeExpected */); -} - -UNIT_TEST(AltitudeGenerationTest_ThreeRoads) -{ - vector const roads = {kRoad1, kRoad2, kRoad3}; - TestBuildingAllFeaturesHaveAltitude(roads, true /* hasAltitudeExpected */); -} - -UNIT_TEST(AltitudeGenerationTest_FourRoads) -{ - vector const roads = {kRoad1, kRoad2, kRoad3, kRoad4}; - TestBuildingAllFeaturesHaveAltitude(roads, true /* hasAltitudeExpected */); -} - -UNIT_TEST(AltitudeGenerationTest_ZeroFeaturesWithoutAltitude) -{ - vector const roads = {}; - TestBuildingNoFeatureHasAltitude(roads, false /* hasAltitudeExpected */); -} - -UNIT_TEST(AltitudeGenerationTest_OneRoadWithoutAltitude) -{ - vector const roads = {kRoad1}; - TestBuildingNoFeatureHasAltitude(roads, false /* hasAltitudeExpected */); -} - -UNIT_TEST(AltitudeGenerationTest_FourRoadsWithoutAltitude) -{ - vector const roads = {kRoad1, kRoad2, kRoad3, kRoad4}; - TestBuildingNoFeatureHasAltitude(roads, false /* hasAltitudeExpected */); -} -} // namespace diff --git a/indexer/CMakeLists.txt b/indexer/CMakeLists.txt index befbe31..2ad25d9 100644 --- a/indexer/CMakeLists.txt +++ b/indexer/CMakeLists.txt @@ -2,8 +2,6 @@ project(indexer) set( SRC - altitude_loader.cpp - altitude_loader.hpp borders.cpp borders.hpp brands_holder.cpp diff --git a/indexer/altitude_loader.cpp b/indexer/altitude_loader.cpp deleted file mode 100644 index ed6b447..0000000 --- a/indexer/altitude_loader.cpp +++ /dev/null @@ -1,124 +0,0 @@ -#include "indexer/altitude_loader.hpp" - -#include "indexer/data_source.hpp" - -#include "coding/reader.hpp" -#include "coding/succinct_mapper.hpp" - -#include "base/logging.hpp" -#include "base/stl_helpers.hpp" - -#include "defines.hpp" - -#include - - -using namespace std; - -namespace -{ -template -void LoadAndMap(size_t dataSize, ReaderSource & src, TCont & cont, - unique_ptr & region) -{ - vector data(dataSize); - src.Read(data.data(), data.size()); - region = make_unique(move(data)); - coding::MapVisitor visitor(region->ImmutableData()); - cont.map(visitor); -} -} // namespace - -namespace feature -{ -AltitudeLoader::AltitudeLoader(DataSource const & dataSource, MwmSet::MwmId const & mwmId) - : m_handle(dataSource.GetMwmHandleById(mwmId)) -{ - if (!m_handle.IsAlive()) - return; - - auto const & mwmValue = *m_handle.GetValue(); - - m_countryFileName = mwmValue.GetCountryFileName(); - - if (mwmValue.GetHeader().GetFormat() < version::Format::v8) - return; - - if (!mwmValue.m_cont.IsExist(ALTITUDES_FILE_TAG)) - return; - - try - { - m_reader = make_unique(mwmValue.m_cont.GetReader(ALTITUDES_FILE_TAG)); - ReaderSource src(*m_reader); - m_header.Deserialize(src); - - LoadAndMap(m_header.GetAltitudeAvailabilitySize(), src, m_altitudeAvailability, - m_altitudeAvailabilityRegion); - LoadAndMap(m_header.GetFeatureTableSize(), src, m_featureTable, m_featureTableRegion); - } - catch (Reader::OpenException const & e) - { - m_header.Reset(); - LOG(LERROR, ("File", m_countryFileName, "Error while reading", ALTITUDES_FILE_TAG, "section.", e.Msg())); - } -} - -bool AltitudeLoader::HasAltitudes() const -{ - return m_reader != nullptr && m_header.m_minAltitude != kInvalidAltitude; -} - -TAltitudes const & AltitudeLoader::GetAltitudes(uint32_t featureId, size_t pointCount) -{ - if (!HasAltitudes()) - { - // The version of mwm is less than version::Format::v8 or there's no altitude section in mwm. - return m_cache.insert(make_pair(featureId, TAltitudes(pointCount, kDefaultAltitudeMeters))).first->second; - } - - auto const it = m_cache.find(featureId); - if (it != m_cache.end()) - return it->second; - - if (!m_altitudeAvailability[featureId]) - { - return m_cache.insert(make_pair(featureId, TAltitudes(pointCount, m_header.m_minAltitude))) - .first->second; - } - - uint64_t const r = m_altitudeAvailability.rank(featureId); - CHECK_LESS(r, m_altitudeAvailability.size(), ("Feature Id", featureId, "of", m_countryFileName)); - uint64_t const offset = m_featureTable.select(r); - CHECK_LESS_OR_EQUAL(offset, m_featureTable.size(), ("Feature Id", featureId, "of", m_countryFileName)); - - uint64_t const altitudeInfoOffsetInSection = m_header.m_altitudesOffset + offset; - CHECK_LESS(altitudeInfoOffsetInSection, m_reader->Size(), ("Feature Id", featureId, "of", m_countryFileName)); - - try - { - Altitudes altitudes; - ReaderSource src(*m_reader); - src.Skip(altitudeInfoOffsetInSection); - bool const isDeserialized = altitudes.Deserialize(m_header.m_minAltitude, pointCount, - m_countryFileName, featureId, src); - - bool const allValid = isDeserialized - && none_of(altitudes.m_altitudes.begin(), altitudes.m_altitudes.end(), - [](TAltitude a) { return a == kInvalidAltitude; }); - if (!allValid) - { - LOG(LERROR, ("Only a part point of a feature has a valid altitdue. Altitudes: ", altitudes.m_altitudes, - ". Feature Id", featureId, "of", m_countryFileName)); - return m_cache.insert(make_pair(featureId, TAltitudes(pointCount, m_header.m_minAltitude))).first->second; - } - - return m_cache.insert(make_pair(featureId, move(altitudes.m_altitudes))).first->second; - } - catch (Reader::OpenException const & e) - { - LOG(LERROR, ("Feature Id", featureId, "of", m_countryFileName, ". Error while getting altitude data:", e.Msg())); - return m_cache.insert(make_pair(featureId, TAltitudes(pointCount, m_header.m_minAltitude))).first->second; - } -} -} // namespace feature diff --git a/indexer/altitude_loader.hpp b/indexer/altitude_loader.hpp deleted file mode 100644 index 0617c56..0000000 --- a/indexer/altitude_loader.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once -#include "indexer/feature_altitude.hpp" -#include "indexer/mwm_set.hpp" - -#include "coding/memory_region.hpp" - -#include -#include -#include - -#include "3party/succinct/rs_bit_vector.hpp" - -class DataSource; - -namespace feature -{ -// @TODO(bykoianko) |m_altitudeAvailability| and |m_featureTable| are saved without -// taking into account endianness. It should be fixed. The plan is -// * to use one bit form AltitudeHeader::m_version for keeping information about endianness. (Zero -// should be used for big endian.) -// * to check the endianness of the reader and the bit while reading and to use an appropriate -// methods for reading. -class AltitudeLoader -{ -public: - AltitudeLoader(DataSource const & dataSource, MwmSet::MwmId const & mwmId); - - /// \returns altitude of feature with |featureId|. All items of the returned vector are valid - /// or the returned vector is empty. - TAltitudes const & GetAltitudes(uint32_t featureId, size_t pointCount); - - bool HasAltitudes() const; - - void ClearCache() { m_cache.clear(); } - -private: - std::unique_ptr m_altitudeAvailabilityRegion; - std::unique_ptr m_featureTableRegion; - - succinct::rs_bit_vector m_altitudeAvailability; - succinct::elias_fano m_featureTable; - - std::unique_ptr m_reader; - std::map m_cache; - AltitudeHeader m_header; - std::string m_countryFileName; - MwmSet::MwmHandle m_handle; -}; -} // namespace feature