@math.gl/web-mercator 3.5.6 → 3.6.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assert.d.ts +2 -0
- package/dist/assert.d.ts.map +1 -0
- package/dist/assert.js +8 -0
- package/dist/es5/assert.js.map +1 -1
- package/dist/es5/fit-bounds.js +33 -37
- package/dist/es5/fit-bounds.js.map +1 -1
- package/dist/es5/fly-to-viewport.js +23 -16
- package/dist/es5/fly-to-viewport.js.map +1 -1
- package/dist/es5/get-bounds.js +1 -1
- package/dist/es5/get-bounds.js.map +1 -1
- package/dist/es5/index.js +6 -0
- package/dist/es5/index.js.map +1 -1
- package/dist/es5/math-utils.js +8 -9
- package/dist/es5/math-utils.js.map +1 -1
- package/dist/es5/normalize-viewport-props.js +10 -10
- package/dist/es5/normalize-viewport-props.js.map +1 -1
- package/dist/es5/web-mercator-utils.js +47 -61
- package/dist/es5/web-mercator-utils.js.map +1 -1
- package/dist/es5/web-mercator-viewport.js +69 -51
- package/dist/es5/web-mercator-viewport.js.map +1 -1
- package/dist/esm/assert.js.map +1 -1
- package/dist/esm/fit-bounds.js +29 -34
- package/dist/esm/fit-bounds.js.map +1 -1
- package/dist/esm/fly-to-viewport.js +13 -9
- package/dist/esm/fly-to-viewport.js.map +1 -1
- package/dist/esm/get-bounds.js +1 -1
- package/dist/esm/get-bounds.js.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/math-utils.js +6 -3
- package/dist/esm/math-utils.js.map +1 -1
- package/dist/esm/normalize-viewport-props.js +13 -9
- package/dist/esm/normalize-viewport-props.js.map +1 -1
- package/dist/esm/web-mercator-utils.js +47 -52
- package/dist/esm/web-mercator-utils.js.map +1 -1
- package/dist/esm/web-mercator-viewport.js +76 -34
- package/dist/esm/web-mercator-viewport.js.map +1 -1
- package/dist/fit-bounds.d.ts +50 -0
- package/dist/fit-bounds.d.ts.map +1 -0
- package/dist/fit-bounds.js +63 -0
- package/dist/fly-to-viewport.d.ts +19 -0
- package/dist/fly-to-viewport.d.ts.map +1 -0
- package/dist/fly-to-viewport.js +87 -0
- package/dist/get-bounds.d.ts +3 -0
- package/dist/get-bounds.d.ts.map +1 -0
- package/dist/get-bounds.js +51 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/math-utils.d.ts +7 -0
- package/dist/math-utils.d.ts.map +1 -0
- package/dist/math-utils.js +26 -0
- package/dist/normalize-viewport-props.d.ts +16 -0
- package/dist/normalize-viewport-props.d.ts.map +1 -0
- package/dist/normalize-viewport-props.js +41 -0
- package/dist/{es5/web-mercator-utils.d.ts → web-mercator-utils.d.ts} +78 -94
- package/dist/web-mercator-utils.d.ts.map +1 -0
- package/dist/web-mercator-utils.js +290 -0
- package/dist/web-mercator-viewport.d.ts +182 -0
- package/dist/web-mercator-viewport.d.ts.map +1 -0
- package/dist/web-mercator-viewport.js +281 -0
- package/package.json +4 -4
- package/src/{assert.js → assert.ts} +1 -1
- package/src/fit-bounds.ts +127 -0
- package/src/{fly-to-viewport.js → fly-to-viewport.ts} +52 -14
- package/src/{get-bounds.js → get-bounds.ts} +7 -5
- package/src/{index.js → index.ts} +5 -0
- package/src/math-utils.ts +32 -0
- package/src/{normalize-viewport-props.js → normalize-viewport-props.ts} +19 -10
- package/src/{web-mercator-utils.js → web-mercator-utils.ts} +186 -76
- package/src/web-mercator-viewport.ts +415 -0
- package/dist/es5/fit-bounds.d.ts +0 -64
- package/dist/es5/fly-to-viewport.d.ts +0 -31
- package/dist/es5/get-bounds.d.ts +0 -3
- package/dist/es5/index.d.ts +0 -23
- package/dist/es5/normalize-viewport-props.d.ts +0 -16
- package/dist/es5/web-mercator-viewport.d.ts +0 -187
- package/dist/esm/fit-bounds.d.ts +0 -64
- package/dist/esm/fly-to-viewport.d.ts +0 -31
- package/dist/esm/get-bounds.d.ts +0 -3
- package/dist/esm/index.d.ts +0 -23
- package/dist/esm/normalize-viewport-props.d.ts +0 -16
- package/dist/esm/web-mercator-utils.d.ts +0 -189
- package/dist/esm/web-mercator-viewport.d.ts +0 -187
- package/src/fit-bounds.d.ts +0 -64
- package/src/fit-bounds.js +0 -84
- package/src/fly-to-viewport.d.ts +0 -31
- package/src/get-bounds.d.ts +0 -3
- package/src/index.d.ts +0 -23
- package/src/math-utils.js +0 -28
- package/src/normalize-viewport-props.d.ts +0 -16
- package/src/web-mercator-utils.d.ts +0 -189
- package/src/web-mercator-viewport.d.ts +0 -187
- package/src/web-mercator-viewport.js +0 -262
|
@@ -4,17 +4,26 @@ import {mod, log2} from './math-utils';
|
|
|
4
4
|
// defined by mapbox-gl
|
|
5
5
|
const TILE_SIZE = 512;
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
/** Description of viewport */
|
|
8
|
+
export type ViewportProps = {
|
|
9
|
+
width: number;
|
|
10
|
+
height: number;
|
|
11
|
+
longitude: number;
|
|
12
|
+
latitude: number;
|
|
13
|
+
zoom: number;
|
|
14
|
+
pitch?: number;
|
|
15
|
+
bearing?: number;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Apply mathematical constraints to viewport props
|
|
20
|
+
* @param props
|
|
21
|
+
*/
|
|
8
22
|
// eslint-disable-next-line complexity
|
|
9
|
-
export default function normalizeViewportProps({
|
|
10
|
-
width,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
latitude,
|
|
14
|
-
zoom,
|
|
15
|
-
pitch = 0,
|
|
16
|
-
bearing = 0
|
|
17
|
-
}) {
|
|
23
|
+
export default function normalizeViewportProps(props: ViewportProps): ViewportProps {
|
|
24
|
+
const {width, height, pitch = 0} = props;
|
|
25
|
+
let {longitude, latitude, zoom, bearing = 0} = props;
|
|
26
|
+
|
|
18
27
|
// Normalize degrees
|
|
19
28
|
if (longitude < -180 || longitude > 180) {
|
|
20
29
|
longitude = mod(longitude + 180, 360) - 180;
|
|
@@ -15,16 +15,46 @@ const RADIANS_TO_DEGREES = 180 / PI;
|
|
|
15
15
|
const TILE_SIZE = 512;
|
|
16
16
|
// Average circumference (40075 km equatorial, 40007 km meridional)
|
|
17
17
|
const EARTH_CIRCUMFERENCE = 40.03e6;
|
|
18
|
+
// Latitude that makes a square world, 2 * atan(E ** PI) - PI / 2
|
|
19
|
+
export const MAX_LATITUDE = 85.051129;
|
|
18
20
|
|
|
19
21
|
// Mapbox default altitude
|
|
20
22
|
export const DEFAULT_ALTITUDE = 1.5;
|
|
21
23
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
export type DistanceScales = {
|
|
25
|
+
unitsPerMeter: number[];
|
|
26
|
+
metersPerUnit: number[];
|
|
27
|
+
unitsPerMeter2?: number[];
|
|
28
|
+
unitsPerDegree: number[];
|
|
29
|
+
degreesPerUnit: number[];
|
|
30
|
+
unitsPerDegree2?: number[];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* PROJECTION MATRIX PARAMETERS
|
|
35
|
+
*
|
|
36
|
+
* TODO how to document mebers
|
|
37
|
+
* @param fov in radians. fov is variable, depends on pitch and altitude
|
|
38
|
+
* @param aspect width/height
|
|
39
|
+
* @param focalDistance distance at which visual scale factor is 1
|
|
40
|
+
* @param near near clipping plane
|
|
41
|
+
* @param far far clipping plane
|
|
42
|
+
*/
|
|
43
|
+
type ProjectionParameters = {
|
|
44
|
+
fov: number;
|
|
45
|
+
aspect: number;
|
|
46
|
+
focalDistance: number;
|
|
47
|
+
near: number;
|
|
48
|
+
far: number;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** Logarithimic zoom to linear scale **/
|
|
52
|
+
export function zoomToScale(zoom: number): number {
|
|
24
53
|
return Math.pow(2, zoom);
|
|
25
54
|
}
|
|
26
55
|
|
|
27
|
-
|
|
56
|
+
/** Linear scale to logarithimic zoom **/
|
|
57
|
+
export function scaleToZoom(scale: number): number {
|
|
28
58
|
return log2(scale);
|
|
29
59
|
}
|
|
30
60
|
|
|
@@ -38,7 +68,8 @@ export function scaleToZoom(scale) {
|
|
|
38
68
|
* Specifies a point on the sphere to project onto the map.
|
|
39
69
|
* @return [x,y] coordinates.
|
|
40
70
|
*/
|
|
41
|
-
export function lngLatToWorld([
|
|
71
|
+
export function lngLatToWorld(lngLat: number[]): [number, number] {
|
|
72
|
+
const [lng, lat] = lngLat;
|
|
42
73
|
assert(Number.isFinite(lng));
|
|
43
74
|
assert(Number.isFinite(lat) && lat >= -90 && lat <= 90, 'invalid latitude');
|
|
44
75
|
|
|
@@ -49,16 +80,28 @@ export function lngLatToWorld([lng, lat]) {
|
|
|
49
80
|
return [x, y];
|
|
50
81
|
}
|
|
51
82
|
|
|
52
|
-
|
|
53
|
-
|
|
83
|
+
/**
|
|
84
|
+
* Unproject world point [x,y] on map onto {lat, lon} on sphere
|
|
85
|
+
*
|
|
86
|
+
* @param xy - array with [x,y] members
|
|
87
|
+
* representing point on projected map plane
|
|
88
|
+
* @return - array with [x,y] of point on sphere.
|
|
89
|
+
* Has toArray method if you need a GeoJSON Array.
|
|
90
|
+
* Per cartographic tradition, lat and lon are specified as degrees.
|
|
91
|
+
*/
|
|
92
|
+
export function worldToLngLat(xy: number[]): [number, number] {
|
|
93
|
+
const [x, y] = xy;
|
|
54
94
|
const lambda2 = (x / TILE_SIZE) * (2 * PI) - PI;
|
|
55
95
|
const phi2 = 2 * (Math.atan(Math.exp((y / TILE_SIZE) * (2 * PI) - PI)) - PI_4);
|
|
56
96
|
return [lambda2 * RADIANS_TO_DEGREES, phi2 * RADIANS_TO_DEGREES];
|
|
57
97
|
}
|
|
58
98
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Returns the zoom level that gives a 1 meter pixel at a certain latitude
|
|
101
|
+
* 1 = C*cos(y)/2^z/TILE_SIZE = C*cos(y)/2^(z+9)
|
|
102
|
+
*/
|
|
103
|
+
export function getMeterZoom(options: {latitude: number}): number {
|
|
104
|
+
const {latitude} = options;
|
|
62
105
|
assert(Number.isFinite(latitude));
|
|
63
106
|
const latCosine = Math.cos(latitude * DEGREES_TO_RADIANS);
|
|
64
107
|
return scaleToZoom(EARTH_CIRCUMFERENCE * latCosine) - 9;
|
|
@@ -70,11 +113,14 @@ export function getMeterZoom({latitude}) {
|
|
|
70
113
|
* In mercator projection mode, the distance scales vary significantly
|
|
71
114
|
* with latitude.
|
|
72
115
|
*/
|
|
73
|
-
|
|
74
|
-
|
|
116
|
+
export function getDistanceScales(options: {
|
|
117
|
+
latitude: number;
|
|
118
|
+
longitude: number;
|
|
119
|
+
highPrecision?: boolean;
|
|
120
|
+
}): DistanceScales {
|
|
121
|
+
const {latitude, longitude, highPrecision = false} = options;
|
|
75
122
|
assert(Number.isFinite(latitude) && Number.isFinite(longitude));
|
|
76
123
|
|
|
77
|
-
const result = {};
|
|
78
124
|
const worldSize = TILE_SIZE;
|
|
79
125
|
const latCosine = Math.cos(latitude * DEGREES_TO_RADIANS);
|
|
80
126
|
|
|
@@ -100,11 +146,13 @@ export function getDistanceScales({latitude, longitude, highPrecision = false})
|
|
|
100
146
|
*
|
|
101
147
|
* Y needs to be flipped when converting delta degree/meter to delta pixels
|
|
102
148
|
*/
|
|
103
|
-
result
|
|
104
|
-
|
|
149
|
+
const result: DistanceScales = {
|
|
150
|
+
unitsPerMeter: [altUnitsPerMeter, altUnitsPerMeter, altUnitsPerMeter],
|
|
151
|
+
metersPerUnit: [1 / altUnitsPerMeter, 1 / altUnitsPerMeter, 1 / altUnitsPerMeter],
|
|
105
152
|
|
|
106
|
-
|
|
107
|
-
|
|
153
|
+
unitsPerDegree: [unitsPerDegreeX, unitsPerDegreeY, altUnitsPerMeter],
|
|
154
|
+
degreesPerUnit: [1 / unitsPerDegreeX, 1 / unitsPerDegreeY, 1 / altUnitsPerMeter]
|
|
155
|
+
};
|
|
108
156
|
|
|
109
157
|
/**
|
|
110
158
|
* Taylor series 2nd order for 1/latCosine
|
|
@@ -129,7 +177,7 @@ export function getDistanceScales({latitude, longitude, highPrecision = false})
|
|
|
129
177
|
/**
|
|
130
178
|
* Offset a lng/lat position by meterOffset (northing, easting)
|
|
131
179
|
*/
|
|
132
|
-
export function addMetersToLngLat(lngLatZ, xyz) {
|
|
180
|
+
export function addMetersToLngLat(lngLatZ: number[], xyz: number[]): number[] {
|
|
133
181
|
const [longitude, latitude, z0] = lngLatZ;
|
|
134
182
|
const [x, y, z] = xyz;
|
|
135
183
|
|
|
@@ -143,28 +191,38 @@ export function addMetersToLngLat(lngLatZ, xyz) {
|
|
|
143
191
|
worldspace[0] += x * (unitsPerMeter[0] + unitsPerMeter2[0] * y);
|
|
144
192
|
worldspace[1] += y * (unitsPerMeter[1] + unitsPerMeter2[1] * y);
|
|
145
193
|
|
|
146
|
-
// @ts-ignore
|
|
147
194
|
const newLngLat = worldToLngLat(worldspace);
|
|
148
195
|
const newZ = (z0 || 0) + (z || 0);
|
|
149
196
|
|
|
150
197
|
return Number.isFinite(z0) || Number.isFinite(z) ? [newLngLat[0], newLngLat[1], newZ] : newLngLat;
|
|
151
198
|
}
|
|
152
199
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
200
|
+
/**
|
|
201
|
+
*
|
|
202
|
+
* view and projection matrix creation is intentionally kept compatible with
|
|
203
|
+
* mapbox-gl's implementation to ensure that seamless interoperation
|
|
204
|
+
* with mapbox and react-map-gl. See: https://github.com/mapbox/mapbox-gl-js
|
|
205
|
+
*/
|
|
206
|
+
export function getViewMatrix(options: {
|
|
159
207
|
// Viewport props
|
|
160
|
-
height
|
|
161
|
-
pitch
|
|
162
|
-
bearing
|
|
163
|
-
altitude
|
|
208
|
+
height: number;
|
|
209
|
+
pitch: number;
|
|
210
|
+
bearing: number;
|
|
211
|
+
altitude: number;
|
|
164
212
|
// Pre-calculated parameters
|
|
165
|
-
scale
|
|
166
|
-
center
|
|
167
|
-
}) {
|
|
213
|
+
scale: number;
|
|
214
|
+
center?: number[];
|
|
215
|
+
}): number[] {
|
|
216
|
+
const {
|
|
217
|
+
// Viewport props
|
|
218
|
+
height,
|
|
219
|
+
pitch,
|
|
220
|
+
bearing,
|
|
221
|
+
altitude,
|
|
222
|
+
// Pre-calculated parameters
|
|
223
|
+
scale,
|
|
224
|
+
center
|
|
225
|
+
} = options;
|
|
168
226
|
// VIEW MATRIX: PROJECTS MERCATOR WORLD COORDINATES
|
|
169
227
|
// Note that mercator world coordinates typically need to be flipped
|
|
170
228
|
//
|
|
@@ -179,8 +237,8 @@ export function getViewMatrix({
|
|
|
179
237
|
mat4.rotateX(vm, vm, -pitch * DEGREES_TO_RADIANS);
|
|
180
238
|
mat4.rotateZ(vm, vm, bearing * DEGREES_TO_RADIANS);
|
|
181
239
|
|
|
182
|
-
scale
|
|
183
|
-
mat4.scale(vm, vm, [
|
|
240
|
+
const relativeScale = scale / height;
|
|
241
|
+
mat4.scale(vm, vm, [relativeScale, relativeScale, relativeScale]);
|
|
184
242
|
|
|
185
243
|
if (center) {
|
|
186
244
|
mat4.translate(vm, vm, vec3.negate([], center));
|
|
@@ -189,17 +247,30 @@ export function getViewMatrix({
|
|
|
189
247
|
return vm;
|
|
190
248
|
}
|
|
191
249
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
250
|
+
/**
|
|
251
|
+
* Calculates mapbox compatible projection matrix from parameters
|
|
252
|
+
*
|
|
253
|
+
* @param options.width Width of "viewport" or window
|
|
254
|
+
* @param options.height Height of "viewport" or window
|
|
255
|
+
* @param options.pitch Camera angle in degrees (0 is straight down)
|
|
256
|
+
* @param options.fovy field of view in degrees
|
|
257
|
+
* @param options.altitude if provided, field of view is calculated using `altitudeToFovy()`
|
|
258
|
+
* @param options.nearZMultiplier control z buffer
|
|
259
|
+
* @param options.farZMultiplier control z buffer
|
|
260
|
+
* @returns project parameters object
|
|
261
|
+
*/
|
|
262
|
+
export function getProjectionParameters(options: {
|
|
263
|
+
width: number;
|
|
264
|
+
height: number;
|
|
265
|
+
fovy?: number;
|
|
266
|
+
altitude?: number;
|
|
267
|
+
pitch?: number;
|
|
268
|
+
nearZMultiplier?: number;
|
|
269
|
+
farZMultiplier?: number;
|
|
270
|
+
}): ProjectionParameters {
|
|
271
|
+
const {width, height, altitude, pitch = 0, nearZMultiplier = 1, farZMultiplier = 1} = options;
|
|
272
|
+
let {fovy = altitudeToFovy(DEFAULT_ALTITUDE)} = options;
|
|
273
|
+
|
|
203
274
|
// For back-compatibility allow field of view to be
|
|
204
275
|
// derived from altitude
|
|
205
276
|
if (altitude !== undefined) {
|
|
@@ -227,31 +298,36 @@ export function getProjectionParameters({
|
|
|
227
298
|
};
|
|
228
299
|
}
|
|
229
300
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
301
|
+
/**
|
|
302
|
+
* CALCULATE PROJECTION MATRIX: PROJECTS FROM CAMERA (VIEW) SPACE TO CLIPSPACE
|
|
303
|
+
*
|
|
304
|
+
* To match mapbox's z buffer:
|
|
305
|
+
* - \<= 0.28: nearZMultiplier: 0.1, farZmultiplier: 1
|
|
306
|
+
* - \>= 0.29: nearZMultiplier: 1 / height, farZMultiplier: 1.01
|
|
307
|
+
*
|
|
308
|
+
* @param options Viewport options
|
|
309
|
+
* @param options.width Width of "viewport" or window
|
|
310
|
+
* @param options.height Height of "viewport" or window
|
|
311
|
+
* @param options.pitch Camera angle in degrees (0 is straight down)
|
|
312
|
+
* @param options.fovy field of view in degrees
|
|
313
|
+
* @param options.altitude if provided, field of view is calculated using `altitudeToFovy()`
|
|
314
|
+
* @param options.nearZMultiplier control z buffer
|
|
315
|
+
* @param options.farZMultiplier control z buffer
|
|
316
|
+
* @returns 4x4 projection matrix
|
|
317
|
+
*/
|
|
318
|
+
export function getProjectionMatrix(options: {
|
|
319
|
+
width: number;
|
|
320
|
+
height: number;
|
|
321
|
+
pitch: number;
|
|
322
|
+
fovy?: number;
|
|
323
|
+
altitude?: number;
|
|
324
|
+
nearZMultiplier: number;
|
|
325
|
+
farZMultiplier: number;
|
|
326
|
+
}): number[] {
|
|
327
|
+
const {fov, aspect, near, far} = getProjectionParameters(options);
|
|
252
328
|
|
|
253
329
|
const projectionMatrix = mat4.perspective(
|
|
254
|
-
[],
|
|
330
|
+
[] as number[],
|
|
255
331
|
fov, // fov in radians
|
|
256
332
|
aspect, // aspect ratio
|
|
257
333
|
near, // near plane
|
|
@@ -261,27 +337,61 @@ export function getProjectionMatrix({
|
|
|
261
337
|
return projectionMatrix;
|
|
262
338
|
}
|
|
263
339
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
340
|
+
/**
|
|
341
|
+
*
|
|
342
|
+
* Convert an altitude to field of view such that the
|
|
343
|
+
* focal distance is equal to the altitude
|
|
344
|
+
*
|
|
345
|
+
* @param altitude - altitude of camera in screen units
|
|
346
|
+
* @return fovy field of view in degrees
|
|
347
|
+
*/
|
|
348
|
+
export function altitudeToFovy(altitude: number): number {
|
|
268
349
|
return 2 * Math.atan(0.5 / altitude) * RADIANS_TO_DEGREES;
|
|
269
350
|
}
|
|
270
351
|
|
|
271
|
-
|
|
352
|
+
/**
|
|
353
|
+
*
|
|
354
|
+
* Convert an field of view such that the
|
|
355
|
+
* focal distance is equal to the altitude
|
|
356
|
+
*
|
|
357
|
+
* @param fovy - field of view in degrees
|
|
358
|
+
* @return altitude altitude of camera in screen units
|
|
359
|
+
*/
|
|
360
|
+
export function fovyToAltitude(fovy: number): number {
|
|
272
361
|
return 0.5 / Math.tan(0.5 * fovy * DEGREES_TO_RADIANS);
|
|
273
362
|
}
|
|
274
363
|
|
|
364
|
+
/**
|
|
365
|
+
* Project flat coordinates to pixels on screen.
|
|
366
|
+
*
|
|
367
|
+
* @param xyz - flat coordinate on 512*512 Mercator Zoom 0 tile
|
|
368
|
+
* @param pixelProjectionMatrix - projection matrix 4x4
|
|
369
|
+
* @return [x, y, depth] pixel coordinate on screen.
|
|
370
|
+
*/
|
|
371
|
+
export function worldToPixels(xyz: number[], pixelProjectionMatrix: number[]): number[];
|
|
372
|
+
|
|
275
373
|
// Project flat coordinates to pixels on screen.
|
|
276
|
-
export function worldToPixels(xyz, pixelProjectionMatrix) {
|
|
374
|
+
export function worldToPixels(xyz: number[], pixelProjectionMatrix: number[]): number[] {
|
|
277
375
|
const [x, y, z = 0] = xyz;
|
|
278
376
|
assert(Number.isFinite(x) && Number.isFinite(y) && Number.isFinite(z));
|
|
279
377
|
|
|
280
378
|
return transformVector(pixelProjectionMatrix, [x, y, z, 1]);
|
|
281
379
|
}
|
|
282
380
|
|
|
283
|
-
|
|
284
|
-
|
|
381
|
+
/**
|
|
382
|
+
* Unproject pixels on screen to flat coordinates.
|
|
383
|
+
*
|
|
384
|
+
* @param xyz - pixel coordinate on screen.
|
|
385
|
+
* @param pixelUnprojectionMatrix - unprojection matrix 4x4
|
|
386
|
+
* @param targetZ - if pixel coordinate does not have a 3rd component (depth),
|
|
387
|
+
* targetZ is used as the elevation plane to unproject onto
|
|
388
|
+
* @return [x, y, Z] flat coordinates on 512*512 Mercator Zoom 0 tile.
|
|
389
|
+
*/
|
|
390
|
+
export function pixelsToWorld(
|
|
391
|
+
xyz: number[],
|
|
392
|
+
pixelUnprojectionMatrix: number[],
|
|
393
|
+
targetZ: number = 0
|
|
394
|
+
): number[] {
|
|
285
395
|
const [x, y, z] = xyz;
|
|
286
396
|
assert(Number.isFinite(x) && Number.isFinite(y), 'invalid pixel coordinate');
|
|
287
397
|
|
|
@@ -300,5 +410,5 @@ export function pixelsToWorld(xyz, pixelUnprojectionMatrix, targetZ = 0) {
|
|
|
300
410
|
const z1 = coord1[2];
|
|
301
411
|
|
|
302
412
|
const t = z0 === z1 ? 0 : ((targetZ || 0) - z0) / (z1 - z0);
|
|
303
|
-
return vec2.lerp([], coord0, coord1, t);
|
|
413
|
+
return vec2.lerp([] as number[], coord0, coord1, t);
|
|
304
414
|
}
|