summaryrefslogtreecommitdiff
path: root/src/mbgl/util/tile_range.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mbgl/util/tile_range.hpp')
-rw-r--r--src/mbgl/util/tile_range.hpp60
1 files changed, 40 insertions, 20 deletions
diff --git a/src/mbgl/util/tile_range.hpp b/src/mbgl/util/tile_range.hpp
index f630a49078..8554cfb65e 100644
--- a/src/mbgl/util/tile_range.hpp
+++ b/src/mbgl/util/tile_range.hpp
@@ -6,41 +6,61 @@
#include <mbgl/util/projection.hpp>
namespace mbgl {
-
namespace util {
class TileRange {
public:
- Range<Point<double>> range;
- uint8_t z;
+ Range<Point<uint32_t>> range;
+ Range<uint8_t> zoomRange;
+
+ // Compute the range of tiles covered by the bounds at maxZoom.
+ static TileRange fromLatLngBounds(const LatLngBounds& bounds, uint8_t minZoom, uint8_t maxZoom) {
+ if (minZoom > maxZoom) {
+ std::swap(minZoom, maxZoom);
+ }
+
+ auto swProj = Projection::project(bounds.southwest().wrapped(), maxZoom);
+ auto ne = bounds.northeast();
+ auto neProj = Projection::project(ne.longitude() > util::LONGITUDE_MAX ? ne.wrapped() : ne , maxZoom);
+
+ const auto maxTile = std::pow(2.0, maxZoom);
+ const auto minX = static_cast<uint32_t>(std::floor(swProj.x));
+ const auto maxX = static_cast<uint32_t>(std::floor(neProj.x));
+ const auto minY = static_cast<uint32_t>(util::clamp(std::floor(neProj.y), 0.0 , maxTile));
+ const auto maxY = static_cast<uint32_t>(util::clamp(std::floor(swProj.y), 0.0, maxTile));
+
+ return TileRange({ {minX, minY}, {maxX, maxY} }, {minZoom, maxZoom});
+ }
// Compute the range of tiles covered by the bounds.
static TileRange fromLatLngBounds(const LatLngBounds& bounds, uint8_t z) {
- auto swProj = Projection::project(bounds.southwest().wrapped(), z);
- auto ne = bounds.northeast();
- auto neProj = Projection::project(ne.longitude() > util::LONGITUDE_MAX ? ne.wrapped() : ne , z);
- const auto minX = std::floor(swProj.x);
- const auto maxX = std::ceil(neProj.x);
- const auto minY = std::floor(neProj.y);
- const auto maxY = std::ceil(swProj.y);
- return TileRange({ {minX, minY}, {maxX, maxY} }, z);
+ return fromLatLngBounds(bounds, z, z);
}
bool contains(const CanonicalTileID& tileID) {
- return z == tileID.z &&
- (range.min.x >= range.max.x ? //For wrapped bounds
- tileID.x >= range.min.x || tileID.x < range.max.x :
- tileID.x < range.max.x && tileID.x >= range.min.x) &&
- tileID.y < range.max.y &&
- tileID.y >= range.min.y;
+ if (tileID.z <= zoomRange.max && tileID.z >= zoomRange.min) {
+ if (tileID.z == 0) {
+ return true;
+ }
+ uint8_t dz = (zoomRange.max - tileID.z);
+ auto x0 = range.min.x >> dz;
+ auto x1 = range.max.x >> dz;
+ auto y0 = range.min.y >> dz;
+ auto y1 = range.max.y >> dz;
+ return (range.min.x > range.max.x ? //For wrapped bounds
+ tileID.x >= x0 || tileID.x <= x1 :
+ tileID.x <= x1 && tileID.x >= x0) &&
+ tileID.y <= y1 &&
+ tileID.y >= y0;
+ }
+ return false;
}
private:
- TileRange(Range<Point<double>> range_, uint8_t z_)
+ TileRange(Range<Point<uint32_t>> range_, Range<uint8_t> z_)
: range(range_),
- z(z_) {
+ zoomRange(z_) {
}
-
};
} // namespace util