wallhammer-web 0.0.0-stage → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +92 -2
- package/dist/base/data.d.ts +67 -0
- package/dist/base/data.js +10 -0
- package/dist/base/destructible.d.ts +167 -0
- package/dist/base/destructible.js +47 -0
- package/dist/base/env-slot.d.ts +101 -0
- package/dist/base/env-slot.js +8 -0
- package/dist/base/frames.d.ts +138 -0
- package/dist/base/frames.js +459 -0
- package/dist/base/index.d.ts +21 -0
- package/dist/base/index.js +11 -0
- package/dist/base/mat34.d.ts +46 -0
- package/dist/base/mat34.js +173 -0
- package/dist/base/material.d.ts +87 -0
- package/dist/base/material.js +6 -0
- package/dist/base/math.d.ts +27 -0
- package/dist/base/math.js +60 -0
- package/dist/base/progress.d.ts +38 -0
- package/dist/base/progress.js +81 -0
- package/dist/base/rig.d.ts +177 -0
- package/dist/base/rig.js +97 -0
- package/dist/base/trig.d.ts +29 -0
- package/dist/base/trig.js +169 -0
- package/dist/base/up.d.ts +26 -0
- package/dist/base/up.js +40 -0
- package/dist/base/view.d.ts +128 -0
- package/dist/base/view.js +136 -0
- package/dist/camera/camera.d.ts +99 -0
- package/dist/camera/camera.js +228 -0
- package/dist/camera/follow.d.ts +120 -0
- package/dist/camera/follow.js +320 -0
- package/dist/camera/free.d.ts +153 -0
- package/dist/camera/free.js +359 -0
- package/dist/camera/index.d.ts +10 -0
- package/dist/camera/index.js +6 -0
- package/dist/camera/orbit.d.ts +93 -0
- package/dist/camera/orbit.js +261 -0
- package/dist/camera/path.d.ts +48 -0
- package/dist/camera/path.js +179 -0
- package/dist/core/ballistic.d.ts +66 -0
- package/dist/core/ballistic.js +104 -0
- package/dist/core/bodies.d.ts +77 -0
- package/dist/core/bodies.js +125 -0
- package/dist/core/clock.d.ts +37 -0
- package/dist/core/clock.js +87 -0
- package/dist/core/cover-fade.d.ts +14 -0
- package/dist/core/cover-fade.js +38 -0
- package/dist/core/index.d.ts +13 -0
- package/dist/core/index.js +8 -0
- package/dist/core/kepler.d.ts +74 -0
- package/dist/core/kepler.js +108 -0
- package/dist/core/level-fade.d.ts +66 -0
- package/dist/core/level-fade.js +123 -0
- package/dist/core/palette.d.ts +15 -0
- package/dist/core/palette.js +47 -0
- package/dist/env/atmosphere.d.ts +129 -0
- package/dist/env/atmosphere.js +707 -0
- package/dist/env/cloud-field.d.ts +44 -0
- package/dist/env/cloud-field.js +111 -0
- package/dist/env/environment.d.ts +217 -0
- package/dist/env/environment.js +441 -0
- package/dist/env/frame-block.d.ts +29 -0
- package/dist/env/frame-block.js +152 -0
- package/dist/env/glsl.d.ts +35 -0
- package/dist/env/glsl.js +246 -0
- package/dist/env/index.d.ts +13 -0
- package/dist/env/index.js +9 -0
- package/dist/env/sky.d.ts +48 -0
- package/dist/env/sky.js +227 -0
- package/dist/env/sun.d.ts +31 -0
- package/dist/env/sun.js +42 -0
- package/dist/gen-arch/build.d.ts +17 -0
- package/dist/gen-arch/build.js +414 -0
- package/dist/gen-arch/damage/damaged.d.ts +44 -0
- package/dist/gen-arch/damage/damaged.js +1119 -0
- package/dist/gen-arch/damage/fall-material.d.ts +10 -0
- package/dist/gen-arch/damage/fall-material.js +47 -0
- package/dist/gen-arch/damage/fall.d.ts +78 -0
- package/dist/gen-arch/damage/fall.js +797 -0
- package/dist/gen-arch/damage/rubble.d.ts +22 -0
- package/dist/gen-arch/damage/rubble.js +188 -0
- package/dist/gen-arch/index.d.ts +25 -0
- package/dist/gen-arch/index.js +16 -0
- package/dist/gen-arch/interior/interior-plan.d.ts +114 -0
- package/dist/gen-arch/interior/interior-plan.js +615 -0
- package/dist/gen-arch/interior/interior.d.ts +97 -0
- package/dist/gen-arch/interior/interior.js +966 -0
- package/dist/gen-arch/structure/building-shader.d.ts +25 -0
- package/dist/gen-arch/structure/building-shader.js +946 -0
- package/dist/gen-arch/structure/buildings.d.ts +248 -0
- package/dist/gen-arch/structure/buildings.js +294 -0
- package/dist/gen-arch/structure/dims.d.ts +77 -0
- package/dist/gen-arch/structure/dims.js +39 -0
- package/dist/gen-arch/structure/distant.d.ts +62 -0
- package/dist/gen-arch/structure/distant.js +316 -0
- package/dist/gen-arch/structure/env-slots.d.ts +18 -0
- package/dist/gen-arch/structure/env-slots.js +47 -0
- package/dist/gen-arch/structure/facade.d.ts +193 -0
- package/dist/gen-arch/structure/facade.js +159 -0
- package/dist/gen-arch/structure/parts.d.ts +31 -0
- package/dist/gen-arch/structure/parts.js +69 -0
- package/dist/gen-arch/structure/roof.d.ts +76 -0
- package/dist/gen-arch/structure/roof.js +568 -0
- package/dist/gen-arch/structure/structure-material.d.ts +23 -0
- package/dist/gen-arch/structure/structure-material.js +55 -0
- package/dist/gen-arch/structure/writer.d.ts +94 -0
- package/dist/gen-arch/structure/writer.js +195 -0
- package/dist/gen-core/cell.d.ts +138 -0
- package/dist/gen-core/cell.js +150 -0
- package/dist/gen-core/cube-sphere.d.ts +57 -0
- package/dist/gen-core/cube-sphere.js +159 -0
- package/dist/gen-core/field.d.ts +55 -0
- package/dist/gen-core/field.js +154 -0
- package/dist/gen-core/geometry.d.ts +31 -0
- package/dist/gen-core/geometry.js +231 -0
- package/dist/gen-core/hash.d.ts +18 -0
- package/dist/gen-core/hash.js +58 -0
- package/dist/gen-core/height-field.d.ts +33 -0
- package/dist/gen-core/height-field.js +137 -0
- package/dist/gen-core/index.d.ts +22 -0
- package/dist/gen-core/index.js +17 -0
- package/dist/gen-core/instance-cells.d.ts +37 -0
- package/dist/gen-core/instance-cells.js +72 -0
- package/dist/gen-core/near-scatter.d.ts +56 -0
- package/dist/gen-core/near-scatter.js +120 -0
- package/dist/gen-core/noise.d.ts +23 -0
- package/dist/gen-core/noise.js +72 -0
- package/dist/gen-core/occupancy.d.ts +21 -0
- package/dist/gen-core/occupancy.js +104 -0
- package/dist/gen-core/sphere-patch.d.ts +138 -0
- package/dist/gen-core/sphere-patch.js +544 -0
- package/dist/gen-core/strip-surface.d.ts +34 -0
- package/dist/gen-core/strip-surface.js +246 -0
- package/dist/gen-core/strip.d.ts +102 -0
- package/dist/gen-core/strip.js +817 -0
- package/dist/gen-ground/env-slots.d.ts +48 -0
- package/dist/gen-ground/env-slots.js +78 -0
- package/dist/gen-ground/ground.d.ts +78 -0
- package/dist/gen-ground/ground.js +208 -0
- package/dist/gen-ground/index.d.ts +9 -0
- package/dist/gen-ground/index.js +7 -0
- package/dist/gen-ground/markings.d.ts +46 -0
- package/dist/gen-ground/markings.js +124 -0
- package/dist/gen-ground/materials.d.ts +18 -0
- package/dist/gen-ground/materials.js +22 -0
- package/dist/gen-ground/terrain-shader.d.ts +5 -0
- package/dist/gen-ground/terrain-shader.js +151 -0
- package/dist/gen-mesh/bare-shapes.d.ts +194 -0
- package/dist/gen-mesh/bare-shapes.js +596 -0
- package/dist/gen-mesh/bisect-surface.d.ts +47 -0
- package/dist/gen-mesh/bisect-surface.js +261 -0
- package/dist/gen-mesh/box.d.ts +18 -0
- package/dist/gen-mesh/box.js +2 -0
- package/dist/gen-mesh/cell-morph-mesh.d.ts +52 -0
- package/dist/gen-mesh/cell-morph-mesh.js +258 -0
- package/dist/gen-mesh/cell-surface-mesh.d.ts +32 -0
- package/dist/gen-mesh/cell-surface-mesh.js +140 -0
- package/dist/gen-mesh/chunk-mesh.d.ts +25 -0
- package/dist/gen-mesh/chunk-mesh.js +112 -0
- package/dist/gen-mesh/drape-mesh.d.ts +36 -0
- package/dist/gen-mesh/drape-mesh.js +98 -0
- package/dist/gen-mesh/height-field-mesh.d.ts +14 -0
- package/dist/gen-mesh/height-field-mesh.js +98 -0
- package/dist/gen-mesh/index.d.ts +26 -0
- package/dist/gen-mesh/index.js +15 -0
- package/dist/gen-mesh/line-mesh.d.ts +25 -0
- package/dist/gen-mesh/line-mesh.js +62 -0
- package/dist/gen-mesh/mesh-builder.d.ts +115 -0
- package/dist/gen-mesh/mesh-builder.js +315 -0
- package/dist/gen-mesh/packed-mesh.d.ts +53 -0
- package/dist/gen-mesh/packed-mesh.js +205 -0
- package/dist/gen-mesh/prism-mesh.d.ts +33 -0
- package/dist/gen-mesh/prism-mesh.js +176 -0
- package/dist/gen-mesh/rig-mesh.d.ts +9 -0
- package/dist/gen-mesh/rig-mesh.js +135 -0
- package/dist/gen-mesh/strip-mesh.d.ts +45 -0
- package/dist/gen-mesh/strip-mesh.js +258 -0
- package/dist/gen-plan/drain.d.ts +58 -0
- package/dist/gen-plan/drain.js +270 -0
- package/dist/gen-plan/grid.d.ts +37 -0
- package/dist/gen-plan/grid.js +182 -0
- package/dist/gen-plan/horizon.d.ts +50 -0
- package/dist/gen-plan/horizon.js +185 -0
- package/dist/gen-plan/index.d.ts +15 -0
- package/dist/gen-plan/index.js +13 -0
- package/dist/gen-plan/line-tiles.d.ts +35 -0
- package/dist/gen-plan/line-tiles.js +190 -0
- package/dist/gen-plan/lines.d.ts +31 -0
- package/dist/gen-plan/lines.js +183 -0
- package/dist/gen-plan/plots.d.ts +40 -0
- package/dist/gen-plan/plots.js +282 -0
- package/dist/gen-plan/route.d.ts +55 -0
- package/dist/gen-plan/route.js +297 -0
- package/dist/gen-plan/sites.d.ts +32 -0
- package/dist/gen-plan/sites.js +209 -0
- package/dist/gen-tex/code-stamp.d.ts +31 -0
- package/dist/gen-tex/code-stamp.js +137 -0
- package/dist/gen-tex/index.d.ts +7 -0
- package/dist/gen-tex/index.js +5 -0
- package/dist/gen-tex/light-horizon.d.ts +47 -0
- package/dist/gen-tex/light-horizon.js +180 -0
- package/dist/gen-tex/textures.d.ts +8 -0
- package/dist/gen-tex/textures.js +49 -0
- package/dist/gen-tex/value-textures.d.ts +46 -0
- package/dist/gen-tex/value-textures.js +102 -0
- package/dist/gen-vegetation/env-slots.d.ts +20 -0
- package/dist/gen-vegetation/env-slots.js +29 -0
- package/dist/gen-vegetation/index.d.ts +8 -0
- package/dist/gen-vegetation/index.js +7 -0
- package/dist/gen-vegetation/materials.d.ts +7 -0
- package/dist/gen-vegetation/materials.js +17 -0
- package/dist/gen-vegetation/planting.d.ts +170 -0
- package/dist/gen-vegetation/planting.js +363 -0
- package/dist/gen-vegetation/tree-shader.d.ts +23 -0
- package/dist/gen-vegetation/tree-shader.js +229 -0
- package/dist/gen-vegetation/trees.d.ts +59 -0
- package/dist/gen-vegetation/trees.js +329 -0
- package/dist/gpu/device.d.ts +286 -0
- package/dist/gpu/device.js +1157 -0
- package/dist/gpu/gl.d.ts +134 -0
- package/dist/gpu/gl.js +276 -0
- package/dist/gpu/index.d.ts +4 -0
- package/dist/gpu/index.js +3 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +13 -0
- package/dist/motion/graph.d.ts +244 -0
- package/dist/motion/graph.js +739 -0
- package/dist/motion/index.d.ts +8 -0
- package/dist/motion/index.js +8 -0
- package/dist/motion/pose-ops.d.ts +30 -0
- package/dist/motion/pose-ops.js +232 -0
- package/dist/motion/reach.d.ts +45 -0
- package/dist/motion/reach.js +264 -0
- package/dist/motion/skeleton.d.ts +49 -0
- package/dist/motion/skeleton.js +99 -0
- package/dist/platform/byte-store.d.ts +21 -0
- package/dist/platform/byte-store.js +137 -0
- package/dist/platform/canvas.d.ts +29 -0
- package/dist/platform/canvas.js +76 -0
- package/dist/platform/context-loss.d.ts +8 -0
- package/dist/platform/context-loss.js +17 -0
- package/dist/platform/free-input.d.ts +51 -0
- package/dist/platform/free-input.js +100 -0
- package/dist/platform/index.d.ts +18 -0
- package/dist/platform/index.js +10 -0
- package/dist/platform/input.d.ts +48 -0
- package/dist/platform/input.js +117 -0
- package/dist/platform/keys.d.ts +12 -0
- package/dist/platform/keys.js +29 -0
- package/dist/platform/loop.d.ts +41 -0
- package/dist/platform/loop.js +84 -0
- package/dist/platform/walk-input.d.ts +74 -0
- package/dist/platform/walk-input.js +226 -0
- package/dist/platform/worker.d.ts +51 -0
- package/dist/platform/worker.js +170 -0
- package/dist/render/bone-instances.d.ts +24 -0
- package/dist/render/bone-instances.js +74 -0
- package/dist/render/cells.d.ts +70 -0
- package/dist/render/cells.js +144 -0
- package/dist/render/glsl.d.ts +66 -0
- package/dist/render/glsl.js +173 -0
- package/dist/render/index.d.ts +26 -0
- package/dist/render/index.js +16 -0
- package/dist/render/instance-sets.d.ts +97 -0
- package/dist/render/instance-sets.js +241 -0
- package/dist/render/near-instances.d.ts +75 -0
- package/dist/render/near-instances.js +219 -0
- package/dist/render/particles.d.ts +63 -0
- package/dist/render/particles.js +215 -0
- package/dist/render/pieces.d.ts +57 -0
- package/dist/render/pieces.js +151 -0
- package/dist/render/point-lights.d.ts +97 -0
- package/dist/render/point-lights.js +243 -0
- package/dist/render/renderer.d.ts +447 -0
- package/dist/render/renderer.js +1100 -0
- package/dist/render/rig.d.ts +74 -0
- package/dist/render/rig.js +576 -0
- package/dist/render/shadow-areas.d.ts +49 -0
- package/dist/render/shadow-areas.js +125 -0
- package/dist/render/shadow-glsl.d.ts +12 -0
- package/dist/render/shadow-glsl.js +146 -0
- package/dist/render/transient.d.ts +61 -0
- package/dist/render/transient.js +213 -0
- package/dist/render/value-glsl.d.ts +17 -0
- package/dist/render/value-glsl.js +208 -0
- package/dist/render/value-layers.d.ts +180 -0
- package/dist/render/value-layers.js +333 -0
- package/dist/runtime/atmosphere.d.ts +37 -0
- package/dist/runtime/atmosphere.js +304 -0
- package/dist/runtime/cloud-shell.d.ts +29 -0
- package/dist/runtime/cloud-shell.js +158 -0
- package/dist/runtime/engine.d.ts +155 -0
- package/dist/runtime/engine.js +305 -0
- package/dist/runtime/environment-link.d.ts +22 -0
- package/dist/runtime/environment-link.js +49 -0
- package/dist/runtime/index.d.ts +13 -0
- package/dist/runtime/index.js +12 -0
- package/dist/runtime/precipitation-glsl.d.ts +2 -0
- package/dist/runtime/precipitation-glsl.js +70 -0
- package/dist/runtime/precipitation.d.ts +11 -0
- package/dist/runtime/precipitation.js +78 -0
- package/dist/runtime/sky.d.ts +30 -0
- package/dist/runtime/sky.js +114 -0
- package/dist/runtime/streaming.d.ts +245 -0
- package/dist/runtime/streaming.js +602 -0
- package/dist/sim/agenda.d.ts +167 -0
- package/dist/sim/agenda.js +425 -0
- package/dist/sim/bytes.d.ts +34 -0
- package/dist/sim/bytes.js +125 -0
- package/dist/sim/destruction/apply.d.ts +59 -0
- package/dist/sim/destruction/apply.js +243 -0
- package/dist/sim/destruction/cell-states.d.ts +54 -0
- package/dist/sim/destruction/cell-states.js +198 -0
- package/dist/sim/destruction/index.d.ts +11 -0
- package/dist/sim/destruction/index.js +10 -0
- package/dist/sim/destruction/int-geometry.d.ts +17 -0
- package/dist/sim/destruction/int-geometry.js +131 -0
- package/dist/sim/destruction/pick.d.ts +13 -0
- package/dist/sim/destruction/pick.js +59 -0
- package/dist/sim/destruction/rubble.d.ts +8 -0
- package/dist/sim/destruction/rubble.js +159 -0
- package/dist/sim/destruction/state.d.ts +15 -0
- package/dist/sim/destruction/state.js +83 -0
- package/dist/sim/destruction/structure.d.ts +30 -0
- package/dist/sim/destruction/structure.js +139 -0
- package/dist/sim/events.d.ts +37 -0
- package/dist/sim/events.js +106 -0
- package/dist/sim/fixed.d.ts +21 -0
- package/dist/sim/fixed.js +64 -0
- package/dist/sim/flow-probe.d.ts +58 -0
- package/dist/sim/flow-probe.js +158 -0
- package/dist/sim/flow.d.ts +257 -0
- package/dist/sim/flow.js +967 -0
- package/dist/sim/index.d.ts +28 -0
- package/dist/sim/index.js +20 -0
- package/dist/sim/interlock.d.ts +47 -0
- package/dist/sim/interlock.js +252 -0
- package/dist/sim/movers.d.ts +228 -0
- package/dist/sim/movers.js +1083 -0
- package/dist/sim/nav.d.ts +72 -0
- package/dist/sim/nav.js +292 -0
- package/dist/sim/paths.d.ts +80 -0
- package/dist/sim/paths.js +197 -0
- package/dist/sim/regions.d.ts +87 -0
- package/dist/sim/regions.js +517 -0
- package/dist/sim/simulation.d.ts +114 -0
- package/dist/sim/simulation.js +291 -0
- package/dist/sim/states.d.ts +23 -0
- package/dist/sim/states.js +54 -0
- package/dist/sim/table.d.ts +49 -0
- package/dist/sim/table.js +126 -0
- package/dist/sim/zones.d.ts +11 -0
- package/dist/sim/zones.js +196 -0
- package/dist/tools/collision-lines.d.ts +12 -0
- package/dist/tools/collision-lines.js +50 -0
- package/dist/tools/data-view-glsl.d.ts +3 -0
- package/dist/tools/data-view-glsl.js +63 -0
- package/dist/tools/data-view-layers.d.ts +20 -0
- package/dist/tools/data-view-layers.js +60 -0
- package/dist/tools/debug-view.d.ts +30 -0
- package/dist/tools/debug-view.js +4 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +7 -0
- package/dist/tools/mesh-checks.d.ts +67 -0
- package/dist/tools/mesh-checks.js +188 -0
- package/dist/tools/profile.d.ts +85 -0
- package/dist/tools/profile.js +173 -0
- package/dist/world/cell-cache.d.ts +32 -0
- package/dist/world/cell-cache.js +60 -0
- package/dist/world/cell-format.d.ts +25 -0
- package/dist/world/cell-format.js +232 -0
- package/dist/world/collide.d.ts +192 -0
- package/dist/world/collide.js +822 -0
- package/dist/world/index.d.ts +16 -0
- package/dist/world/index.js +12 -0
- package/dist/world/mesh-solids.d.ts +48 -0
- package/dist/world/mesh-solids.js +169 -0
- package/dist/world/pick.d.ts +34 -0
- package/dist/world/pick.js +177 -0
- package/dist/world/providers.d.ts +66 -0
- package/dist/world/providers.js +83 -0
- package/dist/world/sphere.d.ts +69 -0
- package/dist/world/sphere.js +271 -0
- package/dist/world/streaming.d.ts +288 -0
- package/dist/world/streaming.js +801 -0
- package/dist/world/walker.d.ts +145 -0
- package/dist/world/walker.js +502 -0
- package/docs/architecture.md +111 -0
- package/docs/cameras.md +302 -0
- package/docs/concepts.md +123 -0
- package/docs/destruction.md +127 -0
- package/docs/frames.md +199 -0
- package/docs/generators.md +575 -0
- package/docs/getting-started.md +123 -0
- package/docs/headless.md +50 -0
- package/docs/motion.md +160 -0
- package/docs/planet.md +152 -0
- package/docs/recipes.md +521 -0
- package/docs/reference.md +364 -0
- package/docs/rendering.md +823 -0
- package/docs/rigs.md +203 -0
- package/docs/simulation.md +235 -0
- package/docs/streaming.md +590 -0
- package/docs/system.md +166 -0
- package/docs/tools.md +175 -0
- package/docs/walking.md +262 -0
- package/package.json +131 -3
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { ByteReader, ByteWriter } from './bytes.ts';
|
|
2
|
+
import type { PathDef, PathStop } from './paths.ts';
|
|
3
|
+
import type { SimState } from './simulation.ts';
|
|
4
|
+
export interface NavEdgeInput {
|
|
5
|
+
from: number;
|
|
6
|
+
to: number;
|
|
7
|
+
/** Integer cost; default the length in mm. */
|
|
8
|
+
cost?: number;
|
|
9
|
+
/** Speed limit in m/s, 0 or missing = none. */
|
|
10
|
+
speed?: number;
|
|
11
|
+
/** Also add the edge in the opposite direction (same cost and limit). */
|
|
12
|
+
both?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface NavGraphInput {
|
|
15
|
+
/** Node positions as x, z pairs in metres. */
|
|
16
|
+
nodes: readonly number[];
|
|
17
|
+
edges: readonly NavEdgeInput[];
|
|
18
|
+
}
|
|
19
|
+
export interface RouteOptions {
|
|
20
|
+
/** The route returns to its start and repeats. */
|
|
21
|
+
loop?: boolean;
|
|
22
|
+
/** Shift to the right of the direction of travel in metres, so opposite directions use separate lanes. */
|
|
23
|
+
rightOffset?: number;
|
|
24
|
+
/** Stops at route vertices (vertex i is where edge i starts; the last vertex of an open route is edges.length). */
|
|
25
|
+
stops?: readonly PathStop[];
|
|
26
|
+
}
|
|
27
|
+
export declare class NavGraph implements SimState {
|
|
28
|
+
readonly name: string;
|
|
29
|
+
readonly nodeCount: number;
|
|
30
|
+
readonly edgeCount: number;
|
|
31
|
+
/** Node positions in metres and in whole millimetres (A* works on the millimetres). */
|
|
32
|
+
readonly nodeX: Float64Array;
|
|
33
|
+
readonly nodeZ: Float64Array;
|
|
34
|
+
private readonly mmX;
|
|
35
|
+
private readonly mmZ;
|
|
36
|
+
readonly edgeFrom: Int32Array;
|
|
37
|
+
readonly edgeTo: Int32Array;
|
|
38
|
+
readonly edgeLength: Int32Array;
|
|
39
|
+
readonly edgeCost: Int32Array;
|
|
40
|
+
readonly edgeSpeed: Float64Array;
|
|
41
|
+
readonly blocked: Uint8Array;
|
|
42
|
+
/** Outgoing edges of node n: `outEdges[outStart[n] … outStart[n + 1]]`, in input order. */
|
|
43
|
+
private readonly outStart;
|
|
44
|
+
private readonly outEdges;
|
|
45
|
+
private readonly cache;
|
|
46
|
+
/** Lowest cost per mm of length, keeps the heuristic admissible. */
|
|
47
|
+
private costPerMm;
|
|
48
|
+
/** Number of A* searches run (cache misses). */
|
|
49
|
+
searches: number;
|
|
50
|
+
constructor(name: string, input: NavGraphInput);
|
|
51
|
+
private updateHeuristic;
|
|
52
|
+
private invalidate;
|
|
53
|
+
/** The first edge from one node to another, or -1. */
|
|
54
|
+
findEdge(from: number, to: number): number;
|
|
55
|
+
/** Outgoing edges of a node, in input order. */
|
|
56
|
+
edgesFrom(node: number): Int32Array;
|
|
57
|
+
setCost(edge: number, cost: number): void;
|
|
58
|
+
setBlocked(edge: number, blocked: boolean): void;
|
|
59
|
+
private heuristic;
|
|
60
|
+
/** Lowest-cost route as a list of edges (empty when from = to), or null when the goal cannot be reached. Cached. */
|
|
61
|
+
findPath(from: number, to: number): readonly number[] | null;
|
|
62
|
+
private search;
|
|
63
|
+
/** Nodes visited by a route of edges, from the first edge's start. */
|
|
64
|
+
routeNodes(edges: readonly number[]): number[];
|
|
65
|
+
/**
|
|
66
|
+
* A path along a route of edges: one segment per edge with the edge as its lane and the edge's speed limit.
|
|
67
|
+
* With `rightOffset` the path runs that far to the right of the edges (mitred at the vertices).
|
|
68
|
+
*/
|
|
69
|
+
path(edges: readonly number[], options?: RouteOptions): PathDef;
|
|
70
|
+
save(w: ByteWriter): void;
|
|
71
|
+
load(r: ByteReader): void;
|
|
72
|
+
}
|
package/dist/sim/nav.js
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
// Navigation graph: nodes in metres, directed edges with an integer length (mm) and cost, an optional speed limit and
|
|
2
|
+
// a blocked flag. A* over integer costs with a fixed tie-break (f, then h, then node index), so the same graph and
|
|
3
|
+
// query always give the same route on every machine. Routes are cached; changing a cost or blocking an edge clears
|
|
4
|
+
// the cache. Costs and blocked flags may change at run time, so the graph is simulation state.
|
|
5
|
+
import { ByteReader, ByteWriter } from './bytes.js';
|
|
6
|
+
import { lengthMm, snapMm } from './fixed.js';
|
|
7
|
+
const CACHE_LIMIT = 4096;
|
|
8
|
+
export class NavGraph {
|
|
9
|
+
name;
|
|
10
|
+
nodeCount;
|
|
11
|
+
edgeCount;
|
|
12
|
+
/** Node positions in metres and in whole millimetres (A* works on the millimetres). */
|
|
13
|
+
nodeX;
|
|
14
|
+
nodeZ;
|
|
15
|
+
mmX;
|
|
16
|
+
mmZ;
|
|
17
|
+
edgeFrom;
|
|
18
|
+
edgeTo;
|
|
19
|
+
edgeLength;
|
|
20
|
+
edgeCost;
|
|
21
|
+
edgeSpeed;
|
|
22
|
+
blocked;
|
|
23
|
+
/** Outgoing edges of node n: `outEdges[outStart[n] … outStart[n + 1]]`, in input order. */
|
|
24
|
+
outStart;
|
|
25
|
+
outEdges;
|
|
26
|
+
cache = new Map();
|
|
27
|
+
/** Lowest cost per mm of length, keeps the heuristic admissible. */
|
|
28
|
+
costPerMm = 1;
|
|
29
|
+
/** Number of A* searches run (cache misses). */
|
|
30
|
+
searches = 0;
|
|
31
|
+
constructor(name, input) {
|
|
32
|
+
this.name = name;
|
|
33
|
+
const n = input.nodes.length / 2;
|
|
34
|
+
this.nodeCount = n;
|
|
35
|
+
this.nodeX = new Float64Array(n);
|
|
36
|
+
this.nodeZ = new Float64Array(n);
|
|
37
|
+
this.mmX = new Int32Array(n);
|
|
38
|
+
this.mmZ = new Int32Array(n);
|
|
39
|
+
for (let i = 0; i < n; i++) {
|
|
40
|
+
// Everything that enters the simulation is quantised at the boundary: whole millimetres, so a graph built with
|
|
41
|
+
// Math.cos/sin gives the same simulation on every machine.
|
|
42
|
+
this.mmX[i] = Math.round(input.nodes[i * 2] * 1000);
|
|
43
|
+
this.mmZ[i] = Math.round(input.nodes[i * 2 + 1] * 1000);
|
|
44
|
+
this.nodeX[i] = this.mmX[i] / 1000;
|
|
45
|
+
this.nodeZ[i] = this.mmZ[i] / 1000;
|
|
46
|
+
}
|
|
47
|
+
const list = [];
|
|
48
|
+
for (const e of input.edges) {
|
|
49
|
+
if (e.from < 0 || e.from >= n || e.to < 0 || e.to >= n || e.from === e.to)
|
|
50
|
+
throw new Error(`NavGraph: bad edge ${e.from} → ${e.to}`);
|
|
51
|
+
list.push({ from: e.from, to: e.to, ...(e.cost !== undefined ? { cost: e.cost } : {}), speed: e.speed ?? 0 });
|
|
52
|
+
if (e.both)
|
|
53
|
+
list.push({ from: e.to, to: e.from, ...(e.cost !== undefined ? { cost: e.cost } : {}), speed: e.speed ?? 0 });
|
|
54
|
+
}
|
|
55
|
+
const m = list.length;
|
|
56
|
+
this.edgeCount = m;
|
|
57
|
+
this.edgeFrom = new Int32Array(m);
|
|
58
|
+
this.edgeTo = new Int32Array(m);
|
|
59
|
+
this.edgeLength = new Int32Array(m);
|
|
60
|
+
this.edgeCost = new Int32Array(m);
|
|
61
|
+
this.edgeSpeed = new Float64Array(m);
|
|
62
|
+
this.blocked = new Uint8Array(m);
|
|
63
|
+
const degree = new Int32Array(n + 1);
|
|
64
|
+
list.forEach((e, i) => {
|
|
65
|
+
this.edgeFrom[i] = e.from;
|
|
66
|
+
this.edgeTo[i] = e.to;
|
|
67
|
+
const len = Math.max(1, lengthMm(this.mmX[e.to] - this.mmX[e.from], this.mmZ[e.to] - this.mmZ[e.from]));
|
|
68
|
+
this.edgeLength[i] = len;
|
|
69
|
+
this.edgeCost[i] = Math.max(0, Math.round(e.cost ?? len));
|
|
70
|
+
this.edgeSpeed[i] = snapMm(e.speed);
|
|
71
|
+
degree[e.from + 1]++;
|
|
72
|
+
});
|
|
73
|
+
this.outStart = new Int32Array(n + 1);
|
|
74
|
+
for (let i = 0; i < n; i++)
|
|
75
|
+
this.outStart[i + 1] = this.outStart[i] + degree[i + 1];
|
|
76
|
+
this.outEdges = new Int32Array(m);
|
|
77
|
+
const fill = this.outStart.slice(0, n);
|
|
78
|
+
for (let i = 0; i < m; i++)
|
|
79
|
+
this.outEdges[fill[this.edgeFrom[i]]++] = i;
|
|
80
|
+
this.updateHeuristic();
|
|
81
|
+
}
|
|
82
|
+
updateHeuristic() {
|
|
83
|
+
let r = Infinity;
|
|
84
|
+
for (let i = 0; i < this.edgeCount; i++)
|
|
85
|
+
r = Math.min(r, this.edgeCost[i] / this.edgeLength[i]);
|
|
86
|
+
this.costPerMm = Number.isFinite(r) ? r : 1;
|
|
87
|
+
}
|
|
88
|
+
invalidate() {
|
|
89
|
+
this.cache.clear();
|
|
90
|
+
this.updateHeuristic();
|
|
91
|
+
}
|
|
92
|
+
/** The first edge from one node to another, or -1. */
|
|
93
|
+
findEdge(from, to) {
|
|
94
|
+
for (let k = this.outStart[from]; k < this.outStart[from + 1]; k++)
|
|
95
|
+
if (this.edgeTo[this.outEdges[k]] === to)
|
|
96
|
+
return this.outEdges[k];
|
|
97
|
+
return -1;
|
|
98
|
+
}
|
|
99
|
+
/** Outgoing edges of a node, in input order. */
|
|
100
|
+
edgesFrom(node) {
|
|
101
|
+
return this.outEdges.subarray(this.outStart[node], this.outStart[node + 1]);
|
|
102
|
+
}
|
|
103
|
+
setCost(edge, cost) {
|
|
104
|
+
this.edgeCost[edge] = Math.max(0, Math.round(cost));
|
|
105
|
+
this.invalidate();
|
|
106
|
+
}
|
|
107
|
+
setBlocked(edge, blocked) {
|
|
108
|
+
this.blocked[edge] = blocked ? 1 : 0;
|
|
109
|
+
this.invalidate();
|
|
110
|
+
}
|
|
111
|
+
heuristic(a, b) {
|
|
112
|
+
return Math.floor(lengthMm(this.mmX[b] - this.mmX[a], this.mmZ[b] - this.mmZ[a]) * this.costPerMm);
|
|
113
|
+
}
|
|
114
|
+
/** Lowest-cost route as a list of edges (empty when from = to), or null when the goal cannot be reached. Cached. */
|
|
115
|
+
findPath(from, to) {
|
|
116
|
+
const key = from * this.nodeCount + to;
|
|
117
|
+
const hit = this.cache.get(key);
|
|
118
|
+
if (hit !== undefined)
|
|
119
|
+
return hit;
|
|
120
|
+
const route = this.search(from, to);
|
|
121
|
+
if (this.cache.size >= CACHE_LIMIT)
|
|
122
|
+
this.cache.clear();
|
|
123
|
+
this.cache.set(key, route);
|
|
124
|
+
return route;
|
|
125
|
+
}
|
|
126
|
+
search(from, to) {
|
|
127
|
+
this.searches++;
|
|
128
|
+
if (from === to)
|
|
129
|
+
return [];
|
|
130
|
+
const n = this.nodeCount;
|
|
131
|
+
const g = new Float64Array(n).fill(Infinity);
|
|
132
|
+
const via = new Int32Array(n).fill(-1);
|
|
133
|
+
const closed = new Uint8Array(n);
|
|
134
|
+
// Binary heap of (f, h, node), ordered by f, then h, then node index.
|
|
135
|
+
const hf = [];
|
|
136
|
+
const hh = [];
|
|
137
|
+
const hn = [];
|
|
138
|
+
const less = (i, j) => hf[i] < hf[j] || (hf[i] === hf[j] && (hh[i] < hh[j] || (hh[i] === hh[j] && hn[i] < hn[j])));
|
|
139
|
+
const swap = (i, j) => {
|
|
140
|
+
[hf[i], hf[j]] = [hf[j], hf[i]];
|
|
141
|
+
[hh[i], hh[j]] = [hh[j], hh[i]];
|
|
142
|
+
[hn[i], hn[j]] = [hn[j], hn[i]];
|
|
143
|
+
};
|
|
144
|
+
const push = (f, h, node) => {
|
|
145
|
+
hf.push(f);
|
|
146
|
+
hh.push(h);
|
|
147
|
+
hn.push(node);
|
|
148
|
+
let i = hf.length - 1;
|
|
149
|
+
while (i > 0) {
|
|
150
|
+
const p = (i - 1) >> 1;
|
|
151
|
+
if (!less(i, p))
|
|
152
|
+
break;
|
|
153
|
+
swap(i, p);
|
|
154
|
+
i = p;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
const pop = () => {
|
|
158
|
+
const top = hn[0];
|
|
159
|
+
const last = hf.length - 1;
|
|
160
|
+
swap(0, last);
|
|
161
|
+
hf.pop();
|
|
162
|
+
hh.pop();
|
|
163
|
+
hn.pop();
|
|
164
|
+
let i = 0;
|
|
165
|
+
for (;;) {
|
|
166
|
+
const l = i * 2 + 1;
|
|
167
|
+
const r = l + 1;
|
|
168
|
+
let m = i;
|
|
169
|
+
if (l < hf.length && less(l, m))
|
|
170
|
+
m = l;
|
|
171
|
+
if (r < hf.length && less(r, m))
|
|
172
|
+
m = r;
|
|
173
|
+
if (m === i)
|
|
174
|
+
break;
|
|
175
|
+
swap(i, m);
|
|
176
|
+
i = m;
|
|
177
|
+
}
|
|
178
|
+
return top;
|
|
179
|
+
};
|
|
180
|
+
g[from] = 0;
|
|
181
|
+
const h0 = this.heuristic(from, to);
|
|
182
|
+
push(h0, h0, from);
|
|
183
|
+
while (hf.length > 0) {
|
|
184
|
+
const u = pop();
|
|
185
|
+
if (closed[u])
|
|
186
|
+
continue;
|
|
187
|
+
if (u === to)
|
|
188
|
+
break;
|
|
189
|
+
closed[u] = 1;
|
|
190
|
+
for (let k = this.outStart[u]; k < this.outStart[u + 1]; k++) {
|
|
191
|
+
const e = this.outEdges[k];
|
|
192
|
+
if (this.blocked[e])
|
|
193
|
+
continue;
|
|
194
|
+
const v = this.edgeTo[e];
|
|
195
|
+
if (closed[v])
|
|
196
|
+
continue;
|
|
197
|
+
const gv = g[u] + this.edgeCost[e];
|
|
198
|
+
if (gv < g[v]) {
|
|
199
|
+
g[v] = gv;
|
|
200
|
+
via[v] = e;
|
|
201
|
+
const h = this.heuristic(v, to);
|
|
202
|
+
push(gv + h, h, v);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (via[to] === -1)
|
|
207
|
+
return null;
|
|
208
|
+
const edges = [];
|
|
209
|
+
for (let v = to; v !== from; v = this.edgeFrom[via[v]])
|
|
210
|
+
edges.push(via[v]);
|
|
211
|
+
return edges.reverse();
|
|
212
|
+
}
|
|
213
|
+
/** Nodes visited by a route of edges, from the first edge's start. */
|
|
214
|
+
routeNodes(edges) {
|
|
215
|
+
if (edges.length === 0)
|
|
216
|
+
return [];
|
|
217
|
+
return [this.edgeFrom[edges[0]], ...edges.map((e) => this.edgeTo[e])];
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* A path along a route of edges: one segment per edge with the edge as its lane and the edge's speed limit.
|
|
221
|
+
* With `rightOffset` the path runs that far to the right of the edges (mitred at the vertices).
|
|
222
|
+
*/
|
|
223
|
+
path(edges, options = {}) {
|
|
224
|
+
if (edges.length === 0)
|
|
225
|
+
throw new Error('NavGraph: an empty route has no path');
|
|
226
|
+
const loop = options.loop ?? false;
|
|
227
|
+
for (let i = 1; i < edges.length; i++) {
|
|
228
|
+
if (this.edgeFrom[edges[i]] !== this.edgeTo[edges[i - 1]])
|
|
229
|
+
throw new Error(`NavGraph: edges ${edges[i - 1]} and ${edges[i]} are not connected`);
|
|
230
|
+
}
|
|
231
|
+
if (loop && this.edgeTo[edges[edges.length - 1]] !== this.edgeFrom[edges[0]])
|
|
232
|
+
throw new Error('NavGraph: a loop route must end where it starts');
|
|
233
|
+
const nodes = this.routeNodes(edges);
|
|
234
|
+
if (loop)
|
|
235
|
+
nodes.pop();
|
|
236
|
+
const off = options.rightOffset ?? 0;
|
|
237
|
+
// Right-hand normal of each edge: (−dz, dx) for a direction (dx, dz), with x east and z south.
|
|
238
|
+
const normal = (e) => {
|
|
239
|
+
const a = this.edgeFrom[e];
|
|
240
|
+
const b = this.edgeTo[e];
|
|
241
|
+
const dx = this.nodeX[b] - this.nodeX[a];
|
|
242
|
+
const dz = this.nodeZ[b] - this.nodeZ[a];
|
|
243
|
+
const l = Math.sqrt(dx * dx + dz * dz) || 1;
|
|
244
|
+
return [-dz / l, dx / l];
|
|
245
|
+
};
|
|
246
|
+
const points = [];
|
|
247
|
+
const count = nodes.length;
|
|
248
|
+
for (let i = 0; i < count; i++) {
|
|
249
|
+
const node = nodes[i];
|
|
250
|
+
let nx = 0;
|
|
251
|
+
let nz = 0;
|
|
252
|
+
if (off !== 0) {
|
|
253
|
+
const inE = i > 0 ? edges[i - 1] : loop ? edges[edges.length - 1] : undefined;
|
|
254
|
+
const outE = i < edges.length ? edges[i] : undefined;
|
|
255
|
+
const a = inE !== undefined ? normal(inE) : null;
|
|
256
|
+
const b = outE !== undefined ? normal(outE) : null;
|
|
257
|
+
if (a && b) {
|
|
258
|
+
const d = 1 + a[0] * b[0] + a[1] * b[1];
|
|
259
|
+
// Mitre: the offset point lies on both offset lines. Near a U-turn it would run away, so it stays plain.
|
|
260
|
+
const k = d > 0.3 ? off / d : off / 2;
|
|
261
|
+
nx = (a[0] + b[0]) * k;
|
|
262
|
+
nz = (a[1] + b[1]) * k;
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
const c = (a ?? b);
|
|
266
|
+
nx = c[0] * off;
|
|
267
|
+
nz = c[1] * off;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
points.push(this.nodeX[node] + nx, this.nodeZ[node] + nz);
|
|
271
|
+
}
|
|
272
|
+
return {
|
|
273
|
+
points,
|
|
274
|
+
loop,
|
|
275
|
+
speeds: edges.map((e) => this.edgeSpeed[e]),
|
|
276
|
+
lanes: [...edges],
|
|
277
|
+
...(options.stops ? { stops: options.stops } : {}),
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
save(w) {
|
|
281
|
+
w.u32(this.edgeCount);
|
|
282
|
+
w.bytes(this.edgeCost);
|
|
283
|
+
w.bytes(this.blocked);
|
|
284
|
+
}
|
|
285
|
+
load(r) {
|
|
286
|
+
if (r.u32() !== this.edgeCount)
|
|
287
|
+
throw new Error('NavGraph: snapshot of another graph');
|
|
288
|
+
r.into(this.edgeCost);
|
|
289
|
+
r.into(this.blocked);
|
|
290
|
+
this.invalidate();
|
|
291
|
+
}
|
|
292
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { ByteReader, ByteWriter } from './bytes.ts';
|
|
2
|
+
import type { SimState } from './simulation.ts';
|
|
3
|
+
export interface PathStop {
|
|
4
|
+
/** Vertex index the stop sits at. */
|
|
5
|
+
vertex: number;
|
|
6
|
+
/** Seconds to stay. */
|
|
7
|
+
dwell: number;
|
|
8
|
+
}
|
|
9
|
+
/** A path as the app describes it. */
|
|
10
|
+
export interface PathDef {
|
|
11
|
+
/** Vertices as x, z pairs in metres. */
|
|
12
|
+
points: readonly number[];
|
|
13
|
+
/** A loop returns from the last vertex to the first and starts over. */
|
|
14
|
+
loop?: boolean;
|
|
15
|
+
/** Speed limit per segment in m/s; 0 or missing = no limit. */
|
|
16
|
+
speeds?: readonly number[];
|
|
17
|
+
/**
|
|
18
|
+
* Lane key per segment (≥ 0), e.g. a graph edge. Entities on the same lane keep their distance (collision `follow`).
|
|
19
|
+
* Missing = the segment has a lane of its own, shared only by entities on this same path.
|
|
20
|
+
*/
|
|
21
|
+
lanes?: readonly number[];
|
|
22
|
+
stops?: readonly PathStop[];
|
|
23
|
+
}
|
|
24
|
+
/** A built path: integer millimetres and ticks. */
|
|
25
|
+
export interface Path {
|
|
26
|
+
readonly id: number;
|
|
27
|
+
readonly loop: boolean;
|
|
28
|
+
/** Vertices in metres (for extraction). */
|
|
29
|
+
readonly xs: Float64Array;
|
|
30
|
+
readonly zs: Float64Array;
|
|
31
|
+
readonly segments: number;
|
|
32
|
+
/** Start of each segment along the path in mm; `segStart[segments]` is the length. */
|
|
33
|
+
readonly segStart: Int32Array;
|
|
34
|
+
/** Speed limit per segment in mm per tick, 0 = none. */
|
|
35
|
+
readonly segLimit: Int32Array;
|
|
36
|
+
/** Lane key per segment. */
|
|
37
|
+
readonly segLane: Float64Array;
|
|
38
|
+
/**
|
|
39
|
+
* Where each segment starts along its lane, mm: the start point projected on the segment's direction. Paths that
|
|
40
|
+
* share a straight lane but enter it at different points (corners cut differently) measure positions on it alike.
|
|
41
|
+
*/
|
|
42
|
+
readonly segOrigin: Int32Array;
|
|
43
|
+
/** Stops along the path in mm (ascending) and their dwell in ticks. */
|
|
44
|
+
readonly stopAt: Int32Array;
|
|
45
|
+
readonly stopDwell: Int32Array;
|
|
46
|
+
/** Freed automatically when its single user leaves it. */
|
|
47
|
+
readonly owned: boolean;
|
|
48
|
+
readonly length: number;
|
|
49
|
+
}
|
|
50
|
+
export declare class PathSet implements SimState {
|
|
51
|
+
readonly name: string;
|
|
52
|
+
readonly hz: number;
|
|
53
|
+
private readonly paths;
|
|
54
|
+
private readonly stored;
|
|
55
|
+
private nextId;
|
|
56
|
+
constructor(name: string, hz: number);
|
|
57
|
+
get size(): number;
|
|
58
|
+
/** Adds a path and returns its id (ids count up and are never reused). */
|
|
59
|
+
add(def: PathDef, options?: {
|
|
60
|
+
owned?: boolean;
|
|
61
|
+
}): number;
|
|
62
|
+
private put;
|
|
63
|
+
get(id: number): Path;
|
|
64
|
+
has(id: number): boolean;
|
|
65
|
+
release(id: number): void;
|
|
66
|
+
/** Point at a distance (mm, may be fractional) along a path, in metres. Distances outside wrap (loop) or clamp. */
|
|
67
|
+
pointAt(p: Path, d: number, out: [number, number]): void;
|
|
68
|
+
/**
|
|
69
|
+
* Position, heading and curvature at a distance along a path (mm), for extraction: `out` gets x, z (metres), the
|
|
70
|
+
* unit direction x, z taken between the points `look` mm behind and ahead, and the signed curvature in 1/m
|
|
71
|
+
* (positive bends right, clockwise seen from above). A trailing segment of a rig uses the same call a fixed
|
|
72
|
+
* distance further back.
|
|
73
|
+
*/
|
|
74
|
+
frameAt(p: Path, d: number, look: number, out: Float64Array): void;
|
|
75
|
+
private readonly fa;
|
|
76
|
+
private readonly fb;
|
|
77
|
+
private readonly fm;
|
|
78
|
+
save(w: ByteWriter): void;
|
|
79
|
+
load(r: ByteReader): void;
|
|
80
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// Paths entities move along: a polyline in metres with an optional speed limit and lane per segment and stops at
|
|
2
|
+
// vertices. Inside, a path is integer millimetres (segment starts, limits in mm per tick, dwell in ticks), so moving
|
|
3
|
+
// along it is exact. A path set is simulation state: paths created at run time (routes) are saved in snapshots.
|
|
4
|
+
import { ByteReader, ByteWriter } from './bytes.js';
|
|
5
|
+
import { lengthMm, mmPerTick, snapMm } from './fixed.js';
|
|
6
|
+
const PRIVATE_LANE_SPAN = 2 ** 20;
|
|
7
|
+
export class PathSet {
|
|
8
|
+
name;
|
|
9
|
+
hz;
|
|
10
|
+
paths = new Map();
|
|
11
|
+
stored = new Map();
|
|
12
|
+
nextId = 1;
|
|
13
|
+
constructor(name, hz) {
|
|
14
|
+
this.name = name;
|
|
15
|
+
this.hz = hz;
|
|
16
|
+
}
|
|
17
|
+
get size() {
|
|
18
|
+
return this.paths.size;
|
|
19
|
+
}
|
|
20
|
+
/** Adds a path and returns its id (ids count up and are never reused). */
|
|
21
|
+
add(def, options = {}) {
|
|
22
|
+
const n = def.points.length / 2;
|
|
23
|
+
if (n < 2 || !Number.isInteger(n))
|
|
24
|
+
throw new Error('PathSet: a path needs at least two x, z points');
|
|
25
|
+
const segments = def.loop ? n : n - 1;
|
|
26
|
+
const id = this.nextId++;
|
|
27
|
+
const lanes = new Float64Array(segments);
|
|
28
|
+
for (let s = 0; s < segments; s++) {
|
|
29
|
+
const lane = def.lanes?.[s];
|
|
30
|
+
lanes[s] = lane !== undefined && lane >= 0 ? lane : -(id * PRIVATE_LANE_SPAN + s + 1);
|
|
31
|
+
}
|
|
32
|
+
const stops = [...(def.stops ?? [])].sort((a, b) => a.vertex - b.vertex);
|
|
33
|
+
const st = {
|
|
34
|
+
// Quantised at the boundary: points to whole millimetres (speeds become whole mm per tick below).
|
|
35
|
+
points: Float64Array.from(def.points, snapMm),
|
|
36
|
+
loop: def.loop ?? false,
|
|
37
|
+
limits: Int32Array.from({ length: segments }, (_, s) => mmPerTick(snapMm(def.speeds?.[s] ?? 0), this.hz)),
|
|
38
|
+
lanes,
|
|
39
|
+
stopVertex: Int32Array.from(stops, (s) => s.vertex),
|
|
40
|
+
stopDwell: Int32Array.from(stops, (s) => Math.round(s.dwell * this.hz)),
|
|
41
|
+
owned: options.owned ?? false,
|
|
42
|
+
};
|
|
43
|
+
this.put(id, st);
|
|
44
|
+
return id;
|
|
45
|
+
}
|
|
46
|
+
put(id, st) {
|
|
47
|
+
const n = st.points.length / 2;
|
|
48
|
+
const segments = st.loop ? n : n - 1;
|
|
49
|
+
const xs = new Float64Array(n);
|
|
50
|
+
const zs = new Float64Array(n);
|
|
51
|
+
for (let i = 0; i < n; i++) {
|
|
52
|
+
xs[i] = st.points[i * 2];
|
|
53
|
+
zs[i] = st.points[i * 2 + 1];
|
|
54
|
+
}
|
|
55
|
+
const segStart = new Int32Array(segments + 1);
|
|
56
|
+
for (let s = 0; s < segments; s++) {
|
|
57
|
+
const b = (s + 1) % n;
|
|
58
|
+
segStart[s + 1] = segStart[s] + Math.max(1, lengthMm((xs[b] - xs[s]) * 1000, (zs[b] - zs[s]) * 1000));
|
|
59
|
+
}
|
|
60
|
+
const segOrigin = new Int32Array(segments);
|
|
61
|
+
for (let s = 0; s < segments; s++) {
|
|
62
|
+
const b = (s + 1) % n;
|
|
63
|
+
const dx = xs[b] - xs[s];
|
|
64
|
+
const dz = zs[b] - zs[s];
|
|
65
|
+
const l = Math.sqrt(dx * dx + dz * dz);
|
|
66
|
+
segOrigin[s] = l > 0 ? Math.round(((xs[s] * dx + zs[s] * dz) / l) * 1000) : 0;
|
|
67
|
+
}
|
|
68
|
+
const stopAt = new Int32Array(st.stopVertex.length);
|
|
69
|
+
for (let k = 0; k < stopAt.length; k++) {
|
|
70
|
+
const v = st.stopVertex[k];
|
|
71
|
+
if (v < 0 || v > segments)
|
|
72
|
+
throw new Error(`PathSet: stop at vertex ${v} outside the path`);
|
|
73
|
+
stopAt[k] = st.loop && v === n ? 0 : segStart[v];
|
|
74
|
+
}
|
|
75
|
+
this.stored.set(id, st);
|
|
76
|
+
this.paths.set(id, { id, loop: st.loop, xs, zs, segments, segStart, segLimit: st.limits, segLane: st.lanes, segOrigin, stopAt, stopDwell: st.stopDwell, owned: st.owned, length: segStart[segments] });
|
|
77
|
+
}
|
|
78
|
+
get(id) {
|
|
79
|
+
const p = this.paths.get(id);
|
|
80
|
+
if (!p)
|
|
81
|
+
throw new Error(`PathSet: no path ${id}`);
|
|
82
|
+
return p;
|
|
83
|
+
}
|
|
84
|
+
has(id) {
|
|
85
|
+
return this.paths.has(id);
|
|
86
|
+
}
|
|
87
|
+
release(id) {
|
|
88
|
+
this.paths.delete(id);
|
|
89
|
+
this.stored.delete(id);
|
|
90
|
+
}
|
|
91
|
+
/** Point at a distance (mm, may be fractional) along a path, in metres. Distances outside wrap (loop) or clamp. */
|
|
92
|
+
pointAt(p, d, out) {
|
|
93
|
+
const len = p.length;
|
|
94
|
+
if (p.loop)
|
|
95
|
+
d = ((d % len) + len) % len;
|
|
96
|
+
else
|
|
97
|
+
d = Math.min(len, Math.max(0, d));
|
|
98
|
+
let lo = 0;
|
|
99
|
+
let hi = p.segments - 1;
|
|
100
|
+
while (lo < hi) {
|
|
101
|
+
const mid = (lo + hi + 1) >> 1;
|
|
102
|
+
if (p.segStart[mid] <= d)
|
|
103
|
+
lo = mid;
|
|
104
|
+
else
|
|
105
|
+
hi = mid - 1;
|
|
106
|
+
}
|
|
107
|
+
const a = lo;
|
|
108
|
+
const b = (lo + 1) % p.xs.length;
|
|
109
|
+
const s0 = p.segStart[lo];
|
|
110
|
+
const t = (d - s0) / (p.segStart[lo + 1] - s0);
|
|
111
|
+
out[0] = p.xs[a] + (p.xs[b] - p.xs[a]) * t;
|
|
112
|
+
out[1] = p.zs[a] + (p.zs[b] - p.zs[a]) * t;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Position, heading and curvature at a distance along a path (mm), for extraction: `out` gets x, z (metres), the
|
|
116
|
+
* unit direction x, z taken between the points `look` mm behind and ahead, and the signed curvature in 1/m
|
|
117
|
+
* (positive bends right, clockwise seen from above). A trailing segment of a rig uses the same call a fixed
|
|
118
|
+
* distance further back.
|
|
119
|
+
*/
|
|
120
|
+
frameAt(p, d, look, out) {
|
|
121
|
+
const a = this.fa;
|
|
122
|
+
const b = this.fb;
|
|
123
|
+
const m = this.fm;
|
|
124
|
+
this.pointAt(p, d, m);
|
|
125
|
+
this.pointAt(p, d - look, a);
|
|
126
|
+
this.pointAt(p, d + look, b);
|
|
127
|
+
out[0] = m[0];
|
|
128
|
+
out[1] = m[1];
|
|
129
|
+
let dx = b[0] - a[0];
|
|
130
|
+
let dz = b[1] - a[1];
|
|
131
|
+
const l = Math.sqrt(dx * dx + dz * dz);
|
|
132
|
+
if (l > 1e-6) {
|
|
133
|
+
dx /= l;
|
|
134
|
+
dz /= l;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
dx = 0;
|
|
138
|
+
dz = -1;
|
|
139
|
+
}
|
|
140
|
+
out[2] = dx;
|
|
141
|
+
out[3] = dz;
|
|
142
|
+
// Turn between the chord behind and the chord ahead: 2·tan(θ/2) = 2·cross / (1 + dot) for unit chords, close to
|
|
143
|
+
// θ for the small angles a short look gives. Over the look distance it is the curvature.
|
|
144
|
+
const ux = m[0] - a[0], uz = m[1] - a[1];
|
|
145
|
+
const vx = b[0] - m[0], vz = b[1] - m[1];
|
|
146
|
+
const lu = Math.sqrt(ux * ux + uz * uz);
|
|
147
|
+
const lv = Math.sqrt(vx * vx + vz * vz);
|
|
148
|
+
if (lu < 1e-6 || lv < 1e-6) {
|
|
149
|
+
out[4] = 0;
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const cross = (ux * vz - uz * vx) / (lu * lv);
|
|
153
|
+
const dot = (ux * vx + uz * vz) / (lu * lv);
|
|
154
|
+
out[4] = dot > -0.99 ? (2 * cross) / (1 + dot) / ((lu + lv) * 0.5) : 0;
|
|
155
|
+
}
|
|
156
|
+
fa = [0, 0];
|
|
157
|
+
fb = [0, 0];
|
|
158
|
+
fm = [0, 0];
|
|
159
|
+
save(w) {
|
|
160
|
+
w.u32(this.nextId);
|
|
161
|
+
w.u32(this.stored.size);
|
|
162
|
+
for (const [id, st] of this.stored) {
|
|
163
|
+
w.u32(id);
|
|
164
|
+
w.u8((st.loop ? 1 : 0) | (st.owned ? 2 : 0));
|
|
165
|
+
for (const a of [st.points, st.limits, st.lanes, st.stopVertex, st.stopDwell]) {
|
|
166
|
+
w.u32(a.length);
|
|
167
|
+
w.bytes(a);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
load(r) {
|
|
172
|
+
this.paths.clear();
|
|
173
|
+
this.stored.clear();
|
|
174
|
+
this.nextId = r.u32();
|
|
175
|
+
const n = r.u32();
|
|
176
|
+
for (let i = 0; i < n; i++) {
|
|
177
|
+
const id = r.u32();
|
|
178
|
+
const flags = r.u8();
|
|
179
|
+
const f64 = () => {
|
|
180
|
+
const a = new Float64Array(r.u32());
|
|
181
|
+
r.into(a);
|
|
182
|
+
return a;
|
|
183
|
+
};
|
|
184
|
+
const i32 = () => {
|
|
185
|
+
const a = new Int32Array(r.u32());
|
|
186
|
+
r.into(a);
|
|
187
|
+
return a;
|
|
188
|
+
};
|
|
189
|
+
const points = f64();
|
|
190
|
+
const limits = i32();
|
|
191
|
+
const lanes = f64();
|
|
192
|
+
const stopVertex = i32();
|
|
193
|
+
const stopDwell = i32();
|
|
194
|
+
this.put(id, { points, loop: (flags & 1) !== 0, owned: (flags & 2) !== 0, limits, lanes, stopVertex, stopDwell });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|