[core] Move TransformState::{un,}project() to Projection
This commit is contained in:
@@ -46,6 +46,22 @@ public:
|
||||
|
||||
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
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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));
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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] };
|
||||
@@ -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 {
|
||||
@@ -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) {
|
||||
|
||||
@@ -66,13 +66,10 @@ public:
|
||||
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;
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -19,7 +20,7 @@ public:
|
||||
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user