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

Commit

Permalink
fix LatLng --> point for perspective views
Browse files Browse the repository at this point in the history
  • Loading branch information
ansis committed Aug 24, 2015
1 parent 99c5976 commit 51c9386
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 99 deletions.
8 changes: 0 additions & 8 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1621,21 +1621,13 @@ - (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(null
{
CGPoint convertedPoint = [self convertPoint:point fromView:view];

// flip y coordinate for iOS view origin top left
//
convertedPoint.y = self.bounds.size.height - convertedPoint.y;

return MGLLocationCoordinate2DFromLatLng(_mbglMap->latLngForPixel(mbgl::vec2<double>(convertedPoint.x, convertedPoint.y)));
}

- (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(nullable UIView *)view
{
mbgl::vec2<double> pixel = _mbglMap->pixelForLatLng(MGLLatLngFromLocationCoordinate2D(coordinate));

// flip y coordinate for iOS view origin in top left
//
pixel.y = self.bounds.size.height - pixel.y;

return [self convertPoint:CGPointMake(pixel.x, pixel.y) toView:view];
}

Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,11 @@ const LatLng Map::latLngForProjectedMeters(const ProjectedMeters projectedMeters
}

const vec2<double> Map::pixelForLatLng(const LatLng latLng) const {
return transform->getState().pixelForLatLng(latLng);
return transform->getState().latLngToPoint(latLng);
}

const LatLng Map::latLngForPixel(const vec2<double> pixel) const {
return transform->getState().latLngForPixel(pixel);
return transform->getState().pointToLatLng(pixel);
}

#pragma mark - Annotations
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/map/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ std::forward_list<TileID> Source::coveringTiles(const TransformState& state) con

// Map four viewport corners to pixel coordinates
box points = state.cornersToBox(z);
const TileCoordinate center = state.pointCoordinate({ state.getWidth() / 2.0f, state.getHeight()/ 2.0f }).zoomTo(z);
const TileCoordinate center = state.pointToCoordinate({ state.getWidth() / 2.0f, state.getHeight()/ 2.0f }).zoomTo(z);

std::forward_list<TileID> covering_tiles = tileCover(z, points, reparseOverscaled ? actualZ : z);

Expand Down
134 changes: 56 additions & 78 deletions src/mbgl/map/transform_state.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <mbgl/map/transform_state.hpp>
#include <mbgl/map/tile_id.hpp>
#include <mbgl/util/projection.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/box.hpp>
#include <mbgl/util/tile_coordinate.hpp>
Expand Down Expand Up @@ -46,10 +45,10 @@ box TransformState::cornersToBox(uint32_t z) const {
double w = width;
double h = height;
box b(
pointCoordinate({ 0, 0 }).zoomTo(z),
pointCoordinate({ w, 0 }).zoomTo(z),
pointCoordinate({ w, h }).zoomTo(z),
pointCoordinate({ 0, h }).zoomTo(z));
pointToCoordinate({ 0, 0 }).zoomTo(z),
pointToCoordinate({ w, 0 }).zoomTo(z),
pointToCoordinate({ w, h }).zoomTo(z),
pointToCoordinate({ 0, h }).zoomTo(z));
return b;
}

Expand Down Expand Up @@ -158,94 +157,73 @@ float TransformState::getPitch() const {

#pragma mark - Projection

const vec2<double> TransformState::pixelForLatLng(const LatLng latLng) const {
LatLng ll = getLatLng();
double zoom = getZoom();

const double centerX = static_cast<double>(width) / 2.0;
const double centerY = static_cast<double>(height) / 2.0;

const double m = Projection::getMetersPerPixelAtLatitude(0, zoom);

const double angle_sin = std::sin(-angle);
const double angle_cos = std::cos(-angle);

const ProjectedMeters givenMeters = Projection::projectedMetersForLatLng(latLng);

const double givenAbsoluteX = givenMeters.easting / m;
const double givenAbsoluteY = givenMeters.northing / m;

const ProjectedMeters centerMeters = Projection::projectedMetersForLatLng(ll);

const double centerAbsoluteX = centerMeters.easting / m;
const double centerAbsoluteY = centerMeters.northing / m;

const double deltaX = givenAbsoluteX - centerAbsoluteX;
const double deltaY = givenAbsoluteY - centerAbsoluteY;

const double translatedX = deltaX + centerX;
const double translatedY = deltaY + centerY;

const double rotatedX = translatedX * angle_cos - translatedY * angle_sin;
const double rotatedY = translatedX * angle_sin + translatedY * angle_cos;

const double rotatedCenterX = centerX * angle_cos - centerY * angle_sin;
const double rotatedCenterY = centerX * angle_sin + centerY * angle_cos;

double x_ = rotatedX + (centerX - rotatedCenterX);
double y_ = rotatedY + (centerY - rotatedCenterY);

return vec2<double>(x_, y_);
float TransformState::lngX(float lng) const {
return (180.0f + lng) * worldSize() / 360.0f;
}

const LatLng TransformState::latLngForPixel(const vec2<double> pixel) const {
LatLng ll = getLatLng();
double zoom = getZoom();

const double centerX = static_cast<double>(width) / 2.0;
const double centerY = static_cast<double>(height) / 2.0;

const double m = Projection::getMetersPerPixelAtLatitude(0, zoom);

const double angle_sin = std::sin(angle);
const double angle_cos = std::cos(angle);

const double unrotatedCenterX = centerX * angle_cos - centerY * angle_sin;
const double unrotatedCenterY = centerX * angle_sin + centerY * angle_cos;
float TransformState::latY(float lat) const {
float y_ = 180.0f / M_PI * std::log(std::tan(M_PI / 4 + lat * M_PI / 360.0f));
return (180.0f - y_) * worldSize() / 360.0f;
}

const double unrotatedX = pixel.x * angle_cos - pixel.y * angle_sin;
const double unrotatedY = pixel.x * angle_sin + pixel.y * angle_cos;
float TransformState::xLng(float x_, float worldSize_) const {
return x_ * 360.0f / worldSize_ - 180.0f;
}

const double givenX = unrotatedX + (centerX - unrotatedCenterX);
const double givenY = unrotatedY + (centerY - unrotatedCenterY);
float TransformState::yLat(float y_, float worldSize_) const {
float y2 = 180.0f - y_ * 360.0f / worldSize_;
return 360.0f / M_PI * std::atan(std::exp(y2 * M_PI / 180.0f)) - 90.0f;
}

const ProjectedMeters centerMeters = Projection::projectedMetersForLatLng(ll);
float TransformState::zoomScale(float zoom) const {
return std::pow(2.0f, zoom);
}

const double centerAbsoluteX = centerMeters.easting / m;
const double centerAbsoluteY = centerMeters.northing / m;
float TransformState::worldSize() const {
return util::tileSize * scale;
}

const double givenAbsoluteX = givenX + centerAbsoluteX - centerX;
const double givenAbsoluteY = givenY + centerAbsoluteY - centerY;
vec2<double> TransformState::latLngToPoint(const LatLng& latLng) const {
return coordinateToPoint(latLngToCoordinate(latLng));
}

ProjectedMeters givenMeters = ProjectedMeters(givenAbsoluteY * m, givenAbsoluteX * m);
LatLng TransformState::pointToLatLng(const vec2<double> point) const {
return coordinateToLatLng(pointToCoordinate(point));
}

// adjust for date line
ProjectedMeters sw, ne;
Projection::getWorldBoundsMeters(sw, ne);
double d = ne.easting - sw.easting;
if (ll.longitude > 0 && givenMeters.easting > centerMeters.easting) givenMeters.easting -= d;
TileCoordinate TransformState::latLngToCoordinate(const LatLng& latLng) const {
const float tileZoom = getIntegerZoom();
const float k = zoomScale(tileZoom) / worldSize();
return {
lngX(latLng.longitude) * k,
latY(latLng.latitude) * k,
tileZoom
};
}

// adjust for world wrap
while (givenMeters.easting < sw.easting) givenMeters.easting += d;
while (givenMeters.easting > ne.easting) givenMeters.easting -= d;
LatLng TransformState::coordinateToLatLng(const TileCoordinate& coord) const {
const float worldSize_ = zoomScale(coord.zoom);
LatLng latLng = {
yLat(coord.row, worldSize_),
xLng(coord.column, worldSize_)
};
while (latLng.longitude < 180.0f) latLng.longitude += 360.0f;
while (latLng.longitude > 180.0f) latLng.longitude -= 360.0f;
return latLng;
}

return Projection::latLngForProjectedMeters(givenMeters);
vec2<double> TransformState::coordinateToPoint(const TileCoordinate& coord) const {
mat4 mat = coordinatePointMatrix(coord.zoom);
matrix::vec4 p;
matrix::vec4 c = {{ coord.column, coord.row, 0, 1 }};
matrix::transformMat4(p, c, mat);
return { p[0] / p[3], p[1] / p[3] };
}

TileCoordinate TransformState::pointCoordinate(const vec2<double> point) const {
TileCoordinate TransformState::pointToCoordinate(const vec2<double> point) const {

float targetZ = 0;
float tileZoom = std::floor(getZoom());
const float tileZoom = getIntegerZoom();

mat4 mat = coordinatePointMatrix(tileZoom);

Expand Down
26 changes: 17 additions & 9 deletions src/mbgl/map/transform_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ class TransformState {
uint16_t getWidth() const;
uint16_t getHeight() const;

float worldSize() const;
float lngX(float lon) const;
float latY(float lat) const;
std::array<float, 2> locationCoordinate(float lon, float lat) const;
void getLonLat(double &lon, double &lat) const;

Expand All @@ -55,18 +52,22 @@ class TransformState {
float getAltitude() const;
float getPitch() const;

// Projection
const vec2<double> pixelForLatLng(const LatLng latLng) const;
const LatLng latLngForPixel(const vec2<double> pixel) const;

// Changing
bool isChanging() const;

double pixel_x() const;
double pixel_y() const;

// Conversions
TileCoordinate pointCoordinate(const vec2<double> point) const;
// Conversion and projection

vec2<double> latLngToPoint(const LatLng& latLng) const;
LatLng pointToLatLng(const vec2<double> point) const;

TileCoordinate latLngToCoordinate(const LatLng& latLng) const;
LatLng coordinateToLatLng(const TileCoordinate& coord) const;

vec2<double> coordinateToPoint(const TileCoordinate& coord) const;
TileCoordinate pointToCoordinate(const vec2<double> point) const;

private:
void constrain(double& scale, double& y) const;
Expand All @@ -78,6 +79,13 @@ class TransformState {
// logical dimensions
uint16_t width = 0, height = 0;

float xLng(float x, float worldSize) const;
float yLat(float y, float worldSize) const;
float lngX(float lon) const;
float latY(float lat) const;
float zoomScale(float zoom) const;
float worldSize() const;

mat4 coordinatePointMatrix(float z) const;
mat4 getPixelMatrix() const;

Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/painter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ void Painter::renderBackground(const StyleLayer &layer_desc) {
patternShader->u_opacity = properties.opacity;

LatLng latLng = state.getLatLng();
vec2<double> center = state.pixelForLatLng(latLng);
vec2<double> center = state.latLngToPoint(latLng);
float scale = 1 / std::pow(2, zoomFraction);

std::array<float, 2> sizeA = imagePosA.size;
Expand Down

0 comments on commit 51c9386

Please sign in to comment.