Merge pull request #2463 from mgsergio/opimize-matching

Reduce number of iterations in matching algorithm.
This commit is contained in:
ygorshenin 2016-03-24 12:09:30 +03:00
commit 53d4f2d9f0

View file

@ -9,6 +9,7 @@
#include "base/macros.hpp"
#include "std/algorithm.hpp"
#include "std/random.hpp"
#include "std/sstream.hpp"
#include "private.h"
@ -32,6 +33,31 @@ bool OsmFeatureHasTags(pugi::xml_node const & osmFt)
{
return osmFt.child("tag");
}
vector<m2::PointD> NaiveSample(vector<m2::PointD> const & source, size_t count)
{
count = min(count, source.size());
vector<m2::PointD> result;
result.reserve(count);
vector<size_t> indexes;
indexes.reserve(count);
mt19937 engine;
uniform_int_distribution<> distrib(0, source.size());
while (count--)
{
size_t index;
do
{
index = distrib(engine);
} while (find(begin(indexes), end(indexes), index) != end(indexes));
result.push_back(source[index]);
indexes.push_back(index);
}
return result;
}
} // namespace
namespace pugi
@ -115,9 +141,10 @@ XMLFeature ChangesetWrapper::GetMatchingNodeFeatureFromOSM(m2::PointD const & ce
XMLFeature ChangesetWrapper::GetMatchingAreaFeatureFromOSM(vector<m2::PointD> const & geometry)
{
// TODO: Make two/four requests using points on inscribed rectagle.
auto const kSamplePointsCount = 3;
bool hasRelation = false;
for (auto const & pt : geometry)
// Try several points in case of poor osm response.
for (auto const & pt : NaiveSample(geometry, kSamplePointsCount))
{
ms::LatLon const ll = MercatorBounds::ToLatLon(pt);
pugi::xml_document doc;