From ee39d312984875d9341616ee9ad8ce76787467ff Mon Sep 17 00:00:00 2001 From: Maksim Andrianov Date: Wed, 15 Jan 2020 16:18:40 +0300 Subject: [PATCH] Review fixes. --- generator/affiliation.cpp | 8 ++-- generator/affiliation.hpp | 6 +-- generator/borders.cpp | 43 +++++-------------- generator/borders.hpp | 30 ++++++------- .../final_processor_intermediate_mwm.cpp | 2 +- 5 files changed, 34 insertions(+), 55 deletions(-) diff --git a/generator/affiliation.cpp b/generator/affiliation.cpp index 6bab02f1d9..4cf0ae66df 100644 --- a/generator/affiliation.cpp +++ b/generator/affiliation.cpp @@ -81,7 +81,7 @@ CountriesFilesAffiliation::GetAffiliations(m2::PointD const & point) const return countries; } -bool CountriesFilesAffiliation::HasRegionByName(std::string const & name) const +bool CountriesFilesAffiliation::HasCountryByName(std::string const & name) const { return m_countryPolygonsTree.HasRegionByName(name); } @@ -203,8 +203,8 @@ CountriesFilesIndexAffiliation::BuildIndex(const std::vector & net) std::vector> interCountries; for (borders::CountryPolygons const & cp : countries) { - cp.ForAnyRegion([&](auto const & region) { - if (!boost::geometry::intersects(region.Data(), box)) + cp.ForAnyPolygon([&](auto const & polygon) { + if (!boost::geometry::intersects(polygon.Data(), box)) return false; interCountries.emplace_back(cp); return true; @@ -256,7 +256,7 @@ std::vector SingleAffiliation::GetAffiliations(FeatureBuilder const return {m_filename}; } -bool SingleAffiliation::HasRegionByName(std::string const & name) const +bool SingleAffiliation::HasCountryByName(std::string const & name) const { return name == m_filename; } diff --git a/generator/affiliation.hpp b/generator/affiliation.hpp index f33c85f089..509048a3a8 100644 --- a/generator/affiliation.hpp +++ b/generator/affiliation.hpp @@ -20,7 +20,7 @@ public: // The method will return the names of the buckets to which the fb belongs. virtual std::vector GetAffiliations(FeatureBuilder const & fb) const = 0; virtual std::vector GetAffiliations(m2::PointD const & point) const = 0; - virtual bool HasRegionByName(std::string const & name) const = 0; + virtual bool HasCountryByName(std::string const & name) const = 0; }; @@ -33,7 +33,7 @@ public: std::vector GetAffiliations(FeatureBuilder const & fb) const override; std::vector GetAffiliations(m2::PointD const & point) const override; - bool HasRegionByName(std::string const & name) const override; + bool HasCountryByName(std::string const & name) const override; protected: borders::CountryPolygonsCollection const & m_countryPolygonsTree; @@ -68,7 +68,7 @@ public: // AffiliationInterface overrides: std::vector GetAffiliations(FeatureBuilder const &) const override; - bool HasRegionByName(std::string const & name) const override; + bool HasCountryByName(std::string const & name) const override; std::vector GetAffiliations(m2::PointD const & point) const override; private: diff --git a/generator/borders.cpp b/generator/borders.cpp index 959036b7f4..242f4b93e0 100644 --- a/generator/borders.cpp +++ b/generator/borders.cpp @@ -38,27 +38,6 @@ namespace borders { namespace { -class PolygonLoader -{ -public: - explicit PolygonLoader(m4::Tree & countries) : m_countries(countries) {} - - void operator()(std::string const & name, std::vector const & borders) - { - RegionsContainer regions; - for (m2::RegionD const & border : borders) - regions.Add(border, border.GetRect()); - - CountryPolygons countryPolygons(name, regions); - - for (m2::RegionD const & border : borders) - m_countries.Add(countryPolygons, border.GetRect()); - } - -private: - m4::Tree & m_countries; -}; - template void ForEachCountry(std::string const & baseDir, ToDo && toDo) { @@ -70,11 +49,11 @@ void ForEachCountry(std::string const & baseDir, ToDo && toDo) Platform::GetFilesByExt(bordersDir, BORDERS_EXTENSION, files); for (std::string file : files) { - std::vector borders; - if (LoadBorders(bordersDir + file, borders)) + std::vector polygons; + if (LoadBorders(bordersDir + file, polygons)) { base::GetNameWithoutExt(file); - toDo(file, borders); + toDo(file, polygons); } } } @@ -172,12 +151,12 @@ bool LoadBorders(std::string const & borderFile, std::vector & outB return false; } - m2::RegionD currentRegion; - while (ReadPolygon(stream, currentRegion, borderFile)) + m2::RegionD currentPolygon; + while (ReadPolygon(stream, currentPolygon, borderFile)) { - CHECK(currentRegion.IsValid(), ("Invalid region in", borderFile)); - outBorders.push_back(currentRegion); - currentRegion = m2::RegionD(); + CHECK(currentPolygon.IsValid(), ("Invalid region in", borderFile)); + outBorders.emplace_back(std::move(currentPolygon)); + currentPolygon = m2::RegionD(); } CHECK(!outBorders.empty(), ("No borders were loaded from", borderFile)); @@ -209,11 +188,11 @@ CountryPolygonsCollection LoadCountriesList(std::string const & baseDir) CountryPolygonsCollection countryPolygonsCollection; ForEachCountry(baseDir, [&](auto const & name, auto const & borders) { - RegionsContainer regions; + PolygonsTree polygons; for (m2::RegionD const & border : borders) - regions.Add(border, border.GetRect()); + polygons.Add(border, border.GetRect()); - countryPolygonsCollection.Add(CountryPolygons(name, regions)); + countryPolygonsCollection.Add(CountryPolygons(name, polygons)); }); LOG(LINFO, ("Countries loaded:", countryPolygonsCollection.GetSize())); diff --git a/generator/borders.hpp b/generator/borders.hpp index 730aa6d1ed..b697e1399c 100644 --- a/generator/borders.hpp +++ b/generator/borders.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #define BORDERS_DIR "borders/" @@ -39,16 +40,16 @@ namespace borders // unwanted consequences (for example, empty spaces may occur between mwms) // but currently we do not take any action against them. -using Region = m2::RegionD; -using RegionsContainer = m4::Tree; +using Polygon = m2::RegionD; +using PolygonsTree = m4::Tree; class CountryPolygons { public: CountryPolygons() = default; - explicit CountryPolygons(std::string const & name, RegionsContainer const & regions) + explicit CountryPolygons(std::string const & name, PolygonsTree const & regions) : m_name(name) - , m_regions(regions) + , m_polygons(regions) { } @@ -59,35 +60,35 @@ public: CountryPolygons & operator=(CountryPolygons const & other) = default; std::string const & GetName() const { return m_name; } - bool IsEmpty() const { return m_regions.IsEmpty(); } + bool IsEmpty() const { return m_polygons.IsEmpty(); } void Clear() { - m_regions.Clear(); + m_polygons.Clear(); m_name.clear(); } bool Contains(m2::PointD const & point) const { - return m_regions.ForAnyInRect(m2::RectD(point, point), [&](auto const & rgn) { + return m_polygons.ForAnyInRect(m2::RectD(point, point), [&](auto const & rgn) { return rgn.Contains(point); }); } template - void ForEachRegion(Do && fn) const + void ForEachPolygon(Do && fn) const { - m_regions.ForEach(std::forward(fn)); + m_polygons.ForEach(std::forward(fn)); } template - bool ForAnyRegion(Do && fn) const + bool ForAnyPolygon(Do && fn) const { - return m_regions.ForAny(std::forward(fn)); + return m_polygons.ForAny(std::forward(fn)); } private: std::string m_name; - RegionsContainer m_regions; + PolygonsTree m_polygons; }; class CountryPolygonsCollection @@ -98,9 +99,8 @@ public: void Add(CountryPolygons const & countryPolygons) { auto const it = m_countryPolygonsMap.emplace(countryPolygons.GetName(), countryPolygons); - countryPolygons.ForEachRegion([&](auto const & region){ - m_regionsTree.Add(it.first->second, region.GetRect()); - return true; + countryPolygons.ForEachPolygon([&](auto const & polygon) { + m_regionsTree.Add(it.first->second, polygon.GetRect()); }); } diff --git a/generator/final_processor_intermediate_mwm.cpp b/generator/final_processor_intermediate_mwm.cpp index 5c13b8fe8a..c149dd9e9a 100644 --- a/generator/final_processor_intermediate_mwm.cpp +++ b/generator/final_processor_intermediate_mwm.cpp @@ -128,7 +128,7 @@ std::string GetCountryNameFromTmpMwmPath(std::string filename) bool FilenameIsCountry(std::string const & filename, AffiliationInterface const & affiliation) { - return affiliation.HasRegionByName(GetCountryNameFromTmpMwmPath(filename)); + return affiliation.HasCountryByName(GetCountryNameFromTmpMwmPath(filename)); } class PlaceHelper