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

map_foreach_test.cpp « map_tests « map - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d4b13949fbcd3fc7e3b64c882e1a5ca657133159 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "../../base/SRC_FIRST.hpp"

#include "../../testing/testing.hpp"

#include "../../geometry/rect_intersect.hpp"

#include "../../platform/platform.hpp"

#include "../../map/feature_vec_model.hpp"

#include "../../indexer/data_header.hpp"
#include "../../indexer/scales.hpp"
#include "../../indexer/feature_visibility.hpp"
#include "../../indexer/feature_processor.hpp"
#include "../../indexer/classificator.hpp"

#include "../../base/logging.hpp"

#include "../../std/string.hpp"
#include "../../std/algorithm.hpp"
#include "../../std/iostream.hpp"

#include "../../base/start_mem_debug.hpp"


typedef vector<pair<FeatureType, string> > feature_cont_t;

class AccumulatorBase
{
  mutable string m_dbgString;
  feature_cont_t & m_cont;

protected:
  int m_scale;

  bool is_drawable(FeatureType const & f) const
  {
    m_dbgString = f.DebugString(m_scale);
    CHECK(m_dbgString == f.DebugString(m_scale), ());

    // Feature that hasn't any geometry for m_scale returns empty DebugString().
    return (!f.IsEmptyGeometry(m_scale) && feature::IsDrawableForIndex(f, m_scale));
  }

  void add(FeatureType const & f) const
  {
    m_cont.push_back(make_pair(f, m_dbgString));
  }

public:
  AccumulatorBase(m2::RectD const & r, feature_cont_t & cont)
    : m_cont(cont)
  {
    m_scale = scales::GetScaleLevel(r);
  }

  void operator() (FeatureType const & f) const
  {
    if (is_drawable(f))
      add(f);
  }
};

class IntersectCheck
{
  m2::RectD m_rect;

  m2::PointD m_prev;
  bool m_isPrev, m_intersect;

public:
  IntersectCheck(m2::RectD const & r)
    : m_rect(r), m_isPrev(false), m_intersect(false)
  {
  }

  void operator() (CoordPointT const & p)
  {
    if (m_intersect) return;

    m2::PointD pt(p.first, p.second);
    if (m_isPrev)
    {
      m2::PointD d1 = m_prev;
      m2::PointD d2 = pt;
      m_intersect = m2::Intersect(m_rect, d1, d2);
    }
    else
      m_isPrev = true;

    m_prev = pt;
  }

  bool IsIntersect() const { return m_intersect; }
};

class AccumulatorEtalon : public AccumulatorBase
{
  typedef AccumulatorBase base_type;

  m2::RectD m_rect;

  bool is_intersect(FeatureType const & f) const
  {
    IntersectCheck check(m_rect);
    f.ForEachPointRef(check, m_scale);
    return check.IsIntersect();
  }

public:
  AccumulatorEtalon(m2::RectD const & r, feature_cont_t & cont)
    : base_type(r, cont), m_rect(r)
  {
  }

  void operator() (FeatureType const & f, uint64_t /*offset*/) const
  {
    if (is_drawable(f) && is_intersect(f))
      add(f);
  }
};

// invoke this comparator to ensure that "sort" and "compare_sequence" use equal criterion
struct compare_strings
{
  int compare(string const & r1, string const & r2) const
  {
    if (r1 < r2)
      return -1;
    if (r2 < r1)
      return 1;
    return 0;
  }
  template <class T>
  int compare(T const & r1, T const & r2) const
  {
    return compare(r1.second, r2.second);
  }
  template <class T>
  bool operator() (T const & r1, T const & r2) const
  {
    return (compare(r1, r2) == -1);
  }
};

template <class TAccumulator, class TSource>
void for_each_in_rect(TSource & src, feature_cont_t & cont, m2::RectD const & rect)
{
  cont.clear();
  TAccumulator acc(rect, cont);
  src.ForEachFeature(rect, acc);
  sort(cont.begin(), cont.end(), compare_strings());
}

class file_source_t
{
  string m_fDat;
public:
  file_source_t(string const & fDat) : m_fDat(fDat) {}

  template <class ToDo>
  void ForEachFeature(m2::RectD const & /*rect*/, ToDo toDo)
  {
    feature::ForEachFromDat(m_fDat, toDo);
  }
};

/// "test" should contain all elements from etalon
template <class TCont, class TCompare>
bool compare_sequence(TCont const & etalon, TCont const & test, TCompare comp, size_t & errInd)
{
  if (test.size() < etalon.size())
    return false;

  typedef typename TCont::const_iterator iter_t;

  iter_t i1 = etalon.begin();
  iter_t i2 = test.begin();
  while (i1 != etalon.end() && i2 != test.end())
  {
    switch (comp.compare(*i1, *i2))
    {
    case 0:
      ++i1;
      ++i2;
      break;
    case -1:
      {
        errInd = distance(etalon.begin(), i1);
        return false;
      }
    case 1:
      ++i2;
      break;
    }
  }

  return true;
}

namespace
{
  class FindOffset
  {
    int m_level;
    pair<FeatureType, string> const & m_test;

  public:
    FindOffset(int level, pair<FeatureType, string> const & test)
      : m_level(level), m_test(test)
    {}

    void operator() (FeatureType const & f, uint64_t offset)
    {
      string const s = f.DebugString(m_level);
      if (s == m_test.second)
      {
        cout << s << endl << "Feature offset = " << offset << endl;

        cout << "Feature classificator types:\n";

        FeatureType::GetTypesFn getTypes;
        f.ForEachTypeRef(getTypes);
        for (size_t i = 0; i < getTypes.m_size; ++i)
          cout << classif().GetFullObjectName(getTypes.m_types[i]) << endl;
      }
    }
  };

  void RunTest(string const & path)
  {
    model::FeaturesFetcher src1;
    src1.InitClassificator();
    src1.AddMap(path);

    feature::DataHeader mapInfo;
    mapInfo.Load(FilesContainerR(path).GetReader(HEADER_FILE_TAG));

    vector<m2::RectD> rects;
    rects.push_back(mapInfo.GetBounds());

    while (!rects.empty())
    {
      m2::RectD r = rects.back();
      rects.pop_back();

      feature_cont_t v1, v2;
      for_each_in_rect<AccumulatorBase>(src1, v1, r);

      file_source_t src2(path);
      for_each_in_rect<AccumulatorEtalon>(src2, v2, r);

      int const level = scales::GetScaleLevel(r);

      size_t const emptyInd = size_t(-1);
      size_t errInd = emptyInd;
      if (!compare_sequence(v2, v1, compare_strings(), errInd))
      {
        if (errInd != emptyInd)
          src2.ForEachFeature(r, FindOffset(level, v2[errInd]));

        TEST(false, ("Failed for rect: ", r, "; Scale level = ", level, ". Etalon size = ", v2.size(), ". Index size = ", v1.size()));
      }

      if (!v2.empty() && (level < scales::GetUpperScale()))
      {
        m2::RectD r1, r2;
        r.DivideByGreaterSize(r1, r2);
        rects.push_back(r1);
        rects.push_back(r2);
      }
    }
  }

  void RunTestForChoice(string const & fName)
  {
    cout << "Run " << fName << "? (y/n)\n";
    char c;
    cin >> c;
    if (c == 'y')
      RunTest(GetPlatform().WritablePathForFile(fName + DATA_FILE_EXTENSION));
  }
}

UNIT_TEST(IndexForEachTest)
{
  RunTestForChoice("minsk-pass");
  RunTestForChoice("london-center");
}