@math.gl/geometry 5.0.0-beta.10 → 5.0.0-beta.12

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.
Files changed (100) hide show
  1. package/dist/arrow/index.cjs +926 -0
  2. package/dist/arrow/index.cjs.map +6 -0
  3. package/dist/arrow/index.d.ts +3 -0
  4. package/dist/arrow/index.d.ts.map +1 -0
  5. package/dist/arrow/index.js +6 -0
  6. package/dist/arrow/index.js.map +1 -0
  7. package/dist/arrow/stream-tessellate.d.ts +34 -0
  8. package/dist/arrow/stream-tessellate.d.ts.map +1 -0
  9. package/dist/arrow/stream-tessellate.js +171 -0
  10. package/dist/arrow/stream-tessellate.js.map +1 -0
  11. package/dist/arrow/tessellate.d.ts +31 -0
  12. package/dist/arrow/tessellate.d.ts.map +1 -0
  13. package/dist/arrow/tessellate.js +102 -0
  14. package/dist/arrow/tessellate.js.map +1 -0
  15. package/dist/color/index.cjs +3 -108
  16. package/dist/color/index.cjs.map +3 -3
  17. package/dist/color/index.d.ts +2 -3
  18. package/dist/color/index.d.ts.map +1 -1
  19. package/dist/color/index.js +1 -1
  20. package/dist/color/index.js.map +1 -1
  21. package/dist/culling/index.cjs +3 -1
  22. package/dist/culling/index.cjs.map +1 -1
  23. package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.d.ts +4 -1
  24. package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.d.ts.map +1 -1
  25. package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.js +9 -2
  26. package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.js.map +1 -1
  27. package/dist/index.cjs +735 -684
  28. package/dist/index.cjs.map +3 -3
  29. package/dist/index.d.ts +3 -3
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +1 -1
  32. package/dist/index.js.map +1 -1
  33. package/dist/polygon/cut-by-grid.d.ts.map +1 -1
  34. package/dist/polygon/cut-by-grid.js +18 -0
  35. package/dist/polygon/cut-by-grid.js.map +1 -1
  36. package/dist/polygon/earcut.d.ts +12 -1
  37. package/dist/polygon/earcut.d.ts.map +1 -1
  38. package/dist/polygon/earcut.js +855 -630
  39. package/dist/polygon/earcut.js.map +1 -1
  40. package/dist/polygon/index.d.ts +1 -0
  41. package/dist/polygon/index.d.ts.map +1 -1
  42. package/dist/polygon/index.js.map +1 -1
  43. package/dist/polygon/subdivide-triangle-mesh.d.ts.map +1 -1
  44. package/dist/polygon/subdivide-triangle-mesh.js +55 -17
  45. package/dist/polygon/subdivide-triangle-mesh.js.map +1 -1
  46. package/dist/utils/compute-vertex-normals.d.ts +2 -2
  47. package/dist/utils/compute-vertex-normals.d.ts.map +1 -1
  48. package/dist/utils/compute-vertex-normals.js +3 -4
  49. package/dist/utils/compute-vertex-normals.js.map +1 -1
  50. package/dist/utils/geometry.d.ts +11 -4
  51. package/dist/utils/geometry.d.ts.map +1 -1
  52. package/dist/utils/geometry.js +19 -2
  53. package/dist/utils/geometry.js.map +1 -1
  54. package/dist/utils/index.d.ts +1 -3
  55. package/dist/utils/index.d.ts.map +1 -1
  56. package/dist/utils/index.js +0 -2
  57. package/dist/utils/index.js.map +1 -1
  58. package/dist/utils/mesh-processing.d.ts +16 -6
  59. package/dist/utils/mesh-processing.d.ts.map +1 -1
  60. package/dist/utils/mesh-processing.js +9 -6
  61. package/dist/utils/mesh-processing.js.map +1 -1
  62. package/dist/utils/primitive-iterator.d.ts +16 -8
  63. package/dist/utils/primitive-iterator.d.ts.map +1 -1
  64. package/dist/utils/primitive-iterator.js +32 -35
  65. package/dist/utils/primitive-iterator.js.map +1 -1
  66. package/package.json +16 -3
  67. package/{dist/color/color-lut-types.js → src/arrow/index.ts} +3 -2
  68. package/src/arrow/stream-tessellate.ts +209 -0
  69. package/src/arrow/tessellate.ts +133 -0
  70. package/src/color/index.ts +3 -3
  71. package/src/culling/lib/algorithms/oriented-bounding-box-quaternion.ts +8 -2
  72. package/src/index.ts +34 -22
  73. package/src/polygon/cut-by-grid.ts +18 -0
  74. package/src/polygon/earcut.ts +946 -668
  75. package/src/polygon/index.ts +1 -0
  76. package/src/polygon/subdivide-triangle-mesh.ts +56 -16
  77. package/src/utils/compute-vertex-normals.ts +5 -6
  78. package/src/utils/geometry.ts +32 -6
  79. package/src/utils/index.ts +5 -5
  80. package/src/utils/mesh-processing.ts +30 -14
  81. package/src/utils/primitive-iterator.ts +45 -43
  82. package/dist/color/color-lut-types.d.ts +0 -16
  83. package/dist/color/color-lut-types.d.ts.map +0 -1
  84. package/dist/color/color-lut-types.js.map +0 -1
  85. package/dist/color/sample-lut.d.ts +0 -13
  86. package/dist/color/sample-lut.d.ts.map +0 -1
  87. package/dist/color/sample-lut.js +0 -132
  88. package/dist/color/sample-lut.js.map +0 -1
  89. package/dist/utils/constants.d.ts +0 -52
  90. package/dist/utils/constants.d.ts.map +0 -1
  91. package/dist/utils/constants.js +0 -39
  92. package/dist/utils/constants.js.map +0 -1
  93. package/dist/utils/gl-type.d.ts +0 -17
  94. package/dist/utils/gl-type.d.ts.map +0 -1
  95. package/dist/utils/gl-type.js +0 -69
  96. package/dist/utils/gl-type.js.map +0 -1
  97. package/src/color/color-lut-types.ts +0 -19
  98. package/src/color/sample-lut.ts +0 -175
  99. package/src/utils/constants.ts +0 -42
  100. package/src/utils/gl-type.ts +0 -87
@@ -13,6 +13,7 @@ export {
13
13
  export type {PolygonWinding, WindingDirection} from './polygon-utils';
14
14
 
15
15
  export {earcut} from './earcut';
16
+ export type {EarcutOptions} from './earcut';
16
17
 
17
18
  export {clipPolygon, clipPolyline} from './lineclip';
18
19
 
@@ -76,9 +76,15 @@ export type SubdividedTriangleMesh = {
76
76
  };
77
77
 
78
78
  type Vertex = {source: number[]; target: number[]; weights: Map<number, number>};
79
- type Face = {vertices: [number, number, number]; sourceTriangle: number};
79
+ type Face = {
80
+ vertices: [number, number, number];
81
+ sourceTriangle: number;
82
+ /** Passed the error probes. Probes are deterministic, so an unsplit face is not probed again. */
83
+ accepted?: boolean;
84
+ };
80
85
 
81
86
  const EDGE_FRACTIONS = [0.25, 0.5, 0.75];
87
+ const EDGE_SAMPLE_ORDER = [1, 0, 2];
82
88
  const FACE_WEIGHTS = [
83
89
  [1 / 3, 1 / 3, 1 / 3],
84
90
  [0.5, 0.25, 0.25],
@@ -115,6 +121,10 @@ export function subdivideTriangleMesh(
115
121
  validateOptions();
116
122
  const vertices: Vertex[] = [];
117
123
  let faces: Face[] = [];
124
+ /** Sampled errors per undirected edge, ordered from its lower vertex index. */
125
+ const edgeErrors = new Map<string, number[]>();
126
+ /** Projected midpoints of sampled edges, reused when the edge is split. */
127
+ const edgeMidpointTargets = new Map<string, number[]>();
118
128
  for (let i = 0; i < mesh.positions.length; i += size) {
119
129
  const source = Array.from({length: size}, (_, j) => mesh.positions[i + j]);
120
130
  if (!source.every(Number.isFinite)) throw new RangeError('Source positions must be finite');
@@ -139,11 +149,15 @@ export function subdivideTriangleMesh(
139
149
  splitEdges.add(edgeKey(a, b));
140
150
  }
141
151
  }
152
+ } else if (face.accepted) {
153
+ // Probed in an earlier pass and not split since.
142
154
  } else if (needsRefinement(face)) {
143
155
  const [a, b, c] = face.vertices;
144
156
  splitEdges.add(edgeKey(a, b));
145
157
  splitEdges.add(edgeKey(b, c));
146
158
  splitEdges.add(edgeKey(c, a));
159
+ } else {
160
+ face.accepted = true;
147
161
  }
148
162
  }
149
163
  if (!splitEdges.size) return buildResult();
@@ -200,7 +214,7 @@ export function subdivideTriangleMesh(
200
214
  return target;
201
215
  }
202
216
 
203
- function error(points: Vertex[], weights: number[]): number {
217
+ function error(points: Vertex[], weights: number[], keepExact?: number[][]): number {
204
218
  const source = blend(
205
219
  points.map(p => p.source),
206
220
  weights
@@ -210,31 +224,54 @@ export function subdivideTriangleMesh(
210
224
  weights
211
225
  );
212
226
  const exact = project(source);
227
+ keepExact?.push(exact);
213
228
  if (!errorMetric) return Math.hypot(...exact.map((value, i) => value - linearTarget[i]));
214
- const value = errorMetric({source, exact, interpolated: linearTarget});
229
+ const value = errorMetric({source, exact: exact.slice(), interpolated: linearTarget});
215
230
  if (!Number.isFinite(value) || value < 0) {
216
231
  throw new RangeError('errorMetric must return a finite nonnegative value');
217
232
  }
218
233
  return value;
219
234
  }
220
235
 
236
+ /** Error at one edge fraction, cached per undirected edge for the neighbouring face.
237
+ * Blending two points is bitwise symmetric, so a->b at f is b->a at 1 - f.
238
+ */
239
+ function edgeError(a: number, b: number, sample: number): number {
240
+ const key = edgeKey(a, b);
241
+ let errors = edgeErrors.get(key);
242
+ if (!errors) {
243
+ errors = [];
244
+ edgeErrors.set(key, errors);
245
+ }
246
+ const slot = a <= b ? sample : EDGE_FRACTIONS.length - 1 - sample;
247
+ let value = errors[slot];
248
+ if (value === undefined) {
249
+ const fraction = EDGE_FRACTIONS[sample];
250
+ // The midpoint sample has the source coordinates that midpoint() would project.
251
+ const keepExact = fraction === 0.5 ? [] : undefined;
252
+ value = error([vertices[a], vertices[b]], [1 - fraction, fraction], keepExact);
253
+ errors[slot] = value;
254
+ if (keepExact) edgeMidpointTargets.set(key, keepExact[0]);
255
+ }
256
+ return value;
257
+ }
258
+
221
259
  function needsRefinement(face: Face): boolean {
222
260
  const points = face.vertices.map(index => vertices[index]);
223
- let exceedsTolerance = false;
261
+ const limit = tolerance ?? Infinity;
224
262
  for (let i = 0; i < 3; i++) {
225
- const a = points[i];
226
- const b = points[(i + 1) % 3];
227
- const length = sourceEdgeLength(a, b);
228
- if (length > maxEdgeLength) exceedsTolerance = true;
229
- for (const fraction of EDGE_FRACTIONS) {
230
- if (error([a, b], [1 - fraction, fraction]) > (tolerance ?? Infinity))
231
- exceedsTolerance = true;
263
+ if (sourceEdgeLength(points[i], points[(i + 1) % 3]) > maxEdgeLength) return true;
264
+ }
265
+ // Midpoints first: a failed face splits all three edges and keeps their projections.
266
+ for (const sample of EDGE_SAMPLE_ORDER) {
267
+ for (let i = 0; i < 3; i++) {
268
+ if (edgeError(face.vertices[i], face.vertices[(i + 1) % 3], sample) > limit) return true;
232
269
  }
233
270
  }
234
271
  for (const weights of FACE_WEIGHTS) {
235
- if (error(points, weights) > (tolerance ?? Infinity)) exceedsTolerance = true;
272
+ if (error(points, weights) > limit) return true;
236
273
  }
237
- return exceedsTolerance;
274
+ return false;
238
275
  }
239
276
 
240
277
  function midpoint(a: number, b: number, midpoints: Map<string, number>): number {
@@ -254,7 +291,7 @@ export function subdivideTriangleMesh(
254
291
  // the original edge endpoints, so at most three source vertices contribute.
255
292
  if (weights.size > 3) throw new Error('Inconsistent mesh provenance');
256
293
  const index = vertices.length;
257
- vertices.push({source, target: project(source), weights});
294
+ vertices.push({source, target: edgeMidpointTargets.get(key) ?? project(source), weights});
258
295
  midpoints.set(key, index);
259
296
  return index;
260
297
  }
@@ -270,8 +307,11 @@ export function subdivideTriangleMesh(
270
307
  const bc = splitEdges.has(edgeKey(b, c)) ? midpoint(b, c, midpoints) : -1;
271
308
  const ca = splitEdges.has(edgeKey(c, a)) ? midpoint(c, a, midpoints) : -1;
272
309
  const count = Number(ab >= 0) + Number(bc >= 0) + Number(ca >= 0);
273
- if (count === 0) emit(a, b, c);
274
- else if (count === 3) {
310
+ if (count === 0) {
311
+ if (output.length >= maxTriangles)
312
+ throw new RangeError('Triangle mesh subdivision exceeded maxTriangles');
313
+ output.push(face);
314
+ } else if (count === 3) {
275
315
  emit(a, ab, ca);
276
316
  emit(ab, b, bc);
277
317
  emit(ca, bc, c);
@@ -3,17 +3,16 @@
3
3
  // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
4
 
5
5
  import {Vector3} from '@math.gl/core';
6
- import {GL} from './constants';
7
- import type {Geometry} from './geometry';
6
+ import type {GeometryInput} from './geometry';
8
7
  import {getAttributeValues, getPositionAttribute} from './geometry';
9
8
  import {makePrimitiveIterator} from './primitive-iterator';
10
9
 
11
10
  /** Computes smooth, area-weighted vertex normals for a triangle geometry. */
12
- export function computeVertexNormals(geometry: Geometry): Float32Array {
11
+ export function computeVertexNormals(geometry: GeometryInput): Float32Array {
13
12
  if (
14
- geometry.mode !== GL.TRIANGLES &&
15
- geometry.mode !== GL.TRIANGLE_STRIP &&
16
- geometry.mode !== GL.TRIANGLE_FAN
13
+ geometry.topology !== 'triangle-list' &&
14
+ geometry.topology !== 'triangle-strip' &&
15
+ geometry.topology !== 'triangle-fan'
17
16
  ) {
18
17
  throw new Error('Triangle geometry is required');
19
18
  }
@@ -3,28 +3,54 @@
3
3
  // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
4
 
5
5
  import type {TypedArray} from '@math.gl/core';
6
+ import type {PrimitiveTopology} from '../lib/geometry';
6
7
 
7
8
  /** A typed geometry attribute with either loaders.gl-style `value` or legacy `values` storage. */
8
9
  export type GeometryAttribute = {
10
+ /** Attribute data in canonical storage. */
9
11
  value?: TypedArray;
12
+ /** Alternate field for attribute data. */
10
13
  values?: TypedArray;
14
+ /** Number of components per element. */
11
15
  size?: number;
12
16
  };
13
17
 
14
18
  /** Minimal geometry shape understood by the geometry utilities. */
15
- export type Geometry = {
16
- mode: number;
19
+ export type GeometryInput = {
20
+ /** Connectivity of the indexed or sequential vertices. */
21
+ topology: PrimitiveTopology | 'line-loop' | 'triangle-fan';
22
+ /** Optional vertex indices in a packed array. */
17
23
  indices?: GeometryAttribute | TypedArray;
24
+ /** Vertex attributes keyed by semantic name. */
18
25
  attributes: Record<string, GeometryAttribute>;
19
26
  };
20
27
 
21
28
  /** Returns whether a value has the minimal geometry shape. */
22
- export function isGeometry(value: unknown): value is Geometry {
29
+ export function isGeometry(value: unknown): value is GeometryInput {
23
30
  if (!value || typeof value !== 'object') {
24
31
  return false;
25
32
  }
26
- const geometry = value as Partial<Geometry>;
27
- return typeof geometry.mode === 'number' && Boolean(geometry.attributes);
33
+ const {topology, attributes} = value as Partial<GeometryInput>;
34
+ if (
35
+ !attributes ||
36
+ typeof attributes !== 'object' ||
37
+ Array.isArray(attributes) ||
38
+ ArrayBuffer.isView(attributes)
39
+ ) {
40
+ return false;
41
+ }
42
+ switch (topology) {
43
+ case 'point-list':
44
+ case 'line-list':
45
+ case 'line-strip':
46
+ case 'line-loop':
47
+ case 'triangle-list':
48
+ case 'triangle-strip':
49
+ case 'triangle-fan':
50
+ return true;
51
+ default:
52
+ return false;
53
+ }
28
54
  }
29
55
 
30
56
  /** Returns the typed array stored in a geometry attribute. */
@@ -40,7 +66,7 @@ export function getAttributeValues(attribute: GeometryAttribute | TypedArray): T
40
66
  }
41
67
 
42
68
  /** Returns a geometry's position attribute. */
43
- export function getPositionAttribute(geometry: Geometry): GeometryAttribute {
69
+ export function getPositionAttribute(geometry: GeometryInput): GeometryAttribute {
44
70
  const position = geometry.attributes['POSITION'] || geometry.attributes['positions'];
45
71
  if (!position) {
46
72
  throw new Error('Geometry does not contain a position attribute');
@@ -4,10 +4,7 @@
4
4
 
5
5
  export type {TypedArray, TypedArrayConstructor} from '@math.gl/core';
6
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';
7
+ export type {GeometryInput, GeometryAttribute} from './geometry';
11
8
  export {isGeometry} from './geometry';
12
9
 
13
10
  export type {Primitive} from './primitive-iterator';
@@ -43,7 +40,10 @@ export {
43
40
  weldGeometry,
44
41
  getDegenerateTriangles
45
42
  } from './mesh-processing';
46
- export type {WeldGeometryOptions, WeldGeometryResult} from './mesh-processing';
43
+ export type {
44
+ WeldGeometryOptions,
45
+ WeldGeometryResult
46
+ } from './mesh-processing';
47
47
 
48
48
  export {interpolatePackedAttributes} from './interpolate-packed-attributes';
49
49
  export type {
@@ -4,21 +4,28 @@
4
4
 
5
5
  import {Matrix4} from '@math.gl/core';
6
6
  import type {TypedArray, TypedArrayConstructor} from '@math.gl/core';
7
- import {GL} from './constants';
8
- import {getAttributeValues, type Geometry, type GeometryAttribute} from './geometry';
7
+ import {getAttributeValues, type GeometryInput, type GeometryAttribute} from './geometry';
9
8
 
9
+ /** Geometry emitted by triangle mesh operations. */
10
+ type TriangleGeometry = GeometryInput & {topology: 'triangle-list'};
11
+
12
+ /** Validated, packed mesh buffers used internally by mesh operations. */
10
13
  type MeshData = {
14
+ /** Attributes with resolved buffers and element sizes. */
11
15
  attributes: Record<string, {value: TypedArray; size: number}>;
16
+ /** Semantic name of the position attribute. */
12
17
  position: string;
18
+ /** Number of vertices in each attribute buffer. */
13
19
  count: number;
20
+ /** Explicit triangle indices, including those generated for non-indexed input. */
14
21
  indices: Uint32Array;
15
22
  };
16
23
 
17
24
  /** Fresh, indexed triangle-list geometry. No input buffers are changed. */
18
25
  export function transformGeometry(
19
- geometry: Geometry,
26
+ geometry: GeometryInput,
20
27
  matrix: Readonly<ArrayLike<number>>
21
- ): Geometry {
28
+ ): TriangleGeometry {
22
29
  const mesh = readMesh(geometry);
23
30
  if (
24
31
  matrix.length !== 16 ||
@@ -99,11 +106,11 @@ export function transformGeometry(
99
106
  if (determinant < 0)
100
107
  for (let i = 0; i < indices.length; i += 3)
101
108
  [indices[i + 1], indices[i + 2]] = [indices[i + 2], indices[i + 1]];
102
- return {mode: GL.TRIANGLES, attributes, indices};
109
+ return {topology: 'triangle-list', attributes, indices};
103
110
  }
104
111
 
105
112
  /** Concatenates compatible triangle meshes and offsets vertex indices. */
106
- export function mergeGeometries(geometries: readonly Geometry[]): Geometry {
113
+ export function mergeGeometries(geometries: readonly GeometryInput[]): TriangleGeometry {
107
114
  if (!geometries.length) throw new RangeError('At least one geometry is required');
108
115
  const meshes = geometries.map(readMesh),
109
116
  first = meshes[0],
@@ -148,21 +155,26 @@ export function mergeGeometries(geometries: readonly Geometry[]): Geometry {
148
155
  for (const index of mesh.indices) indices[indexOffset++] = index + vertexOffset;
149
156
  vertexOffset += mesh.count;
150
157
  }
151
- return {mode: GL.TRIANGLES, attributes, indices};
158
+ return {topology: 'triangle-list', attributes, indices};
152
159
  }
153
160
 
161
+ /** Controls vertex matching during a weld. */
154
162
  export type WeldGeometryOptions = {
155
163
  /** Position quantization step; zero means exact matching. */ positionGridSize?: number;
156
164
  };
165
+ /** Welded mesh and the mapping from its original vertices. */
157
166
  export type WeldGeometryResult = {
158
- geometry: Geometry;
167
+ /** Welded triangle mesh. */
168
+ geometry: TriangleGeometry;
169
+ /** Welded vertex index for each original vertex. */
159
170
  vertexMap: Uint32Array;
171
+ /** Number of duplicate vertices removed. */
160
172
  removedVertices: number;
161
173
  };
162
174
 
163
175
  /** Welds full attribute tuples, preserving UV seams and hard normal edges. */
164
176
  export function weldGeometry(
165
- geometry: Geometry,
177
+ geometry: GeometryInput,
166
178
  options: WeldGeometryOptions = {}
167
179
  ): WeldGeometryResult {
168
180
  const mesh = readMesh(geometry),
@@ -208,7 +220,7 @@ export function weldGeometry(
208
220
  }
209
221
  return {
210
222
  geometry: {
211
- mode: GL.TRIANGLES,
223
+ topology: 'triangle-list',
212
224
  attributes,
213
225
  indices: Uint32Array.from(mesh.indices, index => vertexMap[index])
214
226
  },
@@ -218,7 +230,7 @@ export function weldGeometry(
218
230
  }
219
231
 
220
232
  /** Returns triangle numbers with area <= areaEpsilon, in original draw order. */
221
- export function getDegenerateTriangles(geometry: Geometry, areaEpsilon = 0): Uint32Array {
233
+ export function getDegenerateTriangles(geometry: GeometryInput, areaEpsilon = 0): Uint32Array {
222
234
  if (!Number.isFinite(areaEpsilon) || areaEpsilon < 0)
223
235
  throw new RangeError('Area epsilon must be finite and nonnegative');
224
236
  const mesh = readMesh(geometry),
@@ -256,8 +268,9 @@ export function getDegenerateTriangles(geometry: Geometry, areaEpsilon = 0): Uin
256
268
  return Uint32Array.from(result);
257
269
  }
258
270
 
259
- function readMesh(geometry: Geometry): MeshData {
260
- if (geometry.mode !== GL.TRIANGLES)
271
+ /** Validates triangle connectivity and resolves the packed input buffers. */
272
+ function readMesh(geometry: GeometryInput): MeshData {
273
+ if (geometry.topology !== 'triangle-list')
261
274
  throw new RangeError('Packed triangle-list geometry is required');
262
275
  const position = geometry.attributes['POSITION']
263
276
  ? 'POSITION'
@@ -363,7 +376,10 @@ function toDyadic(value: number): Dyadic {
363
376
  const exponentBits = (high >>> 20) & 0x7ff;
364
377
  const fraction = (BigInt(high & 0xfffff) << 32n) | BigInt(low);
365
378
  const n = exponentBits ? (1n << 52n) | fraction : fraction;
366
- return {n: high >>> 31 ? -n : n, exponent: exponentBits ? exponentBits - 1075 : -1074};
379
+ return {
380
+ n: high >>> 31 ? -n : n,
381
+ exponent: exponentBits ? exponentBits - 1075 : -1074
382
+ };
367
383
  }
368
384
  function subtractDyadic(a: Dyadic, b: Dyadic): Dyadic {
369
385
  const exponent = Math.min(a.exponent, b.exponent);
@@ -2,72 +2,77 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
4
 
5
- import type {TypedArray} from '@math.gl/core';
6
- import {GL} from './constants';
7
- import type {Geometry} from './geometry';
5
+ import type {GeometryInput} from './geometry';
8
6
  import {getAttributeValues, getPositionAttribute, isGeometry} from './geometry';
9
7
 
10
8
  /** One dereferenced point, line, or triangle primitive. */
11
9
  export type Primitive = {
10
+ /** The input geometry’s attribute map. */
12
11
  attributes: object;
13
- type: number;
12
+ /** List topology of the emitted primitive. */
13
+ type: 'point-list' | 'line-list' | 'triangle-list';
14
+ /** Index of the first vertex. */
14
15
  i1: number;
16
+ /** Index of the second vertex on a line or triangle. */
15
17
  i2?: number;
18
+ /** Index of the third vertex on a triangle. */
16
19
  i3?: number;
20
+ /** Zero-based index in this iteration. */
17
21
  primitiveIndex: number;
18
22
  };
19
23
 
20
24
  /**
21
25
  * Iterates through a geometry's primitives and dereferences optional indices.
22
26
  *
23
- * The legacy argument form `(indices, attributes, mode, start, end)` is also supported.
27
+ * @param geometry - Geometry whose optional indices are dereferenced.
28
+ * @param options - Zero-based, end-exclusive range in the index or vertex sequence.
24
29
  */
25
30
  export function* makePrimitiveIterator(
26
- geometryOrIndices?: Geometry | TypedArray | {value?: TypedArray; values?: TypedArray},
27
- attributes: object = {},
28
- mode?: number,
29
- start = 0,
30
- end?: number
31
+ geometry: GeometryInput,
32
+ options: {
33
+ /** Offset of the first index or vertex to consider. */
34
+ start?: number;
35
+ /** Exclusive end of the range. Defaults to the index or vertex count. */
36
+ end?: number;
37
+ } = {}
31
38
  ): 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
+ if (!isGeometry(geometry)) {
40
+ throw new TypeError('Geometry with a supported topology and attributes is required');
41
+ }
42
+ const {start = 0} = options;
43
+ let end = options.end;
44
+ const {attributes, topology} = geometry;
45
+ const indices = geometry.indices ? getAttributeValues(geometry.indices) : undefined;
46
+ if (end === undefined) {
47
+ if (indices) {
48
+ end = indices.length;
49
+ } else {
39
50
  const position = getPositionAttribute(geometry);
40
- end = indices?.length ?? getAttributeValues(position).length / (position.size || 3);
51
+ end = getAttributeValues(position).length / (position.size || 3);
41
52
  }
42
- } else if (geometryOrIndices) {
43
- indices = getAttributeValues(geometryOrIndices);
44
- }
45
-
46
- if (mode === undefined) {
47
- throw new Error('Primitive mode is required');
48
53
  }
49
- end ??= indices?.length ?? start;
50
54
 
55
+ /** Resolves an index-buffer offset or sequential vertex index. */
51
56
  const dereference = (index: number): number => Number(indices ? indices[index] : index);
52
57
  let primitiveIndex = 0;
53
58
 
54
- switch (mode) {
55
- case GL.POINTS:
59
+ switch (topology) {
60
+ case 'point-list':
56
61
  for (let index = start; index < end; index++) {
57
62
  yield {
58
63
  attributes,
59
- type: GL.POINTS,
64
+ type: 'point-list',
60
65
  i1: dereference(index),
61
66
  primitiveIndex: primitiveIndex++
62
67
  };
63
68
  }
64
69
  return;
65
70
 
66
- case GL.LINES:
71
+ case 'line-list':
67
72
  for (let index = start; index + 1 < end; index += 2) {
68
73
  yield {
69
74
  attributes,
70
- type: GL.LINES,
75
+ type: 'line-list',
71
76
  i1: dereference(index),
72
77
  i2: dereference(index + 1),
73
78
  primitiveIndex: primitiveIndex++
@@ -75,11 +80,11 @@ export function* makePrimitiveIterator(
75
80
  }
76
81
  return;
77
82
 
78
- case GL.LINE_STRIP:
83
+ case 'line-strip':
79
84
  for (let index = start; index + 1 < end; index++) {
80
85
  yield {
81
86
  attributes,
82
- type: GL.LINES,
87
+ type: 'line-list',
83
88
  i1: dereference(index),
84
89
  i2: dereference(index + 1),
85
90
  primitiveIndex: primitiveIndex++
@@ -87,11 +92,11 @@ export function* makePrimitiveIterator(
87
92
  }
88
93
  return;
89
94
 
90
- case GL.LINE_LOOP:
95
+ case 'line-loop':
91
96
  for (let index = start; index < end; index++) {
92
97
  yield {
93
98
  attributes,
94
- type: GL.LINES,
99
+ type: 'line-list',
95
100
  i1: dereference(index),
96
101
  i2: dereference(index + 1 < end ? index + 1 : start),
97
102
  primitiveIndex: primitiveIndex++
@@ -99,11 +104,11 @@ export function* makePrimitiveIterator(
99
104
  }
100
105
  return;
101
106
 
102
- case GL.TRIANGLES:
107
+ case 'triangle-list':
103
108
  for (let index = start; index + 2 < end; index += 3) {
104
109
  yield {
105
110
  attributes,
106
- type: GL.TRIANGLES,
111
+ type: 'triangle-list',
107
112
  i1: dereference(index),
108
113
  i2: dereference(index + 1),
109
114
  i3: dereference(index + 2),
@@ -112,12 +117,12 @@ export function* makePrimitiveIterator(
112
117
  }
113
118
  return;
114
119
 
115
- case GL.TRIANGLE_STRIP:
120
+ case 'triangle-strip':
116
121
  for (let index = start; index + 2 < end; index++) {
117
122
  const odd = (index - start) % 2 === 1;
118
123
  yield {
119
124
  attributes,
120
- type: GL.TRIANGLES,
125
+ type: 'triangle-list',
121
126
  i1: dereference(odd ? index + 1 : index),
122
127
  i2: dereference(odd ? index : index + 1),
123
128
  i3: dereference(index + 2),
@@ -126,11 +131,11 @@ export function* makePrimitiveIterator(
126
131
  }
127
132
  return;
128
133
 
129
- case GL.TRIANGLE_FAN:
134
+ case 'triangle-fan':
130
135
  for (let index = start + 1; index + 1 < end; index++) {
131
136
  yield {
132
137
  attributes,
133
- type: GL.TRIANGLES,
138
+ type: 'triangle-list',
134
139
  i1: dereference(start),
135
140
  i2: dereference(index),
136
141
  i3: dereference(index + 1),
@@ -138,8 +143,5 @@ export function* makePrimitiveIterator(
138
143
  };
139
144
  }
140
145
  return;
141
-
142
- default:
143
- throw new Error(`Unknown primitive mode ${mode}`);
144
146
  }
145
147
  }
@@ -1,16 +0,0 @@
1
- /** Numerical RGB lookup table, independent of its file encoding or renderer. */
2
- export type ColorLUT = {
3
- /** Independent 1D curves or one 3D RGB mapping. */
4
- dimension: 1 | 3;
5
- /** Number of samples along each axis. */
6
- size: number;
7
- /** Interleaved RGB values, red coordinate varying fastest for 3D tables. */
8
- data: Float32Array;
9
- /** Minimum input coordinate for each channel. */
10
- domainMinimum: number[];
11
- /** Maximum input coordinate for each channel. */
12
- domainMaximum: number[];
13
- /** Optional strictly increasing normalized knots from 0 through 1. */
14
- inputGrid?: Float64Array;
15
- };
16
- //# sourceMappingURL=color-lut-types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"color-lut-types.d.ts","sourceRoot":"","sources":["../../src/color/color-lut-types.ts"],"names":[],"mappings":"AAIA,gFAAgF;AAChF,MAAM,MAAM,QAAQ,GAAG;IACrB,mDAAmD;IACnD,SAAS,EAAE,CAAC,GAAG,CAAC,CAAC;IACjB,yCAAyC;IACzC,IAAI,EAAE,MAAM,CAAC;IACb,4EAA4E;IAC5E,IAAI,EAAE,YAAY,CAAC;IACnB,iDAAiD;IACjD,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,iDAAiD;IACjD,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,sEAAsE;IACtE,SAAS,CAAC,EAAE,YAAY,CAAC;CAC1B,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"color-lut-types.js","sourceRoot":"","sources":["../../src/color/color-lut-types.ts"],"names":[],"mappings":"AAAA,UAAU;AACV,+BAA+B;AAC/B,4DAA4D"}
@@ -1,13 +0,0 @@
1
- import type { ColorLUT } from "./color-lut-types.js";
2
- /** Reusable RGB sampler; inputs use the authored domain and outputs retain signed/HDR values. */
3
- export type ColorLUTSampler = (red: number, green: number, blue: number) => [number, number, number];
4
- /** Options for interleaved floating-point RGB or RGBA buffers. */
5
- export type ApplyColorLUTOptions = {
6
- /** Number of interleaved components; default 3. Alpha is copied at float32 precision. */
7
- components?: 3 | 4;
8
- };
9
- /** Creates a snapshot sampler with per-channel 1D linear or red-fastest 3D trilinear interpolation. */
10
- export declare function createColorLUTSampler(lut: ColorLUT): ColorLUTSampler;
11
- /** Applies a LUT to floating-point pixels without changing the source or applying transfer functions. */
12
- export declare function applyColorLUT(lut: ColorLUT, colors: Float32Array | Float64Array, options?: ApplyColorLUTOptions): Float32Array;
13
- //# sourceMappingURL=sample-lut.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sample-lut.d.ts","sourceRoot":"","sources":["../../src/color/sample-lut.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,QAAQ,EAAC,6BAA0B;AAEhD,iGAAiG;AACjG,MAAM,MAAM,eAAe,GAAG,CAC5B,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,MAAM,KACT,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAE9B,kEAAkE;AAClE,MAAM,MAAM,oBAAoB,GAAG;IACjC,yFAAyF;IACzF,UAAU,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC;CACpB,CAAC;AAEF,uGAAuG;AACvG,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,QAAQ,GAAG,eAAe,CAOpE;AAmGD,yGAAyG;AACzG,wBAAgB,aAAa,CAC3B,GAAG,EAAE,QAAQ,EACb,MAAM,EAAE,YAAY,GAAG,YAAY,EACnC,OAAO,GAAE,oBAAyB,GACjC,YAAY,CAgBd"}