From 0be3b828ecce1f8992163b02649fd23ce427568b Mon Sep 17 00:00:00 2001 From: tatiana-yan Date: Thu, 17 Jan 2019 14:59:42 +0300 Subject: [PATCH] [indexer][tests] Add tests for LocalityIndex top size. --- coding/geometry_coding.hpp | 22 +++++--- indexer/indexer_tests/locality_index_test.cpp | 56 +++++++++++-------- indexer/locality_object.hpp | 25 ++++++++- 3 files changed, 71 insertions(+), 32 deletions(-) diff --git a/coding/geometry_coding.hpp b/coding/geometry_coding.hpp index bfe2b52205..cc55a67fb1 100644 --- a/coding/geometry_coding.hpp +++ b/coding/geometry_coding.hpp @@ -291,21 +291,27 @@ void SaveInnerTriangles(std::vector const & points, GeometryCodingPa SaveInner(&coding::EncodeTriangleStrip, points, params, sink); } +inline void StripToTriangles(size_t count, OutPointsT const & strip, OutPointsT & triangles) +{ + CHECK_GREATER_OR_EQUAL(count, 2, ()); + triangles.clear(); + triangles.reserve((count - 2) * 3); + for (size_t i = 2; i < count; ++i) + { + triangles.push_back(strip[i - 2]); + triangles.push_back(strip[i - 1]); + triangles.push_back(strip[i]); + } +} + inline void const * LoadInnerTriangles(void const * pBeg, size_t count, GeometryCodingParams const & params, OutPointsT & triangles) { CHECK_GREATER_OR_EQUAL(count, 2, ()); - triangles.clear(); OutPointsT points; void const * res = LoadInner(&coding::DecodeTriangleStrip, pBeg, count, params, points); - triangles.reserve((count - 2) * 3); - for (size_t i = 2; i < count; ++i) - { - triangles.push_back(points[i - 2]); - triangles.push_back(points[i - 1]); - triangles.push_back(points[i]); - } + StripToTriangles(count, points, triangles); return res; } diff --git a/indexer/indexer_tests/locality_index_test.cpp b/indexer/indexer_tests/locality_index_test.cpp index 811313a49d..c15b8a8eff 100644 --- a/indexer/indexer_tests/locality_index_test.cpp +++ b/indexer/indexer_tests/locality_index_test.cpp @@ -73,10 +73,10 @@ UNIT_TEST(BuildLocalityIndexTest) { LocalityObjectVector objects; objects.m_objects.resize(4); - objects.m_objects[0].SetForTests(1, m2::PointD{0, 0}); - objects.m_objects[1].SetForTests(2, m2::PointD{1, 0}); - objects.m_objects[2].SetForTests(3, m2::PointD{1, 1}); - objects.m_objects[3].SetForTests(4, m2::PointD{0, 1}); + objects.m_objects[0].SetForTesting(1, m2::PointD{0, 0}); + objects.m_objects[1].SetForTesting(2, m2::PointD{1, 0}); + objects.m_objects[2].SetForTesting(3, m2::PointD{1, 1}); + objects.m_objects[3].SetForTesting(4, m2::PointD{0, 1}); vector localityIndex; MemWriter> writer(localityIndex); @@ -94,10 +94,10 @@ UNIT_TEST(LocalityIndexRankTest) { LocalityObjectVector objects; objects.m_objects.resize(4); - objects.m_objects[0].SetForTests(1, m2::PointD{1, 0}); - objects.m_objects[1].SetForTests(2, m2::PointD{2, 0}); - objects.m_objects[2].SetForTests(3, m2::PointD{3, 0}); - objects.m_objects[3].SetForTests(4, m2::PointD{4, 0}); + objects.m_objects[0].SetForTesting(1, m2::PointD{1, 0}); + objects.m_objects[1].SetForTesting(2, m2::PointD{2, 0}); + objects.m_objects[2].SetForTesting(3, m2::PointD{3, 0}); + objects.m_objects[3].SetForTesting(4, m2::PointD{4, 0}); vector localityIndex; MemWriter> writer(localityIndex); @@ -125,17 +125,19 @@ UNIT_TEST(LocalityIndexRankTest) UNIT_TEST(LocalityIndexTopSizeTest) { LocalityObjectVector objects; - objects.m_objects.resize(7); + objects.m_objects.resize(8); // Same cell. - objects.m_objects[0].SetForTests(1, m2::PointD{1.0, 0.0}); - objects.m_objects[1].SetForTests(2, m2::PointD{1.0, 0.0}); - objects.m_objects[2].SetForTests(3, m2::PointD{1.0, 0.0}); - objects.m_objects[3].SetForTests(4, m2::PointD{1.0, 0.0}); + objects.m_objects[0].SetForTesting(1, m2::PointD{1.0, 0.0}); + objects.m_objects[1].SetForTesting(2, m2::PointD{1.0, 0.0}); + objects.m_objects[2].SetForTesting(3, m2::PointD{1.0, 0.0}); + objects.m_objects[3].SetForTesting(4, m2::PointD{1.0, 0.0}); // Another close cell. - objects.m_objects[4].SetForTests(5, m2::PointD{1.0, 1.0}); - objects.m_objects[5].SetForTests(6, m2::PointD{1.0, 1.0}); + objects.m_objects[4].SetForTesting(5, m2::PointD{1.0, 1.0}); + objects.m_objects[5].SetForTesting(6, m2::PointD{1.0, 1.0}); // Far cell. - objects.m_objects[6].SetForTests(7, m2::PointD{10.0, 10.0}); + objects.m_objects[6].SetForTesting(7, m2::PointD{10.0, 10.0}); + // The big object contains all points and must be returned on any query. + objects.m_objects[7].SetForTesting(8, m2::RectD{0.0, 0.0, 10.0, 10.0}); vector localityIndex; MemWriter> writer(localityIndex); @@ -143,17 +145,25 @@ UNIT_TEST(LocalityIndexTopSizeTest) MemReader reader(localityIndex.data(), localityIndex.size()); indexer::GeoObjectsIndex index(reader); - TEST_EQUAL(GetRankedIds(index, m2::PointD{1.0, 0.0} /* center */, - m2::PointD{10.0, 10.0} /* border */, 4 /* topSize */) + + // There is only one object (the big object) at this point. + TEST_EQUAL(GetRankedIds(index, m2::PointD{2.0, 2.0} /* center */, + m2::PointD{2.0, 2.0} /* border */, 8 /* topSize */) .size(), - 4, ()); + 1, ()); + + // There are 4 small objects and 1 big object at this point. + TEST_EQUAL(GetRankedIds(index, m2::PointD{1.0, 0.0} /* center */, + m2::PointD{10.0, 10.0} /* border */, 5 /* topSize */) + .size(), + 5, ()); // 4 objects are indexed at the central cell. Index does not guarantee the order but must - // return 4 objects. + // return 4 objects from central cell and the big object. TEST_EQUAL(GetRankedIds(index, m2::PointD{1.0, 0.0} /* center */, m2::PointD{10.0, 10.0} /* border */, 3 /* topSize */) .size(), - 4, ()); + 5, ()); // At the {1.0, 1.0} point there are also 2 objects, but it's not a central cell, index must // return 5 (topSize) objects. @@ -170,8 +180,8 @@ UNIT_TEST(LocalityIndexTopSizeTest) 5, ()); TEST_EQUAL(GetRankedIds(index, m2::PointD{4.0, 0.0} /* center */, - m2::PointD{10.0, 10.0} /* border */, 7 /* topSize */) + m2::PointD{10.0, 10.0} /* border */, 8 /* topSize */) .size(), - 7, ()); + 8, ()); } } // namespace diff --git a/indexer/locality_object.hpp b/indexer/locality_object.hpp index d6c390d5dd..27c50b5093 100644 --- a/indexer/locality_object.hpp +++ b/indexer/locality_object.hpp @@ -1,6 +1,10 @@ #pragma once #include "geometry/point2d.hpp" +#include "geometry/polygon.hpp" +#include "geometry/rect2d.hpp" + +#include "coding/geometry_coding.hpp" #include "base/buffer_vector.hpp" #include "base/geo_object_id.hpp" @@ -41,13 +45,32 @@ public: toDo(m_triangles[i - 2], m_triangles[i - 1], m_triangles[i]); } - void SetForTests(uint64_t id, m2::PointD point) + void SetForTesting(uint64_t id, m2::PointD point) { m_id = id; m_points.clear(); m_points.push_back(point); } + void SetForTesting(uint64_t id, m2::RectD rect) + { + m_id = id; + + m_points.clear(); + m_points.push_back(rect.LeftBottom()); + m_points.push_back(rect.RightBottom()); + m_points.push_back(rect.RightTop()); + m_points.push_back(rect.LeftTop()); + + buffer_vector strip; + auto const index = FindSingleStrip( + m_points.size(), IsDiagonalVisibleFunctor::const_iterator>( + m_points.begin(), m_points.end())); + MakeSingleStripFromIndex(index, m_points.size(), + [&](size_t i) { strip.push_back(m_points[i]); }); + serial::StripToTriangles(strip.size(), strip, m_triangles); + } + private: uint64_t m_id = 0; std::vector m_points;