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
|
#include "base/SRC_FIRST.hpp"
#include "indexer/feature_loader.hpp"
#include "indexer/feature.hpp"
#include "indexer/scales.hpp"
#include "indexer/geometry_serialization.hpp"
#include "indexer/classificator.hpp"
#include "geometry/pointu_to_uint64.hpp"
#include "coding/byte_stream.hpp"
#include "coding/dd_vector.hpp"
#include "base/logging.hpp"
#include "defines.hpp"
namespace feature
{
uint8_t LoaderCurrent::GetHeader()
{
return Header();
}
void LoaderCurrent::ParseTypes()
{
Classificator & c = classif();
ArrayByteSource source(DataPtr() + m_TypesOffset);
size_t const count = m_pF->GetTypesCount();
for (size_t i = 0; i < count; ++i)
m_pF->m_types[i] = c.GetTypeForIndex(ReadVarUint<uint32_t>(source));
m_CommonOffset = CalcOffset(source);
}
void LoaderCurrent::ParseCommon()
{
ArrayByteSource source(DataPtr() + m_CommonOffset);
uint8_t const h = Header();
m_pF->m_params.Read(source, h);
if (m_pF->GetFeatureType() == GEOM_POINT)
{
m_pF->m_center = serial::LoadPoint(source, GetDefCodingParams());
m_pF->m_limitRect.Add(m_pF->m_center);
}
m_Header2Offset = CalcOffset(source);
}
namespace
{
class BitSource
{
char const * m_ptr;
uint8_t m_pos;
public:
BitSource(char const * p) : m_ptr(p), m_pos(0) {}
uint8_t Read(uint8_t count)
{
ASSERT_LESS ( count, 9, () );
uint8_t v = *m_ptr;
v >>= m_pos;
v &= ((1 << count) - 1);
m_pos += count;
if (m_pos >= 8)
{
ASSERT_EQUAL ( m_pos, 8, () );
++m_ptr;
m_pos = 0;
}
return v;
}
char const * RoundPtr()
{
if (m_pos > 0)
{
++m_ptr;
m_pos = 0;
}
return m_ptr;
}
};
template <class TSource> uint8_t ReadByte(TSource & src)
{
return ReadPrimitiveFromSource<uint8_t>(src);
}
}
void LoaderCurrent::ParseHeader2()
{
uint8_t ptsCount = 0, ptsMask = 0, trgCount = 0, trgMask = 0;
BitSource bitSource(DataPtr() + m_Header2Offset);
uint8_t const typeMask = Header() & HEADER_GEOTYPE_MASK;
if (typeMask == HEADER_GEOM_LINE)
{
ptsCount = bitSource.Read(4);
if (ptsCount == 0)
ptsMask = bitSource.Read(4);
else
ASSERT_GREATER ( ptsCount, 1, () );
}
else if (typeMask == HEADER_GEOM_AREA)
{
trgCount = bitSource.Read(4);
if (trgCount == 0)
trgMask = bitSource.Read(4);
}
ArrayByteSource src(bitSource.RoundPtr());
serial::CodingParams const & cp = GetDefCodingParams();
if (typeMask == HEADER_GEOM_LINE)
{
if (ptsCount > 0)
{
int const count = (ptsCount - 2 + 3) / 4;
ASSERT_LESS ( count, 4, () );
for (int i = 0; i < count; ++i)
{
uint32_t mask = ReadByte(src);
m_ptsSimpMask += (mask << (i << 3));
}
char const * start = src.PtrC();
src = ArrayByteSource(serial::LoadInnerPath(start, ptsCount, cp, m_pF->m_points));
m_pF->m_innerStats.m_points = static_cast<uint32_t>(src.PtrC() - start);
}
else
{
m_pF->m_points.push_back(serial::LoadPoint(src, cp));
ReadOffsets(src, ptsMask, m_ptsOffsets);
}
}
else if (typeMask == HEADER_GEOM_AREA)
{
if (trgCount > 0)
{
trgCount += 2;
char const * start = static_cast<char const *>(src.PtrC());
FeatureType::points_t points;
src = ArrayByteSource(serial::LoadInnerTriangles(start, trgCount, cp, points));
m_pF->m_innerStats.m_strips = static_cast<uint32_t>(src.PtrC() - start);
for (uint8_t i = 2; i < trgCount; ++i)
{
m_pF->m_triangles.push_back(points[i-2]);
m_pF->m_triangles.push_back(points[i-1]);
m_pF->m_triangles.push_back(points[i]);
}
}
else
ReadOffsets(src, trgMask, m_trgOffsets);
}
m_pF->m_innerStats.m_size = static_cast<uint32_t>(src.PtrC() - DataPtr());
}
uint32_t LoaderCurrent::ParseGeometry(int scale)
{
uint32_t sz = 0;
if ((Header() & HEADER_GEOTYPE_MASK) == HEADER_GEOM_LINE)
{
size_t const count = m_pF->m_points.size();
if (count < 2)
{
ASSERT_EQUAL ( count, 1, () );
// outer geometry
int const ind = GetScaleIndex(scale, m_ptsOffsets);
if (ind != -1)
{
ReaderSource<FilesContainerR::ReaderT> src(m_Info.GetGeometryReader(ind));
src.Skip(m_ptsOffsets[ind]);
serial::CodingParams cp = GetCodingParams(ind);
cp.SetBasePoint(m_pF->m_points[0]);
serial::LoadOuterPath(src, cp, m_pF->m_points);
sz = static_cast<uint32_t>(src.Pos() - m_ptsOffsets[ind]);
}
}
else
{
// filter inner geometry
FeatureType::points_t points;
points.reserve(count);
int const scaleIndex = GetScaleIndex(scale);
ASSERT_LESS ( scaleIndex, m_Info.GetScalesCount(), () );
points.push_back(m_pF->m_points.front());
for (int i = 1; i < count-1; ++i)
{
// check for point visibility in needed scaleIndex
if (((m_ptsSimpMask >> (2*(i-1))) & 0x3) <= scaleIndex)
points.push_back(m_pF->m_points[i]);
}
points.push_back(m_pF->m_points.back());
m_pF->m_points.swap(points);
}
CalcRect(m_pF->m_points, m_pF->m_limitRect);
}
return sz;
}
uint32_t LoaderCurrent::ParseTriangles(int scale)
{
uint32_t sz = 0;
if ((Header() & HEADER_GEOTYPE_MASK) == HEADER_GEOM_AREA)
{
if (m_pF->m_triangles.empty())
{
uint32_t const ind = GetScaleIndex(scale, m_trgOffsets);
if (ind != -1)
{
ReaderSource<FilesContainerR::ReaderT> src(m_Info.GetTrianglesReader(ind));
src.Skip(m_trgOffsets[ind]);
serial::LoadOuterTriangles(src, GetCodingParams(ind), m_pF->m_triangles);
sz = static_cast<uint32_t>(src.Pos() - m_trgOffsets[ind]);
}
}
CalcRect(m_pF->m_triangles, m_pF->m_limitRect);
}
return sz;
}
void LoaderCurrent::ParseMetadata()
{
try
{
typedef pair<uint32_t, uint32_t> IdxElementT;
DDVector<IdxElementT, FilesContainerR::ReaderT> idx(m_Info.GetMetadataIndexReader());
auto it = lower_bound(idx.begin(), idx.end()
, make_pair(uint32_t(m_pF->m_id.m_index), uint32_t(0))
, [](IdxElementT const & v1, IdxElementT const & v2) { return v1.first < v2.first; }
);
if (it != idx.end() && m_pF->m_id.m_index == it->first)
{
ReaderSource<FilesContainerR::ReaderT> reader(m_Info.GetMetadataReader());
reader.Skip(it->second);
m_pF->GetMetadata().DeserializeFromMWM(reader);
}
}
catch (Reader::OpenException const &)
{
// now ignore exception because not all mwm have needed sections
}
}
int LoaderCurrent::GetScaleIndex(int scale) const
{
int const count = m_Info.GetScalesCount();
// In case of WorldCoasts we should get correct last geometry.
int const lastScale = m_Info.GetLastScale();
if (scale > lastScale)
scale = lastScale;
switch (scale)
{
case -2: return 0;
case -1: return count-1;
default:
for (int i = 0; i < count; ++i)
if (scale <= m_Info.GetScale(i))
return i;
return -1;
}
}
int LoaderCurrent::GetScaleIndex(int scale, offsets_t const & offsets) const
{
int ind = -1;
int const count = static_cast<int>(offsets.size());
// In case of WorldCoasts we should get correct last geometry.
int const lastScale = m_Info.GetLastScale();
if (scale > lastScale)
scale = lastScale;
switch (scale)
{
case -1:
// Choose the best existing geometry for the last visible scale.
ind = count-1;
while (ind >= 0 && offsets[ind] == s_InvalidOffset) --ind;
break;
case -2:
// Choose the worst existing geometry for the first visible scale.
ind = 0;
while (ind < count && offsets[ind] == s_InvalidOffset) ++ind;
break;
default:
{
int const n = m_Info.GetScalesCount();
for (int i = 0; i < n; ++i)
{
if (scale <= m_Info.GetScale(i))
return (offsets[i] != s_InvalidOffset ? i : -1);
}
}
}
if (ind >= 0 && ind < count)
return ind;
else
{
ASSERT ( false, ("Feature should have any geometry ...") );
return -1;
}
}
}
|