@margoengine/loaders 0.0.0-stage → 1.0.0
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 +201 -0
- package/NOTICE +4 -0
- package/README.md +1665 -2
- package/dist/3dm.d.ts +161 -0
- package/dist/3dm.js +289 -0
- package/dist/3ds.d.ts +124 -0
- package/dist/3ds.js +392 -0
- package/dist/acclaim.d.ts +90 -0
- package/dist/acclaim.js +312 -0
- package/dist/amf.d.ts +45 -0
- package/dist/amf.js +256 -0
- package/dist/babylon.d.ts +197 -0
- package/dist/babylon.js +701 -0
- package/dist/common.d.ts +7 -0
- package/dist/common.js +6 -0
- package/dist/crease.d.ts +7 -0
- package/dist/crease.js +67 -0
- package/dist/dae-animation.d.ts +66 -0
- package/dist/dae-animation.js +266 -0
- package/dist/dae-common.d.ts +95 -0
- package/dist/dae-common.js +226 -0
- package/dist/dae-controllers.d.ts +57 -0
- package/dist/dae-controllers.js +164 -0
- package/dist/dae-kinematics.d.ts +65 -0
- package/dist/dae-kinematics.js +461 -0
- package/dist/dae-pose.d.ts +131 -0
- package/dist/dae-pose.js +381 -0
- package/dist/dae-scene.d.ts +203 -0
- package/dist/dae-scene.js +279 -0
- package/dist/dae.d.ts +166 -0
- package/dist/dae.js +644 -0
- package/dist/detect.d.ts +20 -0
- package/dist/detect.js +72 -0
- package/dist/dxf-hatch.d.ts +8 -0
- package/dist/dxf-hatch.js +346 -0
- package/dist/dxf-leaders.d.ts +59 -0
- package/dist/dxf-leaders.js +258 -0
- package/dist/dxf.d.ts +122 -0
- package/dist/dxf.js +985 -0
- package/dist/engine.d.ts +41 -0
- package/dist/engine.js +120 -0
- package/dist/font-opentype.d.ts +14 -0
- package/dist/font-opentype.js +33 -0
- package/dist/font-woff.d.ts +23 -0
- package/dist/font-woff.js +293 -0
- package/dist/font.d.ts +41 -0
- package/dist/font.js +26 -0
- package/dist/gcode.d.ts +92 -0
- package/dist/gcode.js +369 -0
- package/dist/geo-model.d.ts +84 -0
- package/dist/geo-model.js +220 -0
- package/dist/geo-mvt.d.ts +15 -0
- package/dist/geo-mvt.js +302 -0
- package/dist/geo-shp.d.ts +15 -0
- package/dist/geo-shp.js +234 -0
- package/dist/geo-xml.d.ts +6 -0
- package/dist/geo-xml.js +165 -0
- package/dist/geo.d.ts +8 -0
- package/dist/geo.js +59 -0
- package/dist/geometry.d.ts +60 -0
- package/dist/geometry.js +273 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +28 -0
- package/dist/kmz.d.ts +78 -0
- package/dist/kmz.js +173 -0
- package/dist/las.d.ts +92 -0
- package/dist/las.js +110 -0
- package/dist/ldraw.d.ts +118 -0
- package/dist/ldraw.js +474 -0
- package/dist/load.d.ts +72 -0
- package/dist/load.js +205 -0
- package/dist/lottie.d.ts +154 -0
- package/dist/lottie.js +893 -0
- package/dist/lwo.d.ts +81 -0
- package/dist/lwo.js +454 -0
- package/dist/lzf.d.ts +13 -0
- package/dist/lzf.js +56 -0
- package/dist/materialx.d.ts +120 -0
- package/dist/materialx.js +563 -0
- package/dist/md2.d.ts +52 -0
- package/dist/md2.js +112 -0
- package/dist/mmcif.d.ts +14 -0
- package/dist/mmcif.js +246 -0
- package/dist/mmd.d.ts +244 -0
- package/dist/mmd.js +958 -0
- package/dist/mocap.d.ts +71 -0
- package/dist/mocap.js +178 -0
- package/dist/molecule-common.d.ts +4 -0
- package/dist/molecule-common.js +54 -0
- package/dist/mtl.d.ts +125 -0
- package/dist/mtl.js +550 -0
- package/dist/normals.d.ts +7 -0
- package/dist/normals.js +31 -0
- package/dist/nrrd.d.ts +70 -0
- package/dist/nrrd.js +785 -0
- package/dist/nurbs.d.ts +118 -0
- package/dist/nurbs.js +265 -0
- package/dist/obj.d.ts +145 -0
- package/dist/obj.js +462 -0
- package/dist/off.d.ts +10 -0
- package/dist/off.js +155 -0
- package/dist/outline-font.d.ts +87 -0
- package/dist/outline-font.js +110 -0
- package/dist/outline.d.ts +20 -0
- package/dist/outline.js +215 -0
- package/dist/pcd.d.ts +34 -0
- package/dist/pcd.js +258 -0
- package/dist/pdb.d.ts +67 -0
- package/dist/pdb.js +126 -0
- package/dist/ply.d.ts +35 -0
- package/dist/ply.js +392 -0
- package/dist/polygon.d.ts +9 -0
- package/dist/polygon.js +49 -0
- package/dist/prwm.d.ts +24 -0
- package/dist/prwm.js +114 -0
- package/dist/render.d.ts +32 -0
- package/dist/render.js +181 -0
- package/dist/sfnt-cff.d.ts +37 -0
- package/dist/sfnt-cff.js +749 -0
- package/dist/sfnt-layout.d.ts +24 -0
- package/dist/sfnt-layout.js +266 -0
- package/dist/sfnt-reader.d.ts +28 -0
- package/dist/sfnt-reader.js +87 -0
- package/dist/sfnt-variation.d.ts +40 -0
- package/dist/sfnt-variation.js +318 -0
- package/dist/shared/asset-url.js +82 -0
- package/dist/shared/crc32.d.ts +2 -0
- package/dist/shared/crc32.js +47 -0
- package/dist/shared/errors.js +36 -0
- package/dist/shared/inflate.js +284 -0
- package/dist/shared/normals.js +26 -0
- package/dist/shared/scene-scope.js +56 -0
- package/dist/shared/trailing-data.js +45 -0
- package/dist/stl.d.ts +64 -0
- package/dist/stl.js +309 -0
- package/dist/svg.d.ts +90 -0
- package/dist/svg.js +1007 -0
- package/dist/tds.d.ts +2 -0
- package/dist/tds.js +2 -0
- package/dist/textures.d.ts +42 -0
- package/dist/textures.js +53 -0
- package/dist/threemf-beam-reader.d.ts +19 -0
- package/dist/threemf-beam-reader.js +165 -0
- package/dist/threemf-beams.d.ts +52 -0
- package/dist/threemf-beams.js +268 -0
- package/dist/threemf-boolean-read.d.ts +10 -0
- package/dist/threemf-boolean-read.js +113 -0
- package/dist/threemf-boolean.d.ts +59 -0
- package/dist/threemf-boolean.js +20 -0
- package/dist/threemf-csg.d.ts +14 -0
- package/dist/threemf-csg.js +193 -0
- package/dist/threemf-displacement-read.d.ts +8 -0
- package/dist/threemf-displacement-read.js +213 -0
- package/dist/threemf-displacement.d.ts +71 -0
- package/dist/threemf-displacement.js +18 -0
- package/dist/threemf-evaluate.d.ts +36 -0
- package/dist/threemf-evaluate.js +86 -0
- package/dist/threemf-functions.d.ts +33 -0
- package/dist/threemf-functions.js +359 -0
- package/dist/threemf-lattice-mesh.d.ts +9 -0
- package/dist/threemf-lattice-mesh.js +115 -0
- package/dist/threemf-lattice.d.ts +139 -0
- package/dist/threemf-lattice.js +379 -0
- package/dist/threemf-levelset.d.ts +23 -0
- package/dist/threemf-levelset.js +105 -0
- package/dist/threemf-materials.d.ts +162 -0
- package/dist/threemf-materials.js +497 -0
- package/dist/threemf-mesh.d.ts +75 -0
- package/dist/threemf-mesh.js +422 -0
- package/dist/threemf-secure.d.ts +50 -0
- package/dist/threemf-secure.js +153 -0
- package/dist/threemf-tessellate.d.ts +37 -0
- package/dist/threemf-tessellate.js +277 -0
- package/dist/threemf-volume-colour.d.ts +25 -0
- package/dist/threemf-volume-colour.js +233 -0
- package/dist/threemf-volume-sample.d.ts +8 -0
- package/dist/threemf-volume-sample.js +65 -0
- package/dist/threemf-volumetric-read.d.ts +15 -0
- package/dist/threemf-volumetric-read.js +539 -0
- package/dist/threemf-volumetric.d.ts +177 -0
- package/dist/threemf-volumetric.js +28 -0
- package/dist/threemf.d.ts +170 -0
- package/dist/threemf.js +609 -0
- package/dist/tilt.d.ts +62 -0
- package/dist/tilt.js +186 -0
- package/dist/tmx.d.ts +151 -0
- package/dist/tmx.js +394 -0
- package/dist/ttf.d.ts +78 -0
- package/dist/ttf.js +568 -0
- package/dist/types.d.ts +108 -0
- package/dist/types.js +35 -0
- package/dist/usd.d.ts +2 -0
- package/dist/usd.js +2 -0
- package/dist/usd.worker.d.ts +1 -0
- package/dist/usdz.d.ts +50 -0
- package/dist/usdz.js +646 -0
- package/dist/util.d.ts +75 -0
- package/dist/util.js +241 -0
- package/dist/vdb.d.ts +111 -0
- package/dist/vdb.js +663 -0
- package/dist/vrml.d.ts +53 -0
- package/dist/vrml.js +532 -0
- package/dist/vtk-cells.d.ts +29 -0
- package/dist/vtk-cells.js +211 -0
- package/dist/vtk-legacy.d.ts +57 -0
- package/dist/vtk-legacy.js +439 -0
- package/dist/vtk-reader.d.ts +19 -0
- package/dist/vtk-reader.js +134 -0
- package/dist/vtk-xml.d.ts +96 -0
- package/dist/vtk-xml.js +578 -0
- package/dist/vtk.d.ts +13 -0
- package/dist/vtk.js +234 -0
- package/dist/woff-glyf.d.ts +17 -0
- package/dist/woff-glyf.js +215 -0
- package/dist/worker.d.ts +10 -0
- package/dist/worker.js +93 -0
- package/dist/workers/usd.js +3 -0
- package/dist/xfile.d.ts +99 -0
- package/dist/xfile.js +706 -0
- package/dist/xml.d.ts +107 -0
- package/dist/xml.js +843 -0
- package/dist/xyz.d.ts +16 -0
- package/dist/xyz.js +110 -0
- package/dist/zip.d.ts +38 -0
- package/dist/zip.js +150 -0
- package/package.json +289 -4
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import { LoaderError } from './types.js';
|
|
2
|
+
import { limits, toBytes, throwIfAborted, uints } from './util.js';
|
|
3
|
+
import { Reader, integerOf } from './vtk-reader.js';
|
|
4
|
+
import { Builder, gridCell } from './vtk-cells.js';
|
|
5
|
+
const F = 'VTK legacy';
|
|
6
|
+
const fixedCorners = {
|
|
7
|
+
1: 1,
|
|
8
|
+
3: 2,
|
|
9
|
+
5: 3,
|
|
10
|
+
8: 4,
|
|
11
|
+
9: 4,
|
|
12
|
+
10: 4,
|
|
13
|
+
11: 8,
|
|
14
|
+
12: 8,
|
|
15
|
+
13: 6,
|
|
16
|
+
14: 5,
|
|
17
|
+
15: 10,
|
|
18
|
+
16: 12,
|
|
19
|
+
21: 3,
|
|
20
|
+
22: 6,
|
|
21
|
+
23: 8,
|
|
22
|
+
24: 10,
|
|
23
|
+
25: 20,
|
|
24
|
+
};
|
|
25
|
+
/** Reads all five legacy VTK dataset types, ASCII/big endian and pre-5/5.x cell layouts. */
|
|
26
|
+
export function parseVTKLegacy(input, options = {}) {
|
|
27
|
+
throwIfAborted(options.signal, F);
|
|
28
|
+
const bytes = toBytes(input, F), limit = limits(options, F);
|
|
29
|
+
const maxCells = options.maxCells ?? limit.maxTriangles;
|
|
30
|
+
if (!Number.isSafeInteger(maxCells) || maxCells < 0)
|
|
31
|
+
throw new LoaderError(F, 'limit', 'maxCells must be a nonnegative safe integer');
|
|
32
|
+
if (options.surface !== undefined && !['outer', 'all'].includes(options.surface))
|
|
33
|
+
throw new LoaderError(F, 'range', 'surface must be outer or all');
|
|
34
|
+
const reader = new Reader(bytes, false, Math.max(limit.maxVertices * 9, maxCells * 9));
|
|
35
|
+
const banner = /^# vtk DataFile Version (\d+)\.(\d+)\s*$/.exec(reader.line() ?? '');
|
|
36
|
+
if (!banner)
|
|
37
|
+
throw new LoaderError(F, 'header', 'expected a legacy VTK version banner');
|
|
38
|
+
reader.line();
|
|
39
|
+
const encoding = reader.line()?.trim().toUpperCase();
|
|
40
|
+
if (!['ASCII', 'BINARY'].includes(encoding ?? ''))
|
|
41
|
+
throw new LoaderError(F, 'header', 'third line must be ASCII or BINARY');
|
|
42
|
+
const body = reader.mark();
|
|
43
|
+
const data = new Reader(bytes, encoding === 'BINARY', Math.max(limit.maxVertices * 9, maxCells * 9));
|
|
44
|
+
data.reset(body);
|
|
45
|
+
const dataset = data.keyword();
|
|
46
|
+
const type = dataset?.[1]?.toUpperCase() ?? '';
|
|
47
|
+
if (dataset?.[0]?.toUpperCase() !== 'DATASET' ||
|
|
48
|
+
!['POLYDATA', 'STRUCTURED_POINTS', 'STRUCTURED_GRID', 'RECTILINEAR_GRID', 'UNSTRUCTURED_GRID'].includes(type))
|
|
49
|
+
throw new LoaderError(F, 'unsupported', `unsupported DATASET ${type}`);
|
|
50
|
+
let positions, dimensions;
|
|
51
|
+
let origin = [0, 0, 0], spacing = [1, 1, 1];
|
|
52
|
+
const axes = [];
|
|
53
|
+
const cells = [];
|
|
54
|
+
let pending;
|
|
55
|
+
let section = 'field', tuples = 0;
|
|
56
|
+
let declaredPoints, declaredCells;
|
|
57
|
+
let normalName, uvName, colorName;
|
|
58
|
+
const pointData = Object.create(null), cellData = Object.create(null), fieldData = Object.create(null);
|
|
59
|
+
const count = (token, what, max) => {
|
|
60
|
+
const n = integerOf(token, what);
|
|
61
|
+
if (n > max)
|
|
62
|
+
throw new LoaderError(F, 'limit', `${what} exceeds ${max}`);
|
|
63
|
+
return n;
|
|
64
|
+
};
|
|
65
|
+
const pointTotal = () => (dimensions ? dimensions.reduce((a, b) => a * b, 1) : (positions?.length ?? 0) / 3);
|
|
66
|
+
const add = (cell) => {
|
|
67
|
+
if (cells.length >= maxCells)
|
|
68
|
+
throw new LoaderError(F, 'limit', `source cells exceed ${maxCells}`);
|
|
69
|
+
cells.push(cell);
|
|
70
|
+
};
|
|
71
|
+
const save = (name, components, values, tupleCount = tuples) => {
|
|
72
|
+
if (!Number.isSafeInteger(components) || components < 1 || components > 65536)
|
|
73
|
+
throw new LoaderError(F, 'range', 'array component count must be 1..65536');
|
|
74
|
+
const target = section === 'point' ? pointData : section === 'cell' ? cellData : fieldData;
|
|
75
|
+
if (Object.hasOwn(target, name))
|
|
76
|
+
throw new LoaderError(F, 'syntax', `duplicate array ${name}`);
|
|
77
|
+
if (values.length !== tupleCount * components)
|
|
78
|
+
throw new LoaderError(F, 'range', `array ${name} has the wrong tuple count`);
|
|
79
|
+
target[name] = { components, values };
|
|
80
|
+
};
|
|
81
|
+
const readCells = (tokens) => {
|
|
82
|
+
const number = count(tokens[1], 'cell count', maxCells + 1), size = count(tokens[2], 'connectivity size', maxCells * 65536);
|
|
83
|
+
const mark = data.mark(), next = data.keyword();
|
|
84
|
+
if (next?.[0]?.toUpperCase() === 'OFFSETS') {
|
|
85
|
+
const offsets = data.values(next[1] ?? 'vtktypeint64', number, 'cell offsets');
|
|
86
|
+
const key = data.keyword();
|
|
87
|
+
if (key?.[0]?.toUpperCase() !== 'CONNECTIVITY')
|
|
88
|
+
throw new LoaderError(F, 'syntax', 'expected CONNECTIVITY after OFFSETS');
|
|
89
|
+
const values = data.values(key[1] ?? 'vtktypeint64', size, 'cell connectivity');
|
|
90
|
+
if (offsets.length < 1 || offsets[0] !== 0 || offsets.at(-1) !== values.length)
|
|
91
|
+
throw new LoaderError(F, 'range', 'cell offsets must span all connectivity');
|
|
92
|
+
const result = [];
|
|
93
|
+
for (let c = 0; c + 1 < offsets.length; c++) {
|
|
94
|
+
const a = offsets[c], b = offsets[c + 1];
|
|
95
|
+
if (!Number.isSafeInteger(a) || !Number.isSafeInteger(b) || a < 0 || b < a || b > size || b - a > 65536)
|
|
96
|
+
throw new LoaderError(F, 'range', 'invalid cell offsets');
|
|
97
|
+
result.push(Array.from(values.subarray(a, b)));
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
data.reset(mark);
|
|
102
|
+
const values = data.values('int', size, 'cell connectivity'), result = [];
|
|
103
|
+
let at = 0;
|
|
104
|
+
for (let c = 0; c < number; c++) {
|
|
105
|
+
const n = values[at++];
|
|
106
|
+
if (!Number.isSafeInteger(n) || n < 0 || n > 65536 || at + n > size)
|
|
107
|
+
throw new LoaderError(F, 'range', 'invalid cell point count');
|
|
108
|
+
result.push(Array.from(values.subarray(at, at + n)));
|
|
109
|
+
at += n;
|
|
110
|
+
}
|
|
111
|
+
if (at !== size)
|
|
112
|
+
throw new LoaderError(F, 'range', 'cell size does not match connectivity');
|
|
113
|
+
return result;
|
|
114
|
+
};
|
|
115
|
+
for (let tokens = data.keyword(); tokens; tokens = data.keyword()) {
|
|
116
|
+
throwIfAborted(options.signal, F);
|
|
117
|
+
const key = tokens[0].toUpperCase(), what = tokens.join(' ');
|
|
118
|
+
if (key === 'DIMENSIONS') {
|
|
119
|
+
if (dimensions)
|
|
120
|
+
throw new LoaderError(F, 'syntax', 'duplicate DIMENSIONS');
|
|
121
|
+
dimensions = [
|
|
122
|
+
count(tokens[1], 'x dimension', limit.maxVertices),
|
|
123
|
+
count(tokens[2], 'y dimension', limit.maxVertices),
|
|
124
|
+
count(tokens[3], 'z dimension', limit.maxVertices),
|
|
125
|
+
];
|
|
126
|
+
if (dimensions.some((n) => n < 1) || pointTotal() > limit.maxVertices)
|
|
127
|
+
throw new LoaderError(F, 'limit', 'DIMENSIONS exceed the vertex limit or contain zero');
|
|
128
|
+
}
|
|
129
|
+
else if (key === 'ORIGIN' || key === 'SPACING' || key === 'ASPECT_RATIO') {
|
|
130
|
+
const values = tokens.slice(1).map(Number);
|
|
131
|
+
if (values.length !== 3 ||
|
|
132
|
+
values.some((v) => !Number.isFinite(v)) ||
|
|
133
|
+
(key !== 'ORIGIN' && values.some((v) => v <= 0)))
|
|
134
|
+
throw new LoaderError(F, 'range', `invalid ${key}`);
|
|
135
|
+
if (key === 'ORIGIN')
|
|
136
|
+
origin = values;
|
|
137
|
+
else
|
|
138
|
+
spacing = values;
|
|
139
|
+
}
|
|
140
|
+
else if (key === 'POINTS') {
|
|
141
|
+
if (positions)
|
|
142
|
+
throw new LoaderError(F, 'syntax', 'duplicate POINTS');
|
|
143
|
+
const n = count(tokens[1], 'point count', limit.maxVertices);
|
|
144
|
+
positions = Float32Array.from(data.values(tokens[2] ?? 'float', n * 3, what));
|
|
145
|
+
}
|
|
146
|
+
else if (/^[XYZ]_COORDINATES$/.test(key)) {
|
|
147
|
+
const axis = 'XYZ'.indexOf(key[0]), n = count(tokens[1], 'coordinate count', limit.maxVertices);
|
|
148
|
+
if (axes[axis])
|
|
149
|
+
throw new LoaderError(F, 'syntax', 'duplicate coordinate axis');
|
|
150
|
+
axes[axis] = data.values(tokens[2] ?? 'float', n, what);
|
|
151
|
+
}
|
|
152
|
+
else if (['VERTICES', 'LINES', 'POLYGONS', 'TRIANGLE_STRIPS', 'CELLS'].includes(key)) {
|
|
153
|
+
const rows = readCells(tokens);
|
|
154
|
+
if (key === 'CELLS') {
|
|
155
|
+
if (pending)
|
|
156
|
+
throw new LoaderError(F, 'syntax', 'duplicate CELLS');
|
|
157
|
+
pending = rows;
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
if (type !== 'POLYDATA')
|
|
161
|
+
throw new LoaderError(F, 'syntax', `${key} requires POLYDATA`);
|
|
162
|
+
const code = { VERTICES: 2, LINES: 4, POLYGONS: 7, TRIANGLE_STRIPS: 6 }[key];
|
|
163
|
+
for (const points of rows)
|
|
164
|
+
add({ type: code, points });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else if (key === 'CELL_TYPES') {
|
|
168
|
+
const n = count(tokens[1], 'cell type count', maxCells);
|
|
169
|
+
if (!pending || pending.length !== n)
|
|
170
|
+
throw new LoaderError(F, 'range', 'CELL_TYPES must match CELLS');
|
|
171
|
+
const types = data.values('int', n, what);
|
|
172
|
+
for (let i = 0; i < n; i++)
|
|
173
|
+
add({ type: types[i], points: pending[i] });
|
|
174
|
+
pending = undefined;
|
|
175
|
+
}
|
|
176
|
+
else if (key === 'POINT_DATA' || key === 'CELL_DATA') {
|
|
177
|
+
section = key === 'POINT_DATA' ? 'point' : 'cell';
|
|
178
|
+
tuples = count(tokens[1], `${key} tuples`, section === 'point' ? limit.maxVertices : maxCells);
|
|
179
|
+
if (section === 'point')
|
|
180
|
+
declaredPoints = tuples;
|
|
181
|
+
else
|
|
182
|
+
declaredCells = tuples;
|
|
183
|
+
}
|
|
184
|
+
else if (key === 'SCALARS') {
|
|
185
|
+
if (section === 'field')
|
|
186
|
+
throw new LoaderError(F, 'syntax', 'SCALARS require POINT_DATA or CELL_DATA');
|
|
187
|
+
const components = tokens[3] ? count(tokens[3], 'scalar components', 4) : 1;
|
|
188
|
+
const mark = data.mark();
|
|
189
|
+
if (data.keyword()?.[0]?.toUpperCase() !== 'LOOKUP_TABLE')
|
|
190
|
+
data.reset(mark);
|
|
191
|
+
save(tokens[1], components, data.values(tokens[2] ?? 'float', tuples * components, what));
|
|
192
|
+
}
|
|
193
|
+
else if ([
|
|
194
|
+
'NORMALS',
|
|
195
|
+
'VECTORS',
|
|
196
|
+
'TENSORS',
|
|
197
|
+
'TENSORS6',
|
|
198
|
+
'TEXTURE_COORDINATES',
|
|
199
|
+
'COLOR_SCALARS',
|
|
200
|
+
'GLOBAL_IDS',
|
|
201
|
+
'PEDIGREE_IDS',
|
|
202
|
+
].includes(key)) {
|
|
203
|
+
if (section === 'field')
|
|
204
|
+
throw new LoaderError(F, 'syntax', `${key} requires POINT_DATA or CELL_DATA`);
|
|
205
|
+
const components = key === 'TENSORS'
|
|
206
|
+
? 9
|
|
207
|
+
: key === 'TENSORS6'
|
|
208
|
+
? 6
|
|
209
|
+
: key === 'TEXTURE_COORDINATES' || key === 'COLOR_SCALARS'
|
|
210
|
+
? count(tokens[2], 'array components', 4)
|
|
211
|
+
: key.endsWith('_IDS')
|
|
212
|
+
? 1
|
|
213
|
+
: 3;
|
|
214
|
+
const kind = key === 'COLOR_SCALARS'
|
|
215
|
+
? data.binary
|
|
216
|
+
? 'unsigned_char'
|
|
217
|
+
: 'float'
|
|
218
|
+
: key === 'TEXTURE_COORDINATES'
|
|
219
|
+
? tokens[3]
|
|
220
|
+
: tokens[2];
|
|
221
|
+
const values = data.values(kind ?? 'float', tuples * components, what);
|
|
222
|
+
if (key === 'COLOR_SCALARS' && data.binary)
|
|
223
|
+
for (let i = 0; i < values.length; i++)
|
|
224
|
+
values[i] /= 255;
|
|
225
|
+
save(tokens[1], components, values);
|
|
226
|
+
if (section === 'point') {
|
|
227
|
+
if (key === 'NORMALS')
|
|
228
|
+
normalName = tokens[1];
|
|
229
|
+
if (key === 'TEXTURE_COORDINATES' && components >= 2)
|
|
230
|
+
uvName = tokens[1];
|
|
231
|
+
if (key === 'COLOR_SCALARS' && components >= 3)
|
|
232
|
+
colorName = tokens[1];
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
else if (key === 'FIELD') {
|
|
236
|
+
const n = count(tokens[2], 'field array count', 65536);
|
|
237
|
+
for (let i = 0; i < n; i++) {
|
|
238
|
+
const row = data.keyword();
|
|
239
|
+
if (!row)
|
|
240
|
+
throw new LoaderError(F, 'truncated', 'FIELD ends early');
|
|
241
|
+
const components = count(row[1], 'field components', 65536), nTuples = count(row[2], 'field tuples', Math.max(limit.maxVertices, maxCells));
|
|
242
|
+
const values = data.values(row[3] ?? 'float', components * nTuples, `FIELD ${row[0]}`);
|
|
243
|
+
if (section !== 'field' && nTuples !== tuples)
|
|
244
|
+
fieldData[row[0]] = { components, values };
|
|
245
|
+
else
|
|
246
|
+
save(row[0], components, values, nTuples);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
else if (key === 'LOOKUP_TABLE')
|
|
250
|
+
data.values(data.binary ? 'unsigned_char' : 'float', count(tokens[2], 'lookup table size', 65536) * 4, what);
|
|
251
|
+
else if (key === 'METADATA') {
|
|
252
|
+
for (let line = data.line(); line !== null && line.trim(); line = data.line())
|
|
253
|
+
;
|
|
254
|
+
}
|
|
255
|
+
else
|
|
256
|
+
throw new LoaderError(F, 'unsupported', `unknown legacy keyword ${key}`);
|
|
257
|
+
}
|
|
258
|
+
if (pending)
|
|
259
|
+
throw new LoaderError(F, 'syntax', 'CELLS have no matching CELL_TYPES');
|
|
260
|
+
const structured = ['STRUCTURED_POINTS', 'STRUCTURED_GRID', 'RECTILINEAR_GRID'].includes(type);
|
|
261
|
+
if (structured) {
|
|
262
|
+
if (!dimensions)
|
|
263
|
+
throw new LoaderError(F, 'header', 'structured datasets require DIMENSIONS');
|
|
264
|
+
const [nx, ny, nz] = dimensions;
|
|
265
|
+
if (type === 'STRUCTURED_GRID') {
|
|
266
|
+
if (!positions || positions.length !== pointTotal() * 3)
|
|
267
|
+
throw new LoaderError(F, 'range', 'POINTS must match DIMENSIONS');
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
if ((type === 'RECTILINEAR_GRID' && axes.some((a, i) => !a || a.length !== dimensions[i])) ||
|
|
271
|
+
(type === 'RECTILINEAR_GRID' && axes.length !== 3))
|
|
272
|
+
throw new LoaderError(F, 'range', 'coordinate axes must match DIMENSIONS');
|
|
273
|
+
positions = new Float32Array(pointTotal() * 3);
|
|
274
|
+
let at = 0;
|
|
275
|
+
for (let z = 0; z < nz; z++)
|
|
276
|
+
for (let y = 0; y < ny; y++)
|
|
277
|
+
for (let x = 0; x < nx; x++) {
|
|
278
|
+
if (!(at % 4096))
|
|
279
|
+
throwIfAborted(options.signal, F);
|
|
280
|
+
for (let a = 0; a < 3; a++) {
|
|
281
|
+
const n = [x, y, z][a];
|
|
282
|
+
positions[at++] = type === 'RECTILINEAR_GRID' ? axes[a][n] : origin[a] + spacing[a] * n;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (cells.length)
|
|
287
|
+
throw new LoaderError(F, 'syntax', 'structured datasets must use implicit cells');
|
|
288
|
+
const active = dimensions.map((n, i) => (n > 1 ? i : -1)).filter((i) => i >= 0);
|
|
289
|
+
const id = (xyz) => xyz[0] + nx * (xyz[1] + ny * xyz[2]);
|
|
290
|
+
for (let z = 0; z < Math.max(1, nz - 1); z++)
|
|
291
|
+
for (let y = 0; y < Math.max(1, ny - 1); y++)
|
|
292
|
+
for (let x = 0; x < Math.max(1, nx - 1); x++) {
|
|
293
|
+
if (!(cells.length % 1024))
|
|
294
|
+
throwIfAborted(options.signal, F);
|
|
295
|
+
const base = [x, y, z], p = (bits) => id(base.map((v, a) => v + (bits[active.indexOf(a)] ?? 0)));
|
|
296
|
+
if (active.length === 3)
|
|
297
|
+
add({
|
|
298
|
+
type: 12,
|
|
299
|
+
points: [
|
|
300
|
+
[0, 0, 0],
|
|
301
|
+
[1, 0, 0],
|
|
302
|
+
[1, 1, 0],
|
|
303
|
+
[0, 1, 0],
|
|
304
|
+
[0, 0, 1],
|
|
305
|
+
[1, 0, 1],
|
|
306
|
+
[1, 1, 1],
|
|
307
|
+
[0, 1, 1],
|
|
308
|
+
].map(p),
|
|
309
|
+
});
|
|
310
|
+
else if (active.length === 2)
|
|
311
|
+
add({
|
|
312
|
+
type: 9,
|
|
313
|
+
points: [
|
|
314
|
+
[0, 0],
|
|
315
|
+
[1, 0],
|
|
316
|
+
[1, 1],
|
|
317
|
+
[0, 1],
|
|
318
|
+
].map(p),
|
|
319
|
+
});
|
|
320
|
+
else if (active.length === 1)
|
|
321
|
+
add({ type: 3, points: [[0], [1]].map(p) });
|
|
322
|
+
else
|
|
323
|
+
add({ type: 1, points: [id(base)] });
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
if (!positions)
|
|
327
|
+
throw new LoaderError(F, 'header', 'dataset requires POINTS');
|
|
328
|
+
if (positions.some((v) => !Number.isFinite(v)))
|
|
329
|
+
throw new LoaderError(F, 'range', 'point coordinates must be finite float32 values');
|
|
330
|
+
const points = positions.length / 3;
|
|
331
|
+
if ((declaredPoints !== undefined && declaredPoints !== points) ||
|
|
332
|
+
(declaredCells !== undefined && declaredCells !== cells.length))
|
|
333
|
+
throw new LoaderError(F, 'range', 'data tuple count does not match topology');
|
|
334
|
+
if (type === 'POLYDATA')
|
|
335
|
+
cells.sort((a, b) => [2, 4, 7, 6].indexOf(a.type) - [2, 4, 7, 6].indexOf(b.type));
|
|
336
|
+
const builder = new Builder(limit.maxTriangles, options.surface !== 'all'), connectivity = uints(), offsets = uints(), types = uints(), vertices = uints(), vertexCells = uints(), lineCells = uints();
|
|
337
|
+
builder.coords = positions;
|
|
338
|
+
offsets.push(0);
|
|
339
|
+
for (let c = 0; c < cells.length; c++) {
|
|
340
|
+
if (!(c % 1024))
|
|
341
|
+
throwIfAborted(options.signal, F);
|
|
342
|
+
const cell = cells[c];
|
|
343
|
+
if ((fixedCorners[cell.type] !== undefined && cell.points.length !== fixedCorners[cell.type]) ||
|
|
344
|
+
(cell.type === 2 && !cell.points.length) ||
|
|
345
|
+
(cell.type === 4 && cell.points.length < 2) ||
|
|
346
|
+
((cell.type === 6 || cell.type === 7) && cell.points.length < 3))
|
|
347
|
+
throw new LoaderError(F, 'range', `cell ${c} has the wrong point count for type ${cell.type}`);
|
|
348
|
+
if (!Number.isSafeInteger(cell.type) || cell.type < 0)
|
|
349
|
+
throw new LoaderError(F, 'range', 'cell type must be a nonnegative integer');
|
|
350
|
+
if (cell.type === 42) {
|
|
351
|
+
let at = 1;
|
|
352
|
+
const faces = cell.points[0];
|
|
353
|
+
if (!Number.isSafeInteger(faces) || faces < 4)
|
|
354
|
+
throw new LoaderError(F, 'range', 'polyhedron needs at least four faces');
|
|
355
|
+
for (let f = 0; f < faces; f++) {
|
|
356
|
+
const n = cell.points[at++];
|
|
357
|
+
if (!Number.isSafeInteger(n) || n < 3 || at + n > cell.points.length)
|
|
358
|
+
throw new LoaderError(F, 'range', 'invalid polyhedron face stream');
|
|
359
|
+
const face = cell.points.slice(at, at + n);
|
|
360
|
+
for (const index of face)
|
|
361
|
+
builder.check(index, points);
|
|
362
|
+
builder.face(face, c);
|
|
363
|
+
at += n;
|
|
364
|
+
}
|
|
365
|
+
if (at !== cell.points.length)
|
|
366
|
+
throw new LoaderError(F, 'range', 'polyhedron face stream has trailing values');
|
|
367
|
+
}
|
|
368
|
+
else
|
|
369
|
+
for (const index of cell.points)
|
|
370
|
+
builder.check(index, points);
|
|
371
|
+
for (const index of cell.points)
|
|
372
|
+
connectivity.push(index);
|
|
373
|
+
offsets.push(connectivity.length);
|
|
374
|
+
types.push(cell.type);
|
|
375
|
+
if (cell.type === 1 || cell.type === 2)
|
|
376
|
+
for (const index of cell.points) {
|
|
377
|
+
vertices.push(index);
|
|
378
|
+
vertexCells.push(c);
|
|
379
|
+
}
|
|
380
|
+
const linesBefore = builder.lines.length;
|
|
381
|
+
if (cell.type !== 42)
|
|
382
|
+
gridCell(cell.type, cell.points, c, builder, undefined, 0, points, 0);
|
|
383
|
+
for (let i = linesBefore; i < builder.lines.length; i += 2)
|
|
384
|
+
lineCells.push(c);
|
|
385
|
+
}
|
|
386
|
+
builder.finish();
|
|
387
|
+
const triangles = builder.triangles.result();
|
|
388
|
+
const result = {
|
|
389
|
+
type,
|
|
390
|
+
kind: triangles.length ? 'mesh' : 'points',
|
|
391
|
+
positions,
|
|
392
|
+
offsets: offsets.result(),
|
|
393
|
+
connectivity: connectivity.result(),
|
|
394
|
+
cellTypes: types.result(),
|
|
395
|
+
cellCount: cells.length,
|
|
396
|
+
lines: builder.lines.result(),
|
|
397
|
+
lineCells: lineCells.result(),
|
|
398
|
+
vertices: vertices.result(),
|
|
399
|
+
vertexCells: vertexCells.result(),
|
|
400
|
+
triangleCells: builder.cells.result(),
|
|
401
|
+
pointData,
|
|
402
|
+
cellData,
|
|
403
|
+
fieldData,
|
|
404
|
+
warnings: [...builder.warnings],
|
|
405
|
+
};
|
|
406
|
+
if (triangles.length)
|
|
407
|
+
result.indices = triangles;
|
|
408
|
+
if (dimensions)
|
|
409
|
+
result.dimensions = dimensions;
|
|
410
|
+
if (type === 'STRUCTURED_POINTS') {
|
|
411
|
+
result.origin = origin;
|
|
412
|
+
result.spacing = spacing;
|
|
413
|
+
}
|
|
414
|
+
const attributes = Object.create(null);
|
|
415
|
+
for (const [name, field] of Object.entries(pointData))
|
|
416
|
+
if (field.components === 1)
|
|
417
|
+
attributes[name] = Float32Array.from(field.values);
|
|
418
|
+
if (Object.keys(attributes).length)
|
|
419
|
+
result.attributes = attributes;
|
|
420
|
+
const vertexAttribute = (name, width) => {
|
|
421
|
+
const field = pointData[name], out = new Float32Array(points * width);
|
|
422
|
+
for (let p = 0; p < points; p++)
|
|
423
|
+
for (let c = 0; c < width; c++)
|
|
424
|
+
out[p * width + c] = field.values[p * field.components + c];
|
|
425
|
+
return out;
|
|
426
|
+
};
|
|
427
|
+
if (normalName)
|
|
428
|
+
result.normals = vertexAttribute(normalName, 3);
|
|
429
|
+
if (uvName) {
|
|
430
|
+
// VTK puts t = 0 at the image's bottom (OpenGL's convention); Margo puts v = 0 at the top, as parseVTKXML does.
|
|
431
|
+
const uvs = vertexAttribute(uvName, 2);
|
|
432
|
+
for (let i = 1; i < uvs.length; i += 2)
|
|
433
|
+
uvs[i] = 1 - uvs[i];
|
|
434
|
+
result.uvs = uvs;
|
|
435
|
+
}
|
|
436
|
+
if (colorName)
|
|
437
|
+
result.colors = vertexAttribute(colorName, 3);
|
|
438
|
+
return result;
|
|
439
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Internal shared legacy numeric reader. */
|
|
2
|
+
export declare class Reader {
|
|
3
|
+
readonly bytes: Uint8Array;
|
|
4
|
+
readonly maxValues: number;
|
|
5
|
+
pos: number;
|
|
6
|
+
private scanner?;
|
|
7
|
+
constructor(bytes: Uint8Array, binary: boolean, maxValues: number);
|
|
8
|
+
get binary(): boolean;
|
|
9
|
+
mark(): number;
|
|
10
|
+
reset(mark: number): void;
|
|
11
|
+
/** Reads one raw line; returns null at end of file. */
|
|
12
|
+
line(): string | null;
|
|
13
|
+
/** Next non-blank line, split into tokens. */
|
|
14
|
+
keyword(): string[] | null;
|
|
15
|
+
/** Reads `count` values of `type` (`binary` big-endian, or whitespace separated text). */
|
|
16
|
+
values(type: string, count: number, what: string, forceType?: string): Float64Array;
|
|
17
|
+
}
|
|
18
|
+
/** Internal nonnegative count reader. */
|
|
19
|
+
export declare const integerOf: (token: string | undefined, what: string) => number;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { LoaderError } from './types.js';
|
|
2
|
+
import { latin1, TextScanner } from './util.js';
|
|
3
|
+
const F = 'VTK';
|
|
4
|
+
const SIZES = {
|
|
5
|
+
bit: 0,
|
|
6
|
+
unsigned_char: 1,
|
|
7
|
+
char: 1,
|
|
8
|
+
unsigned_short: 2,
|
|
9
|
+
short: 2,
|
|
10
|
+
unsigned_int: 4,
|
|
11
|
+
int: 4,
|
|
12
|
+
unsigned_long: 8,
|
|
13
|
+
long: 8,
|
|
14
|
+
float: 4,
|
|
15
|
+
double: 8,
|
|
16
|
+
vtktypeint8: 1,
|
|
17
|
+
vtktypeuint8: 1,
|
|
18
|
+
vtktypeint16: 2,
|
|
19
|
+
vtktypeuint16: 2,
|
|
20
|
+
vtktypeint32: 4,
|
|
21
|
+
vtktypeuint32: 4,
|
|
22
|
+
vtktypeint64: 8,
|
|
23
|
+
vtktypeuint64: 8,
|
|
24
|
+
vtkidtype: 8,
|
|
25
|
+
};
|
|
26
|
+
/** Internal shared legacy numeric reader. */
|
|
27
|
+
export class Reader {
|
|
28
|
+
constructor(bytes, binary, maxValues) {
|
|
29
|
+
this.bytes = bytes;
|
|
30
|
+
this.maxValues = maxValues;
|
|
31
|
+
this.pos = 0;
|
|
32
|
+
if (!binary)
|
|
33
|
+
this.scanner = new TextScanner(latin1(bytes), F);
|
|
34
|
+
}
|
|
35
|
+
get binary() {
|
|
36
|
+
return !this.scanner;
|
|
37
|
+
}
|
|
38
|
+
mark() {
|
|
39
|
+
return this.scanner ? this.scanner.pos : this.pos;
|
|
40
|
+
}
|
|
41
|
+
reset(mark) {
|
|
42
|
+
if (this.scanner)
|
|
43
|
+
this.scanner.pos = mark;
|
|
44
|
+
else
|
|
45
|
+
this.pos = mark;
|
|
46
|
+
}
|
|
47
|
+
/** Reads one raw line; returns null at end of file. */
|
|
48
|
+
line() {
|
|
49
|
+
if (this.scanner)
|
|
50
|
+
return this.scanner.line();
|
|
51
|
+
const bytes = this.bytes;
|
|
52
|
+
if (this.pos >= bytes.length)
|
|
53
|
+
return null;
|
|
54
|
+
let end = bytes.indexOf(10, this.pos);
|
|
55
|
+
if (end < 0)
|
|
56
|
+
end = bytes.length;
|
|
57
|
+
const line = latin1(bytes, this.pos, Math.min(end, this.pos + 4096));
|
|
58
|
+
this.pos = end + 1;
|
|
59
|
+
return line.endsWith('\r') ? line.slice(0, -1) : line;
|
|
60
|
+
}
|
|
61
|
+
/** Next non-blank line, split into tokens. */
|
|
62
|
+
keyword() {
|
|
63
|
+
for (;;) {
|
|
64
|
+
const line = this.line();
|
|
65
|
+
if (line === null)
|
|
66
|
+
return null;
|
|
67
|
+
const trimmed = line.trim();
|
|
68
|
+
if (trimmed)
|
|
69
|
+
return trimmed.split(/\s+/);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** Reads `count` values of `type` (`binary` big-endian, or whitespace separated text). */
|
|
73
|
+
values(type, count, what, forceType) {
|
|
74
|
+
const kind = (forceType ?? type).toLowerCase();
|
|
75
|
+
const size = Object.hasOwn(SIZES, kind) ? SIZES[kind] : undefined;
|
|
76
|
+
if (size === undefined)
|
|
77
|
+
throw new LoaderError(F, 'unsupported', `unknown data type "${type.slice(0, 32)}" in ${what}`);
|
|
78
|
+
if (!Number.isSafeInteger(count) || count < 0)
|
|
79
|
+
throw new LoaderError(F, 'header', `invalid value count ${count} in ${what}`);
|
|
80
|
+
if (count > this.maxValues)
|
|
81
|
+
throw new LoaderError(F, 'limit', `${what} has ${count} values, more than the limit of ${this.maxValues}`);
|
|
82
|
+
if (this.scanner) {
|
|
83
|
+
if (count * 2 > this.scanner.text.length - this.scanner.pos + 1)
|
|
84
|
+
throw new LoaderError(F, 'truncated', `file ends inside ${what}`);
|
|
85
|
+
const out = new Float64Array(count);
|
|
86
|
+
for (let i = 0; i < count; i++) {
|
|
87
|
+
out[i] = this.scanner.number(what);
|
|
88
|
+
if (kind !== 'float' && kind !== 'double' && !Number.isSafeInteger(out[i]))
|
|
89
|
+
throw new LoaderError(F, 'range', `${what} integer is outside exact JavaScript precision`);
|
|
90
|
+
if (kind === 'bit' && out[i] !== 0 && out[i] !== 1)
|
|
91
|
+
throw new LoaderError(F, 'range', `${what} bit value must be zero or one`);
|
|
92
|
+
}
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
if (size === 0) {
|
|
96
|
+
const packed = Math.ceil(count / 8);
|
|
97
|
+
if (this.pos + packed > this.bytes.length)
|
|
98
|
+
throw new LoaderError(F, 'truncated', `binary data ends inside ${what}`);
|
|
99
|
+
const out = new Float64Array(count);
|
|
100
|
+
for (let i = 0; i < count; i++)
|
|
101
|
+
out[i] = (this.bytes[this.pos + (i >>> 3)] >>> (7 - (i & 7))) & 1;
|
|
102
|
+
this.pos += packed;
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
if (this.pos + count * size > this.bytes.length)
|
|
106
|
+
throw new LoaderError(F, 'truncated', `binary data ends inside ${what}`);
|
|
107
|
+
const view = new DataView(this.bytes.buffer, this.bytes.byteOffset + this.pos, count * size), out = new Float64Array(count);
|
|
108
|
+
const integer = kind !== 'float' && kind !== 'double', unsigned = kind.startsWith('unsigned') || kind.startsWith('vtktypeuint');
|
|
109
|
+
for (let i = 0, o = 0; i < count; i++, o += size) {
|
|
110
|
+
if (!integer)
|
|
111
|
+
out[i] = size === 4 ? view.getFloat32(o) : view.getFloat64(o);
|
|
112
|
+
else if (size === 1)
|
|
113
|
+
out[i] = unsigned ? view.getUint8(o) : view.getInt8(o);
|
|
114
|
+
else if (size === 2)
|
|
115
|
+
out[i] = unsigned ? view.getUint16(o) : view.getInt16(o);
|
|
116
|
+
else if (size === 4)
|
|
117
|
+
out[i] = unsigned ? view.getUint32(o) : view.getInt32(o);
|
|
118
|
+
else {
|
|
119
|
+
out[i] = Number(unsigned ? view.getBigUint64(o) : view.getBigInt64(o));
|
|
120
|
+
if (!Number.isSafeInteger(out[i]))
|
|
121
|
+
throw new LoaderError(F, 'range', `${what} integer is outside exact JavaScript precision`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
this.pos += count * size;
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/** Internal nonnegative count reader. */
|
|
129
|
+
export const integerOf = (token, what) => {
|
|
130
|
+
const value = Number(token);
|
|
131
|
+
if (token === undefined || !Number.isSafeInteger(value) || value < 0)
|
|
132
|
+
throw new LoaderError(F, 'header', `invalid ${what} "${String(token).slice(0, 32)}"`);
|
|
133
|
+
return value;
|
|
134
|
+
};
|