topotopo 0.3.0 → 0.5.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/NOTICE +40 -10
- package/README.md +475 -111
- package/dist/cli.js +328 -133
- package/dist/config.d.ts +48 -0
- package/dist/config.js +170 -0
- package/dist/core/analysis.d.ts +5 -1
- package/dist/core/analysis.js +8 -3
- package/dist/core/bake.d.ts +16 -8
- package/dist/core/bake.js +165 -29
- package/dist/core/bevel.d.ts +3 -1
- package/dist/core/bevel.js +238 -97
- package/dist/core/decimate.d.ts +65 -0
- package/dist/core/decimate.js +409 -3
- package/dist/core/erosion.d.ts +37 -0
- package/dist/core/erosion.js +41 -1
- package/dist/core/geometry.d.ts +5 -1
- package/dist/core/geometry.js +204 -4
- package/dist/core/heightfield.d.ts +27 -4
- package/dist/core/heightfield.js +54 -16
- package/dist/core/imageops.d.ts +6 -0
- package/dist/core/imageops.js +51 -0
- package/dist/core/intrinsics.d.ts +7 -0
- package/dist/core/intrinsics.js +94 -0
- package/dist/core/manifold.d.ts +12 -1
- package/dist/core/manifold.js +78 -6
- package/dist/core/materials.d.ts +11 -0
- package/dist/core/materials.js +56 -0
- package/dist/core/noise.d.ts +9 -0
- package/dist/core/noise.js +10 -1
- package/dist/core/packed.js +11 -0
- package/dist/core/particles.d.ts +1 -1
- package/dist/core/particles.js +1 -1
- package/dist/core/polymerge.d.ts +21 -0
- package/dist/core/polymerge.js +327 -0
- package/dist/core/rbd.js +13 -1
- package/dist/core/repair.d.ts +2 -0
- package/dist/core/repair.js +2 -0
- package/dist/core/report.d.ts +70 -0
- package/dist/core/report.js +275 -0
- package/dist/core/scatter.d.ts +1 -1
- package/dist/core/scatter.js +1 -1
- package/dist/core/scene.d.ts +20 -0
- package/dist/core/scene.js +72 -0
- package/dist/core/sha256.d.ts +4 -0
- package/dist/core/sha256.js +69 -0
- package/dist/core/shapes.d.ts +3 -0
- package/dist/core/shapes.js +34 -0
- package/dist/core/snippet-lib.d.ts +15 -2
- package/dist/core/snippet-lib.js +40 -14
- package/dist/core/solidify.d.ts +38 -0
- package/dist/core/solidify.js +356 -0
- package/dist/core/tangents.d.ts +54 -0
- package/dist/core/tangents.js +218 -0
- package/dist/core/texcoord.d.ts +4 -0
- package/dist/core/texcoord.js +14 -0
- package/dist/core/texnoise.d.ts +6 -1
- package/dist/core/texnoise.js +6 -2
- package/dist/core/texsdf.d.ts +131 -0
- package/dist/core/texsdf.js +637 -0
- package/dist/core/texture.d.ts +163 -9
- package/dist/core/texture.js +844 -20
- package/dist/core/topology.d.ts +32 -0
- package/dist/core/topology.js +238 -7
- package/dist/core/traceimage.d.ts +7 -0
- package/dist/core/traceimage.js +221 -0
- package/dist/core/tritri.d.ts +5 -0
- package/dist/core/tritri.js +120 -0
- package/dist/core/unwrap.d.ts +72 -0
- package/dist/core/unwrap.js +800 -0
- package/dist/core/usercode.d.ts +27 -0
- package/dist/core/usercode.js +87 -0
- package/dist/core/uv.d.ts +15 -16
- package/dist/core/uv.js +36 -189
- package/dist/core/uvislands.d.ts +62 -0
- package/dist/core/uvislands.js +574 -0
- package/dist/core/uvoverlap.d.ts +58 -0
- package/dist/core/uvoverlap.js +220 -0
- package/dist/core/uvpack.d.ts +64 -0
- package/dist/core/uvpack.js +628 -0
- package/dist/core/uvreport.d.ts +127 -0
- package/dist/core/uvreport.js +372 -0
- package/dist/core/wrangle.d.ts +6 -1
- package/dist/core/wrangle.js +27 -16
- package/dist/core/xatlas.d.ts +16 -1
- package/dist/core/xatlas.js +95 -61
- package/dist/engines.d.ts +63 -0
- package/dist/engines.js +180 -0
- package/dist/explain.d.ts +4 -3
- package/dist/explain.js +129 -12
- package/dist/graph/engine.d.ts +92 -29
- package/dist/graph/engine.js +298 -148
- package/dist/graph/expr.d.ts +6 -3
- package/dist/graph/expr.js +18 -8
- package/dist/graph/outputs.d.ts +23 -0
- package/dist/graph/outputs.js +41 -0
- package/dist/graph/packages.d.ts +72 -0
- package/dist/graph/packages.js +261 -0
- package/dist/graph/parallel.d.ts +3 -1
- package/dist/graph/parallel.js +4 -1
- package/dist/graph/params.d.ts +6 -0
- package/dist/graph/params.js +34 -7
- package/dist/graph/scope.d.ts +55 -0
- package/dist/graph/scope.js +14 -0
- package/dist/graph/types.d.ts +75 -12
- package/dist/graph/types.js +4 -2
- package/dist/graph/vars.d.ts +16 -0
- package/dist/graph/vars.js +80 -0
- package/dist/host/artifacts.d.ts +10 -1
- package/dist/host/artifacts.js +37 -6
- package/dist/host/cook.d.ts +241 -0
- package/dist/host/cook.js +844 -0
- package/dist/host/export-targets.d.ts +42 -5
- package/dist/host/export-targets.js +72 -13
- package/dist/host/godot-commands.d.ts +4 -0
- package/dist/host/godot-commands.js +21 -0
- package/dist/host/godot.d.ts +183 -0
- package/dist/host/godot.js +916 -0
- package/dist/host/node.d.ts +12 -7
- package/dist/host/node.js +27 -9
- package/dist/host/pool.js +51 -2
- package/dist/host/sidecars.d.ts +8 -0
- package/dist/host/sidecars.js +50 -0
- package/dist/host/types.d.ts +19 -6
- package/dist/host/version.d.ts +1 -0
- package/dist/host/version.js +3 -0
- package/dist/host/worker.js +5 -2
- package/dist/index.d.ts +5 -3
- package/dist/index.js +3 -2
- package/dist/inspect.d.ts +84 -1
- package/dist/inspect.js +271 -24
- package/dist/io/common.d.ts +33 -5
- package/dist/io/export.js +2 -1
- package/dist/io/files.d.ts +23 -4
- package/dist/io/files.js +60 -8
- package/dist/io/gltf.js +355 -51
- package/dist/io/heightmap.d.ts +3 -0
- package/dist/io/heightmap.js +1 -0
- package/dist/io/import.js +10 -0
- package/dist/io/inspect-scene.d.ts +55 -0
- package/dist/io/inspect-scene.js +155 -0
- package/dist/io/kinds.d.ts +81 -0
- package/dist/io/kinds.js +163 -0
- package/dist/io/points.d.ts +2 -1
- package/dist/io/points.js +3 -1
- package/dist/io/sidecars.d.ts +24 -0
- package/dist/io/sidecars.js +57 -0
- package/dist/io/source.d.ts +65 -0
- package/dist/io/source.js +112 -0
- package/dist/io/targets.d.ts +47 -3
- package/dist/io/targets.js +149 -14
- package/dist/io/textures.js +21 -1
- package/dist/io/unreal.js +5 -1
- package/dist/mcp/docs.d.ts +10 -1
- package/dist/mcp/docs.js +816 -119
- package/dist/mcp/inputs.d.ts +12 -5
- package/dist/mcp/inputs.js +50 -13
- package/dist/mcp/server.js +70 -24
- package/dist/mcp/tools.d.ts +14 -7
- package/dist/mcp/tools.js +207 -108
- package/dist/mcp/verify.d.ts +2 -2
- package/dist/mcp/verify.js +1 -1
- package/dist/mcp/workspace.d.ts +15 -0
- package/dist/mcp/workspace.js +349 -0
- package/dist/nodes/attributes.js +198 -1
- package/dist/nodes/bake.js +207 -83
- package/dist/nodes/beam.d.ts +1 -0
- package/dist/nodes/beam.js +201 -0
- package/dist/nodes/bevel.js +54 -5
- package/dist/nodes/cloth.js +2 -0
- package/dist/nodes/code.js +22 -10
- package/dist/nodes/curves.js +440 -93
- package/dist/nodes/flow.js +53 -33
- package/dist/nodes/generate.js +13 -39
- package/dist/nodes/heightfield.js +248 -77
- package/dist/nodes/imageops.d.ts +1 -0
- package/dist/nodes/imageops.js +203 -0
- package/dist/nodes/index.d.ts +4 -0
- package/dist/nodes/index.js +4 -0
- package/dist/nodes/material.js +91 -23
- package/dist/nodes/measure.js +45 -18
- package/dist/nodes/particles.js +2 -0
- package/dist/nodes/pcg.js +6 -5
- package/dist/nodes/procedural.js +4 -3
- package/dist/nodes/rbd.js +1 -1
- package/dist/nodes/remesh.js +71 -15
- package/dist/nodes/scene.d.ts +1 -0
- package/dist/nodes/scene.js +55 -0
- package/dist/nodes/sdf.js +10 -5
- package/dist/nodes/solid.js +20 -5
- package/dist/nodes/solidify.d.ts +1 -0
- package/dist/nodes/solidify.js +52 -0
- package/dist/nodes/splines.d.ts +15 -1
- package/dist/nodes/splines.js +112 -105
- package/dist/nodes/texture.d.ts +21 -6
- package/dist/nodes/texture.js +1318 -185
- package/dist/nodes/topology.d.ts +7 -0
- package/dist/nodes/topology.js +133 -6
- package/dist/nodes/transform.js +11 -3
- package/dist/nodes/util.d.ts +1 -1
- package/dist/nodes/uv.js +192 -174
- package/dist/nodes/wasmcall.js +6 -2
- package/dist/open.d.ts +19 -0
- package/dist/open.js +12 -0
- package/dist/package.d.ts +288 -0
- package/dist/package.js +772 -0
- package/dist/packages/check.d.ts +32 -0
- package/dist/packages/check.js +169 -0
- package/dist/packages/commands.d.ts +57 -0
- package/dist/packages/commands.js +537 -0
- package/dist/packages/registry.d.ts +796 -0
- package/dist/packages/registry.js +2998 -0
- package/dist/packages/update.d.ts +124 -0
- package/dist/packages/update.js +476 -0
- package/dist/render/options.d.ts +2 -0
- package/dist/render/options.js +44 -5
- package/dist/render/pbr.d.ts +28 -0
- package/dist/render/pbr.js +378 -0
- package/dist/render/preview.d.ts +69 -0
- package/dist/render/preview.js +26 -0
- package/dist/render/raster.d.ts +78 -7
- package/dist/render/raster.js +600 -145
- package/dist/render/texview.d.ts +7 -1
- package/dist/render/texview.js +34 -2
- package/dist/scan.d.ts +61 -0
- package/dist/scan.js +228 -0
- package/dist/session.d.ts +311 -0
- package/dist/session.js +849 -0
- package/dist/viewer/app/api.js +48 -6
- package/dist/viewer/app/camera.js +1 -1
- package/dist/viewer/app/cooking.js +78 -0
- package/dist/viewer/app/embed.js +100 -0
- package/dist/viewer/app/export-popover.js +118 -16
- package/dist/viewer/app/grid.js +6 -2
- package/dist/viewer/app/guides.js +123 -1
- package/dist/viewer/app/keymap.js +7 -4
- package/dist/viewer/app/layers.js +5 -5
- package/dist/viewer/app/main.js +186 -53
- package/dist/viewer/app/markers.js +61 -1
- package/dist/viewer/app/outliner.js +72 -27
- package/dist/viewer/app/output-info.js +45 -0
- package/dist/viewer/app/outputs.css +147 -8
- package/dist/viewer/app/outputs.js +168 -54
- package/dist/viewer/app/packages.js +614 -0
- package/dist/viewer/app/props.js +177 -53
- package/dist/viewer/app/scene.js +35 -6
- package/dist/viewer/app/select.js +5 -4
- package/dist/viewer/app/selection.js +1 -1
- package/dist/viewer/app/status.js +12 -15
- package/dist/viewer/app/style.css +66 -8
- package/dist/viewer/app/units.js +1 -1
- package/dist/viewer/app/vars.js +48 -1
- package/dist/viewer/app/welcome.js +72 -0
- package/dist/viewer/graph-file.d.ts +62 -0
- package/dist/viewer/graph-file.js +175 -0
- package/dist/viewer/index.html +24 -11
- package/dist/viewer/json-edit.d.ts +13 -0
- package/dist/viewer/json-edit.js +274 -0
- package/dist/viewer/outputs.d.ts +49 -5
- package/dist/viewer/outputs.js +264 -56
- package/dist/viewer/packages.d.ts +121 -0
- package/dist/viewer/packages.js +584 -0
- package/dist/viewer/server.d.ts +24 -7
- package/dist/viewer/server.js +289 -31
- package/dist/web/app/web.css +419 -0
- package/dist/web/app/web.js +188 -0
- package/dist/web/bake.d.ts +34 -0
- package/dist/web/bake.js +153 -0
- package/dist/web/folder-page.d.ts +101 -0
- package/dist/web/folder-page.js +146 -0
- package/dist/web/folders.d.ts +280 -0
- package/dist/web/folders.js +970 -0
- package/dist/web/library.d.ts +91 -0
- package/dist/web/library.js +228 -0
- package/dist/web/markdown.d.ts +45 -0
- package/dist/web/markdown.js +677 -0
- package/dist/web/pages.d.ts +258 -0
- package/dist/web/pages.js +301 -0
- package/dist/web/server.d.ts +24 -0
- package/dist/web/server.js +1068 -0
- package/dist/workspace.d.ts +88 -105
- package/dist/workspace.js +359 -375
- package/integrations/godot/addons/topotopo/LICENSE +202 -0
- package/integrations/godot/addons/topotopo/README.md +57 -0
- package/integrations/godot/addons/topotopo/context_menu.gd +43 -0
- package/integrations/godot/addons/topotopo/dock.gd +266 -0
- package/integrations/godot/addons/topotopo/dock.tscn +72 -0
- package/integrations/godot/addons/topotopo/export_filter.gd +72 -0
- package/integrations/godot/addons/topotopo/gltf_instancing.gd +131 -0
- package/integrations/godot/addons/topotopo/icons/topotopo.svg +1 -0
- package/integrations/godot/addons/topotopo/inspector.gd +38 -0
- package/integrations/godot/addons/topotopo/instances_post_import.gd +111 -0
- package/integrations/godot/addons/topotopo/package.gd +23 -0
- package/integrations/godot/addons/topotopo/package_importer.gd +187 -0
- package/integrations/godot/addons/topotopo/plugin.cfg +7 -0
- package/integrations/godot/addons/topotopo/plugin.gd +970 -0
- package/integrations/godot/addons/topotopo/record.gd +435 -0
- package/integrations/godot/addons/topotopo/runner.gd +458 -0
- package/integrations/godot/addons/topotopo/tools/check.gd +42 -0
- package/integrations/godot/addons/topotopo/watcher.gd +199 -0
- package/kernels/pkg/README.md +23 -29
- package/kernels/pkg-simd/README.md +23 -29
- package/library/architecture/versions/1.0/architecture.topo +12 -0
- package/{shelf → library/architecture/versions/1.0/graphs}/arch.json +3 -3
- package/{shelf → library/architecture/versions/1.0/graphs}/beam_truss.json +6 -6
- package/{shelf → library/architecture/versions/1.0/graphs}/brick_wall.json +8 -8
- package/{shelf → library/architecture/versions/1.0/graphs}/door_frame.json +3 -3
- package/{shelf → library/architecture/versions/1.0/graphs}/hex_grid_floor.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/house.json +8 -8
- package/library/architecture/versions/1.0/graphs/lineup.json +42 -0
- package/{shelf → library/architecture/versions/1.0/graphs}/pillar.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/railing.json +5 -5
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_gable.json +2 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_hip.json +2 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_skeleton.json +2 -1
- package/{shelf → library/architecture/versions/1.0/graphs}/scaffold.json +4 -4
- package/{shelf → library/architecture/versions/1.0/graphs}/spiral_stairs.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/stairs.json +6 -6
- package/{shelf → library/architecture/versions/1.0/graphs}/tile_floor.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/wall_segment.json +10 -10
- package/{shelf → library/architecture/versions/1.0/graphs}/window_frame.json +2 -2
- package/library/architecture/versions/1.0/version.json +1119 -0
- package/{shelf → library/nature/versions/1.0/graphs}/bush.json +2 -2
- package/{shelf → library/nature/versions/1.0/graphs}/cliff_block.json +4 -3
- package/{shelf → library/nature/versions/1.0/graphs}/forest_patch.json +5 -5
- package/{shelf → library/nature/versions/1.0/graphs}/grass_clumps.json +2 -2
- package/library/nature/versions/1.0/graphs/lineup.json +24 -0
- package/{shelf → library/nature/versions/1.0/graphs}/log.json +6 -6
- package/{shelf → library/nature/versions/1.0/graphs}/rock.json +5 -4
- package/{shelf → library/nature/versions/1.0/graphs}/scatter_rocks.json +5 -5
- package/{shelf → library/nature/versions/1.0/graphs}/tree_lowpoly.json +3 -2
- package/library/nature/versions/1.0/nature.topo +12 -0
- package/library/nature/versions/1.0/version.json +534 -0
- package/{shelf → library/props/versions/1.0/graphs}/barrel.json +5 -4
- package/{shelf → library/props/versions/1.0/graphs}/crate.json +3 -3
- package/library/props/versions/1.0/graphs/lineup.json +18 -0
- package/{shelf → library/props/versions/1.0/graphs}/pallet.json +4 -3
- package/{shelf → library/props/versions/1.0/graphs}/pipe_run.json +3 -3
- package/{shelf → library/props/versions/1.0/graphs}/sandbag_wall.json +5 -5
- package/library/props/versions/1.0/props.topo +12 -0
- package/library/props/versions/1.0/version.json +336 -0
- package/{shelf → library/street/versions/1.0/graphs}/bench.json +5 -5
- package/{shelf → library/street/versions/1.0/graphs}/bollard.json +2 -1
- package/{shelf → library/street/versions/1.0/graphs}/cable.json +4 -4
- package/{shelf → library/street/versions/1.0/graphs}/fence.json +7 -7
- package/{shelf → library/street/versions/1.0/graphs}/lamp_post.json +2 -2
- package/library/street/versions/1.0/graphs/lineup.json +22 -0
- package/{shelf → library/street/versions/1.0/graphs}/road_strip.json +2 -2
- package/{shelf → library/street/versions/1.0/graphs}/sign_post.json +1 -1
- package/library/street/versions/1.0/street.topo +12 -0
- package/library/street/versions/1.0/version.json +421 -0
- package/package.json +23 -8
- package/dist/graph/migrate.d.ts +0 -23
- package/dist/graph/migrate.js +0 -58
- package/shelf/index.json +0 -190
package/dist/core/manifold.js
CHANGED
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
// level sets, 2D cross-sections). Float point/vertex attributes ride through as
|
|
3
3
|
// manifold "properties" so they are interpolated across new intersection edges.
|
|
4
4
|
import { Geometry } from './geometry.js';
|
|
5
|
-
import {
|
|
5
|
+
import { triangulate } from './topology.js';
|
|
6
|
+
import { mergeTriangles } from './polymerge.js';
|
|
7
|
+
import { primWhere, solidCauses, solidReport } from './report.js';
|
|
8
|
+
import { selfIntersections } from './analysis.js';
|
|
6
9
|
let wasm = null;
|
|
7
10
|
export async function loadManifold() {
|
|
8
11
|
if (wasm)
|
|
@@ -112,12 +115,25 @@ export function toManifold(g, L = propLayout([g])) {
|
|
|
112
115
|
return m;
|
|
113
116
|
}
|
|
114
117
|
catch (e) {
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
+
// why: each cause with its count, where the first one is and its fix (core/report.ts)
|
|
119
|
+
const r = solidReport(g), causes = solidCauses(r);
|
|
120
|
+
const seamsOnly = !causes.length && r.seamEdges > 0;
|
|
121
|
+
const crossing = !seamsOnly && !causes.length ? crossingWhere(g) : '';
|
|
122
|
+
const err = new NotManifoldError(`input is not a closed solid (manifold): ${seamsOnly
|
|
123
|
+
? `its ${r.seamEdges} open edges are uv/normal seam splits (coincident points not welded), first ${r.firstOpen ?? 'at a seam'}`
|
|
124
|
+
: causes.length ? causes.join('; ') : crossing ? `its faces cross each other: ${crossing}` : 'the mesh kernel rejected it (touching faces)'}.`);
|
|
125
|
+
err.hint = seamsOnly
|
|
126
|
+
? 'fuse the points (or file {weld:true} on import) to join the seams; Solid ops need watertight meshes.'
|
|
127
|
+
: crossing ? 'a bevel, offset or extrude too big for a thin part crosses the surface through itself: a smaller offset, or remesh through sdf_from_mesh → sdf_to_mesh.'
|
|
128
|
+
: 'Solid ops need watertight meshes: fix the causes above, or remesh through sdf_from_mesh → sdf_to_mesh. Open surfaces can use clip instead of boolean.';
|
|
118
129
|
throw err;
|
|
119
130
|
}
|
|
120
131
|
}
|
|
132
|
+
/** "3 pairs, first prim 12 of "guard" at (x, y, z)": where a closed mesh crosses itself ('' when it does not). */
|
|
133
|
+
function crossingWhere(g) {
|
|
134
|
+
const x = selfIntersections(g);
|
|
135
|
+
return x.pairs ? `${x.pairs}${x.capped ? '+' : ''} pair(s), first ${primWhere(g, x.prims.indexOf(1))}` : '';
|
|
136
|
+
}
|
|
121
137
|
/**
|
|
122
138
|
* Convert a Manifold back to Geometry. `sources` lists the input geometries with their
|
|
123
139
|
* manifold originalIDs so primitive attributes and groups can be carried over.
|
|
@@ -249,6 +265,61 @@ export function toPolygons(g, plane) {
|
|
|
249
265
|
}
|
|
250
266
|
return { polys, offset: cnt ? off / cnt : 0 };
|
|
251
267
|
}
|
|
268
|
+
/**
|
|
269
|
+
* Contours by nesting: a contour lying wholly inside another is a hole of it (depth 1), one inside a hole an island
|
|
270
|
+
* (depth 2) and so on. Returns the contours wound counter-clockwise at even depth and clockwise at odd depth (so the
|
|
271
|
+
* NonZero rule fills shapes, cuts holes and still unions shapes that only overlap), each one's depth and the index
|
|
272
|
+
* of the contour it lies directly in (-1 at the top).
|
|
273
|
+
*/
|
|
274
|
+
export function orientNested(polys) {
|
|
275
|
+
const n = polys.length;
|
|
276
|
+
const area = polys.map((q) => { let a = 0; for (let i = 0; i < q.length; i++) {
|
|
277
|
+
const p = q[i], r = q[(i + 1) % q.length];
|
|
278
|
+
a += p[0] * r[1] - r[0] * p[1];
|
|
279
|
+
} return a / 2; });
|
|
280
|
+
const box = polys.map((q) => {
|
|
281
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
282
|
+
for (const [x, y] of q) {
|
|
283
|
+
x0 = Math.min(x0, x);
|
|
284
|
+
y0 = Math.min(y0, y);
|
|
285
|
+
x1 = Math.max(x1, x);
|
|
286
|
+
y1 = Math.max(y1, y);
|
|
287
|
+
}
|
|
288
|
+
return [x0, y0, x1, y1];
|
|
289
|
+
});
|
|
290
|
+
const pointIn = (pt, q) => {
|
|
291
|
+
let inside = false;
|
|
292
|
+
for (let i = 0, j = q.length - 1; i < q.length; j = i++) {
|
|
293
|
+
const [xi, yi] = q[i], [xj, yj] = q[j];
|
|
294
|
+
if (yi > pt[1] !== yj > pt[1] && pt[0] < ((xj - xi) * (pt[1] - yi)) / (yj - yi) + xi)
|
|
295
|
+
inside = !inside;
|
|
296
|
+
}
|
|
297
|
+
return inside;
|
|
298
|
+
};
|
|
299
|
+
// a inside b: a's box within b's, and every corner of a inside b (a shape that only overlaps b is not nested)
|
|
300
|
+
const within = (a, b) => {
|
|
301
|
+
const A = box[a], B = box[b];
|
|
302
|
+
if (A[0] < B[0] || A[1] < B[1] || A[2] > B[2] || A[3] > B[3] || Math.abs(area[a]) >= Math.abs(area[b]))
|
|
303
|
+
return false;
|
|
304
|
+
return polys[a].every((pt) => pointIn(pt, polys[b]));
|
|
305
|
+
};
|
|
306
|
+
const parent = new Array(n).fill(-1), depth = new Array(n).fill(0);
|
|
307
|
+
for (let a = 0; a < n; a++) {
|
|
308
|
+
let best = -1;
|
|
309
|
+
for (let b = 0; b < n; b++)
|
|
310
|
+
if (b !== a && within(a, b) && (best < 0 || Math.abs(area[b]) < Math.abs(area[best])))
|
|
311
|
+
best = b;
|
|
312
|
+
parent[a] = best;
|
|
313
|
+
}
|
|
314
|
+
for (let a = 0; a < n; a++) {
|
|
315
|
+
let d = 0;
|
|
316
|
+
for (let r = parent[a]; r >= 0 && d <= n; r = parent[r])
|
|
317
|
+
d++;
|
|
318
|
+
depth[a] = d;
|
|
319
|
+
}
|
|
320
|
+
const out = polys.map((q, i) => ((area[i] > 0) === (depth[i] % 2 === 0) ? q : q.slice().reverse()));
|
|
321
|
+
return { polys: out, depth, parent };
|
|
322
|
+
}
|
|
252
323
|
/**
|
|
253
324
|
* A manifold CrossSection from 2D contours. No contours (empty input, points only, open curves) gives an empty
|
|
254
325
|
* section: manifold's constructor throws a raw TypeError on an empty list.
|
|
@@ -276,7 +347,7 @@ export function polygonsToGeo(polys, plane, offset = 0, asPolygons = true) {
|
|
|
276
347
|
}
|
|
277
348
|
return new Geometry({ positions: pos, prims, closed: asPolygons });
|
|
278
349
|
}
|
|
279
|
-
/**
|
|
350
|
+
/** Faces for a CrossSection in the world plane: one polygon per shape, a few for a shape with holes. */
|
|
280
351
|
export function crossSectionFaces(cs, plane, offset = 0) {
|
|
281
352
|
const W = M();
|
|
282
353
|
const parts = cs.decompose();
|
|
@@ -297,7 +368,8 @@ export function crossSectionFaces(cs, plane, offset = 0) {
|
|
|
297
368
|
const pos = [];
|
|
298
369
|
for (const [u, v] of flat)
|
|
299
370
|
pos.push(U[0] * u + V[0] * v + N[0] * offset, U[1] * u + V[1] * v + N[1] * offset, U[2] * u + V[2] * v + N[2] * offset);
|
|
300
|
-
|
|
371
|
+
// a face with holes as the few polygons it needs, not a fan of triangles
|
|
372
|
+
out.push(mergeTriangles(new Geometry({ positions: pos, prims: tris }), new Float64Array(tris.length)));
|
|
301
373
|
}
|
|
302
374
|
return Geometry.merge(out);
|
|
303
375
|
}
|
package/dist/core/materials.d.ts
CHANGED
|
@@ -5,6 +5,9 @@ export interface Material {
|
|
|
5
5
|
roughness: number;
|
|
6
6
|
emissive: [number, number, number];
|
|
7
7
|
alpha: number;
|
|
8
|
+
/** Absent = legacy auto (blend only for scalar / vertex opacity). */
|
|
9
|
+
alphaMode?: 'OPAQUE' | 'MASK' | 'BLEND';
|
|
10
|
+
alphaCutoff?: number;
|
|
8
11
|
doubleSided: boolean;
|
|
9
12
|
unlit: boolean;
|
|
10
13
|
/**
|
|
@@ -27,12 +30,20 @@ export declare const MATERIALS_DETAIL = "materials";
|
|
|
27
30
|
export declare const DEFAULT_MATERIAL: Readonly<Material>;
|
|
28
31
|
/** Fill missing fields with defaults and coerce types (tolerant of hand-written tables). */
|
|
29
32
|
export declare function normalizeMaterial(m: any): Material;
|
|
33
|
+
export declare function materialAlphaMode(m: Material, vertexAlpha?: boolean): 'OPAQUE' | 'MASK' | 'BLEND';
|
|
30
34
|
/** Every texture path used by a table (deduplicated, table order). */
|
|
31
35
|
export declare function texturePaths(t: MaterialTable): string[];
|
|
32
36
|
/** The material table stored on the geometry (empty when there is none or it does not parse). */
|
|
33
37
|
export declare function readMaterials(g: Geometry): MaterialTable;
|
|
34
38
|
/** Store a table on the geometry (mutates g: clone cooked geometry first). An empty table removes the attribute. */
|
|
35
39
|
export declare function writeMaterials(g: Geometry, t: MaterialTable): void;
|
|
40
|
+
/**
|
|
41
|
+
* The geometry with the file paths of its material textures (its own and its packed sources') rewritten by `resolve`,
|
|
42
|
+
* cloned where something changes; g itself when nothing does. `resolve` sees every texture path that names a file,
|
|
43
|
+
* relative (`relative` true) or absolute, never "image:<name>", "embedded:<name>" or a URL. A package output's result
|
|
44
|
+
* names its textures in the version's folder this way, so exports and renders read them wherever the result goes.
|
|
45
|
+
*/
|
|
46
|
+
export declare function rebaseTexturePaths(g: Geometry, resolve: (path: string, relative: boolean) => string, done?: Map<Geometry, Geometry>): Geometry;
|
|
36
47
|
/** Union of the tables of several geometries; the first definition of a name wins. */
|
|
37
48
|
export declare function mergeMaterialTables(geos: (Geometry | null | undefined)[]): MaterialTable;
|
|
38
49
|
/** Basename of a Houdini material path ("/mat/brick" → "brick"). */
|
package/dist/core/materials.js
CHANGED
|
@@ -22,11 +22,22 @@ export function normalizeMaterial(m) {
|
|
|
22
22
|
roughness: num(m?.roughness, d.roughness),
|
|
23
23
|
emissive: vec3(m?.emissive, d.emissive),
|
|
24
24
|
alpha: num(m?.alpha, d.alpha),
|
|
25
|
+
...alphaFields(m),
|
|
25
26
|
doubleSided: typeof m?.doubleSided === 'boolean' ? m.doubleSided : d.doubleSided,
|
|
26
27
|
unlit: typeof m?.unlit === 'boolean' ? m.unlit : d.unlit,
|
|
27
28
|
...textureField(m?.textures),
|
|
28
29
|
};
|
|
29
30
|
}
|
|
31
|
+
/** Keep auto materials byte-compatible with existing graphs and fingerprints. */
|
|
32
|
+
function alphaFields(m) {
|
|
33
|
+
const mode = typeof m?.alphaMode === 'string' ? m.alphaMode.toUpperCase() : '';
|
|
34
|
+
if (mode !== 'OPAQUE' && mode !== 'MASK' && mode !== 'BLEND')
|
|
35
|
+
return {};
|
|
36
|
+
return { alphaMode: mode, ...(mode === 'MASK' ? { alphaCutoff: Math.max(0, Math.min(1, num(m?.alphaCutoff, 0.5))) } : {}) };
|
|
37
|
+
}
|
|
38
|
+
export function materialAlphaMode(m, vertexAlpha = false) {
|
|
39
|
+
return m.alphaMode ?? (m.alpha < 1 || vertexAlpha ? 'BLEND' : 'OPAQUE');
|
|
40
|
+
}
|
|
30
41
|
/** { textures } with the non-empty string slots, or {} when there is none (keeps texture-free tables unchanged). */
|
|
31
42
|
function textureField(t) {
|
|
32
43
|
if (!t || typeof t !== 'object')
|
|
@@ -78,6 +89,51 @@ export function writeMaterials(g, t) {
|
|
|
78
89
|
sorted[n] = normalizeMaterial(t[n]);
|
|
79
90
|
g.detail.add(MATERIALS_DETAIL, 'string', 1).set(0, JSON.stringify(sorted));
|
|
80
91
|
}
|
|
92
|
+
/** A texture path that names a file relative to a folder: not "image:<name>", "embedded:<name>", a URL or an absolute path. */
|
|
93
|
+
const relativeTexture = (p) => !!p && !/^[A-Za-z][A-Za-z0-9+.-]*:/.test(p) && !/^[\\/]/.test(p);
|
|
94
|
+
/** An absolute file path: "/x", "\\server\x" or a drive ("C:/x", "C:\x"). */
|
|
95
|
+
const absoluteTexture = (p) => !!p && (/^[\\/]/.test(p) || /^[A-Za-z]:[\\/]/.test(p));
|
|
96
|
+
/**
|
|
97
|
+
* The geometry with the file paths of its material textures (its own and its packed sources') rewritten by `resolve`,
|
|
98
|
+
* cloned where something changes; g itself when nothing does. `resolve` sees every texture path that names a file,
|
|
99
|
+
* relative (`relative` true) or absolute, never "image:<name>", "embedded:<name>" or a URL. A package output's result
|
|
100
|
+
* names its textures in the version's folder this way, so exports and renders read them wherever the result goes.
|
|
101
|
+
*/
|
|
102
|
+
export function rebaseTexturePaths(g, resolve, done = new Map()) {
|
|
103
|
+
const seen = done.get(g);
|
|
104
|
+
if (seen)
|
|
105
|
+
return seen;
|
|
106
|
+
let out = g;
|
|
107
|
+
const table = readMaterials(g);
|
|
108
|
+
let changed = false;
|
|
109
|
+
for (const m of Object.values(table))
|
|
110
|
+
for (const k of TEXTURE_SLOTS) {
|
|
111
|
+
const p = m.textures?.[k];
|
|
112
|
+
const relative = relativeTexture(p);
|
|
113
|
+
if (!relative && !absoluteTexture(p))
|
|
114
|
+
continue;
|
|
115
|
+
const to = resolve(p, relative);
|
|
116
|
+
if (to !== p) {
|
|
117
|
+
m.textures[k] = to;
|
|
118
|
+
changed = true;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (changed) {
|
|
122
|
+
out = g.clone();
|
|
123
|
+
writeMaterials(out, table);
|
|
124
|
+
}
|
|
125
|
+
// packed sources are shared and never modified: a rewritten source is a new one
|
|
126
|
+
if (g.primKind) {
|
|
127
|
+
const sources = g.packedSources.map((s) => rebaseTexturePaths(s, resolve, done));
|
|
128
|
+
if (sources.some((s, i) => s !== g.packedSources[i])) {
|
|
129
|
+
if (out === g)
|
|
130
|
+
out = g.clone();
|
|
131
|
+
out.packedSources = sources;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
done.set(g, out);
|
|
135
|
+
return out;
|
|
136
|
+
}
|
|
81
137
|
/** Union of the tables of several geometries; the first definition of a name wins. */
|
|
82
138
|
export function mergeMaterialTables(geos) {
|
|
83
139
|
const out = {};
|
package/dist/core/noise.d.ts
CHANGED
|
@@ -11,5 +11,14 @@ export interface FractalOpts {
|
|
|
11
11
|
}
|
|
12
12
|
/** Layer a base noise into a fractal. Output is normalised back to roughly the base range. */
|
|
13
13
|
export declare function fractal(base: Noise3, o?: FractalOpts): Noise3;
|
|
14
|
+
/**
|
|
15
|
+
* Domain warp: two decorrelated fBm simplex noises, roughly in [-1, 1] (at most 1 in magnitude), for the X and Z offsets
|
|
16
|
+
* of another noise's lookup: n(p + amplitude × (warp.x(q), warp.z(q))) with q = p × frequency bends straight
|
|
17
|
+
* features into meanders. Deterministic per seed.
|
|
18
|
+
*/
|
|
19
|
+
export declare function makeWarp(seed?: number, octaves?: number): {
|
|
20
|
+
x: Noise3;
|
|
21
|
+
z: Noise3;
|
|
22
|
+
};
|
|
14
23
|
/** Divergence-free curl noise from three decorrelated potentials. */
|
|
15
24
|
export declare function makeCurl(n: Noise3, eps?: number): (x: number, y: number, z: number) => [number, number, number];
|
package/dist/core/noise.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Noise library: Perlin (improved), simplex (via simplex-noise), Worley/cellular,
|
|
2
|
-
// value noise, fractal layering (fBm / ridged / turbulence) and curl noise.
|
|
2
|
+
// value noise, fractal layering (fBm / ridged / turbulence), domain warp and curl noise.
|
|
3
3
|
// All generators are seeded and return values roughly in [-1, 1] (worley: [0, ~1]).
|
|
4
4
|
import { createNoise3D } from 'simplex-noise';
|
|
5
5
|
import { rng, hash32 } from './random.js';
|
|
@@ -123,6 +123,15 @@ export function fractal(base, o = {}) {
|
|
|
123
123
|
return mode === 'ridged' || mode === 'turbulence' ? v * 2 - 1 : v;
|
|
124
124
|
};
|
|
125
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Domain warp: two decorrelated fBm simplex noises, roughly in [-1, 1] (at most 1 in magnitude), for the X and Z offsets
|
|
128
|
+
* of another noise's lookup: n(p + amplitude × (warp.x(q), warp.z(q))) with q = p × frequency bends straight
|
|
129
|
+
* features into meanders. Deterministic per seed.
|
|
130
|
+
*/
|
|
131
|
+
export function makeWarp(seed = 0, octaves = 3) {
|
|
132
|
+
const n = fractal(makeNoise('simplex', seed), { octaves, roughness: 0.5, lacunarity: 2.01, fractal: 'fbm' });
|
|
133
|
+
return { x: n, z: (x, y, z) => n(x + 71.37, y + 23.11, z - 47.21) };
|
|
134
|
+
}
|
|
126
135
|
/** Divergence-free curl noise from three decorrelated potentials. */
|
|
127
136
|
export function makeCurl(n, eps = 1e-3) {
|
|
128
137
|
const pA = (x, y, z) => n(x, y, z);
|
package/dist/core/packed.js
CHANGED
|
@@ -21,6 +21,7 @@ import { mergeMaterialTables, writeMaterials } from './materials.js';
|
|
|
21
21
|
import { m4normalMatrix, qFromMat, qMul } from './math.js';
|
|
22
22
|
import { resolveVertexNormals } from './topology.js';
|
|
23
23
|
import { splineCounts, splinesToPolylines } from './splines.js';
|
|
24
|
+
import { TANGENT_ATTRIB, transformTangents } from './tangents.js';
|
|
24
25
|
/**
|
|
25
26
|
* Default triangle budget of the renderer: above it, an evenly spread subset of the instances is drawn. The software
|
|
26
27
|
* rasteriser spends about 2 s on per-mesh analysis and 0.5 s per view for 1.5M triangles; 600k keeps a quad view of
|
|
@@ -294,6 +295,12 @@ export function expandPacked(g, o = {}) {
|
|
|
294
295
|
if (a && a.type === 'float' && a.size === 3)
|
|
295
296
|
vecAttrs.push({ c, a });
|
|
296
297
|
}
|
|
298
|
+
const tanAttrs = [];
|
|
299
|
+
for (const c of ['point', 'vertex']) {
|
|
300
|
+
const a = tables[c].get(TANGENT_ATTRIB);
|
|
301
|
+
if (a && a.type === 'float' && a.size === 4)
|
|
302
|
+
tanAttrs.push({ c, a });
|
|
303
|
+
}
|
|
297
304
|
const orientA = out.points.get('orient');
|
|
298
305
|
const orient = orientA && orientA.type === 'float' && orientA.size === 4 ? orientA.data : null;
|
|
299
306
|
const OP = out.P;
|
|
@@ -361,6 +368,10 @@ export function expandPacked(g, o = {}) {
|
|
|
361
368
|
d[i * 3 + 2] = m[2] * x + m[6] * y + m[10] * z;
|
|
362
369
|
}
|
|
363
370
|
}
|
|
371
|
+
for (const { c, a: A } of tanAttrs) {
|
|
372
|
+
const [e0, e1] = range(c, j);
|
|
373
|
+
transformTangents(A.data.subarray(e0 * 4, e1 * 4), e1 - e0, xform3(m));
|
|
374
|
+
}
|
|
364
375
|
// one-level expansion: instances inside the source compose this instance's transform
|
|
365
376
|
if (out.primKind)
|
|
366
377
|
composePackedXforms(out, m, null, offs[j * 3 + 2], offs[j * 3 + 5]);
|
package/dist/core/particles.d.ts
CHANGED
|
@@ -153,7 +153,7 @@ export interface EmitParams {
|
|
|
153
153
|
* `max_particles` counts every particle from its birth through its death step, including the ones a collision removed
|
|
154
154
|
* (listed in __pholders). `end` is s1, or the end of the first earlier step at which this call's births reach
|
|
155
155
|
* `maxBirths`: the caller integrates [s0, end) and continues from there, which gives the same result as one run (runs
|
|
156
|
-
* can be cut anywhere) while the births held at once stay bounded (
|
|
156
|
+
* can be cut anywhere) while the births held at once stay bounded (a high rate with a short life over many
|
|
157
157
|
* steps materialised every birth of the run and exhausted the heap).
|
|
158
158
|
*/
|
|
159
159
|
export declare function emitSteps(state: Geometry | null, em: EmitterData, s0: number, s1: number, ep: EmitParams): {
|
package/dist/core/particles.js
CHANGED
|
@@ -645,7 +645,7 @@ function phaseOf(ep, key) {
|
|
|
645
645
|
* `max_particles` counts every particle from its birth through its death step, including the ones a collision removed
|
|
646
646
|
* (listed in __pholders). `end` is s1, or the end of the first earlier step at which this call's births reach
|
|
647
647
|
* `maxBirths`: the caller integrates [s0, end) and continues from there, which gives the same result as one run (runs
|
|
648
|
-
* can be cut anywhere) while the births held at once stay bounded (
|
|
648
|
+
* can be cut anywhere) while the births held at once stay bounded (a high rate with a short life over many
|
|
649
649
|
* steps materialised every birth of the run and exhausted the heap).
|
|
650
650
|
*/
|
|
651
651
|
export function emitSteps(state, em, s0, s1, ep) {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Geometry } from './geometry.ts';
|
|
2
|
+
export interface MergeOptions {
|
|
3
|
+
/** Neighbouring triangles join only while their normals agree to this cosine (default about 0.5°). */
|
|
4
|
+
planarCos?: number;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Merge edge-connected flat triangles with the same key (≥ 0) into polygons. A region whose outline is one loop becomes
|
|
8
|
+
* one polygon. A region with holes is cut along straight diagonals between its own points, two per hole, into simple
|
|
9
|
+
* polygons (the shortest pair of cuts, so the pieces stay well shaped). A region whose outline touches itself, or that
|
|
10
|
+
* no clean cut splits, is grown into polygons triangle by triangle instead. Zero-area triangles join any neighbour.
|
|
11
|
+
* Prims that are not triangles, or have key -1, are kept as they are. Prim attributes and groups come from the
|
|
12
|
+
* region's first triangle; vertex attributes from the triangle that brought each corner.
|
|
13
|
+
*/
|
|
14
|
+
export declare function mergeTriangles(g: Geometry, key: ArrayLike<number>, o?: MergeOptions): Geometry;
|
|
15
|
+
/**
|
|
16
|
+
* Cut an outline (counter-clockwise) with holes (clockwise) into simple polygons over the same points: for one hole,
|
|
17
|
+
* two diagonals from its two extreme points (along x or along y, whichever pair is shorter) to the nearest outline
|
|
18
|
+
* points they reach without crossing anything split the shape in two; the other holes go with the piece they lie in,
|
|
19
|
+
* and each piece is cut again. Null when no clean pair of cuts exists.
|
|
20
|
+
*/
|
|
21
|
+
export declare function splitHoles(xy: Map<number, [number, number]>, outer: number[], holes: number[][]): number[][] | null;
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merge edge-connected flat triangles with the same key (≥ 0) into polygons. A region whose outline is one loop becomes
|
|
3
|
+
* one polygon. A region with holes is cut along straight diagonals between its own points, two per hole, into simple
|
|
4
|
+
* polygons (the shortest pair of cuts, so the pieces stay well shaped). A region whose outline touches itself, or that
|
|
5
|
+
* no clean cut splits, is grown into polygons triangle by triangle instead. Zero-area triangles join any neighbour.
|
|
6
|
+
* Prims that are not triangles, or have key -1, are kept as they are. Prim attributes and groups come from the
|
|
7
|
+
* region's first triangle; vertex attributes from the triangle that brought each corner.
|
|
8
|
+
*/
|
|
9
|
+
export function mergeTriangles(g, key, o = {}) {
|
|
10
|
+
if (g.primKind)
|
|
11
|
+
return g;
|
|
12
|
+
const n = g.numPrims, vp = g.vertexPoint, ps = g.primStart, P = g.P;
|
|
13
|
+
const cosTol = o.planarCos ?? 0.99996;
|
|
14
|
+
const isTri = (t) => !!g.primClosed[t] && ps[t + 1] - ps[t] === 3 && key[t] >= 0;
|
|
15
|
+
const nrm = new Float64Array(n * 3), area = new Float64Array(n);
|
|
16
|
+
for (let t = 0; t < n; t++) {
|
|
17
|
+
if (!isTri(t))
|
|
18
|
+
continue;
|
|
19
|
+
const a = vp[ps[t]] * 3, b = vp[ps[t] + 1] * 3, c = vp[ps[t] + 2] * 3;
|
|
20
|
+
const ux = P[b] - P[a], uy = P[b + 1] - P[a + 1], uz = P[b + 2] - P[a + 2];
|
|
21
|
+
const wx = P[c] - P[a], wy = P[c + 1] - P[a + 1], wz = P[c + 2] - P[a + 2];
|
|
22
|
+
const x = uy * wz - uz * wy, y = uz * wx - ux * wz, z = ux * wy - uy * wx, l = Math.hypot(x, y, z);
|
|
23
|
+
area[t] = l / 2;
|
|
24
|
+
if (l > 0) {
|
|
25
|
+
nrm[t * 3] = x / l;
|
|
26
|
+
nrm[t * 3 + 1] = y / l;
|
|
27
|
+
nrm[t * 3 + 2] = z / l;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const np = g.numPoints;
|
|
31
|
+
const hk = (a, b) => a * np + b;
|
|
32
|
+
const half = new Map();
|
|
33
|
+
for (let t = 0; t < n; t++) {
|
|
34
|
+
if (!isTri(t))
|
|
35
|
+
continue;
|
|
36
|
+
for (let k = 0; k < 3; k++)
|
|
37
|
+
half.set(hk(vp[ps[t] + k], vp[ps[t] + ((k + 1) % 3)]), t);
|
|
38
|
+
}
|
|
39
|
+
const flatWith = (t, u) => area[t] === 0 || area[u] === 0 || nrm[t * 3] * nrm[u * 3] + nrm[t * 3 + 1] * nrm[u * 3 + 1] + nrm[t * 3 + 2] * nrm[u * 3 + 2] >= cosTol;
|
|
40
|
+
// regions: same key, flat across the shared edge
|
|
41
|
+
const parent = Int32Array.from({ length: n }, (_, i) => i);
|
|
42
|
+
const find = (x) => { while (parent[x] !== x) {
|
|
43
|
+
parent[x] = parent[parent[x]];
|
|
44
|
+
x = parent[x];
|
|
45
|
+
} return x; };
|
|
46
|
+
for (let t = 0; t < n; t++) {
|
|
47
|
+
if (!isTri(t))
|
|
48
|
+
continue;
|
|
49
|
+
for (let k = 0; k < 3; k++) {
|
|
50
|
+
const u = half.get(hk(vp[ps[t] + ((k + 1) % 3)], vp[ps[t] + k]));
|
|
51
|
+
if (u === undefined || u <= t || key[u] !== key[t] || !flatWith(t, u))
|
|
52
|
+
continue;
|
|
53
|
+
const a = find(t), b = find(u);
|
|
54
|
+
if (a !== b)
|
|
55
|
+
parent[Math.max(a, b)] = Math.min(a, b);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const members = new Map();
|
|
59
|
+
for (let t = 0; t < n; t++)
|
|
60
|
+
if (isTri(t)) {
|
|
61
|
+
const r = find(t);
|
|
62
|
+
let l = members.get(r);
|
|
63
|
+
if (!l)
|
|
64
|
+
members.set(r, l = []);
|
|
65
|
+
l.push(t);
|
|
66
|
+
}
|
|
67
|
+
const outPrims = [], outVerts = [], outStart = [0];
|
|
68
|
+
const emit = (prim, verts) => { outPrims.push(prim); for (const v of verts)
|
|
69
|
+
outVerts.push(v); outStart.push(outVerts.length); };
|
|
70
|
+
for (let t = 0; t < n; t++) {
|
|
71
|
+
if (!isTri(t)) {
|
|
72
|
+
const vs = [];
|
|
73
|
+
for (let v = ps[t]; v < ps[t + 1]; v++)
|
|
74
|
+
vs.push(v);
|
|
75
|
+
emit(t, vs);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const tris = members.get(t);
|
|
79
|
+
if (!tris)
|
|
80
|
+
continue; // not the region's first triangle
|
|
81
|
+
if (tris.length === 1) {
|
|
82
|
+
emit(t, [ps[t], ps[t] + 1, ps[t] + 2]);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
for (const poly of regionPolygons(tris))
|
|
86
|
+
emit(t, poly);
|
|
87
|
+
}
|
|
88
|
+
if (outPrims.length === n)
|
|
89
|
+
return g;
|
|
90
|
+
const out = g.clone();
|
|
91
|
+
out.prims = g.prims.gather(outPrims);
|
|
92
|
+
out.vertices = g.vertices.gather(outVerts);
|
|
93
|
+
out.primStart = Int32Array.from(outStart);
|
|
94
|
+
out.vertexPoint = Int32Array.from(outVerts, (v) => vp[v]);
|
|
95
|
+
out.primClosed = Uint8Array.from(outPrims, (p) => g.primClosed[p]);
|
|
96
|
+
out.primGroups = new Map([...g.primGroups].map(([k, m]) => [k, Uint8Array.from(outPrims, (p) => m[p])]));
|
|
97
|
+
out.vertexGroups = new Map([...g.vertexGroups].map(([k, m]) => [k, Uint8Array.from(outVerts, (v) => m[v])]));
|
|
98
|
+
return out;
|
|
99
|
+
/** Polygons (as source vertex lists) covering one flat region. */
|
|
100
|
+
function regionPolygons(tris) {
|
|
101
|
+
const inRegion = new Set(tris);
|
|
102
|
+
// boundary half-edges a → b, with the source vertex of a in its triangle
|
|
103
|
+
const next = new Map(), vtxAt = new Map();
|
|
104
|
+
let pinched = false;
|
|
105
|
+
for (const t of tris)
|
|
106
|
+
for (let k = 0; k < 3; k++) {
|
|
107
|
+
const a = vp[ps[t] + k], b = vp[ps[t] + ((k + 1) % 3)];
|
|
108
|
+
const u = half.get(hk(b, a));
|
|
109
|
+
if (u !== undefined && inRegion.has(u))
|
|
110
|
+
continue;
|
|
111
|
+
if (next.has(a))
|
|
112
|
+
pinched = true;
|
|
113
|
+
next.set(a, b);
|
|
114
|
+
vtxAt.set(a, ps[t] + k);
|
|
115
|
+
}
|
|
116
|
+
if (!pinched) {
|
|
117
|
+
const loops = [], seen = new Set();
|
|
118
|
+
for (const s of next.keys()) {
|
|
119
|
+
if (seen.has(s))
|
|
120
|
+
continue;
|
|
121
|
+
const loop = [];
|
|
122
|
+
for (let a = s; !seen.has(a); a = next.get(a)) {
|
|
123
|
+
seen.add(a);
|
|
124
|
+
loop.push(a);
|
|
125
|
+
if (!next.has(next.get(a))) {
|
|
126
|
+
pinched = true;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
loops.push(loop);
|
|
131
|
+
}
|
|
132
|
+
if (!pinched && loops.length === 1)
|
|
133
|
+
return [loops[0].map((a) => vtxAt.get(a))];
|
|
134
|
+
if (!pinched) {
|
|
135
|
+
const cut = cutHoles(loops, tris);
|
|
136
|
+
if (cut)
|
|
137
|
+
return cut.map((poly) => poly.map((a) => vtxAt.get(a)));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return grow(tris);
|
|
141
|
+
}
|
|
142
|
+
/** A region with holes cut into simple polygons along diagonals; null when it cannot be done cleanly. */
|
|
143
|
+
function cutHoles(loops, tris) {
|
|
144
|
+
// 2D frame in the region's plane
|
|
145
|
+
let nx = 0, ny = 0, nz = 0;
|
|
146
|
+
for (const t of tris) {
|
|
147
|
+
nx += nrm[t * 3] * area[t];
|
|
148
|
+
ny += nrm[t * 3 + 1] * area[t];
|
|
149
|
+
nz += nrm[t * 3 + 2] * area[t];
|
|
150
|
+
}
|
|
151
|
+
const l = Math.hypot(nx, ny, nz);
|
|
152
|
+
if (!(l > 0))
|
|
153
|
+
return null;
|
|
154
|
+
nx /= l;
|
|
155
|
+
ny /= l;
|
|
156
|
+
nz /= l;
|
|
157
|
+
const ax = Math.abs(nx) < 0.9 ? [1, 0, 0] : [0, 1, 0];
|
|
158
|
+
let ux = ny * ax[2] - nz * ax[1], uy = nz * ax[0] - nx * ax[2], uz = nx * ax[1] - ny * ax[0];
|
|
159
|
+
const ul = Math.hypot(ux, uy, uz);
|
|
160
|
+
ux /= ul;
|
|
161
|
+
uy /= ul;
|
|
162
|
+
uz /= ul;
|
|
163
|
+
const vx = ny * uz - nz * uy, vy = nz * ux - nx * uz, vz = nx * uy - ny * ux;
|
|
164
|
+
const xy = new Map();
|
|
165
|
+
for (const loop of loops)
|
|
166
|
+
for (const a of loop)
|
|
167
|
+
xy.set(a, [P[a * 3] * ux + P[a * 3 + 1] * uy + P[a * 3 + 2] * uz, P[a * 3] * vx + P[a * 3 + 1] * vy + P[a * 3 + 2] * vz]);
|
|
168
|
+
const outer = loops.filter((q) => signedArea(q, xy) > 0);
|
|
169
|
+
if (outer.length !== 1)
|
|
170
|
+
return null;
|
|
171
|
+
return splitHoles(xy, outer[0], loops.filter((q) => q !== outer[0]));
|
|
172
|
+
}
|
|
173
|
+
/** Fallback: polygons grown triangle by triangle while the outline stays one simple loop. */
|
|
174
|
+
function grow(tris) {
|
|
175
|
+
const free = new Set(tris), polys = [];
|
|
176
|
+
const corner = (t, pt) => (vp[ps[t]] === pt ? ps[t] : vp[ps[t] + 1] === pt ? ps[t] + 1 : ps[t] + 2);
|
|
177
|
+
for (const seed of tris) {
|
|
178
|
+
if (!free.has(seed))
|
|
179
|
+
continue;
|
|
180
|
+
free.delete(seed);
|
|
181
|
+
const pts = [vp[ps[seed]], vp[ps[seed] + 1], vp[ps[seed] + 2]], src = [ps[seed], ps[seed] + 1, ps[seed] + 2];
|
|
182
|
+
const on = new Set(pts);
|
|
183
|
+
for (let grew = true; grew;) {
|
|
184
|
+
grew = false;
|
|
185
|
+
for (let i = 0; i < pts.length; i++) {
|
|
186
|
+
const m = pts.length, a = pts[i], b = pts[(i + 1) % m];
|
|
187
|
+
const u = half.get(hk(b, a));
|
|
188
|
+
if (u === undefined || !free.has(u))
|
|
189
|
+
continue;
|
|
190
|
+
let c = -1;
|
|
191
|
+
for (let k = 0; k < 3; k++) {
|
|
192
|
+
const q = vp[ps[u] + k];
|
|
193
|
+
if (q !== a && q !== b)
|
|
194
|
+
c = q;
|
|
195
|
+
}
|
|
196
|
+
if (c < 0)
|
|
197
|
+
continue;
|
|
198
|
+
if (!on.has(c)) {
|
|
199
|
+
pts.splice(i + 1, 0, c);
|
|
200
|
+
src.splice(i + 1, 0, corner(u, c));
|
|
201
|
+
on.add(c);
|
|
202
|
+
}
|
|
203
|
+
else if (m > 3 && pts[(i + 2) % m] === c) {
|
|
204
|
+
const j = (i + 1) % m;
|
|
205
|
+
pts.splice(j, 1);
|
|
206
|
+
src.splice(j, 1);
|
|
207
|
+
on.delete(b);
|
|
208
|
+
i--;
|
|
209
|
+
}
|
|
210
|
+
else if (m > 3 && pts[(i - 1 + m) % m] === c) {
|
|
211
|
+
pts.splice(i, 1);
|
|
212
|
+
src.splice(i, 1);
|
|
213
|
+
on.delete(a);
|
|
214
|
+
i = Math.max(-1, i - 2);
|
|
215
|
+
}
|
|
216
|
+
else
|
|
217
|
+
continue;
|
|
218
|
+
free.delete(u);
|
|
219
|
+
grew = true;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
polys.push(src);
|
|
223
|
+
}
|
|
224
|
+
return polys;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
function signedArea(loop, xy) {
|
|
228
|
+
let a = 0;
|
|
229
|
+
for (let i = 0; i < loop.length; i++) {
|
|
230
|
+
const p = xy.get(loop[i]), q = xy.get(loop[(i + 1) % loop.length]);
|
|
231
|
+
a += p[0] * q[1] - q[0] * p[1];
|
|
232
|
+
}
|
|
233
|
+
return a / 2;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Cut an outline (counter-clockwise) with holes (clockwise) into simple polygons over the same points: for one hole,
|
|
237
|
+
* two diagonals from its two extreme points (along x or along y, whichever pair is shorter) to the nearest outline
|
|
238
|
+
* points they reach without crossing anything split the shape in two; the other holes go with the piece they lie in,
|
|
239
|
+
* and each piece is cut again. Null when no clean pair of cuts exists.
|
|
240
|
+
*/
|
|
241
|
+
export function splitHoles(xy, outer, holes) {
|
|
242
|
+
if (!holes.length)
|
|
243
|
+
return [outer];
|
|
244
|
+
const edges = [];
|
|
245
|
+
for (const loop of [outer, ...holes])
|
|
246
|
+
for (let i = 0; i < loop.length; i++)
|
|
247
|
+
edges.push([loop[i], loop[(i + 1) % loop.length]]);
|
|
248
|
+
const at = (a) => xy.get(a);
|
|
249
|
+
const inPoly = (pt, loop) => {
|
|
250
|
+
let inside = false;
|
|
251
|
+
for (let i = 0, j = loop.length - 1; i < loop.length; j = i++) {
|
|
252
|
+
const [xi, yi] = at(loop[i]), [xj, yj] = at(loop[j]);
|
|
253
|
+
if (yi > pt[1] !== yj > pt[1] && pt[0] < ((xj - xi) * (pt[1] - yi)) / (yj - yi) + xi)
|
|
254
|
+
inside = !inside;
|
|
255
|
+
}
|
|
256
|
+
return inside;
|
|
257
|
+
};
|
|
258
|
+
const clear = (a, b) => {
|
|
259
|
+
const A = at(a), B = at(b);
|
|
260
|
+
for (const [c, d] of edges) {
|
|
261
|
+
if (c === a || c === b || d === a || d === b)
|
|
262
|
+
continue;
|
|
263
|
+
if (segmentsMeet(A, B, at(c), at(d)))
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
const mid = [(A[0] + B[0]) / 2, (A[1] + B[1]) / 2];
|
|
267
|
+
return inPoly(mid, outer) && !holes.some((h) => inPoly(mid, h));
|
|
268
|
+
};
|
|
269
|
+
const reach = (h) => {
|
|
270
|
+
const H = at(h);
|
|
271
|
+
const order = outer.map((o, i) => ({ i, d: Math.hypot(at(o)[0] - H[0], at(o)[1] - H[1]) })).sort((x, y) => x.d - y.d);
|
|
272
|
+
for (const { i, d } of order)
|
|
273
|
+
if (clear(h, outer[i]))
|
|
274
|
+
return { i, d };
|
|
275
|
+
return null;
|
|
276
|
+
};
|
|
277
|
+
const walk = (loop, from, to) => {
|
|
278
|
+
const out = [];
|
|
279
|
+
for (let i = from;; i = (i + 1) % loop.length) {
|
|
280
|
+
out.push(loop[i]);
|
|
281
|
+
if (i === to)
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
return out;
|
|
285
|
+
};
|
|
286
|
+
for (let hi = 0; hi < holes.length; hi++) {
|
|
287
|
+
const H = holes[hi];
|
|
288
|
+
let best = null;
|
|
289
|
+
for (const axis of [0, 1]) {
|
|
290
|
+
let hL = 0, hR = 0;
|
|
291
|
+
for (let k = 1; k < H.length; k++) {
|
|
292
|
+
if (at(H[k])[axis] < at(H[hL])[axis])
|
|
293
|
+
hL = k;
|
|
294
|
+
if (at(H[k])[axis] > at(H[hR])[axis])
|
|
295
|
+
hR = k;
|
|
296
|
+
}
|
|
297
|
+
if (hL === hR)
|
|
298
|
+
continue;
|
|
299
|
+
const L = reach(H[hL]), R = reach(H[hR]);
|
|
300
|
+
if (!L || !R || L.i === R.i)
|
|
301
|
+
continue;
|
|
302
|
+
if (segmentsMeet(at(H[hL]), at(outer[L.i]), at(H[hR]), at(outer[R.i])))
|
|
303
|
+
continue;
|
|
304
|
+
if (!best || L.d + R.d < best.len)
|
|
305
|
+
best = { oL: L.i, oR: R.i, hL, hR, len: L.d + R.d };
|
|
306
|
+
}
|
|
307
|
+
if (!best)
|
|
308
|
+
continue;
|
|
309
|
+
const p1 = [...walk(outer, best.oL, best.oR), ...walk(H, best.hR, best.hL)];
|
|
310
|
+
const p2 = [...walk(outer, best.oR, best.oL), ...walk(H, best.hL, best.hR)];
|
|
311
|
+
const rest = holes.filter((_, j) => j !== hi);
|
|
312
|
+
const in1 = rest.filter((h) => inPoly(at(h[0]), p1)), in2 = rest.filter((h) => !in1.includes(h));
|
|
313
|
+
const a = splitHoles(xy, p1, in1), b = a && splitHoles(xy, p2, in2);
|
|
314
|
+
if (a && b)
|
|
315
|
+
return [...a, ...b];
|
|
316
|
+
}
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
/** Do segments ab and cd touch or cross (collinear overlaps included)? */
|
|
320
|
+
function segmentsMeet(a, b, c, d) {
|
|
321
|
+
const o = (p, q, r) => (q[0] - p[0]) * (r[1] - p[1]) - (q[1] - p[1]) * (r[0] - p[0]);
|
|
322
|
+
const d1 = o(c, d, a), d2 = o(c, d, b), d3 = o(a, b, c), d4 = o(a, b, d);
|
|
323
|
+
if (((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) && ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0)))
|
|
324
|
+
return true;
|
|
325
|
+
const on = (p, q, r) => Math.min(p[0], q[0]) <= r[0] && r[0] <= Math.max(p[0], q[0]) && Math.min(p[1], q[1]) <= r[1] && r[1] <= Math.max(p[1], q[1]);
|
|
326
|
+
return (d1 === 0 && on(c, d, a)) || (d2 === 0 && on(c, d, b)) || (d3 === 0 && on(a, b, c)) || (d4 === 0 && on(a, b, d));
|
|
327
|
+
}
|