Welcome to mirror list, hosted at ThFree Co, Russian Federation.

locality_index_test.cpp « indexer_tests « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 291b8ba342f0b6e31e53ee03e753a285171289dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "testing/testing.hpp"

#include "indexer/cell_id.hpp"
#include "indexer/locality_index.hpp"
#include "indexer/locality_index_builder.hpp"
#include "indexer/locality_object.hpp"

#include "coding/file_container.hpp"
#include "coding/mmap_reader.hpp"
#include "coding/reader.hpp"

#include "geometry/rect2d.hpp"

#include "base/osm_id.hpp"

#include <algorithm>
#include <cstdint>
#include <set>
#include <utility>
#include <vector>

using namespace indexer;
using namespace std;

namespace
{
struct LocalityObjectVector
{
  template <typename ToDo>
  void ForEach(ToDo && toDo) const
  {
    for_each(m_objects.cbegin(), m_objects.cend(), forward<ToDo>(toDo));
  }

  vector<LocalityObject> m_objects;
};

template <class ObjectsVector, class Writer>
void BuildGeoObjectsIndex(ObjectsVector const & objects, Writer & writer,
                          string const & tmpFilePrefix)
{
  auto coverLocality = [](indexer::LocalityObject const & o, int cellDepth,
                          uint64_t cellPenaltyArea) {
    return covering::CoverGeoObject(o, cellDepth, cellPenaltyArea);
  };
  return covering::BuildLocalityIndex<ObjectsVector, Writer, kGeoObjectsDepthLevels>(
      objects, writer, coverLocality, tmpFilePrefix);
}

using Ids = set<uint64_t>;
using RankedIds = vector<uint64_t>;

template <typename LocalityIndex>
Ids GetIds(LocalityIndex const & index, m2::RectD const & rect)
{
  Ids ids;
  index.ForEachInRect([&ids](osm::Id const & id) { ids.insert(id.EncodedId()); }, rect);
  return ids;
};

template <typename LocalityIndex>
RankedIds GetRankedIds(LocalityIndex const & index, m2::PointD const & center,
                       m2::PointD const & border, uint32_t topSize)
{
  RankedIds ids;
  index.ForClosestToPoint([&ids](osm::Id const & id) { ids.push_back(id.EncodedId()); }, center,
                          MercatorBounds::DistanceOnEarth(center, border), topSize);
  return ids;
};

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});

  vector<char> localityIndex;
  MemWriter<vector<char>> writer(localityIndex);
  BuildGeoObjectsIndex(objects, writer, "tmp");
  MemReader reader(localityIndex.data(), localityIndex.size());

  indexer::GeoObjectsIndex<MemReader> index(reader);

  TEST_EQUAL(GetIds(index, m2::RectD{-0.5, -0.5, 0.5, 0.5}), (Ids{1}), ());
  TEST_EQUAL(GetIds(index, m2::RectD{0.5, -0.5, 1.5, 1.5}), (Ids{2, 3}), ());
  TEST_EQUAL(GetIds(index, m2::RectD{-0.5, -0.5, 1.5, 1.5}), (Ids{1, 2, 3, 4}), ());
}

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});

  vector<char> localityIndex;
  MemWriter<vector<char>> writer(localityIndex);
  BuildGeoObjectsIndex(objects, writer, "tmp");
  MemReader reader(localityIndex.data(), localityIndex.size());

  indexer::GeoObjectsIndex<MemReader> index(reader);
  TEST_EQUAL(GetRankedIds(index, m2::PointD{1, 0} /* center */, m2::PointD{4, 0} /* border */,
                          4 /* topSize */),
             (vector<uint64_t>{1, 2, 3, 4}), ());
  TEST_EQUAL(GetRankedIds(index, m2::PointD{1, 0} /* center */, m2::PointD{3, 0} /* border */,
                          4 /* topSize */),
             (vector<uint64_t>{1, 2, 3}), ());
  TEST_EQUAL(GetRankedIds(index, m2::PointD{4, 0} /* center */, m2::PointD{1, 0} /* border */,
                          4 /* topSize */),
             (vector<uint64_t>{4, 3, 2, 1}), ());
  TEST_EQUAL(GetRankedIds(index, m2::PointD{4, 0} /* center */, m2::PointD{1, 0} /* border */,
                          2 /* topSize */),
             (vector<uint64_t>{4, 3}), ());
  TEST_EQUAL(GetRankedIds(index, m2::PointD{3, 0} /* center */, m2::PointD{0, 0} /* border */,
                          1 /* topSize */),
             (vector<uint64_t>{3}), ());
}

UNIT_TEST(LocalityIndexTopSizeTest)
{
  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{1, 0});
  objects.m_objects[2].SetForTests(3, m2::PointD{1, 0});
  objects.m_objects[3].SetForTests(4, m2::PointD{1, 0});

  vector<char> localityIndex;
  MemWriter<vector<char>> writer(localityIndex);
  BuildGeoObjectsIndex(objects, writer, "tmp");
  MemReader reader(localityIndex.data(), localityIndex.size());

  indexer::GeoObjectsIndex<MemReader> index(reader);
  TEST_EQUAL(GetRankedIds(index, m2::PointD{1, 0} /* center */, m2::PointD{0, 0} /* border */,
                          4 /* topSize */)
                 .size(),
             4, ());
  TEST_EQUAL(GetRankedIds(index, m2::PointD{1, 0} /* center */, m2::PointD{0, 0} /* border */,
                          3 /* topSize */)
                 .size(),
             3, ());
  TEST_EQUAL(GetRankedIds(index, m2::PointD{4, 0} /* center */, m2::PointD{0, 0} /* border */,
                          1 /* topSize */)
                 .size(),
             1, ());
}

}  // namespace