Skip to content

Commit

Permalink
restore mapbox compliance test
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoji Chen committed Oct 31, 2019
1 parent b0b24da commit b06ce65
Show file tree
Hide file tree
Showing 7 changed files with 422 additions and 97 deletions.
4 changes: 4 additions & 0 deletions modules/viewport-mercator-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@
"@babel/runtime": "^7.0.0",
"gl-matrix": "^3.0.0",
"math.gl": "3.0.0-beta.2"
},
"devDependencies": {
"mapbox-gl": "^1.0.0",
"mock-browser": "^0.92.14"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export default class WebMercatorViewport extends Viewport {
pitch = 0,
bearing = 0,
altitude = 1.5,
nearZMultiplier,
farZMultiplier
nearZMultiplier = 0.02,
farZMultiplier = 1.01
} = {}) {
// Silently allow apps to send in 0,0 to facilitate isomorphic render etc
width = width || 1;
Expand All @@ -76,8 +76,8 @@ export default class WebMercatorViewport extends Viewport {
pitch,
bearing,
altitude,
nearZMultiplier: nearZMultiplier || 1 / height,
farZMultiplier: farZMultiplier || 1.01
nearZMultiplier,
farZMultiplier
});

const viewMatrix = getViewMatrix({
Expand Down
2 changes: 1 addition & 1 deletion modules/viewport-mercator-project/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ require('./spec/fly-to-viewport.spec');
require('./fp32-limits');

// Test vs. mapbox Transform
// require('./spec/versus-mapbox/versus-mapbox.spec');
require('./spec/versus-mapbox/versus-mapbox.spec');
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// NOTE: Transform is not a public API so we should be careful to always lock
// down mapbox-gl to a specific major, minor, and patch version.
import {Map, LngLat, Point} from './mapbox';
import * as vec4 from 'gl-matrix/vec4';

let Transform;
/* eslint-disable */
Expand Down Expand Up @@ -93,10 +94,14 @@ export class MapboxTransform extends Transform {
mapboxProject(lngLatZ) {
const [lng, lat] = lngLatZ;
const lngLat = new LngLat(lng, lat);
const point = this.locationPoint(lngLat);
const {x, y} = point;
// console.log('Project', [x, y], [lng, lat]);
return lngLatZ.length === 3 ? [x, y, 0] : [x, y];
const coord = this.locationCoordinate(lngLat);

const p = [coord.x * this.worldSize, coord.y * this.worldSize, 0, 1];
vec4.transformMat4(p, p, this.pixelMatrix);
const x = p[0] / p[3];
const y = p[1] / p[3];
const z = p[2] / p[3];
return lngLatZ.length === 3 ? [x, y, z] : [x, y];
}

// Uses map to unproject a coordinate
Expand All @@ -105,18 +110,7 @@ export class MapboxTransform extends Transform {
const point = new Point(x, y);
const latLng = this.pointLocation(point);
const {lng, lat} = latLng;
// console.log('Unproject', [x, y], [lng, lat]);
return xyz.length === 3 ? [lng, lat, 0] : [lng, lat];
}

// Uses map to project a coordinate
mapboxProjectFlat([lng, lat]) {
return [this.lngX(lng), this.latY(lat)];
}

// Uses map to unproject a coordinate
mapboxUnprojectFlat([x, y]) {
return [this.xLng(x), this.yLat(y)];
return [lng, lat];
}

// Uses map to get position for panning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {MapboxTransform} from './mapbox-transform';
import {WebMercatorViewport} from 'viewport-mercator-project';
import test from 'tape-catch';
import {toLowPrecision} from '../../utils/test-utils';
import {equals, config, Matrix4} from 'math.gl';
import {equals, config} from 'math.gl';

import VIEWPORT_PROPS from '../../utils/sample-viewports';

Expand All @@ -20,44 +20,6 @@ const TEST_CASES = [
}
];

test('Viewport vs. Mapbox projectFlat', t => {
for (const viewportName in VIEWPORT_PROPS) {
const viewportProps = VIEWPORT_PROPS[viewportName];

const viewport = new WebMercatorViewport(viewportProps);
const projection = viewport.projectFlat([-122.43, 37.75]);

const transform = new MapboxTransform(viewportProps);
const mapboxProjection = transform.mapboxProjectFlat([-122.43, 37.75]);

t.deepEquals(
toLowPrecision(projection),
toLowPrecision(mapboxProjection),
`projectFlat(${viewportName}) - viewport/mapbox match`
);
}
t.end();
});

test('Viewport vs. Mapbox unprojectFlat', t => {
for (const viewportName in VIEWPORT_PROPS) {
const viewportProps = VIEWPORT_PROPS[viewportName];

const viewport = new WebMercatorViewport(viewportProps);
const unprojection = viewport.unprojectFlat([587, 107]);

const transform = new MapboxTransform(viewportProps);
const mapboxUnprojection = transform.mapboxUnprojectFlat([587, 107]);

t.deepEquals(
toLowPrecision(unprojection),
toLowPrecision(mapboxUnprojection),
`unprojectFlat(${viewportName}) - viewport/mapbox match`
);
}
t.end();
});

test('Viewport vs Mapbox project', t => {
config.EPSILON = 1e-8;

Expand All @@ -81,7 +43,7 @@ test('Viewport vs Mapbox project', t => {
});

test('Viewport vs Mapbox unproject', t => {
config.EPSILON = 1e-8;
config.EPSILON = 1e-7;

for (const viewportName in VIEWPORT_PROPS) {
const viewportProps = VIEWPORT_PROPS[viewportName];
Expand All @@ -105,25 +67,14 @@ test('Viewport vs Mapbox unproject', t => {
/* Mapbox's matrixes projects to screenspace instead of clipspace */
test('Viewport vs Mapbox project 3D', t => {
for (const viewportName in VIEWPORT_PROPS) {
const viewportProps = Object.assign({}, VIEWPORT_PROPS[viewportName]);
viewportProps.nearZMultiplier = 1 / viewportProps.height;
viewportProps.farZMultiplier = 1;
const viewportProps = VIEWPORT_PROPS[viewportName];

const viewport = new WebMercatorViewport(viewportProps);
const transform = new MapboxTransform(viewportProps);

const viewportProjMatrix = new Matrix4(viewport.viewProjectionMatrix)
// our projection matrix does not convert z from meter to pixels
.scale([1, 1, viewport.pixelsPerMeter]);
const mapboxProjMatrix = new Matrix4(Array.from(transform.projMatrix));

const sphericalPosition = [viewport.longitude - 0.05, viewport.latitude - 0.05, 100];
const worldPosition = viewport.projectFlat(sphericalPosition).concat([sphericalPosition[2], 1]);
const viewportProjected = viewportProjMatrix.transformVector(worldPosition);
const mapboxProjected = mapboxProjMatrix.transformVector(worldPosition);

viewportProjected.scale(1 / viewportProjected[3]);
mapboxProjected.scale(1 / mapboxProjected[3]);
const sphericalPosition = [viewport.longitude - 0.05, viewport.latitude - 0.05, 0];
const viewportProjected = viewport.project(sphericalPosition);
const mapboxProjected = transform.mapboxProject(sphericalPosition);

// TODO - math.gl does not deal with significant digits
t.deepEquals(
Expand Down
16 changes: 4 additions & 12 deletions modules/viewport-mercator-project/test/utils/sample-viewports.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ export default {
zoom: 11.5,
bearing: 0,
width: 800,
height: 600,
nearZMultiplier: 1,
farZMultiplier: 1
height: 600
},
pitched: {
latitude: 37.75,
Expand All @@ -16,9 +14,7 @@ export default {
pitch: 30,
bearing: 0,
width: 800,
height: 600,
nearZMultiplier: 1,
farZMultiplier: 1
height: 600
},
rotated: {
latitude: 37.7749,
Expand All @@ -28,9 +24,7 @@ export default {
bearing: 180,
pitch: 60,
width: 1267,
height: 400,
nearZMultiplier: 1,
farZMultiplier: 1
height: 400
},
highLatitude: {
latitude: 75.751537,
Expand All @@ -40,8 +34,6 @@ export default {
bearing: -40,
pitch: 20,
width: 500,
height: 500,
nearZMultiplier: 1,
farZMultiplier: 1
height: 500
}
};
Loading

0 comments on commit b06ce65

Please sign in to comment.