diff --git a/editor/editable_feature_source.cpp b/editor/editable_feature_source.cpp index 308324dc37..b341e9935b 100644 --- a/editor/editable_feature_source.cpp +++ b/editor/editable_feature_source.cpp @@ -14,15 +14,8 @@ bool EditableFeatureSource::GetModifiedFeature(uint32_t index, FeatureType & fea return editor.GetEditedFeature(m_handle.GetId(), index, feature); } -void EditableFeatureSource::ForEachInRectAndScale( - m2::RectD const & rect, int scale, std::function const & fn) const -{ - osm::Editor & editor = osm::Editor::Instance(); - editor.ForEachFeatureInMwmRectAndScale(m_handle.GetId(), fn, rect, scale); -} - -void EditableFeatureSource::ForEachInRectAndScale( - m2::RectD const & rect, int scale, std::function const & fn) const +void EditableFeatureSource::ForEachInRectAndScale(m2::RectD const & rect, int scale, + std::function const & fn) const { osm::Editor & editor = osm::Editor::Instance(); editor.ForEachFeatureInMwmRectAndScale(m_handle.GetId(), fn, rect, scale); diff --git a/editor/editable_feature_source.hpp b/editor/editable_feature_source.hpp index 3dadf59801..e9265b48b4 100644 --- a/editor/editable_feature_source.hpp +++ b/editor/editable_feature_source.hpp @@ -19,9 +19,7 @@ public: FeatureStatus GetFeatureStatus(uint32_t index) const override; bool GetModifiedFeature(uint32_t index, FeatureType & feature) const override; void ForEachInRectAndScale(m2::RectD const & rect, int scale, - std::function const & fn) const override; - void ForEachInRectAndScale(m2::RectD const & rect, int scale, - std::function const & fn) const override; + std::function const & fn) const override; }; class EditableFeatureSourceFactory : public FeatureSourceFactory diff --git a/editor/editor_tests/osm_editor_test.cpp b/editor/editor_tests/osm_editor_test.cpp index a28b417af1..b90d892d9c 100644 --- a/editor/editor_tests/osm_editor_test.cpp +++ b/editor/editor_tests/osm_editor_test.cpp @@ -117,16 +117,13 @@ void GenerateUploadedFeature(MwmSet::MwmId const & mwmId, xf.AttachToParentNode(created); } -template uint32_t CountFeaturesInRect(MwmSet::MwmId const & mwmId, m2::RectD const & rect) { auto & editor = osm::Editor::Instance(); int unused = 0; uint32_t counter = 0; - editor.ForEachFeatureInMwmRectAndScale(mwmId, [&counter](T const & ft) - { - ++counter; - }, rect, unused); + editor.ForEachFeatureInMwmRectAndScale(mwmId, [&counter](uint32_t index) { ++counter; }, rect, + unused); return counter; } @@ -769,13 +766,9 @@ void EditorTest::ForEachFeatureInMwmRectAndScaleTest() CreateCafeAtPoint({22.0, 22.0}, mwmId, emo); } - TEST_EQUAL(CountFeaturesInRect(mwmId, {0.0, 0.0, 2.0, 2.0}), 0, ()); - TEST_EQUAL(CountFeaturesInRect(mwmId, {9.0, 9.0, 21.0, 21.0}), 2, ()); - TEST_EQUAL(CountFeaturesInRect(mwmId, {9.0, 9.0, 23.0, 23.0}), 3, ()); - - TEST_EQUAL(CountFeaturesInRect(mwmId, {0.0, 0.0, 2.0, 2.0}), 0, ()); - TEST_EQUAL(CountFeaturesInRect(mwmId, {9.0, 9.0, 21.0, 21.0}), 2, ()); - TEST_EQUAL(CountFeaturesInRect(mwmId, {9.0, 9.0, 23.0, 23.0}), 3, ()); + TEST_EQUAL(CountFeaturesInRect(mwmId, {0.0, 0.0, 2.0, 2.0}), 0, ()); + TEST_EQUAL(CountFeaturesInRect(mwmId, {9.0, 9.0, 21.0, 21.0}), 2, ()); + TEST_EQUAL(CountFeaturesInRect(mwmId, {9.0, 9.0, 23.0, 23.0}), 3, ()); } void EditorTest::CreateNoteTest() diff --git a/editor/osm_editor.cpp b/editor/osm_editor.cpp index 9aa56222cf..4bfa77f5c7 100644 --- a/editor/osm_editor.cpp +++ b/editor/osm_editor.cpp @@ -486,8 +486,7 @@ bool Editor::RollBackChanges(FeatureID const & fid) } void Editor::ForEachFeatureInMwmRectAndScale(MwmSet::MwmId const & id, - TFeatureIDFunctor const & f, - m2::RectD const & rect, + TFeatureIndexFunctor const & f, m2::RectD const & rect, int /*scale*/) { auto const mwmFound = m_features.find(id); @@ -501,27 +500,7 @@ void Editor::ForEachFeatureInMwmRectAndScale(MwmSet::MwmId const & id, FeatureTypeInfo const & ftInfo = index.second; if (ftInfo.m_status == FeatureStatus::Created && rect.IsPointInside(ftInfo.m_feature.GetCenter())) - f(FeatureID(id, index.first)); - } -} - -void Editor::ForEachFeatureInMwmRectAndScale(MwmSet::MwmId const & id, - TFeatureTypeFunctor const & f, - m2::RectD const & rect, - int /*scale*/) -{ - auto mwmFound = m_features.find(id); - if (mwmFound == m_features.end()) - return; - - // TODO(AlexZ): Check that features are visible at this scale. - // Process only new (created) features. - for (auto & index : mwmFound->second) - { - FeatureTypeInfo & ftInfo = index.second; - if (ftInfo.m_status == FeatureStatus::Created && - rect.IsPointInside(ftInfo.m_feature.GetCenter())) - f(ftInfo.m_feature); + f(index.first); } } diff --git a/editor/osm_editor.hpp b/editor/osm_editor.hpp index da215aa9d2..51236cdaee 100644 --- a/editor/osm_editor.hpp +++ b/editor/osm_editor.hpp @@ -92,16 +92,9 @@ public: void OnMapDeregistered(platform::LocalCountryFile const & localFile) override; - using TFeatureIDFunctor = function; - void ForEachFeatureInMwmRectAndScale(MwmSet::MwmId const & id, - TFeatureIDFunctor const & f, - m2::RectD const & rect, - int scale); - using TFeatureTypeFunctor = function; - void ForEachFeatureInMwmRectAndScale(MwmSet::MwmId const & id, - TFeatureTypeFunctor const & f, - m2::RectD const & rect, - int scale); + using TFeatureIndexFunctor = function; + void ForEachFeatureInMwmRectAndScale(MwmSet::MwmId const & id, TFeatureIndexFunctor const & f, + m2::RectD const & rect, int scale); // TODO(mgsergio): Unify feature functions signatures. diff --git a/indexer/data_source.cpp b/indexer/data_source.cpp index 787c6afb98..7d26184a60 100644 --- a/indexer/data_source.cpp +++ b/indexer/data_source.cpp @@ -4,7 +4,6 @@ #include #include -#include using platform::CountryFile; using platform::LocalCountryFile; @@ -12,64 +11,29 @@ using namespace std; namespace { -template class ReadMWMFunctor { public: - using Fn = function; + using Fn = function; - ReadMWMFunctor(Fn const & fn, FeatureSourceFactory const & factory) : m_fn(fn), m_factory(factory) + ReadMWMFunctor(FeatureSourceFactory const & factory, Fn const & fn) : m_factory(factory), m_fn(fn) { } - template - enable_if_t::value> ProcessElement(FeatureSource & src, - uint32_t index) const - { - if (FeatureStatus::Deleted == src.GetFeatureStatus(index)) - return; - - m_fn(src.GetFeatureId(index)); - } - - template - enable_if_t::value> ProcessElement(FeatureSource & src, - uint32_t index) const - { - FeatureType feature; - switch (src.GetFeatureStatus(index)) - { - case FeatureStatus::Created: CHECK(false, ("Created features index should be generated.")); - case FeatureStatus::Deleted: - case FeatureStatus::Obsolete: return; - case FeatureStatus::Modified: - { - VERIFY(src.GetModifiedFeature(index, feature), ()); - break; - } - case FeatureStatus::Untouched: - { - src.GetOriginalFeature(index, feature); - break; - } - } - m_fn(feature); - } - - // Reads features visible at |scale| covered by |cov| from mwm and applies |m_fn| to it. + // Reads features visible at |scale| covered by |cov| from mwm and applies |m_fn| to them. // Feature reading process consists of two steps: untouched (original) features reading and // touched (created, edited etc.) features reading. void operator()(MwmSet::MwmHandle const & handle, covering::CoveringGetter & cov, int scale) const { auto src = m_factory(handle); - MwmValue const * pValue = handle.GetValue(); - if (pValue) + MwmValue const * mwmValue = handle.GetValue(); + if (mwmValue) { // Untouched (original) features reading. Applies covering |cov| to geometry index, gets // feature ids from it, gets untouched features by ids from |src| and applies |m_fn| by // ProcessElement. - feature::DataHeader const & header = pValue->GetHeader(); + feature::DataHeader const & header = mwmValue->GetHeader(); CheckUniqueIndexes checkUnique(header.GetFormat() >= version::Format::v5); // In case of WorldCoasts we should pass correct scale in ForEachInIntervalAndScale. @@ -79,7 +43,7 @@ public: // Use last coding scale for covering (see index_builder.cpp). covering::Intervals const & intervals = cov.Get(lastScale); - ScaleIndex index(pValue->m_cont.GetReader(INDEX_FILE_TAG), pValue->m_factory); + ScaleIndex index(mwmValue->m_cont.GetReader(INDEX_FILE_TAG), mwmValue->m_factory); // iterate through intervals for (auto const & i : intervals) @@ -87,20 +51,43 @@ public: index.ForEachInIntervalAndScale(i.first, i.second, scale, [&](uint32_t index) { if (!checkUnique(index)) return; - ProcessElement(*src, index); + m_fn(index, *src); }); } } // Check created features container. // Need to do it on a per-mwm basis, because Drape relies on features in a sorted order. // Touched (created, edited) features reading. - src->ForEachInRectAndScale(cov.GetRect(), scale, m_fn); + auto f = [&](uint32_t i) { m_fn(i, *src); }; + src->ForEachInRectAndScale(cov.GetRect(), scale, f); } private: - Fn m_fn; FeatureSourceFactory const & m_factory; + Fn m_fn; }; + +void ReadFeatureType(function const & fn, FeatureSource & src, uint32_t index) +{ + FeatureType feature; + switch (src.GetFeatureStatus(index)) + { + case FeatureStatus::Created: CHECK(false, ("Created features index should be generated.")); + case FeatureStatus::Deleted: + case FeatureStatus::Obsolete: return; + case FeatureStatus::Modified: + { + VERIFY(src.GetModifiedFeature(index, feature), ()); + break; + } + case FeatureStatus::Untouched: + { + src.GetOriginalFeature(index, feature); + break; + } + } + fn(feature); +} } // namespace // FeaturesLoaderGuard --------------------------------------------------------------------- @@ -236,19 +223,31 @@ void DataSource::ForEachInIntervals(ReaderCallback const & fn, covering::Coverin void DataSource::ForEachFeatureIDInRect(FeatureIdCallback const & f, m2::RectD const & rect, int scale) const { - ReadMWMFunctor readFunctor(f, *m_factory); + auto readFeatureId = [&f](uint32_t index, FeatureSource & src) { + if (src.GetFeatureStatus(index) != FeatureStatus::Deleted) + f(src.GetFeatureId(index)); + }; + + ReadMWMFunctor readFunctor(*m_factory, readFeatureId); ForEachInIntervals(readFunctor, covering::LowLevelsOnly, rect, scale); } void DataSource::ForEachInRect(FeatureCallback const & f, m2::RectD const & rect, int scale) const { - ReadMWMFunctor readFunctor(f, *m_factory); + auto readFeatureType = [&f](uint32_t index, FeatureSource & src) { + ReadFeatureType(f, src, index); + }; + ReadMWMFunctor readFunctor(*m_factory, readFeatureType); ForEachInIntervals(readFunctor, covering::ViewportWithLowLevels, rect, scale); } void DataSource::ForEachInScale(FeatureCallback const & f, int scale) const { - ReadMWMFunctor readFunctor(f, *m_factory); + auto readFeatureType = [&f](uint32_t index, FeatureSource & src) { + ReadFeatureType(f, src, index); + }; + + ReadMWMFunctor readFunctor(*m_factory, readFeatureType); ForEachInIntervals(readFunctor, covering::FullCover, m2::RectD::GetInfiniteRect(), scale); } @@ -259,7 +258,10 @@ void DataSource::ForEachInRectForMWM(FeatureCallback const & f, m2::RectD const if (handle.IsAlive()) { covering::CoveringGetter cov(rect, covering::ViewportWithLowLevels); - ReadMWMFunctor readFunctor(f, *m_factory); + auto readFeatureType = [&f](uint32_t index, FeatureSource & src) { + ReadFeatureType(f, src, index); + }; + ReadMWMFunctor readFunctor(*m_factory, readFeatureType); readFunctor(handle, cov, scale); } } diff --git a/indexer/feature_source.cpp b/indexer/feature_source.cpp index 86452d9bc1..254065a0a6 100644 --- a/indexer/feature_source.cpp +++ b/indexer/feature_source.cpp @@ -53,11 +53,6 @@ bool FeatureSource::GetModifiedFeature(uint32_t index, FeatureType & feature) co } void FeatureSource::ForEachInRectAndScale(m2::RectD const & rect, int scale, - function const & fn) const -{ -} - -void FeatureSource::ForEachInRectAndScale(m2::RectD const & rect, int scale, - function const & fn) const + function const & fn) const { } diff --git a/indexer/feature_source.hpp b/indexer/feature_source.hpp index 93e8fc4ee5..6e22f4d939 100644 --- a/indexer/feature_source.hpp +++ b/indexer/feature_source.hpp @@ -42,9 +42,7 @@ public: virtual bool GetModifiedFeature(uint32_t index, FeatureType & feature) const; virtual void ForEachInRectAndScale(m2::RectD const & rect, int scale, - std::function const & fn) const; - virtual void ForEachInRectAndScale(m2::RectD const & rect, int scale, - std::function const & fn) const; + std::function const & fn) const; protected: MwmSet::MwmHandle const & m_handle;