diff options
Diffstat (limited to 'src/mbgl/map/tile_id.cpp')
-rw-r--r-- | src/mbgl/map/tile_id.cpp | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/src/mbgl/map/tile_id.cpp b/src/mbgl/map/tile_id.cpp index 518ee14c42..ad7ec2e0f6 100644 --- a/src/mbgl/map/tile_id.cpp +++ b/src/mbgl/map/tile_id.cpp @@ -5,35 +5,49 @@ namespace mbgl { -TileID TileID::parent(int8_t parent_z) const { +TileID TileID::parent(int8_t parent_z, int8_t sourceMaxZoom) const { assert(parent_z < z); - int32_t dim = std::pow(2, z - parent_z); - return TileID{ - parent_z, - (x >= 0 ? x : x - dim + 1) / dim, - y / dim - }; + auto newX = x; + auto newY = y; + for (auto newZ = z; newZ > parent_z; newZ--) { + if (newZ > sourceMaxZoom) { + // the id represents an overscaled tile, return the same coordinates with a lower z + // do nothing + } else { + newX = newX / 2; + newY = newY / 2; + } + } + + return TileID{parent_z, newX, newY, parent_z > sourceMaxZoom ? sourceMaxZoom : parent_z}; } -std::forward_list<TileID> TileID::children(int32_t child_z) const { - assert(child_z > z); - int32_t factor = std::pow(2, child_z - z); +std::forward_list<TileID> TileID::children(int8_t sourceMaxZoom) const { + auto childZ = z + 1; std::forward_list<TileID> child_ids; - for (int32_t ty = y * factor, y_max = (y + 1) * factor; ty < y_max; ++ty) { - for (int32_t tx = x * factor, x_max = (x + 1) * factor; tx < x_max; ++tx) { - child_ids.emplace_front(child_z, tx, ty); - } + if (z >= sourceMaxZoom) { + // return a single tile id representing a an overscaled tile + child_ids.emplace_front(childZ, x, y, sourceMaxZoom); + + } else { + auto childX = x * 2; + auto childY = y * 2; + child_ids.emplace_front(childZ, childX, childY, childZ); + child_ids.emplace_front(childZ, childX + 1, childY, childZ); + child_ids.emplace_front(childZ, childX, childY + 1, childZ); + child_ids.emplace_front(childZ, childX + 1, childY + 1, childZ); } + return child_ids; } TileID TileID::normalized() const { - int32_t dim = std::pow(2, z); + int32_t dim = std::pow(2, sourceZ); int32_t nx = x, ny = y; while (nx < 0) nx += dim; while (nx >= dim) nx -= dim; - return TileID { z, nx, ny }; + return TileID { z, nx, ny, sourceZ}; } bool TileID::isChildOf(const TileID &parent_id) const { |