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

cities_boundaries_serdes.hpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 406683f94b1a5a4b38a3d351783565ac2e9e2fa8 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#pragma once

#include "indexer/city_boundary.hpp"

#include "coding/bit_streams.hpp"
#include "coding/elias_coder.hpp"
#include "coding/geometry_coding.hpp"
#include "coding/pointd_to_pointu.hpp"
#include "coding/reader.hpp"
#include "coding/varint.hpp"
#include "coding/write_to_sink.hpp"

#include "geometry/bounding_box.hpp"
#include "geometry/calipers_box.hpp"
#include "geometry/diamond_box.hpp"
#include "geometry/mercator.hpp"
#include "geometry/point2d.hpp"

#include "base/assert.hpp"
#include "base/checked_cast.hpp"
#include "base/logging.hpp"
#include "base/macros.hpp"
#include "base/visitor.hpp"

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
#include <vector>

namespace indexer
{
template <typename Sink>
class CitiesBoundariesEncoder
{
public:
  struct Visitor
  {
  public:
    Visitor(Sink & sink, serial::GeometryCodingParams const & params)
      : m_sink(sink), m_params(params), m_last(params.GetBasePoint())
    {
    }

    void operator()(m2::PointD const & p) { return (*this)(ToU(p)); }

    void operator()(m2::PointU const & p)
    {
      WriteVarUint(m_sink, coding::EncodePointDeltaAsUint(p, m_last));
      m_last = p;
    }

    void operator()(m2::BoundingBox const & bbox)
    {
      auto const min = ToU(bbox.Min());
      auto const max = ToU(bbox.Max());

      (*this)(min);
      EncodeNonNegativePointDelta(min, max);
    }

    void operator()(m2::CalipersBox const & cbox)
    {
      auto ps = cbox.Points();

      CHECK(!ps.empty(), ());
      CHECK_LESS_OR_EQUAL(ps.size(), 4, ());
      CHECK(ps.size() != 3, ());

      if (ps.size() == 1)
      {
        auto const p0 = ps[0];
        while (ps.size() != 4)
          ps.push_back(p0);
      }
      else if (ps.size() == 2)
      {
        auto const p0 = ps[0];
        auto const p1 = ps[1];

        ps.push_back(p1);
        ps.push_back(p0);
      }

      ASSERT_EQUAL(ps.size(), 4, ());

      auto const us = ToU(ps);

      (*this)(us[0]);
      coding::EncodePointDelta(m_sink, us[0], us[1]);
      coding::EncodePointDelta(m_sink, us[0], us[3]);
    }

    void operator()(m2::DiamondBox const & dbox)
    {
      auto const ps = ToU(dbox.Points());
      auto const base = ps[0];
      auto const next = ps[1];
      auto const prev = ps[3];

      (*this)(base);

      ASSERT_GREATER_OR_EQUAL(next.x, base.x, ());
      ASSERT_GREATER_OR_EQUAL(next.y, base.y, ());
      WriteVarUint(m_sink, next.x >= base.x ? next.x - base.x : 0);

      ASSERT_GREATER_OR_EQUAL(prev.x, base.x, ());
      ASSERT_LESS_OR_EQUAL(prev.y, base.y, ());
      WriteVarUint(m_sink, prev.x >= base.x ? prev.x - base.x : 0);
    }

    template <typename R>
    void operator()(R const & r)
    {
      r.Visit(*this);
    }

  private:
    m2::PointU ToU(m2::PointD const & p) const
    {
      return PointDToPointU(p, m_params.GetCoordBits());
    }

    std::vector<m2::PointU> ToU(std::vector<m2::PointD> const & ps) const
    {
      std::vector<m2::PointU> us(ps.size());
      for (size_t i = 0; i < ps.size(); ++i)
        us[i] = ToU(ps[i]);
      return us;
    }

    // Writes the difference of two 2d vectors to |m_sink|. The vector |next|
    // must have both its coordinates greater than or equal to those
    // of |curr|. While this condition is unlikely when encoding polylines,
    // this method may be useful when encoding rectangles, in particular
    // bounding boxes of shapes.
    void EncodeNonNegativePointDelta(m2::PointU const & curr, m2::PointU const & next)
    {
      ASSERT_GREATER_OR_EQUAL(next.x, curr.x, ());
      ASSERT_GREATER_OR_EQUAL(next.y, curr.y, ());

      // Paranoid checks due to possible floating point artifacts
      // here. In general, next.x >= curr.x and next.y >= curr.y.
      auto const dx = next.x >= curr.x ? next.x - curr.x : 0;
      auto const dy = next.y >= curr.y ? next.y - curr.y : 0;
      WriteVarUint(m_sink, dx);
      WriteVarUint(m_sink, dy);
    }

    Sink & m_sink;
    serial::GeometryCodingParams m_params;
    m2::PointU m_last;
  };

  CitiesBoundariesEncoder(Sink & sink, serial::GeometryCodingParams const & params)
    : m_sink(sink), m_visitor(sink, params)
  {
  }

  void operator()(std::vector<std::vector<CityBoundary>> const & boundaries)
  {
    WriteVarUint(m_sink, boundaries.size());

    {
      BitWriter<Sink> writer(m_sink);
      for (auto const & bs : boundaries)
      {
        CHECK_LESS(bs.size(), std::numeric_limits<uint64_t>::max(), ());
        auto const success =
            coding::GammaCoder::Encode(writer, static_cast<uint64_t>(bs.size()) + 1);
        ASSERT(success, ());
        UNUSED_VALUE(success);
      }
    }

    for (auto const & bs : boundaries)
    {
      for (auto const & b : bs)
        m_visitor(b);
    }
  }

private:
  Sink & m_sink;
  Visitor m_visitor;
};

template <typename Source>
class CitiesBoundariesDecoderV0
{
public:
  struct Visitor
  {
  public:
    Visitor(Source & source, serial::GeometryCodingParams const & params)
      : m_source(source), m_params(params), m_last(params.GetBasePoint())
    {
    }

    void operator()(m2::PointD & p)
    {
      m2::PointU u;
      (*this)(u);
      p = FromU(u);
    }

    void operator()(m2::PointU & p)
    {
      p = coding::DecodePointDeltaFromUint(ReadVarUint<uint64_t>(m_source), m_last);
      m_last = p;
    }

    void operator()(m2::BoundingBox & bbox)
    {
      m2::PointU min;
      (*this)(min);
      auto const max = DecodeNonNegativePointDelta(min);

      bbox = m2::BoundingBox();
      bbox.Add(FromU(min));
      bbox.Add(FromU(max));
    }

    void operator()(m2::CalipersBox & cbox)
    {
      std::vector<m2::PointU> us(4);
      (*this)(us[0]);
      us[1] = coding::DecodePointDelta(m_source, us[0]);
      us[3] = coding::DecodePointDelta(m_source, us[0]);

      auto ps = FromU(us);
      auto const dp = ps[3] - ps[0];
      ps[2] = ps[1] + dp;

      cbox = m2::CalipersBox(ps);
    }

    void operator()(m2::DiamondBox & dbox)
    {
      m2::PointU base;
      (*this)(base);

      auto const nx = ReadVarUint<uint32_t>(m_source);
      auto const px = ReadVarUint<uint32_t>(m_source);

      dbox = m2::DiamondBox();
      dbox.Add(FromU(base));
      dbox.Add(FromU(base + m2::PointU(nx, nx)));
      dbox.Add(FromU(base + m2::PointU(px, -px)));
      dbox.Add(FromU(base + m2::PointU(nx + px, nx - px)));
    }

    template <typename R>
    void operator()(R & r)
    {
      r.Visit(*this);
    }

  private:
    m2::PointD FromU(m2::PointU const & u) const
    {
      return PointUToPointD(u, m_params.GetCoordBits());
    }

    std::vector<m2::PointD> FromU(std::vector<m2::PointU> const & us) const
    {
      std::vector<m2::PointD> ps(us.size());
      for (size_t i = 0; i < us.size(); ++i)
        ps[i] = FromU(us[i]);
      return ps;
    }

    // Reads the encoded difference from |m_source| and returns the
    // point equal to |base| + difference. It is guaranteed that
    // both coordinates of difference are non-negative.
    m2::PointU DecodeNonNegativePointDelta(m2::PointU const & base)
    {
      auto const dx = ReadVarUint<uint32_t>(m_source);
      auto const dy = ReadVarUint<uint32_t>(m_source);
      return m2::PointU(base.x + dx, base.y + dy);
    }

    Source & m_source;
    serial::GeometryCodingParams const m_params;
    m2::PointU m_last;
  };

  CitiesBoundariesDecoderV0(Source & source, serial::GeometryCodingParams const & params)
    : m_source(source), m_visitor(source, params)
  {
  }

  void operator()(std::vector<std::vector<CityBoundary>> & boundaries)
  {
    auto const size = ReadVarUint<uint64_t>(m_source);
    boundaries.resize(size);

    {
      BitReader<Source> reader(m_source);
      for (auto & bs : boundaries)
      {
        auto const size = coding::GammaCoder::Decode(reader);
        ASSERT_GREATER_OR_EQUAL(size, 1, ());
        bs.resize(size - 1);
      }
    }

    for (auto & bs : boundaries)
    {
      for (auto & b : bs)
        m_visitor(b);
    }
  }

private:
  Source & m_source;
  Visitor m_visitor;
};

struct CitiesBoundariesSerDes
{
  template <typename Sink>
  struct WriteToSinkVisitor
  {
    WriteToSinkVisitor(Sink & sink) : m_sink(sink) {}

    template <typename T>
    std::enable_if_t<std::is_integral<T>::value || std::is_enum<T>::value, void> operator()(
        T const & t, char const * /* name */ = nullptr)
    {
      WriteToSink(m_sink, t);
    }

    template <typename T>
    std::enable_if_t<!std::is_integral<T>::value && !std::is_enum<T>::value, void> operator()(
        T const & t, char const * /* name */ = nullptr)
    {
      t.Visit(*this);
    }

    Sink & m_sink;
  };

  template <typename Source>
  struct ReadFromSourceVisitor
  {
    ReadFromSourceVisitor(Source & source) : m_source(source) {}

    template <typename T>
    std::enable_if_t<std::is_integral<T>::value || std::is_enum<T>::value, void> operator()(
        T & t, char const * /* name */ = nullptr)
    {
      t = ReadPrimitiveFromSource<T>(m_source);
    }

    template <typename T>
    std::enable_if_t<!std::is_integral<T>::value && !std::is_enum<T>::value, void> operator()(
        T & t, char const * /* name */ = nullptr)
    {
      t.Visit(*this);
    }

    Source & m_source;
  };

  static uint8_t constexpr kLatestVersion = 0;

  struct HeaderV0
  {
    static uint8_t const kDefaultCoordBits = 19;

    HeaderV0() {}

    DECLARE_VISITOR(visitor(m_coordBits, "coordBits"))

    uint8_t m_coordBits = kDefaultCoordBits;
  };

  template <typename Sink>
  static void Serialize(Sink & sink, std::vector<std::vector<CityBoundary>> const & boundaries)
  {
    uint8_t const version = kLatestVersion;

    WriteToSinkVisitor<Sink> visitor(sink);
    visitor(version);

    HeaderV0 const header;
    visitor(header);

    serial::GeometryCodingParams const params(
        header.m_coordBits, m2::PointD(MercatorBounds::minX, MercatorBounds::minY));
    CitiesBoundariesEncoder<Sink> encoder(sink, params);
    encoder(boundaries);
  }

  template <typename Source>
  static void Deserialize(Source & source, std::vector<std::vector<CityBoundary>> & boundaries,
                          double & precision)
  {
    ReadFromSourceVisitor<Source> visitor(source);

    uint8_t version;
    visitor(version);

    CHECK_EQUAL(version, 0, ());

    HeaderV0 header;
    visitor(header);

    auto const wx = MercatorBounds::maxX - MercatorBounds::minX;
    auto const wy = MercatorBounds::maxY - MercatorBounds::minY;
    precision = std::max(wx, wy) / pow(2, header.m_coordBits);

    serial::GeometryCodingParams const params(
        header.m_coordBits, m2::PointD(MercatorBounds::minX, MercatorBounds::minY));
    CitiesBoundariesDecoderV0<Source> decoder(source, params);
    decoder(boundaries);
  }
};
}  // namespace indexer