Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unproject not working for -ve y values #10224

Merged
merged 6 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/geo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -900,9 +900,8 @@ class Transform {
* @private
*/
pointCoordinate(p: Point): MercatorCoordinate {
// For p above horizon, don't return point behind camera but clamp p.y at horizon line.
const horizonOffset = this.horizonLineFromTop();
const clamped = horizonOffset > p.y ? new Point(p.x, horizonOffset) : p;
const horizonOffset = this.horizonLineFromTop(false);
const clamped = new Point(p.x, Math.max(horizonOffset, p.y));

return this.rayIntersectionCoordinate(this.pointRayIntersection(clamped));
}
Expand Down Expand Up @@ -1012,17 +1011,16 @@ class Transform {
}

/**
* Returns position oh horizon line, from the top, in pixels. If horizon is not
* visible, returns 0.
* Returns position of horizon line from the top of the map in pixels. If horizon is not visible, returns 0.
* @private
*/
horizonLineFromTop(): number {
horizonLineFromTop(clampToTop: boolean = true): number {
// h is height of space above map center to horizon.
const h = this.height / 2 / Math.tan(this._fov / 2) / Math.tan(Math.max(this._pitch, 0.1)) + this.centerOffset.y;
// incorporate 3% of the area above center to account for reduced precision.
const horizonEpsilon = 0.03;
const offset = this.height / 2 - h * (1 - horizonEpsilon);
return Math.max(0, offset);
return clampToTop ? Math.max(0, offset) : offset;
}

/**
Expand Down
54 changes: 54 additions & 0 deletions test/unit/geo/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,60 @@ test('transform', (t) => {
t.end();
});

test('pointCoordinate retains direction when point is offscreen', (t) => {

function assertDueNorth(t, m1, m2) {
const dx = m2.x - m1.x;
const dy = m2.y - m1.y;
const l = Math.sqrt(dx * dx + dy * dy);
const ndx = dx / l;
const ndy = dy / l;
t.ok(Math.abs(ndx) < 1e-10);
t.ok(Math.abs(ndy + 1) < 1e-10);
}

t.test('no pitch', (t) => {
const transform = new Transform();
transform.center = {lng: 0, lat: 0};
transform.zoom = 16;
transform.pitch = 0;
transform.bearing = 0;
transform.resize(512, 512);

const coord = transform.pointCoordinate(new Point(transform.width / 2, -10000));
assertDueNorth(t, {x: 0.5, y: 0.5, z : 0}, coord);
t.end();
});

t.test('high pitch', (t) => {
const transform = new Transform();
transform.center = {lng: 0, lat: 0};
transform.zoom = 16;
transform.pitch = 80;
transform.bearing = 0;
transform.resize(512, 512);

const coord = transform.pointCoordinate(new Point(transform.width / 2, -10000));
assertDueNorth(t, {x: 0.5, y: 0.5, z : 0}, coord);
t.end();
});

t.test('medium pitch', (t) => {
const transform = new Transform();
transform.center = {lng: 0, lat: 0};
transform.zoom = 16;
transform.pitch = 70;
transform.bearing = 0;
transform.resize(512, 512);

const coord = transform.pointCoordinate(new Point(transform.width / 2, -10000));
assertDueNorth(t, {x: 0.5, y: 0.5, z : 0}, coord);
t.end();
});

t.end();
});

test('coveringTiles', (t) => {
const options = {
minzoom: 1,
Expand Down
8 changes: 4 additions & 4 deletions test/unit/source/source_cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1435,11 +1435,11 @@ test('SourceCache#tilesIn', (t) => {

t.equal(tiles[0].tile.tileID.key, 16);
t.equal(tiles[0].tile.tileSize, 512);
t.deepEqual(round(tiles[0].bufferedTilespaceBounds), {min: {x: 4080, y: 4050}, max: {x:8192, y: 8162}});
t.deepEqual(round(tiles[0].bufferedTilespaceBounds), {min: {x: 4080, y: 4034}, max: {x:8192, y: 8162}});

t.equal(tiles[1].tile.tileID.key, 528);
t.equal(tiles[1].tile.tileSize, 512);
t.deepEqual(round(tiles[1].bufferedTilespaceBounds), {min: {x: 0, y: 4050}, max: {x: 4112, y: 8162}});
t.deepEqual(round(tiles[1].bufferedTilespaceBounds), {min: {x: 0, y: 4034}, max: {x: 4112, y: 8162}});

t.end();
}
Expand Down Expand Up @@ -1484,11 +1484,11 @@ test('SourceCache#tilesIn', (t) => {

t.equal(tiles[0].tile.tileID.key, 17);
t.equal(tiles[0].tile.tileSize, 1024);
t.deepEqual(round(tiles[0].bufferedTilespaceBounds), {min: {x: 4088, y: 4050}, max: {x:8192, y: 8154}});
t.deepEqual(round(tiles[0].bufferedTilespaceBounds), {min: {x: 4088, y: 4042}, max: {x:8192, y: 8154}});

t.equal(tiles[1].tile.tileID.key, 529);
t.equal(tiles[1].tile.tileSize, 1024);
t.deepEqual(round(tiles[1].bufferedTilespaceBounds), {min: {x: 0, y: 4050}, max: {x: 4104, y: 8154}});
t.deepEqual(round(tiles[1].bufferedTilespaceBounds), {min: {x: 0, y: 4042}, max: {x: 4104, y: 8154}});

t.end();
}
Expand Down
11 changes: 11 additions & 0 deletions test/unit/ui/camera.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,17 @@ test('camera', (t) => {
t.end();
});

t.test('pans equally in both directions', (t) => {
const camera = createCamera({bearing: 0});
const c = camera.getCenter();
camera.panBy([0, -10000], {duration: 0});
const c1 = camera.getCenter();
camera.panBy([0, 10000], {duration: 0});
const c2 = camera.getCenter();
t.ok(Math.abs(c1.lat - c.lat) - Math.abs(c2.lat - c.lat) < 1e-10);
t.end();
});

t.test('emits move events, preserving eventData', (t) => {
const camera = createCamera();
let started, moved;
Expand Down