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

Adaptive bounds calculation #12286

Merged
merged 10 commits into from
Oct 17, 2022
55 changes: 54 additions & 1 deletion src/geo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import LngLat from './lng_lat.js';
import LngLatBounds from './lng_lat_bounds.js';
import MercatorCoordinate, {mercatorXfromLng, mercatorYfromLat, mercatorZfromAltitude, latFromMercatorY, MAX_MERCATOR_LATITUDE, circumferenceAtLatitude} from './mercator_coordinate.js';
import MercatorCoordinate, {mercatorXfromLng, mercatorYfromLat, mercatorZfromAltitude, lngFromMercatorX, latFromMercatorY, MAX_MERCATOR_LATITUDE, circumferenceAtLatitude} from './mercator_coordinate.js';
import {getProjection} from './projection/index.js';
import {tileAABB} from '../geo/projection/tile_transform.js';
import Point from '@mapbox/point-geometry';
Expand Down Expand Up @@ -1344,7 +1344,60 @@ class Transform {
new Point(Number.MAX_VALUE, Number.MAX_VALUE);
}

_getGlobeBounds(): LngLatBounds {
const topLeft = new Point(this._edgeInsets.left, this._edgeInsets.top);
const topRight = new Point(this.width - this._edgeInsets.right, this._edgeInsets.top);
const bottomRight = new Point(this.width - this._edgeInsets.right, this.height - this._edgeInsets.bottom);
const bottomLeft = new Point(this._edgeInsets.left, this.height - this._edgeInsets.bottom);
stepankuzmin marked this conversation as resolved.
Show resolved Hide resolved

const tl = this.pointCoordinate3D(topLeft);
const tr = this.pointCoordinate3D(topRight);
const br = this.pointCoordinate3D(bottomRight);
const bl = this.pointCoordinate3D(bottomLeft);

let minX = Math.min(tl.x, bl.x);
let maxX = Math.max(tr.x, br.x);
let minY = Math.min(tl.y, tr.y);
let maxY = Math.max(bl.y, br.y);

// we pick an error threshold for calculating the bbox that balances between performance and precision
const s = Math.pow(2, -this.zoom);
const maxErr = s / 16;

const processSegment = (ax, ay, bx, by) => {
const mx = (ax + bx) / 2;
const my = (ay + by) / 2;

const p = new Point(mx, my);
const pm = this.pointCoordinate3D(p);
const err = Math.max(0, minX - pm.x, minY - pm.y, pm.x - maxX, pm.y - maxY);
mourner marked this conversation as resolved.
Show resolved Hide resolved

minX = Math.min(minX, pm.x);
maxX = Math.max(maxX, pm.x);
minY = Math.min(minY, pm.y);
maxY = Math.max(maxY, pm.y);

if (err > maxErr) {
processSegment(ax, ay, mx, my);
processSegment(mx, my, bx, by);
}
};

processSegment(topLeft.x, topLeft.y, topRight.x, topRight.y);
processSegment(topRight.x, topRight.y, bottomRight.x, bottomRight.y);
processSegment(bottomRight.x, bottomRight.y, bottomLeft.x, bottomLeft.y);
processSegment(bottomLeft.x, bottomLeft.y, topLeft.x, topLeft.y);

const sw = new LngLat(lngFromMercatorX(minX), latFromMercatorY(minY));
const ne = new LngLat(lngFromMercatorX(maxX), latFromMercatorY(maxY));
return new LngLatBounds(sw, ne);
}

_getBounds(min: number, max: number): LngLatBounds {
if (this.projection.name === 'globe') {
return this._getGlobeBounds();
}

const topLeft = new Point(this._edgeInsets.left, this._edgeInsets.top);
const topRight = new Point(this.width - this._edgeInsets.right, this._edgeInsets.top);
const bottomRight = new Point(this.width - this._edgeInsets.right, this.height - this._edgeInsets.bottom);
Expand Down
21 changes: 21 additions & 0 deletions test/unit/ui/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,27 @@ test('Map', (t) => {
t.end();
});

t.test('globe bounds', (t) => {
const map = createMap(t, {zoom: 0, skipCSSStub: true});
const mercatorBounds = map.getBounds();

t.same(
toFixed(mercatorBounds.toArray()),
toFixed([[ -70.3125000000, -57.3265212252, ], [ 70.3125000000, 57.3265212252]])
);

map.setProjection('globe');
t.stub(console, 'warn');
const globeBounds = map.getBounds();

t.same(
toFixed(globeBounds.toArray()),
toFixed([[ -73.8873304141, 73.8873304141, ], [ 73.8873304141, -73.8873304141]])
);

t.end();
});
stepankuzmin marked this conversation as resolved.
Show resolved Hide resolved

t.end();

function toFixed(bounds) {
Expand Down