-
Notifications
You must be signed in to change notification settings - Fork 43
/
map.cpp
executable file
·348 lines (291 loc) · 10.2 KB
/
map.cpp
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
// Copyright 2010-2012 Michael J. Nelson
//
// This file is part of pigmap.
//
// pigmap is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// pigmap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with pigmap. If not, see <http://www.gnu.org/licenses/>.
#include <fstream>
#include <map>
#include "map.h"
#include "utils.h"
using namespace std;
bool MapParams::valid() const
{
return B >= 2 && B <= 16 && T >= 1 && T <= 16;
}
bool MapParams::validZoom() const
{
return baseZoom >= 0 && baseZoom <= 30;
}
bool MapParams::validYRange() const
{
return minY <= maxY && minY >= 0 && maxY <= 255;
}
bool buildParamMap(const vector<string>& lines, map<string, string>& params)
{
for (vector<string>::const_iterator it = lines.begin(); it != lines.end(); it++)
{
vector<string> tokens = tokenize(*it, ' ');
if (tokens.size() != 2)
return false;
params.insert(make_pair(tokens[0], tokens[1]));
}
return true;
}
bool readParam(const map<string, string>& params, const string& key, int& value)
{
map<string, string>::const_iterator it = params.find(key);
if (it == params.end())
return false;
return fromstring(it->second, value);
}
bool MapParams::readFile(const string& outputpath)
{
string filename = outputpath + "/pigmap.params";
vector<string> lines;
map<string, string> params;
if (!readLines(filename, lines) || !buildParamMap(lines, params))
return false;
if (!readParam(params, "B", B) || !readParam(params, "T", T) || !readParam(params, "baseZoom", baseZoom))
return false;
userMinY = readParam(params, "userMinY", minY);
userMaxY = readParam(params, "userMaxY", maxY);
return valid() && validZoom();
}
void MapParams::writeFile(const string& outputpath) const
{
string filename = outputpath + "/pigmap.params";
ofstream outfile(filename.c_str());
outfile << "B " << B << endl << "T " << T << endl << "baseZoom " << baseZoom << endl;
if (userMinY)
outfile << "userMinY " << minY << endl;
if (userMaxY)
outfile << "userMaxY " << maxY << endl;
}
Pixel operator+(const Pixel& p1, const Pixel& p2) {Pixel p = p1; return p += p2;}
Pixel operator-(const Pixel& p1, const Pixel& p2) {Pixel p = p1; return p -= p2;}
TileIdx Pixel::getTile(const MapParams& mp) const
{
int64_t xx = x + 2*mp.B, yy = y + mp.tileSize() - 17*mp.B;
return TileIdx(floordiv(xx, mp.tileSize()), floordiv(yy, mp.tileSize()));
}
bool BBox::includes(const Pixel& p) const {return p.x >= topLeft.x && p.x < bottomRight.x && p.y >= topLeft.y && p.y < bottomRight.y;}
bool BBox::overlaps(const BBox& bb) const
{
if (bb.topLeft.x >= bottomRight.x || bb.topLeft.y >= bottomRight.y || bb.bottomRight.x <= topLeft.x || bb.bottomRight.y <= topLeft.y)
return false;
return true;
}
BlockIdx operator+(const BlockIdx& bi1, const BlockIdx& bi2) {BlockIdx bi = bi1; return bi += bi2;}
BlockIdx operator-(const BlockIdx& bi1, const BlockIdx& bi2) {BlockIdx bi = bi1; return bi -= bi2;}
bool BlockIdx::occludes(const BlockIdx& bi) const
{
int64_t dx = bi.x - x, dz = bi.z - z, dy = bi.y - y;
// we cannot occlude anyone to the N, W, or U of us
if (dx < 0 || dz > 0 || dy > 0)
return false;
// see if the other block's center is 0 or 1 steps away from ours on the triangular grid
// (the actual grid size doesn't matter; just use a dummy size of 2x1)
int64_t imgxdiff = dx*2 + dz*2;
int64_t imgydiff = -dx + dz - dy*2;
return imgxdiff <= 2 && imgydiff <= 2;
}
ChunkIdx BlockIdx::getChunkIdx() const
{
return ChunkIdx(floordiv16(x), floordiv16(z));
}
BlockIdx BlockIdx::topBlock(const Pixel& p, const MapParams& mp)
{
// x = 2Bbx + 2Bbz
// 2Bbx = x - 2Bbz
// bx = x/2B - bz
// y = -Bbx +Bbz -2Bmaxy
// Bbz = y + Bbx + 2Bmaxy
// bz = y/B + bx + 2maxy
// bx = x/2B - y/B - bx - 2maxy
// 2bx = x/2B - y/B - 2maxy
// bx = x/4B - y/2B - maxy <----
// bz = y/B + x/4B - y/2B - maxy + 2maxy
// bz = x/4B + y/2B + maxy <----
return BlockIdx((p.x - 2*p.y)/(4*mp.B) - mp.maxY, (p.x + 2*p.y)/(4*mp.B) + mp.maxY, mp.maxY);
}
string ChunkIdx::toFileName() const
{
return "c." + toBase36(x) + "." + toBase36(z) + ".dat";
}
string ChunkIdx::toFilePath() const
{
return toBase36(mod64pos(x)) + "/" + toBase36(mod64pos(z)) + "/" + toFileName();
}
bool ChunkIdx::fromFilePath(const std::string& filename, ChunkIdx& result)
{
string::size_type pos3 = filename.rfind('.');
string::size_type pos2 = filename.rfind('.', pos3 - 1);
string::size_type pos = filename.rfind('.', pos2 - 1);
// must have three dots, must have only "dat" after last dot, must have only "c"
// and possibly some directories before the first dot
if (pos == string::npos || pos2 == string::npos || pos3 == string::npos ||
filename.compare(pos3, filename.size() - pos3, ".dat") != 0 ||
pos < 1 || filename.compare(pos - 1, 1, "c") != 0 ||
(pos > 1 && filename[pos - 2] != '/'))
return false;
return fromBase36(filename, pos + 1, pos2 - pos - 1, result.x)
&& fromBase36(filename, pos2 + 1, pos3 - pos2 - 1, result.z);
}
RegionIdx ChunkIdx::getRegionIdx() const
{
return RegionIdx(floordiv(x, 32), floordiv(z, 32));
}
vector<TileIdx> ChunkIdx::getTiles(const MapParams& mp) const
{
BBox bbchunk = getBBox(mp);
vector<TileIdx> tiles;
// get tile of NED corner
TileIdx tibase = nedCorner(mp).getCenter(mp).getTile(mp);
tiles.push_back(tibase);
// grab as many tiles down as we need
TileIdx tidown = tibase + TileIdx(0,1);
while (tidown.getBBox(mp).overlaps(bbchunk))
{
tiles.push_back(tidown);
tidown += TileIdx(0,1);
}
// grab as many tiles up as we need
TileIdx tiup = tibase - TileIdx(0,1);
while (tiup.getBBox(mp).overlaps(bbchunk))
{
tiles.push_back(tiup);
tiup -= TileIdx(0,1);
}
// we may also need the tiles to the right of all the ones we have so far
TileIdx tiright = tibase + TileIdx(1,0);
if (tiright.getBBox(mp).overlaps(bbchunk))
{
vector<TileIdx>::size_type oldsize = tiles.size();
for (vector<TileIdx>::size_type i = 0; i < oldsize; i++)
tiles.push_back(tiles[i] + TileIdx(1,0));
}
return tiles;
}
ChunkIdx operator+(const ChunkIdx& ci1, const ChunkIdx& ci2) {ChunkIdx ci = ci1; return ci += ci2;}
ChunkIdx operator-(const ChunkIdx& ci1, const ChunkIdx& ci2) {ChunkIdx ci = ci1; return ci -= ci2;}
string RegionIdx::toOldFileName() const
{
return "r." + tostring(x) + "." + tostring(z) + ".mcr";
}
string RegionIdx::toAnvilFileName() const
{
return "r." + tostring(x) + "." + tostring(z) + ".mca";
}
bool RegionIdx::fromFilePath(const std::string& filename, RegionIdx& result)
{
string::size_type pos3 = filename.rfind('.');
string::size_type pos2 = filename.rfind('.', pos3 - 1);
string::size_type pos = filename.rfind('.', pos2 - 1);
// must have three dots, must have only "mcr" or "mca" after last dot, must have only "r"
// and possibly some directories before the first dot
if (pos == string::npos || pos2 == string::npos || pos3 == string::npos ||
(filename.compare(pos3, filename.size() - pos3, ".mcr") != 0 && filename.compare(pos3, filename.size() - pos3, ".mca") != 0) ||
pos < 1 || filename.compare(pos - 1, 1, "r") != 0 ||
(pos > 1 && filename[pos - 2] != '/'))
return false;
return fromstring(filename.substr(pos + 1, pos2 - pos - 1), result.x)
&& fromstring(filename.substr(pos2 + 1, pos3 - pos2 - 1), result.z);
}
TileIdx operator+(const TileIdx& t1, const TileIdx& t2) {TileIdx t = t1; return t += t2;}
TileIdx operator-(const TileIdx& t1, const TileIdx& t2) {TileIdx t = t1; return t -= t2;}
bool TileIdx::valid(const MapParams& mp) const
{
if (mp.baseZoom == 0)
return x == 0 && y == 0;
int64_t max = (1 << mp.baseZoom);
int64_t offset = max/2;
int64_t gx = x + offset, gy = y + offset;
return gx >= 0 && gx < max && gy >= 0 && gy < max;
}
string TileIdx::toFilePath(const MapParams& mp) const
{
if (!valid(mp))
return string();
if (mp.baseZoom == 0)
return "base.png";
int64_t offset = (1 << (mp.baseZoom-1));
int64_t gx = x + offset, gy = y + offset;
string s;
for (int zoom = mp.baseZoom-1; zoom >= 0; zoom--)
{
int64_t xbit = (gx >> zoom) & 0x1;
int64_t ybit = (gy >> zoom) & 0x1;
s += tostring(xbit + 2*ybit) + "/";
}
s.resize(s.size() - 1); // drop final slash
s += ".png";
return s;
}
BBox TileIdx::getBBox(const MapParams& mp) const
{
Pixel bco = baseChunk(mp).originBlock().getCenter(mp);
Pixel tl = bco + Pixel(-2*mp.B, 17*mp.B - mp.tileSize());
return BBox(tl, tl + Pixel(mp.tileSize(), mp.tileSize()));
}
ZoomTileIdx TileIdx::toZoomTileIdx(const MapParams& mp) const
{
// adjust by offset
int64_t max = (1 << mp.baseZoom);
int64_t offset = max/2;
return ZoomTileIdx(x + offset, y + offset, mp.baseZoom);
}
bool ZoomTileIdx::valid() const
{
int64_t max = (1 << zoom);
return x >= 0 && x < max && y >= 0 && y < max && zoom >= 0;
}
string ZoomTileIdx::toFilePath() const
{
if (!valid())
return string();
if (zoom == 0)
return "base.png";
string s;
for (int z = zoom-1; z >= 0; z--)
{
int64_t xbit = (x >> z) & 0x1;
int64_t ybit = (y >> z) & 0x1;
s += tostring(xbit + 2*ybit) + "/";
}
s.resize(s.size() - 1); // drop final slash
s += ".png";
return s;
}
TileIdx ZoomTileIdx::toTileIdx(const MapParams& mp) const
{
// scale coords up to base zoom
int64_t newx = x, newy = y;
int shift = mp.baseZoom - zoom;
newx <<= shift;
newy <<= shift;
// adjust by offset to get TileIdx
int64_t max = (1 << mp.baseZoom);
int64_t offset = max/2;
return TileIdx(newx - offset, newy - offset);
}
ZoomTileIdx ZoomTileIdx::toZoom(int z) const
{
if (z > zoom)
{
int shift = z - zoom;
return ZoomTileIdx(x << shift, y << shift, z);
}
int shift = zoom - z;
return ZoomTileIdx(x >> shift, y >> shift, z);
}