From bdf4e1549ced444bf6cf3f41efeefc3ed2ee2b2b Mon Sep 17 00:00:00 2001 From: Karim Naaji Date: Wed, 24 Aug 2022 15:06:16 -0700 Subject: [PATCH] Flow and lint --- debug/camera-for-bounds.html | 10 ++++--- flow-typed/gl-matrix.js | 3 +- src/geo/projection/globe_util.js | 2 +- src/geo/projection/projection.js | 2 +- src/geo/transform.js | 7 ++--- src/ui/camera.js | 47 ++++++++++++-------------------- src/util/primitives.js | 4 +-- src/util/util.js | 17 +----------- 8 files changed, 34 insertions(+), 58 deletions(-) diff --git a/debug/camera-for-bounds.html b/debug/camera-for-bounds.html index dd19f3219b6..329eee8cfd3 100644 --- a/debug/camera-for-bounds.html +++ b/debug/camera-for-bounds.html @@ -53,6 +53,8 @@ projection: 'globe' }); +/*global MapboxDraw, turf*/ + const draw = new MapboxDraw({ displayControlsDefault: false, controls: { @@ -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); @@ -74,7 +76,7 @@ "type": "Polygon", "coordinates": [] } -} +}; let bbox; @@ -96,7 +98,7 @@ 'type': 'line', 'source': { 'type': 'geojson', - 'data': data + data }, 'layout': {}, 'paint': { @@ -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); }); }); diff --git a/flow-typed/gl-matrix.js b/flow-typed/gl-matrix.js index 8029c1d4598..a806271d64c 100644 --- a/flow-typed/gl-matrix.js +++ b/flow-typed/gl-matrix.js @@ -52,7 +52,8 @@ declare module "gl-matrix" { declare var vec4: { scale(T, Vec4, number): T, mul(T, Vec4, Vec4): T, - transformMat4(T, Vec4, Mat4): T + transformMat4(T, Vec4, Mat4): T, + normalize(T, Vec4): T }; declare var mat2: { diff --git a/src/geo/projection/globe_util.js b/src/geo/projection/globe_util.js index 9901f48a03b..c82633cac4c 100644 --- a/src/geo/projection/globe_util.js +++ b/src/geo/projection/globe_util.js @@ -425,7 +425,7 @@ function csLatLngToECEF(cosLat: number, sinLat: number, lng: number, radius: num return [sx, sy, sz]; } -export function ECEFToLatLng(ecef: Array): LngLat { +export function ecefToLatLng(ecef: Array): 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]); diff --git a/src/geo/projection/projection.js b/src/geo/projection/projection.js index 1d7e433f189..d5bf4f8a54e 100644 --- a/src/geo/projection/projection.js +++ b/src/geo/projection/projection.js @@ -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; } diff --git a/src/geo/transform.js b/src/geo/transform.js index 6f5345851c1..a31e5594aba 100644 --- a/src/geo/transform.js +++ b/src/geo/transform.js @@ -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; @@ -375,7 +375,7 @@ class Transform { this._calcMatrices(); } - get aspect(): mumber { + get aspect(): number { return this.width / this.height; } @@ -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); diff --git a/src/ui/camera.js b/src/ui/camera.js index 6a2e2b48974..986ff65f276 100644 --- a/src/ui/camera.js +++ b/src/ui/camera.js @@ -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'; @@ -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'; @@ -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'; @@ -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); @@ -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; @@ -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); diff --git a/src/util/primitives.js b/src/util/primitives.js index b1842cb717e..57d646e1f5f 100644 --- a/src/util/primitives.js +++ b/src/util/primitives.js @@ -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; @@ -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) { diff --git a/src/util/util.js b/src/util/util.js index 6a8b6dbf671..6cc7daaf966 100644 --- a/src/util/util.js +++ b/src/util/util.js @@ -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; @@ -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. *