forked from equalpants/pigmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tables.h
executable file
·292 lines (211 loc) · 11.5 KB
/
tables.h
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
// Copyright 2010 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/>.
#ifndef TABLES_H
#define TABLES_H
#include <bitset>
#include <stdint.h>
#include "map.h"
#include "utils.h"
#define CTDATASIZE 3
#define CTLEVEL1BITS 5
#define CTLEVEL2BITS 5
#define CTLEVEL3BITS 8
#define CTLEVEL1SIZE (1 << CTLEVEL1BITS)
#define CTLEVEL2SIZE (1 << CTLEVEL2BITS)
#define CTLEVEL3SIZE (1 << CTLEVEL3BITS)
#define CTTOTALSIZE (CTLEVEL1SIZE * CTLEVEL2SIZE * CTLEVEL3SIZE)
#define CTLEVEL1MASK (CTLEVEL1SIZE - 1)
#define CTLEVEL2MASK ((CTLEVEL2SIZE - 1) << CTLEVEL1BITS)
#define CTLEVEL3MASK (((CTLEVEL3SIZE - 1) << CTLEVEL1BITS) << CTLEVEL2BITS)
#define CTGETLEVEL1(a) (a & CTLEVEL1MASK)
#define CTGETLEVEL2(a) ((a & CTLEVEL2MASK) >> CTLEVEL1BITS)
#define CTGETLEVEL3(a) (((a & CTLEVEL3MASK) >> CTLEVEL2BITS) >> CTLEVEL1BITS)
// variation of ChunkIdx for use with the ChunkTable: translates so that all coords are positive
// ...can also be used to check for the map being too big
struct PosChunkIdx
{
int64_t x, z;
PosChunkIdx(int64_t xx, int64_t zz) : x(xx), z(zz) {}
PosChunkIdx(const ChunkIdx& ci) : x(ci.x + CTTOTALSIZE/2), z(ci.z + CTTOTALSIZE/2) {}
ChunkIdx toChunkIdx() const {return ChunkIdx(x - CTTOTALSIZE/2, z - CTTOTALSIZE/2);}
bool valid() const {return x >= 0 && x < CTTOTALSIZE && z >= 0 && z < CTTOTALSIZE;}
bool operator==(const PosChunkIdx& ci) const {return x == ci.x && z == ci.z;}
bool operator!=(const PosChunkIdx& ci) const {return !operator==(ci);}
};
// structure to hold information about a 32x32 set of chunks: for each chunk, whether it needs to be drawn,
// whether it's even present on disk, etc.
struct ChunkSet
{
// each chunk gets 3 bits:
// -first bit is 1 for required (must be drawn), 0 for not required
// -last two bits describe state of chunk on disk:
// 00: have not tried to find chunk on disk yet
// 01: have successfully read chunk from disk (i.e. it should be in the cache, if we still need it)
// 10: chunk does not exist on disk
// 11: chunk file is corrupted
static const int CHUNK_UNKNOWN = 0;
static const int CHUNK_CACHED = 1;
static const int CHUNK_MISSING = 2;
static const int CHUNK_CORRUPTED = 3;
std::bitset<CTLEVEL1SIZE*CTLEVEL1SIZE*CTDATASIZE> bits;
size_t bitIdx(const PosChunkIdx& ci) const {return (CTGETLEVEL1(ci.z) * CTLEVEL1SIZE + CTGETLEVEL1(ci.x)) * CTDATASIZE;}
void setRequired(const PosChunkIdx& ci) {bits.set(bitIdx(ci));}
void setDiskState(const PosChunkIdx& ci, int state) {size_t bi = bitIdx(ci); bits[bi+1] = state & 0x2; bits[bi+2] = state & 0x1;}
};
// first level of indirection: information about a 32x32 group of ChunkSets, and hence a 1024x1024 set of chunks
struct ChunkGroup
{
// pointers to ChunkSets with the data, or NULL for sets that aren't used
ChunkSet *chunksets[CTLEVEL2SIZE*CTLEVEL2SIZE];
ChunkGroup() {for (int i = 0; i < CTLEVEL2SIZE*CTLEVEL2SIZE; i++) chunksets[i] = NULL;}
~ChunkGroup() {for (int i = 0; i < CTLEVEL2SIZE*CTLEVEL2SIZE; i++) if (chunksets[i] != NULL) delete chunksets[i];}
int chunkSetIdx(const PosChunkIdx& ci) const {return CTGETLEVEL2(ci.z) * CTLEVEL2SIZE + CTGETLEVEL2(ci.x);}
ChunkSet* getChunkSet(const PosChunkIdx& ci) const {return chunksets[chunkSetIdx(ci)];}
void setRequired(const PosChunkIdx& ci);
void setDiskState(const PosChunkIdx& ci, int state);
};
// second (and final) level of indirection: 256x256 groups, so 262144x262144 possible chunks
struct ChunkTable : private nocopy
{
ChunkGroup *chunkgroups[CTLEVEL3SIZE*CTLEVEL3SIZE];
ChunkTable() {for (int i = 0; i < CTLEVEL3SIZE*CTLEVEL3SIZE; i++) chunkgroups[i] = NULL;}
~ChunkTable() {for (int i = 0; i < CTLEVEL3SIZE*CTLEVEL3SIZE; i++) if (chunkgroups[i] != NULL) delete chunkgroups[i];}
int chunkGroupIdx(const PosChunkIdx& ci) const {return CTGETLEVEL3(ci.z) * CTLEVEL3SIZE + CTGETLEVEL3(ci.x);}
ChunkGroup* getChunkGroup(const PosChunkIdx& ci) const {return chunkgroups[chunkGroupIdx(ci)];}
ChunkSet* getChunkSet(const PosChunkIdx& ci) const {ChunkGroup *cg = getChunkGroup(ci); return (cg == NULL) ? NULL : cg->getChunkSet(ci);}
// given indices into the ChunkGroups/ChunkSets/bitset, construct a PosChunkIdx
static PosChunkIdx toPosChunkIdx(int cgi, int csi, int bi);
bool isRequired(const PosChunkIdx& ci) const {ChunkSet *cs = getChunkSet(ci); return (cs == NULL) ? false : cs->bits[cs->bitIdx(ci)];}
int getDiskState(const PosChunkIdx& ci) const {ChunkSet *cs = getChunkSet(ci); return (cs == NULL) ? 0 : ((cs->bits[cs->bitIdx(ci)+1] ? 0x2 : 0) | (cs->bits[cs->bitIdx(ci)+2] ? 0x1 : 0));}
void setRequired(const PosChunkIdx& ci);
void setDiskState(const PosChunkIdx& ci, int state);
void copyFrom(const ChunkTable& ctable);
};
// given a ChunkTable, iterates over the required chunks
// ...this is obsolete and not used, except in some test functions
struct RequiredChunkIterator
{
bool end; // true once we've reached the end
PosChunkIdx current; // if end == false, holds the current chunk
ChunkTable& chunktable;
int cgi, csi, bi;
// constructor initializes us to the first required chunk
RequiredChunkIterator(ChunkTable& ctable);
// move to the next required chunk, or to the end
void advance();
};
#define TTDATASIZE 2
#define TTLEVEL1BITS 4
#define TTLEVEL2BITS 4
#define TTLEVEL3BITS 8
#define TTLEVEL1SIZE (1 << TTLEVEL1BITS)
#define TTLEVEL2SIZE (1 << TTLEVEL2BITS)
#define TTLEVEL3SIZE (1 << TTLEVEL3BITS)
#define TTTOTALSIZE (TTLEVEL1SIZE * TTLEVEL2SIZE * TTLEVEL3SIZE)
#define TTLEVEL1MASK (TTLEVEL1SIZE - 1)
#define TTLEVEL2MASK ((TTLEVEL2SIZE - 1) << TTLEVEL1BITS)
#define TTLEVEL3MASK (((TTLEVEL3SIZE - 1) << TTLEVEL1BITS) << TTLEVEL2BITS)
#define TTGETLEVEL1(a) (a & TTLEVEL1MASK)
#define TTGETLEVEL2(a) ((a & TTLEVEL2MASK) >> TTLEVEL1BITS)
#define TTGETLEVEL3(a) (((a & TTLEVEL3MASK) >> TTLEVEL2BITS) >> TTLEVEL1BITS)
// variation of TileIdx for use with the TileTable: translates so that all coords are positive
// ...can also be used to check for the map being too big
struct PosTileIdx
{
int64_t x, y;
PosTileIdx(int64_t xx, int64_t yy) : x(xx), y(yy) {}
PosTileIdx(const TileIdx& ti) : x(ti.x + TTTOTALSIZE/2), y(ti.y + TTTOTALSIZE/2) {}
TileIdx toTileIdx() const {return TileIdx(x - TTTOTALSIZE/2, y - TTTOTALSIZE/2);}
bool valid() const {return x >= 0 && x < TTTOTALSIZE && y >= 0 && y < TTTOTALSIZE;}
bool operator==(const PosTileIdx& ti) const {return x == ti.x && y == ti.y;}
bool operator!=(const PosTileIdx& ti) const {return !operator==(ti);}
};
// structure to hold information about a 16x16 set of tiles: for each tile, whether it's been drawn yet
struct TileSet
{
// each tile gets two bits: first is whether it's required, second is whether it's been drawn
std::bitset<TTLEVEL1SIZE*TTLEVEL1SIZE*TTDATASIZE> bits;
size_t bitIdx(const PosTileIdx& ti) const {return (TTGETLEVEL1(ti.y) * TTLEVEL1SIZE + TTGETLEVEL1(ti.x)) * TTDATASIZE;}
// assumes that ti actually belongs to this set
bool isRequired(const PosTileIdx& ti) const {return bits[bitIdx(ti)];}
// set tile's required bit and return previous state of bit
bool setRequired(const PosTileIdx& ti) {size_t bi = bitIdx(ti); bool rv = bits[bi]; bits.set(bi); return rv;}
void setDrawn(const PosTileIdx& ti) {bits.set(bitIdx(ti)+1);}
};
// first level of indirection: information about a 256x256 set of tiles
struct TileGroup
{
// pointers to TileSets with the data, or NULL for 16x16 sets that aren't used
TileSet *tilesets[TTLEVEL2SIZE*TTLEVEL2SIZE];
// number of tiles in this group that have been set to required
int64_t reqcount;
TileGroup() : reqcount(0) {for (int i = 0; i < TTLEVEL2SIZE*TTLEVEL2SIZE; i++) tilesets[i] = NULL;}
~TileGroup() {for (int i = 0; i < TTLEVEL2SIZE*TTLEVEL2SIZE; i++) if (tilesets[i] != NULL) delete tilesets[i];}
int tileSetIdx(const PosTileIdx& ti) const {return TTGETLEVEL2(ti.y) * TTLEVEL2SIZE + TTGETLEVEL2(ti.x);}
TileSet* getTileSet(const PosTileIdx& ti) const {return tilesets[tileSetIdx(ti)];}
bool setRequired(const PosTileIdx& ti); // set tile's required bit and return previous state of bit
void setDrawn(const PosTileIdx& ti);
};
// second (and final) level of indirection: a 65536x65536 set of tiles
struct TileTable : private nocopy
{
TileGroup *tilegroups[TTLEVEL3SIZE*TTLEVEL3SIZE];
int64_t reqcount;
TileTable() : reqcount(0) {for (int i = 0; i < TTLEVEL3SIZE*TTLEVEL3SIZE; i++) tilegroups[i] = NULL;}
~TileTable() {for (int i = 0; i < TTLEVEL3SIZE*TTLEVEL3SIZE; i++) if (tilegroups[i] != NULL) delete tilegroups[i];}
int tileGroupIdx(const PosTileIdx& ti) const {return TTGETLEVEL3(ti.y) * TTLEVEL3SIZE + TTGETLEVEL3(ti.x);}
TileGroup* getTileGroup(const PosTileIdx& ti) const {return tilegroups[tileGroupIdx(ti)];}
TileSet* getTileSet(const PosTileIdx& ti) const {TileGroup *tg = getTileGroup(ti); return (tg == NULL) ? NULL : tg->getTileSet(ti);}
// given indices into the TileGroups/TileSets/bitset, construct a PosTileIdx
static PosTileIdx toPosTileIdx(int tgi, int tsi, int bi);
bool isRequired(const PosTileIdx& ti) const {TileSet *ts = getTileSet(ti); return (ts == NULL) ? false : ts->bits[ts->bitIdx(ti)];}
bool isDrawn(const PosTileIdx& ti) const {TileSet *ts = getTileSet(ti); return (ts == NULL) ? false : ts->bits[ts->bitIdx(ti)+1];}
bool setRequired(const PosTileIdx& ti); // set tile's required bit and return previous state of bit
void setDrawn(const PosTileIdx& ti);
// see if an entire zoom tile can be rejected because its TileGroup or TileSet is NULL
bool reject(const ZoomTileIdx& zti, const MapParams& mp) const;
// get the total number of base tiles required to draw a zoom tile
int64_t getNumRequired(const ZoomTileIdx& zti, const MapParams& mp) const;
void copyFrom(const TileTable& ttable);
};
// given a TileTable, iterates over the required tiles
struct RequiredTileIterator
{
bool end; // true once we've reached the end
PosTileIdx current; // if end == false, holds the current tile
TileTable& tiletable;
// these guys are Z-order indices and must be converted to row-major when accessing the TileTable
int ztgi, ztsi, zbi;
// constructor initializes us to the first required tile
RequiredTileIterator(TileTable& ttable);
// move in Z-order to the next required tile, or to the end
void advance();
};
// given a TileTable, iterates over the non-NULL TileGroups
struct TileGroupIterator
{
bool end; // true once we've reached the end
int tgi; // if end == false, holds the current index into TileTable::tilegroups
ZoomTileIdx zti; // if end == false, holds the zoom tile corresponding to the current TileGroup
TileTable& tiletable;
MapParams mp;
// constructor initializes to first non-NULL TileGroup
TileGroupIterator(TileTable& ttable, const MapParams& mparams);
// move to the next non-NULL TileGroup, or to the end
void advance();
};
#endif // TABLES_H