diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2014-01-23 14:20:22 +0100 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2014-01-23 14:20:22 +0100 |
commit | 50021b730c6615d21aefa99f2f553640b43ff231 (patch) | |
tree | 0b23d06b12c2923881251cabdbe13c45f279e463 /src/map | |
parent | b4a95be0b217ac8a73bdc57a0488865b2289d5f5 (diff) | |
download | qtlocation-mapboxgl-50021b730c6615d21aefa99f2f553640b43ff231.tar.gz |
move to layer system
Diffstat (limited to 'src/map')
-rw-r--r-- | src/map/layer.cpp | 3 | ||||
-rw-r--r-- | src/map/tile.cpp | 192 |
2 files changed, 47 insertions, 148 deletions
diff --git a/src/map/layer.cpp b/src/map/layer.cpp new file mode 100644 index 0000000000..56e907c0af --- /dev/null +++ b/src/map/layer.cpp @@ -0,0 +1,3 @@ +#include <llmr/map/layer.hpp> + +using namespace llmr; diff --git a/src/map/tile.cpp b/src/map/tile.cpp index 4d744cb971..983ccff947 100644 --- a/src/map/tile.cpp +++ b/src/map/tile.cpp @@ -8,6 +8,7 @@ #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; @@ -41,6 +42,7 @@ std::forward_list<Tile::ID> Tile::children(const ID& id, int32_t z) { Tile::Tile(ID id) : id(id), state(initial), + fillBuffer(std::make_shared<FillBuffer>()), data(0), bytes(0) { @@ -92,23 +94,13 @@ bool Tile::parse() { try { while (tile.next()) { if (tile.tag == 3) { // layer - uint32_t bytes = (uint32_t)tile.varint(); - parseLayer(tile.data, bytes); - tile.skipBytes(bytes); + parseLayer(tile.message()); } else { tile.skip(); } } - } catch (const pbf::exception& ex) { - fprintf(stderr, "[%p] parsing tile [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what()); - cancel(); - return false; - } catch (const Tile::exception& ex) { - fprintf(stderr, "[%p] parsing tile [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what()); - cancel(); - return false; } catch (const std::exception& ex) { - fprintf(stderr, "[%p] general exception [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what()); + fprintf(stderr, "[%p] exception [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what()); cancel(); return false; } @@ -119,39 +111,23 @@ bool Tile::parse() { state = ready; } - // int i = 0; - // for (const fill_index& index : fillIndices) { - // fprintf(stderr, "[%p] %d: vertex % 8d / % 8d\n", this, i, index.vertex_start, index.vertex_length); - // fprintf(stderr, "[%p] %d: elements % 8d / % 8d\n", this, i, index.elements_start, index.elements_length); - // i++; - // } - return true; } -void Tile::parseLayer(const uint8_t *data, uint32_t bytes) { - pbf layer(data, bytes); - std::string name; - - uint32_t vertex_start = fillBuffer.vertex_length(); - uint32_t elements_start = fillBuffer.elements_length(); - fillIndices.emplace_back(vertex_start, elements_start); - +void Tile::parseLayer(const pbf data) { + pbf layer(data); while (layer.next()) { if (layer.tag == 1) { - name = layer.string(); - fillIndices.back().name = name; - } else if (layer.tag == 2) { - uint32_t bytes = (uint32_t)layer.varint(); - parseFeature(layer.data, bytes); - layer.skipBytes(bytes); + std::string name = layer.string(); + if (name == "water") { + layers.emplace_front(name, createFillBucket(data)); + } } else { layer.skip(); } } } - enum geom_type { Unknown = 0, Point = 1, @@ -159,126 +135,46 @@ enum geom_type { Polygon = 3 }; +std::shared_ptr<Bucket> Tile::createFillBucket(const pbf data) { + pbf layer(data); + std::shared_ptr<FillBucket> bucket = std::make_shared<FillBucket>(fillBuffer); -void Tile::parseFeature(const uint8_t *data, uint32_t bytes) { - pbf feature(data, bytes); - - geom_type type = Unknown; - pbf geom; - - while (feature.next()) { - if (feature.tag == 1) { - /*uint32_t id =*/ feature.varint(); - } else if (feature.tag == 2) { - const uint8_t *tag_end = feature.data + feature.varint(); - while (feature.data < tag_end) { - /*uint32_t key =*/ feature.varint(); - /*uint32_t value =*/ feature.varint(); + 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(); + } } - } else if (feature.tag == 3) { - type = (geom_type)feature.varint(); - } else if (feature.tag == 4) { - geom = feature.message(); - } else { - feature.skip(); - } - } - - if (geom) { - if (type == LineString) { - addLineGeometry(geom); - } else if (type == Polygon) { - addFillGeometry(geom); - } - } -} - -void Tile::addFillGeometry(pbf& geom) { - - std::vector<std::vector<std::pair<int16_t, int16_t>>> lines; - { - std::vector<std::pair<int16_t, int16_t>> line; - Geometry::command cmd; - int32_t x, y; - Geometry geometry(geom); - while ((cmd = geometry.next(x, y)) != Geometry::end) { - if (cmd == Geometry::move_to) { - if (line.size()) { - lines.push_back(line); - line.clear(); - } + if (type == Polygon && geometry) { + bucket->addGeometry(geometry); } - line.emplace_back(x, y); - } - if (line.size()) { - lines.push_back(line); + } else { + layer.skip(); } } - for (const std::vector<std::pair<int16_t, int16_t>>& line : lines) { - uint32_t vertex_start = fillBuffer.vertex_length(); - - fillBuffer.addDegenerate(); - for (const std::pair<int16_t, int16_t>& coord : line) { - fillBuffer.addCoordinate(coord.first, coord.second); - } - - uint32_t vertex_end = fillBuffer.vertex_length(); - - if (vertex_end - vertex_start > 65535) { - throw geometry_too_long_exception(); - } - - if (!fillIndices.size()) { - // Create a new index because there is none yet. - throw std::runtime_error("no index yet"); - } - - fill_index& index = fillIndices.back(); - if (!index.groups.size()) { - throw std::runtime_error("no group yet"); - } - - uint32_t vertex_count = vertex_end - vertex_start; - index.length += vertex_count; - - if (index.groups.back().vertex_length + vertex_count > 65535) { - // Move to a new group because the old one can't hold the geometry. - index.groups.emplace_back(); - } - - fill_index::group& group = index.groups.back(); - - // We're generating triangle fans, so we always start with the first - // coordinate in this polygon. - // The first valid index that is not a degenerate. - uint16_t firstIndex = group.vertex_length + 1; - - assert(firstIndex + vertex_count - 1 < 65536); - - uint32_t elements_start = fillBuffer.elements_length(); - - for (uint32_t i = 2; i < vertex_count; i++) { - fillBuffer.addElements(firstIndex, firstIndex + i - 1, firstIndex + i); - } - - uint32_t elements_end = fillBuffer.elements_length(); - uint32_t elements_count = elements_end - elements_start; - group.vertex_length += vertex_count; - group.elements_length += elements_count; - } + 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); - } -} +// 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); +// } +// } |