summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/mbgl/util/projection.hpp14
-rw-r--r--src/mbgl/map/transform.cpp6
-rw-r--r--src/mbgl/map/transform_state.cpp20
-rw-r--r--src/mbgl/map/transform_state.hpp7
4 files changed, 24 insertions, 23 deletions
diff --git a/include/mbgl/util/projection.hpp b/include/mbgl/util/projection.hpp
index eb45088580..0b6e9f6bf1 100644
--- a/include/mbgl/util/projection.hpp
+++ b/include/mbgl/util/projection.hpp
@@ -8,14 +8,20 @@
namespace mbgl {
+// Spherical Mercator projection
+// http://docs.openlayers.org/library/spherical_mercator.html
class Projection {
-
public:
+ // Map pixel width at given scale.
+ static double worldSize(double scale) {
+ return scale * util::tileSize;
+ }
+
static double getMetersPerPixelAtLatitude(double lat, double zoom) {
const double constrainedZoom = util::clamp(zoom, util::MIN_ZOOM, util::MAX_ZOOM);
- const double mapPixelWidthAtZoom = std::pow(2.0, constrainedZoom) * util::tileSize;
+ const double constrainedScale = std::pow(2.0, constrainedZoom);
const double constrainedLatitude = util::clamp(lat, -util::LATITUDE_MAX, util::LATITUDE_MAX);
- return std::cos(constrainedLatitude * util::DEG2RAD) * util::M2PI * util::EARTH_RADIUS_M / mapPixelWidthAtZoom;
+ return std::cos(constrainedLatitude * util::DEG2RAD) * util::M2PI * util::EARTH_RADIUS_M / worldSize(constrainedScale);
}
static ProjectedMeters projectedMetersForLatLng(const LatLng& latLng) {
@@ -32,7 +38,7 @@ public:
}
static LatLng latLngForProjectedMeters(const ProjectedMeters& projectedMeters) {
- double latitude = (2 * std::atan(std::exp(projectedMeters.northing / util::EARTH_RADIUS_M)) - (M_PI / 2)) * util::RAD2DEG;
+ double latitude = (2 * std::atan(std::exp(projectedMeters.northing / util::EARTH_RADIUS_M)) - (M_PI / 2.0)) * util::RAD2DEG;
double longitude = projectedMeters.easting * util::RAD2DEG / util::EARTH_RADIUS_M;
latitude = util::clamp(latitude, -util::LATITUDE_MAX, util::LATITUDE_MAX);
diff --git a/src/mbgl/map/transform.cpp b/src/mbgl/map/transform.cpp
index bf511ed670..71e7739b5a 100644
--- a/src/mbgl/map/transform.cpp
+++ b/src/mbgl/map/transform.cpp
@@ -7,6 +7,7 @@
#include <mbgl/util/unitbezier.hpp>
#include <mbgl/util/interpolate.hpp>
#include <mbgl/util/chrono.hpp>
+#include <mbgl/util/projection.hpp>
#include <mbgl/math/clamp.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/platform/platform.hpp>
@@ -131,8 +132,8 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim
Duration duration = animation.duration ? *animation.duration : Duration::zero();
- const double startWorldSize = state.worldSize();
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;
@@ -288,8 +289,7 @@ void Transform::flyTo(const CameraOptions &camera, const AnimationOptions &anima
return;
}
- const double startWorldSize = state.worldSize();
-
+ const double startWorldSize = Projection::worldSize(state.scale);
state.panning = true;
state.scaling = true;
state.rotating = angle != startAngle;
diff --git a/src/mbgl/map/transform_state.cpp b/src/mbgl/map/transform_state.cpp
index eb66513626..6256c7d0fe 100644
--- a/src/mbgl/map/transform_state.cpp
+++ b/src/mbgl/map/transform_state.cpp
@@ -17,7 +17,7 @@ TransformState::TransformState(ConstrainMode constrainMode_, ViewportMode viewpo
void TransformState::matrixFor(mat4& matrix, const UnwrappedTileID& tileID) const {
const uint64_t tileScale = 1ull << tileID.canonical.z;
- const double s = worldSize() / tileScale;
+ const double s = Projection::worldSize(scale) / tileScale;
matrix::identity(matrix);
matrix::translate(matrix, matrix,
@@ -108,12 +108,12 @@ LatLng TransformState::getLatLng(LatLng::WrapMode wrapMode) const {
}
double TransformState::pixel_x() const {
- const double center = (width - worldSize()) / 2;
+ const double center = (width - Projection::worldSize(scale)) / 2;
return center + x;
}
double TransformState::pixel_y() const {
- const double center = (height - worldSize()) / 2;
+ const double center = (height - Projection::worldSize(scale)) / 2;
return center + y;
}
@@ -205,7 +205,7 @@ 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)))
- ) * worldSize() / util::DEGREES_MAX;
+ ) * Projection::worldSize(scale) / util::DEGREES_MAX;
}
LatLng TransformState::unproject(const Point<double>& p, double worldSize, LatLng::WrapMode wrapMode) const {
@@ -225,10 +225,6 @@ double TransformState::scaleZoom(double s) const {
return util::log2(s);
}
-double TransformState::worldSize() const {
- return scale * util::tileSize;
-}
-
ScreenCoordinate TransformState::latLngToScreenCoordinate(const LatLng& latLng) const {
if (width == 0 || height == 0) {
return {};
@@ -284,7 +280,7 @@ LatLng TransformState::screenCoordinateToLatLng(const ScreenCoordinate& point, L
mat4 TransformState::coordinatePointMatrix(double z) const {
mat4 proj;
getProjMatrix(proj);
- float s = worldSize() / std::pow(2, z);
+ float s = Projection::worldSize(scale) / std::pow(2, z);
matrix::scale(proj, proj, s, s, 1);
matrix::multiply(proj, getPixelMatrix(), proj);
return proj;
@@ -328,7 +324,7 @@ void TransformState::moveLatLng(const LatLng& latLng, const ScreenCoordinate& an
auto centerCoord = project(getLatLng(LatLng::Unwrapped));
auto latLngCoord = project(latLng);
auto anchorCoord = project(screenCoordinateToLatLng(anchor));
- setLatLngZoom(unproject(centerCoord + latLngCoord - anchorCoord, worldSize()), getZoom());
+ setLatLngZoom(unproject(centerCoord + latLngCoord - anchorCoord, Projection::worldSize(scale)), getZoom());
}
void TransformState::setLatLngZoom(const LatLng &latLng, double zoom) {
@@ -355,8 +351,8 @@ void TransformState::setScalePoint(const double newScale, const ScreenCoordinate
scale = constrainedScale;
x = constrainedPoint.x;
y = constrainedPoint.y;
- Bc = worldSize() / util::DEGREES_MAX;
- Cc = worldSize() / util::M2PI;
+ Bc = Projection::worldSize(scale) / util::DEGREES_MAX;
+ Cc = Projection::worldSize(scale) / util::M2PI;
}
} // namespace mbgl
diff --git a/src/mbgl/map/transform_state.hpp b/src/mbgl/map/transform_state.hpp
index 1e4b2054f7..10d2301ea4 100644
--- a/src/mbgl/map/transform_state.hpp
+++ b/src/mbgl/map/transform_state.hpp
@@ -4,6 +4,7 @@
#include <mbgl/util/geo.hpp>
#include <mbgl/util/geometry.hpp>
#include <mbgl/util/constants.hpp>
+#include <mbgl/util/projection.hpp>
#include <mbgl/util/mat4.hpp>
#include <cstdint>
@@ -88,8 +89,6 @@ private:
// logical dimensions
uint16_t width = 0, height = 0;
- double worldSize() const;
-
mat4 coordinatePointMatrix(double z) const;
mat4 getPixelMatrix() const;
@@ -117,8 +116,8 @@ private:
double pitch = 0.0;
// cache values for spherical mercator math
- double Bc = worldSize() / util::DEGREES_MAX;
- double Cc = worldSize() / util::M2PI;
+ double Bc = Projection::worldSize(scale) / util::DEGREES_MAX;
+ double Cc = Projection::worldSize(scale) / util::M2PI;
};
} // namespace mbgl