@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,71 @@
|
|
|
1
|
+
import { type ThreeMFOptions, type ThreeMFResult } from './threemf.js';
|
|
2
|
+
import type { BinaryInput } from './util.js';
|
|
3
|
+
/** A height map resource. */
|
|
4
|
+
export interface ThreeMFDisplacementMap {
|
|
5
|
+
id: string;
|
|
6
|
+
modelPath?: string;
|
|
7
|
+
/** The package path of its PNG. */
|
|
8
|
+
path: string;
|
|
9
|
+
data: Uint8Array;
|
|
10
|
+
channel: 'R' | 'G' | 'B' | 'A';
|
|
11
|
+
tileU: string;
|
|
12
|
+
tileV: string;
|
|
13
|
+
filter: string;
|
|
14
|
+
}
|
|
15
|
+
/** A group of displacement coordinates over one map and one list of vectors. */
|
|
16
|
+
export interface ThreeMFDisplacementGroup {
|
|
17
|
+
id: string;
|
|
18
|
+
modelPath?: string;
|
|
19
|
+
mapId: string;
|
|
20
|
+
/** In model units, the height of a map value of 1. */
|
|
21
|
+
height: number;
|
|
22
|
+
/** In model units, the height of a map value of 0. */
|
|
23
|
+
offset: number;
|
|
24
|
+
/** `u v f` for each coordinate; `v` points up from the map's lower left corner. */
|
|
25
|
+
coordinates: Float32Array;
|
|
26
|
+
/** Each coordinate's unit displacement vector, `x y z`. */
|
|
27
|
+
vectors: Float32Array;
|
|
28
|
+
}
|
|
29
|
+
/** A built displacement mesh. */
|
|
30
|
+
export interface ThreeMFDisplacementMesh {
|
|
31
|
+
objectId: string;
|
|
32
|
+
modelPath?: string;
|
|
33
|
+
/** The base triangles, in the object's own coordinates and the model's unit. */
|
|
34
|
+
positions: Float32Array;
|
|
35
|
+
indices: Uint32Array;
|
|
36
|
+
/** For each triangle, the index in `displacementGroups` of its coordinate group, or -1 when it is not displaced. */
|
|
37
|
+
triangleGroups: Int32Array;
|
|
38
|
+
/** Three for each triangle: each corner's coordinate in its group, or -1. */
|
|
39
|
+
triangleCoordinates: Int32Array;
|
|
40
|
+
/**
|
|
41
|
+
* Each place the build puts the object: twelve numbers as a 3MF transform writes them, from the object's coordinates
|
|
42
|
+
* to the result's units. The format displaces before it transforms.
|
|
43
|
+
*/
|
|
44
|
+
instances: Float64Array[];
|
|
45
|
+
}
|
|
46
|
+
/** Options of {@link parse3MFDisplacement}. */
|
|
47
|
+
export interface ThreeMFDisplacementOptions extends ThreeMFOptions {
|
|
48
|
+
/** Subdivide and move the displaced triangles. Default true; false gives the base triangles. */
|
|
49
|
+
tessellate?: boolean;
|
|
50
|
+
/** Samples along one texel of a displacement map. Default 1. */
|
|
51
|
+
samplesPerTexel?: number;
|
|
52
|
+
/** The most segments one base edge is split into, rounded down to a power of two: 1 to 65536. Default 256. */
|
|
53
|
+
maxSegments?: number;
|
|
54
|
+
}
|
|
55
|
+
/** The result of {@link parse3MFDisplacement}. */
|
|
56
|
+
export interface ThreeMFDisplacementResult extends ThreeMFResult {
|
|
57
|
+
maps: ThreeMFDisplacementMap[];
|
|
58
|
+
displacementGroups: ThreeMFDisplacementGroup[];
|
|
59
|
+
meshes: ThreeMFDisplacementMesh[];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Parses a 3MF package with the displacement extension. Each displacement mesh is subdivided until a segment spans at
|
|
63
|
+
* most one texel of its map (`samplesPerTexel`) and moved by it, as chapters 2 to 5 of the extension define, then
|
|
64
|
+
* placed by the build: the result's triangles are the displaced surface, in groups marked `displaced`. The maps, the
|
|
65
|
+
* coordinate groups and the base triangles are returned beside them. Self-intersections of a displaced surface are not
|
|
66
|
+
* resolved.
|
|
67
|
+
*
|
|
68
|
+
* @throws LoaderError for a reference to a resource the package does not define, an index outside its group, a map
|
|
69
|
+
* part that is missing or no PNG, a surface over `maxTriangles`, and what `parse3MF` throws.
|
|
70
|
+
*/
|
|
71
|
+
export declare function parse3MFDisplacement(input: BinaryInput, options?: ThreeMFDisplacementOptions): Promise<ThreeMFDisplacementResult>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Reads the 3MF Consortium's Displacement Extension (https://github.com/3MFConsortium/spec_displacement): height maps,
|
|
2
|
+
// displacement vectors, coordinate groups and the displacement meshes that use them.
|
|
3
|
+
import { read3MF } from './threemf.js';
|
|
4
|
+
import { displacementStage } from './threemf-displacement-read.js';
|
|
5
|
+
/**
|
|
6
|
+
* Parses a 3MF package with the displacement extension. Each displacement mesh is subdivided until a segment spans at
|
|
7
|
+
* most one texel of its map (`samplesPerTexel`) and moved by it, as chapters 2 to 5 of the extension define, then
|
|
8
|
+
* placed by the build: the result's triangles are the displaced surface, in groups marked `displaced`. The maps, the
|
|
9
|
+
* coordinate groups and the base triangles are returned beside them. Self-intersections of a displaced surface are not
|
|
10
|
+
* resolved.
|
|
11
|
+
*
|
|
12
|
+
* @throws LoaderError for a reference to a resource the package does not define, an index outside its group, a map
|
|
13
|
+
* part that is missing or no PNG, a surface over `maxTriangles`, and what `parse3MF` throws.
|
|
14
|
+
*/
|
|
15
|
+
export async function parse3MFDisplacement(input, options = {}) {
|
|
16
|
+
const stage = displacementStage(options), read = await read3MF(input, options, stage.hooks);
|
|
17
|
+
return { ...read, ...stage.result() };
|
|
18
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ThreeMFBooleanOptions, ThreeMFBooleanResult } from './threemf-boolean.js';
|
|
2
|
+
import type { ThreeMFDisplacementOptions, ThreeMFDisplacementResult } from './threemf-displacement.js';
|
|
3
|
+
import type { ThreeMFBeamLattice } from './threemf-lattice.js';
|
|
4
|
+
import type { ThreeMFVolumetricOptions, ThreeMFVolumetricResult } from './threemf-volumetric.js';
|
|
5
|
+
import { type ThreeMFResult } from './threemf.js';
|
|
6
|
+
import type { BinaryInput } from './util.js';
|
|
7
|
+
/** Options of {@link parse3MFEvaluated}: those of the readers it joins, each evaluating. */
|
|
8
|
+
export interface ThreeMFEvaluateOptions extends Omit<ThreeMFDisplacementOptions, 'tessellate'>, Omit<ThreeMFVolumetricOptions, 'meshLevelSets'>, Omit<ThreeMFBooleanOptions, 'evaluate'> {
|
|
9
|
+
/** Sides of a beam or ball around its axis: 3 to 256, default 16. */
|
|
10
|
+
beamSegments?: number;
|
|
11
|
+
}
|
|
12
|
+
/** A beam lattice of a mesh object. */
|
|
13
|
+
export interface ThreeMFEvaluatedLattice {
|
|
14
|
+
objectId: string;
|
|
15
|
+
modelPath?: string;
|
|
16
|
+
lattice: ThreeMFBeamLattice;
|
|
17
|
+
}
|
|
18
|
+
/** The result of {@link parse3MFEvaluated}. */
|
|
19
|
+
export interface ThreeMFEvaluatedResult extends ThreeMFResult, Pick<ThreeMFDisplacementResult, 'maps' | 'displacementGroups' | 'meshes'>, Pick<ThreeMFVolumetricResult, 'images' | 'functions' | 'volumes' | 'objects'>, Pick<ThreeMFBooleanResult, 'shapes'> {
|
|
20
|
+
/** Every beam lattice; its triangles are part of the result's wherever the build places its object. */
|
|
21
|
+
lattices: ThreeMFEvaluatedLattice[];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Parses a 3MF package and evaluates every shape in it, whichever extensions it mixes: displacement meshes are
|
|
25
|
+
* tessellated, volumetric level sets meshed, beam lattices and balls made into triangles (clipped by their clipping
|
|
26
|
+
* mesh), volume colours sampled onto the surfaces, and boolean shapes evaluated, their base being any of these. The
|
|
27
|
+
* result holds those surfaces where the build places them, with what each reader returns beside them.
|
|
28
|
+
*
|
|
29
|
+
* A lattice's shells overlap its mesh and each other, which the 3MF positive fill rule reads as one solid; a lattice
|
|
30
|
+
* with a clipping mesh, and a boolean's result, are single surfaces. Clipping and boolean evaluation need the optional
|
|
31
|
+
* `@margoengine/mesh-edit` peer.
|
|
32
|
+
*
|
|
33
|
+
* @throws LoaderError for whatever `parse3MFDisplacement`, `parse3MFVolumetric` and `parse3MFBoolean` refuse, apart
|
|
34
|
+
* from a boolean base of another extension, and for a lattice over `maxTriangles`.
|
|
35
|
+
*/
|
|
36
|
+
export declare function parse3MFEvaluated(input: BinaryInput, options?: ThreeMFEvaluateOptions): Promise<ThreeMFEvaluatedResult>;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// @margoengine/loaders/threemf-evaluate: one call that turns every 3MF shape of a package into triangles. The
|
|
2
|
+
// displacement, volumetric, beam lattice and boolean readers run on one reading of the package, in the order their
|
|
3
|
+
// results feed each other: displacement meshes are tessellated, level sets meshed, lattices made and clipped, volume
|
|
4
|
+
// colours sampled, and boolean shapes evaluated on what came before
|
|
5
|
+
// (https://github.com/3MFConsortium/spec_booleans: a base may be any extension's shape).
|
|
6
|
+
import { booleanStage } from './threemf-boolean-read.js';
|
|
7
|
+
import { displacementStage } from './threemf-displacement-read.js';
|
|
8
|
+
import { lattices } from './threemf-lattice-mesh.js';
|
|
9
|
+
import { volumetricStage } from './threemf-volumetric-read.js';
|
|
10
|
+
import { read3MF } from './threemf.js';
|
|
11
|
+
/** One extension that hands every call to each of `parts` in turn. */
|
|
12
|
+
function joined(parts) {
|
|
13
|
+
return {
|
|
14
|
+
booleans: true,
|
|
15
|
+
levelSets: true,
|
|
16
|
+
welded: true,
|
|
17
|
+
element(model, name, attributes) {
|
|
18
|
+
for (const part of parts)
|
|
19
|
+
part.element?.(model, name, attributes);
|
|
20
|
+
},
|
|
21
|
+
field: {
|
|
22
|
+
open(model, name, attributes) {
|
|
23
|
+
for (const part of parts)
|
|
24
|
+
part.field?.open(model, name, attributes);
|
|
25
|
+
},
|
|
26
|
+
close(model, name) {
|
|
27
|
+
for (const part of parts)
|
|
28
|
+
part.field?.close(model, name);
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
close(model, name) {
|
|
32
|
+
for (const part of parts)
|
|
33
|
+
part.close?.(model, name);
|
|
34
|
+
},
|
|
35
|
+
instance(...placed) {
|
|
36
|
+
for (const part of parts)
|
|
37
|
+
part.instance?.(...placed);
|
|
38
|
+
},
|
|
39
|
+
hasGeometry: () => parts.some((part) => part.hasGeometry?.()),
|
|
40
|
+
async ready(read, context) {
|
|
41
|
+
for (const part of parts)
|
|
42
|
+
await part.ready?.(read, context);
|
|
43
|
+
},
|
|
44
|
+
bucket: () => '',
|
|
45
|
+
corner() { },
|
|
46
|
+
group() { },
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Parses a 3MF package and evaluates every shape in it, whichever extensions it mixes: displacement meshes are
|
|
51
|
+
* tessellated, volumetric level sets meshed, beam lattices and balls made into triangles (clipped by their clipping
|
|
52
|
+
* mesh), volume colours sampled onto the surfaces, and boolean shapes evaluated, their base being any of these. The
|
|
53
|
+
* result holds those surfaces where the build places them, with what each reader returns beside them.
|
|
54
|
+
*
|
|
55
|
+
* A lattice's shells overlap its mesh and each other, which the 3MF positive fill rule reads as one solid; a lattice
|
|
56
|
+
* with a clipping mesh, and a boolean's result, are single surfaces. Clipping and boolean evaluation need the optional
|
|
57
|
+
* `@margoengine/mesh-edit` peer.
|
|
58
|
+
*
|
|
59
|
+
* @throws LoaderError for whatever `parse3MFDisplacement`, `parse3MFVolumetric` and `parse3MFBoolean` refuse, apart
|
|
60
|
+
* from a boolean base of another extension, and for a lattice over `maxTriangles`.
|
|
61
|
+
*/
|
|
62
|
+
export async function parse3MFEvaluated(input, options = {}) {
|
|
63
|
+
const displacement = displacementStage(options), beams = lattices(options), volumetric = volumetricStage(options, beams.add), booleans = booleanStage(options, true), colours = {
|
|
64
|
+
// Last: volume colour goes onto the surfaces the steps before have made.
|
|
65
|
+
ready: async (_read, { models }) => volumetric.paint(models),
|
|
66
|
+
bucket: () => '',
|
|
67
|
+
corner() { },
|
|
68
|
+
group() { },
|
|
69
|
+
},
|
|
70
|
+
// `beams.hooks` only notes the colour groups; its triangles are made inside the volumetric step.
|
|
71
|
+
read = await read3MF(input, options, joined([displacement.hooks, beams.hooks, volumetric.hooks, booleans.hooks, colours]));
|
|
72
|
+
// A boolean's operands are not built themselves, so their volume data is read through the shape.
|
|
73
|
+
for (const { base, operands } of booleans.shapes)
|
|
74
|
+
for (const operand of [base, ...operands]) {
|
|
75
|
+
const sample = volumetric.sampleOf(operand.objectId, operand.modelPath);
|
|
76
|
+
if (sample)
|
|
77
|
+
operand.sample = sample;
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
...read,
|
|
81
|
+
...displacement.result(),
|
|
82
|
+
...volumetric.result(),
|
|
83
|
+
shapes: booleans.shapes,
|
|
84
|
+
lattices: beams.result(),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ThreeMFImageSampler, ThreeMFImplicit } from './threemf-volumetric.js';
|
|
2
|
+
/** A buffer of 1, 3 or 16 numbers, or the id of a resource, which is known before anything runs. */
|
|
3
|
+
export type FieldValue = Float64Array | string;
|
|
4
|
+
type Step = () => void;
|
|
5
|
+
/** The voxels of an image stack: RGBA, 16 bits a sample, sheet by sheet, each top row first. */
|
|
6
|
+
export interface Voxels {
|
|
7
|
+
columns: number;
|
|
8
|
+
rows: number;
|
|
9
|
+
sheets: number;
|
|
10
|
+
data: Uint16Array;
|
|
11
|
+
}
|
|
12
|
+
/** What the functions of one model part read. */
|
|
13
|
+
export interface FieldScope {
|
|
14
|
+
functions: ReadonlyMap<string, ThreeMFImageSampler | ThreeMFImplicit>;
|
|
15
|
+
/** The voxels of an `image3d`. */
|
|
16
|
+
voxels(imageId: string): Voxels;
|
|
17
|
+
/** The signed distance to a mesh object (negative inside), or the unsigned one. */
|
|
18
|
+
distance(objectId: string, signed: boolean): (x: number, y: number, z: number) => number;
|
|
19
|
+
/** The signed distance to a mesh object's beam lattice, clipped as the lattice says. */
|
|
20
|
+
lattice(objectId: string): (x: number, y: number, z: number) => number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Compiles function `id` of a scope over `inputs`, whose buffers the caller writes before each `run`. `outputs` holds
|
|
24
|
+
* the function's outputs by name.
|
|
25
|
+
*
|
|
26
|
+
* @throws LoaderError for a missing or mistyped input, a node this evaluator does not know, and functions that call
|
|
27
|
+
* themselves.
|
|
28
|
+
*/
|
|
29
|
+
export declare function compileField(scope: FieldScope, id: string, inputs: ReadonlyMap<string, FieldValue>): {
|
|
30
|
+
run: Step;
|
|
31
|
+
outputs: Map<string, FieldValue>;
|
|
32
|
+
};
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import { LoaderError } from './types.js';
|
|
2
|
+
const F = '3MF';
|
|
3
|
+
const SIZE = { scalar: 1, vector: 3, matrix: 16, resourceid: 0 };
|
|
4
|
+
const MAX_STEPS = 100_000;
|
|
5
|
+
const bad = (message) => {
|
|
6
|
+
throw new LoaderError(F, 'syntax', message);
|
|
7
|
+
};
|
|
8
|
+
/** A voxel index outside `0..size - 1` under a tile style. */
|
|
9
|
+
const tiled = (style, i, size) => {
|
|
10
|
+
if (style === 'clamp')
|
|
11
|
+
return Math.min(size - 1, Math.max(i, 0));
|
|
12
|
+
const turns = Math.floor(i / size);
|
|
13
|
+
return style === 'mirror' && turns % 2 !== 0 ? (turns + 1) * size - i - 1 : i - turns * size;
|
|
14
|
+
};
|
|
15
|
+
function sampler(f, voxels, pos, out) {
|
|
16
|
+
const { columns, rows, sheets, data } = voxels, rgba = new Float64Array(4), [tileU, tileV, tileW] = f.tile, color = out.get('color'), channels = ['red', 'green', 'blue', 'alpha'].map((name) => out.get(name));
|
|
17
|
+
/** A coordinate brought into 0..1 by its tile style, so a far point keeps its precision. */
|
|
18
|
+
const wrapped = (style, s) => style === 'clamp'
|
|
19
|
+
? Math.min(1, Math.max(0, s))
|
|
20
|
+
: style === 'mirror'
|
|
21
|
+
? 1 - Math.abs(s - 2 * Math.floor(s / 2) - 1)
|
|
22
|
+
: s - Math.floor(s);
|
|
23
|
+
return () => {
|
|
24
|
+
if (!Number.isFinite(pos[0] + pos[1] + pos[2]))
|
|
25
|
+
rgba.fill(NaN);
|
|
26
|
+
else {
|
|
27
|
+
// Voxel centres are at halves; rows run down as v runs up (section 3.2).
|
|
28
|
+
const c = wrapped(tileU, pos[0]) * columns - 0.5, r = (1 - wrapped(tileV, pos[1])) * rows - 0.5, s = wrapped(tileW, pos[2]) * sheets - 0.5;
|
|
29
|
+
const add = (i, j, k, weight) => {
|
|
30
|
+
const at = ((tiled(tileW, k, sheets) * rows + tiled(tileV, i, rows)) * columns + tiled(tileU, j, columns)) * 4;
|
|
31
|
+
for (let n = 0; n < 4; n++)
|
|
32
|
+
rgba[n] += (data[at + n] / 65535) * weight;
|
|
33
|
+
};
|
|
34
|
+
rgba.fill(0);
|
|
35
|
+
if (f.filter === 'nearest') {
|
|
36
|
+
// The nearest centre; halfway between two, the one of lower index.
|
|
37
|
+
const lower = (x) => Math.ceil(x - 0.5 - 1e-9);
|
|
38
|
+
add(lower(r), lower(c), lower(s), 1);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
const i = Math.floor(r), j = Math.floor(c), k = Math.floor(s);
|
|
42
|
+
for (let n = 0; n < 8; n++) {
|
|
43
|
+
const di = (n >> 1) & 1, dj = n & 1, dk = n >> 2, weight = (di ? r - i : 1 - r + i) * (dj ? c - j : 1 - c + j) * (dk ? s - k : 1 - s + k);
|
|
44
|
+
add(i + di, j + dj, k + dk, weight);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
for (let n = 0; n < 4; n++)
|
|
49
|
+
channels[n][0] = rgba[n] * f.scale + f.offset;
|
|
50
|
+
for (let n = 0; n < 3; n++)
|
|
51
|
+
color[n] = channels[n][0];
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const UNARY = {
|
|
55
|
+
sin: Math.sin,
|
|
56
|
+
cos: Math.cos,
|
|
57
|
+
tan: Math.tan,
|
|
58
|
+
asin: Math.asin,
|
|
59
|
+
acos: Math.acos,
|
|
60
|
+
atan: Math.atan,
|
|
61
|
+
abs: Math.abs,
|
|
62
|
+
sqrt: Math.sqrt,
|
|
63
|
+
exp: Math.exp,
|
|
64
|
+
log: Math.log,
|
|
65
|
+
log2: Math.log2,
|
|
66
|
+
log10: Math.log10,
|
|
67
|
+
cosh: Math.cosh,
|
|
68
|
+
sinh: Math.sinh,
|
|
69
|
+
tanh: Math.tanh,
|
|
70
|
+
round: (a) => Math.sign(a) * Math.round(Math.abs(a)),
|
|
71
|
+
ceil: Math.ceil,
|
|
72
|
+
floor: Math.floor,
|
|
73
|
+
sign: Math.sign,
|
|
74
|
+
fract: (a) => a - Math.floor(a),
|
|
75
|
+
};
|
|
76
|
+
const BINARY = {
|
|
77
|
+
addition: (a, b) => a + b,
|
|
78
|
+
subtraction: (a, b) => a - b,
|
|
79
|
+
multiplication: (a, b) => a * b,
|
|
80
|
+
division: (a, b) => a / b,
|
|
81
|
+
atan2: Math.atan2,
|
|
82
|
+
min: Math.min,
|
|
83
|
+
max: Math.max,
|
|
84
|
+
fmod: (a, b) => a % b,
|
|
85
|
+
mod: (a, b) => a - b * Math.floor(a / b),
|
|
86
|
+
// Math.pow gives 1 for an undefined base to the power 0.
|
|
87
|
+
pow: (a, b) => (a !== a || b !== b ? NaN : a ** b),
|
|
88
|
+
};
|
|
89
|
+
/** Writes the inverse of a row-major 4 x 4 matrix, or NaN for one without. Gauss-Jordan with row pivoting. */
|
|
90
|
+
function invert(m, out) {
|
|
91
|
+
const a = Array.from({ length: 4 }, (_, r) => [
|
|
92
|
+
...m.subarray(r * 4, r * 4 + 4),
|
|
93
|
+
...[0, 1, 2, 3].map((c) => +(r === c)),
|
|
94
|
+
]);
|
|
95
|
+
for (let c = 0; c < 4; c++) {
|
|
96
|
+
let pivot = c;
|
|
97
|
+
for (let r = c + 1; r < 4; r++)
|
|
98
|
+
if (Math.abs(a[r][c]) > Math.abs(a[pivot][c]))
|
|
99
|
+
pivot = r;
|
|
100
|
+
if (!(Math.abs(a[pivot][c]) > 0))
|
|
101
|
+
return void out.fill(NaN);
|
|
102
|
+
[a[c], a[pivot]] = [a[pivot], a[c]];
|
|
103
|
+
const scale = a[c][c];
|
|
104
|
+
for (let k = 0; k < 8; k++)
|
|
105
|
+
a[c][k] /= scale;
|
|
106
|
+
for (let r = 0; r < 4; r++) {
|
|
107
|
+
if (r === c)
|
|
108
|
+
continue;
|
|
109
|
+
const factor = a[r][c];
|
|
110
|
+
for (let k = 0; k < 8; k++)
|
|
111
|
+
a[r][k] -= factor * a[c][k];
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
for (let r = 0; r < 4; r++)
|
|
115
|
+
for (let c = 0; c < 4; c++)
|
|
116
|
+
out[r * 4 + c] = Number.isFinite(a[r][4 + c]) ? a[r][4 + c] : NaN;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Compiles function `id` of a scope over `inputs`, whose buffers the caller writes before each `run`. `outputs` holds
|
|
120
|
+
* the function's outputs by name.
|
|
121
|
+
*
|
|
122
|
+
* @throws LoaderError for a missing or mistyped input, a node this evaluator does not know, and functions that call
|
|
123
|
+
* themselves.
|
|
124
|
+
*/
|
|
125
|
+
export function compileField(scope, id, inputs) {
|
|
126
|
+
const steps = [], budget = { steps: MAX_STEPS };
|
|
127
|
+
const outputs = inline(scope, id, inputs, steps, [], budget);
|
|
128
|
+
return {
|
|
129
|
+
outputs,
|
|
130
|
+
run: () => {
|
|
131
|
+
for (const step of steps)
|
|
132
|
+
step();
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function inline(scope, id, inputs, steps, calls, budget) {
|
|
137
|
+
const f = scope.functions.get(id) ?? bad(`function ${id.slice(0, 32)} is not defined`), label = `function ${id.slice(0, 32)}`;
|
|
138
|
+
if (calls.includes(id) || calls.length > 64)
|
|
139
|
+
bad(`${label} calls itself`);
|
|
140
|
+
const outputs = new Map();
|
|
141
|
+
if (f.kind === 'image') {
|
|
142
|
+
const pos = inputs.get('pos');
|
|
143
|
+
if (!(pos instanceof Float64Array) || pos.length !== 3)
|
|
144
|
+
bad(`${label} needs the vector input pos`);
|
|
145
|
+
const out = new Map(['color', 'red', 'green', 'blue', 'alpha'].map((n, i) => [n, new Float64Array(i ? 1 : 3)]));
|
|
146
|
+
steps.push(sampler(f, scope.voxels(f.imageId), pos, out));
|
|
147
|
+
return out;
|
|
148
|
+
}
|
|
149
|
+
const values = new Map();
|
|
150
|
+
for (const { name, type } of f.inputs) {
|
|
151
|
+
const value = inputs.get(name);
|
|
152
|
+
if (value === undefined || (type === 'resourceid') !== (typeof value === 'string'))
|
|
153
|
+
bad(`${label} is called without its ${type} input ${name.slice(0, 64)}`);
|
|
154
|
+
if (value instanceof Float64Array && value.length !== SIZE[type])
|
|
155
|
+
bad(`${label} is called with another type for ${name.slice(0, 64)}`);
|
|
156
|
+
values.set(`inputs.${name}`, value);
|
|
157
|
+
}
|
|
158
|
+
const read = (port) => values.get(port.ref) ?? bad(`${label}: ${String(port.ref).slice(0, 64)} is not an earlier output`);
|
|
159
|
+
for (const node of f.nodes) {
|
|
160
|
+
if (--budget.steps < 0)
|
|
161
|
+
throw new LoaderError(F, 'limit', `${label} expands to more than ${MAX_STEPS} nodes`);
|
|
162
|
+
compileNode(scope, node, label, read, values, steps, [...calls, id], budget);
|
|
163
|
+
}
|
|
164
|
+
for (const port of f.outputs)
|
|
165
|
+
outputs.set(port.name, read(port));
|
|
166
|
+
return outputs;
|
|
167
|
+
}
|
|
168
|
+
function compileNode(scope, node, label, read, values, steps, calls, budget) {
|
|
169
|
+
const { op, attributes } = node, what = `${label}: node ${node.id.slice(0, 64)} (${op.slice(0, 32)})`, port = (name) => node.inputs.find((p) => p.name === name) ?? bad(`${what} needs the input ${name}`), value = (name, size) => {
|
|
170
|
+
const v = read(port(name));
|
|
171
|
+
if (!(v instanceof Float64Array) || (size !== undefined && v.length !== size))
|
|
172
|
+
bad(`${what}: input ${name} has the wrong type`);
|
|
173
|
+
return v;
|
|
174
|
+
}, resource = (name) => {
|
|
175
|
+
const v = read(port(name));
|
|
176
|
+
return typeof v === 'string' ? v : bad(`${what}: input ${name} is not a resource`);
|
|
177
|
+
};
|
|
178
|
+
const store = (name, v) => void values.set(`${node.id}.${name}`, v);
|
|
179
|
+
/** A buffer for the output `name`, or the node's only output. */
|
|
180
|
+
const result = (size, name) => {
|
|
181
|
+
const out = name === undefined ? node.outputs[0] : node.outputs.find((p) => p.name === name), buffer = new Float64Array(size);
|
|
182
|
+
if (!out) {
|
|
183
|
+
if (name === undefined)
|
|
184
|
+
bad(`${what} has no output`);
|
|
185
|
+
return buffer;
|
|
186
|
+
}
|
|
187
|
+
if (SIZE[out.type] !== size)
|
|
188
|
+
bad(`${what}: output ${out.name.slice(0, 64)} has the wrong type`);
|
|
189
|
+
store(out.name, buffer);
|
|
190
|
+
return buffer;
|
|
191
|
+
};
|
|
192
|
+
const defined = (x) => (Number.isFinite(x) ? x : NaN);
|
|
193
|
+
const numbers = (list) => list.map((text) => {
|
|
194
|
+
const n = Number(text);
|
|
195
|
+
return text?.trim() && Number.isFinite(n) ? n : bad(`${what}: invalid constant`);
|
|
196
|
+
});
|
|
197
|
+
if (Object.hasOwn(UNARY, op)) {
|
|
198
|
+
const apply = UNARY[op], a = value('A'), out = result(a.length);
|
|
199
|
+
steps.push(() => {
|
|
200
|
+
for (let i = 0; i < out.length; i++)
|
|
201
|
+
out[i] = defined(apply(a[i]));
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
else if (Object.hasOwn(BINARY, op)) {
|
|
205
|
+
const apply = BINARY[op], a = value('A'), b = value('B', a.length), out = result(a.length);
|
|
206
|
+
steps.push(() => {
|
|
207
|
+
for (let i = 0; i < out.length; i++)
|
|
208
|
+
out[i] = defined(apply(a[i], b[i]));
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
else if (op === 'constant' || op === 'constvec' || op === 'constmat') {
|
|
212
|
+
const list = op === 'constant'
|
|
213
|
+
? [attributes.value]
|
|
214
|
+
: op === 'constvec'
|
|
215
|
+
? [attributes.x, attributes.y, attributes.z]
|
|
216
|
+
: (attributes.matrix ?? '').trim().split(/\s+/), size = op === 'constant' ? 1 : op === 'constvec' ? 3 : 16;
|
|
217
|
+
if (list.length !== size)
|
|
218
|
+
bad(`${what}: invalid constant`);
|
|
219
|
+
result(size).set(numbers(list));
|
|
220
|
+
}
|
|
221
|
+
else if (op === 'constresourceid') {
|
|
222
|
+
const out = node.outputs[0] ?? bad(`${what} has no output`);
|
|
223
|
+
store(out.name, attributes.value ?? attributes.resourceid ?? bad(`${what} needs a value`));
|
|
224
|
+
}
|
|
225
|
+
else if (op === 'composevector' || op === 'composematrix') {
|
|
226
|
+
const names = op === 'composevector'
|
|
227
|
+
? ['x', 'y', 'z']
|
|
228
|
+
: Array.from({ length: 16 }, (_, i) => `m${Math.floor(i / 4)}${i % 4}`), from = names.map((name) => value(name, 1)), out = result(names.length);
|
|
229
|
+
steps.push(() => from.forEach((v, i) => (out[i] = v[0])));
|
|
230
|
+
}
|
|
231
|
+
else if (op === 'vectorfromscalar') {
|
|
232
|
+
const a = value('A', 1), out = result(3);
|
|
233
|
+
steps.push(() => void out.fill(a[0]));
|
|
234
|
+
}
|
|
235
|
+
else if (op === 'decomposevector') {
|
|
236
|
+
const a = value('A', 3), out = ['x', 'y', 'z'].map((name) => result(1, name));
|
|
237
|
+
steps.push(() => out.forEach((v, i) => (v[0] = a[i])));
|
|
238
|
+
}
|
|
239
|
+
else if (op === 'matrixfromcolumns' || op === 'matrixfromrows') {
|
|
240
|
+
const from = ['A', 'B', 'C', 'D'].map((name) => value(name, 3)), rows = op === 'matrixfromrows', out = result(16);
|
|
241
|
+
steps.push(() => {
|
|
242
|
+
for (let r = 0; r < 4; r++)
|
|
243
|
+
for (let c = 0; c < 4; c++)
|
|
244
|
+
out[r * 4 + c] = (rows ? c < 3 : r < 3) ? (rows ? from[r][c] : from[c][r]) : +(r === 3 && c === 3);
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
else if (op === 'dot') {
|
|
248
|
+
const a = value('A', 3), b = value('B', 3), out = result(1);
|
|
249
|
+
steps.push(() => (out[0] = defined(a[0] * b[0] + a[1] * b[1] + a[2] * b[2])));
|
|
250
|
+
}
|
|
251
|
+
else if (op === 'cross') {
|
|
252
|
+
const a = value('A', 3), b = value('B', 3), out = result(3);
|
|
253
|
+
steps.push(() => {
|
|
254
|
+
for (let i = 0; i < 3; i++) {
|
|
255
|
+
const j = (i + 1) % 3, k = (i + 2) % 3;
|
|
256
|
+
out[i] = defined(a[j] * b[k] - a[k] * b[j]);
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
else if (op === 'length') {
|
|
261
|
+
const a = value('A', 3), out = result(1);
|
|
262
|
+
steps.push(() => (out[0] = defined(Math.hypot(a[0], a[1], a[2]))));
|
|
263
|
+
}
|
|
264
|
+
else if (op === 'matrixvectormultiplication' || op === 'matvecmultiplication') {
|
|
265
|
+
const m = value('A', 16), v = value('B', 3), out = result(3);
|
|
266
|
+
steps.push(() => {
|
|
267
|
+
for (let r = 0; r < 3; r++)
|
|
268
|
+
out[r] = defined(m[r * 4] * v[0] + m[r * 4 + 1] * v[1] + m[r * 4 + 2] * v[2] + m[r * 4 + 3]);
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
else if (op === 'transpose') {
|
|
272
|
+
const m = value('A', 16), out = result(16);
|
|
273
|
+
steps.push(() => {
|
|
274
|
+
for (let i = 0; i < 16; i++)
|
|
275
|
+
out[i] = m[(i % 4) * 4 + Math.floor(i / 4)];
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
else if (op === 'inverse') {
|
|
279
|
+
const m = value('A', 16), out = result(16);
|
|
280
|
+
steps.push(() => invert(m, out));
|
|
281
|
+
}
|
|
282
|
+
else if (op === 'clamp') {
|
|
283
|
+
const a = value('A'), low = value('min', a.length), high = value('max', a.length), out = result(a.length);
|
|
284
|
+
steps.push(() => {
|
|
285
|
+
for (let i = 0; i < out.length; i++)
|
|
286
|
+
out[i] = Math.max(low[i], Math.min(a[i], high[i]));
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
else if (op === 'select') {
|
|
290
|
+
const a = value('A'), b = value('B', a.length), c = value('C', a.length), d = value('D', a.length), out = result(a.length);
|
|
291
|
+
steps.push(() => {
|
|
292
|
+
for (let i = 0; i < out.length; i++)
|
|
293
|
+
out[i] = a[i] !== a[i] || b[i] !== b[i] ? NaN : a[i] < b[i] ? c[i] : d[i];
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
else if (op === 'mesh' || op === 'unsignedmesh') {
|
|
297
|
+
const pos = value('pos', 3), distance = scope.distance(resource('mesh'), op === 'mesh'), out = result(1);
|
|
298
|
+
steps.push(() => (out[0] = Number.isFinite(pos[0] + pos[1] + pos[2]) ? defined(distance(pos[0], pos[1], pos[2])) : NaN));
|
|
299
|
+
}
|
|
300
|
+
else if (op === 'beamlattice') {
|
|
301
|
+
// The distance is exact in any band, so the node's accuraterange asks for nothing more.
|
|
302
|
+
const pos = value('pos', 3), distance = scope.lattice(resource('beamlattice')), out = result(1);
|
|
303
|
+
steps.push(() => (out[0] = Number.isFinite(pos[0] + pos[1] + pos[2]) ? defined(distance(pos[0], pos[1], pos[2])) : NaN));
|
|
304
|
+
}
|
|
305
|
+
else if (op === 'functioncall') {
|
|
306
|
+
const called = resource('functionID'), given = new Map(node.inputs.filter((p) => p.name !== 'functionID').map((p) => [p.name, read(p)])), returned = inline(scope, called, given, steps, calls, budget);
|
|
307
|
+
for (const { name, type } of node.outputs) {
|
|
308
|
+
const v = returned.get(name);
|
|
309
|
+
if (v === undefined || (v instanceof Float64Array && v.length !== SIZE[type]))
|
|
310
|
+
bad(`${what}: function ${called.slice(0, 32)} has no ${type} output ${name.slice(0, 64)}`);
|
|
311
|
+
store(name, v);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
else if (op === 'functiongradient' || op === 'normalizedistance') {
|
|
315
|
+
const called = resource('functionID'), { scalaroutput, vectorinput } = attributes;
|
|
316
|
+
if (!scalaroutput || !vectorinput)
|
|
317
|
+
bad(`${what} needs scalaroutput and vectorinput`);
|
|
318
|
+
const at = value(vectorinput, 3), step = value('step', 1), moved = new Float64Array(3), given = new Map(node.inputs.filter((p) => p.name !== 'functionID' && p.name !== 'step').map((p) => [p.name, read(p)])), own = [];
|
|
319
|
+
given.set(vectorinput, moved);
|
|
320
|
+
const f = inline(scope, called, given, own, calls, budget).get(scalaroutput);
|
|
321
|
+
if (!(f instanceof Float64Array) || f.length !== 1)
|
|
322
|
+
bad(`${what}: function ${called.slice(0, 32)} has no scalar output ${scalaroutput.slice(0, 64)}`);
|
|
323
|
+
const scalar = f, gradient = new Float64Array(3);
|
|
324
|
+
const sample = (axis, by) => {
|
|
325
|
+
moved.set(at);
|
|
326
|
+
if (axis >= 0)
|
|
327
|
+
moved[axis] += by;
|
|
328
|
+
for (const run of own)
|
|
329
|
+
run();
|
|
330
|
+
return scalar[0];
|
|
331
|
+
};
|
|
332
|
+
// Central differences, the step at least 1e-8.
|
|
333
|
+
const differentiate = () => {
|
|
334
|
+
const h = Math.max(step[0], 1e-8);
|
|
335
|
+
for (let k = 0; k < 3; k++)
|
|
336
|
+
gradient[k] = defined((sample(k, h) - sample(k, -h)) / (2 * h));
|
|
337
|
+
return Math.hypot(gradient[0], gradient[1], gradient[2]);
|
|
338
|
+
};
|
|
339
|
+
if (op === 'functiongradient') {
|
|
340
|
+
const direction = result(3, 'vector'), raw = result(3, 'gradient'), magnitude = result(1, 'magnitude');
|
|
341
|
+
steps.push(() => {
|
|
342
|
+
const length = differentiate();
|
|
343
|
+
raw.set(gradient);
|
|
344
|
+
magnitude[0] = length;
|
|
345
|
+
for (let k = 0; k < 3; k++)
|
|
346
|
+
direction[k] = defined(gradient[k] / length);
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
const out = result(1);
|
|
351
|
+
steps.push(() => {
|
|
352
|
+
const length = differentiate();
|
|
353
|
+
out[0] = defined(sample(-1, 0) / Math.max(length, 1e-8));
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
else
|
|
358
|
+
throw new LoaderError(F, 'unsupported', `${what}: this node is not evaluated; tessellate level sets elsewhere`);
|
|
359
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ThreeMFEvaluatedLattice, ThreeMFEvaluateOptions } from './threemf-evaluate.js';
|
|
2
|
+
import type { ThreeMFBeamLattice } from './threemf-lattice.js';
|
|
3
|
+
import { type ThreeMFExtension, type ThreeMFMesh, type ThreeMFModel } from './threemf.js';
|
|
4
|
+
/** The lattice step of the evaluating reader. */
|
|
5
|
+
export declare function lattices(options: ThreeMFEvaluateOptions): {
|
|
6
|
+
hooks: ThreeMFExtension;
|
|
7
|
+
add(model: ThreeMFModel, own: ReadonlyMap<string, ThreeMFBeamLattice>, surfaces: ReadonlyMap<string, ThreeMFMesh>, modelPath: string | undefined): Promise<void>;
|
|
8
|
+
result: () => ThreeMFEvaluatedLattice[];
|
|
9
|
+
};
|