Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/modules/types/api-reference/array-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ TypeScript types to simplify working with a mix of typed arrays and standard Jav

Type matching any non-big JavaScript typed array.

When the environment's type libraries define `Float16Array`, it is included conditionally as well.

### `TypedArrayConstructor`

Type matching constructor for any non-big JavaScript typed array.

When the environment's type libraries define `Float16Array`, its constructor is included conditionally as well.

### `BigTypedArray`

Type matching any big JavaScript typed array.
Expand All @@ -34,6 +38,8 @@ JavaScript number arrays of specific lengths.

Type matching any classic JavaScript array containing numbers or any non-big typed array.

This conditionally includes `Float16Array` when the environment provides that type.

### `NumericArray2-NumericArray16`

Types matching number arrays of specific lengths or typed arrays.
Expand Down
10 changes: 9 additions & 1 deletion modules/core/src/lib/gl-matrix.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors

type NumericArray =
/**
* Base union for numeric arrays that are always available in the current lib set.
*/
type NumericArrayBase =
| Int8Array
| Uint8Array
| Int16Array
Expand All @@ -14,6 +17,11 @@ type NumericArray =
| Float64Array
| number[];

// Conditionally include Float16Array without hard-referencing the global symbol.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever

type NumericArray = typeof globalThis extends {Float16Array: {prototype: infer T}}
? NumericArrayBase | T
: NumericArrayBase;

/*
declare module 'gl-matrix/vec2' {
function length(a: Readonly<NumericArray>): number;
Expand Down
2 changes: 1 addition & 1 deletion modules/geospatial/src/lng-lat-rectangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class LngLatRectangle {
return result;
}

get width() {
get width(): number {
let east = this.east;
const west = this.west;

Expand Down
2 changes: 1 addition & 1 deletion modules/proj4/src/lib/proj4-projection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import proj4 from 'proj4';
export class Proj4Projection {
/** Define aliases for one or more projections */
static defineProjectionAliases(aliases: {[name: string]: string}): void {
const aliasArray = [];
const aliasArray: string[][] = [];
for (const alias in aliases) {
aliasArray.push([alias, aliases[alias]]);
}
Expand Down
25 changes: 21 additions & 4 deletions modules/types/src/array-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// Copyright (c) vis.gl contributors

/**
* Type covering all non-big typed arrays
* Base union for non-big JavaScript typed arrays that are always available in the current lib set.
*/
export type TypedArray =
type TypedArrayBase =
| Int8Array
| Uint8Array
| Uint8ClampedArray
Expand All @@ -17,9 +17,10 @@ export type TypedArray =
| Float64Array;

/**
* Type covering constructors for all non-big typed arrays
* Base union for constructors of non-big JavaScript typed arrays that are always available in the
* current lib set.
*/
export type TypedArrayConstructor =
type TypedArrayConstructorBase =
| Int8ArrayConstructor
| Uint8ArrayConstructor
| Uint8ClampedArrayConstructor
Expand All @@ -30,6 +31,22 @@ export type TypedArrayConstructor =
| Float32ArrayConstructor
| Float64ArrayConstructor;

/**
* Type covering all non-big typed arrays
*/
// Conditionally include Float16Array without hard-referencing the global symbol.
export type TypedArray = typeof globalThis extends {Float16Array: {prototype: infer T}}
? TypedArrayBase | T
: TypedArrayBase;

/**
* Type covering constructors for all non-big typed arrays
*/
// Conditionally include the Float16Array constructor without hard-referencing it.
export type TypedArrayConstructor = typeof globalThis extends {Float16Array: infer T}
? TypedArrayConstructorBase | T
: TypedArrayConstructorBase;

/**
* Type covering all big typed arrays
*/
Expand Down
21 changes: 21 additions & 0 deletions modules/types/test/float16-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// math.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors

import type {NumericArray, TypedArray, TypedArrayConstructor} from '@math.gl/types';

type GlobalFloat16ArrayConstructor = typeof globalThis extends {Float16Array: infer T} ? T : never;
type GlobalFloat16Array = typeof globalThis extends {Float16Array: {prototype: infer T}}
? T
: never;

declare const float16Array: GlobalFloat16Array;
declare const float16ArrayConstructor: GlobalFloat16ArrayConstructor;

const typedArray: TypedArray = float16Array;
const numericArray: NumericArray = float16Array;
const typedArrayConstructor: TypedArrayConstructor = float16ArrayConstructor;

void typedArray;
void numericArray;
void typedArrayConstructor;
7 changes: 7 additions & 0 deletions modules/types/test/is-array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ const TEST_CASES: {value: unknown; isTypedArray: boolean; isNumericArray: boolea
{value: '', isTypedArray: false, isNumericArray: false}
];

const Float16ArrayCtor = (globalThis as {Float16Array?: new (length: number) => ArrayBufferView})
.Float16Array;

if (Float16ArrayCtor) {
TEST_CASES.unshift({value: new Float16ArrayCtor(1), isTypedArray: true, isNumericArray: true});
}

test('math.gl#isTypedArray', (t) => {
for (const tc of TEST_CASES) {
t.equal(
Expand Down
30 changes: 19 additions & 11 deletions modules/web-mercator/src/fly-to-viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export type FlytoTransitionOptions = {
maxDuration?: number;
};

type TransitionViewport = {
longitude: number;
latitude: number;
zoom: number;
};

/**
* mapbox-gl-js flyTo : https://www.mapbox.com/mapbox-gl-js/api/#map#flyto.
* It implements “Smooth and efficient zooming and panning.” algorithm by
Expand All @@ -44,14 +50,16 @@ export function flyToViewport(

// If change in center is too small, do linear interpolaiton.
if (u1 < EPSILON) {
const viewport = {};
const viewport: TransitionViewport = {
longitude: 0,
latitude: 0,
zoom: 0
};
for (const key of VIEWPORT_TRANSITION_PROPS) {
const startValue = startProps[key];
const endValue = endProps[key];
// @ts-ignore-error properties are populated dynamically
viewport[key] = lerp(startValue, endValue, t);
}
// @ts-expect-error properties are populated dynamically
return viewport;
}

Expand All @@ -63,7 +71,7 @@ export function flyToViewport(
const scaleIncrement = 1 / w; // Using w method for scaling.
const newZoom = startZoom + scaleToZoom(scaleIncrement);

const newCenterWorld = vec2.scale([], uDelta, u);
const newCenterWorld = vec2.scale([] as number[], uDelta, u) as [number, number];
vec2.add(newCenterWorld, newCenterWorld, startCenterXY);

const newCenter = worldToLngLat(newCenterWorld);
Expand Down Expand Up @@ -103,8 +111,8 @@ function getFlyToTransitionParams(
opts: FlytoTransitionOptions
): {
startZoom: number;
startCenterXY: number[];
uDelta: number[];
startCenterXY: [number, number];
uDelta: [number, number];
w0: number;
u1: number;
S: number;
Expand All @@ -116,15 +124,15 @@ function getFlyToTransitionParams(
opts = Object.assign({}, DEFAULT_OPTS, opts);
const rho = opts.curve;
const startZoom = startProps.zoom;
const startCenter = [startProps.longitude, startProps.latitude];
const startCenter: [number, number] = [startProps.longitude, startProps.latitude];
const startScale = zoomToScale(startZoom);
const endZoom = endProps.zoom;
const endCenter = [endProps.longitude, endProps.latitude];
const endCenter: [number, number] = [endProps.longitude, endProps.latitude];
const scale = zoomToScale(endZoom - startZoom);

const startCenterXY = lngLatToWorld(startCenter);
const endCenterXY = lngLatToWorld(endCenter);
const uDelta = vec2.sub([] as number[], endCenterXY, startCenterXY);
const startCenterXY: [number, number] = lngLatToWorld(startCenter);
const endCenterXY: [number, number] = lngLatToWorld(endCenter);
const uDelta = vec2.sub([] as number[], endCenterXY, startCenterXY) as [number, number];

const w0 = Math.max(startProps.width, startProps.height);
const w1 = w0 / scale;
Expand Down
Loading