@loaders.gl/scientific 5.0.0-beta.4 → 5.0.0-beta.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.
Files changed (62) hide show
  1. package/README.md +6 -2
  2. package/dist/index.cjs +48 -4
  3. package/dist/index.cjs.map +4 -4
  4. package/dist/index.d.ts +4 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +3 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/netcdf/netcdf-loader.cjs +1 -1
  9. package/dist/netcdf/netcdf-loader.js +1 -1
  10. package/dist/netcdf/netcdf-source-loader-types.js +1 -1
  11. package/dist/netcdf/netcdf-source-loader.js +1 -1
  12. package/dist/netcdf.min.js +2 -2
  13. package/dist/npy-loader-types.cjs +1 -1
  14. package/dist/npy-loader-types.js +1 -1
  15. package/dist/npy-loader.cjs +1 -1
  16. package/dist/npy-worker.js +239 -11
  17. package/dist/nrrd/parse-nrrd.d.ts +22 -0
  18. package/dist/nrrd/parse-nrrd.d.ts.map +1 -0
  19. package/dist/nrrd/parse-nrrd.js +228 -0
  20. package/dist/nrrd/parse-nrrd.js.map +1 -0
  21. package/dist/nrrd-loader-types.cjs +58 -0
  22. package/dist/nrrd-loader-types.cjs.map +7 -0
  23. package/dist/nrrd-loader-types.d.ts +31 -0
  24. package/dist/nrrd-loader-types.d.ts.map +1 -0
  25. package/dist/nrrd-loader-types.js +25 -0
  26. package/dist/nrrd-loader-types.js.map +1 -0
  27. package/dist/nrrd-loader.cjs +252 -0
  28. package/dist/nrrd-loader.cjs.map +6 -0
  29. package/dist/nrrd-loader.d.ts +16 -0
  30. package/dist/nrrd-loader.d.ts.map +1 -0
  31. package/dist/nrrd-loader.js +8 -0
  32. package/dist/nrrd-loader.js.map +1 -0
  33. package/dist/vtk/index.cjs +26 -3
  34. package/dist/vtk/index.cjs.map +2 -2
  35. package/dist/vtk/vtk.d.ts.map +1 -1
  36. package/dist/vtk/vtk.js +19 -1
  37. package/dist/vtk/vtk.js.map +1 -1
  38. package/dist/vtk-loader-types.cjs +58 -0
  39. package/dist/vtk-loader-types.cjs.map +7 -0
  40. package/dist/vtk-loader-types.d.ts +34 -0
  41. package/dist/vtk-loader-types.d.ts.map +1 -0
  42. package/dist/vtk-loader-types.js +25 -0
  43. package/dist/vtk-loader-types.js.map +1 -0
  44. package/dist/vtk-loader.cjs +384 -0
  45. package/dist/vtk-loader.cjs.map +6 -0
  46. package/dist/vtk-loader.d.ts +21 -0
  47. package/dist/vtk-loader.d.ts.map +1 -0
  48. package/dist/vtk-loader.js +21 -0
  49. package/dist/vtk-loader.js.map +1 -0
  50. package/dist/zarr/geo-zarr-source-loader.js +1 -1
  51. package/dist/zarr/ome-zarr-source-loader.js +1 -1
  52. package/dist/zarr/zarr-loader-types.cjs +1 -1
  53. package/dist/zarr/zarr-loader-types.js +1 -1
  54. package/dist/zarr.min.js +3 -3
  55. package/package.json +19 -7
  56. package/src/index.ts +7 -1
  57. package/src/nrrd/parse-nrrd.ts +267 -0
  58. package/src/nrrd-loader-types.ts +25 -0
  59. package/src/nrrd-loader.ts +7 -0
  60. package/src/vtk/vtk.ts +28 -1
  61. package/src/vtk-loader-types.ts +25 -0
  62. package/src/vtk-loader.ts +19 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loaders.gl/scientific",
3
- "version": "5.0.0-beta.4",
3
+ "version": "5.0.0-beta.5",
4
4
  "description": "Scientific NumPy arrays and sources for NetCDF, Zarr, GRIB2, HDF5, and VTK",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -76,6 +76,16 @@
76
76
  "types": "./dist/npy-loader-types.d.ts",
77
77
  "import": "./dist/npy-loader-types.js",
78
78
  "require": "./dist/npy-loader-types.cjs"
79
+ },
80
+ "./vtk-loader": {
81
+ "types": "./dist/vtk-loader.d.ts",
82
+ "import": "./dist/vtk-loader.js",
83
+ "require": "./dist/vtk-loader.cjs"
84
+ },
85
+ "./nrrd-loader": {
86
+ "types": "./dist/nrrd-loader.d.ts",
87
+ "import": "./dist/nrrd-loader.js",
88
+ "require": "./dist/nrrd-loader.cjs"
79
89
  }
80
90
  },
81
91
  "sideEffects": false,
@@ -88,7 +98,7 @@
88
98
  "third-party-licenses"
89
99
  ],
90
100
  "scripts": {
91
- "pre-build": "npm run build-bundle && npm run build-bundle-dev && npm run build-cjs-netcdf && npm run build-cjs-zarr && npm run build-cjs-grib2 && npm run build-cjs-hdf5 && npm run build-cjs-vtk && npm run build-cjs-vector-field && npm run build-npy-worker && npm run build-metadata",
101
+ "pre-build": "npm run build-bundle && npm run build-bundle-dev && npm run build-cjs-netcdf && npm run build-cjs-zarr && npm run build-cjs-grib2 && npm run build-cjs-hdf5 && npm run build-cjs-vtk && npm run build-cjs-vector-field && npm run build-npy-worker && npm run build-metadata && npm run build-cjs-vtk-loader && npm run build-cjs-nrrd",
92
102
  "build-bundle": "ocular-bundle ./netcdf-bundle.ts --output=dist/netcdf.min.js && ocular-bundle ./zarr-bundle.ts --output=dist/zarr.min.js",
93
103
  "build-bundle-dev": "ocular-bundle ./netcdf-bundle.ts --env=dev --output=dist/netcdf.dev.js && ocular-bundle ./zarr-bundle.ts --env=dev --output=dist/zarr.dev.js",
94
104
  "build-cjs-netcdf": "esbuild src/netcdf/*.ts --bundle --format=cjs --platform=node --target=node22 --outdir=dist/netcdf --out-extension:.js=.cjs --external:@loaders.gl/* --external:@math.gl/*",
@@ -98,12 +108,14 @@
98
108
  "build-cjs-vtk": "esbuild src/vtk/index.ts --bundle --format=cjs --platform=node --target=node22 --outfile=dist/vtk/index.cjs --external:@loaders.gl/*",
99
109
  "build-cjs-vector-field": "esbuild src/vector-field/index.ts --bundle --format=cjs --platform=node --target=node22 --outfile=dist/vector-field/index.cjs --external:@loaders.gl/*",
100
110
  "build-npy-worker": "esbuild src/workers/npy-worker.ts --outfile=dist/npy-worker.js --target=esnext --bundle --define:__VERSION__=\\\"$npm_package_version\\\"",
101
- "build-metadata": "node ../../scripts/build-scientific-metadata.mjs"
111
+ "build-metadata": "node ../../scripts/build-scientific-metadata.mjs",
112
+ "build-cjs-vtk-loader": "esbuild src/vtk-loader.ts --bundle --format=cjs --platform=node --target=node22 --outfile=dist/vtk-loader.cjs --external:@loaders.gl/*",
113
+ "build-cjs-nrrd": "esbuild src/nrrd-loader.ts --bundle --format=cjs --platform=node --target=node22 --outfile=dist/nrrd-loader.cjs --external:@loaders.gl/*"
102
114
  },
103
115
  "dependencies": {
104
- "@loaders.gl/documents": "5.0.0-beta.4",
105
- "@math.gl/core": "5.0.0-beta.4",
106
- "@math.gl/geospatial": "5.0.0-beta.4",
116
+ "@loaders.gl/documents": "5.0.0-beta.5",
117
+ "@math.gl/core": "5.0.0-beta.5",
118
+ "@math.gl/geospatial": "5.0.0-beta.5",
107
119
  "h5wasm": "^0.10.3",
108
120
  "zarr": "^0.5.0",
109
121
  "zarrita": "~0.7.1"
@@ -130,5 +142,5 @@
130
142
  "cdf",
131
143
  "nc"
132
144
  ],
133
- "gitHead": "c9420f06e870fa353889d55b684e3588d24574c2"
145
+ "gitHead": "673b72c560b284f158ff145ba74707f9a3994935"
134
146
  }
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
- // loaders.gl
2
1
  // SPDX-License-Identifier: MIT
3
2
  // Copyright (c) vis.gl contributors
3
+ // loaders.gl
4
4
 
5
5
  export {NetCDFFormat} from './netcdf/netcdf-format';
6
6
  export {NetCDFLoader, NetCDFWorkerLoader} from './netcdf/netcdf-loader';
@@ -65,3 +65,9 @@ export {NPYLoader, NPYWorkerLoader} from './npy-loader-types';
65
65
  export type {NPYLoaderOptions} from './npy-loader-types';
66
66
  export type {NPYTile} from './lib/parsers/parse-npy';
67
67
  export {NPYFormat} from './npy-format';
68
+
69
+ export {NRRDLoader} from './nrrd-loader-types';
70
+ export type {NRRDData, NRRDArray} from './nrrd/parse-nrrd';
71
+
72
+ export {VTKLoader} from './vtk-loader-types';
73
+ export type {VTKDataset, VTKDataArray, VTKXMLDataset, VTKXMLPiece, VTKXMLArray} from './vtk';
@@ -0,0 +1,267 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) vis.gl contributors
3
+ /** Numeric storage supported by the initial NRRD decoder. */
4
+ export type NRRDArray =
5
+ | Int8Array
6
+ | Uint8Array
7
+ | Int16Array
8
+ | Uint16Array
9
+ | Int32Array
10
+ | Uint32Array
11
+ | Float32Array
12
+ | Float64Array;
13
+ /** Multidimensional NRRD data, preserving file axis order and authored orientation. */
14
+ export type NRRDData = {
15
+ /** Decoded scalar samples. Axis zero varies fastest. */
16
+ data: NRRDArray;
17
+ /** Number of samples on each axis. */
18
+ shape: number[];
19
+ /** Original normalized header fields. */
20
+ fields: Record<string, string>;
21
+ /** Authored custom key/value metadata. */
22
+ metadata: Record<string, string>;
23
+ /** World-space coordinate of the first sample, if authored. */
24
+ origin?: number[];
25
+ /** World-space step vector per array axis; null represents a nonspatial axis. */
26
+ directions?: (number[] | null)[];
27
+ /** Axis kinds, when supplied. */
28
+ kinds?: string[];
29
+ };
30
+
31
+ /** Descriptor for one supported NRRD scalar type. */
32
+ type ScalarType = {
33
+ /** Typed-array constructor. */
34
+ construct: (length: number) => NRRDArray;
35
+ /** Bytes per sample. */
36
+ bytes: number;
37
+ /** Reads a sample with the specified byte order. */
38
+ read: (view: DataView, offset: number, little: boolean) => number;
39
+ /** Whether integer range validation is required. */
40
+ integer: boolean;
41
+ };
42
+
43
+ const TYPES: [string[], ScalarType][] = [
44
+ [
45
+ ['signed char', 'int8', 'int8_t'],
46
+ {
47
+ construct: count => new Int8Array(count),
48
+ bytes: 1,
49
+ read: (view, offset) => view.getInt8(offset),
50
+ integer: true
51
+ }
52
+ ],
53
+ [
54
+ ['uchar', 'unsigned char', 'uint8', 'uint8_t'],
55
+ {
56
+ construct: count => new Uint8Array(count),
57
+ bytes: 1,
58
+ read: (view, offset) => view.getUint8(offset),
59
+ integer: true
60
+ }
61
+ ],
62
+ [
63
+ ['short', 'short int', 'signed short', 'signed short int', 'int16', 'int16_t'],
64
+ {
65
+ construct: count => new Int16Array(count),
66
+ bytes: 2,
67
+ read: (view, offset, little) => view.getInt16(offset, little),
68
+ integer: true
69
+ }
70
+ ],
71
+ [
72
+ ['ushort', 'unsigned short', 'unsigned short int', 'uint16', 'uint16_t'],
73
+ {
74
+ construct: count => new Uint16Array(count),
75
+ bytes: 2,
76
+ read: (view, offset, little) => view.getUint16(offset, little),
77
+ integer: true
78
+ }
79
+ ],
80
+ [
81
+ ['int', 'signed int', 'int32', 'int32_t'],
82
+ {
83
+ construct: count => new Int32Array(count),
84
+ bytes: 4,
85
+ read: (view, offset, little) => view.getInt32(offset, little),
86
+ integer: true
87
+ }
88
+ ],
89
+ [
90
+ ['uint', 'unsigned int', 'uint32', 'uint32_t'],
91
+ {
92
+ construct: count => new Uint32Array(count),
93
+ bytes: 4,
94
+ read: (view, offset, little) => view.getUint32(offset, little),
95
+ integer: true
96
+ }
97
+ ],
98
+ [
99
+ ['float'],
100
+ {
101
+ construct: count => new Float32Array(count),
102
+ bytes: 4,
103
+ read: (view, offset, little) => view.getFloat32(offset, little),
104
+ integer: false
105
+ }
106
+ ],
107
+ [
108
+ ['double'],
109
+ {
110
+ construct: count => new Float64Array(count),
111
+ bytes: 8,
112
+ read: (view, offset, little) => view.getFloat64(offset, little),
113
+ integer: false
114
+ }
115
+ ]
116
+ ];
117
+
118
+ /** Decodes attached raw, ASCII or gzip NRRD without interpreting spatial axes as colors. */
119
+ export async function parseNRRD(input: ArrayBuffer): Promise<NRRDData> {
120
+ const bytes = new Uint8Array(input);
121
+ let headerEnd = -1;
122
+ for (let index = 0; index < Math.min(bytes.length, 1024 * 1024); index++) {
123
+ if (bytes[index] === 10 && bytes[index + 1] === 10) {
124
+ headerEnd = index + 2;
125
+ break;
126
+ }
127
+ if (
128
+ bytes[index] === 13 &&
129
+ bytes[index + 1] === 10 &&
130
+ bytes[index + 2] === 13 &&
131
+ bytes[index + 3] === 10
132
+ ) {
133
+ headerEnd = index + 4;
134
+ break;
135
+ }
136
+ }
137
+ if (headerEnd < 0) throw new Error('NRRD: missing or oversized attached header');
138
+ const lines = new TextDecoder().decode(bytes.subarray(0, headerEnd)).split(/\r?\n/);
139
+ if (!/^NRRD000[1-5]$/.test(lines.shift() || '')) throw new Error('NRRD: unsupported signature');
140
+ const fields: Record<string, string> = Object.create(null);
141
+ const metadata: Record<string, string> = Object.create(null);
142
+ for (const line of lines) {
143
+ if (!line || line.startsWith('#')) continue;
144
+ const separator = line.indexOf(':');
145
+ if (separator <= 0) throw new Error('NRRD: malformed header field');
146
+ const name = line.slice(0, separator);
147
+ if (line[separator + 1] === '=') {
148
+ metadata[name] = line.slice(separator + 2);
149
+ continue;
150
+ }
151
+ const key = name.toLowerCase();
152
+ if (key in fields) throw new Error('NRRD: duplicate header field');
153
+ fields[key] = line.slice(separator + 1).trim();
154
+ }
155
+ if (
156
+ 'data file' in fields ||
157
+ 'datafile' in fields ||
158
+ Number(fields['byte skip'] || 0) !== 0 ||
159
+ Number(fields.byteskip || 0) !== 0 ||
160
+ Number(fields['line skip'] || 0) !== 0 ||
161
+ Number(fields.lineskip || 0) !== 0
162
+ )
163
+ throw new Error('NRRD: detached data and skip directives are unsupported');
164
+ const dimension = Number(fields.dimension);
165
+ const shape = (fields.sizes || '').split(/\s+/).map(Number);
166
+ if (
167
+ !Number.isInteger(dimension) ||
168
+ dimension < 1 ||
169
+ dimension > 16 ||
170
+ shape.length !== dimension ||
171
+ shape.some(size => !Number.isSafeInteger(size) || size < 1)
172
+ )
173
+ throw new Error('NRRD: invalid dimensions');
174
+ const scalar = TYPES.find(([names]) => names.includes((fields.type || '').toLowerCase()))?.[1];
175
+ if (!scalar) throw new Error('NRRD: unsupported scalar type');
176
+ const count = shape.reduce((product, size) => product * size, 1);
177
+ const byteLength = count * scalar.bytes;
178
+ if (!Number.isSafeInteger(byteLength) || byteLength > 256 * 1024 * 1024)
179
+ throw new Error('NRRD: decoded size exceeds 256 MiB limit');
180
+ const encoding = (fields.encoding || '').toLowerCase();
181
+ const ascii = ['ascii', 'text', 'txt'].includes(encoding);
182
+ if (
183
+ !ascii &&
184
+ scalar.bytes > 1 &&
185
+ !['little', 'big'].includes((fields.endian || '').toLowerCase())
186
+ )
187
+ throw new Error('NRRD: binary multibyte data requires endian');
188
+ let payload = bytes.slice(headerEnd);
189
+ if (encoding === 'gzip' || encoding === 'gz') payload = await decompressGzip(payload, byteLength);
190
+ else if (!ascii && encoding !== 'raw') throw new Error('NRRD: unsupported encoding');
191
+ const data = scalar.construct(count);
192
+ if (ascii) {
193
+ const tokens = new TextDecoder().decode(payload).trim().split(/\s+/);
194
+ if (tokens.length !== count) throw new Error('NRRD: ASCII sample count mismatch');
195
+ for (let index = 0; index < count; index++) {
196
+ if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(tokens[index]))
197
+ throw new Error('NRRD: invalid ASCII sample');
198
+ const value = Number(tokens[index]);
199
+ data[index] = value;
200
+ if (!Number.isFinite(data[index]) || (scalar.integer && data[index] !== value))
201
+ throw new Error('NRRD: scalar value out of range');
202
+ }
203
+ } else {
204
+ if (payload.byteLength !== byteLength) throw new Error('NRRD: payload size mismatch');
205
+ const view = new DataView(payload.buffer, payload.byteOffset, payload.byteLength);
206
+ for (let index = 0; index < count; index++)
207
+ data[index] = scalar.read(view, index * scalar.bytes, fields.endian?.toLowerCase() !== 'big');
208
+ }
209
+ const origin = fields['space origin'] ? parseVector(fields['space origin']) : undefined;
210
+ const directions = fields['space directions']
211
+ ?.match(/none|\([^()]*\)/gi)
212
+ ?.map(value => (value.toLowerCase() === 'none' ? null : parseVector(value)));
213
+ if (fields['space directions']) {
214
+ const residue = fields['space directions'].replace(/none|\([^()]*\)/gi, '').trim();
215
+ if (!directions || directions.length !== dimension || residue)
216
+ throw new Error('NRRD: invalid space directions');
217
+ }
218
+ const spatialDimension =
219
+ origin?.length || Number(fields['space dimension']) || directions?.find(value => value)?.length;
220
+ if (directions?.some(value => value && value.length !== spatialDimension))
221
+ throw new Error('NRRD: inconsistent spatial dimension');
222
+ const kinds = fields.kinds?.split(/\s+/);
223
+ if (kinds && kinds.length !== dimension) throw new Error('NRRD: invalid kinds count');
224
+ return {data, shape, fields, metadata, origin, directions, kinds};
225
+ }
226
+
227
+ /** Parses one finite world-space vector. */
228
+ function parseVector(text: string): number[] {
229
+ if (!/^\([^()]+\)$/.test(text.trim())) throw new Error('NRRD: invalid spatial vector');
230
+ const values = text
231
+ .trim()
232
+ .slice(1, -1)
233
+ .split(',')
234
+ .map(value => {
235
+ if (!value.trim()) throw new Error('NRRD: empty spatial vector component');
236
+ return Number(value.trim());
237
+ });
238
+ if (!values.length || values.some(value => !Number.isFinite(value)))
239
+ throw new Error('NRRD: invalid spatial vector');
240
+ return values;
241
+ }
242
+
243
+ /** Decompresses gzip incrementally, enforcing the declared decoded length. */
244
+ async function decompressGzip(
245
+ payload: Uint8Array<ArrayBuffer>,
246
+ expectedLength: number
247
+ ): Promise<Uint8Array<ArrayBuffer>> {
248
+ const stream = new Blob([payload]).stream().pipeThrough(new DecompressionStream('gzip'));
249
+ const reader = stream.getReader();
250
+ const result = new Uint8Array(expectedLength);
251
+ let offset = 0;
252
+ try {
253
+ while (true) {
254
+ const {done, value} = await reader.read();
255
+ if (done) break;
256
+ if (offset + value.length > expectedLength)
257
+ throw new Error('NRRD: gzip exceeds declared size');
258
+ result.set(value, offset);
259
+ offset += value.length;
260
+ }
261
+ } finally {
262
+ await reader.cancel();
263
+ reader.releaseLock();
264
+ }
265
+ if (offset !== expectedLength) throw new Error('NRRD: gzip sample count mismatch');
266
+ return result;
267
+ }
@@ -0,0 +1,25 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) vis.gl contributors
3
+ /** Package version injected by the build. */
4
+ // @ts-ignore __VERSION__ is injected during compilation.
5
+ const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
6
+ import type {NRRDData} from './nrrd/parse-nrrd';
7
+ /** Loads the attached NRRD parser on demand. */
8
+ async function preload() {
9
+ const {NRRDLoaderWithParser} = await import('@loaders.gl/scientific/nrrd-loader');
10
+ return NRRDLoaderWithParser;
11
+ }
12
+ /** Metadata-only attached NRRD array loader. */
13
+ export const NRRDLoader = {
14
+ name: 'NRRD',
15
+ id: 'nrrd',
16
+ module: 'scientific',
17
+ version: VERSION,
18
+ extensions: ['nrrd'],
19
+ mimeTypes: ['application/x-nrrd'],
20
+ binary: true,
21
+ dataType: null as unknown as NRRDData,
22
+ batchType: null as never,
23
+ options: {},
24
+ preload
25
+ } as const;
@@ -0,0 +1,7 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) vis.gl contributors
3
+ import {NRRDLoader} from './nrrd-loader-types';
4
+ import {parseNRRD} from './nrrd/parse-nrrd';
5
+ const {preload: _preload, ...metadata} = NRRDLoader;
6
+ /** Parser-bearing attached NRRD array loader. */
7
+ export const NRRDLoaderWithParser = {...metadata, parse: parseNRRD} as const;
package/src/vtk/vtk.ts CHANGED
@@ -1,6 +1,6 @@
1
- // loaders.gl
2
1
  // SPDX-License-Identifier: MIT
3
2
  // Copyright (c) vis.gl contributors
3
+ // loaders.gl
4
4
 
5
5
  /** Parsed legacy VTK ASCII numeric attribute. */
6
6
  export type VTKDataArray = {
@@ -59,6 +59,7 @@ function numbers(tokens: string[], index: number, count: number): [Float32Array,
59
59
  const value = Number(tokens[index + i]);
60
60
  requireVTK(Number.isFinite(value), 'invalid numeric value');
61
61
  data[i] = value;
62
+ requireVTK(Number.isFinite(data[i]), 'float32 overflow');
62
63
  }
63
64
  return [data, index + count];
64
65
  }
@@ -187,5 +188,31 @@ export function parseVTKLegacy(input: string): VTKDataset {
187
188
  throw new Error('VTK: unsupported keyword ' + keyword);
188
189
  }
189
190
  }
191
+ if (result.cells) {
192
+ requireVTK(result.points, 'cells require points');
193
+ const pointCount = result.points.length / 3;
194
+ requireVTK(
195
+ result.cells.connectivity.every(index => index < pointCount),
196
+ 'vertex index out of range'
197
+ );
198
+ }
199
+ const pointCount = result.points
200
+ ? result.points.length / 3
201
+ : result.dimensions?.reduce((product, dimension) => product * dimension, 1);
202
+ if (pointCount !== undefined) {
203
+ requireVTK(
204
+ result.pointData.every(array => array.data.length / array.components === pointCount),
205
+ 'point attribute count mismatch'
206
+ );
207
+ }
208
+ const cellCount = result.cells
209
+ ? result.cells.offsets.length - 1
210
+ : result.dimensions?.reduce((product, dimension) => product * Math.max(dimension - 1, 1), 1);
211
+ if (cellCount !== undefined) {
212
+ requireVTK(
213
+ result.cellData.every(array => array.data.length / array.components === cellCount),
214
+ 'cell attribute count mismatch'
215
+ );
216
+ }
190
217
  return result;
191
218
  }
@@ -0,0 +1,25 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) vis.gl contributors
3
+ /** Package version injected by the build. */
4
+ // @ts-ignore __VERSION__ is injected during compilation.
5
+ const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
6
+ import type {VTKDataset, VTKXMLDataset} from './vtk';
7
+ /** Loads the existing VTK readers on demand. */
8
+ async function preload() {
9
+ const {VTKLoaderWithParser} = await import('@loaders.gl/scientific/vtk-loader');
10
+ return VTKLoaderWithParser;
11
+ }
12
+ /** Metadata-only loader for the supported legacy ASCII and XML VTK subsets. */
13
+ export const VTKLoader = {
14
+ name: 'VTK',
15
+ id: 'vtk',
16
+ module: 'scientific',
17
+ version: VERSION,
18
+ extensions: ['vtk', 'vti', 'vtu', 'vtp', 'vtr', 'vts'],
19
+ mimeTypes: ['application/vnd.vtk'],
20
+ text: true,
21
+ dataType: null as unknown as VTKDataset | VTKXMLDataset,
22
+ batchType: null as never,
23
+ options: {},
24
+ preload
25
+ } as const;
@@ -0,0 +1,19 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) vis.gl contributors
3
+ import {VTKLoader} from './vtk-loader-types';
4
+ import {parseVTKLegacy, parseVTKXML} from './vtk/index.js';
5
+ const {preload: _preload, ...metadata} = VTKLoader;
6
+ /** Selects the existing VTK parser without changing its dataset representation. */
7
+ function parseTextSync(text: string) {
8
+ const source = text.trimStart();
9
+ if (source.startsWith('<')) return parseVTKXML(source);
10
+ return parseVTKLegacy(source);
11
+ }
12
+ /** Parser-bearing VTK loader retaining scientific topology and attribute associations. */
13
+ export const VTKLoaderWithParser = {
14
+ ...metadata,
15
+ parseTextSync,
16
+ parseText: async (text: string) => parseTextSync(text),
17
+ parseSync: (data: ArrayBuffer) => parseTextSync(new TextDecoder().decode(data)),
18
+ parse: async (data: ArrayBuffer) => parseTextSync(new TextDecoder().decode(data))
19
+ } as const;