@math.gl/geometry-utils 4.2.0-alpha.5
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/LICENSE +140 -0
- package/README.md +8 -0
- package/dist/attribute-compression.d.ts +50 -0
- package/dist/attribute-compression.d.ts.map +1 -0
- package/dist/attribute-compression.js +184 -0
- package/dist/attribute-compression.js.map +1 -0
- package/dist/attribute-iterator.d.ts +8 -0
- package/dist/attribute-iterator.d.ts.map +1 -0
- package/dist/attribute-iterator.js +25 -0
- package/dist/attribute-iterator.js.map +1 -0
- package/dist/compute-vertex-normals.d.ts +4 -0
- package/dist/compute-vertex-normals.d.ts.map +1 -0
- package/dist/compute-vertex-normals.js +54 -0
- package/dist/compute-vertex-normals.js.map +1 -0
- package/dist/constants.d.ts +52 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +39 -0
- package/dist/constants.js.map +1 -0
- package/dist/coordinates.d.ts +3 -0
- package/dist/coordinates.d.ts.map +1 -0
- package/dist/coordinates.js +8 -0
- package/dist/coordinates.js.map +1 -0
- package/dist/geometry.d.ts +20 -0
- package/dist/geometry.d.ts.map +1 -0
- package/dist/geometry.js +31 -0
- package/dist/geometry.js.map +1 -0
- package/dist/gl-type.d.ts +17 -0
- package/dist/gl-type.d.ts.map +1 -0
- package/dist/gl-type.js +69 -0
- package/dist/gl-type.js.map +1 -0
- package/dist/index.cjs +535 -0
- package/dist/index.cjs.map +6 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/primitive-iterator.d.ts +21 -0
- package/dist/primitive-iterator.d.ts.map +1 -0
- package/dist/primitive-iterator.js +117 -0
- package/dist/primitive-iterator.js.map +1 -0
- package/dist/rgb565.d.ts +5 -0
- package/dist/rgb565.d.ts.map +1 -0
- package/dist/rgb565.js +24 -0
- package/dist/rgb565.js.map +1 -0
- package/dist/typed-array-utils.d.ts +3 -0
- package/dist/typed-array-utils.d.ts.map +1 -0
- package/dist/typed-array-utils.js +16 -0
- package/dist/typed-array-utils.js.map +1 -0
- package/package.json +40 -0
- package/src/attribute-compression.ts +232 -0
- package/src/attribute-iterator.ts +28 -0
- package/src/compute-vertex-normals.ts +63 -0
- package/src/constants.ts +42 -0
- package/src/coordinates.ts +8 -0
- package/src/geometry.ts +49 -0
- package/src/gl-type.ts +87 -0
- package/src/index.ts +38 -0
- package/src/primitive-iterator.ts +145 -0
- package/src/rgb565.ts +26 -0
- package/src/typed-array-utils.ts +16 -0
package/src/gl-type.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {TypedArray, TypedArrayConstructor} from '@math.gl/types';
|
|
6
|
+
import {GL_TYPE} from './constants';
|
|
7
|
+
|
|
8
|
+
const GL_TYPE_TO_ARRAY_TYPE: Record<number, TypedArrayConstructor> = {
|
|
9
|
+
[GL_TYPE.DOUBLE]: Float64Array,
|
|
10
|
+
[GL_TYPE.FLOAT]: Float32Array,
|
|
11
|
+
[GL_TYPE.UNSIGNED_SHORT]: Uint16Array,
|
|
12
|
+
[GL_TYPE.UNSIGNED_INT]: Uint32Array,
|
|
13
|
+
[GL_TYPE.UNSIGNED_BYTE]: Uint8Array,
|
|
14
|
+
[GL_TYPE.BYTE]: Int8Array,
|
|
15
|
+
[GL_TYPE.SHORT]: Int16Array,
|
|
16
|
+
[GL_TYPE.INT]: Int32Array,
|
|
17
|
+
[GL_TYPE.UNSIGNED_SHORT_4_4_4_4]: Uint16Array,
|
|
18
|
+
[GL_TYPE.UNSIGNED_SHORT_5_5_5_1]: Uint16Array,
|
|
19
|
+
[GL_TYPE.UNSIGNED_SHORT_5_6_5]: Uint16Array
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const NAME_TO_GL_TYPE: Record<string, number> = Object.fromEntries(
|
|
23
|
+
Object.entries(GL_TYPE).map(([name, glType]) => [name, glType])
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
const TYPE_CONVERSION_ERROR = 'Failed to convert GL type';
|
|
27
|
+
|
|
28
|
+
/** Converts between WebGL component constants and JavaScript typed arrays. */
|
|
29
|
+
export default class GLType {
|
|
30
|
+
/** Returns the WebGL component constant for a typed array or typed-array constructor. */
|
|
31
|
+
static fromTypedArray(arrayOrType: TypedArray | TypedArrayConstructor): number {
|
|
32
|
+
const arrayType = ArrayBuffer.isView(arrayOrType) ? arrayOrType.constructor : arrayOrType;
|
|
33
|
+
if (arrayType === Uint8ClampedArray) {
|
|
34
|
+
return GL_TYPE.UNSIGNED_BYTE;
|
|
35
|
+
}
|
|
36
|
+
for (const [glType, candidateArrayType] of Object.entries(GL_TYPE_TO_ARRAY_TYPE)) {
|
|
37
|
+
if (candidateArrayType === arrayType) {
|
|
38
|
+
return Number(glType);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
throw new Error(TYPE_CONVERSION_ERROR);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Returns a WebGL component constant from its uppercase name. */
|
|
45
|
+
static fromName(name: string): number {
|
|
46
|
+
const glType = NAME_TO_GL_TYPE[name];
|
|
47
|
+
if (glType === undefined) {
|
|
48
|
+
throw new Error(TYPE_CONVERSION_ERROR);
|
|
49
|
+
}
|
|
50
|
+
return glType;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Returns the typed-array constructor for a WebGL component constant. */
|
|
54
|
+
static getArrayType(glType: number): TypedArrayConstructor {
|
|
55
|
+
const ArrayType = GL_TYPE_TO_ARRAY_TYPE[glType];
|
|
56
|
+
if (!ArrayType) {
|
|
57
|
+
throw new Error(TYPE_CONVERSION_ERROR);
|
|
58
|
+
}
|
|
59
|
+
return ArrayType;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Returns the size in bytes of one element of a WebGL component type. */
|
|
63
|
+
static getByteSize(glType: number): number {
|
|
64
|
+
return GLType.getArrayType(glType).BYTES_PER_ELEMENT;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Returns whether a value is a supported WebGL component type. */
|
|
68
|
+
static validate(glType: number): boolean {
|
|
69
|
+
return Boolean(GL_TYPE_TO_ARRAY_TYPE[glType]);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Creates a typed view over an ArrayBuffer or an existing ArrayBuffer view. */
|
|
73
|
+
static createTypedArray(
|
|
74
|
+
glType: number,
|
|
75
|
+
buffer: ArrayBufferLike | ArrayBufferView,
|
|
76
|
+
byteOffset = 0,
|
|
77
|
+
length?: number
|
|
78
|
+
): TypedArray {
|
|
79
|
+
const ArrayType = GLType.getArrayType(glType);
|
|
80
|
+
const arrayBuffer = ArrayBuffer.isView(buffer) ? buffer.buffer : buffer;
|
|
81
|
+
const viewByteOffset = ArrayBuffer.isView(buffer) ? buffer.byteOffset : 0;
|
|
82
|
+
const resolvedByteOffset = viewByteOffset + byteOffset;
|
|
83
|
+
const resolvedLength =
|
|
84
|
+
length ?? Math.floor((buffer.byteLength - byteOffset) / ArrayType.BYTES_PER_ELEMENT);
|
|
85
|
+
return new ArrayType(arrayBuffer as ArrayBuffer, resolvedByteOffset, resolvedLength);
|
|
86
|
+
}
|
|
87
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
export type {TypedArray, TypedArrayConstructor} from '@math.gl/types';
|
|
6
|
+
|
|
7
|
+
export {GL, GL_TYPE, GL_PRIMITIVE, GL_PRIMITIVE_MODE} from './constants';
|
|
8
|
+
export {default as GLType} from './gl-type';
|
|
9
|
+
|
|
10
|
+
export type {Geometry, GeometryAttribute} from './geometry';
|
|
11
|
+
export {isGeometry} from './geometry';
|
|
12
|
+
|
|
13
|
+
export type {Primitive} from './primitive-iterator';
|
|
14
|
+
export {makeAttributeIterator} from './attribute-iterator';
|
|
15
|
+
export {makePrimitiveIterator} from './primitive-iterator';
|
|
16
|
+
export {computeVertexNormals} from './compute-vertex-normals';
|
|
17
|
+
|
|
18
|
+
export {encodeRGB565, decodeRGB565} from './rgb565';
|
|
19
|
+
export {concatTypedArrays} from './typed-array-utils';
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
octEncodeInRange,
|
|
23
|
+
octEncode,
|
|
24
|
+
octEncodeToVector4,
|
|
25
|
+
octDecodeInRange,
|
|
26
|
+
octDecode,
|
|
27
|
+
octDecodeFromVector4,
|
|
28
|
+
octPackFloat,
|
|
29
|
+
octEncodeFloat,
|
|
30
|
+
octDecodeFloat,
|
|
31
|
+
octPack,
|
|
32
|
+
octUnpack,
|
|
33
|
+
compressTextureCoordinates,
|
|
34
|
+
decompressTextureCoordinates,
|
|
35
|
+
zigZagDeltaDecode
|
|
36
|
+
} from './attribute-compression';
|
|
37
|
+
|
|
38
|
+
export {emod} from './coordinates';
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {TypedArray} from '@math.gl/types';
|
|
6
|
+
import {GL} from './constants';
|
|
7
|
+
import type {Geometry} from './geometry';
|
|
8
|
+
import {getAttributeValues, getPositionAttribute, isGeometry} from './geometry';
|
|
9
|
+
|
|
10
|
+
/** One dereferenced point, line, or triangle primitive. */
|
|
11
|
+
export type Primitive = {
|
|
12
|
+
attributes: object;
|
|
13
|
+
type: number;
|
|
14
|
+
i1: number;
|
|
15
|
+
i2?: number;
|
|
16
|
+
i3?: number;
|
|
17
|
+
primitiveIndex: number;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Iterates through a geometry's primitives and dereferences optional indices.
|
|
22
|
+
*
|
|
23
|
+
* The legacy argument form `(indices, attributes, mode, start, end)` is also supported.
|
|
24
|
+
*/
|
|
25
|
+
export function* makePrimitiveIterator(
|
|
26
|
+
geometryOrIndices?: Geometry | TypedArray | {value?: TypedArray; values?: TypedArray},
|
|
27
|
+
attributes: object = {},
|
|
28
|
+
mode?: number,
|
|
29
|
+
start = 0,
|
|
30
|
+
end?: number
|
|
31
|
+
): Iterable<Primitive> {
|
|
32
|
+
let indices: TypedArray | undefined;
|
|
33
|
+
if (isGeometry(geometryOrIndices)) {
|
|
34
|
+
const geometry = geometryOrIndices;
|
|
35
|
+
indices = geometry.indices ? getAttributeValues(geometry.indices) : undefined;
|
|
36
|
+
attributes = geometry.attributes;
|
|
37
|
+
mode = geometry.mode;
|
|
38
|
+
if (end === undefined) {
|
|
39
|
+
const position = getPositionAttribute(geometry);
|
|
40
|
+
end = indices?.length ?? getAttributeValues(position).length / (position.size || 3);
|
|
41
|
+
}
|
|
42
|
+
} else if (geometryOrIndices) {
|
|
43
|
+
indices = getAttributeValues(geometryOrIndices);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (mode === undefined) {
|
|
47
|
+
throw new Error('Primitive mode is required');
|
|
48
|
+
}
|
|
49
|
+
end ??= indices?.length ?? start;
|
|
50
|
+
|
|
51
|
+
const dereference = (index: number): number => Number(indices ? indices[index] : index);
|
|
52
|
+
let primitiveIndex = 0;
|
|
53
|
+
|
|
54
|
+
switch (mode) {
|
|
55
|
+
case GL.POINTS:
|
|
56
|
+
for (let index = start; index < end; index++) {
|
|
57
|
+
yield {
|
|
58
|
+
attributes,
|
|
59
|
+
type: GL.POINTS,
|
|
60
|
+
i1: dereference(index),
|
|
61
|
+
primitiveIndex: primitiveIndex++
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return;
|
|
65
|
+
|
|
66
|
+
case GL.LINES:
|
|
67
|
+
for (let index = start; index + 1 < end; index += 2) {
|
|
68
|
+
yield {
|
|
69
|
+
attributes,
|
|
70
|
+
type: GL.LINES,
|
|
71
|
+
i1: dereference(index),
|
|
72
|
+
i2: dereference(index + 1),
|
|
73
|
+
primitiveIndex: primitiveIndex++
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
return;
|
|
77
|
+
|
|
78
|
+
case GL.LINE_STRIP:
|
|
79
|
+
for (let index = start; index + 1 < end; index++) {
|
|
80
|
+
yield {
|
|
81
|
+
attributes,
|
|
82
|
+
type: GL.LINES,
|
|
83
|
+
i1: dereference(index),
|
|
84
|
+
i2: dereference(index + 1),
|
|
85
|
+
primitiveIndex: primitiveIndex++
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return;
|
|
89
|
+
|
|
90
|
+
case GL.LINE_LOOP:
|
|
91
|
+
for (let index = start; index < end; index++) {
|
|
92
|
+
yield {
|
|
93
|
+
attributes,
|
|
94
|
+
type: GL.LINES,
|
|
95
|
+
i1: dereference(index),
|
|
96
|
+
i2: dereference(index + 1 < end ? index + 1 : start),
|
|
97
|
+
primitiveIndex: primitiveIndex++
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
return;
|
|
101
|
+
|
|
102
|
+
case GL.TRIANGLES:
|
|
103
|
+
for (let index = start; index + 2 < end; index += 3) {
|
|
104
|
+
yield {
|
|
105
|
+
attributes,
|
|
106
|
+
type: GL.TRIANGLES,
|
|
107
|
+
i1: dereference(index),
|
|
108
|
+
i2: dereference(index + 1),
|
|
109
|
+
i3: dereference(index + 2),
|
|
110
|
+
primitiveIndex: primitiveIndex++
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
|
|
115
|
+
case GL.TRIANGLE_STRIP:
|
|
116
|
+
for (let index = start; index + 2 < end; index++) {
|
|
117
|
+
const odd = (index - start) % 2 === 1;
|
|
118
|
+
yield {
|
|
119
|
+
attributes,
|
|
120
|
+
type: GL.TRIANGLES,
|
|
121
|
+
i1: dereference(odd ? index + 1 : index),
|
|
122
|
+
i2: dereference(odd ? index : index + 1),
|
|
123
|
+
i3: dereference(index + 2),
|
|
124
|
+
primitiveIndex: primitiveIndex++
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
return;
|
|
128
|
+
|
|
129
|
+
case GL.TRIANGLE_FAN:
|
|
130
|
+
for (let index = start + 1; index + 1 < end; index++) {
|
|
131
|
+
yield {
|
|
132
|
+
attributes,
|
|
133
|
+
type: GL.TRIANGLES,
|
|
134
|
+
i1: dereference(start),
|
|
135
|
+
i2: dereference(index),
|
|
136
|
+
i3: dereference(index + 1),
|
|
137
|
+
primitiveIndex: primitiveIndex++
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
return;
|
|
141
|
+
|
|
142
|
+
default:
|
|
143
|
+
throw new Error(`Unknown primitive mode ${mode}`);
|
|
144
|
+
}
|
|
145
|
+
}
|
package/src/rgb565.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
/** Decodes a packed RGB565 value into 8-bit RGB components. */
|
|
6
|
+
export function decodeRGB565(rgb565: number, target: number[] = [0, 0, 0]): number[] {
|
|
7
|
+
const red = (rgb565 >> 11) & 31;
|
|
8
|
+
const green = (rgb565 >> 5) & 63;
|
|
9
|
+
const blue = rgb565 & 31;
|
|
10
|
+
target[0] = (red << 3) | (red >> 2);
|
|
11
|
+
target[1] = (green << 2) | (green >> 4);
|
|
12
|
+
target[2] = (blue << 3) | (blue >> 2);
|
|
13
|
+
return target;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Encodes 8-bit RGB components into a packed RGB565 value. */
|
|
17
|
+
export function encodeRGB565(rgb: ArrayLike<number>): number {
|
|
18
|
+
const red = Math.round(clampByte(rgb[0] ?? 0) * (31 / 255));
|
|
19
|
+
const green = Math.round(clampByte(rgb[1] ?? 0) * (63 / 255));
|
|
20
|
+
const blue = Math.round(clampByte(rgb[2] ?? 0) * (31 / 255));
|
|
21
|
+
return (red << 11) | (green << 5) | blue;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function clampByte(value: number): number {
|
|
25
|
+
return Math.max(0, Math.min(255, value));
|
|
26
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
/** Concatenates the visible bytes of multiple typed-array or DataView instances. */
|
|
6
|
+
export function concatTypedArrays(arrays: readonly ArrayBufferView[] = []): Uint8Array {
|
|
7
|
+
const byteLength = arrays.reduce((length, array) => length + array.byteLength, 0);
|
|
8
|
+
const result = new Uint8Array(byteLength);
|
|
9
|
+
let byteOffset = 0;
|
|
10
|
+
for (const array of arrays) {
|
|
11
|
+
const bytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
|
|
12
|
+
result.set(bytes, byteOffset);
|
|
13
|
+
byteOffset += bytes.byteLength;
|
|
14
|
+
}
|
|
15
|
+
return result;
|
|
16
|
+
}
|