summaryrefslogtreecommitdiff
path: root/src/map/tile.cpp
blob: 4dac3e82f179a0c44159a2611bb416bdcefa45f6 (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
#include <llmr/map/tile.hpp>

#include <stdint.h>
#include <cassert>
// #include <iostream>
#include <thread>

#include <llmr/util/pbf.hpp>
#include <llmr/util/string.hpp>
#include <llmr/geometry/geometry.hpp>
#include <llmr/renderer/fill_bucket.hpp>
#include <cmath>

using namespace llmr;

Tile::ID Tile::parent(const ID& id, int32_t z) {
    assert(z < id.z);
    ID pos(id);
    while (pos.z > z) {
        pos.z--;
        pos.x = floor(pos.x / 2);
        pos.y = floor(pos.y / 2);
    }
    return pos;
}


std::forward_list<Tile::ID> Tile::children(const ID& id, int32_t z) {
    assert(z > id.z);
    int32_t factor = pow(2, z - id.z);

    std::forward_list<ID> children;
    for (int32_t y = id.y * factor, y_max = (id.y + 1) * factor; y < y_max; y++) {
        for (int32_t x = id.x * factor, x_max = (id.x + 1) * factor; x < x_max; x++) {
            children.emplace_front(x, y, z);
        }
    }
    return children;
}


Tile::Tile(ID id)
    : id(id),
      state(initial),
      fillBuffer(),
      data(0),
      bytes(0) {

    // Initialize tile debug coordinates
    char coord[32];
    snprintf(coord, sizeof(coord), "%d/%d/%d", id.z, id.x, id.y);
    debugFontVertex.addText(coord, 50, 200, 5);
}

Tile::~Tile() {
    std::lock_guard<std::mutex> lock(mtx);

    // fprintf(stderr, "[%p] deleting tile %d/%d/%d\n", this, id.z, id.x, id.y);
    if (this->data) {
        free(this->data);
    }
}

const std::string Tile::toString() const {
    return util::sprintf("[tile %d/%d/%d]", id.z, id.x, id.y);
}


void Tile::setData(uint8_t *data, uint32_t bytes) {
    this->data = (uint8_t *)malloc(bytes);
    this->bytes = bytes;
    memcpy(this->data, data, bytes);
}

void Tile::cancel() {
    // TODO: thread safety
    if (state != obsolete) {
        state = obsolete;
    } else {
        assert((!"logic error? multiple cancelleations"));
    }
}

bool Tile::parse() {
    std::lock_guard<std::mutex> lock(mtx);

    if (state == obsolete) {
        return false;
    }

    // fprintf(stderr, "[%p] parsing tile [%d/%d/%d]...\n", this, z, x, y);

    pbf tile(data, bytes);
    try {
        while (tile.next()) {
            if (tile.tag == 3) { // layer
                parseLayer(tile.message());
            } else {
                tile.skip();
            }
        }
    } catch (const std::exception& ex) {
        fprintf(stderr, "[%p] exception [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what());
        cancel();
        return false;
    }

    if (state == obsolete) {
        return false;
    } else {
        state = ready;
    }

    return true;
}

void Tile::parseLayer(const pbf data) {
    pbf layer(data);
    while (layer.next()) {
        if (layer.tag == 1) {
            std::string name = layer.string();
            if (name == "water") {
                layers.emplace_front(name, createFillBucket(data));
            }
        } else {
            layer.skip();
        }
    }
}

enum geom_type {
    Unknown = 0,
    Point = 1,
    LineString = 2,
    Polygon = 3
};

std::shared_ptr<Bucket> Tile::createFillBucket(const pbf data) {
    pbf layer(data);
    std::shared_ptr<FillBucket> bucket = std::make_shared<FillBucket>(fillBuffer);

    while (layer.next()) {
        if (layer.tag == 2) { // feature
            pbf feature = layer.message();
            pbf geometry;
            geom_type type = Unknown;

            while (feature.next()) {
                if (feature.tag == 3) { // geometry type
                    type = (geom_type)feature.varint();
                } else if (feature.tag == 4) { // geometry
                    geometry = feature.message();
                } else {
                    feature.skip();
                }
            }

            if (type == Polygon && geometry) {
                bucket->addGeometry(geometry);
            }
        } else {
            layer.skip();
        }
    }

    return bucket;
}

// void Tile::addLineGeometry(pbf& geom) {
//     Geometry geometry(geom);

//     Geometry::command cmd;
//     int32_t x, y;
//     while ((cmd = geometry.next(x, y)) != Geometry::end) {
//         if (cmd == Geometry::move_to) {
//             lineVertex.addDegenerate();
//         }
//         lineVertex.addCoordinate(x, y);
//     }
// }