1 #pragma once
2 
3 #include <mbgl/tile/tile_id.hpp>
4 #include <mbgl/util/range.hpp>
5 #include <mbgl/util/geo.hpp>
6 #include <mbgl/util/projection.hpp>
7 
8 namespace mbgl {
9 namespace util {
10 
11 class TileRange {
12 public:
13     Range<Point<uint32_t>> range;
14     Range<uint8_t> zoomRange;
15 
16     // Compute the range of tiles covered by the bounds at maxZoom.
fromLatLngBounds(const LatLngBounds & bounds,uint8_t minZoom,uint8_t maxZoom)17     static TileRange fromLatLngBounds(const LatLngBounds& bounds, uint8_t minZoom, uint8_t maxZoom) {
18         if (minZoom > maxZoom) {
19             std::swap(minZoom, maxZoom);
20         }
21 
22         auto swProj = Projection::project(bounds.southwest().wrapped(), maxZoom);
23         auto ne = bounds.northeast();
24         auto neProj = Projection::project(ne.longitude() > util::LONGITUDE_MAX ? ne.wrapped() : ne , maxZoom);
25 
26         const auto maxTile = std::pow(2.0, maxZoom);
27         const auto minX = static_cast<uint32_t>(std::floor(swProj.x));
28         const auto maxX = static_cast<uint32_t>(std::floor(neProj.x));
29         const auto minY = static_cast<uint32_t>(util::clamp(std::floor(neProj.y), 0.0 , maxTile));
30         const auto maxY = static_cast<uint32_t>(util::clamp(std::floor(swProj.y), 0.0, maxTile));
31 
32         return TileRange({ {minX, minY}, {maxX, maxY} }, {minZoom, maxZoom});
33     }
34 
35     // Compute the range of tiles covered by the bounds.
fromLatLngBounds(const LatLngBounds & bounds,uint8_t z)36     static TileRange fromLatLngBounds(const LatLngBounds& bounds, uint8_t z) {
37         return fromLatLngBounds(bounds, z, z);
38     }
39 
contains(const CanonicalTileID & tileID)40     bool contains(const CanonicalTileID& tileID) {
41         if (tileID.z <= zoomRange.max && tileID.z >= zoomRange.min) {
42             if (tileID.z == 0) {
43                 return true;
44             }
45             uint8_t dz = (zoomRange.max - tileID.z);
46             auto x0 = range.min.x >> dz;
47             auto x1 = range.max.x >> dz;
48             auto y0 = range.min.y >> dz;
49             auto y1 = range.max.y >> dz;
50             return (range.min.x > range.max.x ? //For wrapped bounds
51                     tileID.x >= x0 || tileID.x <= x1 :
52                     tileID.x <= x1 && tileID.x >= x0) &&
53                 tileID.y <= y1 &&
54                 tileID.y >= y0;
55         }
56         return false;
57     }
58 
59 private:
TileRange(Range<Point<uint32_t>> range_,Range<uint8_t> z_)60     TileRange(Range<Point<uint32_t>> range_, Range<uint8_t> z_)
61      : range(range_),
62      zoomRange(z_) {
63     }
64 };
65 
66 } // namespace util
67 } // namespace mbgl
68