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

Commit

Permalink
[core] Move TransformState::{un,}project() to Projection
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Oct 19, 2016
1 parent f01a829 commit 2188d68
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 35 deletions.
16 changes: 16 additions & 0 deletions include/mbgl/util/projection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ class Projection {

return LatLng(latitude, longitude);
}

static Point<double> project(const LatLng& latLng, double scale) {
return Point<double> {
util::LONGITUDE_MAX + latLng.longitude,
util::LONGITUDE_MAX - util::RAD2DEG * std::log(std::tan(M_PI / 4 + latLng.latitude * M_PI / util::DEGREES_MAX))
} * worldSize(scale) / util::DEGREES_MAX;
}

static LatLng unproject(const Point<double>& p, double scale, LatLng::WrapMode wrapMode = LatLng::Unwrapped) {
auto p2 = p * util::DEGREES_MAX / worldSize(scale);
return LatLng {
util::DEGREES_MAX / M_PI * std::atan(std::exp((util::LONGITUDE_MAX - p2.y) * util::DEG2RAD)) - 90.0,
p2.x - util::LONGITUDE_MAX,
wrapMode
};
}
};

} // namespace mbgl
15 changes: 7 additions & 8 deletions src/mbgl/map/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim
// Find the shortest path otherwise.
else startLatLng.unwrapForShortestPath(latLng);

const Point<double> startPoint = state.project(startLatLng);
const Point<double> endPoint = state.project(latLng);
const Point<double> startPoint = Projection::project(startLatLng, state.scale);
const Point<double> endPoint = Projection::project(latLng, state.scale);

ScreenCoordinate center = getScreenCoordinate(padding);
center.y = state.height - center.y;
Expand All @@ -133,7 +133,6 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim
Duration duration = animation.duration ? *animation.duration : Duration::zero();

const double startScale = state.scale;
const double startWorldSize = Projection::worldSize(startScale);
const double startAngle = state.angle;
const double startPitch = state.pitch;
state.panning = latLng != startLatLng;
Expand All @@ -142,7 +141,7 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim

startTransition(camera, animation, [=](double t) {
Point<double> framePoint = util::interpolate(startPoint, endPoint, t);
LatLng frameLatLng = state.unproject(framePoint, startWorldSize);
LatLng frameLatLng = Projection::unproject(framePoint, startScale);
double frameScale = util::interpolate(startScale, scale, t);
state.setLatLngZoom(frameLatLng, state.scaleZoom(frameScale));

Expand Down Expand Up @@ -184,8 +183,8 @@ void Transform::flyTo(const CameraOptions &camera, const AnimationOptions &anima
LatLng startLatLng = getLatLng(padding).wrapped();
startLatLng.unwrapForShortestPath(latLng);

const Point<double> startPoint = state.project(startLatLng);
const Point<double> endPoint = state.project(latLng);
const Point<double> startPoint = Projection::project(startLatLng, state.scale);
const Point<double> endPoint = Projection::project(latLng, state.scale);

ScreenCoordinate center = getScreenCoordinate(padding);
center.y = state.height - center.y;
Expand Down Expand Up @@ -289,7 +288,7 @@ void Transform::flyTo(const CameraOptions &camera, const AnimationOptions &anima
return;
}

const double startWorldSize = Projection::worldSize(state.scale);
const double startScale = state.scale;
state.panning = true;
state.scaling = true;
state.rotating = angle != startAngle;
Expand All @@ -305,7 +304,7 @@ void Transform::flyTo(const CameraOptions &camera, const AnimationOptions &anima
double frameZoom = startZoom + state.scaleZoom(1 / w(s));

// Convert to geographic coordinates and set the new viewpoint.
LatLng frameLatLng = state.unproject(framePoint, startWorldSize);
LatLng frameLatLng = Projection::unproject(framePoint, startScale);
state.setLatLngZoom(frameLatLng, frameZoom);

if (angle != startAngle) {
Expand Down
28 changes: 6 additions & 22 deletions src/mbgl/map/transform_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,6 @@ bool TransformState::isGestureInProgress() const {

#pragma mark - Projection

Point<double> TransformState::project(const LatLng& ll) const {
return Point<double>(
(util::LONGITUDE_MAX + ll.longitude),
(util::LONGITUDE_MAX - util::RAD2DEG * std::log(std::tan(M_PI / 4 + ll.latitude * M_PI / util::DEGREES_MAX)))
) * Projection::worldSize(scale) / util::DEGREES_MAX;
}

LatLng TransformState::unproject(const Point<double>& p, double worldSize, LatLng::WrapMode wrapMode) const {
Point<double> p2 = p * util::DEGREES_MAX / worldSize;
return LatLng(
util::DEGREES_MAX / M_PI * std::atan(std::exp((util::LONGITUDE_MAX - p2.y) * util::DEG2RAD)) - 90.0f,
p2.x - util::LONGITUDE_MAX,
wrapMode
);
}

double TransformState::zoomScale(double zoom) const {
return std::pow(2.0f, zoom);
}
Expand All @@ -232,7 +216,7 @@ ScreenCoordinate TransformState::latLngToScreenCoordinate(const LatLng& latLng)

mat4 mat = coordinatePointMatrix(getZoom());
vec4 p;
Point<double> pt = project(latLng) / double(util::tileSize);
Point<double> pt = Projection::project(latLng, scale) / double(util::tileSize);
vec4 c = {{ pt.x, pt.y, 0, 1 }};
matrix::transformMat4(p, c, mat);
return { p[0] / p[3], height - p[1] / p[3] };
Expand Down Expand Up @@ -274,7 +258,7 @@ LatLng TransformState::screenCoordinateToLatLng(const ScreenCoordinate& point, L
double z1 = coord1[2] / w1;
double t = z0 == z1 ? 0 : (targetZ - z0) / (z1 - z0);

return unproject(util::interpolate(p0, p1, t), scale, wrapMode);
return Projection::unproject(util::interpolate(p0, p1, t), scale / util::tileSize, wrapMode);
}

mat4 TransformState::coordinatePointMatrix(double z) const {
Expand Down Expand Up @@ -321,10 +305,10 @@ void TransformState::constrain(double& scale_, double& x_, double& y_) const {
}

void TransformState::moveLatLng(const LatLng& latLng, const ScreenCoordinate& anchor) {
auto centerCoord = project(getLatLng(LatLng::Unwrapped));
auto latLngCoord = project(latLng);
auto anchorCoord = project(screenCoordinateToLatLng(anchor));
setLatLngZoom(unproject(centerCoord + latLngCoord - anchorCoord, Projection::worldSize(scale)), getZoom());
auto centerCoord = Projection::project(getLatLng(LatLng::Unwrapped), scale);
auto latLngCoord = Projection::project(latLng, scale);
auto anchorCoord = Projection::project(screenCoordinateToLatLng(anchor), scale);
setLatLngZoom(Projection::unproject(centerCoord + latLngCoord - anchorCoord, scale), getZoom());
}

void TransformState::setLatLngZoom(const LatLng &latLng, double zoom) {
Expand Down
5 changes: 1 addition & 4 deletions src/mbgl/map/transform_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,10 @@ class TransformState {
bool isPanning() const;
bool isGestureInProgress() const;

// Conversion and projection
// Conversion
ScreenCoordinate latLngToScreenCoordinate(const LatLng&) const;
LatLng screenCoordinateToLatLng(const ScreenCoordinate&, LatLng::WrapMode = LatLng::Unwrapped) const;

Point<double> project(const LatLng&) const;
LatLng unproject(const Point<double>&, double worldSize, LatLng::WrapMode = LatLng::Unwrapped) const;

double zoomScale(double zoom) const;
double scaleZoom(double scale) const;

Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/util/tile_coordinate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <mbgl/tile/geometry_tile_data.hpp>
#include <mbgl/tile/tile_id.hpp>
#include <mbgl/util/geometry.hpp>
#include <mbgl/util/projection.hpp>

namespace mbgl {

Expand All @@ -19,7 +20,7 @@ class TileCoordinate {

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

static TileCoordinate fromScreenCoordinate(const TransformState& state, double zoom, const ScreenCoordinate& screenCoordinate) {
Expand Down

0 comments on commit 2188d68

Please sign in to comment.