Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] TileCoordinate::fromLatLng no longer depens on TransformState
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Oct 19, 2016
1 parent 2188d68 commit ef62b78
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion include/mbgl/util/projection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#include <mbgl/util/constants.hpp>
#include <mbgl/util/geo.hpp>
#include <mbgl/util/geometry.hpp>
#include <mbgl/math/clamp.hpp>

#include <cmath>

namespace mbgl {

Expand Down
14 changes: 8 additions & 6 deletions src/mbgl/util/tile_coordinate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@ class TileCoordinate {
TileCoordinatePoint p;
double z;

static TileCoordinate fromLatLng(const TransformState& state, double zoom, const LatLng& latLng) {
const double scale = std::pow(2, zoom - state.getZoom());
return { Projection::project(latLng, state.getScale()) * scale / double(util::tileSize), zoom };
static TileCoordinate fromLatLng(double zoom, const LatLng& latLng) {
const double scale = std::pow(2.0, zoom);
return { Projection::project(latLng, scale) / double(util::tileSize), zoom };
}

static TileCoordinate fromScreenCoordinate(const TransformState& state, double zoom, const ScreenCoordinate& screenCoordinate) {
return fromLatLng(state, zoom, state.screenCoordinateToLatLng(screenCoordinate));
return fromLatLng(zoom, state.screenCoordinateToLatLng(screenCoordinate));
}

TileCoordinate zoomTo(double zoom) const {
return { p * std::pow(2, zoom - z), zoom };
const double scaleDiff = std::pow(2.0, zoom - z);
return { p * scaleDiff, zoom };
}

static GeometryCoordinate toGeometryCoordinate(const UnwrappedTileID& tileID, const TileCoordinatePoint& point) {
const double scale = std::pow(2.0, tileID.canonical.z);
auto zoomed = TileCoordinate { point, 0 }.zoomTo(tileID.canonical.z);
return {
int16_t(util::clamp<int64_t>((zoomed.p.x - tileID.canonical.x - tileID.wrap * std::pow(2.0, tileID.canonical.z)) * util::EXTENT,
int16_t(util::clamp<int64_t>((zoomed.p.x - tileID.canonical.x - tileID.wrap * scale) * util::EXTENT,
std::numeric_limits<int16_t>::min(),
std::numeric_limits<int16_t>::max())),
int16_t(util::clamp<int64_t>((zoomed.p.y - tileID.canonical.y) * util::EXTENT,
Expand Down
11 changes: 5 additions & 6 deletions src/mbgl/util/tile_cover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,12 @@ std::vector<UnwrappedTileID> tileCover(const LatLngBounds& bounds_, int32_t z) {
{ std::max(bounds_.south(), -util::LATITUDE_MAX), bounds_.west() },
{ std::min(bounds_.north(), util::LATITUDE_MAX), bounds_.east() });

const TransformState state;
return tileCover(
TileCoordinate::fromLatLng(state, z, bounds.northwest()).p,
TileCoordinate::fromLatLng(state, z, bounds.northeast()).p,
TileCoordinate::fromLatLng(state, z, bounds.southeast()).p,
TileCoordinate::fromLatLng(state, z, bounds.southwest()).p,
TileCoordinate::fromLatLng(state, z, bounds.center()).p,
TileCoordinate::fromLatLng(z, bounds.northwest()).p,
TileCoordinate::fromLatLng(z, bounds.northeast()).p,
TileCoordinate::fromLatLng(z, bounds.southeast()).p,
TileCoordinate::fromLatLng(z, bounds.southwest()).p,
TileCoordinate::fromLatLng(z, bounds.center()).p,
z);
}

Expand Down
4 changes: 2 additions & 2 deletions test/tile/tile_coordinate.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ TEST(TileCoordinate, FromLatLng) {
for (const auto& pair : edges) {
const auto& latLng = pair.first;
const auto& screenCoordinate = pair.second;
const auto base = TileCoordinate::fromLatLng(transform.getState(), 0, latLng);
const auto base = TileCoordinate::fromLatLng(0, latLng);

// 16 is the maximum zoom level where we actually compute placements.
for (uint8_t integerZoom = 0; integerZoom <= 16; ++integerZoom) {
Expand All @@ -52,7 +52,7 @@ TEST(TileCoordinate, FromLatLng) {
latLng.latitude == 0 ? 0.5 : latLng.latitude == -util::LATITUDE_MAX ? 1.0 : 0,
};

const auto fromLatLng = TileCoordinate::fromLatLng(transform.getState(), zoom, latLng);
const auto fromLatLng = TileCoordinate::fromLatLng(zoom, latLng);
ASSERT_DOUBLE_EQ(fromLatLng.z, zoom);
ASSERT_DOUBLE_EQ(fromLatLng.p.x, tilePoint.x * maxTilesPerAxis);
ASSERT_NEAR(fromLatLng.p.y, tilePoint.y * maxTilesPerAxis, 1.0e-7);
Expand Down

0 comments on commit ef62b78

Please sign in to comment.