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

geometry_coding.cpp « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a3c84d8fd8266f7d5451762710c683e0aff5114e (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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#include "coding/geometry_coding.hpp"

#include "coding/point_to_integer.hpp"

#include "geometry/mercator.hpp"

#include "base/assert.hpp"

#include <complex>
#include <stack>

using namespace std;

namespace
{
inline m2::PointU ClampPoint(m2::PointU const & maxPoint, m2::Point<double> const & point)
{
  using uvalue_t = m2::PointU::value_type;
  // return m2::PointU(my::clamp(static_cast<uvalue_t>(point.x), static_cast<uvalue_t>(0),
  // maxPoint.x),
  //                  my::clamp(static_cast<uvalue_t>(point.y), static_cast<uvalue_t>(0),
  //                  maxPoint.y));

  return m2::PointU(
      static_cast<uvalue_t>(my::clamp(point.x, 0.0, static_cast<double>(maxPoint.x))),
      static_cast<uvalue_t>(my::clamp(point.y, 0.0, static_cast<double>(maxPoint.y))));
}

struct edge_less_p0
{
  using edge_t = tesselator::Edge;

  bool operator()(edge_t const & e1, edge_t const & e2) const
  {
    return (e1.m_p[0] == e2.m_p[0]) ? (e1.m_side < e2.m_side) : (e1.m_p[0] < e2.m_p[0]);
  }
  bool operator()(edge_t const & e1, int e2) const { return e1.m_p[0] < e2; }
  bool operator()(int e1, edge_t const & e2) const { return e1 < e2.m_p[0]; }
};
}  // namespace

namespace coding
{
bool TestDecoding(InPointsT const & points, m2::PointU const & basePoint,
                  m2::PointU const & maxPoint, OutDeltasT const & deltas,
                  void (*fnDecode)(InDeltasT const & deltas, m2::PointU const & basePoint,
                                   m2::PointU const & maxPoint, OutPointsT & points))
{
  size_t const count = points.size();

  vector<m2::PointU> decoded;
  decoded.resize(count);

  OutPointsT decodedA(decoded);
  fnDecode(make_read_adapter(deltas), basePoint, maxPoint, decodedA);

  for (size_t i = 0; i < count; ++i)
    ASSERT_EQUAL(points[i], decoded[i], ());
  return true;
}

m2::PointU PredictPointInPolyline(m2::PointU const & maxPoint, m2::PointU const & p1,
                                  m2::PointU const & p2)
{
  // return ClampPoint(maxPoint, m2::PointI64(p1) + m2::PointI64(p1) - m2::PointI64(p2));
  // return ClampPoint(maxPoint, m2::PointI64(p1) + (m2::PointI64(p1) - m2::PointI64(p2)) / 2);
  return ClampPoint(maxPoint, m2::PointD(p1) + (m2::PointD(p1) - m2::PointD(p2)) / 2.0);
}

uint64_t EncodePointDeltaAsUint(m2::PointU const & actual, m2::PointU const & prediction)
{
  return bits::BitwiseMerge(
      bits::ZigZagEncode(static_cast<int32_t>(actual.x) - static_cast<int32_t>(prediction.x)),
      bits::ZigZagEncode(static_cast<int32_t>(actual.y) - static_cast<int32_t>(prediction.y)));
}

m2::PointU DecodePointDeltaFromUint(uint64_t delta, m2::PointU const & prediction)
{
  uint32_t x, y;
  bits::BitwiseSplit(delta, x, y);
  return m2::PointU(prediction.x + bits::ZigZagDecode(x), prediction.y + bits::ZigZagDecode(y));
}

m2::PointU PredictPointInPolyline(m2::PointU const & maxPoint, m2::PointU const & p1,
                                  m2::PointU const & p2, m2::PointU const & p3)
{
  CHECK_NOT_EQUAL(p2, p3, ());

  complex<double> const c1(p1.x, p1.y);
  complex<double> const c2(p2.x, p2.y);
  complex<double> const c3(p3.x, p3.y);
  complex<double> const d = (c1 - c2) / (c2 - c3);
  complex<double> const c0 = c1 + (c1 - c2) * polar(0.5, 0.5 * arg(d));

  /*
  complex<double> const c1(p1.x, p1.y);
  complex<double> const c2(p2.x, p2.y);
  complex<double> const c3(p3.x, p3.y);
  complex<double> const d = (c1 - c2) / (c2 - c3);
  complex<double> const c01 = c1 + (c1 - c2) * polar(0.5, arg(d));
  complex<double> const c02 = c1 + (c1 - c2) * complex<double>(0.5, 0.0);
  complex<double> const c0 = (c01 + c02) * complex<double>(0.5, 0.0);
  */

  return ClampPoint(maxPoint, m2::PointD(c0.real(), c0.imag()));
}

m2::PointU PredictPointInTriangle(m2::PointU const & maxPoint, m2::PointU const & p1,
                                  m2::PointU const & p2, m2::PointU const & p3)
{
  // parallelogram prediction
  return ClampPoint(maxPoint, m2::PointD(p1 + p2 - p3));
}

void EncodePolylinePrev1(InPointsT const & points, m2::PointU const & basePoint,
                         m2::PointU const & maxPoint, OutDeltasT & deltas)
{
  size_t const count = points.size();
  if (count > 0)
  {
    deltas.push_back(EncodePointDeltaAsUint(points[0], basePoint));
    for (size_t i = 1; i < count; ++i)
      deltas.push_back(EncodePointDeltaAsUint(points[i], points[i - 1]));
  }

  ASSERT(TestDecoding(points, basePoint, maxPoint, deltas, &DecodePolylinePrev1), ());
}

void DecodePolylinePrev1(InDeltasT const & deltas, m2::PointU const & basePoint,
                         m2::PointU const & /*maxPoint*/, OutPointsT & points)
{
  size_t const count = deltas.size();
  if (count > 0)
  {
    points.push_back(DecodePointDeltaFromUint(deltas[0], basePoint));
    for (size_t i = 1; i < count; ++i)
      points.push_back(DecodePointDeltaFromUint(deltas[i], points.back()));
  }
}

void EncodePolylinePrev2(InPointsT const & points, m2::PointU const & basePoint,
                         m2::PointU const & maxPoint, OutDeltasT & deltas)
{
  size_t const count = points.size();
  if (count > 0)
  {
    deltas.push_back(EncodePointDeltaAsUint(points[0], basePoint));
    if (count > 1)
    {
      deltas.push_back(EncodePointDeltaAsUint(points[1], points[0]));
      for (size_t i = 2; i < count; ++i)
        deltas.push_back(EncodePointDeltaAsUint(
            points[i], PredictPointInPolyline(maxPoint, points[i - 1], points[i - 2])));
    }
  }

  ASSERT(TestDecoding(points, basePoint, maxPoint, deltas, &DecodePolylinePrev2), ());
}

void DecodePolylinePrev2(InDeltasT const & deltas, m2::PointU const & basePoint,
                         m2::PointU const & maxPoint, OutPointsT & points)
{
  size_t const count = deltas.size();
  if (count > 0)
  {
    points.push_back(DecodePointDeltaFromUint(deltas[0], basePoint));
    if (count > 1)
    {
      points.push_back(DecodePointDeltaFromUint(deltas[1], points.back()));
      for (size_t i = 2; i < count; ++i)
      {
        size_t const n = points.size();
        points.push_back(DecodePointDeltaFromUint(
            deltas[i], PredictPointInPolyline(maxPoint, points[n - 1], points[n - 2])));
      }
    }
  }
}

void EncodePolylinePrev3(InPointsT const & points, m2::PointU const & basePoint,
                         m2::PointU const & maxPoint, OutDeltasT & deltas)
{
  ASSERT_LESS_OR_EQUAL(basePoint.x, maxPoint.x, (basePoint, maxPoint));
  ASSERT_LESS_OR_EQUAL(basePoint.y, maxPoint.y, (basePoint, maxPoint));

  size_t const count = points.size();
  if (count > 0)
  {
    deltas.push_back(EncodePointDeltaAsUint(points[0], basePoint));
    if (count > 1)
    {
      deltas.push_back(EncodePointDeltaAsUint(points[1], points[0]));
      if (count > 2)
      {
        m2::PointU const prediction = PredictPointInPolyline(maxPoint, points[1], points[0]);
        deltas.push_back(EncodePointDeltaAsUint(points[2], prediction));
        for (size_t i = 3; i < count; ++i)
        {
          m2::PointU const prediction =
              PredictPointInPolyline(maxPoint, points[i - 1], points[i - 2], points[i - 3]);
          deltas.push_back(EncodePointDeltaAsUint(points[i], prediction));
        }
      }
    }
  }

  ASSERT(TestDecoding(points, basePoint, maxPoint, deltas, &DecodePolylinePrev3), ());
}

void DecodePolylinePrev3(InDeltasT const & deltas, m2::PointU const & basePoint,
                         m2::PointU const & maxPoint, OutPointsT & points)
{
  ASSERT_LESS_OR_EQUAL(basePoint.x, maxPoint.x, (basePoint, maxPoint));
  ASSERT_LESS_OR_EQUAL(basePoint.y, maxPoint.y, (basePoint, maxPoint));

  size_t const count = deltas.size();
  if (count > 0)
  {
    points.push_back(DecodePointDeltaFromUint(deltas[0], basePoint));
    if (count > 1)
    {
      m2::PointU const pt0 = points.back();
      points.push_back(DecodePointDeltaFromUint(deltas[1], pt0));
      if (count > 2)
      {
        points.push_back(DecodePointDeltaFromUint(
            deltas[2], PredictPointInPolyline(maxPoint, points.back(), pt0)));
        for (size_t i = 3; i < count; ++i)
        {
          size_t const n = points.size();
          m2::PointU const prediction =
              PredictPointInPolyline(maxPoint, points[n - 1], points[n - 2], points[n - 3]);
          points.push_back(DecodePointDeltaFromUint(deltas[i], prediction));
        }
      }
    }
  }
}

void EncodePolyline(InPointsT const & points, m2::PointU const & basePoint,
                    m2::PointU const & maxPoint, OutDeltasT & deltas)
{
  EncodePolylinePrev2(points, basePoint, maxPoint, deltas);
}

void DecodePolyline(InDeltasT const & deltas, m2::PointU const & basePoint,
                    m2::PointU const & maxPoint, OutPointsT & points)
{
  DecodePolylinePrev2(deltas, basePoint, maxPoint, points);
}

void EncodeTriangleStrip(InPointsT const & points, m2::PointU const & basePoint,
                         m2::PointU const & maxPoint, OutDeltasT & deltas)
{
  size_t const count = points.size();
  if (count > 0)
  {
    ASSERT_GREATER(count, 2, ());

    deltas.push_back(EncodePointDeltaAsUint(points[0], basePoint));
    deltas.push_back(EncodePointDeltaAsUint(points[1], points[0]));
    deltas.push_back(EncodePointDeltaAsUint(points[2], points[1]));

    for (size_t i = 3; i < count; ++i)
    {
      m2::PointU const prediction =
          PredictPointInTriangle(maxPoint, points[i - 1], points[i - 2], points[i - 3]);
      deltas.push_back(EncodePointDeltaAsUint(points[i], prediction));
    }
  }
}

void DecodeTriangleStrip(InDeltasT const & deltas, m2::PointU const & basePoint,
                         m2::PointU const & maxPoint, OutPointsT & points)
{
  size_t const count = deltas.size();
  if (count > 0)
  {
    ASSERT_GREATER(count, 2, ());

    points.push_back(DecodePointDeltaFromUint(deltas[0], basePoint));
    points.push_back(DecodePointDeltaFromUint(deltas[1], points.back()));
    points.push_back(DecodePointDeltaFromUint(deltas[2], points.back()));

    for (size_t i = 3; i < count; ++i)
    {
      size_t const n = points.size();
      m2::PointU const prediction =
          PredictPointInTriangle(maxPoint, points[n - 1], points[n - 2], points[n - 3]);
      points.push_back(DecodePointDeltaFromUint(deltas[i], prediction));
    }
  }
}
}  // namespace coding

namespace serial
{
// GeometryCodingParams ----------------------------------------------------------------------------
GeometryCodingParams::GeometryCodingParams() : m_BasePointUint64(0), m_CoordBits(POINT_COORD_BITS)
{
  m_BasePoint = Uint64ToPointUObsolete(m_BasePointUint64);
}

GeometryCodingParams::GeometryCodingParams(uint8_t coordBits, m2::PointD const & pt)
  : m_CoordBits(coordBits)
{
  SetBasePoint(pt);
}

GeometryCodingParams::GeometryCodingParams(uint8_t coordBits, uint64_t basePointUint64)
  : m_BasePointUint64(basePointUint64), m_CoordBits(coordBits)
{
  m_BasePoint = Uint64ToPointUObsolete(m_BasePointUint64);
}

void GeometryCodingParams::SetBasePoint(m2::PointD const & pt)
{
  m_BasePoint = PointDToPointU(pt, m_CoordBits);
  m_BasePointUint64 = PointUToUint64Obsolete(m_BasePoint);
}

namespace pts
{
m2::PointU D2U(m2::PointD const & p, uint32_t coordBits) { return PointDToPointU(p, coordBits); }

m2::PointD U2D(m2::PointU const & p, uint32_t coordBits)
{
  m2::PointD const pt = PointUToPointD(p, coordBits);
  ASSERT(MercatorBounds::minX <= pt.x && pt.y <= MercatorBounds::maxX, (p, pt, coordBits));
  ASSERT(MercatorBounds::minY <= pt.x && pt.y <= MercatorBounds::maxY, (p, pt, coordBits));
  return pt;
}

m2::PointU GetMaxPoint(GeometryCodingParams const & params)
{
  return D2U(m2::PointD(MercatorBounds::maxX, MercatorBounds::maxY), params.GetCoordBits());
}

m2::PointU GetBasePoint(GeometryCodingParams const & params) { return params.GetBasePoint(); }
}  // namespace pts

void Encode(EncodeFunT fn, vector<m2::PointD> const & points, GeometryCodingParams const & params,
            DeltasT & deltas)
{
  size_t const count = points.size();

  pts::PointsU upoints;
  upoints.reserve(count);

  transform(points.begin(), points.end(), back_inserter(upoints),
            bind(&pts::D2U, placeholders::_1, params.GetCoordBits()));

  ASSERT(deltas.empty(), ());
  deltas.resize(count);

  coding::OutDeltasT adapt(deltas);
  (*fn)(make_read_adapter(upoints), pts::GetBasePoint(params), pts::GetMaxPoint(params), adapt);
}

void Decode(DecodeFunT fn, DeltasT const & deltas, GeometryCodingParams const & params,
            OutPointsT & points, size_t reserveF)
{
  DecodeImpl(fn, deltas, params, points, reserveF);
}

void Decode(DecodeFunT fn, DeltasT const & deltas, GeometryCodingParams const & params,
            vector<m2::PointD> & points, size_t reserveF)
{
  DecodeImpl(fn, deltas, params, points, reserveF);
}

void const * LoadInner(DecodeFunT fn, void const * pBeg, size_t count,
                       GeometryCodingParams const & params, OutPointsT & points)
{
  DeltasT deltas;
  deltas.reserve(count);
  void const * ret =
      ReadVarUint64Array(static_cast<char const *>(pBeg), count, MakeBackInsertFunctor(deltas));

  Decode(fn, deltas, params, points);
  return ret;
}

TrianglesChainSaver::TrianglesChainSaver(GeometryCodingParams const & params)
{
  m_base = pts::GetBasePoint(params);
  m_max = pts::GetMaxPoint(params);
}

void TrianglesChainSaver::operator()(TPoint arr[3], vector<TEdge> edges)
{
  m_buffers.push_back(TBuffer());
  MemWriter<TBuffer> writer(m_buffers.back());

  WriteVarUint(writer, coding::EncodePointDeltaAsUint(arr[0], m_base));
  WriteVarUint(writer, coding::EncodePointDeltaAsUint(arr[1], arr[0]));

  TEdge curr = edges.front();
  curr.m_delta = coding::EncodePointDeltaAsUint(arr[2], arr[1]);

  sort(edges.begin(), edges.end(), edge_less_p0());

  stack<TEdge> st;
  while (true)
  {
    CHECK_EQUAL(curr.m_delta >> 62, 0, ());
    uint64_t delta = curr.m_delta << 2;

    // find next edges
    int const nextNode = curr.m_p[1];
    auto i = lower_bound(edges.begin(), edges.end(), nextNode, edge_less_p0());
    bool const found = (i != edges.end() && i->m_p[0] == nextNode);
    if (found)
    {
      // fill 2 tree-struct bites
      ASSERT_NOT_EQUAL(i->m_side, -1, ());

      uint64_t const one = 1;

      // first child
      delta |= (one << i->m_side);

      vector<TEdge>::iterator j = i + 1;
      if (j != edges.end() && j->m_p[0] == nextNode)
      {
        // second child
        ASSERT_EQUAL(i->m_side, 0, ());
        ASSERT_EQUAL(j->m_side, 1, ());

        delta |= (one << j->m_side);

        // push to stack for further processing
        st.push(*j);
      }

      curr = *i;
    }

    // write delta for current element
    WriteVarUint(writer, delta);

    if (!found)
    {
      // end of chain - pop current from stack or exit
      if (st.empty())
        break;
      else
      {
        curr = st.top();
        st.pop();
      }
    }
  }
}

void DecodeTriangles(coding::InDeltasT const & deltas, m2::PointU const & basePoint,
                     m2::PointU const & maxPoint, coding::OutPointsT & points)
{
  size_t const count = deltas.size();
  ASSERT_GREATER(count, 2, ());

  points.push_back(coding::DecodePointDeltaFromUint(deltas[0], basePoint));
  points.push_back(coding::DecodePointDeltaFromUint(deltas[1], points.back()));
  points.push_back(coding::DecodePointDeltaFromUint(deltas[2] >> 2, points.back()));

  stack<size_t> st;

  size_t ind = 2;
  uint8_t treeBits = deltas[2] & 3;

  for (size_t i = 3; i < count;)
  {
    // points 0, 1 - is a common edge
    // point 2 - is an opposite point for new triangle to calculate prediction
    size_t trg[3];

    if (treeBits & 1)
    {
      // common edge is 1->2
      trg[0] = ind;
      trg[1] = ind - 1;
      trg[2] = ind - 2;

      // push to stack for further processing
      if (treeBits & 2)
        st.push(ind);
    }
    else if (treeBits & 2)
    {
      // common edge is 2->0
      trg[0] = ind - 2;
      trg[1] = ind;
      trg[2] = ind - 1;
    }
    else
    {
      // end of chain - pop current from stack
      ASSERT(!st.empty(), ());
      ind = st.top();
      st.pop();
      treeBits = 2;
      continue;
    }

    // push points
    points.push_back(points[trg[0]]);
    points.push_back(points[trg[1]]);
    points.push_back(coding::DecodePointDeltaFromUint(
        deltas[i] >> 2,
        coding::PredictPointInTriangle(maxPoint, points[trg[0]], points[trg[1]], points[trg[2]])));

    // next step
    treeBits = deltas[i] & 3;
    ind = points.size() - 1;
    ++i;
  }

  ASSERT(treeBits == 0 && st.empty(), ());
}
}  // namespace serial