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

geometry_serialization.cpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 686763d78c0c7822c02c74d9e72f4e922d90e721 (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
#include "indexer/geometry_serialization.hpp"
#include "geometry/mercator.hpp"
#include "indexer/geometry_coding.hpp"

#include "coding/point_to_integer.hpp"

#include "geometry/pointu_to_uint64.hpp"

#include "std/algorithm.hpp"
#include "std/bind.hpp"
#include "std/iterator.hpp"
#include "std/stack.hpp"


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

    inline m2::PointD U2D(m2::PointU const & p, uint32_t coordBits)
    {
      m2::PointD const pt = PointU2PointD(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;
    }

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

    inline m2::PointU GetBasePoint(CodingParams const & params)
    {
      return params.GetBasePoint();
    }

    typedef buffer_vector<m2::PointU, 32> upoints_t;
  }

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

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

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

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

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

  template <class TDecodeFun, class TOutPoints>
  void DecodeImpl(TDecodeFun fn, DeltasT const & deltas, CodingParams const & params,
                  TOutPoints & points, size_t reserveF)
  {
    size_t const count = deltas.size() * reserveF;

    pts::upoints_t upoints;
    upoints.resize(count);

    geo_coding::OutPointsT adapt(upoints);
    (*fn)(make_read_adapter(deltas), pts::GetBasePoint(params), pts::GetMaxPoint(params), adapt);

    if (points.size() < 2)
    {
      // Do not call reserve when loading triangles - they are accumulated to one vector.
      points.reserve(count);
    }

    transform(upoints.begin(), upoints.begin() + adapt.size(), back_inserter(points),
              bind(&pts::U2D, _1, params.GetCoordBits()));
  }

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

  void Decode(DecodeFunT fn, DeltasT const & deltas, CodingParams 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,
                         CodingParams 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(CodingParams const & params)
  {
    m_base = pts::GetBasePoint(params);
    m_max = pts::GetMaxPoint(params);
  }

  namespace
  {
    struct edge_less_p0
    {
      typedef tesselator::Edge edge_t;

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

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

    WriteVarUint(writer, EncodeDelta(arr[0], m_base));
    WriteVarUint(writer, EncodeDelta(arr[1], arr[0]));

    TEdge curr = edges.front();
    curr.m_delta = EncodeDelta(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(geo_coding::InDeltasT const & deltas,
                       m2::PointU const & basePoint,
                       m2::PointU const & maxPoint,
                       geo_coding::OutPointsT & points)
  {
    size_t const count = deltas.size();
    ASSERT_GREATER ( count, 2, () );

    points.push_back(DecodeDelta(deltas[0], basePoint));
    points.push_back(DecodeDelta(deltas[1], points.back()));
    points.push_back(DecodeDelta(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(DecodeDelta(deltas[i] >> 2,
                                   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(), () );
  }
}