1 #pragma once
2 
3 #include <mbgl/map/transform_state.hpp>
4 #include <mbgl/math/clamp.hpp>
5 #include <mbgl/tile/geometry_tile_data.hpp>
6 #include <mbgl/tile/tile_id.hpp>
7 #include <mbgl/util/geometry.hpp>
8 #include <mbgl/util/projection.hpp>
9 
10 namespace mbgl {
11 
12 using TileCoordinatePoint = Point<double>;
13 
14 // Has floating point x/y coordinates.
15 // Used for computing the tiles that need to be visible in the viewport.
16 class TileCoordinate {
17 public:
18     TileCoordinatePoint p;
19     double z;
20 
fromLatLng(double zoom,const LatLng & latLng)21     static TileCoordinate fromLatLng(double zoom, const LatLng& latLng) {
22         const double scale = std::pow(2.0, zoom);
23         return { Projection::project(latLng, scale) / util::tileSize, zoom };
24     }
25 
fromScreenCoordinate(const TransformState & state,double zoom,const ScreenCoordinate & screenCoordinate)26     static TileCoordinate fromScreenCoordinate(const TransformState& state, double zoom, const ScreenCoordinate& screenCoordinate) {
27         return fromLatLng(zoom, state.screenCoordinateToLatLng(screenCoordinate));
28     }
29 
zoomTo(double zoom) const30     TileCoordinate zoomTo(double zoom) const {
31         const double scaleDiff = std::pow(2.0, zoom - z);
32         return { p * scaleDiff, zoom };
33     }
34 
toGeometryCoordinate(const UnwrappedTileID & tileID,const TileCoordinatePoint & point)35     static GeometryCoordinate toGeometryCoordinate(const UnwrappedTileID& tileID, const TileCoordinatePoint& point) {
36         const double scale = std::pow(2.0, tileID.canonical.z);
37         auto zoomed = TileCoordinate { point, 0 }.zoomTo(tileID.canonical.z);
38         return {
39             int16_t(util::clamp<int64_t>((zoomed.p.x - tileID.canonical.x - tileID.wrap * scale) * util::EXTENT,
40                         std::numeric_limits<int16_t>::min(),
41                         std::numeric_limits<int16_t>::max())),
42             int16_t(util::clamp<int64_t>((zoomed.p.y - tileID.canonical.y) * util::EXTENT,
43                         std::numeric_limits<int16_t>::min(),
44                         std::numeric_limits<int16_t>::max()))
45         };
46     }
47 };
48 
49 } // namespace mbgl
50