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/nodes/curves.js
CHANGED
|
@@ -4,12 +4,15 @@
|
|
|
4
4
|
import { GeoBuilder, Geometry } from '../core/geometry.js';
|
|
5
5
|
import { defineNode } from '../graph/registry.js';
|
|
6
6
|
import { NodeError } from '../graph/types.js';
|
|
7
|
-
import { add, cross, dot, length, normalize, scale, sub } from '../core/math.js';
|
|
8
|
-
import { orientOutward, topologyReport } from '../core/topology.js';
|
|
9
|
-
import {
|
|
7
|
+
import { add, cross, dot, length, m4applyDir, m4euler, normalize, scale, sub } from '../core/math.js';
|
|
8
|
+
import { orientOutward, primNormals, topologyReport } from '../core/topology.js';
|
|
9
|
+
import { mergeTriangles } from '../core/polymerge.js';
|
|
10
|
+
import earcut from 'earcut';
|
|
11
|
+
import { crossSection, crossSectionFaces, detectPlane, fromManifold, orientNested, planeMatrix, polygonsToGeo, propLayout, toPolygons } from '../core/manifold.js';
|
|
10
12
|
import { FRAMES, P, mask } from './util.js';
|
|
11
13
|
import { evalRamp } from '../graph/params.js';
|
|
12
14
|
import { arcTable, evalBasis, evaluateSplines, samplesByLength, splineFromPrim, tangentAt, tidySplineAttribs } from '../core/splines.js';
|
|
15
|
+
import { filletCurves } from './splines.js';
|
|
13
16
|
// ---------------------------------------------------------------- helpers
|
|
14
17
|
/** Catmull–Rom (centripetal) through points; returns dense samples. */
|
|
15
18
|
export function catmullRom(pts, closed, sub) {
|
|
@@ -189,23 +192,49 @@ defineNode({
|
|
|
189
192
|
defineNode({
|
|
190
193
|
type: 'curve',
|
|
191
194
|
category: 'curve',
|
|
192
|
-
summary: 'Curve through explicit points; optional Catmull-Rom smoothing; open or closed.',
|
|
195
|
+
summary: 'Curve through explicit points; optional Catmull-Rom smoothing or rounded corners; open or closed.',
|
|
196
|
+
description: 'corner_radius rounds the corners with arcs tangent to both edges: one number for every corner, or a list with one radius per point (0 keeps that corner sharp; the ends of an open curve are never rounded). Radii that do not fit between neighbouring corners shrink. A point that is not three numbers (NaN, null, a missing coordinate) is an error that names it.',
|
|
193
197
|
aliases: ['polyline', 'path', 'spline'],
|
|
194
198
|
inputs: [],
|
|
195
199
|
params: {
|
|
196
200
|
points: { type: 'json', default: [[0, 0, 0], [1, 0.5, 0], [2, 0, 0]], desc: 'list of [x,y,z] (2-number entries → [x, 0, z] on the ground)', aliases: ['positions', 'pts'] },
|
|
197
201
|
closed: { type: 'bool', default: false, desc: 'close the curve (makes a polygon)' },
|
|
198
|
-
smooth: { type: 'bool', default: false, desc: 'Catmull-Rom interpolate through the points' },
|
|
202
|
+
smooth: { type: 'bool', default: false, desc: 'Catmull-Rom interpolate through the points (not with corner_radius)' },
|
|
199
203
|
subdivisions: { type: 'int', default: 8, min: 1, max: 10000, desc: 'smooth: samples per segment' },
|
|
204
|
+
corner_radius: { type: 'json', default: 0, desc: 'round the corners: a radius for every corner, or a list with one per point (0 = sharp)', aliases: ['fillet', 'radius', 'radii', 'corner_radii'] },
|
|
205
|
+
corner_segments: { type: 'int', default: 6, min: 1, max: 10000, desc: 'segments per rounded corner' },
|
|
200
206
|
},
|
|
207
|
+
outputs: 'point group fillet (the points of rounded corners)',
|
|
201
208
|
example: { type: 'curve', params: { points: [[0, 0, 0], [2, 0, 1], [4, 0, -1], [6, 0, 0]], smooth: true, subdivisions: 12 } },
|
|
202
|
-
cook: ({ p }) => {
|
|
209
|
+
cook: ({ p, warn }) => {
|
|
203
210
|
if (!Array.isArray(p.points) || p.points.length < 2)
|
|
204
211
|
throw new NodeError('points must be a list of at least 2 [x,y,z]');
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
212
|
+
const pts = p.points.map((q, i) => {
|
|
213
|
+
const ok = Array.isArray(q) && (q.length === 2 || q.length === 3) && q.every((x) => typeof x === 'number' && Number.isFinite(x));
|
|
214
|
+
const v = ok ? (q.length === 2 ? [q[0], 0, q[1]] : [q[0], q[1], q[2]]) : [];
|
|
215
|
+
if (!ok)
|
|
216
|
+
throw new NodeError(`point ${i} is not three numbers: ${JSON.stringify(q)}`, 'every entry of points is [x, y, z] (or [x, z] on the ground); an expression that divides by zero gives NaN');
|
|
217
|
+
return v;
|
|
218
|
+
});
|
|
219
|
+
const radii = p.corner_radius;
|
|
220
|
+
const perPoint = Array.isArray(radii);
|
|
221
|
+
if (perPoint && radii.length !== pts.length)
|
|
222
|
+
throw new NodeError(`corner_radius has ${radii.length} entries for ${pts.length} points`, 'give one radius per point (0 keeps a corner sharp), or one number for every corner');
|
|
223
|
+
if (!perPoint && !(typeof radii === 'number' && Number.isFinite(radii)))
|
|
224
|
+
throw new NodeError(`corner_radius must be a number or a list of numbers, got ${JSON.stringify(radii)}`);
|
|
225
|
+
if (perPoint && !radii.every((r) => typeof r === 'number' && Number.isFinite(r)))
|
|
226
|
+
throw new NodeError('corner_radius: every entry must be a number');
|
|
227
|
+
const rounded = perPoint ? radii.some((r) => r > 0) : radii > 0;
|
|
228
|
+
if (rounded && p.smooth)
|
|
229
|
+
warn('smooth is ignored: corner_radius rounds the corners instead');
|
|
230
|
+
const final = p.smooth && pts.length > 2 && !rounded ? catmullRom(pts, p.closed, p.subdivisions).pos : pts;
|
|
231
|
+
const g = new Geometry({ positions: final.flat(), prims: [final.map((_, i) => i)], closed: p.closed });
|
|
232
|
+
if (!rounded)
|
|
233
|
+
return g;
|
|
234
|
+
return filletCurves(g, {
|
|
235
|
+
radius: 1, type: 'round', segments: p.corner_segments, angleMin: 0.01, sel: null, clamp: true,
|
|
236
|
+
radiusOf: (pt) => (perPoint ? radii[pt] : radii),
|
|
237
|
+
});
|
|
209
238
|
},
|
|
210
239
|
});
|
|
211
240
|
defineNode({
|
|
@@ -238,17 +267,17 @@ defineNode({
|
|
|
238
267
|
defineNode({
|
|
239
268
|
type: 'shape2d',
|
|
240
269
|
category: 'curve',
|
|
241
|
-
summary: 'Flat 2D shapes as closed polygons: rect, rounded_rect, ngon, star, gear, cross, L, U.',
|
|
270
|
+
summary: 'Flat 2D shapes as closed polygons: rect, rounded_rect, capsule (slot), ngon, star, gear, cross, L, U.',
|
|
242
271
|
aliases: ['rect', 'rectangle', 'ngon', 'star', 'polygon2d'],
|
|
243
272
|
inputs: [],
|
|
244
273
|
params: {
|
|
245
|
-
shape: { type: 'enum', options: ['rect', 'rounded_rect', 'ngon', 'star', 'gear', 'cross', 'L', 'U'], default: 'rect', desc: 'shape type' },
|
|
246
|
-
size: { type: 'vec2', default: [1, 1], desc: 'rect/rounded_rect/cross/L/U: width and depth' },
|
|
274
|
+
shape: { type: 'enum', options: ['rect', 'rounded_rect', 'capsule', 'slot', 'ngon', 'star', 'gear', 'cross', 'L', 'U'], default: 'rect', desc: 'shape type; capsule and slot are the same stadium: a rect whose short ends are half circles' },
|
|
275
|
+
size: { type: 'vec2', default: [1, 1], desc: 'rect/rounded_rect/capsule/slot/cross/L/U: width and depth (overall, ends included)' },
|
|
247
276
|
radius: { type: 'float', default: 0.5, desc: 'ngon/star/gear: outer radius' },
|
|
248
277
|
inner_radius: { type: 'float', default: 0.25, desc: 'star/gear: inner radius' },
|
|
249
278
|
sides: { type: 'int', default: 6, min: 3, max: 100000, desc: 'ngon sides / star points / gear teeth', aliases: ['points', 'teeth'] },
|
|
250
279
|
corner_radius: { type: 'float', default: 0.1, min: 0, desc: 'rounded_rect corner radius' },
|
|
251
|
-
corner_segments: { type: 'int', default: 6, min: 1, max: 10000, desc: 'rounded_rect segments per
|
|
280
|
+
corner_segments: { type: 'int', default: 6, min: 1, max: 10000, desc: 'rounded_rect/capsule/slot: segments per quarter circle' },
|
|
252
281
|
thickness: { type: 'float', default: 0.25, desc: 'cross/L/U arm thickness' },
|
|
253
282
|
rotate: { type: 'float', default: 0, desc: 'rotation in degrees within the plane' },
|
|
254
283
|
plane: P.plane('xz'),
|
|
@@ -263,14 +292,23 @@ defineNode({
|
|
|
263
292
|
case 'rect':
|
|
264
293
|
pts2 = [[-hw, -hd], [hw, -hd], [hw, hd], [-hw, hd]];
|
|
265
294
|
break;
|
|
266
|
-
case 'rounded_rect':
|
|
267
|
-
|
|
295
|
+
case 'rounded_rect':
|
|
296
|
+
case 'capsule':
|
|
297
|
+
case 'slot': {
|
|
298
|
+
const r = p.shape === 'rounded_rect' ? Math.min(p.corner_radius, hw, hd) : Math.min(hw, hd), s = p.corner_segments;
|
|
268
299
|
const cs = [[hw - r, -hd + r, -90], [hw - r, hd - r, 0], [-hw + r, hd - r, 90], [-hw + r, -hd + r, 180]];
|
|
269
300
|
for (const [cx, cy, a0] of cs)
|
|
270
301
|
for (let k = 0; k <= s; k++) {
|
|
271
302
|
const a = ((a0 + (90 * k) / s) * Math.PI) / 180;
|
|
272
303
|
pts2.push([cx + r * Math.cos(a), cy + r * Math.sin(a)]);
|
|
273
304
|
}
|
|
305
|
+
// a radius of half the width or depth makes neighbouring arcs meet: keep their shared end point once
|
|
306
|
+
const eps = 1e-9 * Math.max(hw, hd);
|
|
307
|
+
for (let i = pts2.length - 1; i >= 0 && pts2.length > 3; i--) {
|
|
308
|
+
const q = pts2[(i + 1) % pts2.length];
|
|
309
|
+
if (Math.abs(pts2[i][0] - q[0]) <= eps && Math.abs(pts2[i][1] - q[1]) <= eps)
|
|
310
|
+
pts2.splice(i, 1);
|
|
311
|
+
}
|
|
274
312
|
break;
|
|
275
313
|
}
|
|
276
314
|
case 'ngon':
|
|
@@ -541,7 +579,7 @@ function sweepCurves(g, profiles, o) {
|
|
|
541
579
|
const L = arcLengths(pts, closed), total = L[L.length - 1] || 1;
|
|
542
580
|
const prof = profiles[Math.min(pr, profiles.length - 1)];
|
|
543
581
|
const m = prof.pts.length;
|
|
544
|
-
const rings = [];
|
|
582
|
+
const rings = [], scales = [];
|
|
545
583
|
for (let k = 0; k < pts.length; k++) {
|
|
546
584
|
const u = L[k] / total;
|
|
547
585
|
let s = psA ? psA.get(idx[k]) : 1;
|
|
@@ -549,6 +587,7 @@ function sweepCurves(g, profiles, o) {
|
|
|
549
587
|
s *= o.radiusOf(idx[k]);
|
|
550
588
|
if (o.scaleRamp)
|
|
551
589
|
s *= evalRamp(o.scaleRamp, u);
|
|
590
|
+
scales.push(s);
|
|
552
591
|
const tw = ((o.twist * u) * Math.PI) / 180, c = Math.cos(tw), sn = Math.sin(tw);
|
|
553
592
|
const X = F.X[k], Y = F.Y[k], T = F.T[k];
|
|
554
593
|
const ring = [];
|
|
@@ -572,8 +611,26 @@ function sweepCurves(g, profiles, o) {
|
|
|
572
611
|
}
|
|
573
612
|
}
|
|
574
613
|
if (o.caps && !closed && prof.closed && m >= 3) {
|
|
575
|
-
|
|
576
|
-
|
|
614
|
+
// caps take the profile's own flat layout: its x, y spread over 0..1 (uv_length: in metres at that end's scale)
|
|
615
|
+
let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;
|
|
616
|
+
for (const q of prof.pts) {
|
|
617
|
+
x0 = Math.min(x0, q[0]);
|
|
618
|
+
x1 = Math.max(x1, q[0]);
|
|
619
|
+
y0 = Math.min(y0, q[1]);
|
|
620
|
+
y1 = Math.max(y1, q[1]);
|
|
621
|
+
}
|
|
622
|
+
const span = Math.max(x1 - x0, y1 - y0) || 1;
|
|
623
|
+
for (const end of [0, rings.length - 1]) {
|
|
624
|
+
const ids = end === 0 ? rings[0].slice().reverse() : rings[end].slice();
|
|
625
|
+
const order = end === 0 ? prof.pts.map((_, j) => m - 1 - j) : prof.pts.map((_, j) => j);
|
|
626
|
+
const sc = o.uvLength ? scales[end] : 1 / span;
|
|
627
|
+
const vb = b.numVertices;
|
|
628
|
+
capG.add(b.addPrim(ids, true, pr));
|
|
629
|
+
order.forEach((j, q) => {
|
|
630
|
+
const u = (prof.pts[j][0] - x0) * sc, v = (prof.pts[j][1] - y0) * sc;
|
|
631
|
+
uv.set(vb + q, [end === 0 ? 1 - u : u, v, 0]);
|
|
632
|
+
});
|
|
633
|
+
}
|
|
577
634
|
}
|
|
578
635
|
}
|
|
579
636
|
return b.build();
|
|
@@ -585,14 +642,16 @@ defineNode({
|
|
|
585
642
|
type: 'sweep',
|
|
586
643
|
category: 'curve',
|
|
587
644
|
summary: 'Sweep a cross-section along curves using rotation-minimising frames (tubes, ribbons, rails, mouldings).',
|
|
645
|
+
description: 'The profile is drawn in the XY plane around the origin, looking along the curve: at every curve point it is placed with its +Z along the curve (the direction of travel), its +Y on the up direction (the "up" param, or the point attribute up) kept perpendicular to the curve, and its +X = Y × Z, to the left of the direction of travel when up points up. The frame then turns as little as possible from point to point (rotation-minimising), so a flat ribbon does not twist on its own; twist adds a turn. The sides are wound so a closed profile drawn counter-clockwise (seen from +Z) faces outward; caps on open curves face away from the curve. uv: u runs around the profile, v along the curve; caps take the profile\'s flat layout.',
|
|
588
646
|
inputs: [
|
|
589
647
|
{ name: 'backbone', desc: 'curves to sweep along (Bezier/NURBS curves are sampled adaptively)' },
|
|
590
648
|
{ name: 'profile', desc: 'cross-section curve drawn in the XY plane (optional; one per backbone or a single shared one)', optional: true },
|
|
591
649
|
],
|
|
592
650
|
splines: 'keep',
|
|
593
651
|
params: {
|
|
594
|
-
shape: { type: 'enum', options: ['circle', 'square', 'ribbon', 'half_circle'], default: 'circle', desc: 'built-in profile when no profile input' },
|
|
652
|
+
shape: { type: 'enum', options: ['circle', 'square', 'rect', 'ribbon', 'half_circle'], default: 'circle', desc: 'built-in profile when no profile input; rect is size wide (X) and high (Y), for straps and bars' },
|
|
595
653
|
radius: { type: 'float', default: 0.1, desc: 'built-in profile radius / half width', aliases: ['width', 'rad'] },
|
|
654
|
+
size: { type: 'vec2', default: [0.2, 0.05], desc: 'rect profile: width along the profile X (sideways) and height along Y (up)' },
|
|
596
655
|
divisions: { type: 'int', default: 12, min: 2, max: 1024, desc: 'built-in profile segments', aliases: ['sides', 'cols'] },
|
|
597
656
|
caps: { type: 'bool', default: true, desc: 'cap open ends (closed profiles only)' },
|
|
598
657
|
twist: { type: 'float', default: 0, desc: 'total twist in degrees along each curve' },
|
|
@@ -621,6 +680,10 @@ defineNode({
|
|
|
621
680
|
}
|
|
622
681
|
else if (p.shape === 'square')
|
|
623
682
|
profiles = [{ pts: [[-p.radius, -p.radius, 0], [p.radius, -p.radius, 0], [p.radius, p.radius, 0], [-p.radius, p.radius, 0]], closed: true }];
|
|
683
|
+
else if (p.shape === 'rect') {
|
|
684
|
+
const w = p.size[0] / 2, h = p.size[1] / 2;
|
|
685
|
+
profiles = [{ pts: [[-w, -h, 0], [w, -h, 0], [w, h, 0], [-w, h, 0]], closed: true }];
|
|
686
|
+
}
|
|
624
687
|
else if (p.shape === 'ribbon')
|
|
625
688
|
profiles = [{ pts: [[-p.radius, 0, 0], [p.radius, 0, 0]], closed: false }];
|
|
626
689
|
else if (p.shape === 'half_circle')
|
|
@@ -665,10 +728,13 @@ defineNode({
|
|
|
665
728
|
defineNode({
|
|
666
729
|
type: 'revolve',
|
|
667
730
|
category: 'curve',
|
|
668
|
-
summary: 'Lathe: revolve profile curves
|
|
731
|
+
summary: 'Lathe: revolve profile curves around an axis (Y through the origin by default). Profiles touching the axis close up.',
|
|
732
|
+
description: 'Each profile point turns around the axis line (through origin, along axis) at its own distance from it, so the profile can be drawn in any plane that holds the axis: the XY plane (x = radius) for the default Y axis, the XY plane (y = radius) for axis [1, 0, 0]. angle is measured from where the profile is drawn, turning counter-clockwise when the axis points at the viewer. Closed results face outward.',
|
|
669
733
|
aliases: ['lathe'],
|
|
670
|
-
inputs: [{ name: 'profile', desc: 'curves in the XY plane
|
|
734
|
+
inputs: [{ name: 'profile', desc: 'curves in a plane holding the axis (default: the XY plane, x = distance from the Y axis)' }],
|
|
671
735
|
params: {
|
|
736
|
+
axis: { type: 'vec3', default: [0, 1, 0], desc: 'direction of the axis to turn around', aliases: ['direction', 'dir'] },
|
|
737
|
+
origin: { type: 'vec3', default: [0, 0, 0], desc: 'a point on the axis', aliases: ['center', 'pivot'] },
|
|
672
738
|
segments: { type: 'int', default: 32, min: 3, max: 4096, desc: 'segments around', aliases: ['divisions', 'cols'] },
|
|
673
739
|
angle: { type: 'vec2', default: [0, 360], desc: 'start/end angle in degrees' },
|
|
674
740
|
caps: { type: 'bool', default: false, desc: 'cap partial revolves (closed profiles)' },
|
|
@@ -686,13 +752,23 @@ defineNode({
|
|
|
686
752
|
const g = input(0);
|
|
687
753
|
const span = p.angle[1] - p.angle[0], full = Math.abs(span) >= 360 - 1e-9;
|
|
688
754
|
const n = p.segments, cols = full ? n : n + 1;
|
|
755
|
+
const al = Math.hypot(p.axis[0], p.axis[1], p.axis[2]);
|
|
756
|
+
if (!(al > 0))
|
|
757
|
+
throw new NodeError('axis must not be zero', 'e.g. [0, 1, 0] for the Y axis');
|
|
758
|
+
// a frame with the axis as its Y: points go into it, turn around Y, and come back
|
|
759
|
+
const R = toYAxis([p.axis[0] / al, p.axis[1] / al, p.axis[2] / al]), o = p.origin;
|
|
760
|
+
const local = (i) => {
|
|
761
|
+
const x = g.P[i * 3] - o[0], y = g.P[i * 3 + 1] - o[1], z = g.P[i * 3 + 2] - o[2];
|
|
762
|
+
return [R[0] * x + R[1] * y + R[2] * z, R[3] * x + R[4] * y + R[5] * z, R[6] * x + R[7] * y + R[8] * z];
|
|
763
|
+
};
|
|
764
|
+
const world = (x, y, z) => [o[0] + R[0] * x + R[3] * y + R[6] * z, o[1] + R[1] * x + R[4] * y + R[7] * z, o[2] + R[2] * x + R[5] * y + R[8] * z];
|
|
689
765
|
const b = new GeoBuilder(g);
|
|
690
766
|
const uv = b.attrib('vertex', 'uv', 3);
|
|
691
767
|
for (let pr = 0; pr < g.numPrims; pr++) {
|
|
692
768
|
const idx = g.primPoints(pr), closed = !!g.primClosed[pr];
|
|
693
769
|
const grid = [];
|
|
694
770
|
for (const i of idx) {
|
|
695
|
-
const x
|
|
771
|
+
const [x, y, z] = local(i);
|
|
696
772
|
const r = Math.hypot(x, z);
|
|
697
773
|
if (r < 1e-9) {
|
|
698
774
|
const c = b.copyPoint(i);
|
|
@@ -703,7 +779,8 @@ defineNode({
|
|
|
703
779
|
const row = [];
|
|
704
780
|
for (let j = 0; j < cols; j++) {
|
|
705
781
|
const a = a0 + ((p.angle[0] + (span * j) / n) * Math.PI) / 180;
|
|
706
|
-
|
|
782
|
+
const w = world(r * Math.cos(a), y, -r * Math.sin(a));
|
|
783
|
+
row.push(b.addPoint(w[0], w[1], w[2], i));
|
|
707
784
|
}
|
|
708
785
|
grid.push(row);
|
|
709
786
|
}
|
|
@@ -738,6 +815,19 @@ defineNode({
|
|
|
738
815
|
return out;
|
|
739
816
|
},
|
|
740
817
|
});
|
|
818
|
+
/** Row-major rotation taking unit vector a onto +Y (Rodrigues; a half turn about X when a points down). */
|
|
819
|
+
function toYAxis(a) {
|
|
820
|
+
const c = a[1];
|
|
821
|
+
if (c < -1 + 1e-12)
|
|
822
|
+
return [1, 0, 0, 0, -1, 0, 0, 0, -1];
|
|
823
|
+
// v = a × Y, R = I + [v]x + [v]x² / (1 + c)
|
|
824
|
+
const vx = -a[2], vy = 0, vz = a[0], k = 1 / (1 + c);
|
|
825
|
+
return [
|
|
826
|
+
1 - (vy * vy + vz * vz) * k, -vz + vx * vy * k, vy + vx * vz * k,
|
|
827
|
+
vz + vx * vy * k, 1 - (vx * vx + vz * vz) * k, -vx + vy * vz * k,
|
|
828
|
+
-vy + vx * vz * k, vx + vy * vz * k, 1 - (vx * vx + vy * vy) * k,
|
|
829
|
+
];
|
|
830
|
+
}
|
|
741
831
|
function pointInPoly(pt, poly) {
|
|
742
832
|
let inside = false;
|
|
743
833
|
for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
|
|
@@ -783,18 +873,12 @@ defineNode({
|
|
|
783
873
|
if (!g.numPrims)
|
|
784
874
|
throw new NodeError('extrude needs closed shapes (polygons) as input');
|
|
785
875
|
const plane = p.plane === 'auto' ? detectPlane(g) : p.plane;
|
|
786
|
-
const { polys, offset } = toPolygons(g, plane);
|
|
787
|
-
|
|
788
|
-
let nested = false;
|
|
789
|
-
for (let i = 0; i < polys.length && !nested; i++)
|
|
790
|
-
for (let j = 0; j < polys.length; j++)
|
|
791
|
-
if (i !== j && pointInPoly(polys[i][0], polys[j])) {
|
|
792
|
-
nested = true;
|
|
793
|
-
break;
|
|
794
|
-
}
|
|
876
|
+
const { polys: raw, offset } = toPolygons(g, plane);
|
|
877
|
+
const F = FRAMES[plane];
|
|
795
878
|
const pyramid = p.scale_top[0] === 0 && p.scale_top[1] === 0;
|
|
796
|
-
if (
|
|
797
|
-
|
|
879
|
+
if (pyramid) {
|
|
880
|
+
// tapering to a point: manifold's extrude makes the apex; groups by face direction
|
|
881
|
+
const cs = crossSection(raw, 'EvenOdd');
|
|
798
882
|
let m = cs.extrude(Math.abs(p.height), p.divisions - 1, p.twist, p.scale_top, p.center);
|
|
799
883
|
cs.delete();
|
|
800
884
|
if (p.height < 0) {
|
|
@@ -804,35 +888,107 @@ defineNode({
|
|
|
804
888
|
}
|
|
805
889
|
const mt = m.transform(planeMatrix(plane, offset));
|
|
806
890
|
m.delete();
|
|
807
|
-
const
|
|
891
|
+
const tri = fromManifold(mt, propLayout([])).geo;
|
|
808
892
|
mt.delete();
|
|
893
|
+
const nrm = primNormals(tri), down = Math.sign(p.height || 1);
|
|
894
|
+
const bottom = Uint8Array.from({ length: tri.numPrims }, (_, t) => ((nrm[t * 3] * F.N[0] + nrm[t * 3 + 1] * F.N[1] + nrm[t * 3 + 2] * F.N[2]) * down < -0.9999 ? 1 : 0));
|
|
895
|
+
const out = mergeTriangles(tri, Float64Array.from(bottom));
|
|
896
|
+
const bm = new Uint8Array(out.numPrims), on = primNormals(out);
|
|
897
|
+
for (let t = 0; t < out.numPrims; t++)
|
|
898
|
+
bm[t] = (on[t * 3] * F.N[0] + on[t * 3 + 1] * F.N[1] + on[t * 3 + 2] * F.N[2]) * down < -0.9999 ? 1 : 0;
|
|
899
|
+
out.addGroup('prim', 'bottom', bm);
|
|
900
|
+
out.addGroup('prim', 'top', new Uint8Array(out.numPrims));
|
|
901
|
+
out.addGroup('prim', 'side', bm.map((x) => 1 - x));
|
|
809
902
|
return out;
|
|
810
903
|
}
|
|
811
|
-
|
|
904
|
+
// shapes inside shapes are holes (and shapes inside holes islands): outlines counter-clockwise, holes clockwise
|
|
905
|
+
const { polys, depth, parent } = orientNested(raw);
|
|
906
|
+
const root = polys.map((_, i) => { let r = i; while (parent[r] >= 0)
|
|
907
|
+
r = parent[r]; return r; });
|
|
908
|
+
const centre = polys.map((poly) => [poly.reduce((s, q) => s + q[0], 0) / poly.length, poly.reduce((s, q) => s + q[1], 0) / poly.length]);
|
|
812
909
|
const b = new GeoBuilder();
|
|
813
910
|
const top = b.group('prim', 'top'), bottom = b.group('prim', 'bottom'), side = b.group('prim', 'side');
|
|
814
911
|
const h0 = p.center ? -p.height / 2 : 0;
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
const cx
|
|
818
|
-
const
|
|
912
|
+
const rings = polys.map((poly, i) => {
|
|
913
|
+
// twist and taper about the outermost outline's centre, so holes stay where they are in their shape
|
|
914
|
+
const [cx, cy] = centre[root[i]];
|
|
915
|
+
const out = [];
|
|
819
916
|
for (let k = 0; k <= p.divisions; k++) {
|
|
820
917
|
const t = k / p.divisions, w = offset + h0 + p.height * t;
|
|
821
|
-
const a = ((p.twist * t) * Math.PI) / 180, c = Math.cos(a),
|
|
918
|
+
const a = ((p.twist * t) * Math.PI) / 180, c = Math.cos(a), sn = Math.sin(a);
|
|
822
919
|
const sx = 1 + (p.scale_top[0] - 1) * t, sy = 1 + (p.scale_top[1] - 1) * t;
|
|
823
|
-
|
|
920
|
+
out.push(poly.map(([u, v]) => {
|
|
824
921
|
const du = (u - cx) * sx, dv = (v - cy) * sy;
|
|
825
|
-
const uu = cx + du * c - dv *
|
|
922
|
+
const uu = cx + du * c - dv * sn, vv = cy + du * sn + dv * c;
|
|
826
923
|
return b.addPoint(F.U[0] * uu + F.V[0] * vv + F.N[0] * w, F.U[1] * uu + F.V[1] * vv + F.N[1] * w, F.U[2] * uu + F.V[2] * vv + F.N[2] * w);
|
|
827
924
|
}));
|
|
828
925
|
}
|
|
829
|
-
|
|
926
|
+
return out;
|
|
927
|
+
});
|
|
928
|
+
polys.forEach((poly, i) => {
|
|
929
|
+
const m = poly.length, r = rings[i];
|
|
830
930
|
for (let k = 0; k < p.divisions; k++)
|
|
831
931
|
for (let j = 0; j < m; j++)
|
|
832
|
-
side.add(b.addPrim([
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
932
|
+
side.add(b.addPrim([r[k][j], r[k][(j + 1) % m], r[k + 1][(j + 1) % m], r[k + 1][j]]));
|
|
933
|
+
});
|
|
934
|
+
if (p.caps) {
|
|
935
|
+
// each outline with its own holes is one cap, triangulated on the ring points (so it stays watertight) and
|
|
936
|
+
// merged back into as few polygons as a cap with holes needs
|
|
937
|
+
const capTris = [], capOf = [];
|
|
938
|
+
polys.forEach((poly, i) => {
|
|
939
|
+
if (depth[i] % 2)
|
|
940
|
+
return;
|
|
941
|
+
const holes = polys.map((_, j) => j).filter((j) => parent[j] === i);
|
|
942
|
+
if (!holes.length) {
|
|
943
|
+
bottom.add(b.addPrim(rings[i][0].slice().reverse()));
|
|
944
|
+
top.add(b.addPrim(rings[i][p.divisions].slice()));
|
|
945
|
+
return;
|
|
946
|
+
}
|
|
947
|
+
const flat = [], holeAt = [], ids = [];
|
|
948
|
+
for (const j of [i, ...holes]) {
|
|
949
|
+
if (j !== i)
|
|
950
|
+
holeAt.push(flat.length / 2);
|
|
951
|
+
polys[j].forEach((q, k) => { flat.push(q[0], q[1]); ids.push(k); });
|
|
952
|
+
}
|
|
953
|
+
const owner = [i, ...holes].flatMap((j) => polys[j].map(() => j));
|
|
954
|
+
const tri = earcut(flat, holeAt.length ? holeAt : undefined);
|
|
955
|
+
for (let t = 0; t < tri.length; t += 3) {
|
|
956
|
+
capTris.push([tri[t], tri[t + 1], tri[t + 2]].map((x) => owner[x] * 1e9 + ids[x]));
|
|
957
|
+
capOf.push(i);
|
|
958
|
+
}
|
|
959
|
+
});
|
|
960
|
+
const capGeo = (level, flip) => {
|
|
961
|
+
const cb = new GeoBuilder();
|
|
962
|
+
const map = new Map();
|
|
963
|
+
const pt = (code) => {
|
|
964
|
+
let id = map.get(code);
|
|
965
|
+
if (id === undefined) {
|
|
966
|
+
const j = Math.floor(code / 1e9), k = code - j * 1e9, src = rings[j][level][k];
|
|
967
|
+
id = cb.addPoint(b.pos[src * 3], b.pos[src * 3 + 1], b.pos[src * 3 + 2]);
|
|
968
|
+
map.set(code, id);
|
|
969
|
+
}
|
|
970
|
+
return id;
|
|
971
|
+
};
|
|
972
|
+
// earcut's triangles run one way round in plane coordinates: counter-clockwise faces +N
|
|
973
|
+
for (const t of capTris) {
|
|
974
|
+
const [x, y, z] = t.map(pt);
|
|
975
|
+
const ccw = signed2(t.map((c) => { const j = Math.floor(c / 1e9); return polys[j][c - j * 1e9]; })) > 0;
|
|
976
|
+
cb.addPrim(ccw !== flip ? [x, y, z] : [x, z, y]);
|
|
977
|
+
}
|
|
978
|
+
const tg = cb.build();
|
|
979
|
+
const merged = mergeTriangles(tg, Float64Array.from(capOf));
|
|
980
|
+
// back to the ring points
|
|
981
|
+
const back = new Map();
|
|
982
|
+
for (const [code, id] of map) {
|
|
983
|
+
const j = Math.floor(code / 1e9);
|
|
984
|
+
back.set(id, rings[j][level][code - j * 1e9]);
|
|
985
|
+
}
|
|
986
|
+
return { merged, back };
|
|
987
|
+
};
|
|
988
|
+
for (const [level, grp, flip] of [[0, bottom, true], [p.divisions, top, false]]) {
|
|
989
|
+
const { merged, back } = capGeo(level, flip);
|
|
990
|
+
for (let pr = 0; pr < merged.numPrims; pr++)
|
|
991
|
+
grp.add(b.addPrim(merged.primPoints(pr).map((i) => back.get(i))));
|
|
836
992
|
}
|
|
837
993
|
}
|
|
838
994
|
let out = b.build();
|
|
@@ -841,17 +997,23 @@ defineNode({
|
|
|
841
997
|
return out;
|
|
842
998
|
},
|
|
843
999
|
});
|
|
1000
|
+
/** Twice the signed area of a 2D triangle (> 0 counter-clockwise). */
|
|
1001
|
+
function signed2(t) {
|
|
1002
|
+
return (t[1][0] - t[0][0]) * (t[2][1] - t[0][1]) - (t[2][0] - t[0][0]) * (t[1][1] - t[0][1]);
|
|
1003
|
+
}
|
|
844
1004
|
const NO_SHAPES = 'no closed shapes in the input (polygons, or polylines whose ends meet, with 3+ points): output is empty';
|
|
845
1005
|
defineNode({
|
|
846
1006
|
type: 'offset_curve',
|
|
847
1007
|
category: 'curve',
|
|
848
|
-
summary: '2D offset (grow/shrink) of closed shapes, like PolyExpand2D. Negative distance = inset.',
|
|
849
|
-
|
|
850
|
-
|
|
1008
|
+
summary: '2D offset (grow/shrink) of closed shapes, like PolyExpand2D. Negative distance = inset. Open paths become straps.',
|
|
1009
|
+
description: 'Closed shapes grow by distance (shrink when negative). An open path (a polyline whose ends do not meet) becomes a strap: a closed outline at |distance| on both sides, with its ends cut square at the end points (cap butt), rounded, or extended by the distance; extrude it for a 3D strap or band. Shapes and straps in one input are joined.',
|
|
1010
|
+
aliases: ['polyexpand2d', 'offset2d', 'inset2d', 'strap', 'stroke'],
|
|
1011
|
+
inputs: [{ name: 'shapes', desc: 'closed polygons in a plane, or open paths for straps' }],
|
|
851
1012
|
params: {
|
|
852
|
-
distance: { type: 'float', default: 0.1, desc: 'offset distance (negative shrinks)', aliases: ['offset', 'dist'] },
|
|
1013
|
+
distance: { type: 'float', default: 0.1, desc: 'offset distance (negative shrinks); straps: half their width', aliases: ['offset', 'dist'] },
|
|
853
1014
|
join: { type: 'enum', options: ['round', 'miter', 'square'], default: 'round', desc: 'corner style' },
|
|
854
|
-
|
|
1015
|
+
cap: { type: 'enum', options: ['butt', 'round', 'square'], default: 'butt', desc: 'strap ends: butt = cut square at the end point, round = half circle, square = extended by the distance' },
|
|
1016
|
+
segments: { type: 'int', default: 16, min: 3, max: 10000, desc: 'segments per full circle for round joins and caps' },
|
|
855
1017
|
output: { type: 'enum', options: ['outlines', 'faces'], default: 'outlines', desc: 'outlines = closed polygons per contour; faces = filled with holes cut' },
|
|
856
1018
|
plane: { type: 'enum', options: ['auto', 'xz', 'xy', 'yz'], default: 'auto', desc: 'shape plane' },
|
|
857
1019
|
},
|
|
@@ -865,27 +1027,126 @@ defineNode({
|
|
|
865
1027
|
cook: ({ input, p, warn }) => {
|
|
866
1028
|
const g = input(0);
|
|
867
1029
|
const plane = p.plane === 'auto' ? detectPlane(g) : p.plane;
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
1030
|
+
// open paths (ends apart) are straps; the rest are shapes
|
|
1031
|
+
const isOpen = (pr) => {
|
|
1032
|
+
if (g.primClosed[pr])
|
|
1033
|
+
return false;
|
|
1034
|
+
const ids = g.primPoints(pr);
|
|
1035
|
+
if (ids.length < 2)
|
|
1036
|
+
return false;
|
|
1037
|
+
const a = ids[0] * 3, b = ids[ids.length - 1] * 3;
|
|
1038
|
+
return Math.hypot(g.P[a] - g.P[b], g.P[a + 1] - g.P[b + 1], g.P[a + 2] - g.P[b + 2]) > 1e-9;
|
|
1039
|
+
};
|
|
1040
|
+
const open = Uint8Array.from({ length: g.numPrims }, (_, pr) => (isOpen(pr) ? 1 : 0));
|
|
1041
|
+
const anyOpen = open.some((x) => x);
|
|
1042
|
+
const shapes = anyOpen ? g.subsetPrims(open.map((x) => 1 - x), false) : g;
|
|
1043
|
+
const { polys, offset } = toPolygons(shapes, plane);
|
|
872
1044
|
const jt = p.join === 'round' ? 'Round' : p.join === 'miter' ? 'Miter' : 'Square';
|
|
873
|
-
const
|
|
1045
|
+
const cs = crossSection(polys, 'EvenOdd');
|
|
1046
|
+
let o = cs.offset(p.distance, jt, 2, p.segments);
|
|
874
1047
|
cs.delete();
|
|
875
|
-
|
|
1048
|
+
let nStraps = 0, planeOffset = offset;
|
|
1049
|
+
if (anyOpen) {
|
|
1050
|
+
const lines = g.subsetPrims(open, false);
|
|
1051
|
+
const w = Math.abs(p.distance);
|
|
1052
|
+
if (!polys.length)
|
|
1053
|
+
planeOffset = toPolygons(lines, plane).offset;
|
|
1054
|
+
if (w > 0)
|
|
1055
|
+
for (const path of toPaths(lines, plane)) {
|
|
1056
|
+
const st = strap(path, w, p.join, p.cap, p.segments);
|
|
1057
|
+
const u = o.add(st);
|
|
1058
|
+
o.delete();
|
|
1059
|
+
st.delete();
|
|
1060
|
+
o = u;
|
|
1061
|
+
nStraps++;
|
|
1062
|
+
}
|
|
1063
|
+
else
|
|
1064
|
+
warn('distance is 0: open paths give no strap');
|
|
1065
|
+
}
|
|
1066
|
+
if (!polys.length && !nStraps && g.numPoints)
|
|
1067
|
+
warn(NO_SHAPES);
|
|
1068
|
+
const out = p.output === 'faces' ? crossSectionFaces(o, plane, planeOffset) : polygonsToGeo(o.toPolygons(), plane, planeOffset);
|
|
876
1069
|
o.delete();
|
|
877
1070
|
return out;
|
|
878
1071
|
},
|
|
879
1072
|
});
|
|
1073
|
+
/** Open polylines as 2D point lists in the plane frame. */
|
|
1074
|
+
function toPaths(g, plane) {
|
|
1075
|
+
const F = FRAMES[plane], P = g.P, out = [];
|
|
1076
|
+
for (let pr = 0; pr < g.numPrims; pr++) {
|
|
1077
|
+
const path = [];
|
|
1078
|
+
for (const i of g.primPoints(pr)) {
|
|
1079
|
+
const x = P[i * 3], y = P[i * 3 + 1], z = P[i * 3 + 2];
|
|
1080
|
+
const q = [x * F.U[0] + y * F.U[1] + z * F.U[2], x * F.V[0] + y * F.V[1] + z * F.V[2]];
|
|
1081
|
+
const last = path[path.length - 1];
|
|
1082
|
+
if (!last || Math.hypot(q[0] - last[0], q[1] - last[1]) > 1e-12)
|
|
1083
|
+
path.push(q);
|
|
1084
|
+
}
|
|
1085
|
+
if (path.length >= 2)
|
|
1086
|
+
out.push(path);
|
|
1087
|
+
}
|
|
1088
|
+
return out;
|
|
1089
|
+
}
|
|
1090
|
+
/**
|
|
1091
|
+
* The outline of a strap of half-width w along an open 2D path, as a CrossSection: a rectangle per segment joined at
|
|
1092
|
+
* the corners (round: a disc; miter: the wedge to the mitre point, cut back to a bevel when it would reach past 2·w;
|
|
1093
|
+
* square: the wedge cut flat) and capped at the ends.
|
|
1094
|
+
*/
|
|
1095
|
+
function strap(path, w, join, cap, segments) {
|
|
1096
|
+
const n = path.length;
|
|
1097
|
+
const dir = (i) => { const a = path[i], b = path[i + 1], l = Math.hypot(b[0] - a[0], b[1] - a[1]); return [(b[0] - a[0]) / l, (b[1] - a[1]) / l]; };
|
|
1098
|
+
const parts = [];
|
|
1099
|
+
const disc = (c) => Array.from({ length: segments }, (_, k) => { const a = (2 * Math.PI * k) / segments; return [c[0] + w * Math.cos(a), c[1] + w * Math.sin(a)]; });
|
|
1100
|
+
for (let i = 0; i < n - 1; i++) {
|
|
1101
|
+
const [dx, dy] = dir(i), nx = -dy * w, ny = dx * w;
|
|
1102
|
+
let a = path[i], b = path[i + 1];
|
|
1103
|
+
if (cap === 'square' && i === 0)
|
|
1104
|
+
a = [a[0] - dx * w, a[1] - dy * w];
|
|
1105
|
+
if (cap === 'square' && i === n - 2)
|
|
1106
|
+
b = [b[0] + dx * w, b[1] + dy * w];
|
|
1107
|
+
parts.push([[a[0] + nx, a[1] + ny], [a[0] - nx, a[1] - ny], [b[0] - nx, b[1] - ny], [b[0] + nx, b[1] + ny]]);
|
|
1108
|
+
}
|
|
1109
|
+
for (let i = 1; i < n - 1; i++) {
|
|
1110
|
+
const c = path[i], d0 = dir(i - 1), d1 = dir(i);
|
|
1111
|
+
const turn = d0[0] * d1[1] - d0[1] * d1[0];
|
|
1112
|
+
if (Math.abs(turn) < 1e-12 && d0[0] * d1[0] + d0[1] * d1[1] > 0)
|
|
1113
|
+
continue;
|
|
1114
|
+
if (join === 'round') {
|
|
1115
|
+
parts.push(disc(c));
|
|
1116
|
+
continue;
|
|
1117
|
+
}
|
|
1118
|
+
// the outer side of the corner: the gap between the two rectangles
|
|
1119
|
+
const s = turn > 0 ? -1 : 1;
|
|
1120
|
+
const o0 = [c[0] - d0[1] * w * s, c[1] + d0[0] * w * s], o1 = [c[0] - d1[1] * w * s, c[1] + d1[0] * w * s];
|
|
1121
|
+
const bis = [o0[0] + o1[0] - 2 * c[0], o0[1] + o1[1] - 2 * c[1]], bl = Math.hypot(bis[0], bis[1]);
|
|
1122
|
+
const cosHalf = bl / (2 * w);
|
|
1123
|
+
const miter = cosHalf > 1e-9 ? w / cosHalf : Infinity;
|
|
1124
|
+
if (join === 'miter' && miter <= 2 * w)
|
|
1125
|
+
parts.push([c, o0, [c[0] + (bis[0] / bl) * miter, c[1] + (bis[1] / bl) * miter], o1]);
|
|
1126
|
+
else
|
|
1127
|
+
parts.push([c, o0, o1]);
|
|
1128
|
+
}
|
|
1129
|
+
if (cap === 'round') {
|
|
1130
|
+
parts.push(disc(path[0]));
|
|
1131
|
+
parts.push(disc(path[n - 1]));
|
|
1132
|
+
}
|
|
1133
|
+
// every part counter-clockwise, then one union
|
|
1134
|
+
const ccw = parts.map((q) => { let a = 0; for (let i = 0; i < q.length; i++) {
|
|
1135
|
+
const u = q[i], v = q[(i + 1) % q.length];
|
|
1136
|
+
a += u[0] * v[1] - v[0] * u[1];
|
|
1137
|
+
} return a < 0 ? q.slice().reverse() : q; });
|
|
1138
|
+
return crossSection(ccw, 'Positive');
|
|
1139
|
+
}
|
|
880
1140
|
defineNode({
|
|
881
1141
|
type: 'boolean2d',
|
|
882
1142
|
category: 'curve',
|
|
883
1143
|
summary: '2D boolean of closed shapes in a plane (union / subtract / intersect / xor).',
|
|
1144
|
+
description: 'Within each input, a shape lying wholly inside another is a hole in it (a merged plate and circle make a plate with a hole), a shape inside that hole an island, and shapes that only overlap are joined. Faces with holes come out as a few polygons that share edges.',
|
|
884
1145
|
aliases: ['curve_boolean', 'shape_boolean'],
|
|
885
1146
|
inputs: [{ name: 'a', desc: 'shapes A' }, { name: 'b', desc: 'shapes B (optional: union of A with itself)', optional: true }],
|
|
886
1147
|
params: {
|
|
887
1148
|
operation: { type: 'enum', options: ['union', 'subtract', 'intersect', 'xor'], default: 'union', desc: 'boolean op', aliases: ['op', 'mode'] },
|
|
888
|
-
output: { type: 'enum', options: ['outlines', 'faces'], default: 'faces', desc: 'faces = filled
|
|
1149
|
+
output: { type: 'enum', options: ['outlines', 'faces'], default: 'faces', desc: 'faces = filled, holes cut (a face with holes as a few polygons sharing edges); outlines = closed contour polygons' },
|
|
889
1150
|
plane: { type: 'enum', options: ['auto', 'xz', 'xy', 'yz'], default: 'auto', desc: 'shape plane' },
|
|
890
1151
|
},
|
|
891
1152
|
example: {
|
|
@@ -903,12 +1164,13 @@ defineNode({
|
|
|
903
1164
|
if (!ta.polys.length && !tb?.polys.length && (A.numPoints || B?.numPoints))
|
|
904
1165
|
warn(NO_SHAPES);
|
|
905
1166
|
const offset = ta.polys.length || !tb ? ta.offset : tb.offset;
|
|
906
|
-
|
|
1167
|
+
// within each input a shape inside another is a hole, shapes that overlap union (orientNested)
|
|
1168
|
+
const csA = crossSection(orientNested(ta.polys).polys, 'NonZero');
|
|
907
1169
|
let r;
|
|
908
1170
|
if (!tb)
|
|
909
1171
|
r = csA;
|
|
910
1172
|
else {
|
|
911
|
-
const csB = crossSection(tb.polys, 'NonZero');
|
|
1173
|
+
const csB = crossSection(orientNested(tb.polys).polys, 'NonZero');
|
|
912
1174
|
r = p.operation === 'union' ? csA.add(csB) : p.operation === 'subtract' ? csA.subtract(csB) : p.operation === 'intersect' ? csA.intersect(csB) : csA.add(csB).subtract(csA.intersect(csB));
|
|
913
1175
|
csB.delete();
|
|
914
1176
|
}
|
|
@@ -945,14 +1207,17 @@ defineNode({
|
|
|
945
1207
|
defineNode({
|
|
946
1208
|
type: 'loft',
|
|
947
1209
|
category: 'curve',
|
|
948
|
-
summary: 'Skin a surface through a sequence of curves (in prim order)
|
|
1210
|
+
summary: 'Skin a surface through a sequence of curves (in prim order), or through copies of one profile placed by "sections".',
|
|
1211
|
+
description: 'Sections: with several input curves, each is a section in prim order. With one curve and the sections list, each entry places a copy of it: {"offset": [x, y, z], "scale": [sx, sy], "rotate": [rx, ry, rz]} (any key may be left out; a number for offset moves it along the curve\'s normal, a number for scale scales it evenly), applied as scale and rotate about the curve\'s centre, then the offset. Correspondence: when every section has the same number of points, point k of one section joins point k of the next (insets and rounded corners stay where they are); otherwise each section is resampled evenly by length (resample "length"). Closed sections are turned to run the same way round as the first one, and each starts at the point nearest the previous section\'s start, so the skin does not twist. Winding: the side faces follow the first section\'s direction; a closed result (closed sections with caps, or closed: true) is turned to face outward, caps included. uv: u along the section, v across the sections.',
|
|
949
1212
|
aliases: ['skin'],
|
|
950
|
-
inputs: [{ name: 'curves', desc: 'two or more curves, in order' }],
|
|
1213
|
+
inputs: [{ name: 'curves', desc: 'two or more curves, in order; or one profile with sections' }],
|
|
951
1214
|
params: {
|
|
1215
|
+
sections: { type: 'json', default: [], desc: 'one input curve: a list of placements, each {offset, scale, rotate} (see the description)', aliases: ['stations', 'placements'] },
|
|
1216
|
+
resample: { type: 'enum', options: ['auto', 'length', 'none'], default: 'auto', desc: 'auto = keep the points when every section has the same count, else even by length; length = always resample evenly; none = points by index (counts must match)' },
|
|
952
1217
|
closed: { type: 'bool', default: false, desc: 'connect the last curve back to the first' },
|
|
953
1218
|
caps: { type: 'bool', default: false, desc: 'cap the first and last curves (when they are closed)' },
|
|
954
1219
|
},
|
|
955
|
-
outputs: 'vertex uv',
|
|
1220
|
+
outputs: 'vertex uv; prim groups caps, sides',
|
|
956
1221
|
example: {
|
|
957
1222
|
nodes: {
|
|
958
1223
|
a: { type: 'circle', params: { radius: 1, segments: 32 } },
|
|
@@ -964,41 +1229,63 @@ defineNode({
|
|
|
964
1229
|
output: 'skin',
|
|
965
1230
|
},
|
|
966
1231
|
cook: ({ input, p }) => {
|
|
967
|
-
const g = input(0);
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
1232
|
+
const g = evaluateSplines(input(0));
|
|
1233
|
+
let curves = Array.from({ length: g.numPrims }, (_, pr) => ({ pts: curvePoints(g, pr), closed: !!g.primClosed[pr] }))
|
|
1234
|
+
.filter((c) => c.pts.length >= 2);
|
|
1235
|
+
if (Array.isArray(p.sections) && p.sections.length) {
|
|
1236
|
+
if (curves.length !== 1)
|
|
1237
|
+
throw new NodeError(`sections places copies of one profile, but the input has ${curves.length} curves`, 'merge nothing into the profile, or leave sections empty to loft through the input curves');
|
|
1238
|
+
curves = p.sections.map((sec, i) => placeSection(curves[0], sec, i));
|
|
1239
|
+
}
|
|
1240
|
+
if (curves.length < 2)
|
|
1241
|
+
throw new NodeError('loft needs at least 2 curves', 'merge two or more curves in order, or give one profile and a sections list');
|
|
1242
|
+
// every open polyline whose ends meet is a loop
|
|
1243
|
+
for (const c of curves)
|
|
1244
|
+
if (!c.closed && c.pts.length > 3 && dist3(c.pts[0], c.pts[c.pts.length - 1]) < 1e-9) {
|
|
1245
|
+
c.pts.pop();
|
|
1246
|
+
c.closed = true;
|
|
1247
|
+
}
|
|
972
1248
|
const closedU = curves[0].closed;
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
1249
|
+
if (curves.some((c) => c.closed !== closedU))
|
|
1250
|
+
throw new NodeError('loft sections must be all closed or all open', 'close them with ends {mode: close}, or open them');
|
|
1251
|
+
const same = curves.every((c) => c.pts.length === curves[0].pts.length);
|
|
1252
|
+
if (p.resample === 'none' && !same)
|
|
1253
|
+
throw new NodeError(`resample "none" needs every section with the same number of points (${curves.map((c) => c.pts.length).join(', ')})`, 'use resample "auto" or "length"');
|
|
1254
|
+
const n = Math.max(...curves.map((c) => c.pts.length));
|
|
1255
|
+
let rows = p.resample === 'length' || !same
|
|
1256
|
+
? curves.map((c) => evenByLength(c.pts, c.closed, n))
|
|
1257
|
+
: curves.map((c) => c.pts.map((q) => q.slice()));
|
|
1258
|
+
if (closedU) {
|
|
1259
|
+
// same way round as the first section, each starting where the previous one starts
|
|
1260
|
+
const n0 = loopNormal(rows[0]);
|
|
1261
|
+
for (let r = 1; r < rows.length; r++) {
|
|
1262
|
+
if (dot(loopNormal(rows[r]), n0) < 0)
|
|
1263
|
+
rows[r] = [rows[r][0], ...rows[r].slice(1).reverse()];
|
|
1264
|
+
const prev = rows[r - 1][0];
|
|
1265
|
+
let best = 0, bd = Infinity;
|
|
1266
|
+
rows[r].forEach((q, k) => { const d = dist3(q, prev); if (d < bd) {
|
|
1267
|
+
bd = d;
|
|
1268
|
+
best = k;
|
|
1269
|
+
} });
|
|
1270
|
+
if (best)
|
|
1271
|
+
rows[r] = [...rows[r].slice(best), ...rows[r].slice(0, best)];
|
|
985
1272
|
}
|
|
986
|
-
|
|
987
|
-
});
|
|
1273
|
+
}
|
|
988
1274
|
const b = new GeoBuilder();
|
|
989
1275
|
const uv = b.attrib('vertex', 'uv', 3);
|
|
990
|
-
const
|
|
991
|
-
const
|
|
992
|
-
|
|
1276
|
+
const sides = b.group('prim', 'sides'), capsG = b.group('prim', 'caps');
|
|
1277
|
+
const ids = rows.map((row) => row.map((q) => b.addPoint(q[0], q[1], q[2])));
|
|
1278
|
+
const nr = ids.length, m = rows[0].length, nRows = p.closed ? nr : nr - 1, cols = closedU ? m : m - 1;
|
|
1279
|
+
for (let r = 0; r < nRows; r++)
|
|
993
1280
|
for (let c = 0; c < cols; c++) {
|
|
994
|
-
const r1 = (r + 1) % nr, c1 = (c + 1) %
|
|
1281
|
+
const r1 = (r + 1) % nr, c1 = (c + 1) % m;
|
|
995
1282
|
const vb = b.numVertices;
|
|
996
|
-
b.addPrim([ids[r][c], ids[r][c1], ids[r1][c1], ids[r1][c]]);
|
|
997
|
-
[[c / cols, r /
|
|
1283
|
+
sides.add(b.addPrim([ids[r][c], ids[r][c1], ids[r1][c1], ids[r1][c]]));
|
|
1284
|
+
[[c / cols, r / nRows], [(c + 1) / cols, r / nRows], [(c + 1) / cols, (r + 1) / nRows], [c / cols, (r + 1) / nRows]].forEach(([a, d], k) => uv.set(vb + k, [a, d, 0]));
|
|
998
1285
|
}
|
|
999
1286
|
if (p.caps && closedU && !p.closed) {
|
|
1000
|
-
b.addPrim(ids[0].slice().reverse());
|
|
1001
|
-
b.addPrim(ids[nr - 1].slice());
|
|
1287
|
+
capsG.add(b.addPrim(ids[0].slice().reverse()));
|
|
1288
|
+
capsG.add(b.addPrim(ids[nr - 1].slice()));
|
|
1002
1289
|
}
|
|
1003
1290
|
let out = b.build();
|
|
1004
1291
|
if (topologyReport(out).closed)
|
|
@@ -1006,3 +1293,63 @@ defineNode({
|
|
|
1006
1293
|
return out;
|
|
1007
1294
|
},
|
|
1008
1295
|
});
|
|
1296
|
+
function dist3(a, b) { return Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); }
|
|
1297
|
+
/** Newell normal of a closed loop of points. */
|
|
1298
|
+
function loopNormal(pts) {
|
|
1299
|
+
const n = [0, 0, 0];
|
|
1300
|
+
for (let i = 0; i < pts.length; i++) {
|
|
1301
|
+
const a = pts[i], c = pts[(i + 1) % pts.length];
|
|
1302
|
+
n[0] += (a[1] - c[1]) * (a[2] + c[2]);
|
|
1303
|
+
n[1] += (a[2] - c[2]) * (a[0] + c[0]);
|
|
1304
|
+
n[2] += (a[0] - c[0]) * (a[1] + c[1]);
|
|
1305
|
+
}
|
|
1306
|
+
return n;
|
|
1307
|
+
}
|
|
1308
|
+
/** n points spread evenly by length along a curve (a closed one starts and ends at its first point). */
|
|
1309
|
+
function evenByLength(pts0, closed, n) {
|
|
1310
|
+
const pts = closed ? [...pts0, pts0[0]] : pts0;
|
|
1311
|
+
const L = arcLengths(pts, false), T = L[L.length - 1] || 1;
|
|
1312
|
+
const out = [];
|
|
1313
|
+
for (let i = 0; i < n; i++) {
|
|
1314
|
+
const s = (T * i) / (closed ? n : n - 1);
|
|
1315
|
+
let k = 0;
|
|
1316
|
+
while (k < L.length - 2 && L[k + 1] < s)
|
|
1317
|
+
k++;
|
|
1318
|
+
const f = L[k + 1] > L[k] ? (s - L[k]) / (L[k + 1] - L[k]) : 0;
|
|
1319
|
+
out.push(pts[k].map((v, ci) => v + (pts[k + 1][ci] - v) * f));
|
|
1320
|
+
}
|
|
1321
|
+
return out;
|
|
1322
|
+
}
|
|
1323
|
+
/** A copy of the profile placed by one sections entry: scale and rotate about its centre, then offset. */
|
|
1324
|
+
function placeSection(prof, sec, i) {
|
|
1325
|
+
if (!sec || typeof sec !== 'object' || Array.isArray(sec))
|
|
1326
|
+
throw new NodeError(`sections[${i}] must be an object like {"offset": [0, 0.1, 0], "scale": [0.8, 0.8]}`);
|
|
1327
|
+
const vec = (v, k, def) => {
|
|
1328
|
+
if (v === undefined)
|
|
1329
|
+
return def;
|
|
1330
|
+
const a = typeof v === 'number' ? null : v;
|
|
1331
|
+
if (a && !(Array.isArray(a) && a.every((x) => typeof x === 'number' && Number.isFinite(x))))
|
|
1332
|
+
throw new NodeError(`sections[${i}].${k} must be a number or a list of numbers`);
|
|
1333
|
+
return a ?? v;
|
|
1334
|
+
};
|
|
1335
|
+
const c = prof.pts.reduce((s, q) => [s[0] + q[0], s[1] + q[1], s[2] + q[2]], [0, 0, 0]).map((x) => x / prof.pts.length);
|
|
1336
|
+
const N = normalize(loopNormal(prof.pts));
|
|
1337
|
+
const nrm = length(N) > 0 ? N : [0, 1, 0];
|
|
1338
|
+
const off = vec(sec.offset, 'offset', [0, 0, 0]);
|
|
1339
|
+
const offset = typeof off === 'number' ? [nrm[0] * off, nrm[1] * off, nrm[2] * off] : [off[0] ?? 0, off[1] ?? 0, off[2] ?? 0];
|
|
1340
|
+
const sc = vec(sec.scale, 'scale', [1, 1]);
|
|
1341
|
+
// scale in the profile's own plane: along two axes perpendicular to its normal
|
|
1342
|
+
const ax = Math.abs(nrm[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0];
|
|
1343
|
+
const U = normalize(cross(ax, nrm)), V = cross(nrm, U);
|
|
1344
|
+
const sx = typeof sc === 'number' ? sc : sc[0] ?? 1, sy = typeof sc === 'number' ? sc : sc[1] ?? sx;
|
|
1345
|
+
const rot = vec(sec.rotate, 'rotate', [0, 0, 0]);
|
|
1346
|
+
const R = m4euler(typeof rot === 'number' ? [0, rot, 0] : [rot[0] ?? 0, rot[1] ?? 0, rot[2] ?? 0]);
|
|
1347
|
+
const pts = prof.pts.map((q) => {
|
|
1348
|
+
const d = [q[0] - c[0], q[1] - c[1], q[2] - c[2]];
|
|
1349
|
+
const u = dot(d, U) * sx, v = dot(d, V) * sy, w = dot(d, nrm);
|
|
1350
|
+
const e = [U[0] * u + V[0] * v + nrm[0] * w, U[1] * u + V[1] * v + nrm[1] * w, U[2] * u + V[2] * v + nrm[2] * w];
|
|
1351
|
+
const r = m4applyDir(R, e[0], e[1], e[2]);
|
|
1352
|
+
return [c[0] + r[0] + offset[0], c[1] + r[1] + offset[1], c[2] + r[2] + offset[2]];
|
|
1353
|
+
});
|
|
1354
|
+
return { pts, closed: prof.closed };
|
|
1355
|
+
}
|