@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
package/dist/las.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { type LASCrs, type LASPoints, type LASRecord } from '@margoengine/points/las';
|
|
2
|
+
import { type LoadedGeometry, type ParseLimits } from './types.js';
|
|
3
|
+
import { type BinaryInput } from './util.js';
|
|
4
|
+
export type { LASCrs, LASPoints, LASRecord, LASWaveDescriptor, LASWavePackets } from '@margoengine/points/las';
|
|
5
|
+
/** The public header of a LAS file. */
|
|
6
|
+
export interface LASHeader {
|
|
7
|
+
/** `[major, minor]`. */
|
|
8
|
+
version: [number, number];
|
|
9
|
+
pointFormat: number;
|
|
10
|
+
pointRecordLength: number;
|
|
11
|
+
pointCount: number;
|
|
12
|
+
scale: [number, number, number];
|
|
13
|
+
offset: [number, number, number];
|
|
14
|
+
min: [number, number, number];
|
|
15
|
+
max: [number, number, number];
|
|
16
|
+
/** The software that wrote the file. */
|
|
17
|
+
generator: string;
|
|
18
|
+
/** True when the points are LAZ compressed. */
|
|
19
|
+
compressed: boolean;
|
|
20
|
+
}
|
|
21
|
+
/** What a LAZ decompressor is given: the whole file with its header read. */
|
|
22
|
+
export interface LAZSource {
|
|
23
|
+
bytes: Uint8Array;
|
|
24
|
+
header: LASHeader;
|
|
25
|
+
/** Where the compressed points start. */
|
|
26
|
+
pointOffset: number;
|
|
27
|
+
records: LASRecord[];
|
|
28
|
+
}
|
|
29
|
+
/** Options of {@link parseLAS}. `maxVertices` counts points. */
|
|
30
|
+
export interface LASOptions extends ParseLimits {
|
|
31
|
+
/**
|
|
32
|
+
* Turns LAZ points into plain point records, `pointRecordLength` bytes each and `pointCount` of them, in place of the
|
|
33
|
+
* built-in decoder. Only for a decoder of your own: LAZ files open without it.
|
|
34
|
+
*/
|
|
35
|
+
decompress?: (source: LAZSource) => Uint8Array | Promise<Uint8Array>;
|
|
36
|
+
/** Keep every `step`th point. Default 1, all of them. */
|
|
37
|
+
step?: number;
|
|
38
|
+
signal?: AbortSignal;
|
|
39
|
+
}
|
|
40
|
+
/** What {@link parseLAS} returns: a point cloud with the survey's own attributes. */
|
|
41
|
+
export interface LASCloud extends LoadedGeometry {
|
|
42
|
+
kind: 'points';
|
|
43
|
+
header: LASHeader;
|
|
44
|
+
/**
|
|
45
|
+
* The header's minimum corner, or the first point when the header's box misses the points. `positions` hold each point
|
|
46
|
+
* less this, so survey coordinates stay exact in 32 bits.
|
|
47
|
+
*/
|
|
48
|
+
origin: [number, number, number];
|
|
49
|
+
/** Pulse return strength per point, as stored. */
|
|
50
|
+
intensity: Uint16Array;
|
|
51
|
+
/** ASPRS class per point, such as 2 for ground. */
|
|
52
|
+
classification: Uint8Array;
|
|
53
|
+
/** Which return this point is, from 1. */
|
|
54
|
+
returnNumber: Uint8Array;
|
|
55
|
+
/** How many returns its pulse gave. */
|
|
56
|
+
returnCount: Uint8Array;
|
|
57
|
+
/** GPS time per point, in the formats that have it. */
|
|
58
|
+
gpsTime?: Float64Array;
|
|
59
|
+
records: LASRecord[];
|
|
60
|
+
/** Extra bytes attributes, scaled and offset, by name. */
|
|
61
|
+
extra: Readonly<Record<string, Float64Array>>;
|
|
62
|
+
/** The coordinate reference system, when the file names one. */
|
|
63
|
+
crs?: LASCrs;
|
|
64
|
+
/** Everything else the file stores per point: flags, scan angle, user data, source id, channel, NIR, wave packets. */
|
|
65
|
+
points: LASPoints;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Reads an ASPRS LAS or LAZ point cloud, versions 1.0 to 1.5 and point formats 0 to 10 (0 to 5 refused for 1.5, which removed them): positions by the file's scale
|
|
69
|
+
* and offset, intensity, classification, returns, GPS time and colour, with the header and its variable records, extra
|
|
70
|
+
* bytes and the coordinate reference system. Positions are relative to `origin`, because survey coordinates are too
|
|
71
|
+
* large for 32-bit floats; add it back for world coordinates. Colours are scaled to [0,1] from 16 bits, or from 8 for
|
|
72
|
+
* files that fill only the low byte.
|
|
73
|
+
*
|
|
74
|
+
* This is a thin layer over `parseLAS` of `@margoengine/points/las`. A LAZ file opens as it is: the decoder of
|
|
75
|
+
* `@margoengine/points/laz` loads when the first compressed file arrives, so a plain LAS file never pays for it.
|
|
76
|
+
*
|
|
77
|
+
* @param input The file's bytes.
|
|
78
|
+
* @param options The shared size limits, a thinning step, an AbortSignal, and a decoder of your own if you have one.
|
|
79
|
+
* @returns The cloud with its attributes, header and records.
|
|
80
|
+
* @throws LoaderError with code `header`, `truncated`, `range`, `limit`, `aborted` or `unsupported` (an unknown version
|
|
81
|
+
* or point format, or LAZ item version 1 from LASzip 1.0).
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* import { parseLAS } from '@margoengine/loaders/las';
|
|
86
|
+
*
|
|
87
|
+
* const bytes = await (await fetch('/surveys/quarry.laz')).arrayBuffer();
|
|
88
|
+
* const { positions, classification, origin } = await parseLAS(bytes, { step: 4 });
|
|
89
|
+
* console.log(positions.length / 3, classification[0], origin);
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export declare function parseLAS(input: BinaryInput, options?: LASOptions): Promise<LASCloud>;
|
package/dist/las.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// ASPRS LAS and LAZ point clouds, as the shared output of this package. The reading is `@margoengine/points/las`, the
|
|
2
|
+
// one LAS reader of the engine; this entry shapes its result and names its failures as a `LoaderError`.
|
|
3
|
+
import { parseLAS as readPoints, readLASHeader, } from '@margoengine/points/las';
|
|
4
|
+
import { LoaderError } from './types.js';
|
|
5
|
+
import { limits, throwIfAborted, toBytes } from './util.js';
|
|
6
|
+
const F = 'LAS';
|
|
7
|
+
const CODES = ['header', 'truncated', 'range', 'unsupported'];
|
|
8
|
+
/** The `LoaderError` for a failure of the points reader. A LAZ stream that fails is damaged or cut short. */
|
|
9
|
+
function named(error, signal) {
|
|
10
|
+
if (error instanceof LoaderError)
|
|
11
|
+
return error;
|
|
12
|
+
if (signal?.aborted)
|
|
13
|
+
return new LoaderError(F, 'aborted', 'aborted');
|
|
14
|
+
if (!(error instanceof Error))
|
|
15
|
+
return error;
|
|
16
|
+
const code = error.code, known = typeof code === 'string' && CODES.includes(code);
|
|
17
|
+
if (!known && !(error instanceof RangeError || error instanceof TypeError))
|
|
18
|
+
return error;
|
|
19
|
+
const kind = known ? code : error instanceof RangeError ? 'truncated' : 'header';
|
|
20
|
+
return Object.assign(new LoaderError(F, kind, error.message), { cause: error });
|
|
21
|
+
}
|
|
22
|
+
const shaped = (header) => ({
|
|
23
|
+
version: [header.versionMajor, header.versionMinor],
|
|
24
|
+
pointFormat: header.pointFormat,
|
|
25
|
+
pointRecordLength: header.pointRecordLength,
|
|
26
|
+
pointCount: header.pointCount,
|
|
27
|
+
scale: [...header.scale],
|
|
28
|
+
offset: [...header.offset],
|
|
29
|
+
min: [...header.min],
|
|
30
|
+
max: [...header.max],
|
|
31
|
+
generator: header.generatingSoftware,
|
|
32
|
+
compressed: header.compressed,
|
|
33
|
+
});
|
|
34
|
+
/**
|
|
35
|
+
* Reads an ASPRS LAS or LAZ point cloud, versions 1.0 to 1.5 and point formats 0 to 10 (0 to 5 refused for 1.5, which removed them): positions by the file's scale
|
|
36
|
+
* and offset, intensity, classification, returns, GPS time and colour, with the header and its variable records, extra
|
|
37
|
+
* bytes and the coordinate reference system. Positions are relative to `origin`, because survey coordinates are too
|
|
38
|
+
* large for 32-bit floats; add it back for world coordinates. Colours are scaled to [0,1] from 16 bits, or from 8 for
|
|
39
|
+
* files that fill only the low byte.
|
|
40
|
+
*
|
|
41
|
+
* This is a thin layer over `parseLAS` of `@margoengine/points/las`. A LAZ file opens as it is: the decoder of
|
|
42
|
+
* `@margoengine/points/laz` loads when the first compressed file arrives, so a plain LAS file never pays for it.
|
|
43
|
+
*
|
|
44
|
+
* @param input The file's bytes.
|
|
45
|
+
* @param options The shared size limits, a thinning step, an AbortSignal, and a decoder of your own if you have one.
|
|
46
|
+
* @returns The cloud with its attributes, header and records.
|
|
47
|
+
* @throws LoaderError with code `header`, `truncated`, `range`, `limit`, `aborted` or `unsupported` (an unknown version
|
|
48
|
+
* or point format, or LAZ item version 1 from LASzip 1.0).
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { parseLAS } from '@margoengine/loaders/las';
|
|
53
|
+
*
|
|
54
|
+
* const bytes = await (await fetch('/surveys/quarry.laz')).arrayBuffer();
|
|
55
|
+
* const { positions, classification, origin } = await parseLAS(bytes, { step: 4 });
|
|
56
|
+
* console.log(positions.length / 3, classification[0], origin);
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export async function parseLAS(input, options = {}) {
|
|
60
|
+
const bytes = toBytes(input, F), { maxVertices } = limits(options, F), { signal, decompress } = options, step = options.step ?? 1;
|
|
61
|
+
throwIfAborted(signal, F);
|
|
62
|
+
if (!Number.isSafeInteger(step) || step < 1)
|
|
63
|
+
throw new LoaderError(F, 'range', 'step must be a positive integer');
|
|
64
|
+
try {
|
|
65
|
+
const { header: file } = readLASHeader(bytes), header = shaped(file), kept = Math.ceil(file.pointCount / step);
|
|
66
|
+
if (kept > maxVertices)
|
|
67
|
+
throw new LoaderError(F, 'limit', `${kept} points exceed the limit of ${maxVertices}`);
|
|
68
|
+
const points = await readPoints(bytes, {
|
|
69
|
+
origin: 'min',
|
|
70
|
+
stride: step,
|
|
71
|
+
signal,
|
|
72
|
+
laz: decompress
|
|
73
|
+
? (_file, _header, records) => decompress({ bytes, header, pointOffset: file.pointDataOffset, records: [...records] })
|
|
74
|
+
: undefined,
|
|
75
|
+
});
|
|
76
|
+
const { rgb, intensity, classification, returnNumber, numberOfReturns, gpsTime, extra, crs, records } = points, origin = [...points.origin];
|
|
77
|
+
const cloud = {
|
|
78
|
+
kind: 'points',
|
|
79
|
+
positions: points.positions,
|
|
80
|
+
header,
|
|
81
|
+
origin,
|
|
82
|
+
intensity,
|
|
83
|
+
classification,
|
|
84
|
+
returnNumber,
|
|
85
|
+
returnCount: numberOfReturns,
|
|
86
|
+
records: [...records],
|
|
87
|
+
extra,
|
|
88
|
+
points,
|
|
89
|
+
attributes: { intensity: Float32Array.from(intensity), classification: Float32Array.from(classification) },
|
|
90
|
+
};
|
|
91
|
+
if (gpsTime)
|
|
92
|
+
cloud.gpsTime = gpsTime;
|
|
93
|
+
if (crs)
|
|
94
|
+
cloud.crs = crs;
|
|
95
|
+
if (rgb) {
|
|
96
|
+
let largest = 0;
|
|
97
|
+
for (let i = 0; i < rgb.length; i++)
|
|
98
|
+
if (rgb[i] > largest)
|
|
99
|
+
largest = rgb[i];
|
|
100
|
+
const full = largest > 255 ? 65535 : 255, colors = new Float32Array(rgb.length);
|
|
101
|
+
for (let i = 0; i < rgb.length; i++)
|
|
102
|
+
colors[i] = rgb[i] / full;
|
|
103
|
+
cloud.colors = colors;
|
|
104
|
+
}
|
|
105
|
+
return cloud;
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
throw named(error, signal);
|
|
109
|
+
}
|
|
110
|
+
}
|
package/dist/ldraw.d.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { type AssetResolver, type LoadedGeometry, type LoadedGroup, type ParseLimits } from './types.js';
|
|
2
|
+
import { type BinaryInput } from './util.js';
|
|
3
|
+
/** One colour of {@link LDrawModel.palette}. Colours are sRGB in [0,1]. */
|
|
4
|
+
export interface LDrawColor {
|
|
5
|
+
code: number;
|
|
6
|
+
name: string;
|
|
7
|
+
/** `[r, g, b, a]`. */
|
|
8
|
+
color: [number, number, number, number];
|
|
9
|
+
/** `[r, g, b]` of the edge lines drawn in this colour's complement. */
|
|
10
|
+
edge: [number, number, number];
|
|
11
|
+
}
|
|
12
|
+
/** Options of {@link parseLDraw}. */
|
|
13
|
+
export interface LDrawOptions extends ParseLimits {
|
|
14
|
+
/** Reads a part, by its path below the library folder; {@link resolveFromURL} makes one for a hosted library. Without it, only the file's own contents load. */
|
|
15
|
+
resolve?: AssetResolver;
|
|
16
|
+
/** Folders tried in order for each reference, ending in `/`. Default `''`, `parts/`, `p/`, `models/` and the `unofficial/` two. */
|
|
17
|
+
searchPaths?: readonly string[];
|
|
18
|
+
/** The text of `LDConfig.ldr`. Default: read through `resolve`, else the 16 classic colours. */
|
|
19
|
+
config?: string;
|
|
20
|
+
/** `'meters'` (default) scales by 0.4 mm per unit and turns the model Y up; `'file'` keeps LDraw units, -Y up. */
|
|
21
|
+
units?: 'meters' | 'file';
|
|
22
|
+
/** Colour code of the model's main colour 16. Default 7, light grey. */
|
|
23
|
+
color?: number;
|
|
24
|
+
/** Keep edge and conditional lines. Default true. */
|
|
25
|
+
edges?: boolean;
|
|
26
|
+
/** Sub-file nesting limit. Default 64. */
|
|
27
|
+
maxDepth?: number;
|
|
28
|
+
/** Most sub-file references expanded in the model. Default 4,000,000. */
|
|
29
|
+
maxInstances?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Keeps each placed `.dat` part as one geometry with a matrix per placement, in `instances`, so a model of many alike
|
|
32
|
+
* bricks costs each brick once. What is not a placed part stays in the main mesh, and so do mirrored placements. Default
|
|
33
|
+
* false: everything expands into the main mesh.
|
|
34
|
+
*/
|
|
35
|
+
instanced?: boolean;
|
|
36
|
+
signal?: AbortSignal;
|
|
37
|
+
}
|
|
38
|
+
/** One `.dat` part in one colour, placed one or more times: from `instanced`. */
|
|
39
|
+
export interface LDrawInstances {
|
|
40
|
+
/** The part's file, as the model names it. */
|
|
41
|
+
file: string;
|
|
42
|
+
/** The colour code the part took from its reference. */
|
|
43
|
+
code: number;
|
|
44
|
+
/** The part in its own LDraw units, with a group per colour, as the main mesh has them. */
|
|
45
|
+
positions: Float32Array;
|
|
46
|
+
indices: Uint32Array;
|
|
47
|
+
groups: LoadedGroup[];
|
|
48
|
+
/** Edge and conditional lines in the part's own units, as on {@link LDrawModel}. */
|
|
49
|
+
lines: Float32Array;
|
|
50
|
+
lineColors: Float32Array;
|
|
51
|
+
conditionalLines: Float32Array;
|
|
52
|
+
/** A column-major 4x4 per placement: part units to the model's units and axes. */
|
|
53
|
+
matrices: Float32Array;
|
|
54
|
+
}
|
|
55
|
+
/** What {@link parseLDraw} returns: one triangle mesh with a group per colour, plus the model's lines. */
|
|
56
|
+
export interface LDrawModel extends LoadedGeometry {
|
|
57
|
+
kind: 'mesh';
|
|
58
|
+
groups: LoadedGroup[];
|
|
59
|
+
/** Two xyz points per edge line: `mode: 'segments'` of `@margoengine/lines`. */
|
|
60
|
+
lines: Float32Array;
|
|
61
|
+
/** rgb per point of `lines`. */
|
|
62
|
+
lineColors: Float32Array;
|
|
63
|
+
/** Four xyz points per conditional line: its two ends, then its two control points. */
|
|
64
|
+
conditionalLines: Float32Array;
|
|
65
|
+
/** With `instanced`: the placed parts, which the main mesh and lines then leave out. Otherwise empty. */
|
|
66
|
+
instances: LDrawInstances[];
|
|
67
|
+
/** The colours the model uses. */
|
|
68
|
+
palette: LDrawColor[];
|
|
69
|
+
/** The first title comment of the main file, when it has one. */
|
|
70
|
+
name?: string;
|
|
71
|
+
/** References that no search path could read, each listed once. */
|
|
72
|
+
missing: string[];
|
|
73
|
+
/** Things the parser moved past, each listed once. */
|
|
74
|
+
warnings: string[];
|
|
75
|
+
}
|
|
76
|
+
/** Options of {@link resolveFromURL}. */
|
|
77
|
+
export interface LDrawURLOptions {
|
|
78
|
+
/** Fetch used for each part. Default `globalThis.fetch`. */
|
|
79
|
+
fetch?: typeof fetch;
|
|
80
|
+
/** Cancels the reads in flight. Pass the one you give `parseLDraw`. */
|
|
81
|
+
signal?: AbortSignal;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* A `resolve` for {@link parseLDraw} that reads parts over HTTP from an LDraw library folder you host. The library is
|
|
85
|
+
* the "Complete" download of ldraw.org, unzipped: `LDConfig.ldr`, `parts/` and `p/` directly below `base`.
|
|
86
|
+
* A status other than 2xx rejects, so `parseLDraw` tries the next search path and lists a part found nowhere in `missing`.
|
|
87
|
+
*
|
|
88
|
+
* @param base The library folder, absolute or relative to the page.
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* import { parseLDraw, resolveFromURL } from '@margoengine/loaders/ldraw';
|
|
92
|
+
*
|
|
93
|
+
* const text = await (await fetch('/models/car.ldr')).text();
|
|
94
|
+
* const model = await parseLDraw(text, { resolve: resolveFromURL('/ldraw/') });
|
|
95
|
+
* console.log(model.missing);
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export declare function resolveFromURL(base: string | URL, { fetch: get, signal }?: LDrawURLOptions): AssetResolver;
|
|
99
|
+
/**
|
|
100
|
+
* Reads an LDraw model (`.ldr`, `.dat` or a multi-part `.mpd`). Sub-file references load through `resolve` and are
|
|
101
|
+
* expanded into one mesh, with a group per colour. Colour 16 takes the parent's colour and 24 its edge colour, `0x2RRGGBB`
|
|
102
|
+
* is a direct colour, and `0 !COLOUR` lines and `LDConfig.ldr` fill the palette. Certified BFC files keep their winding;
|
|
103
|
+
* polygons of an uncertified file are written on both sides, so they draw with back faces culled.
|
|
104
|
+
*
|
|
105
|
+
* @param input The file's bytes or text.
|
|
106
|
+
* @param options The resolver, search paths, units, limits and an AbortSignal.
|
|
107
|
+
* @returns The mesh, its edge and conditional lines, the colours used and any references that could not be read.
|
|
108
|
+
* @throws LoaderError with code `syntax`, `limit`, `range` or `aborted`. A missing part is listed in `missing`, not thrown.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* import { parseLDraw } from '@margoengine/loaders/ldraw';
|
|
113
|
+
*
|
|
114
|
+
* const model = await parseLDraw('0 BFC CERTIFY CCW\n3 4 0 0 0 10 0 0 0 0 10\n2 24 0 0 0 10 0 0\n');
|
|
115
|
+
* console.log(model.groups[0].color, model.lines.length, model.missing);
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
export declare function parseLDraw(input: BinaryInput | string, options?: LDrawOptions): Promise<LDrawModel>;
|