Skip to content

Commit

Permalink
Flow and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
karimnaaji committed Aug 25, 2022
1 parent bb7d736 commit bdf4e15
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 58 deletions.
10 changes: 6 additions & 4 deletions debug/camera-for-bounds.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
projection: 'globe'
});

/*global MapboxDraw, turf*/

const draw = new MapboxDraw({
displayControlsDefault: false,
controls: {
Expand All @@ -62,7 +64,7 @@
defaultMode: 'draw_polygon'
});
map.addControl(draw);

map.on('draw.create', updatePolygon);
map.on('draw.delete', updatePolygon);
map.on('draw.update', updatePolygon);
Expand All @@ -74,7 +76,7 @@
"type": "Polygon",
"coordinates": []
}
}
};

let bbox;

Expand All @@ -96,7 +98,7 @@
'type': 'line',
'source': {
'type': 'geojson',
'data': data
data
},
'layout': {},
'paint': {
Expand All @@ -112,7 +114,7 @@
[bbox[0], bbox[1]],
[bbox[2], bbox[3]]
];
const camera = map.cameraForBounds(bounds, { bearing: map.getBearing() });
const camera = map.cameraForBounds(bounds, {bearing: map.getBearing()});
map.easeTo(camera);
});
});
Expand Down
3 changes: 2 additions & 1 deletion flow-typed/gl-matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ declare module "gl-matrix" {
declare var vec4: {
scale<T: Vec4>(T, Vec4, number): T,
mul<T: Vec4>(T, Vec4, Vec4): T,
transformMat4<T: Vec4>(T, Vec4, Mat4): T
transformMat4<T: Vec4>(T, Vec4, Mat4): T,
normalize<T: Vec4>(T, Vec4): T
};

declare var mat2: {
Expand Down
2 changes: 1 addition & 1 deletion src/geo/projection/globe_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ function csLatLngToECEF(cosLat: number, sinLat: number, lng: number, radius: num
return [sx, sy, sz];
}

export function ECEFToLatLng(ecef: Array<number>): LngLat {
export function ecefToLatLng(ecef: Array<number>): LngLat {
const radius = Math.sqrt(ecef[0] * ecef[0] + ecef[1] * ecef[1] + ecef[2] * ecef[2]);

const lng = Math.atan2(ecef[0], ecef[2]);
Expand Down
2 changes: 1 addition & 1 deletion src/geo/projection/projection.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default class Projection {
// `pixelSpaceConversion` is useful for converting between pixel spaces where some logic
// expects mercator pixels, such as raycasting where the scale is expected to be in
// mercator pixels.
pixelSpaceConversion(lat: number, worldSize: number, interpolationT: number): number { // eslint-disable-line
pixelSpaceConversion(lat: number, worldSize: number, interpolationT: number, useFixedPixelSpaceConversion: boolean): number { // eslint-disable-line
return 1.0;
}

Expand Down
7 changes: 3 additions & 4 deletions src/geo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type {PaddingOptions} from './edge_insets.js';
import type Tile from '../source/tile.js';
import type {ProjectionSpecification} from '../style-spec/types.js';
import type {FeatureDistanceData} from '../style-spec/feature_filter/index.js';
import type {Vec3, Vec4, Quat} from 'gl-matrix';
import type {Mat4, Vec3, Vec4, Quat} from 'gl-matrix';

const NUM_WORLD_COPIES = 3;
const DEFAULT_MIN_ZOOM = 0;
Expand Down Expand Up @@ -375,7 +375,7 @@ class Transform {
this._calcMatrices();
}

get aspect(): mumber {
get aspect(): number {
return this.width / this.height;
}

Expand Down Expand Up @@ -2113,8 +2113,7 @@ class Transform {
return 0.5 / Math.tan(this._fov * 0.5) * this.height * projectionScaler;
}


getWorldToCameraMatrix(): mat4 {
getWorldToCameraMatrix(): Mat4 {
const zUnit = this.projection.zAxisUnit === "meters" ? this.pixelsPerMeter : 1.0;
const worldToCamera = this._camera.getWorldToCamera(this.worldSize, zUnit);

Expand Down
47 changes: 18 additions & 29 deletions src/ui/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
clamp,
wrap,
ease as defaultEasing,
pick,
slerpUnitVectors
pick
} from '../util/util.js';
import {number as interpolate} from '../style-spec/util/interpolate.js';
import browser from '../util/browser.js';
Expand All @@ -23,16 +22,15 @@ import MercatorCoordinate, {
mercatorXfromLng,
mercatorYfromLat,
latFromMercatorY,
lngFromMercatorX}
from '../geo/mercator_coordinate.js';
lngFromMercatorX
} from '../geo/mercator_coordinate.js';
import {
latLngToECEF,
ECEFToLatLng,
ecefToLatLng,
GLOBE_RADIUS,
GLOBE_ZOOM_THRESHOLD_MAX,
GLOBE_ZOOM_THRESHOLD_MIN
}
from '../geo/projection/globe_util.js';
} from '../geo/projection/globe_util.js';
import {vec3, vec4, mat4} from 'gl-matrix';
import type {FreeCameraOptions} from './free_camera.js';
import type Transform from '../geo/transform.js';
Expand All @@ -41,7 +39,7 @@ import type {LngLatBoundsLike} from '../geo/lng_lat_bounds.js';
import type {TaskID} from '../util/task_queue.js';
import type {Callback} from '../types/callback.js';
import type {PointLike} from '@mapbox/point-geometry';
import {Aabb, Ray, Frustum} from '../util/primitives.js';
import {Aabb, Frustum} from '../util/primitives.js';
import type {PaddingOptions} from '../geo/edge_insets.js';
import type {Vec3} from 'gl-matrix';

Expand Down Expand Up @@ -647,25 +645,11 @@ class Camera extends Evented {

const coord0 = LngLat.convert(p0);
const coord1 = LngLat.convert(p1);
const origin = latLngToECEF(
(coord0.lat + coord1.lat) * 0.5,
(coord0.lng + coord1.lng) * 0.5);

const ecefCoords = [
latLngToECEF(coord0.lat, coord0.lng),
latLngToECEF(coord1.lat, coord0.lng),
latLngToECEF(coord1.lat, coord1.lng),
latLngToECEF(coord0.lat, coord1.lng),
];

const v0 = vec3.normalize([], ecefCoords[0]);
const v1 = vec3.normalize([], ecefCoords[1]);
const v2 = vec3.normalize([], ecefCoords[2]);
const v3 = vec3.normalize([], ecefCoords[3]);

const center = slerpUnitVectors(
slerpUnitVectors(v0, v3, 0.5),
slerpUnitVectors(v1, v2, 0.5),
0.5);

const zAxis = vec3.normalize([], center);
const zAxis = vec3.normalize([], origin);
const xAxis = vec3.normalize([], vec3.cross([], zAxis, [0, 1, 0]));
const yAxis = vec3.cross([], xAxis, zAxis);

Expand Down Expand Up @@ -703,13 +687,17 @@ class Camera extends Evented {
}

const aabb = new Aabb(min, max);
tr.center = ECEFToLatLng(center);

const center = vec3.transformMat4([], aabb.center, aabbOrientation);

vec3.normalize(center, center);
vec3.scale(center, center, GLOBE_RADIUS);
tr.center = ecefToLatLng(center);

const worldToCamera = tr.getWorldToCameraMatrix();
const cameraToWorld = mat4.invert(new Float64Array(16), worldToCamera);
aabb.applyTransform(mat4.multiply([], worldToCamera, aabbOrientation));

vec3.scale(center, center, GLOBE_RADIUS);
vec3.transformMat4(center, center, worldToCamera);

const aabbHalfExtentZ = (aabb.max[2] - aabb.min[2]) * 0.5;
Expand All @@ -719,7 +707,8 @@ class Camera extends Evented {
const aabbClosestPoint = vec3.add(offsetZ, center, offsetZ);
const offsetDistance = frustumDistance + (tr.pitch === 0 ? 0 : vec3.distance(center, aabbClosestPoint));

const normal = vec3.sub([], center, tr.globeCenterInViewSpace);
const globeCenter = tr.globeCenterInViewSpace;
const normal = vec3.sub([], center, [globeCenter[0], globeCenter[1], globeCenter[2]]);
vec3.normalize(normal, normal);
vec3.scale(normal, normal, offsetDistance);

Expand Down
4 changes: 2 additions & 2 deletions src/util/primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import {vec3, vec4} from 'gl-matrix';
import assert from 'assert';

import type {Vec3} from 'gl-matrix';
import type {Vec3, Mat4} from 'gl-matrix';

class Ray {
pos: Vec3;
Expand Down Expand Up @@ -208,7 +208,7 @@ class Aabb {
return new Aabb(qMin, qMax);
}

applyTransform(transform: mat4): Aabb {
applyTransform(transform: Mat4): Aabb {
const corners = this.getCorners();

for (let i = 0; i < corners.length; ++i) {
Expand Down
17 changes: 1 addition & 16 deletions src/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import window from './window.js';
import assert from 'assert';

import type {Callback} from '../types/callback.js';
import {Mat4, Vec3, Vec4} from 'gl-matrix';
import {vec3} from 'gl-matrix';
import type {Mat4, Vec4} from 'gl-matrix';

const DEG_TO_RAD = Math.PI / 180;
const RAD_TO_DEG = 180 / Math.PI;
Expand Down Expand Up @@ -513,20 +512,6 @@ export function mapValue(value: number, min: number, max: number, outMin: number
return clamp((value - min) / (max - min) * (outMax - outMin) + outMin, outMin, outMax);
}

export function slerpUnitVectors(v0: Vec3, v1: Vec3, t: number): Vec3 {
const theta = Math.acos(vec3.dot(v0, v1));
const d = Math.sin(theta);

const t0 = Math.sin((1.0 - t) * theta);
const t1 = Math.sin(t * theta);

vec3.scale(v0, v0, t0);
vec3.scale(v1, v1, t1);

const v = vec3.add([], v0, v1);
return vec3.scale(v, v, 1.0 / d);
}

/**
* Check if two arrays have at least one common element.
*
Expand Down

0 comments on commit bdf4e15

Please sign in to comment.